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 UnitaryGate.repeat() method #12986

Merged
merged 14 commits into from
Aug 26, 2024
5 changes: 4 additions & 1 deletion qiskit/circuit/gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ def __pow__(self, exponent: float) -> "Gate":
return self.power(exponent)

def _return_repeat(self, exponent: float) -> "Gate":
return Gate(name=f"{self.name}*{exponent}", num_qubits=self.num_qubits, params=self.params)
gate = Gate(name=f"{self.name}*{exponent}", num_qubits=self.num_qubits, params=[])
gate.validate_parameter = self.validate_parameter
gate.params = self.params
return gate

def control(
self,
Expand Down
6 changes: 6 additions & 0 deletions releasenotes/notes/fix_11990-8551c7250207fc76.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
Fixes an error when calling the method :meth:`.UnitaryGate.repeat`.
Refer to `#11990 <https://github.com/Qiskit/qiskit/issues/11990>` for more
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs a trailing underscore (but @ElePT might know this better than me 🙂)

Suggested change
Refer to `#11990 <https://github.com/Qiskit/qiskit/issues/11990>` for more
Refer to `#11990 <https://github.com/Qiskit/qiskit/issues/11990>`__ for more

details.
6 changes: 6 additions & 0 deletions test/python/circuit/library/test_permutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ def test_inverse(self):
expected_inverse_perm = PermutationGate([3, 0, 5, 1, 4, 2])
self.assertTrue(np.array_equal(inverse_perm.pattern, expected_inverse_perm.pattern))

def test_repeat(self):
"""Test the ``repeat`` method."""
pattern = [2, 4, 1, 3, 0]
perm = PermutationGate(pattern)
self.assertTrue(np.allclose(Operator(perm.repeat(2)), Operator(perm) @ Operator(perm)))


class TestPermutationGatesOnCircuit(QiskitTestCase):
"""Tests for quantum circuits containing permutations."""
Expand Down
15 changes: 15 additions & 0 deletions test/python/circuit/test_isometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,21 @@ def test_isometry_inverse(self, iso):
result = Operator(qc)
np.testing.assert_array_almost_equal(result.data, np.identity(result.dim[0]))

@data(
np.eye(2, 2),
random_unitary(2, seed=297102).data,
np.eye(4, 4),
random_unitary(4, seed=123642).data,
random_unitary(8, seed=568288).data,
)
def test_isometry_repeat(self, iso):
"""Tests for the repeat of isometries from n to n qubits"""
iso_gate = Isometry(iso, 0, 0)

op = Operator(iso_gate)
op_double = Operator(iso_gate.repeat(2))
np.testing.assert_array_almost_equal(op @ op, op_double)


if __name__ == "__main__":
unittest.main()
7 changes: 7 additions & 0 deletions test/python/circuit/test_uc.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ def test_inverse_ucg(self):

self.assertTrue(np.allclose(unitary_desired, unitary))

def test_repeat(self):
"""test repeat operation"""
gates = [random_unitary(2, seed=seed).data for seed in [124435, 876345, 687462, 928365]]

uc = UCGate(gates, up_to_diagonal=False)
self.assertTrue(np.allclose(Operator(uc.repeat(2)), Operator(uc) @ Operator(uc)))


def _get_ucg_matrix(squs):
return block_diag(*squs)
Expand Down
5 changes: 5 additions & 0 deletions test/python/circuit/test_unitary.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ def test_adjoint(self):
uni = UnitaryGate([[0, 1j], [-1j, 0]])
self.assertTrue(numpy.array_equal(uni.adjoint().to_matrix(), uni.to_matrix()))

def test_repeat(self):
"""test repeat operation"""
uni = UnitaryGate([[1, 0], [0, 1j]])
self.assertTrue(numpy.array_equal(Operator(uni.repeat(2)), Operator(uni) @ Operator(uni)))


class TestUnitaryCircuit(QiskitTestCase):
"""Matrix gate circuit tests."""
Expand Down