MLIR Passes v1.0
Loading...
Searching...
No Matches
SwitchOperations.hpp
Go to the documentation of this file.
1/* This code and any associated documentation is provided "as is"
2
3Copyright 2025 Munich Quantum Software Stack Project
4
5Licensed under the Apache License, Version 2.0 with LLVM Exceptions (the
6"License"); you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9https://github.com/Munich-Quantum-Software-Stack/passes/blob/develop/LICENSE
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14License for the specific language governing permissions and limitations under
15the License.
16
17SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
18*******************************************************************************
19 author Martin Letras
20 date February 2025
21 version 1.0
22*******************************************************************************
23* This source code and the accompanying materials are made available under *
24* the terms of the Apache License 2.0 which accompanies this distribution. *
25******************************************************************************/
36#pragma once
37
39#include "cudaq/Optimizer/Dialect/Quake/QuakeDialect.h"
40#include "cudaq/Optimizer/Dialect/Quake/QuakeOps.h"
41#include "mlir/Dialect/SCF/IR/SCF.h"
42#include "mlir/Rewrite/FrozenRewritePatternSet.h"
43#include "mlir/Transforms/DialectConversion.h"
44
45#include "llvm/Support/Casting.h"
46#include "llvm/Support/raw_ostream.h"
47
48using namespace mlir;
49using namespace mqss::support::quakeDialect;
50
51namespace mqss::support::transforms {
52
65template <typename T1, typename T2, typename T3, typename T4>
66void patternSwitch(mlir::Operation *currentOp) {
67 auto currentGate = dyn_cast_or_null<T2>(*currentOp);
68 if (!currentGate)
69 return;
70 // check single qubit T2 operation
71 if (currentGate.getControls().size() != 0 ||
72 currentGate.getTargets().size() != 1)
73 return;
74 // get previous
75 auto prevOp =
76 getPreviousOperationOnTarget(currentGate, currentGate.getTargets()[0]);
77 if (!prevOp)
78 return;
79 auto prevGate = dyn_cast<T1>(prevOp);
80 // check single qubit operation
81 if (prevGate.getControls().size() != 0 || prevGate.getTargets().size() != 1)
82 return;
83// I found the pattern, then I remove it from the circuit
84#ifdef DEBUG
85 llvm::outs() << "Current Operation: ";
86 currentGate->print(llvm::outs());
87 llvm::outs() << "\n";
88 llvm::outs() << "Previous Operation: ";
89 prevGate->print(llvm::outs());
90 llvm::outs() << "\n";
91#endif
92 mlir::IRRewriter rewriter(currentGate->getContext());
93 rewriter.setInsertionPointAfter(currentGate);
94 auto newGate = rewriter.create<T3>(
95 currentGate.getLoc(), currentGate.isAdj(), currentGate.getParameters(),
96 currentGate.getControls(), currentGate.getTargets());
97 rewriter.setInsertionPointAfter(newGate);
98 rewriter.create<T4>(prevGate.getLoc(), prevGate.isAdj(),
99 prevGate.getParameters(), prevGate.getControls(),
100 prevGate.getTargets());
101 rewriter.eraseOp(prevGate);
102 rewriter.eraseOp(currentGate);
103}
104} // namespace mqss::support::transforms
mlir::Operation * getPreviousOperationOnTarget(mlir::Operation *currentOp, mlir::Value targetQubit)
Function get the previous operation on a given target qubit.
void patternSwitch(mlir::Operation *currentOp)
Detects and rewrites a pattern of four quantum operations.
Definition SwitchOperations.hpp:66