@@ -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