Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix num_qubits property on AsymetricDepolarizing Channel #4652

Merged
merged 4 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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