qpp-cpu: gate application reaches only 1–3% of memory bandwidth (full state copy + per-amplitude index arithmetic in qpp::applyCTRL)

Aperta
#6,014 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
42/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
performance

Direzione di ricerca

Start with runtime/nvqir/qpp/QppCircuitSimulator.cpp around qpp::apply, then inspect applyCTRL and coeff_idx_ket in Quantum++'s include/operations.hpp. Run the issue's Python benchmark against qpp-cpu to establish the baseline. Done means the one- and two-qubit gate path avoids the identified full copy and per-amplitude O(n) index work, with repeatable performance measurements showing the intended streaming behavior.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Required prerequisites
  • Consult the security policy. If reporting a security vulnerability, do not report the bug using this form. Use the process described in the policy to report the issue.
  • Make sure you've read the documentation. Your issue may be addressed there.
  • Search the issue tracker to verify that this hasn't already been reported. +1 or comment there if it has.
  • If possible, make a PR with a failing test to give us a starting point to work on!
Describe the bug

On the qpp-cpu target, applying a single one- or two-qubit gate is roughly 30–220× slower than one pass over the state vector would take, so the CPU simulator reaches only 0.4–3% of the machine's measured memory bandwidth (two machines below). Sampling is not the cause: at 16 qubits, drawing 1,000 shots is about 2% of a sample() call.

The cost seems to come from how gates are applied. Every gate goes through Quantum++'s generic qudit routine applyCTRL, which copies the whole state and rebuilds multi-dimensional indices for every amplitude (details under Suggestions). This makes qpp-cpu a misleadingly slow baseline in CPU-vs-GPU comparisons.

Steps to reproduce the bug

Minimal script. Time per gate is the slope of get_state time between 1 and 3 layers of the same circuit, which cancels allocation and JIT costs:

import time
import cudaq

cudaq.set_target("qpp-cpu")


@cudaq.kernel
def layers(n: int, depth: int):
    q = cudaq.qvector(n)
    for layer in range(depth):
        for i in range(n):
            rx(1.57, q[i])
            rz(0.78, q[i])
        for i in range(n - 1):
            cx(q[i], q[i + 1])


def best_time(n, depth, reps=3):
    cudaq.get_state(layers, n, depth)  # warm-up (JIT)
    times = []
    for _ in range(reps):
        start = time.perf_counter()
        cudaq.get_state(layers, n, depth)
        times.append(time.perf_counter() - start)
    return min(times)


for n in (12, 14, 16, 18):
    gates_per_layer = 3 * n - 1
    per_gate = (best_time(n, 3) - best_time(n, 1)) / (2 * gates_per_layer)
    state_bytes = 16 * 2**n
    print(f"{n} qubits: {per_gate * 1e6:8.1f} us/gate, "
          f"{per_gate / 2**n * 1e9:5.1f} ns/amplitude, "
          f"{2 * state_bytes / per_gate / 1e9:5.2f} GB/s effective")

Output on Google Colab (Intel Xeon @ 2.00 GHz, 2 vCPUs):

12 qubits:   1319.8 us/gate, 322.2 ns/amplitude,  0.10 GB/s effective
14 qubits:   5678.2 us/gate, 346.6 ns/amplitude,  0.09 GB/s effective
16 qubits:  25928.7 us/gate, 395.6 ns/amplitude,  0.08 GB/s effective
18 qubits: 126317.4 us/gate, 481.9 ns/amplitude,  0.07 GB/s effective

The cost per amplitude grows in proportion to the number of qubits: 322.2/12, 346.6/14, 395.6/16 and 481.9/18 are all 25–27 ns, i.e. about 26·n ns per amplitude per gate. A streaming update would cost the same per amplitude at every size; a cost that scales with n is consistent with the O(n) multi-index arithmetic per amplitude described under Suggestions (point 4).

Full measurements with the same method (benchmark code: https://github.com/AdzAhm/cudaq-performance-benchmarking, experiments/roofline.py and experiments/sampling_scaling.py):

GitHub Actions runner (Intel Xeon 6973P-C, 4 logical cores, NumPy copy bandwidth 29.3 GB/s):

Qubits State size Time per gate ns per amplitude Effective bandwidth Share of copy bandwidth
8 4 KiB 14.3 µs 56 0.57 GB/s 2.0%
10 16 KiB 92.0 µs 90 0.36 GB/s 1.2%
12 64 KiB 138 µs 34 0.95 GB/s 3.2%
14 256 KiB 879 µs 54 0.60 GB/s 2.0%
16 1 MiB 3.99 ms 61 0.52 GB/s 1.8%

Google Colab (Intel Xeon @ 2.00 GHz, 2 vCPUs, NumPy copy bandwidth 10.8 GB/s):

Qubits State size Time per gate ns per amplitude Effective bandwidth Share of copy bandwidth
8 4 KiB 49.4 µs 193 0.17 GB/s 1.5%
10 16 KiB 238 µs 232 0.14 GB/s 1.3%
12 64 KiB 1.12 ms 272 0.12 GB/s 1.1%
14 256 KiB 10.9 ms 665 0.05 GB/s 0.4%
16 1 MiB 21.5 ms 329 0.10 GB/s 0.9%

Sampling, for comparison (GitHub runner):

Qubits Evolution Cost per extra shot Share of a 1,000-shot sample() call
8 0.36 ms 0.23 µs 60%
12 7.7 ms 0.49 µs 16%
16 194 ms 1.47 µs 2%

For scale: on the same Colab VM, the nvidia target (Tesla T4) runs a 20-qubit, 4-layer version of this circuit in 33.9 ms; qpp-cpu takes 110 s.

Expected behavior

Applying a one- or two-qubit gate on qpp-cpu should be roughly memory-bound: one streaming pass over the state per gate, i.e. on the order of 1–3 ns per amplitude (a read + write pass at 29.3 GB/s is about 1.1 ns per complex128 amplitude; at 10.8 GB/s about 3 ns). Measured cost is 34–90 ns per amplitude on the 4-core runner and 190–670 ns on Colab.

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

Not a regression

Environment
  • CUDA-Q version: 0.15.1 (pip wheel, built from aca5853)
  • Python version: 3.13 (Google Colab) and 3.12 (GitHub Actions runner)
  • Operating system: Linux. Google Colab (Intel Xeon @ 2.00 GHz, 2 vCPUs) and GitHub Actions ubuntu-latest (Intel Xeon 6973P-C, 4 logical cores)
Suggestions

Where the time appears to go (CUDA-Q at aca5853, Quantum++ pinned at 3048006):

  1. Every gate allocates a new state: state = qpp::apply(state, matrix, targets) / qpp::applyCTRL(...)
    https://github.com/NVIDIA/cuda-quantum/blob/aca5853a76d499ecc3d5f97c2e06163ae99d9c75/runtime/nvqir/qpp/QppCircuitSimulator.cpp#L260-L263
  2. qpp::apply forwards to applyCTRL with no controls:
    https://github.com/softwareQinc/qpp/blob/304800611697e8abd8fd424bafa72a45f644f9ed/include/operations.hpp#L537
  3. applyCTRL starts by copying the full state (result = rstate):
    https://github.com/softwareQinc/qpp/blob/304800611697e8abd8fd424bafa72a45f644f9ed/include/operations.hpp#L357
  4. Each output amplitude is computed in coeff_idx_ket through n2multiidx / multiidx2n conversions over all n subsystems (with a % d per index), i.e. O(n) integer work per amplitude for a 2×2 gate:
    https://github.com/softwareQinc/qpp/blob/304800611697e8abd8fd424bafa72a45f644f9ed/include/operations.hpp#L199-L230
  5. In the no-control branch, coeff_idx_ket(1, m, r) is evaluated twice per amplitude, once for .second and once for .first:
    https://github.com/softwareQinc/qpp/blob/304800611697e8abd8fd424bafa72a45f644f9ed/include/operations.hpp#L367-L368

Sampling looks fine by comparison: qpp::sample computes the probabilities once and draws each shot from a std::discrete_distribution:
https://github.com/softwareQinc/qpp/blob/304800611697e8abd8fd424bafa72a45f644f9ed/include/instruments.hpp#L982-L1008

Possible directions:

  • Apply 1- and 2-qubit gates in place with a strided kernel (as most state-vector simulators do), keeping qpp::applyCTRL as the general qudit fallback.
  • At minimum, evaluate coeff_idx_ket once per amplitude in the no-control branch (a small upstream fix in Quantum++).
  • If qpp-cpu is intended only as a reference backend, saying so in the docs would help, since it is a natural CPU baseline in CPU-vs-GPU comparisons.

Happy to test a fix.

I found this bug when working on my repo: https://github.com/AdzAhm/cudaq-performance-benchmarking

Lingua principale
C++
Stelle
1.2k
Fork
508
Merge medio
1g 19h
PR unite (30g)
100

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di NVIDIA/cuda-quantum

Tutte le issue di NVIDIA/cuda-quantum

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.