Repository navigation
Trait 2/pauli sum cliff prop - Add Clifford propagation, arithmetic, and formatting - #303
Merged
Merged
Conversation
Contributor
|
david-pl
reviewed
Nov 17, 2026
|
|
||
| /// 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> |
Collaborator
There was a problem hiding this comment.
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.
| } | ||
| } | ||
|
|
||
| impl<S: Support, P> CliffordBatch for Sum<S, P> where Self: Clifford {} |
Collaborator
There was a problem hiding this comment.
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() |
Collaborator
There was a problem hiding this comment.
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.
…, focused on reusing shared ppvm-traits-2 implementations and moved single term accumulation into shared usages
…ing s directory on s_many
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>
david-pl
approved these changes
Nov 18, 2026
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)
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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: