Skip to content

Complete legacy tracing for dynamic magic wires - #3027

Open
Qottmann wants to merge 10 commits into
qottmann/allocate-magic-corefrom
qottmann/allocate-magic
Open

Complete legacy tracing for dynamic magic wires#3027
Qottmann wants to merge 10 commits into
qottmann/allocate-magic-corefrom
qottmann/allocate-magic

Conversation

@Qottmann

@Qottmann Qottmann commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds all legacy (capture=False) Catalyst support for magic-T dynamic wires. Stacks on #3029, which lands MLIR lowering and the capture path only.

Diff: 6 files, +429 / −42 (single commit on top of #3029).

This PR owns every jax_tracer.py change (+218 lines): qref-mode tracing, allocate/deallocate, MCM/PPM routing, control-flow wire propagation, register lifetime, and a compiler fix.

Enables TemporaryAND-style workflows: allocate magic-T ancillas → PPM → reactive qp.cond.

Legacy jax_tracer foundation

  • QRegPromise qref mode + dynamic-wire → qubit mapping
  • _trace_allocate / _trace_deallocate via unified allocate_prim / deallocate_prim
  • Route observables through qref compbasis when dynamic wires present
  • Enable qref mode when tape uses dynamic allocation

Mid-circuit measurement routing

  • MidCircuitPauliMeasureqref_pauli_measure_p when dynamic wires present
  • MidCircuitMeasureqref_measure_p when dynamic wires present

Control-flow dynamic-wire propagation

  • Inherit wire mappings into nested cond / for / while / adjoint regions
  • Preserve mappings after control-flow ops for post-branch deallocate

Register lifetime & ordering

  • Deallocate qref device register at end of legacy tape
  • Defer deallocation equations until after all qubit users (sort_eqns)

ppr-to-ppm fix

  • Sink quantum.dealloc_qb past late PPM ops to avoid magic-qubit use-after-dealloc

Tests (+114 pytest lines)

  • Non-capture zero-state + magic-T smoke tests
  • Magic-T allocation (capture=True/False), pauli_measure, mid-circuit measure
  • Legacy qp.cond with pre-allocated dynamic wires

Review guide

Area Files
Legacy tracing core frontend/catalyst/jax_tracer.py
MCM/PPM frontend/catalyst/api_extensions/quantum_operators.py
Control flow frontend/catalyst/api_extensions/control_flow.py
Equation ordering frontend/catalyst/jax_extras/tracing.py
Compiler mlir/lib/PBC/Transforms/ppr_to_ppm.cpp

Test plan

  • frontend/test/pytest/test_dynamic_qubit_allocation.py (legacy + dual-mode tests)
  • make format check=1 (clang-format, black, isort)
  • Merge after #3029

Related

@Qottmann Qottmann changed the title Support magic state allocation via qp.allocate in catalyst Complete legacy tracing for dynamic magic wires Jul 16, 2026
@Qottmann
Qottmann changed the base branch from main to qottmann/allocate-magic-core July 16, 2026 15:08
joshuasteier pushed a commit to joshuasteier/pennylane that referenced this pull request Jul 21, 2026
## Summary
- Extend `AllocateState` and `qp.allocate()` with `state="magic"` and
`state="magic_conj"` for PBC-aligned magic state allocation.
- Reject `restored=True` for magic state allocations since those states
cannot be restored before deallocation.
- Raise `AllocationError` in `resolve_dynamic_wires` for magic states
until device simulator support is added.

## Test plan
- [x] `tests/test_allocation.py` — enum values, capture jaxpr params,
`restored=True` error, `resolve_dynamic_wires` error
- [x] CI

Merge this PR before the companion [Catalyst
PR](PennyLaneAI/catalyst#3027)

Made with [Cursor](https://cursor.com)

Expose the PBC magic state preparation routine [sc-124605]

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
@Qottmann
Qottmann force-pushed the qottmann/allocate-magic branch from 56de60b to 57f2855 Compare July 22, 2026 10:02
Qottmann added a commit that referenced this pull request Jul 22, 2026
Keep #3029 focused on MLIR lowering and the capture path. Legacy tape
tracing, non-capture tests, and jax_tracer qref-mode changes move to #3027.

Co-authored-by: Cursor <cursoragent@cursor.com>
@Qottmann
Qottmann force-pushed the qottmann/allocate-magic branch from 57f2855 to c1e71d2 Compare July 22, 2026 10:17
Qottmann added a commit that referenced this pull request Jul 22, 2026
Keep #3029 focused on MLIR lowering and the capture path. Legacy tape
tracing, non-capture tests, and jax_tracer qref-mode changes move to #3027.

Co-authored-by: Cursor <cursoragent@cursor.com>
@Qottmann
Qottmann force-pushed the qottmann/allocate-magic-core branch from efcea1a to 541804b Compare July 22, 2026 10:49
@Qottmann
Qottmann force-pushed the qottmann/allocate-magic branch from c1e71d2 to 265e202 Compare July 22, 2026 10:49
Qottmann added a commit that referenced this pull request Jul 22, 2026
Restore capture=False allocate error coverage, add negative deallocate tests,
consolidate deallocate guard logic, document staged fabricate primitives for

Co-authored-by: Cursor <cursoragent@cursor.com>
#3027, and fix the sharp_bits.rst cross-reference syntax.
@Qottmann
Qottmann force-pushed the qottmann/allocate-magic branch 2 times, most recently from b51910e to ebfaee5 Compare July 23, 2026 10:13
Qottmann added a commit that referenced this pull request Jul 23, 2026
Keep #3029 focused on MLIR lowering and the capture path. Legacy tape
tracing, non-capture tests, and jax_tracer qref-mode changes move to #3027.

Co-authored-by: Cursor <cursoragent@cursor.com>
Qottmann added a commit that referenced this pull request Jul 23, 2026
Restore capture=False allocate error coverage, add negative deallocate tests,
consolidate deallocate guard logic, document staged fabricate primitives for

Co-authored-by: Cursor <cursoragent@cursor.com>
#3027, and fix the sharp_bits.rst cross-reference syntax.
@Qottmann
Qottmann force-pushed the qottmann/allocate-magic-core branch from bb4d012 to 391268d Compare July 23, 2026 10:34
@Qottmann
Qottmann force-pushed the qottmann/allocate-magic branch from ebfaee5 to 01ee275 Compare July 23, 2026 10:34
Qottmann and others added 3 commits July 23, 2026 13:00
Own all jax_tracer legacy support on top of #3029: qref-mode dynamic
wires, allocate/deallocate tracing, MCM/PPM routing, control-flow wire
propagation, qref register lifetime, and ppr-to-ppm deallocation ordering.
Includes legacy and dual-mode pytest coverage.

Co-authored-by: Cursor <cursoragent@cursor.com>
Combine capture and legacy tracing into a single release-note entry for the
full allocate feature, following changelog grouping guidelines.

Co-authored-by: Cursor <cursoragent@cursor.com>
Replace removed qref_dealloc_qb_p with PennyLane deallocate_prim after the
primitive cleanup in the stacked core PR.

Co-authored-by: Cursor <cursoragent@cursor.com>
@Qottmann
Qottmann force-pushed the qottmann/allocate-magic branch from 01ee275 to 3fe4bd1 Compare July 23, 2026 11:00
JerryChen97 pushed a commit that referenced this pull request Jul 23, 2026
## Summary

PennyLane **dev62** is the minimum pin that includes both
[PennyLaneAI/pennylane#9846](PennyLaneAI/pennylane#9846)
(`magic-T`) and
[PennyLaneAI/pennylane#9841](PennyLaneAI/pennylane#9841)
(resource API). This PR bumps Catalyst to that pin and fixes the
resulting fallout.

**Resource API migration (#9841)**
- Bump PennyLane to `0.46.0.dev62` in `.dep-versions` and
`doc/requirements.txt`
- Replace `SymbolicSpecsResources` checks with
`SpecsResources.is_symbolic`
- Rename resource fields in tests: `gate_types`/`gate_counts` →
`quantum_operations`, `measurements` → `measurement_processes`,
`num_gates` → `total_quantum_operations`
- Drop `gate_sizes` assertions (no longer tracked)
- Handle `PBCSpecsResources` in PPR specs tests (depth fields)

**dev62 fallout (Operator2 / magic-T)**
- `decompose.py`: safe `.params` access for Operator2 ops; skip abstract
`Adjoint(...)` / `Controlled(...)` decomposition-graph nodes that lack
`op_type`
- PLXPR tests: single-qubit ops like `S`/`T` now lower to
`qref_operator` instead of `qref_qinst`
- `test_adjoint.py`: replace removed `qp.Rotation` with `qp.Rot`
- `test_quantum_control.py`: compare `ControlledOp2` decompositions with
`qp.equal`
- `test_specs.py`: normalize doubly-controlled `C(S)` counts in
`test_complex`

This is the base branch for the magic-state allocation stack (`#3029` →
`#3027`).

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
Qottmann and others added 4 commits July 24, 2026 13:09
…gic.

Resolve test file conflicts: keep legacy MCM/PPM tests from #3027, drop
redundant MLIR pytest removed on core, and remove obsolete capture-only error test.

Co-authored-by: Cursor <cursoragent@cursor.com>
Use quantum.custom result wires in reference-semantics conversion tests and
tie ref-side CHECKs to the fabricated qubit in QRef fabricate lit tests.

Co-authored-by: Cursor <cursoragent@cursor.com>
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