NVIDIA/cuda-quantum

Calling kernel functions with list parameters gives incorrect results

annagrin opened this issue · 1 comments

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

Executing the following code

def test_call():

    cudaq.reset_target()

    @cudaq.kernel(verbose=True)
    def test_param(i: int, v1: list[int]) -> int: 
        return i

    l = [42]
    print(test_param(0, l))
    print(test_param(1, l))
    print(test_param(2, l))
    print(test_param(3, l))

test_call()

Gives incorrect results:

[Visit Return] = return i]
[Visit Name i]
push Value(%1 = "cc.load"(%0) : (!cc.ptr<i64>) -> i64)
pop Value(%1 = "cc.load"(%0) : (!cc.ptr<i64>) -> i64)
module attributes {quake.mangled_name_map = {__nvqpp__mlirgen__test_param = "__nvqpp__mlirgen__test_param_PyKernelEntryPointRewrite"}} {
  func.func @__nvqpp__mlirgen__test_param(%arg0: i64, %arg1: !cc.stdvec<i64>) -> i64 attributes {"cudaq-entrypoint"} {
    %0 = cc.alloca i64
    cc.store %arg0, %0 : !cc.ptr<i64>
    %1 = cc.load %0 : !cc.ptr<i64>
    return %1 : i64
  }
}

42
42
42
42

Steps to reproduce the bug

def test_call():

    cudaq.reset_target()

    @cudaq.kernel(verbose=True)
    def test_param(i: int, v1: list[int]) -> int: 
        return i

    l = [42]
    print(test_param(0, l))
    print(test_param(1, l))
    print(test_param(2, l))
    print(test_param(3, l))

test_call() 

Expected behavior

0
1
2
3

Is this a regression? If it is, put the last known working version (or commit) here.

Not a regression

Environment

  • CUDA Quantum version:
  • Python version:
  • C++ compiler:
  • Operating system:

Suggestions

No response

Interesting. Seems like a bug in how we are extracting the return value from the rawArgs memory pointer at py_alt_launch_kernel.cpp:274. @schweitzpgi maybe you could double check me there on the arithmetic? I think in this case I'm indexing into the stdvec appended data.