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

Consider unit in Delay comparisons (backport #13816) #13827

Merged
merged 1 commit into from
Feb 11, 2025
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
5 changes: 5 additions & 0 deletions qiskit/circuit/delay.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ def to_matrix(self) -> np.ndarray:
"""
return self.__array__(dtype=complex)

def __eq__(self, other):
return (
isinstance(other, Delay) and self.unit == other.unit and self._compare_parameters(other)
)

def __repr__(self):
"""Return the official string representing the delay."""
return f"{self.__class__.__name__}(duration={self.params[0]}[unit={self.unit}])"
Expand Down
5 changes: 5 additions & 0 deletions releasenotes/notes/delay-compare-b7ecb26b94ff0cd3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
Comparisons of :class:`~.circuit.Delay` instructions, including within circuits, now require the
units to be equal as well as the duration value.
37 changes: 37 additions & 0 deletions test/python/circuit/test_delay.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,43 @@ def test_to_matrix_return_identity_matrix(self):
expected = np.array([[1, 0], [0, 1]], dtype=complex)
self.assertTrue(np.array_equal(actual, expected))

def test_equality(self):
# At the time `__eq__` was specialised for `Delay`, the class was undergoing changes and
# moving to Rust, so we didn't also modify the Python-space semantics to declare equality
# between (say) 1000ms and 1s. We could revisit that decision once the data model settles.
#
# This test then deliberately doesn't assert about mixed-scale comparisons, only comparisons
# between the same units, and 'dt' to absolute times.
def circuit_from(delay):
out = QuantumCircuit(1)
out.append(delay, [0], [])
return out

a = Parameter("a")
left_instructions, right_instructions = [], []
left_circuits, right_circuits = [], []
for unit in ("s", "ms", "us", "ns", "ps", "dt"):
for base in (left_instructions, right_instructions):
base.append(Delay(1, unit))
base.append(Delay(5.0, unit))
base.append(Delay(a, unit))
for base in (left_circuits, right_circuits):
base.append(circuit_from(Delay(1, unit)))
base.append(circuit_from(Delay(5.0, unit)))
base.append(circuit_from(Delay(a, unit)))
self.assertEqual(left_instructions, right_instructions)
self.assertEqual(left_circuits, right_circuits)

# We can't do all the non-equal tests in a single list comparison, since any single list
# failure would mask any spurious successes.
for unit in ("s", "ms", "us", "ns", "ps"):
self.assertNotEqual(Delay(2, unit), Delay(2, "dt"))
self.assertNotEqual(circuit_from(Delay(2, unit)), circuit_from(Delay(2, "dt")))
self.assertNotEqual(Delay(2, "dt"), Delay(2, unit))
self.assertNotEqual(circuit_from(Delay(2, "dt")), circuit_from(Delay(2, unit)))
self.assertNotEqual(Delay(a, unit), Delay(a, "dt"))
self.assertNotEqual(circuit_from(Delay(a, unit)), circuit_from(Delay(a, "dt")))


class TestParameterizedDelay(QiskitTestCase):
"""Test delay instruction with parameterized duration."""
Expand Down