diff --git a/qiskit/circuit/library/data_preparation/initializer.py b/qiskit/circuit/library/data_preparation/initializer.py index 0e38f067403c..984c7c0d310a 100644 --- a/qiskit/circuit/library/data_preparation/initializer.py +++ b/qiskit/circuit/library/data_preparation/initializer.py @@ -21,6 +21,7 @@ from qiskit.circuit.quantumcircuit import QuantumCircuit from qiskit.circuit.quantumregister import QuantumRegister from qiskit.circuit.instruction import Instruction +from qiskit.circuit.library.generalized_gates import Isometry from .state_preparation import StatePreparation if typing.TYPE_CHECKING: @@ -86,9 +87,11 @@ def gates_to_uncompute(self) -> QuantumCircuit: """Call to create a circuit with gates that take the desired vector to zero. Returns: - Circuit to take ``self.params`` vector to :math:`|{00\\ldots0}\\rangle` + QuantumCircuit: circuit to take ``self.params`` vector to :math:`|{00\\ldots0}\\rangle` """ - return self._stateprep._gates_to_uncompute() + + isom = Isometry(self.params, 0, 0) + return isom._gates_to_uncompute() @property def params(self): diff --git a/qiskit/circuit/library/data_preparation/state_preparation.py b/qiskit/circuit/library/data_preparation/state_preparation.py index df69f9eab8c5..52006a8bb378 100644 --- a/qiskit/circuit/library/data_preparation/state_preparation.py +++ b/qiskit/circuit/library/data_preparation/state_preparation.py @@ -174,7 +174,7 @@ def _define_synthesis_isom(self): initialize_circuit = QuantumCircuit(q, name="init_def") isom = Isometry(self.params, 0, 0) - initialize_circuit.append(isom, q[:]) + initialize_circuit.compose(isom.definition, copy=False, inplace=True) # invert the circuit to create the desired vector from zero (assuming # the qubits are in the zero state) diff --git a/releasenotes/notes/fix_initialize_gates_to_uncompute-d0dba6a642d07f30.yaml b/releasenotes/notes/fix_initialize_gates_to_uncompute-d0dba6a642d07f30.yaml new file mode 100644 index 000000000000..3c24569455e2 --- /dev/null +++ b/releasenotes/notes/fix_initialize_gates_to_uncompute-d0dba6a642d07f30.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - | + Fix a bug that caused the method :meth:`Initialize.gates_to_uncompute()` fail. diff --git a/test/python/circuit/test_initializer.py b/test/python/circuit/test_initializer.py index a37c51f48184..2507fbc6c139 100644 --- a/test/python/circuit/test_initializer.py +++ b/test/python/circuit/test_initializer.py @@ -465,6 +465,14 @@ def test_mutating_params(self): self.assertEqual(decom_circ.data[2].operation.name, "state_preparation") self.assertEqual(decom_circ.data[2].operation.params, ["0", "0"]) + def test_gates_to_uncompute(self): + """Test the gates_to_uncompute() method.""" + desired_vector = [0.5, 0.5, 0.5, 0.5] + initialize = Initialize(desired_vector) + qc = initialize.gates_to_uncompute().inverse() + vec = Statevector(qc) + self.assertTrue(vec == Statevector(desired_vector)) + class TestInstructionParam(QiskitTestCase): """Test conversion of numpy type parameters."""