Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

State synthesis for quantum devices #2291

Open
wants to merge 44 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
ac01dd1
DCO Remediation Commit for Ben Howe <[email protected]>
bmhowe23 Oct 11, 2024
21a87c1
State pointer synthesis for quantum hardware
annagrin Sep 17, 2024
3fc56de
Merge with main
annagrin Oct 17, 2024
7969a75
Merge with main
annagrin Oct 17, 2024
755d0d1
Fix test failure on anyon platform
annagrin Oct 17, 2024
dc5e77e
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin Oct 17, 2024
382bc99
Make StateInitialization a funcOp pass
annagrin Oct 17, 2024
d3a05d4
Fix issues and tests for the rest of quantum architectures
annagrin Oct 18, 2024
ac151f2
Merge with main
annagrin Oct 18, 2024
51ef054
Fix failing quantinuum state prep tests
annagrin Oct 18, 2024
0cdf3e9
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin Oct 18, 2024
5307aa4
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin Oct 21, 2024
a7f5387
Address CR comments
annagrin Oct 21, 2024
eb8db13
Merge with main
annagrin Oct 21, 2024
9f0937f
Format
annagrin Oct 21, 2024
2f3a623
Fix failing test
annagrin Oct 22, 2024
b381350
Format
annagrin Oct 22, 2024
dc87ca4
Format
annagrin Oct 22, 2024
e4c7735
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin Oct 22, 2024
53a34c9
Replaced getState intrinsic by cc.get_state op
annagrin Oct 22, 2024
30777f3
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin Oct 22, 2024
fe6d409
Remove print
annagrin Oct 22, 2024
48704e3
Remove getCudaqState references
annagrin Oct 22, 2024
137f621
Minor updates
annagrin Oct 22, 2024
ad7c6bc
Fix failing quake test
annagrin Oct 23, 2024
83683f7
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin Nov 4, 2024
78c0a44
Add a few state-related cc ops
annagrin Nov 5, 2024
6682c39
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into st…
annagrin Nov 5, 2024
102f819
Fix test_argument_conversion
annagrin Nov 5, 2024
6b2c015
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into st…
annagrin Nov 5, 2024
5ea1d97
Add printing in failing tests
annagrin Nov 5, 2024
074c60f
Add printing in failing tests
annagrin Nov 5, 2024
310f6ca
Fix failing tests
annagrin Nov 12, 2024
f0176ae
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into st…
annagrin Nov 12, 2024
d17fa6d
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin Nov 12, 2024
3425182
Merge with state-ops
annagrin Nov 12, 2024
6fdccba
Add description for new algorithm for state syntesis
annagrin Nov 12, 2024
fc5e154
Merge with main
annagrin Jan 9, 2025
1dfa805
Fix tests
annagrin Jan 9, 2025
b67fc88
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin Jan 9, 2025
9563371
Make intermediate IR legal by separating allocs
annagrin Jan 21, 2025
f32b066
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin Jan 21, 2025
008e8c1
DCO Remediation Commit for Anna Gringauze <[email protected]>
annagrin Jan 21, 2025
84a4369
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin Jan 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/cudaq/Optimizer/Builder/Intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ static constexpr const char createCudaqStateFromDataFP32[] =
// Delete a state created by the runtime functions above.
static constexpr const char deleteCudaqState[] = "__nvqpp_cudaq_state_delete";

// Get state of a kernel (placeholder function, calls are always replaced in
// opts)
static constexpr const char getCudaqState[] = "__nvqpp_cudaq_state_get";
annagrin marked this conversation as resolved.
Show resolved Hide resolved

/// Builder for lowering the clang AST to an IR for CUDA-Q. Lowering includes
/// the transformation of both quantum and classical computation. Different
/// features of the CUDA-Q programming model are lowered into different dialects
Expand Down
38 changes: 38 additions & 0 deletions include/cudaq/Optimizer/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,44 @@ def DeleteStates : Pass<"delete-states", "mlir::ModuleOp"> {
}];
}

def StateInitialization : Pass<"state-initialization", "mlir::ModuleOp"> {
let summary =
"Replace `quake.init_state` instructions with call to the kernel generating the state";
let description = [{
Argument synthesis for state pointers for quantum devices substitutes state
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Argument synthesis for state pointers for quantum devices substitutes state
Argument synthesis for state pointers for quantum devices will substitute

argument by a new state created from `__nvqpp_cudaq_state_get` intrinsic, which
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
argument by a new state created from `__nvqpp_cudaq_state_get` intrinsic, which
a new state created from the `__nvqpp_cudaq_state_get` intrinsic for the state argument.

in turn accepts the name for the synthesized kernel that generated the state.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
in turn accepts the name for the synthesized kernel that generated the state.
The `__nvqpp_cudaq_state_get` intrinsic accepts the symbol for the synthesized kernel that generated the state.


This optimization completes the replacement of `quake.init_state` instruction by:

- Replace `quake.init_state` by a call that `get_state` call refers to.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Replace `quake.init_state` by a call that `get_state` call refers to.
- Replacing `quake.init_state` by a call to a kernel to construct the state. e.g., The `cudaq::get_state` call refers to the result of a specific quantum kernel being invoked with a set of parameters.

- Remove all unneeded instructions.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Remove all unneeded instructions.
- Remove any unneeded Ops.

I think we only really need to remove Ops that have side-effects. Any that are pure and unused will just go away with the next DCE that runs.


For example:

Before StateInitialization (state-initialization):
```
func.func @foo() attributes {"cudaq-entrypoint", "cudaq-kernel", no_this} {
%0 = cc.string_literal "__nvqpp__mlirgen__test_init_state.modified_0" : !cc.ptr<!cc.array<i8 x 45>>
%1 = cc.cast %0 : (!cc.ptr<!cc.array<i8 x 45>>) -> !cc.ptr<i8>
%2 = call @__nvqpp_cudaq_state_get(%1) : (!cc.ptr<i8>) -> !cc.ptr<!cc.state>
%3 = call @__nvqpp_cudaq_state_numberOfQubits(%2) : (!cc.ptr<!cc.state>) -> i64
%4 = quake.alloca !quake.veq<?>[%3 : i64]
%5 = quake.init_state %4, %2 : (!quake.veq<?>, !cc.ptr<!cc.state>) -> !quake.veq<?>
return
}
```

After StateInitialization (state-initialization):
```
func.func @foo() attributes {"cudaq-entrypoint", "cudaq-kernel", no_this} {
%5 = call @__nvqpp__mlirgen__test_init_state.modified_0() : () -> !quake.veq<?>
return
}
```
}];
}

def StatePreparation : Pass<"state-prep", "mlir::ModuleOp"> {
let summary =
"Convert state vector data into gates";
Expand Down
4 changes: 4 additions & 0 deletions lib/Optimizer/Builder/Intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ static constexpr IntrinsicCode intrinsicTable[] = {

{cudaq::deleteCudaqState, {}, R"#(
func.func private @__nvqpp_cudaq_state_delete(%p : !cc.ptr<!cc.state>) -> ()
)#"},

{cudaq::getCudaqState, {}, R"#(
func.func private @__nvqpp_cudaq_state_get(%p : !cc.ptr<i8>) -> !cc.ptr<!cc.state>
)#"},

{cudaq::getNumQubitsFromCudaqState, {}, R"#(
Expand Down
3 changes: 2 additions & 1 deletion lib/Optimizer/CodeGen/VerifyNVQIRCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ struct VerifyNVQIRCallOpsPass
cudaq::getNumQubitsFromCudaqState,
cudaq::createCudaqStateFromDataFP32,
cudaq::createCudaqStateFromDataFP64,
cudaq::deleteCudaqState};
cudaq::deleteCudaqState,
cudaq::getCudaqState};
// It must be either NVQIR extension functions or in the allowed list.
return std::find(NVQIR_FUNCS.begin(), NVQIR_FUNCS.end(), functionName) !=
NVQIR_FUNCS.end() ||
Expand Down
1 change: 1 addition & 0 deletions lib/Optimizer/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ add_cudaq_library(OptTransforms
QuakeSynthesizer.cpp
RefToVeqAlloc.cpp
RegToMem.cpp
StateInitialization.cpp
StatePreparation.cpp
UnitarySynthesis.cpp
WiresToWiresets.cpp
Expand Down
11 changes: 7 additions & 4 deletions lib/Optimizer/Transforms/LiftArrayAlloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,10 @@ class AllocaPattern : public OpRewritePattern<cudaq::cc::AllocaOp> {
if (auto load = dyn_cast<cudaq::cc::LoadOp>(useuser)) {
rewriter.setInsertionPointAfter(useuser);
LLVM_DEBUG(llvm::dbgs() << "replaced load\n");
rewriter.replaceOpWithNewOp<cudaq::cc::ExtractValueOp>(
load, eleTy, conArr,
ArrayRef<cudaq::cc::ExtractValueArg>{offset});
auto extract = rewriter.create<cudaq::cc::ExtractValueOp>(
loc, eleTy, conArr, ArrayRef<cudaq::cc::ExtractValueArg>{offset});
rewriter.replaceAllUsesWith(load, extract);
toErase.push_back(load);
continue;
}
if (isa<cudaq::cc::StoreOp>(useuser))
Expand All @@ -199,8 +200,10 @@ class AllocaPattern : public OpRewritePattern<cudaq::cc::AllocaOp> {
toErase.push_back(alloc);
}

for (auto *op : toErase)
for (auto *op : toErase) {
op->dropAllUses();
rewriter.eraseOp(op);
}

return success();
}
Expand Down
146 changes: 146 additions & 0 deletions lib/Optimizer/Transforms/StateInitialization.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*******************************************************************************
* Copyright (c) 2022 - 2024 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/Dialect/CC/CCOps.h"
#include "cudaq/Optimizer/Dialect/Quake/QuakeOps.h"
#include "cudaq/Optimizer/Transforms/Passes.h"
#include "mlir/Dialect/Complex/IR/Complex.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "mlir/Transforms/Passes.h"
#include <span>

namespace cudaq::opt {
#define GEN_PASS_DEF_STATEINITIALIZATION
#include "cudaq/Optimizer/Transforms/Passes.h.inc"
} // namespace cudaq::opt

#define DEBUG_TYPE "state-initialization"

using namespace mlir;

namespace {

static bool isCall(Operation *callOp, std::vector<const char *> &&names) {
if (callOp) {
if (auto createStateCall = dyn_cast<func::CallOp>(callOp)) {
if (auto calleeAttr = createStateCall.getCalleeAttr()) {
annagrin marked this conversation as resolved.
Show resolved Hide resolved
auto funcName = calleeAttr.getValue().str();
if (std::find(names.begin(), names.end(), funcName) != names.end())
return true;
annagrin marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
return false;
}

static bool isGetStateCall(Operation *callOp) {
return isCall(callOp, {cudaq::getCudaqState});
}

static bool isNumberOfQubitsCall(Operation *callOp) {
return isCall(callOp, {cudaq::getNumQubitsFromCudaqState});
}

// clang-format off
/// Replace `quake.init_state` by a call to a (modified) kernel that produced the state.
annagrin marked this conversation as resolved.
Show resolved Hide resolved
/// ```
/// %0 = cc.string_literal "callee.modified_0" : !cc.ptr<!cc.array<i8 x 27>>
/// %1 = cc.cast %0 : (!cc.ptr<!cc.array<i8 x 27>>) -> !cc.ptr<i8>
/// %2 = call @__nvqpp_cudaq_state_get(%1) : (!cc.ptr<i8>) -> !cc.ptr<!cc.state>
/// %3 = call @__nvqpp_cudaq_state_numberOfQubits(%2) : (!cc.ptr<!cc.state>) -> i64
/// %4 = quake.alloca !quake.veq<?>[%3 : i64]
/// %5 = quake.init_state %4, %2 : (!quake.veq<?>, !cc.ptr<!cc.state>) -> !quake.veq<?>
/// ───────────────────────────────────────────
/// ...
/// %5 = call @callee.modified_0() : () -> !quake.veq<?>
annagrin marked this conversation as resolved.
Show resolved Hide resolved
/// ```
// clang-format on
class StateInitPattern : public OpRewritePattern<quake::InitializeStateOp> {
public:
using OpRewritePattern::OpRewritePattern;

LogicalResult matchAndRewrite(quake::InitializeStateOp initState,
PatternRewriter &rewriter) const override {
auto loc = initState.getLoc();
auto allocaOp = initState.getOperand(0).getDefiningOp();
annagrin marked this conversation as resolved.
Show resolved Hide resolved
auto getStateOp = initState.getOperand(1).getDefiningOp();
auto numOfQubits = allocaOp->getOperand(0).getDefiningOp();

if (isGetStateCall(getStateOp)) {
auto calleeNameOp = getStateOp->getOperand(0);
if (auto cast =
dyn_cast<cudaq::cc::CastOp>(calleeNameOp.getDefiningOp())) {
calleeNameOp = cast.getOperand();

if (auto literal = dyn_cast<cudaq::cc::CreateStringLiteralOp>(
calleeNameOp.getDefiningOp())) {
auto calleeName = literal.getStringLiteral();

Value result =
rewriter
.create<func::CallOp>(loc, initState.getType(), calleeName,
mlir::ValueRange{})
.getResult(0);
rewriter.replaceAllUsesWith(initState, result);
initState.erase();
allocaOp->dropAllUses();
rewriter.eraseOp(allocaOp);
if (isNumberOfQubitsCall(numOfQubits)) {
numOfQubits->dropAllUses();
rewriter.eraseOp(numOfQubits);
}
getStateOp->dropAllUses();
rewriter.eraseOp(getStateOp);
cast->dropAllUses();
rewriter.eraseOp(cast);
literal->dropAllUses();
rewriter.eraseOp(literal);
return success();
}
}
}
return failure();
annagrin marked this conversation as resolved.
Show resolved Hide resolved
}
};

class StateInitializationPass
: public cudaq::opt::impl::StateInitializationBase<
StateInitializationPass> {
public:
using StateInitializationBase::StateInitializationBase;

void runOnOperation() override {
auto *ctx = &getContext();
auto module = getOperation();
for (Operation &op : *module.getBody()) {
auto func = dyn_cast<func::FuncOp>(op);
if (!func)
continue;

std::string funcName = func.getName().str();
RewritePatternSet patterns(ctx);
patterns.insert<StateInitPattern>(ctx);

LLVM_DEBUG(llvm::dbgs()
<< "Before state initialization: " << func << '\n');

if (failed(applyPatternsAndFoldGreedily(func.getOperation(),
std::move(patterns))))
signalPassFailure();

LLVM_DEBUG(llvm::dbgs()
<< "After state initialization: " << func << '\n');
}
}
};
} // namespace
5 changes: 3 additions & 2 deletions python/runtime/cudaq/algorithms/py_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ class PyRemoteSimulationState : public RemoteSimulationState {
}
}

std::pair<std::string, std::vector<void *>> getKernelInfo() const override {
return {kernelName, argsData->getArgs()};
std::optional<std::pair<std::string, std::vector<void *>>>
getKernelInfo() const override {
return std::make_pair(kernelName, argsData->getArgs());
}

std::complex<double> overlap(const cudaq::SimulationState &other) override {
Expand Down
2 changes: 1 addition & 1 deletion python/runtime/cudaq/platform/py_alt_launch_kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ MlirModule synthesizeKernel(const std::string &name, MlirModule module,
auto isLocalSimulator = platform.is_simulator() && !platform.is_emulated();
auto isSimulator = isLocalSimulator || isRemoteSimulator;

cudaq::opt::ArgumentConverter argCon(name, unwrap(module), isSimulator);
cudaq::opt::ArgumentConverter argCon(name, unwrap(module));
argCon.gen(runtimeArgs.getArgs());
std::string kernName = cudaq::runtime::cudaqGenPrefixName + name;
SmallVector<StringRef> kernels = {kernName};
Expand Down
Loading
Loading