Kernel accepts np.array as an arg only if preceded by another arg
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
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.
Good catch, it does work with np.ndarray indeed! Thank you.
Still perplexed as to why it won't raise an error with np.array when there is an arg preceding it.
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.
I was just testing. It's probably not recommended, but doesn't give exp wrt Z?
I was just testing. It's probably not recommended, but doesn't give exp wrt Z?
Would you please share an example?
Hey @sacpis what kind of example would you need?