NVIDIA / cuda-quantum

C++ and Python support for the CUDA Quantum programming model for heterogeneous quantum-classical workflows
https://nvidia.github.io/cuda-quantum/
Other
568 stars 192 forks source link

Sample with function pointer as input #354

Open amccaskey opened 1 year ago

amccaskey commented 1 year ago
__qpu__ void bar(cudaq::qubit& q) {
  x(q);
}

struct baz {
  __qpu__ void operator()(cudaq::qubit& q) {
    x(q);
  }
};

struct foo {
  template <typename CallableKernel>
  __qpu__ void operator()(CallableKernel &&func, int size) {
    cudaq::qreg q(size);
    func(q[0]);
    mz(q[0]);
  }
};

int main() {
  // Not supported, fails
  auto result = cudaq::sample(1000, foo{}, bar, 1);
}

This can be fixed with an update the sample.h to remove the extra std::forward at line 209.

But this leads to another bug with the argument being valid input to a cuda quantum kernel (error: kernel argument type not supported). The argument type is !cc.ptr<!cc.ptr<(!quake.ref) -> ()>>.

amccaskey commented 1 year ago

I think we may need to mandate that callable parameters to a kernel must be fully typed.

schweitzpgi commented 1 year ago

The bridge needs some work in this area, for sure. The AST can appear to be using pass by reference even though the called function is expecting a pass by value. The bridge isn't consistent about handling it; sometimes it just gets confused and fails, but in some cases it does more work and injects a cc.load, etc.