catalyst icon indicating copy to clipboard operation
catalyst copied to clipboard

Added merge rotation patterns for qml.Rot and qml.CRot

Open Mohxen opened this issue 1 year ago • 1 comments

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 and clang-format-14 are 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 to mlir/test, and Runtime tests should be added to runtime/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.Rot and qml.CRot to Catalyst’s MLIR rotation merging pass. Implemented the necessary trigonometric calculations to accurately determine the combined rotation angles when merging these gates, using Arith.h and Math.h from MLIR for operations such as cosine, sine, and addition.
  • Test Updates: Modified test_peephole_optimizations.py to include tests for merging qml.Rot and qml.CRot gates.

Benefits:

  • Improved Circuit Efficiency: Reduces sequences of qml.Rot or qml.CRot gates 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.Rot and qml.CRot from the merge rotation pass due to incorrect angle merging.
  • Issue #1220: Discussion on reintroducing qml.Rot and qml.CRot into the merge rotation pass with correct angle calculations.

Mohxen avatar Nov 03 '24 04:11 Mohxen

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

paul0403 avatar Nov 03 '24 19:11 paul0403