Repository navigation
add noise, amplitude damping, and loss channels to ppvm-pauli-sum-2 #315
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
Open
JonhasA
wants to merge
5
commits into
trait-2/pauli-sum-2-rots
Choose a base branch
from
trait-2/pauli-sum-2-loss-noise
base: trait-2/pauli-sum-2-rots
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bfbd0cd
adding support for coefficient-only Pauli and depolarizing channels
JonhasA 8022be3
adding amplitude damping and loss via branching
JonhasA b56007f
refactor(ppvm-pauli-sum-2): simplify noise, damping, and loss channels
JonhasA f3f3fe9
perf(ppvm-pauli-sum-2): restore direct sign-flip path for Clifford X/Y/Z
JonhasA 0998b16
test(ppvm-pauli-sum-2): cover BranchInPlace multi-branch merge semantics
JonhasA 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,38 @@ | ||
| // SPDX-FileCopyrightText: 2026 The PPVM Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! Heisenberg amplitude damping: X, Y scale by sqrt(1 - gamma); Z maps to | ||
| //! (1 - gamma) Z + gamma I through the branching machinery. Lost sites are | ||
| //! skipped. Zero branches stay stored; reduction is caller-controlled. | ||
|
|
||
| use num::One; | ||
| use ppvm_traits_2::{AmplitudeDamping, PauliBits, loss::LossInfo}; | ||
|
|
||
| use crate::{BranchInPlace, Sum}; | ||
|
|
||
| impl<S: BranchInPlace, P> AmplitudeDamping<S::Coeff> for Sum<S, P> | ||
| where | ||
| S::Key: PauliBits + LossInfo, | ||
| S::Coeff: num::Float, | ||
| { | ||
| fn amplitude_damping(&mut self, qubit: usize, gamma: S::Coeff) { | ||
| self.check_qubit(qubit); | ||
| let survive = S::Coeff::one() - gamma; | ||
| let transverse = num::Float::sqrt(survive); | ||
| self.storage.branch_in_place(|key, coeff| { | ||
| if key.is_lost(qubit) { | ||
| return None; | ||
| } | ||
| if key.x_bit(qubit) { | ||
| *coeff *= transverse; | ||
| return None; | ||
| } | ||
| if !key.z_bit(qubit) { | ||
| return None; | ||
| } | ||
| let branch = *coeff * gamma; | ||
| *coeff *= survive; | ||
| Some((key.toggled_bits(qubit, false, true), branch)) | ||
| }); | ||
| } | ||
| } |
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,93 @@ | ||
| // SPDX-FileCopyrightText: 2026 The PPVM Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! Heisenberg loss maps matching `ppvm-tableau-2`. `I` projects onto a live | ||
| //! site and `L` onto a lost one. Loss branches clear an observable's loss flag; | ||
| //! reset adds an `L` copy of each `I`/`Z` term, since a lost qubit returns in | ||
| //! `|0>`. Zero coefficients stay stored; reduction is caller-controlled. | ||
|
|
||
| use crate::{BranchInPlace, Sum}; | ||
| use num::{One, Zero}; | ||
| use ppvm_traits_2::{ | ||
| CorrelatedLossChannel, LossChannel, PauliBits, ResetLossChannel, | ||
| loss::{LossInfo, LossState}, | ||
| }; | ||
|
|
||
| impl<S: BranchInPlace, P> LossChannel<S::Coeff> for Sum<S, P> | ||
| where | ||
| S::Key: LossState, | ||
| { | ||
| fn loss_channel(&mut self, qubit: usize, p: S::Coeff) { | ||
| self.check_qubit(qubit); | ||
| let survive = S::Coeff::one() - p.clone(); | ||
| self.storage.branch_in_place(|key, coeff| { | ||
| if key.is_lost(qubit) { | ||
| Some((key.loss_cleared(qubit), coeff.clone() * p.clone())) | ||
| } else { | ||
| *coeff *= &survive; | ||
| None | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| impl<S: BranchInPlace, P> CorrelatedLossChannel<S::Coeff> for Sum<S, P> | ||
| where | ||
| S::Key: LossState, | ||
| { | ||
| fn correlated_loss_channel( | ||
| &mut self, | ||
| a: usize, | ||
| b: usize, | ||
| [both, single, after_loss]: [S::Coeff; 3], | ||
| ) { | ||
| self.check_pair(a, b); | ||
| let survive = S::Coeff::one() - (single.clone() + single.clone()) - both.clone(); | ||
| let partner_survives = S::Coeff::one() - after_loss.clone(); | ||
| self.storage.branch_in_place(|key, coeff| { | ||
| let mut branches = [None, None, None]; | ||
| match (key.is_lost(a), key.is_lost(b)) { | ||
| (false, false) => *coeff *= &survive, | ||
| // Reached from one already lost (p_LN) or from both live (p_LL). | ||
| (true, true) => { | ||
| let weight = coeff.clone() * after_loss.clone(); | ||
| let mut neither = key.loss_cleared(a); | ||
| neither.clear_lost(b); | ||
| branches = [ | ||
| Some((key.loss_cleared(a), weight.clone())), | ||
| Some((key.loss_cleared(b), weight)), | ||
| Some((neither, coeff.clone() * both.clone())), | ||
| ]; | ||
| } | ||
| // Reached from both live (p_LQ), or the partner survived p_LN. | ||
| (lost_a, _) => { | ||
| let lost = if lost_a { a } else { b }; | ||
| branches[0] = Some((key.loss_cleared(lost), coeff.clone() * single.clone())); | ||
| *coeff *= &partner_survives; | ||
| } | ||
| } | ||
| branches.into_iter().flatten() | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| impl<S: BranchInPlace, P> ResetLossChannel for Sum<S, P> | ||
| where | ||
| S::Key: PauliBits + LossState, | ||
| { | ||
| fn reset_loss_channel(&mut self, qubit: usize) { | ||
| self.check_qubit(qubit); | ||
| self.storage.branch_in_place(|key, coeff| { | ||
| if key.is_lost(qubit) { | ||
| *coeff = S::Coeff::zero(); | ||
| return None; | ||
| } | ||
| if key.x_bit(qubit) { | ||
| return None; | ||
| } | ||
| let mut lost = key.clone(); | ||
| lost.set_lost(qubit); | ||
| Some((lost, coeff.clone())) | ||
| }); | ||
| } | ||
| } |
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,116 @@ | ||
| // SPDX-FileCopyrightText: 2026 The PPVM Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! Coefficient-only Heisenberg Pauli and depolarizing channels: each term is | ||
| //! rescaled in place, so no keys are created, removed, or merged. A Pauli keeps | ||
| //! its sign under errors it commutes with, so its factor is one minus twice | ||
| //! the anticommuting probability. Lost sites are skipped, matching the | ||
| //! tableau. Probabilities are assumed valid; coefficients may be complex. | ||
|
|
||
| use num::One; | ||
| use ppvm_traits_2::{ | ||
| Depolarizing, Depolarizing2, PauliBits, PauliError, TwoQubitPauliError, loss::LossInfo, | ||
| }; | ||
|
|
||
| use crate::{Sum, UpdateCoefficients}; | ||
|
|
||
| /// Packed code at `qubit` (`0=I, 1=X, 2=Z, 3=Y`), treating a lost site as `I`. | ||
| #[inline] | ||
| fn live_code<K: PauliBits + LossInfo>(key: &K, qubit: usize) -> usize { | ||
| if key.is_lost(qubit) { | ||
| 0 | ||
| } else { | ||
| key.pauli_code(qubit) as usize | ||
| } | ||
| } | ||
|
|
||
| impl<S: UpdateCoefficients, P> PauliError<S::Coeff> for Sum<S, P> | ||
| where | ||
| S::Key: PauliBits + LossInfo, | ||
| { | ||
| fn pauli_error(&mut self, qubit: usize, [px, py, pz]: [S::Coeff; 3]) { | ||
| self.check_qubit(qubit); | ||
| let factor = |a: S::Coeff, b: S::Coeff| { | ||
| let p = a + b; | ||
| S::Coeff::one() - (p.clone() + p) | ||
| }; | ||
| // Indexed by packed code: X anticommutes with Y, Z; Z with X, Y; Y with X, Z. | ||
| let factors = [ | ||
| S::Coeff::one(), | ||
| factor(py.clone(), pz.clone()), | ||
| factor(px.clone(), py), | ||
| factor(px, pz), | ||
| ]; | ||
| self.storage.update_coefficients(|key, coeff| { | ||
| let code = live_code(key, qubit); | ||
| if code != 0 { | ||
| *coeff *= &factors[code]; | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| impl<S: UpdateCoefficients, P> TwoQubitPauliError<S::Coeff> for Sum<S, P> | ||
| where | ||
| S::Key: PauliBits + LossInfo, | ||
| { | ||
| fn two_qubit_pauli_error(&mut self, a: usize, b: usize, p: [S::Coeff; 15]) { | ||
| self.check_pair(a, b); | ||
| // Probabilities use I, X, Y, Z order; packed codes use I, X, Z, Y. | ||
| const CODE: [usize; 4] = [0, 1, 3, 2]; | ||
| let anti = |u: usize, v: usize| ((u & 1) & (v >> 1)) ^ ((u >> 1) & (v & 1)) != 0; | ||
| // Indexed by 4 * code_a + code_b. | ||
| let factors: [S::Coeff; 16] = std::array::from_fn(|pair| { | ||
| let mut factor = S::Coeff::one(); | ||
| for (i, probability) in p.iter().enumerate() { | ||
| let error = i + 1; | ||
| if anti(pair / 4, CODE[error / 4]) != anti(pair % 4, CODE[error % 4]) { | ||
| factor = factor - (probability.clone() + probability.clone()); | ||
| } | ||
| } | ||
| factor | ||
| }); | ||
| self.storage.update_coefficients(|key, coeff| { | ||
| let pair = 4 * live_code(key, a) + live_code(key, b); | ||
| if pair != 0 { | ||
| *coeff *= &factors[pair]; | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| /// Stim's `DEPOLARIZE1(p)`: each of `X`, `Y`, `Z` with probability `p/3`. | ||
| impl<S: UpdateCoefficients, P> Depolarizing<S::Coeff> for Sum<S, P> | ||
| where | ||
| S::Key: PauliBits + LossInfo, | ||
| S::Coeff: From<f64>, | ||
| { | ||
| fn depolarize1(&mut self, qubit: usize, p: S::Coeff) { | ||
| self.check_qubit(qubit); | ||
| let factor = S::Coeff::one() - p * S::Coeff::from(4.0 / 3.0); | ||
| self.storage.update_coefficients(|key, coeff| { | ||
| if live_code(key, qubit) != 0 { | ||
| *coeff *= &factor; | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| /// Stim's `DEPOLARIZE2(p)`: each non-identity pair with probability `p/15`. | ||
| impl<S: UpdateCoefficients, P> Depolarizing2<S::Coeff> for Sum<S, P> | ||
| where | ||
| S::Key: PauliBits + LossInfo, | ||
| S::Coeff: From<f64>, | ||
| { | ||
| fn depolarize2(&mut self, a: usize, b: usize, p: S::Coeff) { | ||
| self.check_pair(a, b); | ||
| let factor = S::Coeff::one() - p * S::Coeff::from(16.0 / 15.0); | ||
| self.storage.update_coefficients(|key, coeff| { | ||
| let active_a = !key.is_lost(a) && key.pauli_code(a) != 0; | ||
| let active_b = !key.is_lost(b) && key.pauli_code(b) != 0; | ||
| if active_a || active_b { | ||
| *coeff *= &factor; | ||
| } | ||
| }); | ||
| } | ||
| } | ||
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
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.
Could you re-use the
live_codehelper here?