Skip to content

Commit

Permalink
[Backport] backport multiple commits to testing branch (#323)
Browse files Browse the repository at this point in the history
Backports:

* af612a2  Fix default-only switch statements (#324)
* 9613280 remove qubits from SynchronizeOp (#322)

---------

Co-authored-by: mbhealy <[email protected]>
  • Loading branch information
bcdonovan and mbhealy authored May 2, 2024
1 parent f791a97 commit ffbe8ef
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 8 deletions.
17 changes: 17 additions & 0 deletions include/Dialect/Pulse/Utils/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,21 @@ MixFrameOp getMixFrameOp(PulseOpTy pulseOp,
return mixFrameOp;
}

template <typename PulseOpTy>
llvm::StringRef getMixFrameName(PulseOpTy pulseOp, SequenceOp sequenceOp) {

auto frameArgIndex =
pulseOp.getTarget().template cast<BlockArgument>().getArgNumber();

assert(sequenceOp->hasAttrOfType<ArrayAttr>("pulse.args") and
"no pulse.args found for the pulse cal sequence.");

auto argAttr = sequenceOp->getAttrOfType<ArrayAttr>("pulse.args");

llvm::StringRef frameName =
argAttr[frameArgIndex].template dyn_cast<StringAttr>().getValue();

return frameName;
}

} // end namespace mlir::pulse
5 changes: 4 additions & 1 deletion lib/Conversion/QUIRToPulse/LoadPulseCals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "Dialect/Pulse/IR/PulseDialect.h"
#include "Dialect/Pulse/IR/PulseOps.h"
#include "Dialect/QCS/IR/QCSOps.h"
#include "Dialect/QUIR/IR/QUIROps.h"
#include "Dialect/QUIR/IR/QUIRTraits.h"
#include "Dialect/QUIR/Utils/Utils.h"
Expand Down Expand Up @@ -145,7 +146,9 @@ void LoadPulseCalsPass::loadPulseCals(CallCircuitOp callCircuitOp,
loadPulseCals(castOp, callCircuitOp, funcOp);
else if (auto castOp = dyn_cast<mlir::quir::ResetQubitOp>(op))
loadPulseCals(castOp, callCircuitOp, funcOp);
else {
else if (isa<mlir::qcs::DelayCyclesOp>(op)) {
// no pulse call to load for a delay cycles op
} else {
LLVM_DEBUG(llvm::dbgs() << "no pulse cal loading needed for " << op);
assert((!op->hasTrait<mlir::quir::UnitaryOp>() and
!op->hasTrait<mlir::quir::CPTPOp>()) &&
Expand Down
13 changes: 11 additions & 2 deletions lib/Conversion/QUIRToPulse/QUIRToPulse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ void QUIRToPulsePass::runOnOperation() {
// erase circuit ops
moduleOp->walk([&](CircuitOp circOp) { circOp->erase(); });

// Remove all arguments from synchronization ops
moduleOp->walk([](qcs::SynchronizeOp synchOp) {
synchOp.getQubitsMutable().assign(ValueRange({}));
});

// erase qubit ops and constant angle ops
moduleOp->walk([&](Operation *op) {
if (isa<mlir::quir::DeclareQubitOp>(op))
Expand Down Expand Up @@ -216,6 +221,11 @@ QUIRToPulsePass::convertCircuitToSequence(CallCircuitOp &callCircuitOp,
PulseOpSchedulingInterface::setDuration(pulseCalCallSequenceOp,
durValue);
}
} else if (isa<qcs::DelayCyclesOp>(quirOp)) {
// a qcs.delay_cycles may be inserted into a quir.circuit and should be
// placed outside of the sequence at the call point
auto *newDelayCyclesOp = builder.clone(*quirOp);
newDelayCyclesOp->moveAfter(callCircuitOp);
} else
assert(((isa<quir::ConstantOp>(quirOp) or isa<quir::ReturnOp>(quirOp) or
isa<quir::CircuitOp>(quirOp))) &&
Expand All @@ -229,7 +239,7 @@ QUIRToPulsePass::convertCircuitToSequence(CallCircuitOp &callCircuitOp,
convertedPulseSequenceOpReturnTypes.size()));
convertedPulseSequenceOp.setType(newFuncType);
entryBuilder.create<mlir::pulse::ReturnOp>(
convertedPulseSequenceOp.back().back().getLoc(),
convertedPulseSequenceOp.getLoc(),
mlir::ValueRange{convertedPulseSequenceOpReturnValues});
convertedPulseSequenceOp->moveBefore(mainFunc);

Expand All @@ -239,7 +249,6 @@ QUIRToPulsePass::convertCircuitToSequence(CallCircuitOp &callCircuitOp,
convertedPulseSequenceOp,
convertedPulseSequenceOpArgs);
convertedPulseCallSequenceOp->moveAfter(callCircuitOp);

return convertedPulseCallSequenceOp;
}

Expand Down
9 changes: 4 additions & 5 deletions lib/Frontend/OpenQASM3/QUIRGenQASM3Visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,11 +540,10 @@ void QUIRGenQASM3Visitor::visit(const ASTSwitchStatementNode *node) {
SmallVector<uint32_t> caseValues;
for (auto const &[key, caseStatement] : node->GetCaseStatementsMap())
caseValues.push_back(caseStatement->GetCaseIndex());
if (!caseValues.empty())
caseValuesAttr = DenseIntElementsAttr::get(
VectorType::get(static_cast<int64_t>(caseValues.size()),
builder.getIntegerType(32)),
caseValues);
caseValuesAttr = DenseIntElementsAttr::get(
VectorType::get(static_cast<int64_t>(caseValues.size()),
builder.getIntegerType(32)),
caseValues);

auto caseOperands = node->GetCaseStatementsMap();

Expand Down
4 changes: 4 additions & 0 deletions releasenotes/notes/default-only-switch-64bb951256d7d2b5.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
Switch statements with only a default region will now correctly compile.
32 changes: 32 additions & 0 deletions test/Frontend/OpenQASM3/switch-default-only.qasm3
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
OPENQASM 3.0;
// RUN: qss-compiler -X=qasm --emit=ast-pretty %s | FileCheck %s --match-full-lines --check-prefix AST-PRETTY
// RUN: qss-compiler -X=qasm --emit=mlir %s --enable-circuits-from-qasm=false| FileCheck %s --match-full-lines --check-prefixes MLIR
// RUN: qss-compiler -X=qasm --emit=mlir %s --enable-circuits-from-qasm | FileCheck %s --match-full-lines --check-prefixes MLIR

//
// This code is part of Qiskit.
//
// (C) Copyright IBM 2023, 2024.
//
// This code is licensed under the Apache License, Version 2.0 with LLVM
// Exceptions. You may obtain a copy of this license in the LICENSE.txt
// file in the root directory of this source tree.
//
// Any modifications or derivative works of this code must retain this
// copyright notice, and modified files need to carry a notice indicating
// that they have been altered from the originals.

int i = 15;
qubit $0;
// MLIR: quir.switch %{{.*}}{
// MLIR-NEXT: }[]
// AST-PRETTY: SwitchStatementNode(SwitchQuantity(name=i, type=ASTTypeIdentifier),
switch (i) {
// AST-PRETTY:statements=[
// AST-PRETTY:],
// AST-PRETTY:default statement=[
// AST-PRETTY:])
default: {
}
break;
}

0 comments on commit ffbe8ef

Please sign in to comment.