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 #12976

Merged
merged 9 commits into from
Aug 20, 2024
14 changes: 14 additions & 0 deletions qiskit/circuit/library/data_preparation/state_preparation.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,20 @@ def validate_parameter(self, parameter):
def _return_repeat(self, exponent: float) -> "Gate":
return Gate(name=f"{self.name}*{exponent}", num_qubits=self.num_qubits, params=[])

### helper functions for initializer.gates_to_uncompute() method ###
ShellyGarion marked this conversation as resolved.
Show resolved Hide resolved

def _gates_to_uncompute(self):
"""Call to create a circuit with gates that take the desired vector to zero.
Returns:
QuantumCircuit: circuit to take self.params vector to :math:`|{00\\ldots0}\\rangle`
"""
q = QuantumRegister(self.num_qubits)
circuit = QuantumCircuit(q, name="disentangler")

isom = Isometry(self._params_arg, 0, 0)
circuit.append(isom, q[:])
return circuit.inverse()


class UniformSuperpositionGate(Gate):
r"""Implements a uniform superposition 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