Skip to content

feat: Quil arithmetic expressions as gate arguments in the exact simulators - #1869

Open
bramathon wants to merge 2 commits into
1855-experimental-exact-simulatorsfrom
1860-affine-parameters
Open

feat: Quil arithmetic expressions as gate arguments in the exact simulators#1869
bramathon wants to merge 2 commits into
1855-experimental-exact-simulatorsfrom
1860-affine-parameters

Conversation

@bramathon

@bramathon bramathon commented Sep 11, 2026

Copy link
Copy Markdown
Collaborator

Mini-PR onto 1855-experimental-exact-simulators (#1860), as requested in the review discussion there.

What

A gate angle may now be any Quil arithmetic expression over memory references: + - * / ^, the functions SIN, COS, SQRT, EXP, CIS, and real or complex literals. RX(theta[0]/2 + pi) (what quilc emits for a compiled parametric program), RZ(2*SIN(phi[1])) and CPH(CIS(theta[0])) 0 for a DEFGATE with a complex parameter all simulate directly instead of failing at construction.

How

  • _resolver._compile_expression walks the pyQuil expression tree and produces a ParameterExpression: the slots it reads, a JAX closure, and a shape key with references replaced by %0, %1, … (SIN(theta[0]) and SIN(theta[1]) share the key SIN(%0)). ParametricGate.arguments holds a literal or a ParameterExpression per gate argument.
  • _GateBatch keys on (gate, literals, expression shapes, embedding) and gathers each member's slot values, so same-shaped expressions still share one vmap; the traced graph stays proportional to the number of distinct gate kinds.
  • Complex-valued arguments (CIS, a complex literal) are accepted for DEFGATE gates and rejected with a clear error for the built-in gates, which take real angles (quax's gate constructors reject complex angles). SQRT/^ are evaluated in real arithmetic; a negative radicand gives nan where Quil would give a complex number, which the docstring says.
  • Parameter-free expressions such as SIN(pi/4) fold to literals.

Tests

test/unit/test_parameter_expressions.py (48 tests): shape keys and slot order for 13 forms, key sharing across slots, the complex flag, numeric evaluation against Quil semantics (including CIS and complex literals), equality with the literal gate for 12 expression shapes, a quilc-style Quil-text program, CIS/complex arithmetic through a DEFGATE, the complex-argument error for built-in gates, feed-forward still rejected, slot sharing/batching, jit + grad through RX(SIN(theta)) (analytic derivative), and literal folding.

🤖 Generated with Claude Code

https://claude.ai/code/session_01JfZKmgqinMhR4BS87y4G4F

@github-actions

github-actions Bot commented Sep 11, 2026

Copy link
Copy Markdown

🐰 Bencher Report

Projectpyquil
Branch1860-affine-parameters
Testbedci-runner-linux

⚠️ WARNING: No Threshold found!

Without a Threshold, no Alerts will ever be generated.

Click here to create a new Threshold
For more information, see the Threshold documentation.
To only post results if a Threshold exists, set the --ci-only-thresholds flag.

Click to view all benchmark results
BenchmarkLatencyseconds (s)
test/benchmarks/test_program.py::test_copy_everything_except_instructions📈 view plot
⚠️ NO THRESHOLD
9.61 s
test/benchmarks/test_program.py::test_instructions📈 view plot
⚠️ NO THRESHOLD
3.35 s
test/benchmarks/test_program.py::test_iteration📈 view plot
⚠️ NO THRESHOLD
3.31 s
🐰 View full continuous benchmarking report in Bencher

A gate angle may now be any affine expression a*theta + b in a single
memory reference (RX(theta[0]/2 + pi), RZ(-2*phi[1]), RX(theta + theta)),
which is what quilc emits when it compiles a parametric program. The
scale and offset travel with the ParametricGate and are per-member data of
a gate batch, so the batching key is unchanged and such gates still share
one vmap with plain references. Products, powers and functions of
references are rejected with an error naming the parameter.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JfZKmgqinMhR4BS87y4G4F
@bramathon
bramathon force-pushed the 1860-affine-parameters branch from 6b2134c to 85c393d Compare September 11, 2026 09:50
Replace the affine-only parser with a compiler from pyQuil expression
trees to JAX functions: + - * / ^, SIN, COS, SQRT, EXP, CIS, and real or
complex literals. A ParameterExpression carries the slots it reads and a
shape key (references replaced by %0, %1, ...); gate batches are keyed by
shape, so SIN(theta[0]) and SIN(theta[1]) still share one vmap and differ
only in the slots gathered per member. Complex-valued arguments (CIS, a
complex literal) are accepted for DEFGATE gates and rejected with a clear
error for the built-in gates, whose angles are real. Parameter-free
expressions such as SIN(pi/4) fold to literals.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JfZKmgqinMhR4BS87y4G4F
@bramathon bramathon changed the title feat: affine gate-parameter expressions in the exact simulators feat: Quil arithmetic expressions as gate arguments in the exact simulators Sep 11, 2026
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.

1 participant