Skip to content

Commit

Permalink
redo
Browse files Browse the repository at this point in the history
  • Loading branch information
NoureldinYosri committed Feb 8, 2024
1 parent dcc02cf commit b91f1fd
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 330 deletions.
11 changes: 1 addition & 10 deletions cirq-core/cirq/devices/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,4 @@
SuperconductingQubitsNoiseProperties,
)

from cirq.devices.noise_utils import (
OpIdentifier,
decay_constant_to_xeb_fidelity,
decay_constant_to_pauli_error,
pauli_error_to_decay_constant,
xeb_fidelity_to_decay_constant,
pauli_error_from_t1,
average_error,
decoherence_pauli_error,
)
from cirq.devices.noise_utils import OpIdentifier
109 changes: 0 additions & 109 deletions cirq-core/cirq/devices/noise_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.

from typing import TYPE_CHECKING, Any, Dict, Tuple, Type, Union
import numpy as np

from cirq import ops, protocols, value
from cirq._compat import proper_repr
Expand Down Expand Up @@ -95,111 +94,3 @@ def _from_json_dict_(cls, gate_type, qubits, **kwargs) -> 'OpIdentifier':
if isinstance(gate_type, str):
gate_type = protocols.cirq_type_from_json(gate_type)
return cls(gate_type, *qubits)


# TODO: expose all from top-level cirq?
def decay_constant_to_xeb_fidelity(decay_constant: float, num_qubits: int = 2) -> float:
"""Calculates the XEB fidelity from the depolarization decay constant.
Args:
decay_constant: Depolarization decay constant.
num_qubits: Number of qubits.
Returns:
Calculated XEB fidelity.
"""
N = 2**num_qubits
return 1 - ((1 - decay_constant) * (1 - 1 / N))


def decay_constant_to_pauli_error(decay_constant: float, num_qubits: int = 1) -> float:
"""Calculates pauli error from the depolarization decay constant.
Args:
decay_constant: Depolarization decay constant.
num_qubits: Number of qubits.
Returns:
Calculated Pauli error.
"""
N = 2**num_qubits
return (1 - decay_constant) * (1 - 1 / N / N)


def pauli_error_to_decay_constant(pauli_error: float, num_qubits: int = 1) -> float:
"""Calculates depolarization decay constant from pauli error.
Args:
pauli_error: The pauli error.
num_qubits: Number of qubits.
Returns:
Calculated depolarization decay constant.
"""
N = 2**num_qubits
return 1 - (pauli_error / (1 - 1 / N / N))


def xeb_fidelity_to_decay_constant(xeb_fidelity: float, num_qubits: int = 2) -> float:
"""Calculates the depolarization decay constant from XEB fidelity.
Args:
xeb_fidelity: The XEB fidelity.
num_qubits: Number of qubits.
Returns:
Calculated depolarization decay constant.
"""
N = 2**num_qubits
return 1 - (1 - xeb_fidelity) / (1 - 1 / N)


def pauli_error_from_t1(t_ns: float, t1_ns: float) -> float:
"""Calculates the pauli error from T1 decay constant.
This computes error for a specific duration, `t`.
Args:
t_ns: The duration of the gate in ns.
t1_ns: The T1 decay constant in ns.
Returns:
Calculated Pauli error resulting from T1 decay.
"""
t2 = 2 * t1_ns
return (1 - np.exp(-t_ns / t2)) / 2 + (1 - np.exp(-t_ns / t1_ns)) / 4


def average_error(decay_constant: float, num_qubits: int = 1) -> float:
"""Calculates the average error from the depolarization decay constant.
Args:
decay_constant: Depolarization decay constant.
num_qubits: Number of qubits.
Returns:
Calculated average error.
"""
N = 2**num_qubits
return (1 - decay_constant) * (1 - 1 / N)


def decoherence_pauli_error(t1_ns: float, tphi_ns: float, gate_time_ns: float) -> float:
"""The component of Pauli error caused by decoherence on a single qubit.
Args:
t1_ns: T1 time in nanoseconds.
tphi_ns: Tphi time in nanoseconds.
gate_time_ns: Duration in nanoseconds of the gate affected by this error.
Returns:
Calculated Pauli error resulting from decoherence.
"""
gamma_2 = (1 / (2 * t1_ns)) + 1 / tphi_ns

exp1 = np.exp(-gate_time_ns / t1_ns)
exp2 = np.exp(-gate_time_ns * gamma_2)
px = 0.25 * (1 - exp1)
py = px
pz = 0.5 * (1 - exp2) - px
return px + py + pz
84 changes: 1 addition & 83 deletions cirq-core/cirq/devices/noise_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import numpy as np
import pytest

import cirq
from cirq.devices.noise_utils import (
OpIdentifier,
decay_constant_to_xeb_fidelity,
decay_constant_to_pauli_error,
pauli_error_to_decay_constant,
xeb_fidelity_to_decay_constant,
pauli_error_from_t1,
average_error,
decoherence_pauli_error,
)
from cirq.devices.noise_utils import OpIdentifier


def test_op_identifier():
Expand Down Expand Up @@ -67,74 +56,3 @@ def test_op_id_instance():
gate = cirq.SingleQubitCliffordGate.from_xz_map((cirq.X, False), (cirq.Z, False))
op_id = OpIdentifier(gate, q0)
cirq.testing.assert_equivalent_repr(op_id)


@pytest.mark.parametrize(
'decay_constant,num_qubits,expected_output',
[(0.01, 1, 1 - (0.99 * 1 / 2)), (0.05, 2, 1 - (0.95 * 3 / 4))],
)
def test_decay_constant_to_xeb_fidelity(decay_constant, num_qubits, expected_output):
val = decay_constant_to_xeb_fidelity(decay_constant, num_qubits)
assert val == expected_output


@pytest.mark.parametrize(
'decay_constant,num_qubits,expected_output',
[(0.01, 1, 0.99 * 3 / 4), (0.05, 2, 0.95 * 15 / 16)],
)
def test_decay_constant_to_pauli_error(decay_constant, num_qubits, expected_output):
val = decay_constant_to_pauli_error(decay_constant, num_qubits)
assert val == expected_output


@pytest.mark.parametrize(
'pauli_error,num_qubits,expected_output',
[(0.01, 1, 1 - (0.01 / (3 / 4))), (0.05, 2, 1 - (0.05 / (15 / 16)))],
)
def test_pauli_error_to_decay_constant(pauli_error, num_qubits, expected_output):
val = pauli_error_to_decay_constant(pauli_error, num_qubits)
assert val == expected_output


@pytest.mark.parametrize(
'xeb_fidelity,num_qubits,expected_output',
[(0.01, 1, 1 - 0.99 / (1 / 2)), (0.05, 2, 1 - 0.95 / (3 / 4))],
)
def test_xeb_fidelity_to_decay_constant(xeb_fidelity, num_qubits, expected_output):
val = xeb_fidelity_to_decay_constant(xeb_fidelity, num_qubits)
assert val == expected_output


@pytest.mark.parametrize(
't,t1_ns,expected_output',
[
(20, 1e5, (1 - np.exp(-20 / 2e5)) / 2 + (1 - np.exp(-20 / 1e5)) / 4),
(4000, 1e4, (1 - np.exp(-4000 / 2e4)) / 2 + (1 - np.exp(-4000 / 1e4)) / 4),
],
)
def test_pauli_error_from_t1(t, t1_ns, expected_output):
val = pauli_error_from_t1(t, t1_ns)
assert val == expected_output


@pytest.mark.parametrize(
'decay_constant,num_qubits,expected_output', [(0.01, 1, 0.99 * 1 / 2), (0.05, 2, 0.95 * 3 / 4)]
)
def test_average_error(decay_constant, num_qubits, expected_output):
val = average_error(decay_constant, num_qubits)
assert val == expected_output


@pytest.mark.parametrize(
'T1_ns,Tphi_ns,gate_time_ns', [(1e4, 2e4, 25), (1e5, 2e3, 25), (1e4, 2e4, 4000)]
)
def test_decoherence_pauli_error(T1_ns, Tphi_ns, gate_time_ns):
val = decoherence_pauli_error(T1_ns, Tphi_ns, gate_time_ns)
# Expected value is of the form:
#
# (1/4) * [1 - e^(-t/T1)] + (1/2) * [1 - e^(-t/(2*T1) - t/Tphi]
#
expected_output = 0.25 * (1 - np.exp(-gate_time_ns / T1_ns)) + 0.5 * (
1 - np.exp(-gate_time_ns * ((1 / (2 * T1_ns)) + 1 / Tphi_ns))
)
assert val == expected_output
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from functools import cached_property
from typing import Dict, TYPE_CHECKING, List, Set, Type

from cirq import ops, devices
from cirq import ops, devices, qis
from cirq.devices import noise_utils

if TYPE_CHECKING:
Expand Down Expand Up @@ -129,7 +129,7 @@ def expected_gates(cls) -> Set[Type[ops.Gate]]:
def _get_pauli_error(self, p_error: float, op_id: noise_utils.OpIdentifier):
time_ns = float(self.gate_times_ns[op_id.gate_type])
for q in op_id.qubits:
p_error -= noise_utils.decoherence_pauli_error(self.t1_ns[q], self.tphi_ns[q], time_ns)
p_error -= qis.decoherence_pauli_error(self.t1_ns[q], self.tphi_ns[q], time_ns)
return p_error

@cached_property
Expand Down
10 changes: 10 additions & 0 deletions cirq-core/cirq/qis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,13 @@
validate_qid_shape,
validate_normalized_state_vector,
)

from cirq.qis.noise_utils import (
decay_constant_to_xeb_fidelity,
decay_constant_to_pauli_error,
pauli_error_to_decay_constant,
xeb_fidelity_to_decay_constant,
pauli_error_from_t1,
average_error,
decoherence_pauli_error,
)
82 changes: 0 additions & 82 deletions cirq-core/cirq/qis/noise_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,90 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import TYPE_CHECKING, Any, Dict, Tuple, Type, Union
import numpy as np

from cirq import ops, protocols, value
from cirq._compat import proper_repr

if TYPE_CHECKING:
import cirq


# Tag for gates to which noise must be applied.
PHYSICAL_GATE_TAG = 'physical_gate'


@value.value_equality(distinct_child_types=True)
class OpIdentifier:
"""Identifies an operation by gate and (optionally) target qubits."""

def __init__(self, gate_type: Type['cirq.Gate'], *qubits: 'cirq.Qid'):
self._gate_type = gate_type
self._gate_family = ops.GateFamily(gate_type)
self._qubits: Tuple['cirq.Qid', ...] = tuple(qubits)

@property
def gate_type(self) -> Type['cirq.Gate']:
# set to a type during initialization, never modified
return self._gate_type

@property
def qubits(self) -> Tuple['cirq.Qid', ...]:
return self._qubits

def _predicate(self, *args, **kwargs):
return self._gate_family._predicate(*args, **kwargs)

def is_proper_subtype_of(self, op_id: 'OpIdentifier'):
"""Returns true if this is contained within op_id, but not equal to it.
If this returns true, (x in self) implies (x in op_id), but the reverse
implication does not hold. op_id must be more general than self (either
by accepting any qubits or having a more general gate type) for this
to return true.
"""
more_specific_qubits = self.qubits and not op_id.qubits
more_specific_gate = self.gate_type != op_id.gate_type and issubclass(
self.gate_type, op_id.gate_type
)
if more_specific_qubits:
return more_specific_gate or self.gate_type == op_id.gate_type
elif more_specific_gate:
return more_specific_qubits or self.qubits == op_id.qubits
else:
return False

def __contains__(self, item: Union[ops.Gate, ops.Operation]) -> bool:
if isinstance(item, ops.Gate):
return (not self._qubits) and self._predicate(item)
return (
(not self.qubits or (item.qubits == self._qubits))
and item.gate is not None
and self._predicate(item.gate)
)

def __str__(self):
return f'{self.gate_type}{self.qubits}'

def __repr__(self) -> str:
qubits = ', '.join(map(repr, self.qubits))
return f'cirq.devices.noise_utils.OpIdentifier({proper_repr(self.gate_type)}, {qubits})'

def _value_equality_values_(self) -> Any:
return (self.gate_type, self.qubits)

def _json_dict_(self) -> Dict[str, Any]:
if hasattr(self.gate_type, '__name__'):
return {'gate_type': protocols.json_cirq_type(self._gate_type), 'qubits': self._qubits}
return {'gate_type': self._gate_type, 'qubits': self._qubits}

@classmethod
def _from_json_dict_(cls, gate_type, qubits, **kwargs) -> 'OpIdentifier':
if isinstance(gate_type, str):
gate_type = protocols.cirq_type_from_json(gate_type)
return cls(gate_type, *qubits)


# TODO: expose all from top-level cirq?
def decay_constant_to_xeb_fidelity(decay_constant: float, num_qubits: int = 2) -> float:
Expand Down
Loading

0 comments on commit b91f1fd

Please sign in to comment.