Skip to content

Commit 968ed4c

Browse files
authored
Remove cached_property backport (#6398)
We require recent-enough Python which has `functools.cached_property`. Fixes #6395
1 parent 4d35768 commit 968ed4c

35 files changed

+52
-69
lines changed

cirq-core/cirq/_compat.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,6 @@ def with_debug(value: bool) -> Iterator[None]:
6161
__cirq_debug__.reset(token)
6262

6363

64-
try:
65-
from functools import cached_property # pylint: disable=unused-import
66-
except ImportError:
67-
from backports.cached_property import cached_property # type: ignore[no-redef]
68-
69-
7064
# Sentinel used by wrapped_no_args below when method has not yet been cached.
7165
_NOT_FOUND = object()
7266

cirq-core/cirq/_compat_test.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
from cirq._compat import (
3939
block_overlapping_deprecation,
4040
cached_method,
41-
cached_property,
4241
proper_repr,
4342
dataclass_repr,
4443
deprecated,
@@ -1011,23 +1010,6 @@ def f(x):
10111010
f(5)
10121011

10131012

1014-
def test_cached_property():
1015-
class Foo:
1016-
def __init__(self):
1017-
self.bar_calls = 0
1018-
1019-
@cached_property
1020-
def bar(self):
1021-
self.bar_calls += 1
1022-
return []
1023-
1024-
foo = Foo()
1025-
bar = foo.bar
1026-
bar2 = foo.bar
1027-
assert bar2 is bar
1028-
assert foo.bar_calls == 1
1029-
1030-
10311013
class Bar:
10321014
def __init__(self) -> None:
10331015
self.foo_calls: Dict[int, int] = collections.Counter()

cirq-core/cirq/circuits/circuit_operation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
component operations in order, including any nested CircuitOperations.
2020
"""
2121
import math
22+
from functools import cached_property
2223
from typing import (
2324
Callable,
2425
cast,
@@ -38,7 +39,7 @@
3839
import sympy
3940

4041
from cirq import circuits, ops, protocols, value, study
41-
from cirq._compat import cached_property, proper_repr
42+
from cirq._compat import proper_repr
4243

4344
if TYPE_CHECKING:
4445
import cirq

cirq-core/cirq/circuits/frozen_circuit.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
"""An immutable version of the Circuit data structure."""
15+
from functools import cached_property
1516
from typing import (
1617
AbstractSet,
1718
FrozenSet,
@@ -90,7 +91,7 @@ def tags(self) -> Tuple[Hashable, ...]:
9091
"""Returns a tuple of the Circuit's tags."""
9192
return self._tags
9293

93-
@_compat.cached_property
94+
@cached_property
9495
def untagged(self) -> 'cirq.FrozenCircuit':
9596
"""Returns the underlying FrozenCircuit without any tags."""
9697
return self._from_moments(self._moments) if self.tags else self
@@ -148,7 +149,7 @@ def _is_measurement_(self) -> bool:
148149
def all_qubits(self) -> FrozenSet['cirq.Qid']:
149150
return super().all_qubits()
150151

151-
@_compat.cached_property
152+
@cached_property
152153
def _all_operations(self) -> Tuple['cirq.Operation', ...]:
153154
return tuple(super().all_operations())
154155

cirq-core/cirq/devices/superconducting_qubits_noise_properties.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717

1818
import abc
1919
from dataclasses import dataclass, field
20+
from functools import cached_property
2021
from typing import Dict, TYPE_CHECKING, List, Set, Type
2122

22-
from cirq import _compat, ops, devices
23+
from cirq import ops, devices
2324
from cirq.devices import noise_utils
2425

2526
if TYPE_CHECKING:
@@ -131,7 +132,7 @@ def _get_pauli_error(self, p_error: float, op_id: noise_utils.OpIdentifier):
131132
p_error -= noise_utils.decoherence_pauli_error(self.t1_ns[q], self.tphi_ns[q], time_ns)
132133
return p_error
133134

134-
@_compat.cached_property
135+
@cached_property
135136
def _depolarizing_error(self) -> Dict[noise_utils.OpIdentifier, float]:
136137
"""Returns the portion of Pauli error from depolarization."""
137138
depol_errors = {}

cirq-core/cirq/ops/clifford_gate.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from cirq import protocols, value, linalg, qis
2222
from cirq._import import LazyLoader
23-
from cirq._compat import cached_property, cached_method
23+
from cirq._compat import cached_method
2424
from cirq.ops import common_gates, named_qubit, raw_types, pauli_gates, phased_x_z_gate
2525
from cirq.ops.pauli_gates import Pauli
2626
from cirq.type_workarounds import NotImplementedType
@@ -693,7 +693,7 @@ def to_phased_xz_gate(self) -> phased_x_z_gate.PhasedXZGate:
693693
"""
694694
return self._to_phased_xz_gate
695695

696-
@cached_property
696+
@functools.cached_property
697697
def _to_phased_xz_gate(self) -> phased_x_z_gate.PhasedXZGate:
698698
x_to_flip, z_to_flip = self.clifford_tableau.rs
699699
flip_index = int(z_to_flip) * 2 + x_to_flip
@@ -792,7 +792,7 @@ def _has_unitary_(self) -> bool:
792792
def _unitary_(self) -> np.ndarray:
793793
return self._unitary
794794

795-
@cached_property
795+
@functools.cached_property
796796
def _unitary(self) -> np.ndarray:
797797
mat = np.eye(2)
798798
qubit = named_qubit.NamedQubit('arbitrary')
@@ -810,7 +810,7 @@ def decompose_gate(self) -> Sequence['cirq.Gate']:
810810
"""
811811
return self._decompose_gate
812812

813-
@cached_property
813+
@functools.cached_property
814814
def _decompose_gate(self) -> Sequence['cirq.Gate']:
815815
if self == SingleQubitCliffordGate.H:
816816
return [common_gates.H]
@@ -829,7 +829,7 @@ def decompose_rotation(self) -> Sequence[Tuple[Pauli, int]]:
829829
"""
830830
return self._decompose_rotation
831831

832-
@cached_property
832+
@functools.cached_property
833833
def _decompose_rotation(self) -> Sequence[Tuple[Pauli, int]]:
834834
x_rot = self.pauli_tuple(pauli_gates.X)
835835
y_rot = self.pauli_tuple(pauli_gates.Y)
@@ -926,7 +926,7 @@ def _circuit_diagram_info_(
926926
def _value_equality_values_(self):
927927
return self._value_equality_values
928928

929-
@cached_property
929+
@functools.cached_property
930930
def _value_equality_values(self):
931931
return self._clifford_tableau.matrix().tobytes() + self._clifford_tableau.rs.tobytes()
932932

cirq-core/cirq/ops/control_values.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import abc
15+
from functools import cached_property
1516
from typing import Collection, Tuple, TYPE_CHECKING, Any, Dict, Iterator, Optional, Sequence, Union
1617
import itertools
1718

18-
from cirq import protocols, value, _compat
19+
from cirq import protocols, value
1920

2021
if TYPE_CHECKING:
2122
import cirq
@@ -144,7 +145,7 @@ def __init__(self, data: Sequence[Union[int, Collection[int]]]):
144145
(cv,) if isinstance(cv, int) else tuple(sorted(set(cv))) for cv in data
145146
)
146147

147-
@_compat.cached_property
148+
@cached_property
148149
def is_trivial(self) -> bool:
149150
return self._qubit_sums == ((1,),) * self._num_qubits_()
150151

@@ -252,7 +253,7 @@ def __init__(self, data: Collection[Sequence[int]], *, name: Optional[str] = Non
252253
if not all(len(p) == num_qubits for p in self._conjunctions):
253254
raise ValueError(f'Each term of {self._conjunctions} should be of length {num_qubits}.')
254255

255-
@_compat.cached_property
256+
@cached_property
256257
def is_trivial(self) -> bool:
257258
return self._conjunctions == ((1,) * self._num_qubits_(),)
258259

cirq-core/cirq/sim/state_vector_simulator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"""Abstract classes for simulations which keep track of state vector."""
1515

1616
import abc
17+
from functools import cached_property
1718
from typing import Any, Dict, Iterator, Sequence, Type, TYPE_CHECKING, Generic, TypeVar
1819

1920
import numpy as np
@@ -121,7 +122,7 @@ def __init__(
121122
qubit_map=final_simulator_state.qubit_map,
122123
)
123124

124-
@_compat.cached_property
125+
@cached_property
125126
def final_state_vector(self) -> np.ndarray:
126127
return self._get_merged_sim_state().target_tensor.reshape(-1)
127128

cirq-ft/cirq_ft/algos/and_gate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from functools import cached_property
1516
from typing import Sequence, Tuple
1617

1718
import numpy as np
1819
from numpy.typing import NDArray
1920

2021
import attr
2122
import cirq
22-
from cirq._compat import cached_property
2323
from cirq_ft import infra
2424

2525

cirq-ft/cirq_ft/algos/apply_gate_to_lth_target.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from functools import cached_property
1516
import itertools
1617
from typing import Callable, Sequence, Tuple
1718

1819
import attr
1920
import cirq
2021
import numpy as np
21-
from cirq._compat import cached_property
2222
from cirq_ft import infra
2323
from cirq_ft.algos import unary_iteration_gate
2424

0 commit comments

Comments
 (0)