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

[WIP] [runtime] adding mx and my support for qarrays #2231

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
36 changes: 36 additions & 0 deletions runtime/cudaq/qis/qubit_qis.h
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,42 @@ std::vector<measure_result> mz(QubitRange &q) {
return b;
}

// Measure all qubits in the range, return vector of 0,1
#if CUDAQ_USE_STD20
template <typename QubitRange>
requires std::ranges::range<QubitRange>
#else
template <
typename QubitRange,
typename = std::enable_if_t<!std::is_same_v<
std::remove_reference_t<std::remove_cv_t<QubitRange>>, cudaq::qubit>>>
#endif
std::vector<measure_result> my(QubitRange &q) {
std::vector<measure_result> b;
for (auto &qq : q) {
b.push_back(my(qq));
}
return b;
}

// Measure all qubits in the range, return vector of 0,1
#if CUDAQ_USE_STD20
template <typename QubitRange>
requires std::ranges::range<QubitRange>
#else
template <
typename QubitRange,
typename = std::enable_if_t<!std::is_same_v<
std::remove_reference_t<std::remove_cv_t<QubitRange>>, cudaq::qubit>>>
#endif
std::vector<measure_result> mx(QubitRange &q) {
std::vector<measure_result> b;
for (auto &qq : q) {
b.push_back(mx(qq));
}
return b;
}

template <typename... Qs>
std::vector<measure_result> mz(qubit &q, Qs &&...qs);

Expand Down
38 changes: 38 additions & 0 deletions test/AST-Quake/mx_qarray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// RUN: cudaq-quake %s | FileCheck %s

#include <cudaq.h>

template <std::size_t N>
struct ghz {
auto operator()() __qpu__ {

// Compile-time sized array like std::array
cudaq::qarray<N> q;
h(q[0]);
for (int i = 0; i < N - 1; i++) {
x<cudaq::ctrl>(q[i], q[i + 1]);
}
mx(q);
}
};

int main() {

auto kernel = ghz<10>{};
auto counts = cudaq::sample(kernel);

if (!cudaq::mpi::is_initialized() || cudaq::mpi::rank() == 0) {
counts.dump();

// Fine grain access to the bits and counts
for (auto &[bits, count] : counts) {
printf("Observed: %s, %lu\n", bits.data(), count);
}
}

return 0;
}


// CHECK-LABEL: func.func @__nvqpp__mlirgen__ghz{{.*}}{
// CHECK: %[[MEASOUT:.*]] = quake.mx %[[VAL_0:.*]] : (!quake.veq<10>) -> !cc.stdvec<!quake.measure>
38 changes: 38 additions & 0 deletions test/AST-Quake/my_qarray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// RUN: cudaq-quake %s | FileCheck %s

#include <cudaq.h>

template <std::size_t N>
struct ghz {
auto operator()() __qpu__ {

// Compile-time sized array like std::array
cudaq::qarray<N> q;
h(q[0]);
for (int i = 0; i < N - 1; i++) {
x<cudaq::ctrl>(q[i], q[i + 1]);
}
my(q);
}
};

int main() {

auto kernel = ghz<10>{};
auto counts = cudaq::sample(kernel);

if (!cudaq::mpi::is_initialized() || cudaq::mpi::rank() == 0) {
counts.dump();

// Fine grain access to the bits and counts
for (auto &[bits, count] : counts) {
printf("Observed: %s, %lu\n", bits.data(), count);
}
}

return 0;
}


// CHECK-LABEL: func.func @__nvqpp__mlirgen__ghz{{.*}}{
// CHECK: %[[MEASOUT:.*]] = quake.my %[[VAL_0:.*]] : (!quake.veq<10>) -> !cc.stdvec<!quake.measure>
Loading