diff --git a/runtime/cudaq/qis/qubit_qis.h b/runtime/cudaq/qis/qubit_qis.h index 5e09625d1c..097695ca57 100644 --- a/runtime/cudaq/qis/qubit_qis.h +++ b/runtime/cudaq/qis/qubit_qis.h @@ -792,6 +792,42 @@ std::vector mz(QubitRange &q) { return b; } +// Measure all qubits in the range, return vector of 0,1 +#if CUDAQ_USE_STD20 +template + requires std::ranges::range +#else +template < + typename QubitRange, + typename = std::enable_if_t>, cudaq::qubit>>> +#endif +std::vector my(QubitRange &q) { + std::vector 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 + requires std::ranges::range +#else +template < + typename QubitRange, + typename = std::enable_if_t>, cudaq::qubit>>> +#endif +std::vector mx(QubitRange &q) { + std::vector b; + for (auto &qq : q) { + b.push_back(mx(qq)); + } + return b; +} + template std::vector mz(qubit &q, Qs &&...qs); diff --git a/test/AST-Quake/mx_qarray.cpp b/test/AST-Quake/mx_qarray.cpp new file mode 100644 index 0000000000..003606ff97 --- /dev/null +++ b/test/AST-Quake/mx_qarray.cpp @@ -0,0 +1,38 @@ +// RUN: cudaq-quake %s | FileCheck %s + +#include + +template +struct ghz { + auto operator()() __qpu__ { + + // Compile-time sized array like std::array + cudaq::qarray q; + h(q[0]); + for (int i = 0; i < N - 1; i++) { + x(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 \ No newline at end of file diff --git a/test/AST-Quake/my_qarray.cpp b/test/AST-Quake/my_qarray.cpp new file mode 100644 index 0000000000..c90fd61874 --- /dev/null +++ b/test/AST-Quake/my_qarray.cpp @@ -0,0 +1,38 @@ +// RUN: cudaq-quake %s | FileCheck %s + +#include + +template +struct ghz { + auto operator()() __qpu__ { + + // Compile-time sized array like std::array + cudaq::qarray q; + h(q[0]); + for (int i = 0; i < N - 1; i++) { + x(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 \ No newline at end of file