Skip to content
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- #645: Fix negative sign in circuit extraction strings

- Fixed Kraus coefficients and probability validation in `pauli_channel`.

### Changed

- #587:
Expand Down
17 changes: 11 additions & 6 deletions graphix/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from graphix import linalg_validations as lv
from graphix.ops import Ops
from graphix.utils import check_probability

if TYPE_CHECKING:
from collections.abc import Iterable, Iterator
Expand Down Expand Up @@ -207,15 +208,19 @@ def pauli_channel(px: float, py: float, pz: float) -> KrausChannel:
(1-p_X-p_Y-p_Z) \rho + p_X X \rho X + p_Y Y \rho Y + p_Z Z \rho Z)

"""
if px + py + pz > 1:
check_probability(px)
check_probability(py)
check_probability(pz)
p_sum = px + py + pz
if p_sum > 1:
raise ValueError("The sum of probabilities must not exceed 1.")
p_i = 1 - px - py - pz
p_i = 1 - p_sum
return KrausChannel(
[
KrausData(np.sqrt(1 - p_i), Ops.I),
KrausData(np.sqrt(px / 3.0), Ops.X),
KrausData(np.sqrt(py / 3.0), Ops.Y),
KrausData(np.sqrt(pz / 3.0), Ops.Z),
KrausData(np.sqrt(p_i), Ops.I),
KrausData(np.sqrt(px), Ops.X),
KrausData(np.sqrt(py), Ops.Y),
KrausData(np.sqrt(pz), Ops.Z),
]
)

Expand Down
48 changes: 48 additions & 0 deletions tests/test_kraus.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
amplitude_damping_channel,
dephasing_channel,
depolarising_channel,
pauli_channel,
two_qubit_amplitude_damping_channel,
two_qubit_depolarising_channel,
two_qubit_depolarising_tensor_channel,
)
from graphix.ops import Ops
from graphix.sim.density_matrix import DensityMatrix

if TYPE_CHECKING:
from numpy.random import Generator
Expand Down Expand Up @@ -121,6 +123,52 @@ def test_depolarising_channel(self, fx_rng: Generator) -> None:
assert np.allclose(depol_channel[i].coef, data[i].coef)
assert np.allclose(depol_channel[i].operator, data[i].operator)

def test_pauli_channel(self, fx_rng: Generator) -> None:
px, py, pz = fx_rng.dirichlet([1.0, 1.0, 1.0, 1.0])[:3]
p_i = 1.0 - (px + py + pz)
data = [
KrausData(np.sqrt(p_i), Ops.I),
KrausData(np.sqrt(px), Ops.X),
KrausData(np.sqrt(py), Ops.Y),
KrausData(np.sqrt(pz), Ops.Z),
]

channel = pauli_channel(px, py, pz)

assert isinstance(channel, KrausChannel)
assert channel.nqubit == 1
assert len(channel) == 4

for i in range(len(channel)):
assert np.allclose(channel[i].coef, data[i].coef)
assert np.allclose(channel[i].operator, data[i].operator)

dm = DensityMatrix(data=np.array([np.cos(0.3), np.sin(0.3) * np.exp(1j * 0.7)]))
rho = dm.rho.astype(np.complex128)
dm.apply_channel(channel, [0])
expected = p_i * rho + px * (Ops.X @ rho @ Ops.X) + py * (Ops.Y @ rho @ Ops.Y) + pz * (Ops.Z @ rho @ Ops.Z)
assert np.allclose(dm.rho, expected)

# Boundary and silent-normalization cases
max_mixed_channel = pauli_channel(0.25, 0.25, 0.25)
for item in max_mixed_channel:
assert np.allclose(item.coef, 0.5)
dm_max_mixed = DensityMatrix(data=rho)
dm_max_mixed.apply_channel(max_mixed_channel, [0])
assert np.allclose(dm_max_mixed.rho, 0.5 * Ops.I)

identity_channel = pauli_channel(0.0, 0.0, 0.0)
assert np.allclose(identity_channel[0].coef, 1.0)

unit_sum_channel = pauli_channel(0.1, 0.2, 0.7)
assert np.allclose(unit_sum_channel[0].coef, 0.0)

with pytest.raises(ValueError, match="sum of probabilities"):
pauli_channel(0.5, 0.5, 0.5)

with pytest.raises(ValueError, match="probability"):
pauli_channel(-0.1, 0.2, 0.2)

def test_2_qubit_depolarising_channel(self, fx_rng: Generator) -> None:
prob = fx_rng.uniform()
data = [
Expand Down