Required prerequisites
Describe the bug
Several data shapes that marshal correctly as annotated kernel
arguments fail when captured from the enclosing scope. This breaks
the kernel-factory idiom (a host function that prepares data and returns a
@cudaq.kernel closure over it), which library code relies on to hide
argument plumbing from users. Three cases, plus one launcher inconsistency
found while verifying them:
(a) Captured empty list — "Cannot infer runtime argument type":
import cudaq
cudaq.set_target("qpp-cpu")
@cudaq.kernel
def with_arg(values: list[int]):
q = cudaq.qvector(1)
if len(values) > 0:
x(q[0])
cudaq.sample(with_arg, []) # OK
captured = []
@cudaq.kernel
def with_capture():
q = cudaq.qvector(1)
if len(captured) > 0:
x(q[0])
cudaq.sample(with_capture) # RuntimeError: Cannot infer runtime argument type
(b) Captured list of plain strings (Pauli words) — strings auto-convert
when passed as list[cudaq.pauli_word] arguments, but the same list
captured fails with "Cannot handle conversion of python type <class 'str'>
to MLIR type" (workaround: convert to cudaq.pauli_word before capture):
@cudaq.kernel
def arg_form(words: list[cudaq.pauli_word]):
q = cudaq.qvector(2)
for i in range(len(words)):
exp_pauli(0.1, q, words[i])
cudaq.sample(arg_form, ["XI", "ZZ"]) # OK
words = ["XI", "ZZ"]
@cudaq.kernel
def capture_form():
q = cudaq.qvector(2)
for i in range(len(words)):
exp_pauli(0.1, q, words[i])
cudaq.sample(capture_form) # error: Cannot handle conversion of python type <class 'str'> to MLIR type.
(c) Captured nested lists — list[list[float]] /
list[list[cudaq.pauli_word]] work as annotated arguments (via
get_state) but fail as captures with "getElementType(): incompatible
function arguments".
(d) Launcher inconsistency for nested-list arguments: the same
nested-list-argument kernel succeeds via cudaq.get_state but fails via
cudaq.sample with "Invalid runtime argument type", so argument validation
differs per launch path:
@cudaq.kernel
def nested(ws: list[list[cudaq.pauli_word]], cs: list[list[float]]):
q = cudaq.qvector(2)
for i in range(len(ws)):
g = ws[i]
gc = cs[i]
for j in range(len(g)):
exp_pauli(gc[j], q, g[j])
nw = [[cudaq.pauli_word("XY"), cudaq.pauli_word("YX")], [cudaq.pauli_word("ZI")]]
nc = [[0.5, -0.5], [1.0]]
cudaq.get_state(nested, nw, nc) # OK
cudaq.sample(nested, nw, nc) # error: Invalid runtime argument type
Steps to reproduce the bug
See the four self-contained snippets above; each was executed against the
environment below.
Expected behavior
Captured values lower like the equivalent annotated arguments (or the
diagnostics name the offending captured variable), and sample accepts
whatever get_state accepts.
Is this a regression? If it is, put the last known working version (or commit) here.
Not a regression
Environment
- CUDA-Q built from source at commit
0be565550f4c23affdcbed9e4eaec38d2d0915e6
- Python 3.11.x, target
qpp-cpu (library mode)
- AlmaLinux 8.10, x86_64
All reproducers below were executed and verified against this build.
Required prerequisites
Describe the bug
Several data shapes that marshal correctly as annotated kernel
arguments fail when captured from the enclosing scope. This breaks
the kernel-factory idiom (a host function that prepares data and returns a
@cudaq.kernelclosure over it), which library code relies on to hideargument plumbing from users. Three cases, plus one launcher inconsistency
found while verifying them:
(a) Captured empty list — "Cannot infer runtime argument type":
(b) Captured list of plain strings (Pauli words) — strings auto-convert
when passed as
list[cudaq.pauli_word]arguments, but the same listcaptured fails with "Cannot handle conversion of python type <class 'str'>
to MLIR type" (workaround: convert to
cudaq.pauli_wordbefore capture):(c) Captured nested lists —
list[list[float]]/list[list[cudaq.pauli_word]]work as annotated arguments (viaget_state) but fail as captures with "getElementType(): incompatiblefunction arguments".
(d) Launcher inconsistency for nested-list arguments: the same
nested-list-argument kernel succeeds via
cudaq.get_statebut fails viacudaq.samplewith "Invalid runtime argument type", so argument validationdiffers per launch path:
Steps to reproduce the bug
See the four self-contained snippets above; each was executed against the
environment below.
Expected behavior
Captured values lower like the equivalent annotated arguments (or the
diagnostics name the offending captured variable), and
sampleacceptswhatever
get_stateaccepts.Is this a regression? If it is, put the last known working version (or commit) here.
Not a regression
Environment
0be565550f4c23affdcbed9e4eaec38d2d0915e6qpp-cpu(library mode)All reproducers below were executed and verified against this build.