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
501 stars 182 forks source link

CUDA-Q kernels can NOT serve as input to other quantum kernels and invoked by kernel function #2282

Open ikkoham opened 1 week ago

ikkoham commented 1 week ago

Required prerequisites

Describe the bug

The example code in https://nvidia.github.io/cuda-quantum/latest/specification/cudaq/kernels.html does not work.

[10] CUDA-Q kernels can serve as input to other quantum kernels and invoked by kernel function body code. To support CUDA-Q kernel parameterization on callable quantum kernel code, programmers can leverage standard C++ template definitions or dynamic typing in language bindings such as Python:

@cudaq.kernel()
def MyStatePrep(qubits : cudaq.qview):
    ... apply state prep operations on qubits ...

@cudaq.kernel()
def MyGenericAlgorithm(statePrep : typing.Callable[[cudaq.qview], None]):
    q = cudaq.qvector(10)
    statePrep(q)
    ...

MyGenericAlgorithm(MyStatePrep)

Steps to reproduce the bug

It raises AttributeError: 'Attribute' object has no attribute 'id'

Expected behavior

Work

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

Not sure

Environment

Suggestions

No response

amccaskey commented 1 week ago

This is a simple bug in parsing the typing.Callable string in the argument type annotation. If you instead import as

from typing import Callable

and then remove typing.

def MyGenericAlgorithm(statePrep : Callable[[cudaq.qview], None]):

it should work. Still you should be able to keep the typing. and it should work. We'll have to get a fix in for that.

ikkoham commented 1 week ago

Thank you for quick response. I confirmed that your suggestion works well!