Skip to content

Commit aa09a02

Browse files
authored
Fix Initialize.gates_to_uncompute method (Qiskit#12976)
* add back files needed in Initialize.gates_to_uncompute() * add a test for Initialize.gates_to_uncompute() method * fix a comment * add release notes * update gates_to_uncompute such that it will call Isometry * remove unused imports * transfer code from StatePreparation to Initialize.gates_to_uncompute * update code following review
1 parent d430e58 commit aa09a02

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

qiskit/circuit/library/data_preparation/initializer.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from qiskit.circuit.quantumcircuit import QuantumCircuit
2222
from qiskit.circuit.quantumregister import QuantumRegister
2323
from qiskit.circuit.instruction import Instruction
24+
from qiskit.circuit.library.generalized_gates import Isometry
2425
from .state_preparation import StatePreparation
2526

2627
if typing.TYPE_CHECKING:
@@ -86,9 +87,11 @@ def gates_to_uncompute(self) -> QuantumCircuit:
8687
"""Call to create a circuit with gates that take the desired vector to zero.
8788
8889
Returns:
89-
Circuit to take ``self.params`` vector to :math:`|{00\\ldots0}\\rangle`
90+
QuantumCircuit: circuit to take ``self.params`` vector to :math:`|{00\\ldots0}\\rangle`
9091
"""
91-
return self._stateprep._gates_to_uncompute()
92+
93+
isom = Isometry(self.params, 0, 0)
94+
return isom._gates_to_uncompute()
9295

9396
@property
9497
def params(self):

qiskit/circuit/library/data_preparation/state_preparation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def _define_synthesis_isom(self):
174174
initialize_circuit = QuantumCircuit(q, name="init_def")
175175

176176
isom = Isometry(self.params, 0, 0)
177-
initialize_circuit.append(isom, q[:])
177+
initialize_circuit.compose(isom.definition, copy=False, inplace=True)
178178

179179
# invert the circuit to create the desired vector from zero (assuming
180180
# the qubits are in the zero state)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
Fix a bug that caused the method :meth:`Initialize.gates_to_uncompute()` fail.

test/python/circuit/test_initializer.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,14 @@ def test_mutating_params(self):
465465
self.assertEqual(decom_circ.data[2].operation.name, "state_preparation")
466466
self.assertEqual(decom_circ.data[2].operation.params, ["0", "0"])
467467

468+
def test_gates_to_uncompute(self):
469+
"""Test the gates_to_uncompute() method."""
470+
desired_vector = [0.5, 0.5, 0.5, 0.5]
471+
initialize = Initialize(desired_vector)
472+
qc = initialize.gates_to_uncompute().inverse()
473+
vec = Statevector(qc)
474+
self.assertTrue(vec == Statevector(desired_vector))
475+
468476

469477
class TestInstructionParam(QiskitTestCase):
470478
"""Test conversion of numpy type parameters."""

0 commit comments

Comments
 (0)