Skip to content
Open
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
4 changes: 3 additions & 1 deletion Cargo.lock

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

4 changes: 4 additions & 0 deletions crates/ppvm-pauli-sum-2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ ppvm-lossy-pauli-word-2 = { path = "../ppvm-lossy-pauli-word-2" }
ppvm-phased-pauli-word-2 = { path = "../ppvm-phased-pauli-word-2" }
num = "0.4"
approx = { version = "0.5.1", features = ["num-complex"] }

[dev-dependencies]
ppvm-pauli-sum = { path = "../ppvm-pauli-sum" }
criterion = "0.8.2"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Unused?

18 changes: 3 additions & 15 deletions crates/ppvm-pauli-sum-2/src/clifford.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,10 @@
// 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 ppvm_traits_2::{Clifford, CliffordBatch, Coefficient, PauliBits, Phase, 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))
Expand Down Expand Up @@ -126,6 +112,7 @@ impl<S, P> Clifford for Sum<S, P>
where
S: RekeyBijective + SignFlipByKey,
S::Key: PauliBits + LossInfo,
Phased<S::Key>: Clifford,
{
fn x(&mut self, qubit: usize) {
self.check_qubit(qubit);
Expand Down Expand Up @@ -192,6 +179,7 @@ impl<S, P> CliffordBatch for Sum<S, P>
where
S: RekeyBijective + SignFlipByKey,
S::Key: PauliBits + LossInfo,
Phased<S::Key>: Clifford,
{
fn x_many(&mut self, indices: &[usize]) {
if let &[qubit] = indices {
Expand Down
3 changes: 2 additions & 1 deletion crates/ppvm-pauli-sum-2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ mod compare;
mod display;
mod ops;
mod policy;
mod rotation;
mod store;
mod sum;

pub use policy::{
CoefficientThreshold, CombinedPolicy, MaxLossWeight, MaxPauliWeight, NoPolicy, Policy,
};
pub use store::{HashMapStore, RekeyBijective, SignFlipByKey, StoreAlloc};
pub use store::{BranchInPlace, HashMapStore, RekeyBijective, SignFlipByKey, StoreAlloc};
pub use sum::{PreserveSet, Sum};

pub use ppvm_lossy_pauli_word_2::LossyPauliWord;
Expand Down
250 changes: 250 additions & 0 deletions crates/ppvm-pauli-sum-2/src/rotation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
// SPDX-FileCopyrightText: 2026 The PPVM Authors
// SPDX-License-Identifier: Apache-2.0

//! Heisenberg rotations, with reduction and truncation left to the caller.
//! Two-site generators drop lost sites, leaving a rotation on the survivor.
//! For CRx, a lost control cancels the rotations; a lost target leaves
//! Rz(-theta/2) on the control.

use ppvm_traits_2::{
Angle, CRx, Coefficient, Pauli, PauliBits, RotXY, RotationOne, RotationOneBatch, RotationTwo,
RotationTwoBatch, Support, TGate, U3Gate, loss::LossInfo,
};

use crate::{BranchInPlace, Sum};

impl<S, P, A> RotationOne<S::Coeff, A> for Sum<S, P>
where
S: BranchInPlace,
S::Key: PauliBits + LossInfo,
A: Angle<S::Coeff>,
{
fn rotate_1(&mut self, axis: Pauli, qubit: usize, theta: A) {
self.check_qubit(qubit);
if axis == Pauli::I {
return;
}
let (sin, cos) = theta.sin_cos();
self.storage
.branch_in_place(|key, coeff| single_branch(key, coeff, axis, qubit, &sin, &cos));
}
}

impl<S: Support, P, A> RotationOneBatch<S::Coeff, A> for Sum<S, P>
where
Self: RotationOne<S::Coeff, A>,
A: Angle<S::Coeff>,
{
}

impl<S, P, A> RotationTwo<S::Coeff, A> for Sum<S, P>
where
S: BranchInPlace,
S::Key: PauliBits + LossInfo,
A: Angle<S::Coeff>,
{
fn rotate_2(&mut self, axis_a: Pauli, axis_b: Pauli, a: usize, b: usize, theta: A) {
self.check_pair(a, b);
let (sin, cos) = theta.sin_cos();
self.storage.branch_in_place(|key, coeff| {
// A lost site drops out of the generator, preserving the reference's
// single-qubit fallback on the surviving site.
let axis_a = if key.is_lost(a) { Pauli::I } else { axis_a };
let axis_b = if key.is_lost(b) { Pauli::I } else { axis_b };
let (mask_a, anti_a, negative_a) = local_product(axis_a, key.x_bit(a), key.z_bit(a));
let (mask_b, anti_b, negative_b) = local_product(axis_b, key.x_bit(b), key.z_bit(b));
if anti_a == anti_b {
return None;
}
// Exactly one local product anticommutes; the other has phase +1.
let negative = if anti_a { negative_a } else { negative_b };
let branch = coeff.clone() * sin.mul_sign(if negative { -1 } else { 1 });
*coeff *= &cos;
Some((key.toggled_bits2(a, mask_a, b, mask_b), branch))
});
}

#[inline]
fn rxx(&mut self, a: usize, b: usize, theta: A) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why do we need these overrides? Shouldn't the generic rotate_2 method be enough here?

self.check_pair(a, b);
let (sin, cos) = theta.sin_cos();
self.storage.branch_in_place(|key, coeff| {
if key.is_lost(a) {
return single_branch(key, coeff, Pauli::X, b, &sin, &cos);
}
if key.is_lost(b) {
return single_branch(key, coeff, Pauli::X, a, &sin, &cos);
}
let za = key.z_bit(a);
let zb = key.z_bit(b);
if za == zb {
return None;
}
let xa = key.x_bit(a);
let xb = key.x_bit(b);
let x_anti = if za { xa } else { xb };
let sign = if x_anti { -1 } else { 1 };
let branch = coeff.clone() * sin.mul_sign(sign);
*coeff *= &cos;
Some((
key.toggled_bits2(a, [true, false], b, [true, false]),
branch,
))
});
}

#[inline]
fn ryy(&mut self, a: usize, b: usize, theta: A) {
self.check_pair(a, b);
let (sin, cos) = theta.sin_cos();
self.storage.branch_in_place(|key, coeff| {
if key.is_lost(a) {
return single_branch(key, coeff, Pauli::Y, b, &sin, &cos);
}
if key.is_lost(b) {
return single_branch(key, coeff, Pauli::Y, a, &sin, &cos);
}
let xa = key.x_bit(a);
let za = key.z_bit(a);
let xb = key.x_bit(b);
let zb = key.z_bit(b);
let anti_a = xa != za;
if anti_a == (xb != zb) {
return None;
}
let x_anti = if anti_a { xa } else { xb };
let sign = if x_anti { 1 } else { -1 };
let branch = coeff.clone() * sin.mul_sign(sign);
*coeff *= &cos;
Some((key.toggled_bits2(a, [true, true], b, [true, true]), branch))
});
}

#[inline]
fn rzz(&mut self, a: usize, b: usize, theta: A) {
self.check_pair(a, b);
let (sin, cos) = theta.sin_cos();
self.storage.branch_in_place(|key, coeff| {
if key.is_lost(a) {
return single_branch(key, coeff, Pauli::Z, b, &sin, &cos);
}
if key.is_lost(b) {
return single_branch(key, coeff, Pauli::Z, a, &sin, &cos);
}
let xa = key.x_bit(a);
let xb = key.x_bit(b);
if xa == xb {
return None;
}
let za = key.z_bit(a);
let zb = key.z_bit(b);
let z_anti = if xa { za } else { zb };
let sign = if z_anti { 1 } else { -1 };
let branch = coeff.clone() * sin.mul_sign(sign);
*coeff *= &cos;
Some((
key.toggled_bits2(a, [false, true], b, [false, true]),
branch,
))
});
}
}

/// Generator mask, anticommutation, and sign of i * axis * word (when anticommuting).
#[inline]
fn local_product(axis: Pauli, x: bool, z: bool) -> ([bool; 2], bool, bool) {
match axis {
Pauli::I => ([false, false], false, false),
Pauli::X => ([true, false], z, x),
Pauli::Y => ([true, true], x != z, z),
Pauli::Z => ([false, true], x, !z),
}
}

impl<S: Support, P, A> RotationTwoBatch<S::Coeff, A> for Sum<S, P>
where
Self: RotationTwo<S::Coeff, A>,
A: Angle<S::Coeff>,
{
}

impl<S: Support, P> TGate for Sum<S, P>
where
Self: RotationOne<S::Coeff, f64>,
f64: Angle<S::Coeff>,
{
fn t(&mut self, qubit: usize) {
self.rz(qubit, std::f64::consts::FRAC_PI_4);
}

fn t_dag(&mut self, qubit: usize) {
self.rz(qubit, -std::f64::consts::FRAC_PI_4);
}
}

impl<S: Support, P, A> U3Gate<S::Coeff, A> for Sum<S, P>
where
Self: RotationOne<S::Coeff, A>,
A: Angle<S::Coeff>,
{
fn u3(&mut self, qubit: usize, theta: A, phi: A, lambda: A) {
// U3 = Rz(phi) Ry(theta) Rz(lambda); propagate in Heisenberg order.
self.rz(qubit, phi);
self.ry(qubit, theta);
self.rz(qubit, lambda);
}
}

impl<S: Support, P, A> RotXY<S::Coeff, A> for Sum<S, P>
where
Self: RotationOne<S::Coeff, A>,
A: Angle<S::Coeff> + Clone + std::ops::Neg<Output = A>,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you remove Neg here? Current symbolic angles don't have it implemented.

{
fn r(&mut self, qubit: usize, axis_angle: A, theta: A) {
self.rz(qubit, axis_angle.clone());
self.rx(qubit, theta);
self.rz(qubit, -axis_angle);
}
}

impl<S: Support, P, A> CRx<S::Coeff, A> for Sum<S, P>
where
Self: RotationOne<S::Coeff, A> + RotationTwo<S::Coeff, A>,
A: Angle<S::Coeff> + Clone + std::ops::Mul<f64, Output = A> + std::ops::Neg<Output = A>,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hm, okay, we may need some follow-up work on ppvm-sym-2. I don't think we can currently represent theta * 0.5 with symbolic angles. Let's keep that out of this PR though.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do you think it might make sense to immediately use the Halvable trait here? I can have a look at what the ppvm-sym-2 changes have to be after, but it is probably easier if the angle just needs to support .half().
(

pub trait Halvable: Coefficient {
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I thought about it but Halvable is not implemented for angles, but for Coefficient. But essentially, yes, it might be re-used. We basically need some symbolic representation of a half-angle, I think.

{
fn crx(&mut self, control: usize, target: usize, theta: A) {
self.check_pair(control, target);
// |1><1| ⊗ X = (IX - ZX)/2. The two generators commute.
// Loss follows the constituent rotations' surviving-site convention.
let half = theta * 0.5;
self.rx(target, half.clone());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is always executed on the target. If the control is lost or |0>, rounding errors could still lead to small branching. Probably not much of an issue in practice, but this could also be bad for performance since you'd have to do a branch -> truncate pass in order to get rid of that.

self.rzx(control, target, -half);
}
}

// Shared by single-qubit rotations and the surviving-site loss fallback.
#[inline]
fn single_branch<W: PauliBits + LossInfo + Clone, C: Coefficient>(
key: &W,
coeff: &mut C,
axis: Pauli,
qubit: usize,
sin: &C,
cos: &C,
) -> Option<(W, C)> {
if key.is_lost(qubit) {
return None;
}
let x = key.x_bit(qubit);
let z = key.z_bit(qubit);
// Heisenberg conjugation: Rx sends Z to cos(theta) Z + sin(theta) Y.
let (toggle_x, toggle_z, negative) = match axis {
Pauli::X if z => (true, false, x),
Pauli::Y if x != z => (true, true, z),
Pauli::Z if x => (false, true, !z),
_ => return None,
};
let branch = coeff.clone() * sin.mul_sign(if negative { -1 } else { 1 });
*coeff *= cos;
Some((key.toggled_bits(qubit, toggle_x, toggle_z), branch))
}
Loading
Loading