Skip to content

Commit

Permalink
erase ops more efficiently
Browse files Browse the repository at this point in the history
  • Loading branch information
reza-j committed Apr 2, 2024
1 parent 084e79b commit f564f5d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 55 deletions.
18 changes: 7 additions & 11 deletions include/Conversion/QUIRToPulse/QUIRToPulse.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ struct QUIRToPulsePass
// will be reset every time convertCircuitToSequence is called and will be
// used by several functions that are called within that function
uint convertedSequenceOpArgIndex;
std::map<uint, uint> circuitArgToConvertedSequenceArgMap;
std::unordered_map<uint, uint> circuitArgToConvertedSequenceArgMap;
SmallVector<Value> convertedPulseSequenceOpArgs;
std::unordered_map<std::string, uint> operandNameToIndexMap;

Expand Down Expand Up @@ -126,14 +126,15 @@ struct QUIRToPulsePass
mlir::func::FuncOp &mainFunc);
// map of the hashed location of quir angle/duration ops to their converted
// pulse ops
std::map<std::string, mlir::Value> classicalQUIROpLocToConvertedPulseOpMap;
std::unordered_map<std::string, mlir::Value>
classicalQUIROpLocToConvertedPulseOpMap;

// port name to Port_CreateOp map
std::map<std::string, mlir::pulse::Port_CreateOp> openedPorts;
std::unordered_map<std::string, mlir::pulse::Port_CreateOp> openedPorts;
// mixframe name to MixFrameOp map
std::map<std::string, mlir::pulse::MixFrameOp> openedMixFrames;
std::unordered_map<std::string, mlir::pulse::MixFrameOp> openedMixFrames;
// waveform name to Waveform_CreateOp map
std::map<std::string, mlir::pulse::Waveform_CreateOp> openedWfrs;
std::unordered_map<std::string, mlir::pulse::Waveform_CreateOp> openedWfrs;
// add a port to IR if it's not already added and return the Port_CreateOp
mlir::pulse::Port_CreateOp addPortOpToIR(std::string const &portName,
mlir::func::FuncOp &mainFunc,
Expand All @@ -149,14 +150,9 @@ struct QUIRToPulsePass
mlir::func::FuncOp &mainFunc,
mlir::OpBuilder &builder);

void addCircuitToEraseList(mlir::Operation *op);
void addCircuitOperandToEraseList(mlir::Operation *op);
std::vector<mlir::Operation *> quirCircuitEraseList;
std::vector<mlir::Operation *> quirCircuitOperandEraseList;

// parse the waveform containers and add them to pulseNameToWaveformMap
void parsePulseWaveformContainerOps(std::string &waveformContainerPath);
std::map<std::string, Waveform_CreateOp> pulseNameToWaveformMap;
std::unordered_map<std::string, Waveform_CreateOp> pulseNameToWaveformMap;

llvm::StringMap<Operation *> symbolMap;
mlir::quir::CircuitOp getCircuitOp(mlir::quir::CallCircuitOp &callCircuitOp);
Expand Down
53 changes: 11 additions & 42 deletions lib/Conversion/QUIRToPulse/QUIRToPulse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,26 +111,17 @@ void QUIRToPulsePass::runOnOperation() {
callCircOp->erase();
});

// erase the quir circuits
LLVM_DEBUG(llvm::dbgs() << "\nErasing quir circuits:\n");
for (auto *op : quirCircuitEraseList) {
LLVM_DEBUG(op->dump());
op->erase();
}

// erase quir barriers before erasing the operands
moduleOp->walk([&](mlir::quir::BarrierOp barrierOp) { barrierOp->erase(); });

// erase the quir circuit operands
LLVM_DEBUG(llvm::dbgs() << "\nErasing quir circuit operands:\n");
for (auto *op : quirCircuitOperandEraseList) {
LLVM_DEBUG(op->dump());
op->erase();
}
// erase circuit ops
moduleOp->walk([&](CircuitOp circOp) { circOp->erase(); });

// erase the rest of quir.declare_qubits (unused in the input program)
moduleOp->walk([&](mlir::quir::DeclareQubitOp declareQubitOp) {
declareQubitOp->erase();
// erase qubit ops and constant angle ops
moduleOp->walk([&](Operation *op) {
if (auto castOp = dyn_cast<mlir::quir::DeclareQubitOp>(op))
op->erase();
else if (auto castOp = dyn_cast<mlir::quir::ConstantOp>(op)) {
if (castOp.getType().isa<::mlir::quir::AngleType>())
op->erase();
}
});
}

Expand All @@ -145,7 +136,6 @@ QUIRToPulsePass::convertCircuitToSequence(CallCircuitOp &callCircuitOp,
LLVM_DEBUG(llvm::dbgs() << "\nConverting QUIR circuit " << circName << ":\n");
assert(callCircuitOp && "callCircuit op is null");
assert(circuitOp && "circuit op is null");
addCircuitToEraseList(circuitOp);

// build an empty pulse sequence
SmallVector<Value> arguments;
Expand Down Expand Up @@ -284,10 +274,7 @@ void QUIRToPulsePass::processCircuitArgs(
convertedPulseSequenceOpArgs.push_back(convertedDurationToI64);
} else if (argumentType.isa<mlir::quir::QubitType>()) {
auto *qubitOp = callCircuitOp.getOperand(cnt).getDefiningOp();
addCircuitOperandToEraseList(qubitOp);
}

else
} else
llvm_unreachable("unkown circuit argument.");
}
}
Expand Down Expand Up @@ -524,7 +511,6 @@ mlir::Value QUIRToPulsePass::convertAngleToF64(Operation *angleOp,
if (classicalQUIROpLocToConvertedPulseOpMap.find(angleLocHash) ==
classicalQUIROpLocToConvertedPulseOpMap.end()) {
if (auto castOp = dyn_cast<quir::ConstantOp>(angleOp)) {
addCircuitOperandToEraseList(angleOp);
double const angleVal =
castOp.getAngleValueFromConstant().convertToDouble();
auto f64Angle = builder.create<mlir::arith::ConstantOp>(
Expand All @@ -538,7 +524,6 @@ mlir::Value QUIRToPulsePass::convertAngleToF64(Operation *angleOp,
angleCastedOp->moveAfter(castOp);
classicalQUIROpLocToConvertedPulseOpMap[angleLocHash] = angleCastedOp;
} else if (auto castOp = dyn_cast<oq3::CastOp>(angleOp)) {
addCircuitOperandToEraseList(angleOp);
auto castOpArg = castOp.getArg();
if (auto paramCastOp =
dyn_cast<qcs::ParameterLoadOp>(castOpArg.getDefiningOp())) {
Expand All @@ -563,7 +548,6 @@ mlir::Value QUIRToPulsePass::convertDurationToI64(
if (classicalQUIROpLocToConvertedPulseOpMap.find(durLocHash) ==
classicalQUIROpLocToConvertedPulseOpMap.end()) {
if (auto castOp = dyn_cast<quir::ConstantOp>(durationOp)) {
addCircuitOperandToEraseList(durationOp);
auto durVal =
quir::getDuration(castOp).get().getDuration().convertToDouble();
assert(castOp.getType().dyn_cast<DurationType>().getUnits() ==
Expand Down Expand Up @@ -621,21 +605,6 @@ QUIRToPulsePass::addWfrOpToIR(std::string const &wfrName,
return openedWfrs[wfrName];
}

void QUIRToPulsePass::addCircuitToEraseList(mlir::Operation *op) {
assert(op && "caller requested adding a null op to erase list");
if (std::find(quirCircuitEraseList.begin(), quirCircuitEraseList.end(), op) ==
quirCircuitEraseList.end())
quirCircuitEraseList.push_back(op);
}

void QUIRToPulsePass::addCircuitOperandToEraseList(mlir::Operation *op) {
assert(op && "caller requested adding a null op to erase list");
if (std::find(quirCircuitOperandEraseList.begin(),
quirCircuitOperandEraseList.end(),
op) == quirCircuitOperandEraseList.end())
quirCircuitOperandEraseList.push_back(op);
}

void QUIRToPulsePass::parsePulseWaveformContainerOps(
std::string &waveformContainerPath) {
std::string errorMessage;
Expand Down
2 changes: 0 additions & 2 deletions test/Conversion/QUIRToPulse/convert-quir-to-pulse.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ module {
%7:2 = quir.call_circuit @circuit_0_q5_q3_circuit_1_q5(%5, %3) : (!quir.qubit<1>, !quir.qubit<1>) -> (i1, i1)
// CHECK-NOT: %7:2 = quir.call_circuit @circuit_0_q5_q3_circuit_1_q5(%5, %3) : (!quir.qubit<1>, !quir.qubit<1>) -> (i1, i1)
// CHECK: %14:4 = pulse.call_sequence @circuit_0_q5_q3_circuit_1_q5_sequence(%1, %3, %5, %7, %9, %11) : (!pulse.mixed_frame, !pulse.mixed_frame, !pulse.mixed_frame, !pulse.mixed_frame, !pulse.mixed_frame, !pulse.mixed_frame) -> (i1, i1, i1, i1)
quir.barrier %3, %5 : (!quir.qubit<1>, !quir.qubit<1>) -> ()
// CHECK-NOT: %quir.barrier %3, %5 : (!quir.qubit<1>, !quir.qubit<1>) -> ()
%8:2 = quir.call_circuit @circuit_2_q5_q3_circuit_3_q5(%5, %3) : (!quir.qubit<1>, !quir.qubit<1>) -> (i1, i1)
// CHECK-NOT: %8:2 = quir.call_circuit @circuit_2_q5_q3_circuit_3_q5(%5, %3) : (!quir.qubit<1>, !quir.qubit<1>) -> (i1, i1)
// CHECK: %15:6 = pulse.call_sequence @circuit_2_q5_q3_circuit_3_q5_sequence(%3, %12, %13, %5, %7, %9, %11) : (!pulse.mixed_frame, !pulse.mixed_frame, !pulse.mixed_frame, !pulse.mixed_frame, !pulse.mixed_frame, !pulse.mixed_frame, !pulse.mixed_frame) -> (i1, i1, i1, i1, i1, i1)
Expand Down

0 comments on commit f564f5d

Please sign in to comment.