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
458 stars 161 forks source link

Iterable data types within quantum kernels #1436

Open marwafar opened 5 months ago

marwafar commented 5 months ago

Required prerequisites

Describe the feature

If I run the example below:

import cudaq

qubits_num=4

@cudaq.kernel
def circuit():

    qubits=cudaq.qvector(qubits_num)

    s='0001'

    for i in s:
        if i=='1':
            x(qubits[i])

    mz(qubits)

result=cudaq.sample(circuit,shots_count=5000)
print(result)

I get the following error:

cudaq.kernel.ast_bridge.CompilerError: test.py:11: error: {} iterable type not supported.
     (offending source -> for i in s:
    if i == '1':
        x(qubits[i]))

There are many cases where we need to use if statement. Currently, we have to repeat the code multiple time for each if statement.

Edit from bettinaheim: We have a similar case of "not supported" when iterating over a register of qubits. Let's define more broadly what iterations to support within kernels. Re string, see also https://github.com/NVIDIA/cuda-quantum/issues/1452.

amccaskey commented 5 months ago

Just a note here - this is an error with iterating through a string, which we have not implement yet. This is not an error with if statements. If you update to s = [0, 0, 0, 1] this code should work fine.

bettinaheim commented 4 months ago

I updated this to more broadly capture iterable data types within kernels, to also include e.g. iterating over a register of qubits.