Skip to content

Commit 7b93aec

Browse files
committed
fix nested ops issue and others in pass/pattern
1 parent 9fde078 commit 7b93aec

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

mlir/lib/Passes/GateDecompositionPass.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ struct GateDecompositionPass final
3939

4040
// Configure greedy driver
4141
mlir::GreedyRewriteConfig config;
42+
// start at top of program to maximize collected sub-circuits
4243
config.setUseTopDownTraversal(true);
44+
// only optimize existing operations to avoid unnecessary sub-circuit
45+
// collections of already decomposed gates
46+
config.setStrictness(GreedyRewriteStrictness::ExistingOps);
4347

4448
// Apply patterns in an iterative and greedy manner.
4549
if (mlir::failed(

mlir/lib/Passes/Patterns/GateDecompositionPattern.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ struct GateDecompositionPattern final
9090
mlir::LogicalResult
9191
matchAndRewrite(UnitaryOpInterface op,
9292
mlir::PatternRewriter& rewriter) const override {
93+
if (op->getParentOfType<CtrlOp>()) {
94+
// application of pattern might not work on gates inside a control
95+
// modifier because rotation gates need to create new constants which are
96+
// not allowed inside a control body; also, the foreign gate dection does
97+
// not work and e.g. a CNOT will not be recognized as such and thus will
98+
// be further decomposed into a RX gate inside the control body which is
99+
// most likely undesired
100+
return mlir::failure();
101+
}
102+
93103
auto collectSeries = [](UnitaryOpInterface op, bool singleQubitOnly) {
94104
if (singleQubitOnly) {
95105
return TwoQubitSeries::getSingleQubitSeries(op);
@@ -103,8 +113,8 @@ struct GateDecompositionPattern final
103113
!series.containsOnlyGates(singleQubitGates, twoQubitGates);
104114

105115
if (series.gates.empty() || (series.gates.size() < 3 &&
106-
(!forceApplication || containsForeignGates))) {
107-
// too short
116+
!(forceApplication && containsForeignGates))) {
117+
// empty or too short and only contains valid gates anyway
108118
return mlir::failure();
109119
}
110120

@@ -169,7 +179,7 @@ struct GateDecompositionPattern final
169179
// only accept new sequence if it shortens existing series by more than two
170180
// gates; this prevents an oscillation with phase gates
171181
if (bestSequence->complexity() + 2 >= series.complexity &&
172-
(!forceApplication || !containsForeignGates)) {
182+
!(forceApplication && containsForeignGates)) {
173183
return mlir::failure();
174184
}
175185

@@ -245,6 +255,10 @@ struct GateDecompositionPattern final
245255
while (auto user = getUser(result.outQubits[i],
246256
&helpers::isSingleQubitOperation)) {
247257
foundGate = result.appendSingleQubitGate(*user);
258+
if (!foundGate) {
259+
// result.outQubits was not updated, prevent endless loop
260+
break;
261+
}
248262
}
249263
}
250264

0 commit comments

Comments
 (0)