From d4f4b31c26533eff714e20b3830899ffa3ad6815 Mon Sep 17 00:00:00 2001 From: Chen-Yang Su Date: Tue, 1 Dec 2026 01:43:03 -0800 Subject: [PATCH] Fix Kraus coefficients in pauli_channel --- CHANGELOG.md | 2 ++ graphix/channels.py | 17 ++++++++++------ tests/test_kraus.py | 48 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4203455dc..b5cc31eb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: diff --git a/graphix/channels.py b/graphix/channels.py index d8078148a..936deb73f 100644 --- a/graphix/channels.py +++ b/graphix/channels.py @@ -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 @@ -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), ] ) diff --git a/tests/test_kraus.py b/tests/test_kraus.py index 8a6b9fd0f..04d616a27 100644 --- a/tests/test_kraus.py +++ b/tests/test_kraus.py @@ -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 @@ -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 = [