-
-
Notifications
You must be signed in to change notification settings - Fork 46
✨ Implement (controlled) swap reconstruction MLIR pass #1158
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
Changes from all commits
3a3ae93
262a04b
cc52d8a
a27b480
7bf4ea5
383ae20
fd8bfc4
591d132
4be3815
1035f2d
14a953b
7f3f8b2
c5d02c2
ddd1cea
621edfb
c621e99
02f4d1b
9d87d24
d46a6b8
daece3f
ff68d5f
4b18b91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,6 +64,34 @@ def ElidePermutations : Pass<"elide-permutations", "mlir::ModuleOp"> { | |
| }]; | ||
| } | ||
|
|
||
| def SwapReconstruction : Pass<"swap-reconstruction", "mlir::ModuleOp"> { | ||
| let summary = "This pass searches for CNOTs that can be merged into a SWAP."; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not fully correct or at least does not cover the exact functionality of the pass. |
||
| let description = [{ | ||
| Three CNOTs that are equivalent to a SWAP are directly replaced with a (potentially controlled) SWAP gate. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not fully correct. A CNOT, per definition, only has a single-control. I believe the general description here could be streamlined and clarified a little bit. I am happy to take a stab at this once the functionality is in the "right" place and moderately consolidated.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, thanks for clearing that up, I guess I should update all of the comments to not use the term "CNOT" anymore.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That depends in my opinion. If you are really talking about a CNOT (a single-controlled X gate), then the name makes sense and is quite established. |
||
|
|
||
| For swap reconstructions with just two CNOTs (utilizing an insertion of two self-cancelling CNOTs), only non-controlled SWAP gates are inserted. | ||
|
|
||
| Examples of swap reconstructions: | ||
|
|
||
| ``` | ||
| ┌───┐ ┌───┐ | ||
| ──■──┤ X ├ ──■──┤ X ├──■────■── ──╳────■── | ||
| ┌─┴─┐└─┬─┘ => ┌─┴─┐└─┬─┘┌─┴─┐┌─┴─┐ => | ┌─┴─┐ | ||
| ┤ X ├──■── ┤ X ├──■──┤ X ├┤ X ├ ──╳──┤ X ├ | ||
| └───┘ └───┘ └───┘└───┘ └───┘ | ||
| ``` | ||
|
|
||
| ``` | ||
| ──■────■────■── ──■── | ||
| | ┌─┴─┐ | | | ||
| ──■──┤ X ├──■── => ──╳── | ||
| ┌─┴─┐└─┬─┘┌─┴─┐ | | ||
| ┤ X ├──■──┤ X ├ ──╳── | ||
| └───┘ └───┘ | ||
| ``` | ||
|
Comment on lines
+84
to
+91
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe it would be helpful to also write down the other pattern where only the middle gate is controlled and the sandwiching gates are simple CNOTs. |
||
| }]; | ||
| } | ||
|
|
||
| def QuantumSinkPass : Pass<"quantum-sink", "mlir::ModuleOp"> { | ||
| let summary = "This pass attempts to push down operations into branches for possible optimizations."; | ||
| let description = [{ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * Copyright (c) 2023 - 2025 Chair for Design Automation, TUM | ||
| * Copyright (c) 2025 Munich Quantum Software Company GmbH | ||
| * All rights reserved. | ||
| * | ||
| * SPDX-License-Identifier: MIT | ||
| * | ||
| * Licensed under the MIT License | ||
| */ | ||
|
|
||
| #include "mlir/Dialect/Common/Compat.h" | ||
| #include "mlir/Dialect/MQTOpt/Transforms/Passes.h" | ||
|
|
||
| #include <mlir/IR/PatternMatch.h> | ||
| #include <mlir/Support/LLVM.h> | ||
| #include <utility> | ||
|
|
||
| namespace mqt::ir::opt { | ||
|
|
||
| #define GEN_PASS_DEF_SWAPRECONSTRUCTION | ||
| #include "mlir/Dialect/MQTOpt/Transforms/Passes.h.inc" | ||
|
|
||
| /** | ||
| * @brief This pass uses the swap reconstruction patterns to replace according | ||
| * CNOT patterns with SWAP gates. | ||
| */ | ||
| struct SwapReconstruction final | ||
| : impl::SwapReconstructionBase<SwapReconstruction> { | ||
|
|
||
| void runOnOperation() override { | ||
| // Get the current operation being operated on. | ||
| auto op = getOperation(); | ||
| auto* ctx = &getContext(); | ||
|
|
||
| // Define the set of patterns to use. | ||
| mlir::RewritePatternSet patterns(ctx); | ||
| populateSwapReconstructionPatterns(patterns); | ||
|
|
||
| // Apply patterns in an iterative and greedy manner. | ||
| if (mlir::failed(APPLY_PATTERNS_GREEDILY(op, std::move(patterns)))) { | ||
| signalPassFailure(); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| } // namespace mqt::ir::opt |
Uh oh!
There was an error while loading. Please reload this page.