Skip to content
Merged
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
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ members = [
# Built by `cargo build --workspace --all-targets` in CI so the skill
# can't silently drift away from the public API.
"crates/ppvm-tui",
"skills/ppvm-usage/examples/rust", "crates/ppvm-lossy-pauli-word-2",
"skills/ppvm-usage/examples/rust", "crates/ppvm-lossy-pauli-word-2", "crates/ppvm-phased-pauli-word-2",
]

# Single version for every crate and the `bloqade-ppvm` Python package
Expand Down
16 changes: 9 additions & 7 deletions crates/ppvm-lossy-pauli-word-2/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{

use bitvec::array::BitArray;
use ppvm_pauli_word_2::{DefaultStorage, PauliStorage};
use ppvm_traits_2::{LossState, LossySite, Pauli, PauliBits, Word};
use ppvm_traits_2::{LossState, LossySite, Pauli, PauliBits, Word, loss::LossInfo};

pub struct LossyPauliWord<A: PauliStorage = DefaultStorage, H = fxhash::FxBuildHasher> {
pub(crate) xbits: BitArray<A>,
Expand Down Expand Up @@ -426,12 +426,19 @@ impl<A: PauliStorage, H> PauliBits for LossyPauliWord<A, H> {
}
}

impl<A: PauliStorage, H> LossState for LossyPauliWord<A, H> {
impl<A: PauliStorage, H> LossInfo for LossyPauliWord<A, H> {
#[inline]
fn is_lost(&self, i: usize) -> bool {
LossyPauliWord::is_lost(self, i)
}

#[inline]
fn loss_weight(&self) -> usize {
LossyPauliWord::loss_weight(self)
}
}

impl<A: PauliStorage, H> LossState for LossyPauliWord<A, H> {
#[inline]
fn set_lost(&mut self, i: usize) {
LossyPauliWord::set_lost(self, i);
Expand All @@ -441,11 +448,6 @@ impl<A: PauliStorage, H> LossState for LossyPauliWord<A, H> {
fn clear_lost(&mut self, i: usize) {
LossyPauliWord::clear_loss(self, i);
}

#[inline]
fn loss_weight(&self) -> usize {
LossyPauliWord::loss_weight(self)
}
}

#[cfg(test)]
Expand Down
10 changes: 9 additions & 1 deletion crates/ppvm-pauli-word-2/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::marker::PhantomData;
use bitvec::array::BitArray;
use bitvec::view::BitView;
use num::{One, Zero};
use ppvm_traits_2::{Pauli, PauliBits, Word};
use ppvm_traits_2::{Pauli, PauliBits, Word, loss::LossInfo};

use crate::hash::structural_hash;
use crate::{DefaultStorage, HashFinalize, PauliStorage};
Expand Down Expand Up @@ -226,6 +226,14 @@ impl<A: PauliStorage, H> Word for PauliWord<A, H> {
}
}

/// Ordinary words report no loss. So, we just have NOOP LossInfo reporting.
impl<A, H> LossInfo for PauliWord<A, H>
where
A: PauliStorage,
H: BuildHasher + Default + HashFinalize,
{
}

impl<A, H> PauliBits for PauliWord<A, H>
where
A: PauliStorage,
Expand Down
2 changes: 0 additions & 2 deletions crates/ppvm-pauli-word-2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ mod column;
mod data;
mod hash;
mod pauli_parser;
mod phase;
mod product;
mod storage;

pub use column::PauliKeyColumn;
pub use data::PauliWord;
pub use hash::HashFinalize;
pub use phase::Phased;
pub use storage::{DefaultStorage, PauliStorage};
7 changes: 0 additions & 7 deletions crates/ppvm-pauli-word-2/src/pauli_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,6 @@ mod tests {
for input in ["IIIIIIIII", "XXXXXXXXI", "IIIIIIIIX", "I_I_I_I_I_I_I_I_I"] {
assert!(std::panic::catch_unwind(|| PauliWord::<u8>::from(input)).is_err());
assert!(std::panic::catch_unwind(|| PauliWord::<u8>::from(input.to_owned())).is_err());
let phased = format!("+i{input}");
assert!(
std::panic::catch_unwind(|| {
crate::Phased::<PauliWord<u8>>::from(phased.as_str())
})
.is_err()
);
}
for (input, expected) in [
("", ""),
Expand Down
23 changes: 14 additions & 9 deletions crates/ppvm-pauli-word-2/src/product.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,24 @@ where
/// Returns the XORed word and its residual phase for the coefficient to absorb.
/// Both inputs must have equal width; zero unused bits stay zero.
fn key_mul(&self, other: &Self) -> (Self, Phase) {
let mut product = *self;
let phase = product.key_mul_assign(other);
(product, phase)
}

/// Multiplies in place, returning the residual phase and refreshing the hash once.
#[inline(always)]
fn key_mul_assign(&mut self, other: &Self) -> Phase {
debug_assert_eq!(
self.nqubits, other.nqubits,
"twisted product requires equal-width words",
);

let lhs_x = self.xbits.data.as_raw_slice();
let lhs_z = self.zbits.data.as_raw_slice();
let lhs_x = self.xbits.data.as_raw_mut_slice();
let lhs_z = self.zbits.data.as_raw_mut_slice();
let rhs_x = other.xbits.data.as_raw_slice();
let rhs_z = other.zbits.data.as_raw_slice();

let mut product = Self::new(self.nqubits);
let product_x = product.xbits.data.as_raw_mut_slice();
let product_z = product.zbits.data.as_raw_mut_slice();
let mut sign_count = 0u32;
let mut imaginary_count = 0u32;

Expand All @@ -56,13 +61,13 @@ where
imaginary_count += imaginary_mask.count_ones();

// XOR gives the product's Pauli letters; the masks account for phase.
product_x[chunk] = left_x ^ right_x;
product_z[chunk] = left_z ^ right_z;
lhs_x[chunk] = left_x ^ right_x;
lhs_z[chunk] = left_z ^ right_z;
}

product.refresh_hash();
self.refresh_hash();
let phase_exponent = ((2 * sign_count + imaginary_count) % 4) as u8;
(product, Phase::from_exponent(phase_exponent))
Phase::from_exponent(phase_exponent)
}
}

Expand Down
12 changes: 12 additions & 0 deletions crates/ppvm-phased-pauli-word-2/Cargo.toml
Comment thread
david-pl marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "ppvm-phased-pauli-word-2"
version = "0.1.0"
edition = "2024"
publish = false

[dependencies]
ppvm-traits-2 = { path = "../ppvm-traits-2" }
ppvm-pauli-word-2 = { path = "../ppvm-pauli-word-2" }

[dev-dependencies]
ppvm-lossy-pauli-word-2 = { path = "../ppvm-lossy-pauli-word-2" }
Loading
Loading