Skip to content

Preserve change-op-basis structure in adjoint rule of ChangeOpBasis2 - #10568

Merged
dwierichs merged 44 commits into
mainfrom
adjoint-change-op-basis2
Nov 21, 2026
Merged

dwierichs merged 44 commits into
mainfrom
adjoint-change-op-basis2

Conversation

@dwierichs

@dwierichs dwierichs commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Context:

Since #10511 we keep ChangeOpBasis2 intact under program capture, so C(ChangeOpBasis2) only controls the target. The adjoint rule, however, still splits the operator into three plain operations: compute, adjoint(target), uncompute, without the change-op-basis structure.
Thus, controlled adjoints end up controlling all three parts.

While fixing this, I found that the adjoint was also wrong when uncompute_op is given explicitly and isn't the adjoint of compute_op. Both the rule and ChangeOpBasis2.adjoint() kept compute and uncompute unchanged, which only gives the right unitary when uncompute is the adjoint of compute. The existing test test_adjoint_decomposition_with_explicit_uncompute didn't catch this, because the operator's matrix is computed from the same decomposition.

Description of the Change:

  • The Adjoint(ChangeOpBasis2) rule now emits a single ChangeOpBasis2(adjoint(uncompute), adjoint(target), inv(compute)), and its resources are that one operator.
  • a similar fix is applied to the legacy change_op_basis
  • ChangeOpBasis2.adjoint() now inverts and swaps compute and uncompute in the same way.
  • The new _inverse helper unwraps an existing Adjoint/Adjoint2 rather than nesting another adjoint. In the common case where uncompute defaults to adjoint(compute), the adjoint keeps the original compute_op, with no Adjoint(Adjoint(...)) resource keys.

Benefits:

Possible Drawbacks:

Related GitHub Issues:

Follow-up to #10511.

Made with AI

mudit2812 and others added 30 commits October 30, 2026 12:22
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.
Remove the temporary companion-wheel CI setup and match only the known shared-qubit compiler error in the four affected cases.

Assisted by Codex.
Updated changelog to reflect changes in change_op_basis function regarding operator instances and program capture.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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>
JerryChen97 and others added 8 commits November 19, 2026 13:13
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.
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.
The adjoint rule of ChangeOpBasis2 now emits a ChangeOpBasis2 with the
inverted target, so that controlling the adjoint only controls the
target. The adjoint also inverts and swaps compute and uncompute, which
fixes adjoints with an explicit uncompute_op that is not the adjoint of
compute_op.

Assisted-by: Cursor (Claude)
Co-authored-by: Cursor <cursoragent@cursor.com>
Assisted-by: Cursor (Claude)
Co-authored-by: Cursor <cursoragent@cursor.com>
@dwierichs
dwierichs marked this pull request as ready for review November 20, 2026 08:22
@dwierichs dwierichs changed the title [AI-generated] Preserve the basis change in the adjoint of ChangeOpBasis2 Preserve change-op-basis structure in adjoint rule of ChangeOpBasis2 Nov 20, 2026
dwierichs and others added 2 commits November 20, 2026 09:34
The adjoint rule now introduces basis-change nodes, which adds 10
entries to the all_decomps doctest and flips an RX/RZ cost tie in the
decomposition inspector test. Restore the pylint suppression that CI
requires.

Assisted-by: Cursor (Claude)
Co-authored-by: Cursor <cursoragent@cursor.com>
@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.38%. Comparing base (2faa29b) to head (b1da21c).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #10568      +/-   ##
==========================================
- Coverage   99.38%   99.38%   -0.01%     
==========================================
  Files         613      613              
  Lines       67668    67673       +5     
==========================================
+ Hits        67255    67259       +4     
- Misses        413      414       +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.

Base automatically changed from cob-subcircuit to main November 20, 2026 15:47
dwierichs and others added 2 commits November 20, 2026 22:40
The legacy Adjoint(ChangeOpBasis) rule applied compute, adjoint(target)
and uncompute, which is only correct if uncompute is the adjoint of
compute. It now applies the adjoints of uncompute, target and compute,
unwrapping operands that already are adjoints. The adjoint tests now
also run with program capture enabled, and the changelog entry for the
structured adjoint is limited to Operator2 operands.

Assisted-by: Cursor (Claude)
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>

# Conflicts:
#	doc/releases/changelog-dev.rst
#	pennylane/decomposition/all_decomps.py
@dwierichs

Copy link
Copy Markdown
Contributor Author

Review by Opus 5.5 on high effort.

Review summary

Verdict: Approve once CI on the merged head is green.

Correctness

  • ChangeOpBasis2(C, T, U) applies C, then T, then U, so its inverse is ChangeOpBasis2(U†, T†, C†). The Adjoint(ChangeOpBasis2) rule and ChangeOpBasis2.adjoint() now both produce this. Before, both were wrong whenever an explicit uncompute_op is not the adjoint of compute_op.
  • The legacy Adjoint(ChangeOpBasis) rule had the same bug (it applied C, T†, U). It now applies U†, T†, C†.
  • Both rules unwrap operands that are already adjoints. In the common case where uncompute defaults to adjoint(compute), this avoids nested Adjoint(Adjoint(...)) nodes and resource keys.
  • Resource estimates match the emitted gates (assert_valid_decomp_rule) with and without program capture. I checked asymmetric, default, adjoint-compute and parametrized operands.

Tests

  • New regression tests for both operator types fail on the old rules. The legacy test checks the matrix explicitly, because ChangeOpBasis has has_matrix = False and assert_decomposable skips the matrix comparison for it.
  • The adjoint tests run both with and without capture (enable_and_disable_capture).

Scope note

  • Without capture, C(Adjoint(ChangeOpBasis2)) already controls only the target via flip_control_adjoint, so gate counts on the tape path don't change. The structured adjoint matters under capture, where flip_control_adjoint currently hands back the same C(Adjoint(...)) it was given. That is a separate, pre-existing issue that may deserve its own ticket.

@dwierichs
dwierichs added this pull request to the merge queue Nov 21, 2026
Merged via the queue into main with commit bdcd527 Nov 21, 2026
54 checks passed
@dwierichs
dwierichs deleted the adjoint-change-op-basis2 branch November 21, 2026 08:59
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