Skip to content

Update ChangeOpBasis to only accept operator inputs - #10511

Merged
JerryChen97 merged 38 commits into
mainfrom
cob-subcircuit
Nov 20, 2026
Merged

JerryChen97 merged 38 commits into
mainfrom
cob-subcircuit

Conversation

@mudit2812

@mudit2812 mudit2812 commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Context:
Currently, change_op_basis cannot be used with program capture when receiving quantum functions as input. To circumvent this, we unroll it before capture so that we capture the compute, target, and uncompute operators separately. This creates a major source of inefficiency because we lose the ability to decompose controlled COBs efficiently, because the decomposition of controlled COBs does not need to add controls to the compute and uncompute operators. Since we now have qp.subcircuit, we can continue allowing the use of quantum functions as input while enabling true capture compatibility by enforcing its use in place of vanilla quantum functions.

Description of the Change:

  • Update change_op_basis to stop unrolling when program capture is enabled.
    • Raise an informative error if the inputs aren't operators
  • Update decomposition rules across the ecosystem that used change_op_basis with quantum functions to now use qp.subcircuit

Benefits:

Possible Drawbacks:

Related GitHub Issues:

[sc-135561][sc-137499]

To reproduce the passing case from #10357, install this PR's PennyLane branch with Catalyst 0.16.0.dev144, then run the issue's sample unchanged, including qp.specs(node, level="all")(0.6931). Keep Toffoli excluded from the gate set. The example completes with CNOT, RZ and PauliX resources and no Toffoli requirement. Resource counts are conservative upper bounds. Broader shared-wire cases still depend on Catalyst #3470.

AI assistance was used with Codex

Require operator operands under capture and retain legacy callable support outside capture. Cover composite identity and mixed-operand retracing.

Authored with assistance from an AI assistant (Codex).
Replace callable operands across the affected rules with registered subcircuits or direct operators. Preserve composite resource dependencies and provide an internal subcircuit import path for built-in rules.

Validate capture replay, resource declarations, numerical behavior, and control handling. Targeted validation passed 1,182 tests and 21 source examples; Catalyst compilation remains unverified because generated bindings lack RuntimeCallOp.

Authored with assistance from an AI assistant (Codex).
Restore subcircuit to its original module and defer utility imports that
depend on partially initialized operator or template classes. Keep
built-in subcircuit classes at module scope and preserve callable package
exports, including aliases and directly imported dependencies.

Add a fresh-process regression covering imports, decomposition registration,
and serialization.

Validation: 249 focused PennyLane tests and 36 Catalyst tests passed.

Authored with assistance from OpenAI Codex.
Comment thread doc/releases/changelog-dev.rst Resolved
@JerryChen97 JerryChen97 self-assigned this Nov 16, 2026
@JerryChen97
JerryChen97 marked this pull request as ready for review November 17, 2026 17:12
Remove the temporary companion-wheel CI setup and match only the known shared-qubit compiler error in the four affected cases.

Assisted by Codex.
@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.39%. Comparing base (58a3d98) to head (272bc4f).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #10511   +/-   ##
=======================================
  Coverage   99.38%   99.39%           
=======================================
  Files         614      614           
  Lines       67703    67696    -7     
=======================================
- Hits        67290    67284    -6     
+ Misses        413      412    -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

🟡 Changes recommended

Multi-wire MultiRZ QJIT remains broken behind expected-failure marks, and _SelectTHCBasis omits live angle-register wires needed by matrix construction and wire-based transforms.

3 open issues

Review effort: Max

Comment thread tests/ops/qubit/test_parametric_ops.py Resolved

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.

🔵 Needs a closer look

Making rotation_data compilable embeds full QROM tables in capture metadata and resource keys, causing costly flattening and repeated full-table hashing for large THC inputs.

2 open issues
1 previously missed issue

In code that hasn't changed since last review

Medium severity Keep QROM table bits dynamic to avoid repeated serialization

pennylane/​templates/​subroutines/​select_thc.py:382

rotation_data includes all QROM table bits, so declaring it compilable puts those bits into capture metadata and _SelectTHCBasis resource keys instead of array operands. For large THC inputs, capture flattens the tables into scalar leaves and Operator2.__hash__ serializes the full tables on each call, adding repeated work proportional to their size. Keep only the precision and batch layout static; pass the tables as a dynamic boolean array and construct QROM resource declarations from its shape.

1 resolved since last review

Review effort: Max

@JerryChen97
JerryChen97 requested a max review from Copilot November 18, 2026 00:44
mudit2812 added a commit to PennyLaneAI/catalyst that referenced this pull request Nov 18, 2026
…operators (#3470)

**Context:**
Catalyst cannot currently support hybrid operators with multiple
operator arguments that share wires. This is a blocker for updates being
added to `change_op_basis` in
PennyLaneAI/pennylane#10511.

**Description of the Change:**

Frontend:
* When tracing a rule body, prepare dummy base-wire arguments in
alphabetical order of their wire argument names, with control work wires
appended separately. This keeps the generated slices consistent with
MLIR decomposition lowering.
* Lowering of the OperatorOp instance itself is unchanged, since it
already produces a sorted `qubit_map`.
* Pass abstract wire types to resource functions and compose nested
resource controls using operator objects.

MLIR:
* During decomposition lowering, gather rule wires by iterating through
`OperatorOp::getQubitMap()`, via `getRuleQubits()`.
* `OperatorOp::getWireLens()` now directly iterates through `qubit_map`
in both QRef and Quantum, without sorting it back into operand order.
* Allow `qref.operator` to opt out of the non-control qubit uniqueness
check. Control-control and control-target overlaps remain invalid.
* During value-semantics conversion, deduplicate aliased qubit operands
and remap `qubit_map` so each physical qubit is threaded once.

Regression tests exercise overlapping operator arguments using generic
composite operators. Existing tests continue to verify that
`change_op_basis` unrolls during capture.

**Benefits:**
Better support for composite operators

**Possible Drawbacks:**

**Related GitHub Issues:**

[sc-136999]

AI assistance was used with Codex
Reviewed using Codex Astra 6 on high.

@dwierichs dwierichs 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.

Amazing, thank you so much for this @mudit2812 @JerryChen97
It's so nice to see the abstraction match what's going on 😍

  • I'm wondering if we can make use of the default uncompute in more places, but let's look at that in a separate PR! :)
  • Some subroutines are capitalized, others are not. Should we capitalize consistently?

Comment thread doc/releases/changelog-dev.rst Resolved
Comment thread tests/ops/qubit/test_parametric_ops.py Resolved
Comment thread tests/ops/qubit/test_parametric_ops.py Resolved
Comment thread tests/testing/conftest.py Resolved

@dwierichs dwierichs 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.

Amazing, thank you so much for this @mudit2812 @JerryChen97
It's so nice to see the abstraction match what's going on 😍

  • I'm wondering if we can make use of the default uncompute in more places, but let's look at that in a separate PR! :)
  • Some subroutines are capitalized, others are not. Should we capitalize consistently?

dwierichs and others added 10 commits November 19, 2026 14:38
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Use PascalCase for @Subcircuit definitions because the decorator returns
an Operator2 subclass. Keep ordinary resource and decomposition functions
in snake_case, and preserve leading underscores for private classes.

Rename the 12 inconsistent subcircuits and update their references,
inspector output, and documentation examples.

Assisted by Codex.
@JerryChen97

Copy link
Copy Markdown
Contributor

Amazing, thank you so much for this @mudit2812 @JerryChen97 It's so nice to see the abstraction match what's going on 😍

  • I'm wondering if we can make use of the default uncompute in more places, but let's look at that in a separate PR! :)
  • Some subroutines are capitalized, others are not. Should we capitalize consistently?

Standardized the naming issue in 6dd470a @dwierichs

Describe the current initialization order: import the subcircuit decorator before the operator modules that use it.

Assisted by Codex.
Replace the private _THCSelect subcircuit with SelectTHC(...) @ Z(sign_wire). The product exposes both factors, preserves Z-then-SelectTHC execution order, and stays inside the enclosing compute-uncompute boundary.

Assisted by Codex.
Use a static reverse flag instead of separate forward and inverse subcircuit classes, matching the other basis helpers. Register direction-dependent resources and update the ordinary and phase-gradient callers and existing tests.

Assisted by Codex.
Add a shared test helper for validating a selected decomposition rule and the registered decompositions of its structured compute, target, and uncompute operands. Reuse it in the PPR, parametric basis change, and SingleExcitation tests, removing duplicated loops and operator-specific exclusions.

Assisted by Codex.

@dwierichs dwierichs 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.

Thanks for addressing everything, @JerryChen97 🎉
LGTM!

@JerryChen97
JerryChen97 added this pull request to the merge queue Nov 20, 2026
Merged via the queue into main with commit c651711 Nov 20, 2026
54 checks passed
@JerryChen97
JerryChen97 deleted the cob-subcircuit branch November 20, 2026 15:47
JerryChen97 added a commit to PennyLaneAI/catalyst that referenced this pull request Nov 20, 2026
…3499)

- [x] TODO: bump version again after PL TestPyPI uploaded

Update the ordinary and symbolic resource-collection tests to expect
retained `ChangeOpBasis2` nodes instead of flattened gates.

Blocked by:

- #3470: shared-wire hybrid operands (this PR's base).
- PennyLaneAI/pennylane#10511: retained basis changes during capture.

Assisted by Codex.

---------

Co-authored-by: Mudit Pandey <mudit.pandey@xanadu.ai>
Co-authored-by: Mudit Pandey <18223836+mudit2812@users.noreply.github.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.

4 participants