Skip to content

Trait 2/pauli sum cliff prop - Add Clifford propagation, arithmetic, and formatting - #303

Merged
JonhasA merged 11 commits into
trait-2/ppvm-pauli-sum-2from
trait-2/pauli-sum-cliff-prop
Nov 18, 2026
Merged

JonhasA merged 11 commits into
trait-2/ppvm-pauli-sum-2from
trait-2/pauli-sum-cliff-prop

Conversation

@JonhasA

@JonhasA JonhasA commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

To be merged after #302

This patch extends ppvm-pauli-sum-2 with Clifford propagation and sum arithmetic using the current traits. This includes the following:

  1. Add Clifford gates and batch support, absorbing conjugation signs into coefficients and respecting lost sites.
  2. Add addition, subtraction, and scalar multiplication with owned and borrowed operands.
  3. Add exact equality, absolute/relative approximate comparison, and weight-sorted Display/Debug formatting.

@JonhasA
JonhasA requested a review from david-pl November 17, 2026 05:56
@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.2
Preview removed because the pull request was closed.
2026-11-18 19:15 UTC

@david-pl david-pl left a comment

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 think this PR is good to go. Maybe add the CliffordBatch impl though. And in case #297 lands first, we should implement ApplyGateRule.

Did you run benchmarks here to make sure we're not regressing performance?


/// 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>

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.

FYI, once #297 lands, you can implement the ApplyGateRule trait instead (just two methods), which gives you Clifford for free and allows you to re-use the Lean-proven kernels.

Comment thread crates/ppvm-pauli-sum-2/src/clifford.rs Outdated
}
}

impl<S: Support, P> CliffordBatch for Sum<S, P> where Self: Clifford {}

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.

For performance we should add a dedicated batch impl here. Otherwise, you are re-hashing every step of the loop when one re-hash would do.

equal: impl Fn(&S::Coeff, &S::Coeff) -> bool,
) -> bool {
self.n_sites() == other.n_sites()
&& self.len() == other.len()

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.

There's an edge case here: say you have two maps {X: 1.0} and {X: 1.0, Z: 1e-17}. Technically, these could be equal if you set an epsilon > 1e-17 in AbsDiffEq below, but the length check makes it fail the check. I doubt it will be a problem in practice though, since truncation should take care of those.

JonhasA and others added 7 commits November 17, 2026 11:29
…, focused on reusing shared ppvm-traits-2 implementations and moved single term accumulation into shared usages
I ran some benchmarks comparing ppvm-paulisum-2 to ppvm-paulisum on the
original branch, with mixed results. There were some regressions when
applying single gates specifically. The root cause is that conjugating
terms moved every key around, even if the gate wouldn't touch the key.
Now, this is checked beforehand and performance is better in every
benchmark than on current main.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@JonhasA
JonhasA merged commit 97f6b48 into trait-2/ppvm-pauli-sum-2 Nov 18, 2026
17 checks passed
@JonhasA
JonhasA deleted the trait-2/pauli-sum-cliff-prop branch November 18, 2026 19:15
lkdvos added a commit that referenced this pull request Nov 20, 2026
#313)

## Summary

Adds `ppvm-sym-2`, the symbolic coefficient ring for the `-2` engine. A
`Term` is a polynomial in `sin(xᵥ)` and `cos(xᵥ)` of circuit parameters
with real `f64` coefficients, so a circuit is propagated once and
evaluated at many angles. It implements `Coefficient`, `Angle`,
`ImaginaryUnit`, `Conjugate` and `Halvable`, and depends only on
`ppvm-traits-2`.

## Design

```rust
struct Prod { factors: Vec<Factor>, sin_pow: usize, imag: bool } // [i] · Π sin(xᵥ)^a cos(xᵥ)^b
struct Sum  { c0: f64, terms: FxHashMap<Prod, f64>, aux: FxHashMap<Prod, f64> }
enum Inner  { Sum(Sum), One(Prod, f64), Const(f64) }
struct Term { inner: Inner, max_sin: usize, min_eps: f64 }
struct Var(u32) // an angle, not a Term
```

- **One representation per value.**
- A monomial keeps only an `imag` bit. Every phase operation returns the
±1 it moves into the coefficient.
- The real identity monomial is never a table key; constants live in
`c₀` or `Const`.
  - Cancelled monomials are removed, so `x − x` is structurally empty.
- **Equality by value:** `Const(c)` equals a constant-only `Sum`, and
`One` equals a one-entry `Sum`.
- **Truncation is an invariant.**
  - `max_sin` and `min_eps` apply to monomials, never to the constant.
- Setting them prunes; every operation truncates its result with the
left operand's settings, including `One × One`.
- **`Var` is its own angle type**, so a bare variable can't enter
arithmetic and no operator panics.
- **Evaluation:** `eval` is the real part of `eval_complex`, with a
per-call `sin`/`cos` cache.
- **No engine dependency:** scale a sum with `sum.scale(&term)`.
- **Only `Term`, `Var`, `MissingVariable` and the default thresholds are
public**; the representation above is crate-private.

## Why

- **The imag bit, with signs in the coefficient.**
- `ppvm-sym` stores `i^k` on the monomial, so −1 has two spellings;
every phase bug fixed in #305 traces back to that.
- Complex coefficients would double the cost of the real path, which
never needs `i`.
- Split real/imaginary tables would only speed up odd `mul_phase`, which
Clifford propagation (#303) never calls.
- **`One` and `Const` stay**, because most coefficients during
propagation are a single monomial (#204), and these forms never allocate
a hash map.
- **Settings live on `Term`, not `Sum`**, because `One` holds monomials
too.

## Differences from #204's `ppvm-sym-2`

| | #204 | This PR |
|---|---|---|
| Phase | `phase: u8` on the monomial | `imag` bit |
| Variables | `Inner::Var` | separate `Var` type |
| Engine | depends on `ppvm-pauli-sum-2`, for `sum *= term` | no
dependency |

## Open questions for reviewers

- **`magnitude`** is +∞ for symbolic terms, which is cautious, so
`CoefficientThreshold` never drops one. The ℓ¹ norm would bound the
value at every angle and allow more truncation.
- **No `sum *= term` operator.** It needs an edge between the ring and
the pauli engine in one direction or the other, since that isn't done
yet this is left out for now.
- **The bit-exact evaluation order.** Relaxing it would allow one pass
per factor and enable SIMD but requires reassociating floating point
operations.

## Testing

- **46 unit tests**, run over 14 forms that cover every representation
and phase:
  - evaluation is a ring homomorphism to ℂ;
  - the ring axioms hold for every triple;
  - `max_sin` truncation is exact for every form and operator;
  - a 60-step multiply chain tracks the running product.

## Follow-ups

- A benchmark against `ppvm-sym` (about 2.5× faster on the ring
workloads, checked locally), stacked on this PR.
- The `GaussianInt` trait test for `ppvm-traits-2`, taken from #204. (Do
we still want/need this?)
- Full-engine tests once `ppvm-pauli-sum-2` has rotations.

Assisted by [Claude Code](https://claude.com/claude-code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants