Skip to content

[Decomposition] Custom decomposition rules for symbolic operators #7347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 39 commits into from
May 20, 2025

Conversation

astralcai
Copy link
Contributor

@astralcai astralcai commented Apr 29, 2025

Context:

Description of the Change:

Symbolic operator types can now be given as strings to the op_type argument of add_decomps or as keys of the dictionaries passed to the alt_decomps and fixed_decomps arguments of the decompose transform, allowing custom decomposition rules to be defined and registered for symbolic operators.

@register_resources({qml.RY: 1})
def my_adjoint_ry(phi, wires, **_):
    qml.RY(-phi, wires=wires)

@qml.register_resources({qml.RX: 1})
def my_adjoint_rx(phi, wires, **__):
    qml.RX(-phi, wires)

# Registers a decomposition rule for the adjoint of RY globally
qml.add_decomps("Adjoint(RY)", my_adjoint_ry)

@partial(
    qml.transforms.decompose,
    gate_set={"RX", "CNOT"},
    fixed_decomps={"Adjoint(RX)": my_adjoint_rx}
)
@qml.qnode(qml.device("default.qubit"))
def circuit():
    qml.adjoint(qml.RX(0.5), wires=[0])
    qml.CNOT(wires=[0, 1])
    qml.adjoint(qml.RY(0.5), wires=[1])
    return qml.expval(qml.Z(0))
>>> print(qml.draw(circuit)())
0: ──RX(-0.50)─╭●────────────┤  <Z>
1: ────────────╰X──RY(-0.50)─┤

Benefits:

Possible Drawbacks:

Related GitHub Issues:

[sc-89297]

Copy link

codecov bot commented Apr 29, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.69%. Comparing base (0991a71) to head (fe18943).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7347      +/-   ##
==========================================
- Coverage   99.69%   99.69%   -0.01%     
==========================================
  Files         537      537              
  Lines       50955    50954       -1     
==========================================
- Hits        50800    50799       -1     
  Misses        155      155              

☔ View full report in Codecov by Sentry.
📢 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.

@astralcai astralcai requested a review from JerryChen97 April 29, 2025 18:56
@PietropaoloFrisoni PietropaoloFrisoni self-requested a review April 30, 2025 14:45
Copy link
Member

@PietropaoloFrisoni PietropaoloFrisoni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll complete the review as soon as possible (need to interrupt for a few minutes :) )

Co-authored-by: Pietropaolo Frisoni <[email protected]>
Copy link
Contributor

@JerryChen97 JerryChen97 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have more questions here! Just a good-to-have non-blocking comment

@astralcai astralcai added the do not merge ⚠️ Do not merge the pull request until this label is removed label May 14, 2025
@astralcai astralcai changed the base branch from master to conditional-decomp May 15, 2025 16:54
Base automatically changed from conditional-decomp to master May 20, 2025 20:22
@astralcai astralcai added this pull request to the merge queue May 20, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 20, 2025
@astralcai astralcai added this pull request to the merge queue May 20, 2025
@astralcai astralcai removed the do not merge ⚠️ Do not merge the pull request until this label is removed label May 20, 2025
@astralcai astralcai removed this pull request from the merge queue due to a manual request May 20, 2025
@astralcai astralcai enabled auto-merge May 20, 2025 21:20
@astralcai astralcai added this pull request to the merge queue May 20, 2025
Merged via the queue into master with commit 0346190 May 20, 2025
53 checks passed
@astralcai astralcai deleted the symbolic-rules-01 branch May 20, 2025 21:57
github-merge-queue bot pushed a commit that referenced this pull request May 21, 2025
**Context:**

This is a follow-up PR to
#7347

**Description of the Change:**

There was previously a lot of custom logic for retrieving decomposition
rules of symbolic operators. With the infrastructure added in
#7347, this PR is able to
clean up most of it.

- We no longer hard-code a list of operators whose adjoint is an
instance of the same type. Instead, each operator with a custom adjoint
decomposition registers a decomposition rule for the adjoint of itself.
For example, `Hadamard` was previously hardcoded as an operator that is
its own adjoint, now there is a decomposition rule for
`"Adjoint(Hadamard)"` which produces an instance of the base operator.
- We removed the constraint that powers of an operator must be integers.
Non-integer powers can be supported by adding custom decomposition rules
for `"Pow(SomeOp)"`. All operators in PL which overrides `pow` now has a
custom decomposition rule registered under the power of itself.
- Defined some general stock decomposition rules that can be registered
under any operator. For example, a `self_adjoint` decomposition rule is
defined, and it is registered with all operators that are self-adjoint
(to avoid having duplicate code)

**Related Shortcut Stories:**

[sc-90114]

---------

Co-authored-by: Pietropaolo Frisoni <[email protected]>
github-merge-queue bot pushed a commit that referenced this pull request May 23, 2025
…7362)

**Context:**

As a follow up to #7347,
most of the custom logic for handling controlled decompositions can be
generalized away.

**Description of the Change:**

- `controlled_resource_rep` now mirrors the custom dispatch logic of
`qml.ctrl` for consistency.
- We no longer refer to a hard-coded list of custom controlled operators
when constructing the decomposition graph, instead, each operator that
has a corresponding custom controlled operator now has a decomposition
rule registered under its controlled version, e.g., decomposition rules
added for `"C(H)"` that produces a `CH`.
- Defined controlled decomposition rules for `QubitUnitary`, `H`, `X`,
`Y`, `Z`, `SWAP`, `RX`, `RY`, `RZ`, `Rot`. `PhaseShift`, `GlobalPhase`.

**Benefits:**

**Possible Drawbacks:**

**Related GitHub Issues:**

[sc-90116]

---------

Co-authored-by: Pietropaolo Frisoni <[email protected]>
Co-authored-by: Yushao Chen (Jerry) <[email protected]>
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.

5 participants