forked from NVIDIA/cuda-quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveMeasurements.cpp
More file actions
66 lines (57 loc) · 2.47 KB
/
Copy pathRemoveMeasurements.cpp
File metadata and controls
66 lines (57 loc) · 2.47 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
/*******************************************************************************
* 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/Intrinsics.h"
#include "cudaq/Optimizer/CodeGen/Passes.h"
#include "cudaq/Optimizer/CodeGen/QIRFunctionNames.h"
#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "mlir/Transforms/Passes.h"
namespace cudaq::opt {
#define GEN_PASS_DEF_REMOVEMEASUREMENTS
#include "cudaq/Optimizer/CodeGen/Passes.h.inc"
} // namespace cudaq::opt
#define DEBUG_TYPE "qir-remove-measurements"
using namespace mlir;
namespace {
class EraseMeasurements : public OpRewritePattern<LLVM::CallOp> {
public:
using OpRewritePattern<LLVM::CallOp>::OpRewritePattern;
LogicalResult matchAndRewrite(LLVM::CallOp call,
PatternRewriter &rewriter) const override {
if (auto callee = call.getCallee()) {
if (*callee == cudaq::opt::QIRMeasureBody ||
*callee == cudaq::opt::QIRRecordOutput ||
*callee == cudaq::opt::QIRArrayRecordOutput) {
rewriter.eraseOp(call);
return success();
}
}
return failure();
}
};
/// Remove Measurements
///
/// This pass removes measurements and the corresponding output recording calls.
/// This is needed for backends that don't support selective measurement calls.
/// For example: https://github.com/NVIDIA/cuda-quantum/issues/512
struct RemoveMeasurementsPass
: public cudaq::opt::impl::RemoveMeasurementsBase<RemoveMeasurementsPass> {
using RemoveMeasurementsBase::RemoveMeasurementsBase;
void runOnOperation() override {
auto *op = getOperation();
auto *context = &getContext();
RewritePatternSet patterns(context);
patterns.insert<EraseMeasurements>(context);
LLVM_DEBUG(llvm::dbgs() << "Before measurement erasure:\n" << *op);
if (failed(applyPatternsGreedily(op, std::move(patterns))))
signalPassFailure();
LLVM_DEBUG(llvm::dbgs() << "After measurement erasure:\n" << *op);
}
};
} // namespace