cuda-quantum icon indicating copy to clipboard operation
cuda-quantum copied to clipboard

Kernel accepts np.array as an arg only if preceded by another arg

Open poojarao8 opened this issue 4 months ago • 8 comments

Required prerequisites

  • [x] 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.
  • [x] Make sure you've read the documentation. Your issue may be addressed there.
  • [x] 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

When I pass a numpy array as an arg to a kernel, I get the following error.

RuntimeError: error: Invalid runtime argument type. Argument of type <class 'float'> was provided, but list[float] was expected.

However, this error disappears when there is another arg preceding the np.array.

Steps to reproduce the bug

The following code gives an error:

import cudaq
import numpy as np

@cudaq.kernel
def kernel( b: np.array):
    q = cudaq.qvector(2)
    x(q[0])

bvec = np.zeros(8)
result = cudaq.sample(kernel, bvec).expectation()

Error:

RuntimeError: error: Invalid runtime argument type. Argument of type <class 'float'> was provided, but list[float] was expected.

However, the following code does NOT produce an error.

import cudaq
import numpy as np

@cudaq.kernel
def kernel( np: int, b: np.array):
    q = cudaq.qvector(2)
    x(q[0])

bvec = np.zeros(8)
result = cudaq.sample(kernel, 2, bvec).expectation()

Expected behavior

Should be able to pass a np array as an arg to a kernel.

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

Not a regression

Environment

  • CUDA-Q version:
  • Python version:
  • C++ compiler:
  • Operating system:

Suggestions

No response

poojarao8 avatar Sep 30 '25 02:09 poojarao8

Thanks Pooja for reporting this. Shouldn't it be np.ndarray? It works with that. np.array is a function that serves as a constructor for creating np.ndarray objects.

sacpis avatar Sep 30 '25 03:09 sacpis

Good catch, it does work with np.ndarray indeed! Thank you.

poojarao8 avatar Sep 30 '25 03:09 poojarao8

Still perplexed as to why it won't raise an error with np.array when there is an arg preceding it.

poojarao8 avatar Sep 30 '25 03:09 poojarao8

Sure, we will look into the second case. Unrelated to this issue, just one small thing in the example .expectation() is called on observe or observe_async.

sacpis avatar Sep 30 '25 03:09 sacpis

I was just testing. It's probably not recommended, but doesn't give exp wrt Z?

poojarao8 avatar Sep 30 '25 04:09 poojarao8

I was just testing. It's probably not recommended, but doesn't give exp wrt Z?

Would you please share an example?

sacpis avatar Sep 30 '25 04:09 sacpis

Hey @sacpis what kind of example would you need?

poojarao8 avatar Sep 30 '25 18:09 poojarao8

Hey @sacpis what kind of example would you need?

No worries.

sacpis avatar Sep 30 '25 18:09 sacpis