Skip to content
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
8 changes: 8 additions & 0 deletions qiskit/circuit/library/standard_gates/p.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,11 @@ def control(
def inverse(self, annotated: bool = False):
r"""Return inverted MCPhase gate (:math:`MCPhase(\lambda)^{\dagger} = MCPhase(-\lambda)`)"""
return MCPhaseGate(-self.params[0], self.num_ctrl_qubits)

def __eq__(self, other):
return (
isinstance(other, MCPhaseGate)
and self.num_ctrl_qubits == other.num_ctrl_qubits
and self.ctrl_state == other.ctrl_state
and self._compare_parameters(other)
)
7 changes: 7 additions & 0 deletions qiskit/circuit/library/standard_gates/u.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,10 @@ def __deepcopy__(self, memo=None):
out = super().__deepcopy__(memo)
out._params = _copy.deepcopy(out._params, memo)
return out

def __eq__(self, other):
return (
isinstance(other, CUGate)
and self.ctrl_state == other.ctrl_state
and self._compare_parameters(other)
)
18 changes: 18 additions & 0 deletions qiskit/circuit/library/standard_gates/u1.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ def __array__(self, dtype=None, copy=None):
lam = float(self.params[0])
return numpy.array([[1, 0], [0, numpy.exp(1j * lam)]], dtype=dtype)

def __eq__(self, other):
return isinstance(other, U1Gate) and self._compare_parameters(other)


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

def __eq__(self, other):
return (
isinstance(other, CU1Gate)
and self.ctrl_state == other.ctrl_state
and self._compare_parameters(other)
)


class MCU1Gate(ControlledGate):
r"""Multi-controlled-U1 gate.
Expand Down Expand Up @@ -481,3 +491,11 @@ def inverse(self, annotated: bool = False):
MCU1Gate: inverse gate.
"""
return MCU1Gate(-self.params[0], self.num_ctrl_qubits)

def __eq__(self, other):
return (
isinstance(other, MCU1Gate)
and self.num_ctrl_qubits == other.num_ctrl_qubits
and self.ctrl_state == other.ctrl_state
and self._compare_parameters(other)
)
3 changes: 3 additions & 0 deletions qiskit/circuit/library/standard_gates/u2.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,6 @@ def __array__(self, dtype=None, copy=None):
],
dtype=dtype or complex,
)

def __eq__(self, other):
return isinstance(other, U2Gate) and self._compare_parameters(other)
10 changes: 10 additions & 0 deletions qiskit/circuit/library/standard_gates/u3.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ def __array__(self, dtype=None, copy=None):
dtype=dtype or complex,
)

def __eq__(self, other):
return isinstance(other, U3Gate) and self._compare_parameters(other)


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

def __eq__(self, other):
return (
isinstance(other, CU3Gate)
and self.ctrl_state == other.ctrl_state
and self._compare_parameters(other)
)


def _generate_gray_code(num_bits):
"""Generate the gray code for ``num_bits`` bits."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
features_circuits:
- |
Specialized implementations of :meth:`~object.__eq__` have been added for all standard-library circuit gates.
Most of the standard gates already specialized this method, but a few did not, and could cause significant
slowdowns in unexpected places.