-
Notifications
You must be signed in to change notification settings - Fork 66
Add amplitude damping noise model #544
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
Closed
CodeMaverick2
wants to merge
4
commits into
TeamGraphix:master
from
CodeMaverick2:feat/amplitude-damping-noise-model-497
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ece6bde
Add amplitude damping noise model with analytic density-matrix tests …
CodeMaverick2 7a5714b
Cover remaining amplitude damping noise model lines for codecov
CodeMaverick2 f55fadf
Address review feedback on amplitude damping channels and analytic te…
CodeMaverick2 39b1d37
Fix import order in amplitude damping density matrix tests.
CodeMaverick2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.