Added merge rotation patterns for qml.Rot and qml.CRot
Before submitting
Please complete the following checklist when submitting a PR:
-
[ ] All new functions and code must be clearly commented and documented.
-
[ ] Ensure that code is properly formatted by running
make format. The latest version of black andclang-format-14are used in CI/CD to check formatting. -
[ ] All new features must include a unit test. Integration and frontend tests should be added to
frontend/test, Quantum dialect and MLIR tests should be added tomlir/test, and Runtime tests should be added toruntime/tests.
When all the above are checked, delete everything above the dashed line and fill in the pull request template.
Context:
In quantum circuits, consecutive rotation gates about the same axis on the same qubit can often be combined into a single gate. For instance, two successive Rx gates with angles a and b can be merged into a single Rx(a + b) gate. While this merging is straightforward for single-axis rotations, it becomes more complex for composite rotations like qml.Rot and qml.CRot, which involve rotations around multiple axes. Previously, an attempt to include qml.Rot and qml.CRot in Catalyst's merge rotation pass was reverted due to the non-commutative nature of these rotations. However, by applying the appropriate full-angle formulas derived from Euler angles, these gates can be accurately merged.
The scalar formula for combining these rotations is derived from the [PennyLane Single Qubit Fusion documentation (https://docs.pennylane.ai/en/stable/code/api/pennylane.transforms.single_qubit_fusion.html#derivation), which describes the mathematical approach for fusing consecutive rotation operations.
Description of the Change:
This update reintroduces qml.Rot and qml.CRot into Catalyst's merge rotation pass by implementing the correct mathematical formulas for combining their angles.
-
Merge Rotation Pattern Update:
Added
qml.Rotandqml.CRotto Catalyst’s MLIR rotation merging pass. Implemented the necessary trigonometric calculations to accurately determine the combined rotation angles when merging these gates, usingArith.handMath.hfrom MLIR for operations such as cosine, sine, and addition. -
Test Updates: Modified
test_peephole_optimizations.pyto include tests for mergingqml.Rotandqml.CRotgates.
Benefits:
-
Improved Circuit Efficiency: Reduces sequences of
qml.Rotorqml.CRotgates into a single operation, optimizing circuit depth and potentially reducing execution time. - Enhanced Accuracy: Ensures that the merged gates accurately represent the intended composite rotations, maintaining the correct quantum state transformations.
- Comprehensive Testing: Provides confidence in the correctness of the optimization pass through rigorous testing.
Possible Drawbacks:
The trigonometric calculations required to merge qml.Rot and qml.CRot gates are more complex than simple angle addition, which may introduce additional computational overhead during the compilation process.
Related GitHub Issues:
- Issue #1162: Initial implementation of the merge rotation pass.
-
Pull Request #1206: Removal of
qml.Rotandqml.CRotfrom the merge rotation pass due to incorrect angle merging. -
Issue #1220: Discussion on reintroducing
qml.Rotandqml.CRotinto the merge rotation pass with correct angle calculations.
@Mohxen Thanks for working on this!
I will take a closer look on Monday; for now, let me quickly mention that you can compile your new pass/pattern with make dialects from the top level catalyst directory, and test it with your examples.