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

[C++ bridge] Add support for std::vector::data method. #2241

Merged
merged 3 commits into from
Oct 1, 2024
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
11 changes: 11 additions & 0 deletions lib/Frontend/nvqpp/ConvertExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,17 @@ bool QuakeBridgeVisitor::VisitCallExpr(clang::CallExpr *x) {
return pushValue(builder.create<cc::ComputePtrOp>(
loc, elePtrTy, vecPtr, ValueRange{negativeOneIndex}));
}
if (funcName.equals("data"))
if (auto memberCall = dyn_cast<clang::CXXMemberCallExpr>(x))
if (memberCall->getImplicitObjectArgument()) {
[[maybe_unused]] auto calleeTy = popType();
assert(isa<FunctionType>(calleeTy));
// data() returns a pointer to a sequence of elements.
auto eleTy = cast<cc::SpanLikeType>(svec.getType()).getElementType();
auto eleArrTy = cc::PointerType::get(cc::ArrayType::get(eleTy));
return pushValue(
builder.create<cc::StdvecDataOp>(loc, eleArrTy, svec));
}

TODO_loc(loc, "unhandled std::vector member function, " + funcName);
}
Expand Down
18 changes: 18 additions & 0 deletions test/AST-Quake/vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ struct simple_float_rotation {
}
};

struct difficult_symphony {
auto operator()(std::vector<float> theta) __qpu__ {
float *firstData = theta.data();
cudaq::qvector q(1);
rx(firstData[0], q[0]);
mz(q);
}
};

// clang-format off
// CHECK-LABEL: func.func @__nvqpp__mlirgen__difficult_symphony(
// CHECK-SAME: %[[VAL_0:.*]]: !cc.stdvec<f32>{{.*}}) attributes {"cudaq-entrypoint", "cudaq-kernel"} {
// CHECK: %[[VAL_1:.*]] = cc.stdvec_data %[[VAL_0]] : (!cc.stdvec<f32>) -> !cc.ptr<!cc.array<f32 x ?>>
// clang-format on

int main() {
std::vector<double> vec_args = {0.63};

Expand All @@ -63,6 +78,9 @@ int main() {
printf("Observed: %s, %lu\n", bits.c_str(), count);
}

auto bob_counts = cudaq::sample(difficult_symphony{}, float_args);
bob_counts.dump();

// can get <ZZ...Z> from counts too
printf("Exp: %lf\n", float_counts.expectation());
return 0;
Expand Down
Loading