Skip to content

Commit

Permalink
Renaming qkernel_ref to qkernel per the spec. (#2253)
Browse files Browse the repository at this point in the history
* Remove anyon target test. It's nondeterministic. See #2249.

* Rename qkernel_ref to qkernel as per the latest spec.
  • Loading branch information
schweitzpgi authored Oct 4, 2024
1 parent 704ecb6 commit 529d1af
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 50 deletions.
11 changes: 5 additions & 6 deletions include/cudaq/Optimizer/Dialect/CC/CCTypes.td
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,16 @@ def cc_CallableType : CCType<"Callable", "callable"> {
}

def cc_IndirectCallableType : CCType<"IndirectCallable", "indirect_callable"> {
let summary = "Proxy for cudaq::qkernel_ref.";
let summary = "Proxy for cudaq::qkernel.";
let description = [{
An entry-point kernel may take a reference to another kernel as an argument.
The passed kernel may be entirely opaque at compile-time with its definition
present in some other compilation module.

It is on the programmer to use the cudaq::qkernel_ref type. This wrapper
class is very much like std::function, but it extends that functionality
with some extra information for the runtime to be able to "link" the
distinct kernels on the device side and provide, for example, LTO at
JIT compile time.
It is on the programmer to use the cudaq::qkernel type. This wrapper class
is very much like std::function, but it extends that functionality with some
extra information for the runtime to be able to "link" the distinct kernels
on the device side and provide, for example, LTO at JIT compile time.
}];

let parameters = (ins "mlir::FunctionType":$signature);
Expand Down
2 changes: 1 addition & 1 deletion lib/Frontend/nvqpp/ConvertDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ bool QuakeBridgeVisitor::interceptRecordDecl(clang::RecordDecl *x) {
return pushType(cc::StateType::get(ctx));
if (name.equals("pauli_word"))
return pushType(cc::CharspanType::get(ctx));
if (name.equals("qkernel_ref")) {
if (name.equals("qkernel")) {
auto *cts = cast<clang::ClassTemplateSpecializationDecl>(x);
// Traverse template argument 0 to get the function's signature.
if (!TraverseType(cts->getTemplateArgs()[0].getAsType()))
Expand Down
4 changes: 2 additions & 2 deletions runtime/cudaq/cudaq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "cuda_runtime_api.h"
#endif
#include "cudaq/platform.h"
#include "cudaq/qis/qkernel_ref.h"
#include "cudaq/qis/qkernel.h"
#include "cudaq/utils/registry.h"
#include "distributed/mpi_plugin.h"
#include <dlfcn.h>
Expand Down Expand Up @@ -253,7 +253,7 @@ void cudaq::registry::__cudaq_registerLinkableKernel(void *hostSideFunc,
std::intptr_t cudaq::registry::__cudaq_getLinkableKernelKey(void *p) {
if (!p)
throw std::runtime_error("cannot get kernel key, nullptr");
const auto &qk = *reinterpret_cast<const cudaq::qkernel_ref<void()> *>(p);
const auto &qk = *reinterpret_cast<const cudaq::qkernel<void()> *>(p);
return reinterpret_cast<std::intptr_t>(*qk.get_entry_kernel_from_holder());
}

Expand Down
38 changes: 19 additions & 19 deletions runtime/cudaq/qis/qkernel_ref.h → runtime/cudaq/qis/qkernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
namespace cudaq {

/// In library mode, the quake compiler is not involved. To streamline things,
/// just have qkernel_ref alias the std::function template class.
/// just have `qkernel` alias the std::function template class.
template <typename Sig>
using qkernel_ref = std::function<Sig>;
using qkernel = std::function<Sig>;

} // namespace cudaq

Expand Down Expand Up @@ -100,7 +100,7 @@ template <typename A>
using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<A>>;
#endif

/// A qkernel_ref<> must be used to wrap `CUDA-Q` kernels (callables annotated
/// A `qkernel` must be used to wrap `CUDA-Q` kernels (callables annotated
/// with the `__qpu__` attribute) when those kernels are \e referenced other
/// than by a direct call in code outside of quantum kernels proper. Supports
/// free functions, classes with call operators, and lambdas.
Expand All @@ -110,18 +110,18 @@ using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<A>>;
/// either stitch together execution in a simulation environment and/or JIT
/// compile and re-link these kernels into a cohesive quantum circuit.
template <typename>
class qkernel_ref;
class qkernel;

template <typename R, typename... As>
class qkernel_ref<R(As...)> {
class qkernel<R(As...)> {
public:
qkernel_ref() {}
qkernel_ref(std::nullptr_t) {}
qkernel_ref(const qkernel_ref &) = default;
qkernel_ref(qkernel_ref &&) = default;
qkernel() {}
qkernel(std::nullptr_t) {}
qkernel(const qkernel &) = default;
qkernel(qkernel &&) = default;

template <typename FUNC,
bool SELF = std::is_same_v<remove_cvref_t<FUNC>, qkernel_ref>>
bool SELF = std::is_same_v<remove_cvref_t<FUNC>, qkernel>>
using DecayType = typename std::enable_if_t<!SELF, std::decay<FUNC>>::type;

template <typename FUNC, typename DFUNC = DecayType<FUNC>,
Expand All @@ -131,7 +131,7 @@ class qkernel_ref<R(As...)> {
struct CallableType : RES {};

template <typename S, typename = std::enable_if_t<CallableType<S>::value>>
qkernel_ref(S &&f) {
qkernel(S &&f) {
using PS = remove_cvref_t<S>;
if constexpr (std::is_same_v<PS, R (*)(As...)> ||
std::is_same_v<PS, R(As...)>) {
Expand Down Expand Up @@ -162,31 +162,31 @@ class qkernel_ref<R(As...)> {
// Deduction guides for C++20.

template <typename R, typename... As>
qkernel_ref(R (*)(As...)) -> qkernel_ref<R(As...)>;
qkernel(R (*)(As...)) -> qkernel<R(As...)>;

template <typename>
struct qkernel_ref_deduction_guide_helper {};
struct qkernel_deduction_guide_helper {};

template <typename R, typename P, typename... As>
struct qkernel_ref_deduction_guide_helper<R (P::*)(As...)> {
struct qkernel_deduction_guide_helper<R (P::*)(As...)> {
using type = R(As...);
};
template <typename R, typename P, typename... As>
struct qkernel_ref_deduction_guide_helper<R (P::*)(As...) const> {
struct qkernel_deduction_guide_helper<R (P::*)(As...) const> {
using type = R(As...);
};
template <typename R, typename P, typename... As>
struct qkernel_ref_deduction_guide_helper<R (P::*)(As...) &> {
struct qkernel_deduction_guide_helper<R (P::*)(As...) &> {
using type = R(As...);
};
template <typename R, typename P, typename... As>
struct qkernel_ref_deduction_guide_helper<R (P::*)(As...) const &> {
struct qkernel_deduction_guide_helper<R (P::*)(As...) const &> {
using type = R(As...);
};

template <typename F, typename S = typename qkernel_ref_deduction_guide_helper<
template <typename F, typename S = typename qkernel_deduction_guide_helper<
decltype(&F::operator())>::type>
qkernel_ref(F) -> qkernel_ref<S>;
qkernel(F) -> qkernel<S>;

#endif // CUDAQ_USE_STD20

Expand Down
2 changes: 1 addition & 1 deletion runtime/cudaq/qis/qubit_qis.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "cudaq/qis/modifiers.h"
#include "cudaq/qis/pauli_word.h"
#include "cudaq/qis/qarray.h"
#include "cudaq/qis/qkernel_ref.h"
#include "cudaq/qis/qkernel.h"
#include "cudaq/qis/qreg.h"
#include "cudaq/qis/qvector.h"
#include "cudaq/spin_op.h"
Expand Down
2 changes: 1 addition & 1 deletion runtime/cudaq/utils/registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void cudaqRegisterLambdaName(const char *, const char *);
/// Register a kernel with the runtime for kernel runtime stitching.
void __cudaq_registerLinkableKernel(void *, const char *, void *);

/// Return the kernel key from a qkernel_ref object. If \p p is a `nullptr` this
/// Return the kernel key from a `qkernel` object. If \p p is a `nullptr` this
/// will throw a runtime error.
std::intptr_t __cudaq_getLinkableKernelKey(void *p);

Expand Down
3 changes: 1 addition & 2 deletions targettests/SeparateCompilation/basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ __qpu__ void dunkadee(cudaq::qvector<> &q) {
#include "baselib.h"
#include <iostream>

__qpu__ void
userKernel(const cudaq::qkernel_ref<void(cudaq::qvector<> &)> &init) {
__qpu__ void userKernel(const cudaq::qkernel<void(cudaq::qvector<> &)> &init) {
cudaq::qvector q(2);
init(q);
}
Expand Down
3 changes: 1 addition & 2 deletions targettests/SeparateCompilation/class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ void HereIsTheThing::operator()(cudaq::qvector<> &q) __qpu__ {
#include "classlib.h"
#include <iostream>

__qpu__ void
userKernel(const cudaq::qkernel_ref<void(cudaq::qvector<> &)> &init) {
__qpu__ void userKernel(const cudaq::qkernel<void(cudaq::qvector<> &)> &init) {
cudaq::qvector q(2);
init(q);
}
Expand Down
3 changes: 1 addition & 2 deletions targettests/SeparateCompilation/deduction_guide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ __qpu__ void dunkadee(cudaq::qvector<> &q) { x(q[0]); }
#include "udedgulib.h"
#include <iostream>

__qpu__ void
userKernel(const cudaq::qkernel_ref<void(cudaq::qvector<> &)> &init) {
__qpu__ void userKernel(const cudaq::qkernel<void(cudaq::qvector<> &)> &init) {
cudaq::qvector q(2);
init(q);
}
Expand Down
5 changes: 2 additions & 3 deletions targettests/SeparateCompilation/emulate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@ __qpu__ void dunkadee(cudaq::qvector<> &q) { x(q[0]); }
#include "emulib.h"
#include <iostream>

__qpu__ void
userKernel(const cudaq::qkernel_ref<void(cudaq::qvector<> &)> &init) {
__qpu__ void userKernel(const cudaq::qkernel<void(cudaq::qvector<> &)> &init) {
cudaq::qvector q(2);
init(q);
}

int main() {
cudaq::sample(10, userKernel,
cudaq::qkernel_ref<void(cudaq::qvector<> &)>{dunkadee});
cudaq::qkernel<void(cudaq::qvector<> &)>{dunkadee});
std::cout << "Hello, World!\n";
return 0;
}
Expand Down
5 changes: 2 additions & 3 deletions targettests/SeparateCompilation/lambda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@

#include "cudaq.h"

__qpu__ void userKernel(const cudaq::qkernel_ref<void(cudaq::qvector<> &)> &);
__qpu__ void userKernel(const cudaq::qkernel<void(cudaq::qvector<> &)> &);

//--- anonlib.cpp

#include "anonlib.h"

__qpu__ void
userKernel(const cudaq::qkernel_ref<void(cudaq::qvector<> &)> &init) {
__qpu__ void userKernel(const cudaq::qkernel<void(cudaq::qvector<> &)> &init) {
cudaq::qvector q(2);
init(q);
}
Expand Down
5 changes: 2 additions & 3 deletions targettests/SeparateCompilation/multiple_callables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

#include "cudaq.h"

__qpu__ void entry(const cudaq::qkernel_ref<void(cudaq::qvector<> &)> &o,
const cudaq::qkernel_ref<void(cudaq::qvector<> &, int)> &p) {
__qpu__ void entry(const cudaq::qkernel<void(cudaq::qvector<> &)> &o,
const cudaq::qkernel<void(cudaq::qvector<> &, int)> &p) {
cudaq::qvector q(2);
o(q);
p(q, 1);
Expand All @@ -25,4 +25,3 @@ int main() {
entry(l, m);
return 0;
}

1 change: 0 additions & 1 deletion targettests/execution/cudaq_observe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

// REQUIRES: c++20
// clang-format off
// RUN: nvq++ --target anyon --emulate %s -o %t && %t | FileCheck %s
// RUN: nvq++ --target ionq --emulate %s -o %t && %t | FileCheck %s
// 2 different IQM machines for 2 different topologies
// RUN: nvq++ --target iqm --iqm-machine Adonis --emulate %s -o %t && %t | FileCheck %s
Expand Down
8 changes: 4 additions & 4 deletions test/AST-Quake/indirect_callable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@

__qpu__ int rando_qernel(double);

__qpu__ void superstar_qernel(const cudaq::qkernel_ref<int(double)>& bob, double dub) {
__qpu__ void superstar_qernel(const cudaq::qkernel<int(double)>& bob, double dub) {
auto size = bob(dub);
cudaq::qvector q(size);
mz(q);
}

void meanwhile_on_safari() {
cudaq::qkernel_ref<int(double)> tiger{rando_qernel};
cudaq::qkernel<int(double)> tiger{rando_qernel};
superstar_qernel(tiger, 11.0);
}

// clang-format off
// CHECK-LABEL: func.func @__nvqpp__mlirgen__function_superstar_qernel._Z16superstar_qernelRKN5cudaq11qkernel_refIFidEEEd(
// CHECK-LABEL: func.func @__nvqpp__mlirgen__function_superstar_qernel._Z16superstar_qernelRKN5cudaq7qkernelIFidEEEd(
// CHECK-SAME: %[[VAL_0:.*]]: !cc.indirect_callable<(f64) -> i32>,
// CHECK-SAME: %[[VAL_1:.*]]: f64) attributes {"cudaq-entrypoint", "cudaq-kernel", no_this} {
// CHECK: %[[VAL_2:.*]] = cc.alloca f64
Expand All @@ -40,7 +40,7 @@ void meanwhile_on_safari() {
// CHECK: return
// CHECK: }

// CHECK-LABEL: func.func @_Z16superstar_qernelRKN5cudaq11qkernel_refIFidEEEd(
// CHECK-LABEL: func.func @_Z16superstar_qernelRKN5cudaq7qkernelIFidEEEd(
// CHECK-SAME: %[[VAL_0:.*]]: !cc.ptr<i8>,
// CHECK-SAME: %[[VAL_1:.*]]: f64) attributes {no_this} {
// CHECK: return
Expand Down

0 comments on commit 529d1af

Please sign in to comment.