Skip to content

Commit

Permalink
Fix Initialize.gates_to_uncompute method (#12976)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ShellyGarion authored Aug 20, 2024
1 parent d430e58 commit aa09a02
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
7 changes: 5 additions & 2 deletions qiskit/circuit/library/data_preparation/initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
Fix a bug that caused the method :meth:`Initialize.gates_to_uncompute()` fail.
8 changes: 8 additions & 0 deletions test/python/circuit/test_initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down

0 comments on commit aa09a02

Please sign in to comment.