Skip to content

Conversation

@maliasadi
Copy link
Member

@maliasadi maliasadi commented Sep 4, 2025

  • Catalyst uses the new graph-based decomposition system, resulting in the same final gate set as PennyLane.
  • A new MLIR decompose-lowering will rewrite the circuit and apply decomposition rules in the core Catalyst compiler.
  • Circuits containing structure (for and while loops) can be decomposed with Catalyst present.
  • Decomposition rules with classical StableHlo ops can be compiled [sc-100673]
  • Decomposition rules can include dynamically allocated wires and still work with Catalyst [WIP]
  • Decomposition rules can be manually added to operations via add_decomps and fixed/alt_decomps.
qml.capture.enable()
qml.decomposition.enable_graph()

@qml.qjit
@partial(qml.transforms.decompose, gate_set={qml.H, qml.CNOT, qml.ControlledPhaseShift})
@qml.qnode(qml.device("null.qubit"))
def circuit():
    [qml.Hadamard(i) for i in range(4)]
    qml.QFT(wires=range(4))
    return qml.state()

@codecov
Copy link

codecov bot commented Sep 4, 2025

Codecov Report

❌ Patch coverage is 99.20635% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 97.46%. Comparing base (0a78ffe) to head (f1d0fb5).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
frontend/catalyst/from_plxpr/decompose.py 98.68% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2029      +/-   ##
==========================================
+ Coverage   97.45%   97.46%   +0.01%     
==========================================
  Files          90       91       +1     
  Lines       10476    10591     +115     
  Branches      972      990      +18     
==========================================
+ Hits        10209    10323     +114     
+ Misses        213      212       -1     
- Partials       54       56       +2     

☔ 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.

@maliasadi maliasadi force-pushed the feature/handle_gdecomp_v4 branch from f22bcd7 to 02be034 Compare September 5, 2025 18:01
@paul0403
Copy link
Member

paul0403 commented Sep 9, 2025

Just a question: what is this PR's relationship to #1962 ?

@maliasadi
Copy link
Member Author

Just a question: what is this PR's relationship to #1962 ?

@paul0403 The main difference is the scope! This PR enables decomposition at the MLIR level and improves interoperability with other MLIR/xDSL passes. #1962 is a temporary patch to apply decomposition at PLxPR that is still aligned with the compiler-specific decomposition in this PR. But for the compiler-specific decomposition, we don't use the graph-based decomposition to find the solutions because

  • the experimental graph-based system hasn't been maintained w/ capture enabled and from_plxpr
  • the compiler-specific decomp mainly deals with high-level operators and templates that each have a single decomposition rule, so using the graph wouldn’t provide much benefit in most cases

Copy link
Contributor

@rniczh rniczh left a comment

Choose a reason for hiding this comment

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

Awesome 🎉 just small changes and questions

@astralcai astralcai self-requested a review September 15, 2025 20:48
Copy link
Member

@paul0403 paul0403 left a comment

Choose a reason for hiding this comment

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

Looks good from my side, seems like all the "hacky" bits are isolated in the new decompose.py and there are plans to use the upstream PL decomposition interpreter, so the main catalyst from_plxpr program flow is not seeing anything increasing hacky-ness 😆

Approving pending Astral's comments regarding decomp logic (which I'm not super familiar with).

rniczh and others added 9 commits September 23, 2025 14:28
… present in MLIR (#2001)

**Context:**

Introduces a new MLIR pass that enables user-defined decomposition of
quantum gate operations. The pass discovers decomposition functions in
the module and uses MLIR's inlining infrastructure to replace target
quantum operations with their decomposed implementations.

**Description of the Change:**

This PR will **recognize** the `func.func` as decomposition if they have
name start with the pattern `<Gate>_rule[.]*` where the `<Gate>` is a
`quantum.custom` op it expect to replace (It's not what generated from
frontend). Also you can just mark the attributes
`catalyst.decomposition` and `catalyst.decomposition.target_op` at
decomposition function, it still works.

And this pass just **discover** those decomposition functions and
**replace** the target operation with `call` to the function and rely on
the Inliner interface to inline these decomposition function if needed.

The stages of the `--decompose-lowering`:
1. `decompose_lowering.cpp`: Main pass that orchestrates the
decomposition process
2. `DecomposeLoweringPatterns`: Pattern rewriting logic that replaces
`quantum.custom` operations with function call
3. `QuantumInlinerInterface`: Dialect interface that enables MLIR's
inliner to process quantum operations correctly
4. Remove unused decomposition function (if the inliner decide not to
inline the certain function, then it will leave a `func.call`, so the
func is used, shouldn't be removed)

The type signature between decomposition function provided from the
frontend is different to the `quantum.custom` op, `OpSignatureAnalyzer`
does the trick to get the enough information to generate a function call
to decomposition function.

Current right now, frontend choose to generate the decomposition
function with type:
```
(qreg, param*, inWires*, inCtrlWires*?, inCtrlValues*?) -> qreg
```
We need to figure out the information that need to pass to create the
function call: qreg, `in wires indices`, and `in ctrl wires`.

`OpSignatureAnalyzer` will use the target `quantum.custom` op (that one
we need to replace) to traverse the qubit, and find out the qreg, and
wire indices. The traversal logic based on the following assumption for
the quantum dialect:

1. The ordering of qubits is preserved in quantum instructions
2. For other instructions, they will use a register, so will hit an
extract before them

It promise that every gates, the result (qubits) will match to the
operands (qubits) at the same index. That's the important assumptions to
support the traversal logic here. Traverse the qubit, match the index,
keep going up until reach the `qauntum.extract` operation.

After then, we use those information to generate a function call and
rely on the Inliner in MLIR infra to replace it.

**Benefits:**

The decision to use MLIR's inlining infrastructure (`InlinerPass`) is to
provide future-proofing and flexibility.

**Possible Drawbacks:**

**Related GitHub Issues:**
[sc-98593]

---------

Co-authored-by: Ali Asadi <[email protected]>
Copy link
Contributor

@astralcai astralcai left a comment

Choose a reason for hiding this comment

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

Thank you for answering all my questions.

@maliasadi maliasadi merged commit a13db03 into main Sep 26, 2025
38 checks passed
@maliasadi maliasadi deleted the feature/handle_gdecomp_v4 branch September 26, 2025 14:36
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.

7 participants