Generative AI

Leverage AI Coding Assistants to Develop Quantum Applications at Scale with NVIDIA CUDA-Q

AI coding assistants have become ubiquitous across the software development landscape. Developers are increasingly using tools like GitHub Copilot, Amazon CodeWhisperer, and Cursor to boost productivity in computing tasks. These tools enable you to quickly generate, debug, and understand code; streamline workflows; and enhance collaboration across projects.

While AI coding assistants are well-established in classical computing, their application in quantum computing is just beginning to gain traction. This application is one of many ways AI can enable quantum computing. This post explores how Cursor, one of the leading AI-assisted integrated development environments (IDE), can be leveraged to develop code for NVIDIA CUDA-Q, a platform for high-performance, hybrid quantum applications. 

CUDA-Q is an open-source platform that integrates GPUs, CPUs, and QPUs to enable scalable, hybrid quantum computing. It’s currently used by a diverse range of users, from students to quantum researchers to life scientists to HPC scientists leveraging today’s most advanced GPU-supercomputers.

With AI coding assistants, getting started with CUDA-Q and developing quantum applications at scale has never been easier. The following sections demonstrate how to use Cursor to: 

  • Generate CUDA-Q code
  • Query the codebase and docs 
  • Port existing code to CUDA-Q

These examples will showcase how AI tools can streamline the process of building and accelerating CUDA-Q applications at scale. 

Getting started

To begin, download and install CUDA-Q and Cursor. An easy way to install CUDA-Q is to pull the CUDA-Q Docker image and run the container. The PyPI Python installation instructions, as well as the C++ installation guide can be found in CUDA-Q Quick Start

Once the CUDA-Q container is running, download and install Cursor. Cursor’s look and feel will be very familiar to users of Microsoft Visual Studio Code (Cursor was forked from VSCode). Then attach Cursor to the CUDA-Q container and open the environment, which will show a variety of CUDA-Q examples and tutorials included in the container. Cursor may also prompt you to install additional extensions that it detects are necessary for the CUDA-Q environment.

Screenshot of a dropdown menu in the Cursor IDE.
Figure 1. Connecting Cursor to a running CUDA-Q container with the Attach to Running Container option

With a fresh Python notebook, you can start using the Cursor AI features to generate CUDA-Q code. Cursor offers a range of base models, including claude-3.5-sonnet, gpt-4o, cursor-small, and several others. It can index your entire codebase, enabling you to query it directly. Additionally, Cursor enables you to add context to queries by specifying files, documentation, and websites to the chat context before it provides answers. Its knowledge base was trained on major quantum computing frameworks, so it knows CUDA-Q syntax out of the box. Note that the examples in this post use the claude-3.5-sonnet model from the free version of Cursor.

Generating code

Use the Cursor built-in chat window (⌘-L) to start generating a few CUDA-Q examples. To improve Cursor’s responses, explicitly link the CUDA-Q docs to Cursor’s chat context. In the chat window, type @docs, select +New doc, and link the URL of the CUDA-Q documentation.

For example, ask the chat, “How do I initialize a CUDA-Q kernel?”

Screenshot of Cursor’s chat window generating a CUDA-Q kernel
Figure 2. Asking Cursor’s chat how to initialize a CUDA-Q kernel

Cursor correctly outputs a simple hello world example to define a quantum kernel, giving a superposition state. 

CUDA-Q kernels are functions that can be executed on a quantum resource. The quantum resource in these examples will be the CPU simulation backend; however, CUDA-Q makes it easy to accelerate your simulations on GPU systems of all scales and a variety of physical QPUs.

You can ask for an explanation of certain aspects of the code, or ask follow-up questions. In response to the question, “Show me how to execute the kernel and print the results” (Figure 3). The chat generates a Bell state example and an explanation (Figure 4).

Screenshot  of Cursor’s chat generating follow-up code on executing CUDA-Q kernels
Figure 3. Asking Cursor a follow-up question on how to execute CUDA-Q kernels
Screenshot of Cursor’s chat giving an explanation of the generated code
Figure 4. Cursor’s explanation of the code it generated to execute a CUDA-Q kernel using the sample method

To execute the code, click the ‘Run as cell’ button. This correctly outputs the following statistics of the Bell state:

{ 00:504 11:496 }

Measured 00: 504 times
Measured 11: 496 times

You can also ask the chat to generate a more advanced example of a single layer of a quantum neural network with parameterized gates:

Screenshot of Cursor generating a quantum neural network layer
Figure 5. Cursor generating a more advanced CUDA-Q example with a quantum neural network layer with parameterized gates

The chat correctly generates a kernel called qnn_layer, which rotates each qubit with three parameters, then applies a layer of entangling gates across four qubits. It provides the following helpful explanation:

Screenshot of Cursor’s explanation for the generated quantum neural network layer
Figure 6. Cursor’s explanation of the generated quantum neural network layer

The chat is a good way for beginners to generate examples from scratch. Users who are already familiar with CUDA-Q can proceed to code in the IDE and use Cursor’s tab-complete feature, which already knows CUDA-Q syntax.

Querying the codebase

Use the chat to ask questions and explore CUDA-Q features directly by linking the docs and specific files explicitly with the @ command. Unfamiliar codebases can be challenging to navigate, and useful features can sometimes be hidden or hard to find. While reading the docs and combing through the source code is often the recommended way of familiarizing oneself with a framework, the reality is few people have the time or patience to do this comprehensively. 

Querying the codebase and docs through the chat directly is an amazingly effective way to understand a codebase’s structure, capabilities, and hidden features. This approach enables interactive learning and encourages follow-up questions, enabling users to develop familiarity with CUDA-Q organically. 

You can use the chat to learn how to return the statevector instead of shots, and to ask about other simulation backends available with CUDA-Q.

Screenshot of querying Cursor’s chat on how to use other CUDA-Q backends
Figure 7. Using Cursor to query the CUDA-Q codebase on how to use other backends

The chat correctly points to the get_state function, which when executed yields the real and imaginary components of the qubit in the superposition state:

(0.707107,0)
(0.707107,0)

For setting a different simulation target, the chat provides two other options, the ”density-matrix-cpu” simulator and “nvidia” target for GPU-accelerated simulations. Additional helpful (and correct) examples include invoking the cudaq.draw function to visualize circuits:

Screenshot of Cursor giving additional examples of using GPU-accelerated simulation backends
Figure 8. Examples of using GPU-accelerated simulation backends

Running the following cell (Figure 9, top) returns the circuit diagram (Figure 9, bottom):

An image of Cursor generating an example of how to display quantum circuits (above) with the corresponding output (below).
Figure 9. Cursor generating examples of displaying generated quantum circuits using the draw method (top) and the corresponding output (bottom)

For other visualization tools, Cursor points out some of the new CUDA-Q features from the latest v0.8 release to visualize qubit states on the Bloch sphere:

Screenshot of Cursor generating code to visualize the qubit state on the Bloch sphere
Figure 10. Cursor generating code to visualize the qubit state on the Bloch sphere, using features from the latest CUDA-Q v0.8 release

This produces the following Bloch sphere visualization:

An image of the Bloch sphere
Figure 11. Bloch sphere visualization in CUDA-Q of the generated Cursor code

It represents the state: 

(-0.00150066,-0.00170142)
(0.996383,-0.084946)

It is produced by the rotation angles: [0.80783041, 3.13705533, 3.77932564]

Porting to CUDA-Q

You can use Cursor to experiment with porting codes written in other quantum frameworks to CUDA-Q to leverage the excellent performance and scalability of CUDA-Q . 

For example, in response to the query, “Give me an example of porting Pythonic code from another quantum framework to CUDA-Q,” Cursor does an excellent job of translating the syntax of Qiskit to the syntax of CUDA-Q. 

Example Cursor CUDA-Q porting query.
An image of Cursor translating Qiskit code to CUDA-Q
Figure 12. Cursor translating Qiskit code to CUDA-Q with the line-by-line translation highlighted

The chat provides the following helpful explanation of the differences:

An image of Cursor explaining the difference between CUDA-Q and Qiskit code that it has ported
Figure 13. Cursor provides a helpful explanation of the syntactical difference of CUDA-Q versus Qiskit in the ported code

You can now leverage the performance of CUDA-Q for this code.

Verify output

While experimenting with these examples, the chat occasionally generates minor syntactical mistakes that cause errors when the code is executed. When this happens, the user may need to manually debug the error, although sometimes the chat can resolve the issue if the error is raised to it.

Does this mean AI-assisted coding tools are useless? Of course not. Although they require human supervision to verify their output, AI-assisted coding tools significantly increase the speed and efficiency of generating code examples, providing immediate feedback and explanations for the code.

Conclusion

AI coding assistants are a powerful way of improving quantum developer productivity and lowering the barrier to entry to developing scalable, high-performance hybrid quantum applications using CUDA-Q. This post has shown that coding assistants like Cursor do an excellent job generating CUDA-Q code, providing helpful explanations of the codebase, and enabling users of other frameworks to leverage CUDA-Q to accelerate their applications. 

Get started today with NVIDIA CUDA-Q and Cursor.

Discuss (0)

Tags