NVIDIA / NVIDIA/cuda-quantum

Support for calling C++ functions within kernels

Open
#3,753 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stale-notified
Dominant language
C++
Stars
1.1k
Forks
455
Avg merge
1d 22h
Merged PRs (30d)
165

Description

Required prerequisites
  • Consult the security policy. If reporting a security vulnerability, do not report the bug using this form. Use the process described in the policy to report the issue.
  • Make sure you've read the documentation. Your issue may be addressed there.
  • Search the issue tracker to verify that this hasn't already been reported. +1 or comment there if it has.
  • If possible, make a PR with a failing test to give us a starting point to work on!
Describe the bug

Is calling a "normal" C++ function (say foo) within a kernel a supported operation? I'm not sure what the semantics of that would be... I'm asking because the following:

#include <cudaq.h>

#include <vector>

std::vector<int> foo() { return {0, 1}; }

void kernel() __qpu__ { foo(); }

int main() { cudaq::sample(kernel); }

compiles, but segfaults at runtime. It works fine if the returned value is on the stack (e.g. int, or std::pair).

Interestingly, this pattern (i.e. calling a vector-returning C++ function from within a kernel) is used in our tests in the file unittests/integration/ghz_nisq_tester.cpp, and it works just fine. In fact, the above example runs just fine as a test in our ctest suite...

Steps to reproduce the bug

put the above code in test.cpp. Then run nvq++ test.cpp && ./a.out.

Expected behavior

unclear: either a compilation error or a successful run; not a segfault

Is this a regression? If it is, put the last known working version (or commit) here.

Not a regression

Environment
  • CUDA-Q version: main
  • Python version: n/a
  • C++ compiler: clang-16
  • Operating system:
Suggestions

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in test.cpp and run nvq++ test.cpp && ./a.out to confirm the runtime segfault. Compare it with unittests/integration/ghz_nisq_tester.cpp, where a vector-returning C++ function reportedly works in the ctest suite. Determine whether the supported result should be a compilation error or a successful run, and add coverage for the chosen behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers, quantum-computing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.