-
Notifications
You must be signed in to change notification settings - Fork 35
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
Add merge rotation patterns for qml.Rot
and qml.CRot
#1220
Comments
qml.(C)Rot
qml.Rot
and qml.CRot
Hi @paul0403, In tackling the merging of qml.Rot and qml.CRot gates, I’m considering an approach that goes beyond simple angle addition due to the non-commutativity of Y and Z rotations. Specifically, based on your document, I’m using the Z-Y-Z Euler angle decomposition to calculate the combined rotations, treating each gate as a rotation matrix. The steps involve: Converting individual qml.Rot or qml.CRot angles into matrices. This approach aims to preserve the correct sequence and dependencies of rotations. Could you advise if this method is consistent with the intended use in Catalyst, or if adjustments are recommended? |
Hi @Mohxen , I'll write up a more detailed response tomorrow. For now let me link you to the explicit formulas for the result rotation gate's angles in PennyLane's parametrization |
Hi @Mohxen, I would advise against using the matrix approach for the following reasons:
Don't hesitate to let me know if you need any help! |
Hi @paul0403, Thank you so much for the guidance and detailed points! I’m working to make this contribution usable within Catalyst, beyond just completing my assessment. Using the scalar formulas from PennyLane’s documentation, I’ve implemented the approach in merge_rotation.cpp with MLIR’s Arith and Math libraries, and I’ve successfully passed make all and make test. Currently, I’m focusing on MergeRotationsTest.mlir, although I’m not entirely sure which specific checks we need, given that we already have existing tests for rot and crot. Additionally, I’m setting up a frontend test in test_peephole_optimizations.py to observe the merged rotations' output, and I’m considering whether a new test is needed explicitly for this merge behavior. |
Hi @paul0403, Thank you for the feedback! I've followed your instructions and opened a pull request as you asked. The PR for my changes is available at #1270. This PR includes the merge rotation patterns for qml.Rot and qml.CRot with full-angle formulas, as discussed, and I have added tests to verify the merging behavior. Could you please review my PR and provide any feedback? I’m happy to make any edits based on your suggestions. Thank you again for your guidance! |
Context
In quantum circuits, two neighbouring rotation gates about the same rotation axis on the same wire can be merged into one, i.e.$R_x(a) R_x(b) = R_x(a+b)$ .
In #1162, a merge rotation pass was implemented in Catalyst. The pass included qml.Rot/qml.CRot, but then it was realized that the angles in these two functions don't simply add when being merged, because y and z rotations do not commute. These two functions were removed from merge rotation pass in #1206 .
However,
qml.Rot
can be merged; it's just that the new angles of the merged gate are more complicated: https://docs.pennylane.ai/en/stable/code/api/pennylane.transforms.merge_rotations.html#details-on-rotGoal
We should include
qml.Rot
andqml.CRot
in merge rotation pass, with the full angle formulas, instead of just removing them from the pattern.Requirements:
Technical details
Catalyst has its own mlir
quantum
dialect. The operation definitions are inQuantumOps.td
. The operation of particular interest to us is theCustomOp
, which corresponds to a general named gate, with the name being stored as a string.To see what kind of input mlir the pass will receive, see merge rotation mlir test for an example. In particular, the "Rot" and "CRot" tests right now checks that no merging happens; they should be updated to check that the correct angles are produced and then used by the merged gate.
We currently already have merge rotation patterns for all other rotation gates. The pattern simply adds the two rotation angles, because the simple rotation gates can be easily merged like that. You can add the pattern for
qml.Rot
andqml.CRot
to this same file.You should not need to modify the actual merge rotation pass containing the pattern applicator, though you can if you wish.
make dialects
from the top-level catalyst directory. This will build all passes in Catalyst and run all filecheck tests inmlir/test
.You can also invoke individual passes on an arbitrary input mlir file with
catalyst/mlir/build/bin/quantum-opt
, and use it like the standard mliropt
. At the top of each mlir test file, you can see how each pass is invoked.You can use
quantum-opt --help
to see all available passes.You might want to see what Catalyst mlir a frontend PennyLane python program would produce. To lower frontend PennyLane python code to mlir, you can use the
keep_intermediate
option inqjit
. See here.To apply the merge rotation pass from frontend PennyLane, you can use the
catalyst.passes.merge_rotations
decorator. See here for the documentation on a similar optimization,cancel_inverses
,alongside how to usekeep_intermediate
, and here for merge_rotation.Various mathematical operations, like sqrt and arctan, are in the mlir math dialect
Installation help
Complete instructions to install Catalyst from source can be found here. Note that due to the size of the llvm-project it can take a while (~3 hrs on a personal laptop) to compile.
The text was updated successfully, but these errors were encountered: