Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Initialize.gates_to_uncompute method (backport #12976) #13000

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading