From c4fb3e045dcb606c605d49878e427abff2263532 Mon Sep 17 00:00:00 2001 From: Orion Martin <40585662+95-martin-orion@users.noreply.github.com> Date: Wed, 2 Mar 2022 09:38:04 -0800 Subject: [PATCH] use cached_property --- ...superconducting_qubits_noise_properties.py | 19 ++++++------------- ...conducting_qubits_noise_properties_test.py | 10 +++++----- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/cirq-core/cirq/devices/superconducting_qubits_noise_properties.py b/cirq-core/cirq/devices/superconducting_qubits_noise_properties.py index a9def858c450..9c572b47709b 100644 --- a/cirq-core/cirq/devices/superconducting_qubits_noise_properties.py +++ b/cirq-core/cirq/devices/superconducting_qubits_noise_properties.py @@ -16,7 +16,7 @@ from dataclasses import dataclass, field from typing import Dict, TYPE_CHECKING, List, Set -from cirq import ops, devices +from cirq import _compat, ops, devices from cirq.devices.noise_utils import ( OpIdentifier, decoherence_pauli_error, @@ -56,7 +56,6 @@ class SuperconductingQubitsNoiseProperties(devices.NoiseProperties, abc.ABC): validate: bool = True _qubits: List['cirq.Qid'] = field(init=False, default_factory=list) - _depolarizing_error: Dict[OpIdentifier, float] = field(init=False, default_factory=dict) def __post_init__(self): if not self.validate: @@ -133,13 +132,9 @@ def _get_pauli_error(self, p_error: float, op_id: OpIdentifier): p_error -= decoherence_pauli_error(self.t1_ns[q], self.tphi_ns[q], time_ns) return p_error - def _get_depolarizing_error(self) -> Dict[OpIdentifier, float]: - """Returns the portion of Pauli error from depolarization. - - The result of this method is memoized.""" - if self._depolarizing_error: - return self._depolarizing_error - + @_compat.cached_property + def _depolarizing_error(self) -> Dict[OpIdentifier, float]: + """Returns the portion of Pauli error from depolarization.""" depol_errors = {} for op_id, p_error in self.gate_pauli_errors.items(): gate_type = op_id.gate_type @@ -155,9 +150,7 @@ def _get_depolarizing_error(self) -> Dict[OpIdentifier, float]: f'but {op_id.qubits} were given.' ) depol_errors[op_id] = self._get_pauli_error(p_error, op_id) - # memoization is OK - self._depolarizing_error = depol_errors - return self._depolarizing_error + return depol_errors def build_noise_models(self) -> List['cirq.NoiseModel']: noise_models: List['cirq.NoiseModel'] = [] @@ -179,7 +172,7 @@ def build_noise_models(self) -> List['cirq.NoiseModel']: f'\nGates: {gate_types}\nSupported: {self.expected_gates()}' ) - depolarizing_error = self._get_depolarizing_error() + depolarizing_error = self._depolarizing_error added_pauli_errors = { op_id: ops.depolarize(p_error, len(op_id.qubits)).on(*op_id.qubits) for op_id, p_error in depolarizing_error.items() diff --git a/cirq-core/cirq/devices/superconducting_qubits_noise_properties_test.py b/cirq-core/cirq/devices/superconducting_qubits_noise_properties_test.py index 7b88898b6250..82f125df084d 100644 --- a/cirq-core/cirq/devices/superconducting_qubits_noise_properties_test.py +++ b/cirq-core/cirq/devices/superconducting_qubits_noise_properties_test.py @@ -170,8 +170,8 @@ def test_depol_memoization(): # Verify that depolarizing error is memoized. q0 = cirq.LineQubit(0) props = TestNoiseProperties(**default_props([q0], [])) - depol_error_a = props._get_depolarizing_error() - depol_error_b = props._get_depolarizing_error() + depol_error_a = props._depolarizing_error + depol_error_b = props._depolarizing_error assert depol_error_a == depol_error_b assert depol_error_a is depol_error_b @@ -188,7 +188,7 @@ def test_depol_validation(): validate=False, ) with pytest.raises(ValueError, match='takes 1 qubit'): - _ = z_2q_props._get_depolarizing_error() + _ = z_2q_props._depolarizing_error # Create unvalidated properties with an unsupported gate. toffoli_props = TestNoiseProperties( @@ -200,7 +200,7 @@ def test_depol_validation(): validate=False, ) with pytest.raises(ValueError, match='not in the supported gate list'): - _ = toffoli_props._get_depolarizing_error() + _ = toffoli_props._depolarizing_error # Create unvalidated properties with too many qubits on a CZ gate. cz_3q_props = TestNoiseProperties( @@ -212,7 +212,7 @@ def test_depol_validation(): validate=False, ) with pytest.raises(ValueError, match='takes 2 qubit'): - _ = cz_3q_props._get_depolarizing_error() + _ = cz_3q_props._depolarizing_error # If t1_ns is missing, values are filled in as needed.