-
-
Notifications
You must be signed in to change notification settings - Fork 55
✨ Add SWAPAbsorption Pass #1683
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
base: main
Are you sure you want to change the base?
Changes from all commits
00ad822
77250d1
515e6bf
f6e1c39
b0474ff
10302c5
da710c7
d793c73
de89411
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 |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * Copyright (c) 2023 - 2026 Chair for Design Automation, TUM | ||
| * Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH | ||
| * All rights reserved. | ||
| * | ||
| * SPDX-License-Identifier: MIT | ||
| * | ||
| * Licensed under the MIT License | ||
| */ | ||
|
|
||
| #include "mlir/Dialect/QCO/IR/QCOOps.h" | ||
| #include "mlir/Dialect/QCO/Transforms/Passes.h" | ||
| #include "mlir/Dialect/QCO/Utils/Drivers.h" | ||
| #include "mlir/Dialect/QCO/Utils/WireIterator.h" | ||
| #include "mlir/Dialect/QTensor/IR/QTensorOps.h" | ||
|
Check warning on line 15 in mlir/lib/Dialect/QCO/Transforms/Optimizations/SwapAbsorption.cpp
|
||
|
|
||
| #include <llvm/ADT/STLExtras.h> | ||
|
Check warning on line 17 in mlir/lib/Dialect/QCO/Transforms/Optimizations/SwapAbsorption.cpp
|
||
| #include <mlir/Dialect/Func/IR/FuncOps.h> | ||
| #include <mlir/IR/BuiltinOps.h> | ||
| #include <mlir/IR/PatternMatch.h> | ||
| #include <mlir/IR/Visitors.h> | ||
|
Check warning on line 21 in mlir/lib/Dialect/QCO/Transforms/Optimizations/SwapAbsorption.cpp
|
||
| #include <mlir/Support/LLVM.h> | ||
| #include <mlir/Support/WalkResult.h> | ||
|
|
||
| #include <cstddef> | ||
|
Check warning on line 25 in mlir/lib/Dialect/QCO/Transforms/Optimizations/SwapAbsorption.cpp
|
||
| #include <utility> | ||
|
|
||
| namespace mlir::qco { | ||
| #define GEN_PASS_DEF_SWAPABSORPTIONPASS | ||
| #include "mlir/Dialect/QCO/Transforms/Passes.h.inc" | ||
|
|
||
| namespace { | ||
| struct SwapAbsorption : impl::SwapAbsorptionPassBase<SwapAbsorption> { | ||
| public: | ||
| using SwapAbsorptionPassBase::SwapAbsorptionPassBase; | ||
|
|
||
| protected: | ||
| void runOnOperation() override { | ||
| ModuleOp anchor = getOperation(); | ||
| IRRewriter rewriter(&getContext()); | ||
|
|
||
| for (auto func : anchor.getOps<func::FuncOp>()) { | ||
| SmallVector<WireIterator> wires; | ||
| for (auto op : func.getOps<StaticOp>()) { | ||
| wires.emplace_back(op.getQubit()); | ||
| } | ||
|
|
||
| SmallVector<SWAPOp> readyToAbsorb; | ||
| readyToAbsorb.reserve((wires.size() + 1) / 2); | ||
|
|
||
| std::ignore = walkProgramGraph<WireDirection::Forward>( | ||
| wires, [&](const ReadyRange& ready, ReleasedOps& released) { | ||
| for (const auto& [op, indices] : ready) { | ||
| if (isa<SWAPOp>(op)) { | ||
| readyToAbsorb.emplace_back(op); | ||
| } | ||
| released.emplace_back(op); | ||
| } | ||
| return WalkResult::interrupt(); | ||
| }); | ||
|
|
||
| for (auto swapOp : readyToAbsorb) { | ||
| auto in0 = swapOp.getQubit0In(); | ||
| auto in1 = swapOp.getQubit1In(); | ||
|
|
||
| auto out0 = swapOp.getQubit0Out(); | ||
| auto out1 = swapOp.getQubit1Out(); | ||
|
|
||
| // TODO: What if single qubit gates are in front of the SWAP input? | ||
| // Tipp: Use the WireIterator. | ||
| StaticOp op0 = cast<StaticOp>(in0.getDefiningOp()); | ||
| StaticOp op1 = cast<StaticOp>(in1.getDefiningOp()); | ||
|
|
||
| rewriter.replaceAllUsesWith(out0, op1.getQubit()); | ||
| rewriter.replaceAllUsesWith(out1, op0.getQubit()); | ||
| rewriter.eraseOp(swapOp); | ||
| } | ||
| } | ||
| } | ||
| }; | ||
| } // namespace | ||
| } // namespace mlir::qco | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /* | ||
| * Copyright (c) 2023 - 2026 Chair for Design Automation, TUM | ||
| * Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH | ||
| * All rights reserved. | ||
| * | ||
| * SPDX-License-Identifier: MIT | ||
| * | ||
| * Licensed under the MIT License | ||
| */ | ||
|
|
||
| #include "mlir/Dialect/QCO/Builder/QCOProgramBuilder.h" | ||
| #include "mlir/Dialect/QCO/IR/QCODialect.h" | ||
| #include "mlir/Dialect/QCO/IR/QCOOps.h" | ||
| #include "mlir/Dialect/QCO/Transforms/Passes.h" | ||
|
|
||
| #include <gtest/gtest.h> | ||
| #include <llvm/Support/LogicalResult.h> | ||
| #include <mlir/Dialect/Arith/IR/Arith.h> | ||
| #include <mlir/Dialect/Func/IR/FuncOps.h> | ||
| #include <mlir/IR/BuiltinOps.h> | ||
| #include <mlir/IR/DialectRegistry.h> | ||
| #include <mlir/IR/OperationSupport.h> | ||
| #include <mlir/IR/OwningOpRef.h> | ||
| #include <mlir/IR/Value.h> | ||
| #include <mlir/Pass/PassManager.h> | ||
| #include <mlir/Support/LLVM.h> | ||
|
|
||
| #include <cassert> | ||
| #include <memory> | ||
|
|
||
| using namespace mlir; | ||
| using namespace mlir::qco; | ||
|
|
||
| namespace { | ||
|
|
||
| class SwapAbsorbPassTest : public testing::Test { | ||
|
|
||
| protected: | ||
| void SetUp() override { | ||
| // Register all necessary dialects | ||
| DialectRegistry registry; | ||
| registry.insert<qco::QCODialect, arith::ArithDialect, func::FuncDialect>(); | ||
| context = std::make_unique<MLIRContext>(); | ||
| context->appendDialectRegistry(registry); | ||
| context->loadAllAvailableDialects(); | ||
| } | ||
|
|
||
| static void applySwapAbsorb(OwningOpRef<ModuleOp>& moduleOp) { | ||
| PassManager pm(moduleOp->getContext()); | ||
| pm.addPass(qco::createSwapAbsorptionPass()); | ||
| auto res = pm.run(*moduleOp); | ||
|
|
||
| ASSERT_TRUE(succeeded(res)); | ||
| } | ||
|
|
||
| std::unique_ptr<MLIRContext> context; | ||
| }; | ||
| }; // namespace | ||
|
|
||
| TEST_F(SwapAbsorbPassTest, PassDoesNotChangeSwaplessProgram) { | ||
|
|
||
| qco::QCOProgramBuilder builder(context.get()); | ||
| builder.initialize(); | ||
|
|
||
| const auto q00 = builder.staticQubit(0); | ||
| const auto q10 = builder.staticQubit(1); | ||
|
|
||
| const auto q01 = builder.h(q00); | ||
| const auto [q02, q11] = builder.cx(q01, q10); | ||
|
|
||
| builder.sink(q02); | ||
| builder.sink(q11); | ||
|
|
||
| auto moduleThroughPass = builder.finalize(); | ||
| auto originalModule = moduleThroughPass->clone(); | ||
|
|
||
| applySwapAbsorb(moduleThroughPass); | ||
| ASSERT_TRUE(mlir::OperationEquivalence::isEquivalentTo( | ||
| moduleThroughPass.get(), originalModule, | ||
| mlir::OperationEquivalence::Flags::IgnoreLocations)); | ||
| } | ||
|
Comment on lines
+60
to
+81
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. Because there is also a QCO program builder, we don't require the conversion from the QC to QCO dialect here.
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. I took the liberty to remove the QC builder and the conversion. Make sure to study the changes! 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. copy that! this might lead to easier comparison of original and altered circuit as well. i can compare the indices of the static qubits afterwards as they should be changed by the pass, am i correct with this assumption? currently working on that to setup more test cases ...
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. I think the easiest way to check this - without having to worry about the order of the static operations1 - is to verify that the quantum operations use the correct static indices as input. Example: Footnotes
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. works just fine 👍 i have the changes in my fork at the moment, is there any possibility to append the commit to this very PR somehow or do i have to create a separate PR from my fork?
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. Although suboptimal, I locally merged your changes into the branch of the main repository. Seems to work. Maybe once you think you are done, ping me again here in this thread and I will re-merge the changes 👀 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. got it, fine with me :) |
||
|
|
||
| TEST_F(SwapAbsorbPassTest, PassReordersTwoQubitCircuitWithLeadingSwap) { | ||
|
|
||
| qco::QCOProgramBuilder builder(context.get()); | ||
| builder.initialize(); | ||
|
|
||
| const auto q00 = builder.staticQubit(0); | ||
| const auto q10 = builder.staticQubit(1); | ||
|
|
||
| const auto [q01, q11] = builder.swap(q00, q10); | ||
|
|
||
| const auto q02 = builder.id(q01); | ||
| const auto q12 = builder.id(q11); | ||
|
|
||
| builder.sink(q02); | ||
| builder.sink(q12); | ||
|
|
||
| auto moduleThroughPass = builder.finalize(); | ||
| applySwapAbsorb(moduleThroughPass); | ||
|
|
||
| ASSERT_EQ(q10, ((IdOp)q02.getDefiningOp()).getInputQubit(0)); | ||
| ASSERT_EQ(q00, ((IdOp)q12.getDefiningOp()).getInputQubit(0)); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sure to add a nice summary and description of the swap-absorption pass here.