36
36
37
37
import numpy as np
38
38
39
- from cirq import protocols , ops , qis
39
+ from cirq import protocols , ops , qis , _compat
40
40
from cirq ._import import LazyLoader
41
41
from cirq .ops import raw_types , op_tree
42
42
from cirq .protocols import circuit_diagram_info_protocol
@@ -148,8 +148,7 @@ def operation_at(self, qubit: raw_types.Qid) -> Optional['cirq.Operation']:
148
148
"""
149
149
if self .operates_on ([qubit ]):
150
150
return self .__getitem__ (qubit )
151
- else :
152
- return None
151
+ return None
153
152
154
153
def with_operation (self , operation : 'cirq.Operation' ) -> 'cirq.Moment' :
155
154
"""Returns an equal moment, but with the given op added.
@@ -238,9 +237,11 @@ def without_operations_touching(self, qubits: Iterable['cirq.Qid']) -> 'cirq.Mom
238
237
if qubits .isdisjoint (frozenset (operation .qubits ))
239
238
)
240
239
240
+ @_compat .cached_method ()
241
241
def _is_parameterized_ (self ) -> bool :
242
242
return any (protocols .is_parameterized (op ) for op in self )
243
243
244
+ @_compat .cached_method ()
244
245
def _parameter_names_ (self ) -> AbstractSet [str ]:
245
246
return {name for op in self for name in protocols .parameter_names (op )}
246
247
@@ -266,6 +267,7 @@ def _with_measurement_key_mapping_(self, key_map: Mapping[str, str]):
266
267
for op in self .operations
267
268
)
268
269
270
+ @_compat .cached_method ()
269
271
def _measurement_key_names_ (self ) -> FrozenSet [str ]:
270
272
return frozenset (str (key ) for key in self ._measurement_key_objs_ ())
271
273
@@ -333,6 +335,7 @@ def _approx_eq_(self, other: Any, atol: Union[int, float]) -> bool:
333
335
def __ne__ (self , other ) -> bool :
334
336
return not self == other
335
337
338
+ @_compat .cached_method ()
336
339
def __hash__ (self ):
337
340
return hash ((Moment , self ._sorted_operations_ ()))
338
341
@@ -406,6 +409,7 @@ def expand_to(self, qubits: Iterable['cirq.Qid']) -> 'cirq.Moment':
406
409
operations .append (ops .I (q ))
407
410
return Moment (* operations )
408
411
412
+ @_compat .cached_method ()
409
413
def _has_kraus_ (self ) -> bool :
410
414
"""Returns True if self has a Kraus representation and self uses <= 10 qubits."""
411
415
return all (protocols .has_kraus (op ) for op in self .operations ) and len (self .qubits ) <= 10
0 commit comments