-
Notifications
You must be signed in to change notification settings - Fork 268
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
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 21a87c1
State pointer synthesis for quantum hardware
annagrin 3fc56de
Merge with main
annagrin 7969a75
Merge with main
annagrin 755d0d1
Fix test failure on anyon platform
annagrin dc5e77e
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin 382bc99
Make StateInitialization a funcOp pass
annagrin d3a05d4
Fix issues and tests for the rest of quantum architectures
annagrin ac151f2
Merge with main
annagrin 51ef054
Fix failing quantinuum state prep tests
annagrin 0cdf3e9
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin 5307aa4
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin a7f5387
Address CR comments
annagrin eb8db13
Merge with main
annagrin 9f0937f
Format
annagrin 2f3a623
Fix failing test
annagrin b381350
Format
annagrin dc87ca4
Format
annagrin e4c7735
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin 53a34c9
Replaced getState intrinsic by cc.get_state op
annagrin 30777f3
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin fe6d409
Remove print
annagrin 48704e3
Remove getCudaqState references
annagrin 137f621
Minor updates
annagrin ad7c6bc
Fix failing quake test
annagrin 83683f7
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin 78c0a44
Add a few state-related cc ops
annagrin 6682c39
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into st…
annagrin 102f819
Fix test_argument_conversion
annagrin 6b2c015
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into st…
annagrin 5ea1d97
Add printing in failing tests
annagrin 074c60f
Add printing in failing tests
annagrin 310f6ca
Fix failing tests
annagrin f0176ae
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into st…
annagrin d17fa6d
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin 3425182
Merge with state-ops
annagrin 6fdccba
Add description for new algorithm for state syntesis
annagrin fc5e154
Merge with main
annagrin 1dfa805
Fix tests
annagrin b67fc88
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin 9563371
Make intermediate IR legal by separating allocs
annagrin f32b066
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin 008e8c1
DCO Remediation Commit for Anna Gringauze <[email protected]>
annagrin 84a4369
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin 1c0a4b3
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin f8e35eb
Address some PR comments
annagrin e79ad6a
Address more CR comments
annagrin 88cd5d5
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin c0d9ae9
Cleanup
annagrin 1ecd8cc
Address CR comments
annagrin 0238a66
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin de387fc
Address more CR comments
annagrin a5150a5
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin 7cf306a
Address more CR comments
annagrin 16de803
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin 1402471
Store new functions in subst module and update synthesis
annagrin 816fdca
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin 9a528dd
Make argument synthesis transitive
annagrin af2fd79
Update callers of synthesis
annagrin a25d468
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin 4d6f7ee
Use PointerOf in quake defs
annagrin e7d95d5
Addressed more CR comments
annagrin c4d600f
Recursive with caching
annagrin e58f577
StateAggregatorWithArgumentConverter
annagrin 6c0dd7d
Make ArgumentConverter handle the state call tree
annagrin 472ff60
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into ar…
annagrin 5ef4c3d
Make argument converter handle kernels created from states
annagrin ab28edb
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin 5e43057
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into ar…
annagrin 36d9425
Merge branch 'argument-converter-all-functions' of https://github.com…
annagrin 7c76533
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin 7977a2e
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin efdbbcc
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin 6fefc27
Fix null alloc size and add tests
annagrin 7e39116
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin 55e25dd
Keep storing ops when generating numSubits func
annagrin 745fc5d
Cleanup
annagrin 4322ee4
Merge with main
annagrin b8c8c8a
Cleanup
annagrin 98c19af
Merge with main
annagrin edf0224
Cleanup
annagrin 6b5161a
Fix failing test
annagrin 054f4b7
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin 4df4390
Fix failing doc build
annagrin a5d63c5
Address CR comments
annagrin eba1c1d
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into qu…
annagrin 4a12db0
Address more CR comments and add a test
annagrin cc1faea
Address more CR comments
annagrin 395acbe
Merge with main
annagrin 175a70d
Fix links
annagrin e1449de
Fix links
annagrin 25d18db
Merge with main
annagrin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.