Skip to content
Closed
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
1 change: 1 addition & 0 deletions mlir/include/mlir/Dialect/MQTOpt/Transforms/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace mqt::ir::opt {
void populateGateEliminationPatterns(mlir::RewritePatternSet& patterns);
void populateMergeRotationGatesPatterns(mlir::RewritePatternSet& patterns);
void populateElidePermutationsPatterns(mlir::RewritePatternSet& patterns);
void populateSwapReconstructionPatterns(mlir::RewritePatternSet& patterns);
void populateQuantumSinkShiftPatterns(mlir::RewritePatternSet& patterns);
void populateQuantumSinkPushPatterns(mlir::RewritePatternSet& patterns);
void populateToQuantumComputationPatterns(mlir::RewritePatternSet& patterns,
Expand Down
28 changes: 28 additions & 0 deletions mlir/include/mlir/Dialect/MQTOpt/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Copy link
Member

Choose a reason for hiding this comment

The 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.
Copy link
Member

Choose a reason for hiding this comment

The 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.
X gates with multiple controls are typically referred to as multi-controlled X gates or multi-controlled Toffoli gates.

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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 = [{
Expand Down
46 changes: 46 additions & 0 deletions mlir/lib/Dialect/MQTOpt/Transforms/SwapReconstruction.cpp
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
Loading
Loading