Open zohimchandani opened 7 months ago
This is expected behavior. You passed a matrix of parameters, which means you are trying to broadcast observe across all rows of the matrix. But you did not provide a list of qubit_count
.
If you don't provide a list of qubit_count
we can't know if you really intended for the broadcast or not. The user may have just accidentally passed a matrix to a vector arg. We can't really know unless you provide an argument set for all arguments.
I recall us having this conversation before.
Changing qubit_count = 1
to qubit_count = [1]
makes it work.
import cudaq
from cudaq import spin
import numpy as np
n_samples = 1
n_params = 2
h = spin.z(0)
qubit_count = [1]
params = np.random.rand(n_samples, n_params)
@cudaq.kernel
def kernel(qubit_count: int, params: np.ndarray):
qvector = cudaq.qvector(qubit_count)
rx(params[0], qvector[0])
ry(params[1], qvector[0])
result = cudaq.observe(kernel, h, qubit_count, params)
Strictly speaking, the type of qubit_count
is a list
but the kernel sees individual elements hence why int
works - not sure if this is an error we want to track?
Moreover, if I want to run a 1q circuit for 1000 different samples, I have to create a list of 1000 1's for qubit_count
which is not natural behaviour from a user perspective:
n_samples = 1000
qubit_count = [1 for i in range(n_samples)]
What we should enable is for something like this to function:
n_samples = 1000
qubit_count = 1
Thanks :)
Good topic for you to bring up at a language spec meeting.
Can discuss more at the meeting but noting it here for reference:
import cudaq
from cudaq import spin
import torch
import numpy as np
hamiltonian = spin.z(0)
n_samples = 3
n_params = 2
params = torch.rand(n_samples, n_params)
qubit_count = [1 for i in range(params.shape[0])]
@cudaq.kernel
def kernel(qubit_count: int, params: np.ndarray):
qvector = cudaq.qvector(qubit_count)
h(qvector)
ry(params[0], qvector[0])
rx(params[1], qvector[0])
result = cudaq.observe(kernel, hamiltonian, qubit_count, params)
The above works when qubit_count
is a list[int]
however the supplied type to the kernel is a int
.
Moreover, params
is a torch.Tensor
but the supplied type to the kernel is a np.ndarray
Required prerequisites
Describe the bug
When we have an
int
as an input to the kernel, it seems to work fine as shown below:If we input a
np.ndarray
, this also works fine:However, 2 inputs seems to throw an error:
Steps to reproduce the bug
NA
Expected behavior
NA
Is this a regression? If it is, put the last known working version (or commit) here.
Not a regression
Environment
Suggestions
No response