|
13 | 13 | // that they have been altered from the originals.
|
14 | 14 | //
|
15 | 15 | //===----------------------------------------------------------------------===//
|
16 |
| -// |
17 |
| -// This file declares the pass for converting QUIR to Pulse dialect |
18 |
| -// |
| 16 | +/// |
| 17 | +/// This file declares the pass for converting QUIR circuits to Pulse sequences |
| 18 | +/// |
19 | 19 | //===----------------------------------------------------------------------===//
|
20 | 20 |
|
21 |
| -#ifndef PULSE_CONVERSION_QUIRTOPULSE_H |
22 |
| -#define PULSE_CONVERSION_QUIRTOPULSE_H |
| 21 | +#ifndef QUIRTOPULSE_CONVERSION_H |
| 22 | +#define QUIRTOPULSE_CONVERSION_H |
| 23 | + |
| 24 | +#include "Dialect/OQ3/IR/OQ3Ops.h" |
| 25 | +#include "Dialect/Pulse/IR/PulseOps.h" |
| 26 | +#include "Dialect/QCS/IR/QCSOps.h" |
23 | 27 |
|
24 |
| -#include "mlir/IR/Builders.h" |
25 |
| -#include "mlir/IR/Location.h" |
26 | 28 | #include "mlir/IR/MLIRContext.h"
|
27 |
| -#include "mlir/IR/Value.h" |
28 | 29 | #include "mlir/Pass/Pass.h"
|
29 |
| -#include "mlir/Transforms/DialectConversion.h" |
30 |
| -#include <functional> |
31 |
| -#include <memory> |
| 30 | + |
| 31 | +#include <queue> |
32 | 32 |
|
33 | 33 | namespace mlir::pulse {
|
34 | 34 |
|
35 |
| -class QUIRTypeConverter : public TypeConverter { |
36 |
| -public: |
37 |
| - using TypeConverter::TypeConverter; |
38 |
| - QUIRTypeConverter(); |
39 |
| -}; |
| 35 | +struct QUIRToPulsePass |
| 36 | + : public PassWrapper<QUIRToPulsePass, OperationPass<ModuleOp>> { |
| 37 | + std::string WAVEFORM_CONTAINER = ""; |
| 38 | + |
| 39 | + // this pass can optionally receive a path to a file containing pulse waveform |
| 40 | + // container operations, which will contain pulse waveform operations that |
| 41 | + // will be passed as argument to pulse calibration sequences. |
| 42 | + QUIRToPulsePass() = default; |
| 43 | + QUIRToPulsePass(const QUIRToPulsePass &pass) : PassWrapper(pass) {} |
| 44 | + QUIRToPulsePass(std::string inWfrContainer) { |
| 45 | + WAVEFORM_CONTAINER = std::move(inWfrContainer); |
| 46 | + } |
| 47 | + |
| 48 | + void runOnOperation() override; |
| 49 | + |
| 50 | + llvm::StringRef getArgument() const override; |
| 51 | + llvm::StringRef getDescription() const override; |
| 52 | + |
| 53 | + // optionally, one can override the path to pulse waveform container file with |
| 54 | + // this option; e.g., to write a LIT test one can invoke this pass with |
| 55 | + // --quir-to-pulse=waveform-container=<path-to-waveform-container-file> |
| 56 | + Option<std::string> waveformContainer{ |
| 57 | + *this, "waveform-container", |
| 58 | + llvm::cl::desc("an MLIR file containing waveform container operations"), |
| 59 | + llvm::cl::value_desc("filename"), llvm::cl::init("")}; |
40 | 60 |
|
| 61 | + mlir::Operation *mainFuncFirstOp; |
| 62 | + |
| 63 | + // convert quir circuit to pulse sequence |
| 64 | + void convertCircuitToSequence(mlir::quir::CallCircuitOp callCircuitOp, |
| 65 | + FuncOp &mainFunc, ModuleOp moduleOp); |
| 66 | + // helper datastructure for converting quir circuit to pulse sequence; these |
| 67 | + // will be reset every time convertCircuitToSequence is called and will be |
| 68 | + // used by several functions that are called within that function |
| 69 | + uint convertedSequenceOpArgIndex; |
| 70 | + std::map<uint, uint> circuitArgToConvertedSequenceArgMap; |
| 71 | + SmallVector<Value> convertedPulseSequenceOpArgs; |
| 72 | + std::vector<mlir::Attribute> convertedPulseCallSequenceOpOperandNames; |
| 73 | + |
| 74 | + // process the args of the circuit op, and add corresponding args to the |
| 75 | + // converted pulse sequence op |
| 76 | + void processCircuitArgs(mlir::quir::CallCircuitOp callCircuitOp, |
| 77 | + mlir::quir::CircuitOp circuitOp, |
| 78 | + SequenceOp convertedPulseSequenceOp, FuncOp &mainFunc, |
| 79 | + mlir::OpBuilder &builder); |
| 80 | + |
| 81 | + // process the args of the pulse cal sequence op corresponding to quirOp |
| 82 | + void processPulseCalArgs(mlir::Operation *quirOp, |
| 83 | + SequenceOp pulseCalSequenceOp, |
| 84 | + SmallVector<Value> &pulseCalSeqArgs, |
| 85 | + SequenceOp convertedPulseSequenceOp, |
| 86 | + FuncOp &mainFunc, mlir::OpBuilder &builder); |
| 87 | + void getQUIROpClassicalOperands(mlir::Operation *quirOp, |
| 88 | + std::queue<Value> &angleOperands, |
| 89 | + std::queue<Value> &durationOperands); |
| 90 | + void processMixFrameOpArg(std::string const &mixFrameName, |
| 91 | + std::string const &portName, |
| 92 | + SequenceOp convertedPulseSequenceOp, |
| 93 | + SmallVector<Value> &quirOpPulseCalSeqArgs, |
| 94 | + Value argumentValue, FuncOp &mainFunc, |
| 95 | + mlir::OpBuilder &builder); |
| 96 | + void processPortOpArg(std::string const &portName, |
| 97 | + SequenceOp convertedPulseSequenceOp, |
| 98 | + SmallVector<Value> &quirOpPulseCalSeqArgs, |
| 99 | + Value argumentValue, FuncOp &mainFunc, |
| 100 | + mlir::OpBuilder &builder); |
| 101 | + void processWfrOpArg(std::string const &wfrName, |
| 102 | + SequenceOp convertedPulseSequenceOp, |
| 103 | + SmallVector<Value> &quirOpPulseCalSeqArgs, |
| 104 | + Value argumentValue, FuncOp &mainFunc, |
| 105 | + mlir::OpBuilder &builder); |
| 106 | + void processAngleArg(Value nextAngleOperand, |
| 107 | + SequenceOp convertedPulseSequenceOp, |
| 108 | + SmallVector<Value> &quirOpPulseCalSeqArgs, |
| 109 | + mlir::OpBuilder &builder); |
| 110 | + void processDurationArg(Value frontDurOperand, |
| 111 | + SequenceOp convertedPulseSequenceOp, |
| 112 | + SmallVector<Value> &quirOpPulseCalSeqArgs, |
| 113 | + mlir::OpBuilder &builder); |
| 114 | + |
| 115 | + // convert angle to F64 |
| 116 | + mlir::Value convertAngleToF64(Operation *angleOp, mlir::OpBuilder &builder); |
| 117 | + // convert duration to I64 |
| 118 | + mlir::Value convertDurationToI64(mlir::quir::CallCircuitOp callCircuitOp, |
| 119 | + Operation *durOp, uint &cnt, |
| 120 | + mlir::OpBuilder &builder, FuncOp &mainFunc); |
| 121 | + // map of the hashed location of quir angle/duration ops to their converted |
| 122 | + // pulse ops |
| 123 | + std::map<std::string, mlir::Value> classicalQUIROpLocToConvertedPulseOpMap; |
| 124 | + |
| 125 | + // port name to Port_CreateOp map |
| 126 | + std::map<std::string, mlir::pulse::Port_CreateOp> openedPorts; |
| 127 | + // mixframe name to MixFrameOp map |
| 128 | + std::map<std::string, mlir::pulse::MixFrameOp> openedMixFrames; |
| 129 | + // waveform name to Waveform_CreateOp map |
| 130 | + std::map<std::string, mlir::pulse::Waveform_CreateOp> openedWfrs; |
| 131 | + // add a port to IR if it's not already added and return the Port_CreateOp |
| 132 | + mlir::pulse::Port_CreateOp addPortOpToIR(std::string const &portName, |
| 133 | + FuncOp &mainFunc, |
| 134 | + mlir::OpBuilder &builder); |
| 135 | + // add a mixframe to IR if it's not already added and return the MixFrameOp |
| 136 | + mlir::pulse::MixFrameOp addMixFrameOpToIR(std::string const &mixFrameName, |
| 137 | + std::string const &portName, |
| 138 | + FuncOp &mainFunc, |
| 139 | + mlir::OpBuilder &builder); |
| 140 | + // add a waveform to IR if it's not already added and return the |
| 141 | + // Waveform_CreateOp |
| 142 | + mlir::pulse::Waveform_CreateOp addWfrOpToIR(std::string const &wfrName, |
| 143 | + FuncOp &mainFunc, |
| 144 | + mlir::OpBuilder &builder); |
| 145 | + |
| 146 | + void addCircuitToEraseList(mlir::Operation *op); |
| 147 | + void addCallCircuitToEraseList(mlir::Operation *op); |
| 148 | + void addCircuitOperandToEraseList(mlir::Operation *op); |
| 149 | + std::vector<mlir::Operation *> quirCircuitEraseList; |
| 150 | + std::vector<mlir::Operation *> quirCallCircuitEraseList; |
| 151 | + std::vector<mlir::Operation *> quirCircuitOperandEraseList; |
| 152 | + |
| 153 | + // parse the waveform containers and add them to pulseNameToWaveformMap |
| 154 | + void parsePulseWaveformContainerOps(std::string &waveformContainerPath); |
| 155 | + std::map<std::string, Waveform_CreateOp> pulseNameToWaveformMap; |
| 156 | + |
| 157 | + static mlir::quir::CircuitOp |
| 158 | + getCircuitOp(mlir::quir::CallCircuitOp callCircuitOp); |
| 159 | +}; |
41 | 160 | } // namespace mlir::pulse
|
42 | 161 |
|
43 |
| -#endif // PULSE_CONVERSION_QUIRTOPULSE_H |
| 162 | +#endif // QUIRTOPULSE_CONVERSION_H |
0 commit comments