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 .license-tools-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
".pyi": "DOCSTRING_STYLE",
".in": "POUND_STYLE",
".mlir": "SLASH_STYLE",
".pdll": "SLASH_STYLE",
".td": "SLASH_STYLE",
".yaml": "POUND_STYLE",
".toml": "POUND_STYLE",
Expand Down
8 changes: 5 additions & 3 deletions mlir/include/mlir/Dialect/Common/IR/StdOps.td.inc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
//
// Licensed under the MIT License

#ifndef @DIALECT_NAME@_STD_OPS
#define @DIALECT_NAME@_STD_OPS
#ifndef @DIALECT_NAME_UPPER@_STD_OPS
#define @DIALECT_NAME_UPPER@_STD_OPS

include "mlir/Dialect/@DIALECT_NAME@/IR/@[email protected]"

def GPhaseOp : UnitaryOp<"gphase", [NoTarget, OneParameter]> {
let summary = "GPhase operation";
Expand Down Expand Up @@ -329,4 +331,4 @@ def XXplusYYOp : UnitaryOp<"xx_plus_yy", [TwoTarget, TwoParameters]> {
}];
}

#endif // @DIALECT_NAME@_STD_OPS
#endif // @DIALECT_NAME_UPPER@_STD_OPS
3 changes: 2 additions & 1 deletion mlir/include/mlir/Dialect/MQTOpt/IR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#
# Licensed under the MIT License

set(DIALECT_NAME "MQTOPT")
set(DIALECT_NAME "MQTOpt")
set(DIALECT_NAME_UPPER "MQTOPT")
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/../../Common/IR/StdOps.td.inc
${CMAKE_CURRENT_BINARY_DIR}/MQTOptStdOps.td @ONLY)

Expand Down
1 change: 0 additions & 1 deletion mlir/include/mlir/Dialect/MQTOpt/Transforms/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ namespace mqt::ir::opt {

void populateGateEliminationPatterns(mlir::RewritePatternSet& patterns);
void populateMergeRotationGatesPatterns(mlir::RewritePatternSet& patterns);
void populateElidePermutationsPatterns(mlir::RewritePatternSet& patterns);
void populateQuantumSinkShiftPatterns(mlir::RewritePatternSet& patterns);
void populateQuantumSinkPushPatterns(mlir::RewritePatternSet& patterns);
void populateToQuantumComputationPatterns(mlir::RewritePatternSet& patterns,
Expand Down
8 changes: 7 additions & 1 deletion mlir/include/mlir/Dialect/MQTOpt/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#ifndef MQTO_PASSES
#define MQTO_PASSES

include "mlir/Dialect/PDL/IR/PDLDialect.td"
include "mlir/Dialect/PDLInterp/IR/PDLInterpOps.td"
include "mlir/Pass/PassBase.td"

def MQTCoreRoundTrip : Pass<"mqt-core-round-trip", "mlir::ModuleOp"> {
Expand Down Expand Up @@ -45,7 +47,7 @@ def MergeRotationGates : Pass<"merge-rotation-gates", "mlir::ModuleOp"> {
}];
}

def ElidePermutations : Pass<"elide-permutations", "mlir::ModuleOp"> {
def SwapReconstructionAndElision : Pass<"swap-reconstruction-and-elision", "mlir::ModuleOp"> {
let summary = "This pass removes any permutation (i.e., SWAP gates) it encounters and modifies the order of the qubits instead.";
let description = [{
Elides permutations (i.e., SWAP gates) by removing them from the circuit and, instead, permuting the respective qubit values.
Expand All @@ -62,6 +64,10 @@ def ElidePermutations : Pass<"elide-permutations", "mlir::ModuleOp"> {
%q0_1, %q1_1 = mqtopt.x() %q0_0 ctrl %q1_0 : !mqtopt.Qubit ctrl !mqtopt.Qubit
```
}];
let dependentDialects = [
"mlir::pdl::PDLDialect",
"mlir::pdl_interp::PDLInterpDialect",
];
}

def QuantumSinkPass : Pass<"quantum-sink", "mlir::ModuleOp"> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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
*/

#pragma once

#include <mlir/IR/PatternMatch.h>
#include <mlir/Parser/Parser.h>
#include <mlir/Pass/Pass.h>

namespace mqt::ir::opt {

#define GEN_PASS_DECL_SWAPRECONSTRUCTIONANDELISION
#include "SwapReconstructionAndElision.h.inc"
#include "mlir/Dialect/MQTOpt/Transforms/Passes.h.inc"
} // namespace mqt::ir::opt
3 changes: 2 additions & 1 deletion mlir/include/mlir/Dialect/MQTRef/IR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#
# Licensed under the MIT License

set(DIALECT_NAME "MQTREF")
set(DIALECT_NAME "MQTRef")
set(DIALECT_NAME_UPPER "MQTREF")
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/../../Common/IR/StdOps.td.inc
${CMAKE_CURRENT_BINARY_DIR}/MQTRefStdOps.td @ONLY)

Expand Down
15 changes: 13 additions & 2 deletions mlir/lib/Dialect/MQTOpt/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,19 @@ add_compile_options(-fexceptions)

file(GLOB TRANSFORMS_SOURCES *.cpp)

add_mlir_library(MLIRMQTOptTransforms ${TRANSFORMS_SOURCES} LINK_LIBS ${LIBRARIES} DEPENDS
MLIRMQTOptTransformsIncGen)
add_mlir_pdll_library(SwapReconstructionAndElisionIncGen SwapReconstructionAndElision.pdll
SwapReconstructionAndElision.h.inc)

add_mlir_library(
MLIRMQTOptTransforms
${TRANSFORMS_SOURCES}
LINK_LIBS
${LIBRARIES}
DEPENDS
MLIRMQTOptTransformsIncGen
SwapReconstructionAndElisionIncGen)

target_include_directories(MLIRMQTOptTransforms PUBLIC ${CMAKE_CURRENT_BINARY_DIR})

# collect header files
file(GLOB_RECURSE TRANSFORMS_HEADERS_SOURCE
Expand Down
58 changes: 0 additions & 58 deletions mlir/lib/Dialect/MQTOpt/Transforms/ElidePermutationsPattern.cpp

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@
* Licensed under the MIT License
*/

#include "mlir/Dialect/MQTOpt/Transforms/Passes.h"
#include "mlir/Dialect/MQTOpt/Transforms/SwapReconstructionAndElision.h"

#include <mlir/Dialect/PDLInterp/IR/PDLInterp.h>

Check warning on line 13 in mlir/lib/Dialect/MQTOpt/Transforms/SwapReconstructionAndElision.cpp

View workflow job for this annotation

GitHub Actions / 🇨‌ Lint / 🚨 Lint

mlir/lib/Dialect/MQTOpt/Transforms/SwapReconstructionAndElision.cpp:13:1 [misc-include-cleaner]

included header PDLInterp.h is not used directly
#include <mlir/IR/PatternMatch.h>
#include <mlir/Support/LLVM.h>
#include <mlir/Transforms/GreedyPatternRewriteDriver.h>
#include <utility>

namespace mqt::ir::opt {

#define GEN_PASS_DEF_ELIDEPERMUTATIONS
#define GEN_PASS_DEF_SWAPRECONSTRUCTIONANDELISION
#include "mlir/Dialect/MQTOpt/Transforms/Passes.h.inc"

/**
* @brief This pattern attempts to remove SWAP gates by re-ordering qubits.
*/
struct ElidePermutations final
: impl::ElidePermutationsBase<ElidePermutations> {
struct SwapReconstructionAndElision final
: impl::SwapReconstructionAndElisionBase<SwapReconstructionAndElision> {

void runOnOperation() override {
// Get the current operation being operated on.
Expand All @@ -33,7 +34,7 @@

// Define the set of patterns to use.
mlir::RewritePatternSet patterns(ctx);
populateElidePermutationsPatterns(patterns);
populateGeneratedPDLLPatterns(patterns);

// Apply patterns in an iterative and greedy manner.
if (mlir::failed(mlir::applyPatternsGreedily(op, std::move(patterns)))) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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/MQTOpt/IR/MQTOptStdOps.td"

Constraint ExtractCNotResults(op: Op<mqtopt.x>) -> (target: Value, control: Value) {
return (op.0, op.1);
}

Constraint IsEmpty(valueRange: ValueRange) [{
return mlir::success(valueRange.empty());
}];

Constraint HasTwoElements(valueRange: ValueRange) [{
return mlir::success(valueRange.size() == 2);
}];

Rewrite BuildXOp(op: Op, target: Value, posControls: ValueRange, negControls: ValueRange) -> Op [{
auto qubitType = target.getType();
auto outType = llvm::SmallVector<mlir::Type>{1, qubitType};
auto outPosCtrlType = llvm::SmallVector<mlir::Type>{posControls.size(), qubitType};
auto outNegCtrlType = llvm::SmallVector<mlir::Type>{negControls.size(), qubitType};

return rewriter.create<mqt::ir::opt::XOp>(op->getLoc(), outType, outPosCtrlType, outNegCtrlType, mlir::DenseF64ArrayAttr{}, mlir::DenseBoolArrayAttr{},
mlir::ValueRange{}, llvm::SmallVector<mlir::Value>{target}, posControls, mlir::ValueRange{});
}];

Rewrite Self(op: Op) => op;

Rewrite First(valueRange: ValueRange) -> Value [{
return valueRange[0];
}];

Rewrite Second(valueRange: ValueRange) -> Value [{
return valueRange[1];
}];

Pattern SwapReconstructionAndElisionPattern with recursion {
// match
let root = op<mqtopt.x>(_: [ValueRange, IsEmpty], target: Value, control: Value, _: [ValueRange, IsEmpty]);
let rootOut = ExtractCNotResults(root);
let secondCNot = op<mqtopt.x>(_: [ValueRange, IsEmpty], rootOut.control, rootOut.target, _: [ValueRange, IsEmpty]);

// rewrite
rewrite root with {
replace secondCNot with (rootOut.control, rootOut.target);
replace root with op<mqtopt.x>((), (control), (target), ()) {operandSegmentSizes = attr<"array<i32: 0, 1, 1, 0>">, resultSegmentSizes = attr<"array<i32: 1, 1, 0>">};
};
}

Pattern ElidePermutationsPattern with recursion {
// match
let root = op<mqtopt.swap>(params: ValueRange, targets: ValueRange, posCtrls: ValueRange, negCtrls: ValueRange);
IsEmpty(params);
HasTwoElements(targets);
IsEmpty(posCtrls);
IsEmpty(negCtrls);

// rewrite
replace root with (Second(targets), First(targets));
}
Loading
Loading