Skip to content

Commit

Permalink
use cached_property
Browse files Browse the repository at this point in the history
  • Loading branch information
95-martin-orion committed Mar 2, 2022
1 parent 335379e commit c4fb3e0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
19 changes: 6 additions & 13 deletions cirq-core/cirq/devices/superconducting_qubits_noise_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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'] = []
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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.

Expand Down

0 comments on commit c4fb3e0

Please sign in to comment.