Skip to content
Closed
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
3 changes: 2 additions & 1 deletion graphix/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from graphix.graphsim import GraphState
from graphix.instruction import Instruction
from graphix.measurements import BlochMeasurement, Measurement, PauliMeasurement
from graphix.noise_models import DepolarisingNoiseModel, NoiseModel
from graphix.noise_models import AmplitudeDampingNoiseModel, DepolarisingNoiseModel, NoiseModel
from graphix.opengraph import OpenGraph
from graphix.optimization import StandardizedPattern
from graphix.parameter import Placeholder
Expand All @@ -27,6 +27,7 @@

__all__ = [
"ANGLE_PI",
"AmplitudeDampingNoiseModel",
"Axis",
"BasicStates",
"BlochMeasurement",
Expand Down
57 changes: 57 additions & 0 deletions graphix/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,63 @@ def dephasing_channel(prob: float) -> KrausChannel:
)


def amplitude_damping_channel(gamma: float) -> KrausChannel:
r"""Single-qubit amplitude damping channel.

.. math::
K_1 = \begin{pmatrix}
1 & 0 \\
0 & \sqrt{1-\gamma}
\end{pmatrix},\quad
K_2 = \begin{pmatrix}
0 & \sqrt{\gamma} \\
0 & 0
\end{pmatrix}

Parameters
----------
gamma : float
The damping probability, between 0 and 1.

Returns
-------
:class:`graphix.channels.KrausChannel` object
containing the corresponding Kraus operators
"""
return KrausChannel(
[
KrausData(1.0, np.array([[1, 0], [0, np.sqrt(1 - gamma)]], dtype=np.complex128)),
KrausData(1.0, np.array([[0, np.sqrt(gamma)], [0, 0]], dtype=np.complex128)),
]
)


def two_qubit_amplitude_damping_channel(gamma: float) -> KrausChannel:
r"""Two-qubit amplitude damping channel (independent tensor product).

The two-qubit channel is formed by the tensor product of two single-qubit
amplitude damping channels, yielding 4 Kraus operators:

.. math::
\{K_1 \otimes K_1,\; K_1 \otimes K_2,\; K_2 \otimes K_1,\; K_2 \otimes K_2\}

Parameters
----------
gamma : float
The damping probability, between 0 and 1.

Returns
-------
:class:`graphix.channels.KrausChannel` object
containing the corresponding Kraus operators
"""
single_ops = [
np.array([[1, 0], [0, np.sqrt(1 - gamma)]], dtype=np.complex128),
np.array([[0, np.sqrt(gamma)], [0, 0]], dtype=np.complex128),
]
return KrausChannel([KrausData(1.0, np.kron(left, right)) for left in single_ops for right in single_ops])


def depolarising_channel(prob: float) -> KrausChannel:
r"""Single-qubit depolarizing channel.

Expand Down
8 changes: 8 additions & 0 deletions graphix/noise_models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

from typing import TYPE_CHECKING

from graphix.noise_models.amplitude_damping import (
AmplitudeDampingNoise,
AmplitudeDampingNoiseModel,
TwoQubitAmplitudeDampingNoise,
)
from graphix.noise_models.depolarising import DepolarisingNoise, DepolarisingNoiseModel, TwoQubitDepolarisingNoise
from graphix.noise_models.noise_model import (
ApplyNoise,
Expand All @@ -16,11 +21,14 @@
from graphix.noise_models.noise_model import CommandOrNoise as CommandOrNoise

__all__ = [
"AmplitudeDampingNoise",
"AmplitudeDampingNoiseModel",
"ApplyNoise",
"ComposeNoiseModel",
"DepolarisingNoise",
"DepolarisingNoiseModel",
"Noise",
"NoiseModel",
"TwoQubitAmplitudeDampingNoise",
"TwoQubitDepolarisingNoise",
]
153 changes: 153 additions & 0 deletions graphix/noise_models/amplitude_damping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
"""Amplitude damping noise model."""

from __future__ import annotations

from typing import TYPE_CHECKING

import typing_extensions

from graphix.channels import amplitude_damping_channel, two_qubit_amplitude_damping_channel
from graphix.command import BaseM, CommandKind
from graphix.measurements import toggle_outcome
from graphix.noise_models.noise_model import ApplyNoise, Noise, NoiseModel
from graphix.rng import ensure_rng
from graphix.utils import Probability

if TYPE_CHECKING:
from collections.abc import Iterable

from numpy.random import Generator

from graphix.channels import KrausChannel
from graphix.measurements import Outcome
from graphix.noise_models.noise_model import CommandOrNoise


class AmplitudeDampingNoise(Noise):
"""One-qubit amplitude damping noise with damping parameter ``gamma``."""

gamma = Probability()

def __init__(self, gamma: float) -> None:
"""Initialize one-qubit amplitude damping noise.

Parameters
----------
gamma : float
Damping parameter of the noise, between 0 and 1.
"""
self.gamma = gamma

@property
@typing_extensions.override
def nqubits(self) -> int:
"""Return the number of qubits targetted by the noise element."""
return 1

@typing_extensions.override
def to_kraus_channel(self) -> KrausChannel:
"""Return the Kraus channel describing the noise element."""
return amplitude_damping_channel(self.gamma)


class TwoQubitAmplitudeDampingNoise(Noise):
"""Two-qubit amplitude damping noise with damping parameter ``gamma``."""

gamma = Probability()

def __init__(self, gamma: float) -> None:
"""Initialize two-qubit amplitude damping noise.

Parameters
----------
gamma : float
Damping parameter of the noise, between 0 and 1.
"""
self.gamma = gamma

@property
@typing_extensions.override
def nqubits(self) -> int:
"""Return the number of qubits targetted by the noise element."""
return 2

@typing_extensions.override
def to_kraus_channel(self) -> KrausChannel:
"""Return the Kraus channel describing the noise element."""
return two_qubit_amplitude_damping_channel(self.gamma)


class AmplitudeDampingNoiseModel(NoiseModel):
"""Amplitude damping noise model.

:param NoiseModel: Parent abstract class class:`NoiseModel`
:type NoiseModel: class
"""

def __init__(
self,
prepare_error_prob: float = 0.0,
x_error_prob: float = 0.0,
z_error_prob: float = 0.0,
entanglement_error_prob: float = 0.0,
measure_channel_prob: float = 0.0,
measure_error_prob: float = 0.0,
) -> None:
self.prepare_error_prob = prepare_error_prob
self.x_error_prob = x_error_prob
self.z_error_prob = z_error_prob
self.entanglement_error_prob = entanglement_error_prob
self.measure_error_prob = measure_error_prob
self.measure_channel_prob = measure_channel_prob

@typing_extensions.override
def input_nodes(
self, nodes: Iterable[int], rng: Generator | None = None, *, stacklevel: int = 1
) -> list[CommandOrNoise]:
"""Return the noise to apply to input nodes."""
return [ApplyNoise(noise=AmplitudeDampingNoise(self.prepare_error_prob), nodes=[node]) for node in nodes]

@typing_extensions.override
def command(
self, cmd: CommandOrNoise, rng: Generator | None = None, *, stacklevel: int = 1
) -> list[CommandOrNoise]:
"""Return the noise to apply to the command ``cmd``."""
match cmd.kind:
case CommandKind.N:
return [cmd, ApplyNoise(noise=AmplitudeDampingNoise(self.prepare_error_prob), nodes=[cmd.node])]
case CommandKind.E:
return [
cmd,
ApplyNoise(
noise=TwoQubitAmplitudeDampingNoise(self.entanglement_error_prob),
nodes=list(cmd.nodes),
),
]
case CommandKind.M:
return [ApplyNoise(noise=AmplitudeDampingNoise(self.measure_channel_prob), nodes=[cmd.node]), cmd]
case CommandKind.X:
return [
cmd,
ApplyNoise(noise=AmplitudeDampingNoise(self.x_error_prob), nodes=[cmd.node], domain=cmd.domain),
]
case CommandKind.Z:
return [
cmd,
ApplyNoise(noise=AmplitudeDampingNoise(self.z_error_prob), nodes=[cmd.node], domain=cmd.domain),
]
case CommandKind.C | CommandKind.T | CommandKind.ApplyNoise:
return [cmd]
case CommandKind.S:
raise ValueError("Unexpected signal!")
case _:
typing_extensions.assert_never(cmd.kind)

@typing_extensions.override
def confuse_result(
self, cmd: BaseM, result: Outcome, rng: Generator | None = None, *, stacklevel: int = 1
) -> Outcome:
"""Assign wrong measurement result cmd = "M"."""
rng = ensure_rng(rng, stacklevel=stacklevel + 1)
if rng.uniform() < self.measure_error_prob:
return toggle_outcome(result)
return result
44 changes: 44 additions & 0 deletions tests/test_kraus.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
from graphix.channels import (
KrausChannel,
KrausData,
amplitude_damping_channel,
dephasing_channel,
depolarising_channel,
two_qubit_amplitude_damping_channel,
two_qubit_depolarising_channel,
two_qubit_depolarising_tensor_channel,
)
Expand Down Expand Up @@ -180,3 +182,45 @@ def test_2_qubit_depolarising_tensor_channel(self, fx_rng: Generator) -> None:
for i in range(len(depol_tensor_channel_2_qubit)):
assert np.allclose(depol_tensor_channel_2_qubit[i].coef, data[i].coef)
assert np.allclose(depol_tensor_channel_2_qubit[i].operator, data[i].operator)

def test_amplitude_damping_channel(self, fx_rng: Generator) -> None:
gamma = fx_rng.uniform()
data = [
KrausData(1.0, np.array([[1.0, 0.0], [0.0, np.sqrt(1 - gamma)]], dtype=np.complex128)),
KrausData(1.0, np.array([[0.0, np.sqrt(gamma)], [0.0, 0.0]], dtype=np.complex128)),
]

channel = amplitude_damping_channel(gamma)

assert channel.nqubit == 1
assert len(channel) == 2

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

@pytest.mark.parametrize("gamma", [-0.1, 1.1])
def test_amplitude_damping_channel_invalid_gamma(self, gamma: float) -> None:
with pytest.raises(ValueError, match="The specified channel is not normalized"):
amplitude_damping_channel(gamma)
with pytest.raises(ValueError, match="The specified channel is not normalized"):
two_qubit_amplitude_damping_channel(gamma)
Comment on lines +203 to +207

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In line with my comment in channels.py, you can check that this raises a ValueError from the KrausChannel constructor method with the text "The specified channel is not normalized". Please update this accordingly.


def test_2_qubit_amplitude_damping_channel(self, fx_rng: Generator) -> None:
gamma = fx_rng.uniform()
one_qubit_channel = amplitude_damping_channel(gamma)
data = [
KrausData(left.coef * right.coef, np.kron(left.operator, right.operator))
for left in one_qubit_channel
for right in one_qubit_channel
]

channel = two_qubit_amplitude_damping_channel(gamma)

assert isinstance(channel, KrausChannel)
assert channel.nqubit == 2
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)
Loading
Loading