Skip to content

Commit

Permalink
Check identity first when comparing moments (#6388)
Browse files Browse the repository at this point in the history
  • Loading branch information
maffoo authored Dec 20, 2023
1 parent a2530fe commit 99993b9
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions cirq-core/cirq/circuits/moment.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,14 +343,14 @@ def __eq__(self, other) -> bool:
if not isinstance(other, type(self)):
return NotImplemented

return self._sorted_operations_() == other._sorted_operations_()
return self is other or self._sorted_operations_() == other._sorted_operations_()

def _approx_eq_(self, other: Any, atol: Union[int, float]) -> bool:
"""See `cirq.protocols.SupportsApproximateEquality`."""
if not isinstance(other, type(self)):
return NotImplemented

return protocols.approx_eq(
return self is other or protocols.approx_eq(
self._sorted_operations_(), other._sorted_operations_(), atol=atol
)

Expand Down Expand Up @@ -510,13 +510,11 @@ def _from_json_dict_(cls, operations, **kwargs):
return cls.from_ops(*operations)

def __add__(self, other: 'cirq.OP_TREE') -> 'cirq.Moment':

if isinstance(other, circuits.AbstractCircuit):
return NotImplemented # Delegate to Circuit.__radd__.
return self.with_operations(other)

def __sub__(self, other: 'cirq.OP_TREE') -> 'cirq.Moment':

must_remove = set(op_tree.flatten_to_ops(other))
new_ops = []
for op in self.operations:
Expand Down

0 comments on commit 99993b9

Please sign in to comment.