Skip to content

Commit 494e4bb

Browse files
authored
Implement __eq__ for remaining standard-library gates except mcx (#13327)
Most standard-library gates had a specialised `__eq__` added in gh-11370, but a small number were left over. This adds specialisation to almost all the stragglers, which were mostly old soft-deperecated gates. Despite not appearing in most circuits, these gates have an outsized effect on equality comparisons if they ever _do_ appear, because they involve construction of entire `definition` fields.
1 parent 54fe09b commit 494e4bb

6 files changed

Lines changed: 52 additions & 0 deletions

File tree

qiskit/circuit/library/standard_gates/p.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,11 @@ def control(
431431
def inverse(self, annotated: bool = False):
432432
r"""Return inverted MCPhase gate (:math:`MCPhase(\lambda)^{\dagger} = MCPhase(-\lambda)`)"""
433433
return MCPhaseGate(-self.params[0], self.num_ctrl_qubits)
434+
435+
def __eq__(self, other):
436+
return (
437+
isinstance(other, MCPhaseGate)
438+
and self.num_ctrl_qubits == other.num_ctrl_qubits
439+
and self.ctrl_state == other.ctrl_state
440+
and self._compare_parameters(other)
441+
)

qiskit/circuit/library/standard_gates/u.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,3 +393,10 @@ def __deepcopy__(self, memo=None):
393393
out = super().__deepcopy__(memo)
394394
out._params = _copy.deepcopy(out._params, memo)
395395
return out
396+
397+
def __eq__(self, other):
398+
return (
399+
isinstance(other, CUGate)
400+
and self.ctrl_state == other.ctrl_state
401+
and self._compare_parameters(other)
402+
)

qiskit/circuit/library/standard_gates/u1.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ def __array__(self, dtype=None, copy=None):
170170
lam = float(self.params[0])
171171
return numpy.array([[1, 0], [0, numpy.exp(1j * lam)]], dtype=dtype)
172172

173+
def __eq__(self, other):
174+
return isinstance(other, U1Gate) and self._compare_parameters(other)
175+
173176

174177
class CU1Gate(ControlledGate):
175178
r"""Controlled-U1 gate.
@@ -341,6 +344,13 @@ def __array__(self, dtype=None, copy=None):
341344
[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, eith, 0], [0, 0, 0, 1]], dtype=dtype
342345
)
343346

347+
def __eq__(self, other):
348+
return (
349+
isinstance(other, CU1Gate)
350+
and self.ctrl_state == other.ctrl_state
351+
and self._compare_parameters(other)
352+
)
353+
344354

345355
class MCU1Gate(ControlledGate):
346356
r"""Multi-controlled-U1 gate.
@@ -481,3 +491,11 @@ def inverse(self, annotated: bool = False):
481491
MCU1Gate: inverse gate.
482492
"""
483493
return MCU1Gate(-self.params[0], self.num_ctrl_qubits)
494+
495+
def __eq__(self, other):
496+
return (
497+
isinstance(other, MCU1Gate)
498+
and self.num_ctrl_qubits == other.num_ctrl_qubits
499+
and self.ctrl_state == other.ctrl_state
500+
and self._compare_parameters(other)
501+
)

qiskit/circuit/library/standard_gates/u2.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,6 @@ def __array__(self, dtype=None, copy=None):
144144
],
145145
dtype=dtype or complex,
146146
)
147+
148+
def __eq__(self, other):
149+
return isinstance(other, U2Gate) and self._compare_parameters(other)

qiskit/circuit/library/standard_gates/u3.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ def __array__(self, dtype=None, copy=None):
178178
dtype=dtype or complex,
179179
)
180180

181+
def __eq__(self, other):
182+
return isinstance(other, U3Gate) and self._compare_parameters(other)
183+
181184

182185
class CU3Gate(ControlledGate):
183186
r"""Controlled-U3 gate (3-parameter two-qubit gate).
@@ -368,6 +371,13 @@ def __array__(self, dtype=None, copy=None):
368371
dtype=dtype or complex,
369372
)
370373

374+
def __eq__(self, other):
375+
return (
376+
isinstance(other, CU3Gate)
377+
and self.ctrl_state == other.ctrl_state
378+
and self._compare_parameters(other)
379+
)
380+
371381

372382
def _generate_gray_code(num_bits):
373383
"""Generate the gray code for ``num_bits`` bits."""
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
features_circuits:
3+
- |
4+
Specialized implementations of :meth:`~object.__eq__` have been added for all standard-library circuit gates.
5+
Most of the standard gates already specialized this method, but a few did not, and could cause significant
6+
slowdowns in unexpected places.

0 commit comments

Comments
 (0)