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
523 stars 185 forks source link

Improve IntelliSense/Pylance integration within kernels #2259

Open bebora opened 1 month ago

bebora commented 1 month ago

Required prerequisites

Describe the feature

The documentation suggests using VS Code for a seamless development experience. However, the integration between CUDA-Q and VS Code is not perfect. I get warning/error squiggles under code from some example kernels.

Quantum kernel developers could benefit from proper error detection to quickly identify wrong code, instead of having to run the code and see that it does indeed run.

The following section illustrates how to obtain some of the aforementioned linting errors.

Steps

Set up and run a docker image (the nightly one or the 0.8.0 one; both are affected):

docker pull nvcr.io/nvidia/nightly/cuda-quantum:latest
docker run -it --name cuda-quantum nvcr.io/nvidia/nightly/cuda-quantum:latest

Open VS Code, make sure to have the Dev Containers extension installed and connect to the running container using Ctrl+Shift+P -> Dev Containers: Attach to Running Container... Wait for the default extensions to be set up.

C++

Open the file examples/cpp/basics/cuquantum_backends.cpp and wait for IntelliSense to complete the scanning process. Hover over h(q[0]); and the following message appears: Function definition for 'h' not found. Hover over mz(q); and the following error appears: no instance of "mz" matches the argument list These kind of issues are mostly present in kernels defined as operator() of a struct. On the other hand, inline kernels, such as the one in examples/cpp/basics/noise_bit_flip.cpp, do not usually cause warnings (counterexample: open examples/cpp/basics/bernstein_vazirani.cpp).

Python

Open the file examples/python/cuquantum_backends.py and wait for Pylance to complete the scanning process. Hover over h(qvector)/x.ctrl(...)/mz(qvector) and the following message appears: "h"/"x"/"mz" is not defined

khalatepradnya commented 1 month ago

For Pylance error(s) workaround, can you try adding from cudaq.qis import * after the import cudaq statement?

bebora commented 1 month ago

Hi @khalatepradnya, that import does indeed remove the warnings without affecting code execution. There is only one issue left in the Python examples: type hints. Take a look at examples/python/bernstein_vazirani.py:

@cudaq.kernel
def oracle(register: cudaq.qview, auxillary_qubit: cudaq.qubit,
           hidden_bits: List[int]):

Pylance does not like those cudaq.qview and cudaq.qubit hints: Variable not allowed in type expression