Skip to content

[python] Captured-variable support lags kernel-argument support (empty lists, str Pauli words, nested lists); nested-list args also differ between get_state and sample #4847

Description

@wsttiger

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.

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 listslist[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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions