Skip to content

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

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

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

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.

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