Skip to content

Complete legacy tracing for dynamic magic wires#3027

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

Complete legacy tracing for dynamic magic wires#3027
Qottmann wants to merge 2 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.
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>
@Qottmann
Qottmann force-pushed the qottmann/allocate-magic branch from 265e202 to 8327ff1 Compare July 22, 2026 11:03
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