Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions mlir/include/mlir/Dialect/QCO/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,10 @@ def MappingPass : Pass<"place-and-route", "mlir::ModuleOp"> {
"The number of inserted SWAPs">];
}

def SwapAbsorptionPass : Pass<"absorb-swaps", "mlir::ModuleOp"> {
let dependentDialects = ["mlir::qco::QCODialect"];
let summary = "";
Copy link
Copy Markdown
Collaborator Author

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.

let options = [];
}

#endif // MLIR_DIALECT_QCO_TRANSFORMS_PASSES_TD
82 changes: 82 additions & 0 deletions mlir/lib/Dialect/QCO/Transforms/Optimizations/SwapAbsorption.cpp
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

View workflow job for this annotation

GitHub Actions / 🇨‌ Lint / 🚨 Lint

mlir/lib/Dialect/QCO/Transforms/Optimizations/SwapAbsorption.cpp:15:1 [misc-include-cleaner]

included header QTensorOps.h is not used directly

#include <llvm/ADT/STLExtras.h>

Check warning on line 17 in mlir/lib/Dialect/QCO/Transforms/Optimizations/SwapAbsorption.cpp

View workflow job for this annotation

GitHub Actions / 🇨‌ Lint / 🚨 Lint

mlir/lib/Dialect/QCO/Transforms/Optimizations/SwapAbsorption.cpp:17:1 [misc-include-cleaner]

included header STLExtras.h is not used directly
#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

View workflow job for this annotation

GitHub Actions / 🇨‌ Lint / 🚨 Lint

mlir/lib/Dialect/QCO/Transforms/Optimizations/SwapAbsorption.cpp:21:1 [misc-include-cleaner]

included header Visitors.h is not used directly
#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

View workflow job for this annotation

GitHub Actions / 🇨‌ Lint / 🚨 Lint

mlir/lib/Dialect/QCO/Transforms/Optimizations/SwapAbsorption.cpp:25:1 [misc-include-cleaner]

included header cstddef is not used directly
#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
Expand Up @@ -7,7 +7,7 @@
# Licensed under the MIT License

set(target_name mqt-core-mlir-unittest-optimizations)
add_executable(${target_name} test_qco_merge_single_qubit_rotation.cpp)
add_executable(${target_name} test_qco_merge_single_qubit_rotation.cpp test_swapabsorption.cpp)

target_link_libraries(
${target_name}
Expand Down
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
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The 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!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The 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:

0 ---X---[H]---.---<sink>
     |         |
1 ---X---------⨁---<sink>

↓ (absorb SWAPs)

1 ---[H]---.---<sink>       // Input of `HOp` is `qco.static 1`?
           |                //
0 ---------⨁---<sink>       // Input of `CtrlOp(target)` is `qco.static 0`?
   

Footnotes

  1. Note that the order (from top to bottom) of the qco.static operations doesn't matter.

Copy link
Copy Markdown

@jmoosburger jmoosburger May 5, 2026

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The 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 👀

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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));
}
Loading