Repository navigation
fix(ppvm-sym): correct real-valued results in Term arithmetic - #305
Conversation
|
| for (k, v) in old_terms.drain() { | ||
| self.add_term(k * p.clone(), v * coeff, max, min_eps); | ||
| } | ||
| self.c0 = 0.0; |
There was a problem hiding this comment.
If you have two phased identity terms, this can delete it, e.g. multiplying something like (2i) * (-i).
There was a problem hiding this comment.
Thanks, good catch! I think this should be fixed now, by taking the coefficient beforehand?
|
@david-pl, after a bit more careful analysis of what was already in here and the amount of work to do here vs what to defer to the |
- `Term += f64` on a single-monomial term built the promoted `Sum` and never assigned it, so the addition was a silent no-op. - `Term *= Term` with a single-monomial lhs and a `Sum` rhs computed the product via `mul_term` but never assigned it, returning the lhs. - `Sum x Sum` gated the `c0 * terms` cross products on `c0 > min_eps` instead of `c0.abs() > min_eps`, dropping them for negative constants. - `Display for Sum` returned early for a constant-only sum, dropping the closing bracket (`"[3.000 "` instead of `"[3.000]"`). Phase handling is unchanged: `eval` ignores a monomial's phase in v0.3, and the phase representation is reworked in the sym-2 crate. Assisted-by: Claude Code
9c188a2 to
9f01ea3
Compare
## Summary - #273 renamed the `python-tests` job to `Python tests (${{ matrix.python-version }})`. The `main` ruleset still requires a check called `Python tests`, so every PR now waits on it forever (e.g. #305). - This adds an aggregator job named `Python tests`. It runs with `if: always()` after the matrix and passes only if every leg succeeded. The required check is satisfied again, and changing the matrix later won't mean editing the ruleset. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
## 🤖 New release * `ppvm-traits`: 0.4.0 -> 0.4.1 * `ppvm-pauli-word`: 0.4.0 -> 0.4.1 * `ppvm-pauli-sum`: 0.4.0 -> 0.4.1 * `ppvm-sym`: 0.4.0 -> 0.4.1 (✓ API compatible changes) * `ppvm-tableau`: 0.4.0 -> 0.4.1 * `stim-parser`: 0.4.0 -> 0.4.1 * `ppvm-stim`: 0.4.0 -> 0.4.1 * `ppvm-tableau-sum`: 0.4.0 -> 0.4.1 * `vihaco-circuit-isa`: 0.4.0 -> 0.4.1 * `ppvm-vihaco`: 0.4.0 -> 0.4.1 * `ppvm-tui`: 0.4.0 -> 0.4.1 * `ppvm-cli`: 0.4.0 -> 0.4.1 <details><summary><i><b>Changelog</b></i></summary><p> ## `ppvm-cli` <blockquote> ## [0.4.1](v0.4.0...v0.4.1) - 2026-11-19 ### Fixed - *(ppvm-sym)* correct real-valued results in Term arithmetic ([#305](#305)) </blockquote> </p></details> --- This PR was generated with [release-plz](https://github.com/release-plz/release-plz/). Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
#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)
Minimal v0.3.x fixes for
ppvm-sym, limited to its real-valued semantics.Scope. This PR assumes the only current consumer of
ppvm-symis real-valued: Heisenberg propagation of Hermitian observables, read out withTerm::eval. Along that path the coefficients stay real, andevalignores a monomial's phase. So this PR fixes only bugs that change those real values:Term += f64on a single-monomial term silently did nothing.Term *= Termwith a single-monomial left side and aSumright side computed the product and discarded it.Sum × Sumdropped the cross terms of the constant part when it was negative (c0 > min_epsinstead ofc0.abs() > min_eps).Displayof aSumwith only a constant printed"[3.000 "; it now prints"[3.000]".Not in scope: phase handling. A monomial carries a phase
iᵏnext to a signed coefficient, so−1can be stored two ways (asi²or as a sign), and there are known quirks around phase-only monomials andmul_phase. None of them changes a realevalresult today. An earlier version of this PR patched them, but the patches madeevalinconsistent, so that part is reverted here. The phase representation will be reworked properly in the sym-2 crate.Each fix has a regression test that fails without it. The
examples/symbolic.rsoutput is unchanged.Assisted by Claude Code