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

Check identity first when comparing circuits #6375

Merged
merged 2 commits into from
Dec 19, 2023
Merged
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
19 changes: 12 additions & 7 deletions cirq-core/cirq/circuits/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
AbstractSet,
Any,
Callable,
Mapping,
MutableSequence,
cast,
Dict,
FrozenSet,
Iterable,
Iterator,
List,
Mapping,
MutableSequence,
Optional,
overload,
Sequence,
Expand All @@ -58,9 +58,9 @@
from cirq.circuits._bucket_priority_queue import BucketPriorityQueue
from cirq.circuits.circuit_operation import CircuitOperation
from cirq.circuits.insert_strategy import InsertStrategy
from cirq.circuits.moment import Moment
from cirq.circuits.qasm_output import QasmOutput
from cirq.circuits.text_diagram_drawer import TextDiagramDrawer
from cirq.circuits.moment import Moment
from cirq.protocols import circuit_diagram_info_protocol
from cirq.type_workarounds import NotImplementedType

Expand Down Expand Up @@ -203,19 +203,24 @@ def unfreeze(self, copy: bool = True) -> 'cirq.Circuit':
copy: If True and 'self' is a Circuit, returns a copy that circuit.
"""

def __bool__(self):
def __bool__(self) -> bool:
return bool(self.moments)

def __eq__(self, other):
def __eq__(self, other) -> bool:
if not isinstance(other, AbstractCircuit):
return NotImplemented
return tuple(self.moments) == tuple(other.moments)
return other is self or (
len(self.moments) == len(other.moments)
and all(m0 == m1 for m0, m1 in zip(self.moments, other.moments))
)

def _approx_eq_(self, other: Any, atol: Union[int, float]) -> bool:
"""See `cirq.protocols.SupportsApproximateEquality`."""
if not isinstance(other, AbstractCircuit):
return NotImplemented
return cirq.protocols.approx_eq(tuple(self.moments), tuple(other.moments), atol=atol)
return other is self or cirq.protocols.approx_eq(
tuple(self.moments), tuple(other.moments), atol=atol
)

def __ne__(self, other) -> bool:
return not self == other
Expand Down
Loading