From 3ecb40ba31bd0c6e323a6083af846bd152edd015 Mon Sep 17 00:00:00 2001 From: Shelly Garion <46566946+ShellyGarion@users.noreply.github.com> Date: Tue, 20 Aug 2024 16:13:37 +0300 Subject: [PATCH] Fix Initialize.gates_to_uncompute method (#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 (cherry picked from commit aa09a027bedeaa74f97771d1febc479d5d81d319) --- qiskit/circuit/library/data_preparation/initializer.py | 7 +++++-- .../circuit/library/data_preparation/state_preparation.py | 2 +- ...ix_initialize_gates_to_uncompute-d0dba6a642d07f30.yaml | 4 ++++ test/python/circuit/test_initializer.py | 8 ++++++++ 4 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 releasenotes/notes/fix_initialize_gates_to_uncompute-d0dba6a642d07f30.yaml 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."""