Skip to content

Commit

Permalink
Fix num_qubits property on AsymetricDepolarizing Channel (quantumlib#…
Browse files Browse the repository at this point in the history
…4652)

This is a breaking change!

It removes the `num_qubits` property and instead one should use the `num_qubits` method.
  • Loading branch information
dabacon authored and rht committed May 1, 2023
1 parent c1395c1 commit 6b8a8fc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 6 additions & 6 deletions cirq-core/cirq/ops/common_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from cirq import protocols, value
from cirq.ops import raw_types, common_gates, pauli_gates, gate_features, identity


if TYPE_CHECKING:
import cirq

Expand Down Expand Up @@ -53,6 +54,10 @@ def __init__(
(including identity). The input $\rho$ is the density matrix before the
depolarization.
Note: prior to Cirq v0.14, this class contained `num_qubits` as a property.
This violates the contract of `cirq.Gate` so it was removed. One can
instead get the number of qubits by calling the method `num_qubits`.
Args:
p_x: The probability that a Pauli X and no other gate occurs.
p_y: The probability that a Pauli Y and no other gate occurs.
Expand Down Expand Up @@ -178,11 +183,6 @@ def p_z(self) -> float:
raise ValueError('num_qubits should be 1')
return self._error_probabilities.get('Z', 0.0)

@property
def num_qubits(self) -> int:
"""The number of qubits"""
return self._num_qubits

@property
def error_probabilities(self) -> Dict[str, float]:
"""A dictionary from Pauli gates to probability"""
Expand All @@ -193,7 +193,7 @@ def _json_dict_(self) -> Dict[str, Any]:

def _approx_eq_(self, other: Any, atol: float) -> bool:
return (
self.num_qubits == other.num_qubits
self._num_qubits == other._num_qubits
and np.isclose(self.p_i, other.p_i, atol=atol).item()
and np.isclose(self.p_x, other.p_x, atol=atol).item()
and np.isclose(self.p_y, other.p_y, atol=atol).item()
Expand Down
4 changes: 3 additions & 1 deletion cirq-core/cirq/ops/common_channels_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ def test_asymmetric_depolarizing_channel():
)
assert cirq.has_kraus(d)

assert cirq.AsymmetricDepolarizingChannel(p_x=0, p_y=0.1, p_z=0).num_qubits() == 1


def test_asymmetric_depolarizing_mixture():
d = cirq.asymmetric_depolarize(0.1, 0.2, 0.3)
Expand Down Expand Up @@ -729,7 +731,7 @@ def test_default_asymmetric_depolarizing_channel():
assert d.p_x == 0.0
assert d.p_y == 0.0
assert d.p_z == 0.0
assert d.num_qubits == 1
assert d.num_qubits() == 1


def test_bad_error_probabilities_gate():
Expand Down

0 comments on commit 6b8a8fc

Please sign in to comment.