Skip to content
首页/SDK/NVIDIA CUDA-Q
🟢

NVIDIA CUDA-Q

GPU 加速的量子计算。模拟大型电路的速度比 CPU 模拟器快几个数量级——免费且开源。

开源GPU 加速C++ 内核Python 与 C++

什么是 CUDA-Q?

NVIDIA CUDA-Q(原名 CUDA Quantum)是一个用于混合量子-经典计算的开源平台。它的 GPU 加速模拟器处理 30+ 量子比特电路的速度比 CPU 模拟器快数百到数千倍。CUDA-Q 同时支持 Python 和 C++,并且可以在任何支持 CUDA 的 NVIDIA GPU 上完全免费运行。

CUDA-Q is the fastest way to push a quantum circuit past ~30 qubits — see how it stacks up against the other free simulators.

CUDA-Q 需要 NVIDIA GPU 才能实现完整加速。对于仅有 CPU 的机器,它也可以在 CPU 模拟模式下运行,或者在配备 T4 GPU 的 Google Colab 上免费试用。

安装

terminal
# Option 1: pip (recommended for Python users)
pip install cudaq

# Option 2: Docker (for full CUDA environment)
docker pull nvcr.io/nvidia/nightly/cuda-quantum:latest
docker run --gpus all -it nvcr.io/nvidia/nightly/cuda-quantum

# Option 3: Google Colab (free GPU!)
# In a Colab cell with T4 GPU runtime:
# !pip install cudaq

使用 @kernel 装饰器编写内核

CUDA-Q 的核心概念是 @cudaq.kernel 装饰器——它将 Python 函数标记为量子内核,这些内核会被编译并在 GPU 上执行。

cudaq_kernel.py
import cudaq

# Define a quantum kernel — compiled to GPU
@cudaq.kernel
def bell_state():
    # Allocate 2 qubits
    qvec = cudaq.qvector(2)
    # Apply gates
    h(qvec[0])
    cx(qvec[0], qvec[1])
    mz(qvec)  # Measure all

# Sample the kernel — runs on GPU
counts = cudaq.sample(bell_state, shots_count=10000)
print(counts)          # { 00:4998 11:5002 }
print(counts.most_probable())  # '00' or '11'

# Get statevector
state = cudaq.get_state(bell_state)
print(state)  # [(0.707+0j), 0j, 0j, (0.707+0j)]

用于 VQE 的参数化内核

cudaq_vqe.py
import cudaq
from cudaq import spin
import numpy as np
from scipy.optimize import minimize

@cudaq.kernel
def ansatz(theta: float):
    q = cudaq.qvector(2)
    x(q[0])          # |10⟩ initial state
    ry(theta, q[0])
    cx(q[0], q[1])

# Define Hamiltonian using Pauli operators
hamiltonian = (
      5.907 * spin.z(0)
    + 2.151 * spin.z(1)
    + 5.907 * spin.z(0) * spin.z(1)
    + 0.219 * spin.x(0) * spin.x(1)
    + 0.219 * spin.y(0) * spin.y(1)
)

def cost(theta_list):
    # cudaq.observe computes ⟨ψ|H|ψ⟩ analytically on GPU
    exp_val = cudaq.observe(ansatz, hamiltonian, theta_list[0])
    return exp_val.expectation()

# Minimize the energy
result = minimize(cost, x0=[0.0], method='COBYLA', options={'maxiter': 200})
print(f"Ground state energy: {result.fun:.6f}")
print(f"Optimal theta: {result.x[0]:.4f}")

多 GPU 与异步执行

cudaq_multigpu.py
import cudaq
import asyncio

@cudaq.kernel
def ghz_state(n: int):
    qvec = cudaq.qvector(n)
    h(qvec[0])
    for i in range(n - 1):
        cx(qvec[i], qvec[i + 1])
    mz(qvec)

# Asynchronous batch execution across GPUs
async def run_experiments():
    tasks = [
        cudaq.sample_async(ghz_state, n, shots_count=1000)
        for n in [4, 8, 12, 16]
    ]
    results = await asyncio.gather(*[t for t in tasks])
    for n, r in zip([4, 8, 12, 16], results):
        print(f"GHZ({n}): {r.most_probable()}")

asyncio.run(run_experiments())

# Selecting GPU backend explicitly
cudaq.set_target("nvidia")       # Single GPU
cudaq.set_target("nvidia-mgpu")  # Multi-GPU (needs cuQuantum)

Keep exploring

💡

Also available via HLQuantum

Want to run the same circuit on multiple backends without rewriting your code? HLQuantum abstracts this SDK (and 5 others) behind a single unified API.

python
import hlquantum as hlq

qc = hlq.Circuit(2)
qc.h(0).cx(0, 1).measure_all()

# One line to switch between any backend
result = hlq.run(qc, shots=1024)              # auto-detect
result = hlq.run(qc, shots=1024, backend="cudaq")  # explicit