-
Notifications
You must be signed in to change notification settings - Fork 355
Expand file tree
/
Copy pathCombineMeasurements.cpp
More file actions
283 lines (241 loc) · 9.83 KB
/
CombineMeasurements.cpp
File metadata and controls
283 lines (241 loc) · 9.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*******************************************************************************
* Copyright (c) 2022 - 2026 NVIDIA Corporation & Affiliates. *
* All rights reserved. *
* *
* This source code and the accompanying materials are made available under *
* the terms of the Apache License 2.0 which accompanies this distribution. *
******************************************************************************/
#include "PassDetails.h"
#include "cudaq/Optimizer/Builder/Factory.h"
#include "cudaq/Optimizer/CodeGen/QIRAttributeNames.h"
#include "cudaq/Optimizer/Dialect/CC/CCOps.h"
#include "cudaq/Optimizer/Dialect/Quake/QuakeOps.h"
#include "cudaq/Optimizer/Dialect/Quake/QuakeTypes.h"
#include "cudaq/Optimizer/Transforms/Passes.h"
#include "nlohmann/json.hpp"
#include "llvm/Support/Debug.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "mlir/Transforms/Passes.h"
namespace cudaq::opt {
#define GEN_PASS_DEF_COMBINEMEASUREMENTS
#include "cudaq/Optimizer/Transforms/Passes.h.inc"
} // namespace cudaq::opt
#define DEBUG_TYPE "combine-measurements"
using namespace mlir;
namespace {
// After combine-quantum-alloc, we have one top allocation per function.
// The following type is used to store qubit mapping from result qubit
// index to the actual qubit index and register name.
// map[result] --> [qb,regName]
// Note: register name is currently not used in `OpenQasm2` backends,
// so we supply a bogus name.
using OutputNamesType =
std::map<std::size_t, std::pair<std::size_t, std::string>>;
struct Analysis {
Analysis() = default;
Analysis(const Analysis &) = delete;
Analysis(Analysis &&) = delete;
Analysis &operator=(const Analysis &) = delete;
mlir::DenseMap<mlir::Value, std::size_t> measurements;
OutputNamesType resultQubitVals;
quake::MzOp lastMeasurement;
bool empty() const { return measurements.empty(); }
LogicalResult analyze(func::FuncOp func) {
quake::AllocaOp qalloc;
std::size_t currentOffset = 0;
for (auto &block : func.getRegion()) {
for (auto &op : block) {
if (auto alloc = dyn_cast_or_null<quake::AllocaOp>(&op)) {
if (qalloc)
return op.emitError("Multiple qalloc statements found");
qalloc = alloc;
} else if (auto measure = dyn_cast_or_null<quake::MzOp>(&op)) {
if (!measure.use_empty()) {
measure.emitWarning("Measurements with uses are not supported");
return success();
}
auto veqOp = measure.getOperand(0);
auto ty = veqOp.getType();
std::size_t size = 0;
if (auto veqTy = dyn_cast<quake::RefType>(ty))
size = 1;
else if (auto veqTy = dyn_cast<quake::VeqType>(ty)) {
size = veqTy.getSize();
if (size == 0)
return op.emitError("Unknown measurement size");
}
measurements[measure.getMeasOut()] = currentOffset;
lastMeasurement = measure;
currentOffset += size;
}
}
}
return success();
}
};
class ExtendQubitMeasurePattern : public OpRewritePattern<quake::MzOp> {
public:
using OpRewritePattern::OpRewritePattern;
explicit ExtendQubitMeasurePattern(MLIRContext *ctx, Analysis &analysis)
: OpRewritePattern(ctx), analysis(analysis) {}
// Replace a pattern such as:
// ```
// %0 = ...: !quake.veq<2>
// %1 = quake.extract_ref %0[0] : (!quake.veq<2>) -> !quake.ref
// %measOut = quake.mz %1 : (!quake.ref) -> !quake.measure
// ```
// with:
// ```
// %1 = ... : !quake.veq<4>
// %measOut = quake.mz %1 : (!quake.veq<4>) -> !quake.measurements<4>
// ```
// And collect output names information: `"[[[0,[1,"q0"]],[1,[2,"q1"]]]]"`
LogicalResult matchAndRewrite(quake::MzOp measure,
PatternRewriter &rewriter) const override {
auto veqOp = measure.getOperand(0);
if (auto extract = veqOp.getDefiningOp<quake::ExtractRefOp>()) {
auto veq = extract.getVeq();
std::size_t idx;
if (extract.hasConstantIndex())
idx = extract.getConstantIndex();
else if (auto cst =
extract.getIndex().getDefiningOp<arith::ConstantIntOp>())
idx = static_cast<std::size_t>(cst.value());
else
return extract.emitError("Non-constant index in ExtractRef");
auto offset = analysis.measurements[measure.getMeasOut()];
analysis.resultQubitVals[offset] =
std::make_pair(idx, std::to_string(idx));
Type resultType;
if (auto veqTy = dyn_cast<quake::VeqType>(veq.getType());
veqTy && veqTy.hasSpecifiedSize())
resultType = quake::MeasurementsType::get(measure->getContext(),
veqTy.getSize());
else
resultType = quake::MeasurementsType::getUnsized(measure->getContext());
if (measure == analysis.lastMeasurement) {
rewriter.replaceOpWithNewOp<quake::MzOp>(measure, TypeRange{resultType},
ValueRange{veq},
measure.getRegisterNameAttr());
return success();
}
if (measure.use_empty()) {
// Erasing an unused intermediate measurement is a valid rewrite action.
rewriter.eraseOp(measure);
return success();
}
}
return failure();
}
private:
Analysis &analysis;
};
class ExtendVeqMeasurePattern : public OpRewritePattern<quake::MzOp> {
public:
using OpRewritePattern::OpRewritePattern;
explicit ExtendVeqMeasurePattern(MLIRContext *ctx, Analysis &analysis)
: OpRewritePattern(ctx), analysis(analysis) {}
// Replace a pattern such as:
// ```
// %1 = ... : !quake.veq<4>
// %2 = quake.subveq %1, %c1, %c2 : (!quake.veq<4>, i32, i32) ->
// !quake.veq<2>
// %measOut = quake.mz %2 : (!quake.veq<2>) -> !quake.measurements<2>
// ```
// with:
// ```
// %1 = ... : !quake.veq<4>
// %measOut = quake.mz %1 : (!quake.veq<4>) -> !quake.measurements<4>
// ```
// And collect output names information: `"[[[0,[1,"q0"]],[1,[2,"q1"]]]]"`
LogicalResult matchAndRewrite(quake::MzOp measure,
PatternRewriter &rewriter) const override {
auto veqOp = measure.getOperand(0);
if (auto subveq = veqOp.getDefiningOp<quake::SubVeqOp>()) {
std::size_t low;
if (subveq.hasConstantLowerBound())
low = subveq.getConstantLowerBound();
else {
auto value = cudaq::opt::factory::getIntIfConstant(subveq.getLower());
if (!value.has_value())
return subveq.emitError("Non-constant lower index in subveq");
low = static_cast<std::size_t>(value.value());
}
std::size_t high;
if (subveq.hasConstantUpperBound())
high = subveq.getConstantUpperBound();
else {
auto value = cudaq::opt::factory::getIntIfConstant(subveq.getUpper());
if (!value.has_value())
return subveq.emitError("Non-constant upper index in subveq");
high = static_cast<std::size_t>(value.value());
}
for (std::size_t i = low; i <= high; i++) {
auto start = analysis.measurements[measure.getMeasOut()];
auto offset = i - low + start;
analysis.resultQubitVals[offset] = std::make_pair(i, std::to_string(i));
}
if (measure == analysis.lastMeasurement) {
auto veq = subveq.getVeq();
Type resultType;
if (auto veqTy = dyn_cast<quake::VeqType>(veq.getType());
veqTy && veqTy.hasSpecifiedSize())
resultType = quake::MeasurementsType::get(measure->getContext(),
veqTy.getSize());
else
resultType =
quake::MeasurementsType::getUnsized(measure->getContext());
rewriter.replaceOpWithNewOp<quake::MzOp>(measure, TypeRange{resultType},
ValueRange{veq},
measure.getRegisterNameAttr());
} else if (measure.use_empty())
rewriter.eraseOp(measure);
return success();
}
return failure();
}
private:
Analysis &analysis;
};
class CombineMeasurementsPass
: public cudaq::opt::impl::CombineMeasurementsBase<
CombineMeasurementsPass> {
public:
using CombineMeasurementsBase::CombineMeasurementsBase;
void runOnOperation() override {
auto *ctx = &getContext();
func::FuncOp func = getOperation();
OpBuilder builder(func);
LLVM_DEBUG(llvm::dbgs() << "Function before combining measurements:\n"
<< func << "\n\n");
// Analyze the function to find all qubit mappings.
Analysis analysis;
if (failed(analysis.analyze(func))) {
func.emitOpError("Combining measurements failed");
signalPassFailure();
}
if (analysis.empty())
return;
// Extend measurement into one last full measurement.
RewritePatternSet patterns(ctx);
patterns.insert<ExtendQubitMeasurePattern, ExtendVeqMeasurePattern>(
ctx, analysis);
if (failed(applyPatternsAndFoldGreedily(func.getOperation(),
std::move(patterns)))) {
func.emitOpError("Combining measurements failed");
signalPassFailure();
}
// Add output names mapping attribute.
if (!analysis.resultQubitVals.empty()) {
nlohmann::json resultQubitJSON{analysis.resultQubitVals};
func->setAttr(cudaq::opt::QIROutputNamesAttrName,
builder.getStringAttr(resultQubitJSON.dump()));
}
LLVM_DEBUG(llvm::dbgs() << "Function after combining measurements:\n"
<< func << "\n\n");
}
};
} // namespace