ITADN

[python] Dataclass kernel argument with a list[int] field raises std::bad_cast when the list contains a negative value

#4846Openwsttiger 创建于 2026-07-05
W
wsttigercommented
### Required prerequisites - [x] Consult the [security policy](https://github.com/NVIDIA/cuda-quantum/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. - [x] Make sure you've read the [documentation](https://nvidia.github.io/cuda-quantum/latest). Your issue may be addressed there. - [x] Search the [issue tracker](https://github.com/NVIDIA/cuda-quantum/issues) to verify that this hasn't already been reported. +1 or comment there if it has. ### Describe the bug A `@dataclass` used as a kernel argument works when its `list[int]` fields contain only non-negative values, but launching raises `RuntimeError: std::bad_cast` as soon as any list element is negative. The same list passed as a bare `list[int]` argument works fine with negative values, so the defect is specific to the struct-member marshaling path (it behaves as if elements are cast through an unsigned type during packing). Observed matrix: | Case | Result | |---|---| | dataclass, scalar fields only | OK | | dataclass, `list[int]`/`list[float]` fields, non-negative values | OK (tested 1-5 list fields, mixed lengths) | | dataclass, any `list[int]` field containing a negative value | `std::bad_cast` | | bare `list[int]` argument containing negative values | OK | | captured dataclass with negative list element | `std::bad_cast` | ### Steps to reproduce the bug ```python from dataclasses import dataclass import cudaq cudaq.set_target("qpp-cpu") @dataclass(slots=True) class Args: values: list[int] @cudaq.kernel def kernel(args: Args): q = cudaq.qvector(1) if args.values[0] < 0: x(q[0]) cudaq.sample(kernel, Args([1])) # OK cudaq.sample(kernel, Args([-1])) # RuntimeError: std::bad_cast @cudaq.kernel def bare(values: list[int]): q = cudaq.qvector(1) if values[0] < 0: x(q[0]) cudaq.sample(bare, [-1]) # OK -> '1' ``` ### Expected behavior Negative integers in a dataclass `list[int]` field marshal identically to negative integers in a bare `list[int]` argument. ### 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. Found while developing a Python-only algorithms library on top of CUDA-Q (porting C++ device kernels to Python `@cudaq.kernel`s). ### Suggestions Impact: any struct carrying signs (e.g. the sign array of an LCU decomposition) cannot be an aggregated kernel argument; libraries must thread parallel flat lists through every kernel signature instead.
0 条评论