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

Open
wants to merge 22 commits into
base: master
Choose a base branch
from

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.65%. Comparing base (353fb36) to head (220f671).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7347      +/-   ##
==========================================
- Coverage   99.65%   99.65%   -0.01%     
==========================================
  Files         529      529              
  Lines       50603    50600       -3     
==========================================
- Hits        50427    50424       -3     
  Misses        176      176              

☔ 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

Comment on lines 165 to 171
if issubclass(op.op_type, qml.ops.Adjoint):
decomps.extend(self._get_adjoint_decompositions(op))

elif issubclass(op.op_type, qml.ops.Pow):
decomps.extend(self._get_pow_decompositions(op))

elif op.op_type in (qml.ops.Controlled, qml.ops.ControlledOp):
Copy link
Contributor

Choose a reason for hiding this comment

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

We do we use issubclass for adjoint and pow but op_type in for controlled? Seems like it might be simpler to use the same pattern for all three categories.

Copy link
Contributor

Choose a reason for hiding this comment

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

Note that AdjointOpObs and PowOpObs will be going away in July, so we can potentially make decisions based around it being removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For Controlled, we don't want any of the custom controlled operations like CH, CNOT, etc. to take this branch, but for Adjoint, we want all Adjoint ops including AdjointOperation to take this branch.

@@ -208,88 +215,65 @@ def _add_decomp_rule_to_op(
except DecompositionNotApplicable:
pass # ignore decompositions that are not applicable to the given op params.

def _add_adjoint_decomp_node(self, op_node: CompressedResourceOp, op_node_idx: int) -> int:
"""Adds an adjoint decomposition node."""
def _get_adjoint_decompositions(self, op: CompressedResourceOp) -> list[DecompositionRule]:
Copy link
Contributor

Choose a reason for hiding this comment

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

What about using something like resource_op when we are dealing with a CompressedResourceOp. Might make it a little clearer what of object we are dealing with, as I tend to associate op with Operator.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm renaming them back to op_node for consistency in the context of the graph.

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