Skip to content

Commit 181d7aa

Browse files
authored
Check value equality first when comparing circuits (#6375)
Review: @dstrain115
1 parent 1961207 commit 181d7aa

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

cirq-core/cirq/circuits/circuit.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929
AbstractSet,
3030
Any,
3131
Callable,
32-
Mapping,
33-
MutableSequence,
3432
cast,
3533
Dict,
3634
FrozenSet,
3735
Iterable,
3836
Iterator,
3937
List,
38+
Mapping,
39+
MutableSequence,
4040
Optional,
4141
overload,
4242
Sequence,
@@ -58,9 +58,9 @@
5858
from cirq.circuits._bucket_priority_queue import BucketPriorityQueue
5959
from cirq.circuits.circuit_operation import CircuitOperation
6060
from cirq.circuits.insert_strategy import InsertStrategy
61+
from cirq.circuits.moment import Moment
6162
from cirq.circuits.qasm_output import QasmOutput
6263
from cirq.circuits.text_diagram_drawer import TextDiagramDrawer
63-
from cirq.circuits.moment import Moment
6464
from cirq.protocols import circuit_diagram_info_protocol
6565
from cirq.type_workarounds import NotImplementedType
6666

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

206-
def __bool__(self):
206+
def __bool__(self) -> bool:
207207
return bool(self.moments)
208208

209-
def __eq__(self, other):
209+
def __eq__(self, other) -> bool:
210210
if not isinstance(other, AbstractCircuit):
211211
return NotImplemented
212-
return tuple(self.moments) == tuple(other.moments)
212+
return other is self or (
213+
len(self.moments) == len(other.moments)
214+
and all(m0 == m1 for m0, m1 in zip(self.moments, other.moments))
215+
)
213216

214217
def _approx_eq_(self, other: Any, atol: Union[int, float]) -> bool:
215218
"""See `cirq.protocols.SupportsApproximateEquality`."""
216219
if not isinstance(other, AbstractCircuit):
217220
return NotImplemented
218-
return cirq.protocols.approx_eq(tuple(self.moments), tuple(other.moments), atol=atol)
221+
return other is self or cirq.protocols.approx_eq(
222+
tuple(self.moments), tuple(other.moments), atol=atol
223+
)
219224

220225
def __ne__(self, other) -> bool:
221226
return not self == other

0 commit comments

Comments
 (0)