Open bmhowe23 opened 1 week ago
There already is a table to lookup all kernel functions in the executable and one can get the names of all of them. Using pointer values doesn't really make much sense though, since the location of the function depends on whether you mean the host (which isn't too helpful) or device side and, if the device side, which instance of the function to use.
The cudaq::qkernel
feature should be used as it does allow a kernel's name to be "passed" and determined by functions such as cudaq::sample
.
Another consideration here relates to "which instance" above but could be addressed by the JIT compiler keeping a log of JITted kernels and the metadata information—which is done quite late in, e.g., the QIR codegen path, both AOT and JIT. This would allow an association between the JITted kernel and its metadata. That is, JITted kernel which may include slices of other kernels and which itself may be an address on the heap or a string set to a remote or not-sure-what.
I just realized I copied the baseline qir_simple_cond-1.cpp into the bug report above rather than the intended function-version of the bug. I just edited the original post to reflect that.
Required prerequisites
Describe the bug
Adaptive quantum kernels written as C++ functions or lambdas with
__qpu__
attributes do not work correctly because internal logic in the CUDA-Q runtime cannot lookup the name of C++ functions or lambdas at runtime.Steps to reproduce the bug
The following example (based off of targettests/execution/qir_simple_cond-1.cpp from the existing CUDA-Q tree, but slightly modified to use functions instead of class operators) reproduces the problem.
Save the above file to
test-if.cpp
; then compile and run withnvq++
:The easiest way to see the underlying root cause for the bug is by running with
CUDAQ_LOG_LEVEL=info
. This is a circuit that requires mid-circuit measurements, so the simulator should run the circuitnShots
times, but as you can see from the logs, it only runs it once and generates the shots from the remaining state vector. This is wrong.The root cause of the underlying bug is that
cudaq::sample()
(and other CUDA-Q algorithms) cannot convert theQuantumKernel
being passed to it into a valid string name. This causescudaq::kernelHasConditionalFeedback()
to returnfalse
, even though it should returntrue
in this case.Throwing in some additional keywords for GitHub issue searchs:
context.hasConditionalsOnMeasureResults
is not correctqubitMeasurementFeedback
attribute not being found despiteadd-metadata
pass setting it correctly.Expected behavior
The above example should work the exact same way as it does in targettests/execution/qir_simple_cond-1.cpp.
Is this a regression? If it is, put the last known working version (or commit) here.
Not a regression
Environment
Suggestions
There are at least two possible solutions to this problem:
nvq++
to use-rdynamic
to add function/string lookup tables into the executables so that we can properly retrieve the name of all quantum kernels. (Thanks, @1tnguyen.)nvq++
compiler to inject code where it saves the name of__qpu__
functions into a map that can be indexed by function pointers at runtime. This will not work for library mode, but if we are considering removing support for that at some point, that may not be an issue.