Skip to content

fix(ppvm-sym): correct real-valued results in Term arithmetic - #305

Merged
david-pl merged 1 commit into
mainfrom
claude/determined-keller-900142
Nov 19, 2026
Merged

david-pl merged 1 commit into
mainfrom
claude/determined-keller-900142

Conversation

@lkdvos

@lkdvos lkdvos commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Minimal v0.3.x fixes for ppvm-sym, limited to its real-valued semantics.

Scope. This PR assumes the only current consumer of ppvm-sym is real-valued: Heisenberg propagation of Hermitian observables, read out with Term::eval. Along that path the coefficients stay real, and eval ignores a monomial's phase. So this PR fixes only bugs that change those real values:

  1. Term += f64 on a single-monomial term silently did nothing.
  2. Term *= Term with a single-monomial left side and a Sum right side computed the product and discarded it.
  3. Sum × Sum dropped the cross terms of the constant part when it was negative (c0 > min_eps instead of c0.abs() > min_eps).
  4. Display of a Sum with 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 −1 can be stored two ways (as i² or as a sign), and there are known quirks around phase-only monomials and mul_phase. None of them changes a real eval result today. An earlier version of this PR patched them, but the patches made eval inconsistent, 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.rs output is unchanged.

Assisted by Claude Code

Copilot AI balanced review requested due to automatic review settings November 17, 2026 18:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@lkdvos
lkdvos requested review from JonhasA and david-pl November 17, 2026 19:00
@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-19 13:04 UTC

for (k, v) in old_terms.drain() {
self.add_term(k * p.clone(), v * coeff, max, min_eps);
}
self.c0 = 0.0;

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.

If you have two phased identity terms, this can delete it, e.g. multiplying something like (2i) * (-i).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Thanks, good catch! I think this should be fixed now, by taking the coefficient beforehand?

Comment thread crates/ppvm-sym/src/term.rs Resolved
Copilot AI balanced review requested due to automatic review settings November 18, 2026 12:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings November 18, 2026 12:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings November 18, 2026 15:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@lkdvos lkdvos changed the title fix(ppvm-sym): keep promoted sum, compose monomial phase, keep phase-only terms fix(ppvm-sym): correct real-valued results in Term arithmetic Nov 18, 2026
@lkdvos

lkdvos commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator Author

@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 ppvm-sym-2 crate, I made some changes that now only fix issues that are a bit more superficial. The main point is that I think this was only ever really correct for "hermitian" applications, where there actually aren't any phases at all, which is why this never really got caught. I will properly handle those in the sym-2 version, and made this a bit more minimal to make it easier to review and avoid adding in bugs here. Let me know what you think, happy to rework or even just leave this PR out if this is not really that important.

- `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
@lkdvos
lkdvos force-pushed the claude/determined-keller-900142 branch from 9c188a2 to 9f01ea3 Compare November 18, 2026 15:46
Copilot AI balanced review requested due to automatic review settings November 18, 2026 15:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@lkdvos
lkdvos requested a review from david-pl November 18, 2026 15:48
david-pl added a commit that referenced this pull request Nov 19, 2026
## 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>
@david-pl
david-pl merged commit 1de25f0 into main Nov 19, 2026
21 checks passed
@david-pl
david-pl deleted the claude/determined-keller-900142 branch November 19, 2026 13:04
@github-actions github-actions Bot mentioned this pull request Nov 19, 2026
david-pl pushed a commit that referenced this pull request Nov 20, 2026
## 🤖 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>
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.

3 participants