GPU Backend
The GPU backend is optional and gated behind the gpu feature. Building it needs no
CUDA toolkit: the NVIDIA driver loads when the first GpuContext opens, and NVRTC
when kernels need compiling.
Running it needs a CUDA-capable device, a driver for CUDA 12.0 or newer, and NVRTC
from a CUDA 12 or CUDA 13 toolkit on the loader path (PATH on Windows, the ld.so
search path on Linux). NVRTC 12 is preferred when both load, since NVRTC 13 does not
compile for cards older than Turing. A missing driver or NVRTC surfaces as a
PrismError from GpuContext::new.
cargo build --release --features "parallel gpu"
cargo nextest run --features "parallel gpu" --test golden_gpu --test golden_gpu_density_matrix
Constructing a GpuContext compiles the CUDA kernels through NVRTC, one to three
seconds on a GTX 1080 Ti. NVRTC emits a cubin for the device's exact architecture, which
loads on any CUDA 12 driver. An NVRTC older than the device compiles PTX for the newest
architecture it knows, and the driver JITs it. That path needs a driver at least as new
as the NVRTC, and GpuContext::new names both versions when it is not.
The compiled image is cached in prism-q-ptx under the user cache directory, keyed by
device architecture, crate version and kernel source, so later processes skip the
compile. A cached cubin loads without NVRTC. Delete the directory to force a recompile.
The device runs statevector, stabilizer and density matrix execution and compiled BTS
sampling. BackendKind has four device kinds, and the backends and samplers below opt in
directly:
BackendKind::AutoGpu { context }(simulate(circuit).gpu_auto(ctx)). The decision tree runs unchanged, and a selected statevector or stabilizer workload that clears its family's qubit crossover and fits in VRAM runs on the device. Everything else, including a device allocation that fails atinit, takes the identical CPU path.RunMetadata::placementreportsHostorDevicefor the run.BackendKind::StatevectorGpu { context }(simulate(circuit).gpu(ctx)). Keeps fusion and subsystem decomposition, and leaves sub-circuits belowcrate::gpu::min_qubits()(default 14,PRISM_GPU_MIN_QUBITSoverride) on the CPU. Measurements and resets in one instruction run are decided on the device and read back together, at the end of the run or before a classically conditioned gate or region. A noisy trajectory shot keeps them on the device across its noise sites until a conditional, a region, a readout flip, a Kraus branch picked on the host, or the end of the shot. Terminal shots and counts are drawn on the device, and one state index per shot comes back in place of the2^nprobability vector, while that moves fewer bytes: fewer shots than about two thirds of the amplitudes. More shots read the probabilities back and sample on the host.BackendKind::StabilizerGpu { context }. Gates apply to a device tableau through one word-grouped batched Clifford kernel (stab_apply_word_grouped). Measurement and reset keep pivot search, row cascade, phase fixup and deterministic outcomes on the device, a run of measurements with a few launches and no host copy per measurement. Circuits from 1,000 qubits run on the device (STABILIZER_MIN_QUBITS_DEFAULT,PRISM_STABILIZER_GPU_MIN_QUBITSoverride), unless a measurement group shorter than eight, a reset or a guarded region would take a host round trip each; those stay on the CPU at any width. Direct backend benchmarks should useStabilizerBackend::with_gpu(ctx)to exclude diagnostic readbacks fromprobabilities(),export_tableau()andexport_statevector(). Golden tests cover every kernel path, including 500q GHZ measure-all.BackendKind::DensityMatrixGpu { context }. The exact mixture in device memory. Explicit only: neitherAutonorAutoGpuselects it, and it has no crossover and no host fallback.initbudgets the4^nbuffer against free VRAM and errors before allocating when it does not fit, so an 11 GiB card holds 14 qubits (4 GiB at 14 plus a2^ndiagonal scratch; 15 needs 16 GiB). The unitary half reuses the dense statevector kernels on the embedded2n-qubit buffer, and every channel, measurement and readout sweep has a kernel of its own. The noisySimulateterminals answer from the device mixture exactly asDensityMatrixdoes.DensityMatrixBackend::new(seed).with_gpu(ctx)is the direct form.StatevectorBackend::new(seed).with_gpu(ctx). Every instruction routes to CUDA once the context is attached, with no crossover or subsystem decomposition.StabilizerBackend::new(seed).with_gpu(ctx), for kernel benchmarks and targeted correctness tests.run_shots_compiled_with_gpu(orCompiledSampler::with_gpu(ctx)). BTS sampling for flat sparse parity: one kernel per chunk of65_536shots, random bits generated on the host, and the CPUsample_bts_meas_majorlayout. Sparse parity CSR arrays, packed reference bits and scratch stay cached on the device. A sampler stays on the CPU, where launch and transfer setup would cost more than the kernel saves, below131_072shots (PRISM_GPU_BTS_MIN_SHOTS), below rank 4 (PRISM_GPU_BTS_MIN_RANK; GHZ and other low-rank samplers expand each shot from a few bits), or with fewer than 2 parity entries per measurement (PRISM_GPU_BTS_MIN_WEIGHT_FACTOR).sample_bulk_packed_devicereturns aDevicePackedShotshandle. Marginals reduce to one counter per measurement row on the device. Exact counts use a bounded device hash reduction for up to 8 packed measurement words when the compact result is cheaper to transfer than the full shot matrix, and a host copy otherwise.NoisyCompiledSampler::with_gpu(ctx). The same BTS path with Pauli noise. Noise masks come from an RNG stream seeded on the device when the event probabilities allow it, or from the host otherwise, and counts and marginals reduce on the device. The device stream differs from the CPU one, so output matches the CPU statistically, not shot for shot. Readout error keeps materialized shots on the CPU.CompiledDetectorSampler::with_gpu(ctx). Detector and observable sampling over the device measurement sampler, with parity rows derived as on the CPU.
On the hard statevector path (StatevectorGpu, with_gpu), a state that does not fit
the free VRAM is rejected at init with an error naming the requested and free device
memory.
GpuContext reports on its device before a run: device_name, vram_bytes and
vram_available, max_qubits_for_statevector (the advisory cap from free memory), and
fits_statevector(n) for the amplitudes alone. fits_statevector_with_scratch(n) and
fits_tableau(n) are the checks AutoGpu runs before placing a block. synchronize
waits for queued kernels, which timing device work needs. gpu::is_available() opens no
context. gpu::nvrtc_available() reports whether NVRTC loads, which matters only when no
cached kernel image fits the device. Under bench-internal, pass_census counts
launches, passes over the state and host copies since the context opened.
Python reaches the four device kinds too. The Linux and Windows wheels carry the gpu
feature, and pip install "prism-q[cuda12]" adds NVRTC from PyPI. See
Python Bindings.
Module layout (src/gpu/)
| File | Role |
|---|---|
mod.rs | GpuContext public entry point, the GpuState and GpuTableau device buffers (private to the crate), crossover knobs, GpuPassCensus (bench-internal) |
device.rs | GpuDevice: cudarc wrapper, compiles the kernel image at device construction |
memory.rs | GpuBuffer: device Complex64 storage |
tiles.rs | Lowering of a fused stream to mixed tile ops and the tile planner |
kernels/mod.rs | KERNEL_NAMES, LauncherScratch, composed kernel_source() concatenating dense, stabilizer, BTS and density |
kernels/dense.rs | Rust launchers for every Gate variant; CUDA C source in kernels/dense.cu |
kernels/stabilizer.rs | Launchers for tableau init, the batched Clifford kernel stab_apply_word_grouped, stab_rowmul_words, and measurement as pivot search, cascade, fixup and a deterministic branch; source in kernels/stabilizer.cu |
kernels/bts.rs | Launchers for compiled BTS shot sampling; source in kernels/bts.cu |
kernels/density.rs | Launchers for the density-matrix path: sandwiches, Kraus channels, projection and Pauli expectation; source in kernels/density.cu |
Kernel design notes live in the module docstrings under src/gpu/kernels/. The
statevector crossover measurement is on MIN_QUBITS_DEFAULT in src/gpu/mod.rs.
Kernel coverage
Every Gate variant runs on the device, though not each through a kernel of its own.
The one- and two-qubit gates with a fixed matrix share one dense matrix launcher.
QftBlock runs as shared memory DIF passes (qft_dif_pass, up to 11 stages each) plus
a bit reversal pass where the flags need one, and PauliRot lowers to a CNOT ladder
around a parity-phase kernel. Batched variants (BatchPhase, BatchRzz,
DiagonalBatch, MultiFused { all_diagonal: true }) use LUT kernels fed by the same
host table builders as the CPU path.
Non-diagonal MultiFused uses a shared memory tiled kernel (apply_multi_fused_tiled,
TILE_Q = 10, TILE_SIZE = 1024) over ten qubits per pass: the five lowest, which keep
a warp's loads contiguous, plus up to five of the sub-gates' higher targets. A
MultiFused over n qubits takes about (n - 5) / 5 passes, and a pass with fewer than
three sub-gates falls back to per gate launches. Multi2q launches once per sub-gate. A
sub-gate or Fused2q whose matrix is exactly a CX, swap, CZ or controlled phase runs on
that gate's kernel, and one with a single nonzero per row (a permutation with phases, or
a diagonal) on apply_monomial_2q. Only the rest pay the dense 4x4 multiply, which
takes three real multiply-adds per complex product (the Gauss form). On a card with
reduced FP64 rate such as the GTX 1080 Ti it costs about one sweep of the state instead
of one and a half.
From 18 qubits the statevector fuses for the device (FusionProfile::Device, see
Fusion Pipeline) and apply_instructions
lowers the stream to tiles. One apply_mixed_tile block gathers the 2^11 amplitudes of
11 qubits (the two lowest plus up to nine the planner picks) into 32 KB of shared memory,
applies every op the tile holds, and writes them back: one pass for a run of 1q
matrices, diagonals, CX, swap, CZ, controlled phases and Rzz. A real 1q matrix, or one
shaped like Rx, takes half the multiplies of a general one. Dense 4x4 blocks, Mcu,
QftBlock and the rest keep their kernels. On a GTX 1080 Ti a five-layer hardware
efficient ansatz at 24 qubits drops from 115 dense passes to 7 tile passes, and a random
depth-10 circuit from 73 to 7. The tiles are FP64 bound on that card, so a dense 1q gate
still costs about half a sweep.
The CUDA C source sits in .cu files beside the launchers and reaches
KERNEL_SOURCE_TEMPLATE through include_str!. Placeholders in kernels/dense.cu are
filled at device construction. Batch table sizes such as {{BP_TABLE_SIZE}} come from the
constants in src/backend/statevector/kernels/batch_tables/mod.rs, so CPU and GPU share
one value; the tile width {{TILE_Q}} comes from the GPU tile constants.
Correctness
tests/golden_gpu.rs compares GPU amplitudes against the CPU statevector within
1e-12 for every gate variant, the fusion paths, and the BackendKind::StatevectorGpu
public dispatch path at the crossover boundary. tests/golden_gpu_density_matrix.rs
does the same for the full density matrix buffer. Both suites fail when no device
opens, unless PRISM_ALLOW_NO_GPU=1 lets them skip on a host without a card. No CI job
opens a device; scripts/test-gpu.ps1 runs them on a host with one.
Shot reproducibility
The golden equality tests do not cover three limits on what a seed guarantees.
- CPU against GPU: agreement in distribution, not bit for bit. Both paths draw the
same RNG stream for a given shot seed, but the device reduction behind a measurement
probability sums in tree order with FMA contraction and can differ from the host sum
in the last ulp. A uniform draw landing between the two flips that outcome and every
one after it. Where the probability is a dyadic rational (0.5, 1.0, and the
amplitudes Clifford gates reach) both sums are exact and the shots match, as
statevector_gpu_mid_measure_shots_match_cpupins.statevector_gpu_shot_frequencies_match_cpu_off_dyadicpins the general case: equal frequencies within 5 sigma afterRx(0.3). - Terminal shots and counts on the GPU statevector: below about two shots for every
three amplitudes, the device walks a scan of the state in blocks of 4096 amplitudes,
which rounds differently from the host's running sum, so a draw within rounding of the
boundary between two outcomes can land on the neighbouring one. The choice of walk
counts bytes only, so neither it nor the result depends on the card or the Rayon
thread count.
statevector_gpu_terminal_sampling_matches_cpu_marginalspins the frequencies against the CPU probabilities. - GPU BTS sampling: reproducible outright. The random bits are drawn on the device from one seed per call, one xoshiro256++ stream per 64-shot batch and block of 32 parity columns, so the shots do not depend on the Rayon thread count or on how a call is split into chunks, and the host and device result paths return the same shots.
Current limits
- Stabilizer
probabilities(),export_tableau(), andexport_statevector()read back to the CPU. - Every trajectory shot rebuilds the backend and reallocates the device buffer, measured at 0.1 ms per shot at 20 qubits.
- A one-qubit
CustomKraus channel with more than 16 operators, or with an operator whose diagonal test depends on the branch weight, picks its branch on the host from a reduced density matrix read back from the device (rdm_qubit). Damping, thermal relaxation and the otherCustomchannels pick the branch on the device. - Two-qubit Kraus channels compute the pair reduced density matrix on the device
(
rdm_2q, 128-byte readback) and apply the chosen branch on the device. PRISM_PTX_CACHE_DIRoverrides the disk PTX and cubin cache location; setting it to an empty string disables disk caching.