Repository navigation
Trait 2/pauli sum cliff prop - Add Clifford propagation, arithmetic, and formatting #303
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
Changes from all commits
917c483
de5fc4b
8a5ffbb
ef08443
41bcade
6e17744
4f1ca1a
983f939
2387b84
334d7a6
e342fc8
File filter
Filter by extension
Conversations
Jump to
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.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,261 @@ | ||
| // SPDX-FileCopyrightText: 2026 The PPVM Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| use ppvm_phased_pauli_word_2::Phased; | ||
| use ppvm_traits_2::{ | ||
| Clifford, CliffordBatch, Coefficient, PauliBits, Phase, Support, loss::LossInfo, | ||
| }; | ||
|
|
||
| use crate::{RekeyBijective, SignFlipByKey, Sum}; | ||
|
|
||
| impl<S: Support, P> Sum<S, P> { | ||
| fn check_qubit(&self, qubit: usize) { | ||
| assert!(qubit < self.n_sites(), "qubit out of bounds"); | ||
| } | ||
|
|
||
| fn check_pair(&self, qubit0: usize, qubit1: usize) { | ||
| self.check_qubit(qubit0); | ||
| self.check_qubit(qubit1); | ||
| assert_ne!(qubit0, qubit1, "two-qubit gates require distinct qubits"); | ||
| } | ||
| } | ||
|
|
||
| /// Whether a gate on `qubit` can change `key`: every Clifford fixes `I` and lost sites. | ||
| fn active<K: PauliBits + LossInfo>(key: &K, qubit: usize) -> bool { | ||
| !key.is_lost(qubit) && (key.x_bit(qubit) || key.z_bit(qubit)) | ||
| } | ||
|
|
||
| impl<S: RekeyBijective, P> Sum<S, P> | ||
| where | ||
| S::Key: PauliBits + LossInfo, | ||
| { | ||
| /// Conjugate the terms for which `touched` is true; all others are left in place. | ||
| fn conjugate_terms( | ||
| &mut self, | ||
| touched: impl Fn(&S::Key) -> bool, | ||
| gate: impl Fn(&mut Phased<S::Key>), | ||
| ) { | ||
| self.storage.rekey_bijective(|key, coeff| { | ||
| if !touched(key) { | ||
| return None; | ||
| } | ||
| let mut phased_word = Phased::new(key.clone()); | ||
| gate(&mut phased_word); | ||
| let (new_key, phase) = phased_word.into_parts(); | ||
| // Keys carry no phase; absorb the conjugation sign into the coefficient. | ||
| match phase { | ||
| Phase::Pos1 => {} | ||
| Phase::Neg1 => coeff.mul_sign_assign(-1), | ||
| _ => unreachable!("Clifford conjugation produces only real signs"), | ||
| } | ||
| (new_key != *key).then_some(new_key) | ||
| }); | ||
| } | ||
|
|
||
| fn conjugate_single(&mut self, qubit: usize, gate: impl Fn(&mut Phased<S::Key>)) { | ||
| self.check_qubit(qubit); | ||
| self.conjugate_terms(|key| active(key, qubit), gate); | ||
| } | ||
|
|
||
| fn conjugate_pair(&mut self, a: usize, b: usize, gate: impl Fn(&mut Phased<S::Key>)) { | ||
| self.check_pair(a, b); | ||
| self.conjugate_terms(|key| active(key, a) || active(key, b), gate); | ||
| } | ||
|
|
||
| fn conjugate_single_batch( | ||
| &mut self, | ||
| indices: &[usize], | ||
| gate: impl Fn(&mut Phased<S::Key>, usize), | ||
| ) { | ||
| for &qubit in indices { | ||
| self.check_qubit(qubit); | ||
| } | ||
| match indices { | ||
| [] => {} | ||
| &[qubit] => self.conjugate_single(qubit, |word| gate(word, qubit)), | ||
| _ => self.conjugate_terms( | ||
| |key| indices.iter().any(|&q| active(key, q)), | ||
| |word| { | ||
| for &qubit in indices { | ||
| gate(word, qubit); | ||
| } | ||
| }, | ||
| ), | ||
| } | ||
| } | ||
|
|
||
| fn conjugate_pair_batch( | ||
| &mut self, | ||
| pairs: &[(usize, usize)], | ||
| gate: impl Fn(&mut Phased<S::Key>, usize, usize), | ||
| ) { | ||
| for &(a, b) in pairs { | ||
| self.check_pair(a, b); | ||
| } | ||
| match pairs { | ||
| [] => {} | ||
| &[(a, b)] => self.conjugate_pair(a, b, |word| gate(word, a, b)), | ||
| _ => self.conjugate_terms( | ||
| |key| pairs.iter().any(|&(a, b)| active(key, a) || active(key, b)), | ||
| |word| { | ||
| for &(a, b) in pairs { | ||
| gate(word, a, b); | ||
| } | ||
| }, | ||
| ), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<S: SignFlipByKey, P> Sum<S, P> { | ||
| fn flip_signs_many(&mut self, indices: &[usize], negate: impl Fn(&S::Key, usize) -> bool) { | ||
| if indices.is_empty() { | ||
| return; | ||
| } | ||
| for &qubit in indices { | ||
| self.check_qubit(qubit); | ||
| } | ||
| self.storage | ||
| .flip_sign_by_key(|key| indices.iter().fold(false, |flip, &q| flip ^ negate(key, q))); | ||
| } | ||
| } | ||
|
|
||
| /// Conjugate each term, validating qubit indices before changing storage. | ||
| /// Two-qubit gates also require distinct indices, including on empty sums. | ||
| impl<S, P> Clifford for Sum<S, P> | ||
| where | ||
| S: RekeyBijective + SignFlipByKey, | ||
| S::Key: PauliBits + LossInfo, | ||
| { | ||
| fn x(&mut self, qubit: usize) { | ||
| self.check_qubit(qubit); | ||
| self.storage | ||
| .flip_sign_by_key(|key| !key.is_lost(qubit) && key.z_bit(qubit)); | ||
| } | ||
|
|
||
| fn y(&mut self, qubit: usize) { | ||
| self.check_qubit(qubit); | ||
| self.storage | ||
| .flip_sign_by_key(|key| !key.is_lost(qubit) && (key.x_bit(qubit) ^ key.z_bit(qubit))); | ||
| } | ||
|
|
||
| fn z(&mut self, qubit: usize) { | ||
| self.check_qubit(qubit); | ||
| self.storage | ||
| .flip_sign_by_key(|key| !key.is_lost(qubit) && key.x_bit(qubit)); | ||
| } | ||
|
|
||
| fn h(&mut self, qubit: usize) { | ||
| self.conjugate_single(qubit, |word| word.h(qubit)); | ||
| } | ||
|
|
||
| fn s(&mut self, qubit: usize) { | ||
| self.conjugate_single(qubit, |word| word.s(qubit)); | ||
| } | ||
|
|
||
| fn s_dag(&mut self, qubit: usize) { | ||
| self.conjugate_single(qubit, |word| word.s_dag(qubit)); | ||
| } | ||
|
|
||
| fn sqrt_x(&mut self, qubit: usize) { | ||
| self.conjugate_single(qubit, |word| word.sqrt_x(qubit)); | ||
| } | ||
|
|
||
| fn sqrt_x_dag(&mut self, qubit: usize) { | ||
| self.conjugate_single(qubit, |word| word.sqrt_x_dag(qubit)); | ||
| } | ||
|
|
||
| fn sqrt_y(&mut self, qubit: usize) { | ||
| self.conjugate_single(qubit, |word| word.sqrt_y(qubit)); | ||
| } | ||
|
|
||
| fn sqrt_y_dag(&mut self, qubit: usize) { | ||
| self.conjugate_single(qubit, |word| word.sqrt_y_dag(qubit)); | ||
| } | ||
|
|
||
| fn cnot(&mut self, control: usize, target: usize) { | ||
| self.conjugate_pair(control, target, |word| word.cnot(control, target)); | ||
| } | ||
|
|
||
| fn cz(&mut self, qubit0: usize, qubit1: usize) { | ||
| self.conjugate_pair(qubit0, qubit1, |word| word.cz(qubit0, qubit1)); | ||
| } | ||
|
|
||
| fn cy(&mut self, control: usize, target: usize) { | ||
| self.conjugate_pair(control, target, |word| word.cy(control, target)); | ||
| } | ||
| } | ||
|
|
||
| /// Apply gates in input order, rebuilding storage once per batch of changed keys. | ||
| /// Pauli batches only update signs. Validate the whole batch before changing terms. | ||
| impl<S, P> CliffordBatch for Sum<S, P> | ||
| where | ||
| S: RekeyBijective + SignFlipByKey, | ||
| S::Key: PauliBits + LossInfo, | ||
| { | ||
| fn x_many(&mut self, indices: &[usize]) { | ||
| if let &[qubit] = indices { | ||
| self.x(qubit); | ||
| return; | ||
| } | ||
| self.flip_signs_many(indices, |key, q| !key.is_lost(q) && key.z_bit(q)); | ||
| } | ||
|
|
||
| fn y_many(&mut self, indices: &[usize]) { | ||
| if let &[qubit] = indices { | ||
| self.y(qubit); | ||
| return; | ||
| } | ||
| self.flip_signs_many(indices, |key, q| { | ||
| !key.is_lost(q) && (key.x_bit(q) ^ key.z_bit(q)) | ||
| }); | ||
| } | ||
|
|
||
| fn z_many(&mut self, indices: &[usize]) { | ||
| if let &[qubit] = indices { | ||
| self.z(qubit); | ||
| return; | ||
| } | ||
| self.flip_signs_many(indices, |key, q| !key.is_lost(q) && key.x_bit(q)); | ||
| } | ||
|
|
||
| fn h_many(&mut self, indices: &[usize]) { | ||
| self.conjugate_single_batch(indices, Phased::h); | ||
| } | ||
|
|
||
| fn s_many(&mut self, indices: &[usize]) { | ||
| self.conjugate_single_batch(indices, Phased::s); | ||
| } | ||
|
|
||
| fn s_dag_many(&mut self, indices: &[usize]) { | ||
| self.conjugate_single_batch(indices, Phased::s_dag); | ||
| } | ||
|
|
||
| fn sqrt_x_many(&mut self, indices: &[usize]) { | ||
| self.conjugate_single_batch(indices, Phased::sqrt_x); | ||
| } | ||
|
|
||
| fn sqrt_x_dag_many(&mut self, indices: &[usize]) { | ||
| self.conjugate_single_batch(indices, Phased::sqrt_x_dag); | ||
| } | ||
|
|
||
| fn sqrt_y_many(&mut self, indices: &[usize]) { | ||
| self.conjugate_single_batch(indices, Phased::sqrt_y); | ||
| } | ||
|
|
||
| fn sqrt_y_dag_many(&mut self, indices: &[usize]) { | ||
| self.conjugate_single_batch(indices, Phased::sqrt_y_dag); | ||
| } | ||
|
|
||
| fn cnot_many(&mut self, pairs: &[(usize, usize)]) { | ||
| self.conjugate_pair_batch(pairs, Phased::cnot); | ||
| } | ||
|
|
||
| fn cz_many(&mut self, pairs: &[(usize, usize)]) { | ||
| self.conjugate_pair_batch(pairs, Phased::cz); | ||
| } | ||
|
|
||
| fn cy_many(&mut self, pairs: &[(usize, usize)]) { | ||
| self.conjugate_pair_batch(pairs, Phased::cy); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // SPDX-FileCopyrightText: 2026 The PPVM Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! Comparisons include width and explicit zero terms, but ignore configuration. | ||
|
|
||
| use approx::{AbsDiffEq, RelativeEq}; | ||
| use ppvm_traits_2::Support; | ||
|
|
||
| use crate::Sum; | ||
|
|
||
| impl<S: Support, P> Sum<S, P> { | ||
| fn compare_coefficients( | ||
| &self, | ||
| other: &Self, | ||
| equal: impl Fn(&S::Coeff, &S::Coeff) -> bool, | ||
| ) -> bool { | ||
| self.n_sites() == other.n_sites() | ||
| && self.len() == other.len() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's an edge case here: say you have two maps |
||
| && self.iter().all(|(key, coeff)| { | ||
| other | ||
| .get(&key) | ||
| .is_some_and(|other_coeff| equal(&coeff, &other_coeff)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl<S: Support, P> PartialEq for Sum<S, P> { | ||
| fn eq(&self, other: &Self) -> bool { | ||
| self.compare_coefficients(other, |a, b| a == b) | ||
| } | ||
| } | ||
|
|
||
| impl<S: Support, P> Eq for Sum<S, P> where S::Coeff: Eq {} | ||
|
|
||
| impl<S: Support, P> AbsDiffEq for Sum<S, P> | ||
| where | ||
| S::Coeff: AbsDiffEq, | ||
| <S::Coeff as AbsDiffEq>::Epsilon: Copy, | ||
| { | ||
| type Epsilon = <S::Coeff as AbsDiffEq>::Epsilon; | ||
|
|
||
| fn default_epsilon() -> Self::Epsilon { | ||
| S::Coeff::default_epsilon() | ||
| } | ||
|
|
||
| fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool { | ||
| self.compare_coefficients(other, |a, b| a.abs_diff_eq(b, epsilon)) | ||
| } | ||
| } | ||
|
|
||
| impl<S: Support, P> RelativeEq for Sum<S, P> | ||
| where | ||
| S::Coeff: RelativeEq, | ||
| <S::Coeff as AbsDiffEq>::Epsilon: Copy, | ||
| { | ||
| fn default_max_relative() -> Self::Epsilon { | ||
| S::Coeff::default_max_relative() | ||
| } | ||
|
|
||
| fn relative_eq( | ||
| &self, | ||
| other: &Self, | ||
| epsilon: Self::Epsilon, | ||
| max_relative: Self::Epsilon, | ||
| ) -> bool { | ||
| self.compare_coefficients(other, |a, b| a.relative_eq(b, epsilon, max_relative)) | ||
| } | ||
| } | ||
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.
FYI, once #297 lands, you can implement the
ApplyGateRuletrait instead (just two methods), which gives youCliffordfor free and allows you to re-use the Lean-proven kernels.