PennyLaneAI / catalyst

A JIT compiler for hybrid quantum programs in PennyLane
https://docs.pennylane.ai/projects/catalyst
Apache License 2.0
101 stars 26 forks source link

Implement a peephole optimization library for Catalyst: cancel inverses #883

Open paul0403 opened 1 week ago

paul0403 commented 1 week ago

Context: This is the first in a series of works that adds a series of automatic peephole optimization passes in the Catalyst pipeline.

Description of the Change: This particular PR augments the existing -remove-chained-self-inverse pass. Currently this pass only cancels two neighbouring Hadamards; in fact currently this is the only peephole optimization in Catalyst. This PR adds the following self-inverse gates for cancellation: Pauli{X,Y,Z}, controlled{X,Y,Z} (note that controlled X is just CNOT), SWAP, Toffoli

This PR also adds a frontend decorator catalyst.cancel_inverses. When the decorator is set on a qnode, a new pipeline that performs the inverse cancelling pass will be run on this qnode. This new pipeline is turned off by default and is located after QUANTUM_COMPILATION_PASS, before BUFFERIZATION_PASS. This position is currently arbitrary and could use some discussion.

Benefits: An automatic optimization pipeline is beneficial for any compiler (e.g. the -O3 pipeline for gcc and clang). In particular, the goal of this new series of works is to reduce the number of gate counts. The lower the gate count in a circuit, the less damage loss and errors do.

Possible Drawbacks:

Related GitHub Issues:

[sc-67659] [sc-67519]

github-actions[bot] commented 1 week ago

Hello. You may have forgotten to update the changelog! Please edit doc/changelog.md on your branch with:

codecov[bot] commented 1 week ago

Codecov Report

Attention: Patch coverage is 58.82353% with 14 lines in your changes missing coverage. Please review.

Project coverage is 97.84%. Comparing base (bb73aa2) to head (a3c9a6a).

Files Patch % Lines
frontend/catalyst/api_extensions/pipeline.py 60.86% 9 Missing :warning:
frontend/catalyst/compiler.py 50.00% 4 Missing and 1 partial :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #883 +/- ## ========================================== - Coverage 97.97% 97.84% -0.13% ========================================== Files 71 72 +1 Lines 10451 10485 +34 Branches 949 954 +5 ========================================== + Hits 10239 10259 +20 - Misses 169 182 +13 - Partials 43 44 +1 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

paul0403 commented 1 week ago

As per @josh146 's UI suggestion, adding the quantum_circuit_transforms option to qjit directly might not be a good idea, since a jitted workflow might contain multiple circuits, and the user might only want to run a pass on a subset of these circuits.

UI options:

We did not decide on a UI yet; at any rate, there should be some documentation of the pass pipeline in the docs.

#######

Update: an epic regarding UI is made: https://app.shortcut.com/xanaduai/epic/67492/p1-a-ui-is-available-for-users-to-specify-quantum-compilation-pipelines-from-python-4?group_by=none&vc_group_by=day&ct_workflow=all&cf_workflow=500000005

paul0403 commented 4 days ago

It would make more sense for the two items of work to be separated into two PRs (since they are two different stories): 1.Cancel more than just Hadamards in mlir 2.The frontend decorator UI

Actually, having them both here makes sense too. I'll just tag both story.

paul0403 commented 4 days ago

Self reminder to add tests and documentation

I don't know why codecov didn't bark when I hadn't added tests yet. Do not approve!

The tests should include that the functionality of the circuit does not change under the transformation pass.