NVIDIA / cuda-quantum

C++ and Python support for the CUDA Quantum programming model for heterogeneous quantum-classical workflows
https://nvidia.github.io/cuda-quantum/
Other
566 stars 192 forks source link

Sampling and broadcasting API improvements #1486

Open zohimchandani opened 7 months ago

zohimchandani commented 7 months ago

Required prerequisites

Describe the bug

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)

When we have an int as an input to the kernel, it seems to work fine as shown below:

@cudaq.kernel
def kernel(qubit_count: int):

    qvector = cudaq.qvector(qubit_count)

    h(qvector[0])

result = cudaq.observe(kernel, h, qubit_count)

If we input a np.ndarray, this also works fine:

@cudaq.kernel
def kernel(params: np.ndarray):

    qvector = cudaq.qvector(1)

    rx(params[0], qvector[0])
    ry(params[1], qvector[0])

result = cudaq.observe(kernel, h, params)

However, 2 inputs seems to throw an error:

@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)
RuntimeError: 2D array argument provided for an observe broadcast, but argument 0 (<class 'int'>) must be a list.
print(cudaq.__version__)
CUDA Quantum Version latest (https://github.com/NVIDIA/cuda-quantum 1c434b15044e9fe1df32af4a00d662aa270aa69e)

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

amccaskey commented 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.

zohimchandani commented 7 months ago

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 :)

amccaskey commented 7 months ago

Good topic for you to bring up at a language spec meeting.

zohimchandani commented 7 months ago

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