Skip to content

State synthesis for quantum devices #2291

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

Merged
merged 92 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
92 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
1c0a4b3
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin Feb 11, 2025
f8e35eb
Address some PR comments
annagrin Feb 12, 2025
e79ad6a
Address more CR comments
annagrin Feb 12, 2025
88cd5d5
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin Feb 12, 2025
c0d9ae9
Cleanup
annagrin Feb 13, 2025
1ecd8cc
Address CR comments
annagrin Feb 13, 2025
0238a66
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin Feb 14, 2025
de387fc
Address more CR comments
annagrin Feb 14, 2025
a5150a5
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin Feb 14, 2025
7cf306a
Address more CR comments
annagrin Feb 18, 2025
16de803
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin Feb 18, 2025
1402471
Store new functions in subst module and update synthesis
annagrin Feb 20, 2025
816fdca
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin Feb 20, 2025
9a528dd
Make argument synthesis transitive
annagrin Feb 20, 2025
af2fd79
Update callers of synthesis
annagrin Feb 20, 2025
a25d468
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin Feb 20, 2025
4d6f7ee
Use PointerOf in quake defs
annagrin Feb 20, 2025
e7d95d5
Addressed more CR comments
annagrin Feb 20, 2025
c4d600f
Recursive with caching
annagrin Feb 21, 2025
e58f577
StateAggregatorWithArgumentConverter
annagrin Feb 21, 2025
6c0dd7d
Make ArgumentConverter handle the state call tree
annagrin Feb 25, 2025
472ff60
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into ar…
annagrin Mar 3, 2025
5ef4c3d
Make argument converter handle kernels created from states
annagrin Mar 3, 2025
ab28edb
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin Mar 3, 2025
5e43057
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into ar…
annagrin Mar 3, 2025
36d9425
Merge branch 'argument-converter-all-functions' of https://github.com…
annagrin Mar 3, 2025
7c76533
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin Mar 4, 2025
7977a2e
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin Mar 6, 2025
efdbbcc
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin Mar 10, 2025
6fefc27
Fix null alloc size and add tests
annagrin Mar 10, 2025
7e39116
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin Mar 12, 2025
55e25dd
Keep storing ops when generating numSubits func
annagrin Mar 12, 2025
745fc5d
Cleanup
annagrin Mar 12, 2025
4322ee4
Merge with main
annagrin Mar 12, 2025
b8c8c8a
Cleanup
annagrin Mar 12, 2025
98c19af
Merge with main
annagrin Mar 14, 2025
edf0224
Cleanup
annagrin Mar 14, 2025
6b5161a
Fix failing test
annagrin Mar 14, 2025
054f4b7
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin Mar 14, 2025
4df4390
Fix failing doc build
annagrin Mar 14, 2025
a5d63c5
Address CR comments
annagrin Mar 18, 2025
eba1c1d
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin Mar 18, 2025
4a12db0
Address more CR comments and add a test
annagrin Mar 18, 2025
cc1faea
Address more CR comments
annagrin Mar 18, 2025
395acbe
Merge with main
annagrin Mar 20, 2025
175a70d
Fix links
annagrin Mar 20, 2025
e1449de
Fix links
annagrin Mar 20, 2025
25d18db
Merge with main
annagrin Mar 20, 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";

/// 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
argument by a new state created from `__nvqpp_cudaq_state_get` intrinsic, which
in turn accepts the name 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.
- Remove all unneeded instructions.

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()) {
auto funcName = calleeAttr.getValue().str();
if (std::find(names.begin(), names.end(), funcName) != names.end())
return true;
}
}
}
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.
/// ```
/// %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<?>
/// ```
// 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();
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();
}
};

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