|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 | """An immutable version of the Circuit data structure."""
|
15 |
| -from typing import TYPE_CHECKING, FrozenSet, Iterable, Iterator, Optional, Sequence, Tuple, Union |
| 15 | +from typing import TYPE_CHECKING, FrozenSet, Iterable, Iterator, Sequence, Tuple, Union |
16 | 16 |
|
17 | 17 | import numpy as np
|
18 | 18 |
|
19 |
| -from cirq import ops, protocols |
| 19 | +from cirq import protocols, _compat |
20 | 20 | from cirq.circuits import AbstractCircuit, Alignment, Circuit
|
21 | 21 | from cirq.circuits.insert_strategy import InsertStrategy
|
22 | 22 | from cirq.type_workarounds import NotImplementedType
|
@@ -51,81 +51,59 @@ def __init__(
|
51 | 51 | base = Circuit(contents, strategy=strategy)
|
52 | 52 | self._moments = tuple(base.moments)
|
53 | 53 |
|
54 |
| - # These variables are memoized when first requested. |
55 |
| - self._num_qubits: Optional[int] = None |
56 |
| - self._unitary: Optional[Union[np.ndarray, NotImplementedType]] = None |
57 |
| - self._qid_shape: Optional[Tuple[int, ...]] = None |
58 |
| - self._all_qubits: Optional[FrozenSet['cirq.Qid']] = None |
59 |
| - self._all_operations: Optional[Tuple[ops.Operation, ...]] = None |
60 |
| - self._has_measurements: Optional[bool] = None |
61 |
| - self._all_measurement_key_objs: Optional[FrozenSet['cirq.MeasurementKey']] = None |
62 |
| - self._are_all_measurements_terminal: Optional[bool] = None |
63 |
| - self._control_keys: Optional[FrozenSet['cirq.MeasurementKey']] = None |
64 |
| - |
65 | 54 | @property
|
66 | 55 | def moments(self) -> Sequence['cirq.Moment']:
|
67 | 56 | return self._moments
|
68 | 57 |
|
69 | 58 | def __hash__(self):
|
70 | 59 | return hash((self.moments,))
|
71 | 60 |
|
72 |
| - # Memoized methods for commonly-retrieved properties. |
73 |
| - |
| 61 | + @_compat.cached_method |
74 | 62 | def _num_qubits_(self) -> int:
|
75 |
| - if self._num_qubits is None: |
76 |
| - self._num_qubits = len(self.all_qubits()) |
77 |
| - return self._num_qubits |
| 63 | + return len(self.all_qubits()) |
78 | 64 |
|
| 65 | + @_compat.cached_method |
79 | 66 | def _qid_shape_(self) -> Tuple[int, ...]:
|
80 |
| - if self._qid_shape is None: |
81 |
| - self._qid_shape = super()._qid_shape_() |
82 |
| - return self._qid_shape |
| 67 | + return super()._qid_shape_() |
83 | 68 |
|
| 69 | + @_compat.cached_method |
84 | 70 | def _unitary_(self) -> Union[np.ndarray, NotImplementedType]:
|
85 |
| - if self._unitary is None: |
86 |
| - self._unitary = super()._unitary_() |
87 |
| - return self._unitary |
| 71 | + return super()._unitary_() |
88 | 72 |
|
| 73 | + @_compat.cached_method |
89 | 74 | def _is_measurement_(self) -> bool:
|
90 |
| - if self._has_measurements is None: |
91 |
| - self._has_measurements = protocols.is_measurement(self.unfreeze()) |
92 |
| - return self._has_measurements |
| 75 | + return protocols.is_measurement(self.unfreeze()) |
93 | 76 |
|
| 77 | + @_compat.cached_method |
94 | 78 | def all_qubits(self) -> FrozenSet['cirq.Qid']:
|
95 |
| - if self._all_qubits is None: |
96 |
| - self._all_qubits = super().all_qubits() |
97 |
| - return self._all_qubits |
| 79 | + return super().all_qubits() |
| 80 | + |
| 81 | + @_compat.cached_property |
| 82 | + def _all_operations(self) -> Tuple['cirq.Operation', ...]: |
| 83 | + return tuple(super().all_operations()) |
98 | 84 |
|
99 | 85 | def all_operations(self) -> Iterator['cirq.Operation']:
|
100 |
| - if self._all_operations is None: |
101 |
| - self._all_operations = tuple(super().all_operations()) |
102 | 86 | return iter(self._all_operations)
|
103 | 87 |
|
104 | 88 | def has_measurements(self) -> bool:
|
105 |
| - if self._has_measurements is None: |
106 |
| - self._has_measurements = super().has_measurements() |
107 |
| - return self._has_measurements |
| 89 | + return self._is_measurement_() |
108 | 90 |
|
| 91 | + @_compat.cached_method |
109 | 92 | def all_measurement_key_objs(self) -> FrozenSet['cirq.MeasurementKey']:
|
110 |
| - if self._all_measurement_key_objs is None: |
111 |
| - self._all_measurement_key_objs = super().all_measurement_key_objs() |
112 |
| - return self._all_measurement_key_objs |
| 93 | + return super().all_measurement_key_objs() |
113 | 94 |
|
114 | 95 | def _measurement_key_objs_(self) -> FrozenSet['cirq.MeasurementKey']:
|
115 | 96 | return self.all_measurement_key_objs()
|
116 | 97 |
|
| 98 | + @_compat.cached_method |
117 | 99 | def _control_keys_(self) -> FrozenSet['cirq.MeasurementKey']:
|
118 |
| - if self._control_keys is None: |
119 |
| - self._control_keys = super()._control_keys_() |
120 |
| - return self._control_keys |
| 100 | + return super()._control_keys_() |
121 | 101 |
|
| 102 | + @_compat.cached_method |
122 | 103 | def are_all_measurements_terminal(self) -> bool:
|
123 |
| - if self._are_all_measurements_terminal is None: |
124 |
| - self._are_all_measurements_terminal = super().are_all_measurements_terminal() |
125 |
| - return self._are_all_measurements_terminal |
126 |
| - |
127 |
| - # End of memoized methods. |
| 104 | + return super().are_all_measurements_terminal() |
128 | 105 |
|
| 106 | + @_compat.cached_method |
129 | 107 | def all_measurement_key_names(self) -> FrozenSet[str]:
|
130 | 108 | return frozenset(str(key) for key in self.all_measurement_key_objs())
|
131 | 109 |
|
|
0 commit comments