-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a frontend UI to invoke the mlir quantum peephole transform…
…ation passes (#911) **Context:** Implement a frontend UI to invoke the mlir quantum peephole transformation passes. Currently there is a `remove-chained-self-inverse` mlir pass in Catalyst that removes two neighbouring Hadamard gates in a circuit, but there is no way to actually invoke it from the frontend. This PR implements this frontend as a decorator on a qnode. When the `catalyst.cancel_inverses` decorator is added onto a qnode, the `remove-chained-self-inverse` mlir pass will be run on that qnode. Following the decision to split #883 into two PRs, this is the frontend portion. **Description of the Change:** We implment the peephole optimization library with the mlir transform dialect https://mlir.llvm.org/docs/Dialects/Transform/, https://mlir.llvm.org/docs/Tutorials/transform/ Briefly, we wish to generate the following mlir during frontend tracing and lowering ``` module @workflow { func.func private @f{ ... } func.func private @g{ ... } module attributes {transform.with_named_sequence} { transform.named_sequence @__transform_main(%arg0: !transform.op<"builtin.module">) { %0 = transform.apply_registered_pass "remove-chained-self-inverse" to %arg0 {options = "func-name=f"} : (!transform.op<"builtin.module">) -> !transform.op<"builtin.module"> transform.yield } } } ``` The outer module is the payload (the module to run the transform on), and the inner module is the transformer (the module that schedules what passes to run). The schedule in this example is run the `-remove-chained-self-inverse` pass on the payload module `@workflow`, with pass option `func-name=f`. The `transform.named_sequence` must be terminated by a `transform.yield`. A new pass, `ApplyTransformSequence.cpp`, will perform the following: 1. Remove the transformer module from the top-level payload module (which is its parent), and save the transformer module operation in memory 2. Perform the transform, through API provided by the mlir transform dialect 3. Delete the transformer module ###### We generate this mlir through two new jax primitives. The first one, `transform_named_sequence_p`, will be lowered to `transform.named_sequence` with a `transform.yield` inside and put it in a parent transformer module marked with the unit attribute `transform.with_named_sequence`. The parent transformer module is inserted into the top-level payload module. The second one, `apply_registered_pass_p`, will be lowered to a `transform.apply_registered_pass` inside the `transform.named_sequence`. If one `transform.apply_registered_pass` already exists in the sequence, the new pass will be added to after the previous one. The design is as follows: 0. QJIT start 1. Capture jaxpr (`QJIT.capture()`) 1.1. Before capture, we insert the `transform_named_sequence_p` to **every** jaxpr produced (the purpose is so that transform dialect will not fail) 1.2. During capture, if the API `@cancel_inverses` is called, inject a corresponding `apply_registered_pass_p` into jaxpr 2. Generate mlir and compile (`QJIT.generate_ir()`, `QJIT.compile()`) 2.1. The jaxpr primitives are lowered to mlir. 2.2. Here the `Compiler` class will run the pipeline, with the new `-apply-transform-sequence` pass **Benefits:** The user can now run quantum mlir passes from the frontend. [sc-67519]
- Loading branch information
Showing
22 changed files
with
1,016 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.