kernel.apply_call raises AttributeError for a value-returning @cudaq.kernel
Maintainers usually reply within 1 day
Nobody has claimed this yet.
- #5165 by @udsy19 — closed without merging
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 45/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Quiet
- Domain
- compilers, testing-qa
Research direction
Start in python/cudaq/kernel/kernel_builder.py at resolve_callable_arg and __applyControlOrAdjoint, then inspect CC call verification in lib/Optimizer/Dialect/CC/CCOps.cpp. Run the regression coverage in python/tests/builder/test_kernel_builder.py with a value-returning @cudaq.kernel passed to apply_call. Done means the call emits valid IR, the module verifies, and apply_call continues to return None.
Written by the indexing model from the issue text.
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
Passing a @cudaq.kernel that returns a value to kernel_builder's apply_call always fails with an AttributeError from inside the builder. Kernels that return nothing work fine, so the failure depends only on whether the callee has a return type.
The closure that wraps the decorator kernel is built in PyKernel.resolve_callable_arg. In the branch that handles a callee with a result it takes .result off the func.CallOp and then asks that OpResult for .results:
# python/cudaq/kernel/kernel_builder.py:1481-1483 (on main @ 228f099a0)
if funcTy.results:
call = func.CallOp(fn, vs).result
cc.ReturnOp(call.results)
func.CallOp(...).result is an OpResult, which has no .results, so the line is unreachable-in-practice code that was never exercised — there is no test in python/tests where a builder apply_calls a value-returning decorator kernel.
There is a second defect hiding behind the first, which is why this is not a one-token fix. Once the closure genuinely returns a value, the lambda's !cc.callable signature has a result, but __applyControlOrAdjoint hardcodes an empty result list on the call:
# python/cudaq/kernel/kernel_builder.py:729
cc.CallCallableOp([], otherFuncCloned, mlirValues)
cudaq::cc::CallCallableOp::verify() (lib/Optimizer/Dialect/CC/CCOps.cpp:2544-2551) checks coarity, so fixing only the AttributeError swaps the crash for an invalid module (see below). Both spots need the fix.
Steps to reproduce the bug
import cudaq
@cudaq.kernel
def flipAndMeasure(q: cudaq.qubit) -> bool:
x(q)
return mz(q)
flipAndMeasure.compile()
kernel = cudaq.make_kernel()
q = kernel.qalloc()
kernel.apply_call(flipAndMeasure, q)
kernel.mz(q)
print(cudaq.sample(kernel))
Output on CUDA-Q 0.15.1 (macOS arm64, cuda-quantum-cu13 wheel):
Traceback (most recent call last):
File "repro.py", line 14, in <module>
kernel.apply_call(flipAndMeasure, q)
File ".../cudaq/kernel/kernel_builder.py", line 1428, in apply_call
target = self.resolve_callable_arg(self.insertPoint,
DecoratorCapture(target))
File ".../cudaq/kernel/kernel_builder.py", line 1481, in resolve_callable_arg
cc.ReturnOp(call.results)
^^^^^^^^^^^^
AttributeError: 'cudaq.mlir._mlir_libs._mlir.ir.OpResult' object has no attribute 'results'
Dropping the return annotation (def flipAndMeasure(q: cudaq.qubit):) makes the same program work, which isolates the trigger to the callee's return type.
For the second defect: changing only func.CallOp(fn, vs).result to func.CallOp(fn, vs) gets past the AttributeError and then produces a module that does not verify:
error: 'cc.call_callable' op call has incorrect coarity
RuntimeError: pass pipeline failed
Expected behavior
kernel.apply_call(valueReturningKernel, ...) should apply the callee just like any other kernel. The callee's return value is not observable through apply_call (it returns None, and the plain func.call path for builder-to-builder calls already discards the callee result the same way), but the call itself must be emitted and the module must verify.
A related API question for the maintainers, which I have deliberately left out of the fix: should apply_call return a QuakeValue for the callee's result so the builder can consume it? That would be a new API surface rather than a bug fix, so the fix below keeps apply_call returning None and simply stops the crash / invalid IR. Happy to follow up if that is wanted.
Is this a regression? If it is, put the last known working version (or commit) here.
Not a regression as far as I can tell. apply_call learned to accept @cudaq.kernel decorators in #3667 / #3693, and this branch of the closure builder appears never to have worked.
Environment
- CUDA-Q version: 0.15.1 (
cuda-quantum-cu13wheel, commitaca5853); code inspected at228f099a0onmain - Python version: 3.13
- C++ compiler: n/a (pip wheel)
- Operating system: macOS 26.5.1 (arm64)
Suggestions
Two changes in python/cudaq/kernel/kernel_builder.py:
-
In
resolve_callable_arg, keep the op and forward its results:call = func.CallOp(fn, vs) cc.ReturnOp(call.results) -
In
__applyControlOrAdjoint, carry the callable's result types over to the call socc.call_callableverifies:lambdaTy = FunctionType(TypeAttr(target.attributes['function_type']).value) otherFTy = lambdaTy.inputs otherResTy = lambdaTy.results ... cc.CallCallableOp(otherResTy, otherFuncCloned, mlirValues)
Only apply_call reaches the cc.create_lambda / cc.call_callable path — adjoint and control pass their target straight to __cloneOrGetFunction — so this does not affect those.
I have a PR ready with these two changes plus a regression test in
python/tests/builder/test_kernel_builder.py.
- Dominant language
- C++
- Stars
- 1.2k
- Forks
- 508
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 100
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from NVIDIA/cuda-quantum
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
NVIDIA/cuda-quantum#6035 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 1-3 hours Newbie friendliness 77/100
NVIDIA/cuda-quantum#6034 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
NVIDIA/cuda-quantum#6033 ·
Maintainers usually reply within 1 day
-
Point the CUDA-Q Pulse research preview docs to the released cudaq-pulse packagePossibly taken @fedonman claimed this 2 days ago. Open
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
NVIDIA/cuda-quantum#6031 ·
Maintainers usually reply within 1 day
-
Realtime fnv1a_hash sign-extends char, so a non-ASCII handler name gets a platform-dependent function idPossibly taken @fedonman claimed this 2 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
NVIDIA/cuda-quantum#6029 ·
Maintainers usually reply within 1 day
All issues in NVIDIA/cuda-quantum
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
Maintainers usually reply within 2 days
-
bug data-types
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
ClickHouse/clickhouse-cpp#645 ·
Maintainers usually reply within 1 day
-
bug compatibility difficulty:easy prio:normal
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
NatronGitHub/openfx-misc#524 ·
Maintainers usually reply within 1 day
-
documentation enhancement help wanted P0
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Awesome-Embedded-Learning-Studio/Tutorial_AwesomeModernCPP#359 ·
Maintainers usually reply within 1 day
-
Type: Bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
openscad/openscad#7360 · 2 comments ·
Maintainers usually reply within 1 day