prism-q 0.35.0

Fast Rust quantum circuit simulator. OpenQASM 3.0, multiple backends, AVX2 SIMD kernels, optional CUDA and MPI, QEC tooling, Python bindings.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
//! GPU execution context that CPU backends attach to route their hot kernels to a CUDA
//! device, plus the device statevector and tableau types.
//!
//! Device statevectors are `2 * 2^n` interleaved (re, im) `f64` values, the layout of
//! `num_complex::Complex64` and CUDA's `double2`, so neither side converts.

pub(crate) mod device;
pub(crate) mod kernels;
pub(crate) mod memory;

use std::sync::Arc;

use num_complex::Complex64;

use crate::error::Result;

pub(crate) use self::device::GpuDevice;
pub(crate) use self::memory::GpuBuffer;

/// Default for `PRISM_GPU_MIN_QUBITS`: below this many qubits,
/// [`crate::BackendKind::StatevectorGpu`] builds a host `StatevectorBackend` instead.
///
/// Measured at 14 on a GTX 1080 Ti, where smaller states fit in L3 and PCIe round trips
/// and launch latency dominate.
pub const MIN_QUBITS_DEFAULT: usize = 14;

/// Whether a CUDA device is present and usable in this process.
///
/// Safe to call without a [`GpuContext`] and without the `cudarc` dynamic
/// library loaded. Returns `false` if detection fails for any reason.
pub fn is_available() -> bool {
    GpuContext::is_available()
}

/// Whether NVRTC, which compiles the kernels when no cached image matches, loads in this
/// process under one of the names `cudarc` searches (`libnvrtc.so.12`, `nvrtc64_120_0.dll`).
pub fn nvrtc_available() -> bool {
    device::nvrtc_present()
}

/// Statevector GPU crossover in qubits, read from `PRISM_GPU_MIN_QUBITS` once per process.
pub fn min_qubits() -> usize {
    static CACHED: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
    *CACHED
        .get_or_init(|| crate::env_knobs::usize_knob("PRISM_GPU_MIN_QUBITS", MIN_QUBITS_DEFAULT, 0))
}

/// Default for `PRISM_GPU_BTS_MIN_SHOTS`: smaller batches stay on the CPU BTS sampler
/// even with a GPU context attached, skipping the launch and transfer setup.
pub const BTS_MIN_SHOTS_DEFAULT: usize = 131_072;

/// Default for `PRISM_GPU_BTS_MIN_RANK`. Low-rank samplers such as GHZ or independent H
/// layers run faster on the CPU even at large shot counts, since each shot expands from a
/// few random bits.
pub const BTS_MIN_RANK_DEFAULT: usize = 4;

/// Default for `PRISM_GPU_BTS_MIN_WEIGHT_FACTOR`. The GPU path requires
/// `total_weight >= num_measurements * factor`, which filters out parity maps too sparse
/// to cover the launch overhead.
pub const BTS_MIN_WEIGHT_FACTOR_DEFAULT: usize = 2;

pub(crate) fn bts_min_shots() -> usize {
    static CACHED: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
    *CACHED.get_or_init(|| {
        crate::env_knobs::usize_knob("PRISM_GPU_BTS_MIN_SHOTS", BTS_MIN_SHOTS_DEFAULT, 0)
    })
}

pub(crate) fn bts_min_rank() -> usize {
    static CACHED: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
    *CACHED.get_or_init(|| {
        crate::env_knobs::usize_knob("PRISM_GPU_BTS_MIN_RANK", BTS_MIN_RANK_DEFAULT, 1)
    })
}

pub(crate) fn bts_min_weight_factor() -> usize {
    static CACHED: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
    *CACHED.get_or_init(|| {
        crate::env_knobs::usize_knob(
            "PRISM_GPU_BTS_MIN_WEIGHT_FACTOR",
            BTS_MIN_WEIGHT_FACTOR_DEFAULT,
            1,
        )
    })
}

/// Default for `PRISM_STABILIZER_GPU_MIN_QUBITS`, set high so
/// [`crate::BackendKind::StabilizerGpu`] stays on the host path.
///
/// The device path is correct, but no direct backend benchmark yet justifies a lower
/// crossover. Set the variable to `0` to opt in.
pub const STABILIZER_MIN_QUBITS_DEFAULT: usize = 100_000;

/// Stabilizer GPU crossover in qubits, read from `PRISM_STABILIZER_GPU_MIN_QUBITS` once
/// per process.
pub fn stabilizer_min_qubits() -> usize {
    static CACHED: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
    *CACHED.get_or_init(|| {
        crate::env_knobs::usize_knob(
            "PRISM_STABILIZER_GPU_MIN_QUBITS",
            STABILIZER_MIN_QUBITS_DEFAULT,
            0,
        )
    })
}

/// Device bytes a GPU statevector needs per amplitude: 16 for the interleaved
/// complex buffer, 8 for the probabilities scratch, 1 of margin for the measurement
/// partials and launcher metadata. The soft `Auto` gate and the hard explicit gate
/// both budget with it.
pub(crate) const STATEVECTOR_BYTES_PER_AMPLITUDE: usize = 25;

/// Device handle and compiled kernel module, shared across backends and simulations
/// through an `Arc` so the device initializes once.
pub struct GpuContext {
    device: Arc<GpuDevice>,
    launcher_scratch: std::sync::Mutex<kernels::LauncherScratch>,
}

impl std::fmt::Debug for GpuContext {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("GpuContext")
            .field("device", &self.device)
            .finish_non_exhaustive()
    }
}

impl GpuContext {
    /// Initialise the context for the given CUDA device ordinal.
    ///
    /// Compiles the kernel module at construction. Subsequent calls reuse the cached image.
    pub fn new(device_id: usize) -> Result<Arc<Self>> {
        let device = Arc::new(GpuDevice::new(device_id)?);
        Ok(Arc::new(Self {
            device,
            launcher_scratch: std::sync::Mutex::new(kernels::LauncherScratch::default()),
        }))
    }

    pub fn is_available() -> bool {
        GpuDevice::is_available()
    }

    /// Product name of the device bound to this context.
    pub fn device_name(&self) -> Result<String> {
        self.device.name()
    }

    /// Total VRAM on the device bound to this context.
    pub fn vram_bytes(&self) -> Result<usize> {
        self.device.vram_bytes()
    }

    /// Free VRAM currently available on the device bound to this context.
    ///
    /// Reflects allocations by all processes sharing the device, not only those
    /// made through this `GpuContext`.
    pub fn vram_available(&self) -> Result<usize> {
        self.device.vram_available()
    }

    /// Maximum qubit count for a dense Complex64 statevector in the currently
    /// free VRAM of this device.
    pub fn max_qubits_for_statevector(&self) -> Result<usize> {
        self.device.max_qubits_for_statevector()
    }

    /// Whether the currently-available VRAM can hold a dense Complex64
    /// statevector for `num_qubits` qubits.
    ///
    /// Counts only the amplitude buffer at 16 bytes per amplitude;
    /// `fits_statevector_with_scratch` also budgets the probabilities and
    /// measurement scratch.
    pub fn fits_statevector(&self, num_qubits: usize) -> Result<bool> {
        if num_qubits >= usize::BITS as usize - 4 {
            return Ok(false);
        }
        let amplitude_bytes = (1usize << num_qubits).checked_mul(16).ok_or_else(|| {
            crate::error::PrismError::InvalidParameter {
                message: format!("num_qubits={num_qubits} overflows usize"),
            }
        })?;
        let available = self.vram_available()?;
        Ok(amplitude_bytes <= available)
    }

    /// Whether the currently-available VRAM holds a dense statevector for
    /// `num_qubits` plus the reduction scratch `GpuState` keeps live.
    ///
    /// Budgets 25 bytes per amplitude: 16 for amplitudes, 8 for the probabilities
    /// scratch, and 1 of margin for the measurement partials (`ceil(2^n / 512)` f64) and
    /// launcher metadata.
    /// This is the fail-fast gate for the `Auto` GPU statevector leaf; the backend's
    /// soft mode ([`StatevectorBackend::with_gpu_auto`](crate::backend::statevector::StatevectorBackend::with_gpu_auto))
    /// catches any transient allocation that slips past it.
    pub fn fits_statevector_with_scratch(&self, num_qubits: usize) -> Result<bool> {
        if num_qubits >= usize::BITS as usize - 5 {
            return Ok(false);
        }
        let bytes = (1usize << num_qubits)
            .checked_mul(STATEVECTOR_BYTES_PER_AMPLITUDE)
            .ok_or_else(|| crate::error::PrismError::InvalidParameter {
                message: format!("num_qubits={num_qubits} overflows usize"),
            })?;
        Ok(bytes <= self.vram_available()?)
    }

    /// Whether the currently-available VRAM holds a stabilizer tableau for
    /// `num_qubits`, matching the device layout in [`GpuTableau`].
    ///
    /// The tableau stores `(2n+1)` rows of `2 * ceil(n/64)` u64 words plus one
    /// phase byte per row; the fixed measurement scratch is negligible. Used as
    /// the fail-closed gate for the `Auto` GPU stabilizer leaf.
    pub fn fits_tableau(&self, num_qubits: usize) -> Result<bool> {
        let total_rows = num_qubits.checked_mul(2).and_then(|r| r.checked_add(1));
        let num_words = num_qubits.div_ceil(64).max(1);
        let xz_words = total_rows.and_then(|rows| rows.checked_mul(2 * num_words));
        let bytes = xz_words
            .and_then(|w| w.checked_mul(8))
            .and_then(|b| b.checked_add(total_rows.unwrap_or(usize::MAX)));
        match bytes {
            Some(bytes) => Ok(bytes <= self.vram_available()?),
            None => Ok(false),
        }
    }

    /// Block until every kernel queued on this context's stream has finished.
    ///
    /// Launches are asynchronous, so a caller timing device work must call
    /// this before reading the clock; a readback synchronizes on its own.
    pub fn synchronize(&self) -> Result<()> {
        self.device
            .stream()?
            .synchronize()
            .map_err(|e| driver_err("synchronize", e))
    }

    pub(crate) fn device(&self) -> &GpuDevice {
        &self.device
    }

    pub(crate) fn launcher_scratch(&self) -> std::sync::MutexGuard<'_, kernels::LauncherScratch> {
        self.launcher_scratch
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
    }

    #[cfg(test)]
    pub(crate) fn stub_for_tests() -> Arc<Self> {
        Arc::new(Self {
            device: Arc::new(GpuDevice::stub_for_tests()),
            launcher_scratch: std::sync::Mutex::new(kernels::LauncherScratch::default()),
        })
    }
}

/// Device statevector of `2 * 2^num_qubits` interleaved (re, im) f64s.
///
/// As on the CPU statevector backend, measurement collapse accumulates into
/// `pending_norm`, applied at `export_statevector` or `probabilities` time.
#[derive(Debug)]
pub struct GpuState {
    context: Arc<GpuContext>,
    buffer: GpuBuffer<f64>,
    num_qubits: usize,
    pending_norm: f64,
    /// Reused across `probabilities()` calls so shot sampling does not reallocate `2^n`
    /// f64s per read.
    probs_scratch: std::cell::RefCell<Option<GpuBuffer<f64>>>,
}

impl GpuState {
    /// Allocate a fresh |0…0⟩ state on the device bound to `context`.
    ///
    /// Rejects `num_qubits` at or above `usize::BITS - 4`, the bound
    /// [`GpuContext::fits_statevector`] uses, where the 16 bytes per amplitude
    /// overflow `usize`.
    pub fn new(context: Arc<GpuContext>, num_qubits: usize) -> Result<Self> {
        if num_qubits >= usize::BITS as usize - 4 {
            return Err(crate::error::PrismError::InvalidParameter {
                message: format!("num_qubits={num_qubits} overflows addressable memory"),
            });
        }
        let len = 2usize << num_qubits;
        let buffer = GpuBuffer::<f64>::alloc_zeros(context.device(), len)?;
        let mut state = Self {
            context: context.clone(),
            buffer,
            num_qubits,
            pending_norm: 1.0,
            probs_scratch: std::cell::RefCell::new(None),
        };
        kernels::dense::launch_set_initial_state(&context, &mut state)?;
        Ok(state)
    }

    /// Allocate a device state initialized from host amplitudes. The slice
    /// length must be a power of two of at least 2; callers validate before
    /// converting.
    pub fn from_host_amplitudes(context: Arc<GpuContext>, amps: &[Complex64]) -> Result<Self> {
        debug_assert!(amps.len().is_power_of_two() && amps.len() >= 2);
        let num_qubits = amps.len().trailing_zeros() as usize;
        let buffer = GpuBuffer::<f64>::from_host(context.device(), complex_as_interleaved(amps))?;
        Ok(Self {
            context,
            buffer,
            num_qubits,
            pending_norm: 1.0,
            probs_scratch: std::cell::RefCell::new(None),
        })
    }

    /// A second device state holding a copy of this one's amplitudes and pending norm.
    pub(crate) fn duplicate(&self) -> Result<Self> {
        let device = self.context.device();
        let mut buffer = GpuBuffer::<f64>::alloc_zeros(device, self.buffer.len())?;
        buffer.copy_from_device(device, &self.buffer)?;
        Ok(Self {
            context: self.context.clone(),
            buffer,
            num_qubits: self.num_qubits,
            pending_norm: self.pending_norm,
            probs_scratch: std::cell::RefCell::new(None),
        })
    }

    pub fn num_qubits(&self) -> usize {
        self.num_qubits
    }

    /// Multiplicative norm correction deferred from measurement collapse.
    pub fn pending_norm(&self) -> f64 {
        self.pending_norm
    }

    /// Read the amplitudes with `pending_norm` applied. The device copy lands in the
    /// returned vector's own storage, with no second host copy.
    pub fn export_statevector(&self) -> Result<Vec<Complex64>> {
        let mut raw = vec![0.0_f64; self.buffer.len()];
        self.buffer.copy_to_host(self.context.device(), &mut raw)?;
        let mut out = interleaved_into_complex(raw);
        if self.pending_norm != 1.0 {
            scale_in_place(&mut out, self.pending_norm, self.num_qubits);
        }
        Ok(out)
    }

    /// Compute per-basis-state probabilities with `pending_norm²` scaling, via a GPU
    /// reduction kernel. Only `2^n` f64s cross PCIe (vs `2·2^n` for raw amplitudes).
    pub fn probabilities(&self) -> Result<Vec<f64>> {
        kernels::dense::launch_compute_probabilities(&self.context, self)
    }

    pub(crate) fn context(&self) -> &Arc<GpuContext> {
        &self.context
    }

    pub(crate) fn buffer(&self) -> &GpuBuffer<f64> {
        &self.buffer
    }

    pub(crate) fn buffer_mut(&mut self) -> &mut GpuBuffer<f64> {
        &mut self.buffer
    }

    pub(crate) fn set_pending_norm(&mut self, norm: f64) {
        self.pending_norm = norm;
    }

    /// Cached probabilities buffer, `None` until the first caller allocates it. The caller
    /// grows it when `num_qubits` has increased.
    pub(crate) fn probs_scratch(&self) -> std::cell::RefMut<'_, Option<GpuBuffer<f64>>> {
        self.probs_scratch.borrow_mut()
    }
}

/// View complex amplitudes as their interleaved `[re, im, re, im, ...]` f64s.
pub(crate) fn complex_as_interleaved(values: &[Complex64]) -> &[f64] {
    // SAFETY: `Complex64` is `repr(C)` of two `f64` with no padding, so `values` covers
    // exactly `2 * values.len()` initialized f64s at 8-byte alignment, borrowed for the
    // lifetime of the returned slice.
    unsafe { std::slice::from_raw_parts(values.as_ptr().cast::<f64>(), 2 * values.len()) }
}

/// Reinterpret an interleaved `[re, im, re, im, ...]` vector as complex amplitudes
/// without copying. `raw.len()` must be even.
fn interleaved_into_complex(raw: Vec<f64>) -> Vec<Complex64> {
    let len = raw.len() / 2;
    assert_eq!(raw.len(), 2 * len, "interleaved buffer has an odd length");
    let ptr = Box::into_raw(raw.into_boxed_slice()) as *mut f64;
    // SAFETY: `Complex64` is `repr(C)` of two `f64`, so `len` of them occupy exactly the
    // `2 * len` f64s behind `ptr` at the same 8-byte alignment, and every pair is an
    // initialized value. The boxed slice has length equal to its capacity, so the vector
    // built here owns an allocation whose layout (`16 * len` bytes, align 8) matches the
    // one it will free, and the box was consumed by `into_raw` so nothing else frees it.
    unsafe { Vec::from_raw_parts(ptr.cast::<Complex64>(), len, len) }
}

/// Multiply every amplitude by the real `factor`, in parallel above the statevector
/// threshold so a 27 qubit export does not serialize a 2 GiB pass.
fn scale_in_place(amps: &mut [Complex64], factor: f64, num_qubits: usize) {
    use crate::backend::simd;
    let factor = Complex64::new(factor, 0.0);
    #[cfg(feature = "parallel")]
    if num_qubits >= crate::backend::PARALLEL_THRESHOLD_QUBITS {
        use rayon::prelude::*;
        amps.par_chunks_mut(crate::backend::MIN_PAR_ELEMS)
            .for_each(|chunk| simd::scale_complex_slice(chunk, factor));
        return;
    }
    #[cfg(not(feature = "parallel"))]
    let _ = num_qubits;
    simd::scale_complex_slice(amps, factor);
}

fn driver_err(op: &str, err: impl std::fmt::Display) -> crate::error::PrismError {
    crate::error::PrismError::BackendUnsupported {
        backend: "gpu".to_string(),
        operation: format!("{op}: {err}"),
    }
}

/// Device stabilizer tableau.
///
/// The `xz` and `phase` buffers mirror the CPU tableau layout in
/// [`crate::backend::stabilizer::StabilizerBackend`]:
///
/// - `xz`: `(2n+1)` rows × `2 * num_words` u64s per row. Word ordering per row is
///   X-bits in `[0, num_words)` then Z-bits in `[num_words, 2*num_words)`.
/// - `phase`: `(2n+1)` bytes, one per row (0 = +1, 1 = -1).
///
/// `num_words = ceil(n / 64)`. The scratch row at index `2n` is reserved for
/// measurement computations.
#[derive(Debug)]
pub struct GpuTableau {
    context: Arc<GpuContext>,
    xz: GpuBuffer<u64>,
    phase: GpuBuffer<u8>,
    measure_pivot: GpuBuffer<i32>,
    measure_outcome: GpuBuffer<u8>,
    num_qubits: usize,
    num_words: usize,
}

impl GpuTableau {
    /// Allocate a fresh identity tableau on the device bound to `context`.
    pub fn new(context: Arc<GpuContext>, num_qubits: usize) -> Result<Self> {
        let num_words = num_qubits.div_ceil(64);
        let total_rows = 2 * num_qubits + 1;
        let xz_len = total_rows * 2 * num_words.max(1);
        let phase_len = total_rows;

        let xz = GpuBuffer::<u64>::alloc_zeros(context.device(), xz_len)?;
        let phase = GpuBuffer::<u8>::alloc_zeros(context.device(), phase_len)?;
        let measure_pivot = GpuBuffer::<i32>::alloc_zeros(context.device(), 1)?;
        let measure_outcome = GpuBuffer::<u8>::alloc_zeros(context.device(), 1)?;

        let mut tableau = Self {
            context: context.clone(),
            xz,
            phase,
            measure_pivot,
            measure_outcome,
            num_qubits,
            num_words,
        };
        kernels::stabilizer::launch_set_initial_tableau(&context, &mut tableau)?;
        Ok(tableau)
    }

    pub fn num_qubits(&self) -> usize {
        self.num_qubits
    }

    /// Number of u64 words per bit-packed row half (ceil(n / 64)).
    pub fn num_words(&self) -> usize {
        self.num_words
    }

    pub(crate) fn xz_mut(&mut self) -> &mut GpuBuffer<u64> {
        &mut self.xz
    }

    pub(crate) fn xz_phase_mut(&mut self) -> (&mut GpuBuffer<u64>, &mut GpuBuffer<u8>) {
        (&mut self.xz, &mut self.phase)
    }

    /// The second buffer is the pivot sentinel scratch `stab_measure_find_pivot` writes.
    pub(crate) fn xz_pivot_mut(&mut self) -> (&mut GpuBuffer<u64>, &mut GpuBuffer<i32>) {
        (&mut self.xz, &mut self.measure_pivot)
    }

    /// The third buffer is the one-byte deterministic outcome scratch.
    pub(crate) fn xz_phase_outcome_mut(
        &mut self,
    ) -> (&mut GpuBuffer<u64>, &mut GpuBuffer<u8>, &mut GpuBuffer<u8>) {
        (&mut self.xz, &mut self.phase, &mut self.measure_outcome)
    }

    pub(crate) fn total_rows(&self) -> usize {
        2 * self.num_qubits + 1
    }

    /// Copy the tableau to host in the CPU `StabilizerBackend` layout, reading each phase
    /// byte as `b != 0`.
    pub fn copy_to_host(&self) -> Result<(Vec<u64>, Vec<bool>)> {
        let device = self.context.device();
        let mut xz = vec![0u64; self.xz.len()];
        self.xz.copy_to_host(device, &mut xz)?;
        let mut phase_bytes = vec![0u8; self.phase.len()];
        self.phase.copy_to_host(device, &mut phase_bytes)?;
        let phase = phase_bytes.iter().map(|&b| b != 0).collect();
        Ok((xz, phase))
    }

    /// Upload host `xz` and `phase` buffers, in the `copy_to_host` layout,
    /// into the device tableau.
    pub fn copy_from_host(&mut self, xz: &[u64], phase: &[bool]) -> Result<()> {
        let device = self.context.device();
        self.xz.copy_from_host(device, xz)?;
        let phase_bytes: Vec<u8> = phase.iter().map(|&b| u8::from(b)).collect();
        self.phase.copy_from_host(device, &phase_bytes)?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::error::PrismError;

    #[test]
    fn stub_context_reports_available_false() {
        let ctx = GpuContext::stub_for_tests();
        assert!(ctx.device().is_stub());
    }

    #[test]
    fn state_new_on_stub_returns_unsupported() {
        let ctx = GpuContext::stub_for_tests();
        assert!(matches!(
            GpuState::new(ctx, 4).unwrap_err(),
            PrismError::BackendUnsupported { .. }
        ));
    }

    #[test]
    fn tableau_new_on_stub_returns_unsupported() {
        let ctx = GpuContext::stub_for_tests();
        assert!(matches!(
            GpuTableau::new(ctx, 4).unwrap_err(),
            PrismError::BackendUnsupported { .. }
        ));
    }

    #[test]
    fn min_qubits_default_when_env_unset() {
        // The value is cached per process and the env var may override it, so only
        // plausibility is checked.
        let n = min_qubits();
        assert!(
            (1..=32).contains(&n),
            "implausible gpu crossover threshold: {n}"
        );
    }

    #[test]
    fn stub_vram_available_rejects_cleanly() {
        let ctx = GpuContext::stub_for_tests();
        assert!(matches!(
            ctx.vram_available().unwrap_err(),
            PrismError::BackendUnsupported { .. }
        ));
    }

    #[test]
    fn stub_fits_statevector_rejects_cleanly() {
        let ctx = GpuContext::stub_for_tests();
        assert!(matches!(
            ctx.fits_statevector(4).unwrap_err(),
            PrismError::BackendUnsupported { .. }
        ));
    }

    #[test]
    fn fits_statevector_rejects_overflowing_qubit_counts() {
        let ctx = GpuContext::stub_for_tests();
        // usize::BITS - 4 boundary: `1 << 60` times 16 bytes is already 16 EiB
        // which no GPU has. The function clamps these to `Ok(false)` before
        // touching the device, so even the stub context returns cleanly.
        assert!(!ctx.fits_statevector(128).unwrap());
    }

    #[test]
    fn state_new_rejects_overflowing_qubit_counts() {
        let ctx = GpuContext::stub_for_tests();
        // 63 is the silent case: `2 << 63` wraps to a zero-length buffer that
        // allocates fine and is then written past. 62 already fails at
        // allocation. The bound runs before allocation, so the stub reaches it.
        assert!(matches!(
            GpuState::new(ctx, 63).unwrap_err(),
            PrismError::InvalidParameter { .. }
        ));
    }

    #[test]
    fn fits_statevector_with_scratch_rejects_cleanly_on_stub() {
        let ctx = GpuContext::stub_for_tests();
        assert!(matches!(
            ctx.fits_statevector_with_scratch(4).unwrap_err(),
            PrismError::BackendUnsupported { .. }
        ));
    }

    #[test]
    fn fits_statevector_with_scratch_clamps_overflow_before_device() {
        let ctx = GpuContext::stub_for_tests();
        assert!(!ctx.fits_statevector_with_scratch(128).unwrap());
    }

    #[test]
    fn fits_tableau_rejects_cleanly_on_stub() {
        let ctx = GpuContext::stub_for_tests();
        assert!(matches!(
            ctx.fits_tableau(64).unwrap_err(),
            PrismError::BackendUnsupported { .. }
        ));
    }

    #[test]
    fn fits_tableau_clamps_overflow_before_device() {
        let ctx = GpuContext::stub_for_tests();
        assert!(!ctx.fits_tableau(usize::MAX / 2).unwrap());
    }
}