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

Add C++ kernel_builder::qalloc(state&) #1722

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 16 additions & 0 deletions runtime/cudaq/builder/kernel_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,22 @@ QuakeValue qalloc(ImplicitLocOpBuilder &builder,
return QuakeValue(builder, qubits);
}

QuakeValue qalloc(mlir::ImplicitLocOpBuilder &builder, cudaq::state *state,
StateVectorStorage &stateVectorStorage) {
auto *context = builder.getContext();
auto statePtrTy = cudaq::cc::PointerType::get(cc::StateType::get(context));
auto statePtr = builder.create<cc::CastOp>(
1tnguyen marked this conversation as resolved.
Show resolved Hide resolved
builder.getLoc(), statePtrTy,
builder.create<arith::ConstantIntOp>(
reinterpret_cast<std::intptr_t>(state), 64));
// Add the initialize state op
Value qubits = builder.create<quake::AllocaOp>(
quake::VeqType::get(context, state->get_num_qubits()));
qubits = builder.create<quake::InitializeStateOp>(qubits.getType(), qubits,
statePtr);
return QuakeValue(builder, qubits);
}

QuakeValue constantVal(ImplicitLocOpBuilder &builder, double val) {
llvm::APFloat d(val);
Value constant =
Expand Down
10 changes: 10 additions & 0 deletions runtime/cudaq/builder/kernel_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ QuakeValue qalloc(mlir::ImplicitLocOpBuilder &builder,
StateVectorStorage &stateVectorData,
StateVectorVariant &&state, simulation_precision precision);

/// @brief Allocate a `qvector` from a user provided state.
QuakeValue qalloc(mlir::ImplicitLocOpBuilder &builder, cudaq::state *state,
StateVectorStorage &stateVectorData);

/// @brief Create a QuakeValue representing a constant floating-point number
QuakeValue constantVal(mlir::ImplicitLocOpBuilder &builder, double val);

Expand Down Expand Up @@ -478,6 +482,12 @@ class kernel_builder : public details::kernel_builder_base {
simulation_precision::fp32);
}

// Overload for `cudaq::state`
QuakeValue qalloc(const cudaq::state &state) {
return details::qalloc(*opBuilder.get(), const_cast<cudaq::state *>(&state),
stateVectorStorage);
}

/// @brief Return a `QuakeValue` representing the constant floating-point
/// value.
QuakeValue constantVal(double val) {
Expand Down
20 changes: 20 additions & 0 deletions unittests/integration/builder_tester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,26 @@ TEST(BuilderTester, checkFromStateVector) {
EXPECT_EQ(counts.size(), 1);
EXPECT_EQ(counts.count("110"), 1000);
}

{
// qalloc with state
auto kernel = cudaq::make_kernel();
auto qubits = kernel.qalloc(2);
kernel.h(qubits[0]);
auto state = cudaq::get_state(kernel);
// Send on the state to the next kernel
auto anotherOne = cudaq::make_kernel();
auto newQubits = anotherOne.qalloc(state);
anotherOne.x<cudaq::ctrl>(newQubits[0], newQubits[1]);
std::cout << anotherOne << "\n";
auto counts = cudaq::sample(anotherOne);
std::size_t counter = 0;
for (auto &[k, v] : counts) {
counter += v;
EXPECT_TRUE(k == "00" || k == "11");
}
EXPECT_EQ(counter, 1000);
}
}

CUDAQ_TEST(BuilderTester, checkCanProgressivelyBuild) {
Expand Down
Loading