oxicuda-quantum 0.4.1

Quantum simulation primitives for OxiCUDA — state-vector simulation, Pauli expectation values, VQE/QAOA, density matrices, Trotter-Suzuki, quantum kernels
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
//! On-device GPU validation for the hand-written PTX kernels in
//! [`crate::ptx_kernels`].
//!
//! Each test JIT-compiles a kernel's PTX for the live device's SM version via
//! `Module::from_ptx`, launches it on the real CUDA device through
//! `oxicuda-launch`, copies the results back, and asserts numerical equivalence
//! to the crate's CPU reference (or an independent host re-derivation of the
//! kernel's documented arithmetic). The launch ABI mirrors the `oxicuda-snn`
//! canary: device buffers are passed as their `CUdeviceptr` (`.param .u64`),
//! scalars as the matching Rust scalar type, in the kernel's declared parameter
//! order.
//!
//! ## Oracle strength tiers (honest accounting)
//!
//! * **Crate oracle** (strongest) — compared within FP32 tolerance to a `pub`
//!   CPU function the kernel is meant to mirror:
//!   `statevec_apply_1q` ↔ [`crate::statevec::apply_1q::apply_1q_inplace`],
//!   `statevec_apply_2q` ↔ [`crate::statevec::apply_2q::apply_2q_inplace`],
//!   `statevec_apply_cnot` ↔ [`crate::gates::controlled::apply_cnot`],
//!   `expval_pauli` ↔ [`crate::pauli::expval::expval_z_string`].
//! * **Independent host re-derivation** — the op has no single dedicated crate
//!   function (the diagonal phase / probability reduction is fused into larger
//!   routines on the CPU), so the oracle is an independent Rust re-implementation
//!   of the kernel's *documented* arithmetic: `partial_trace` (reduced-state
//!   diagonal), `trotter_step` (ZZ diagonal phase), `measure_prob` (outcome
//!   probability), `qft_butterfly` (twiddled Cooley-Tukey butterfly). These
//!   still genuinely fail if ptxas miscompiles or the PTX has a wrong index /
//!   constant, because the host code is independent of the JIT-compiled PTX.
//!
//! Every test skips (returns early) when no CUDA device is present, so the suite
//! stays green on CPU-only machines.

use std::sync::Arc;

use num_complex::Complex;

use oxicuda_driver::{Context, Device, Module, Stream};
use oxicuda_launch::{Kernel, LaunchParams};
use oxicuda_memory::DeviceBuffer;

use crate::handle::LcgRng;

type C = Complex<f32>;

// ---------------------------------------------------------------------------
// Shared helpers
// ---------------------------------------------------------------------------

/// A live CUDA context plus the device's SM version, or `None` if unavailable.
///
/// `Context::new` calls `cuCtxCreate`, which both creates the context and makes
/// it current on the calling thread; the returned `Arc<Context>` must be kept
/// alive for the whole test (nextest runs each test in its own process, so a
/// per-test context is fine).
struct GpuFixture {
    ctx: Arc<Context>,
    sm: u32,
}

/// Acquire a GPU fixture, or `None` when no driver / device is present.
fn gpu_fixture() -> Option<GpuFixture> {
    oxicuda_driver::init().ok()?;
    if Device::count().ok()? == 0 {
        return None;
    }
    let Ok(dev) = Device::get(0) else {
        return None;
    };
    let (major, minor) = dev.compute_capability().ok()?;
    let sm = (major * 10 + minor) as u32;
    let ctx = Context::new(&dev).ok()?;
    Some(GpuFixture {
        ctx: Arc::new(ctx),
        sm,
    })
}

/// Relative-with-absolute-floor closeness test for FP32 comparisons.
fn close(a: f32, b: f32, rel: f32, abs: f32) -> bool {
    (a - b).abs() <= rel * a.abs().max(b.abs()) + abs
}

/// Worst (relative, absolute) divergence over two equal-length slices.
fn worst_diff(gpu: &[f32], cpu: &[f32]) -> (f32, f32) {
    let mut worst_abs = 0.0_f32;
    let mut worst_rel = 0.0_f32;
    for (&g, &c) in gpu.iter().zip(cpu.iter()) {
        let a = (g - c).abs();
        if a > worst_abs {
            worst_abs = a;
        }
        let denom = g.abs().max(c.abs());
        if denom > 0.0 {
            let r = a / denom;
            if r > worst_rel {
                worst_rel = r;
            }
        }
    }
    (worst_rel, worst_abs)
}

/// JIT-compile `ptx` and look up `entry`, returning a launchable kernel.
fn load_kernel(ptx: &str, entry: &str) -> Kernel {
    let module = Module::from_ptx(ptx)
        .unwrap_or_else(|e| panic!("PTX JIT compile failed for `{entry}`: {e}"));
    Kernel::from_module(Arc::new(module), entry)
        .unwrap_or_else(|e| panic!("kernel `{entry}` not found in module: {e}"))
}

/// `ceil(n / block)` as a 1-D grid size.
fn grid_1d(n: u32, block: u32) -> u32 {
    n.div_ceil(block)
}

/// Build a random complex state of `dim` amplitudes (re, im split) with `|ψ| = 1`.
fn random_state(dim: usize, rng: &mut LcgRng) -> (Vec<f32>, Vec<f32>) {
    let mut re = vec![0.0_f32; dim];
    let mut im = vec![0.0_f32; dim];
    let mut norm = 0.0_f32;
    for k in 0..dim {
        let r = rng.next_f32() * 2.0 - 1.0;
        let i = rng.next_f32() * 2.0 - 1.0;
        re[k] = r;
        im[k] = i;
        norm += r * r + i * i;
    }
    let inv = 1.0_f32 / norm.sqrt();
    for k in 0..dim {
        re[k] *= inv;
        im[k] *= inv;
    }
    (re, im)
}

/// Re-assemble (re, im) f32 vectors into a complex vector.
fn join(re: &[f32], im: &[f32]) -> Vec<C> {
    re.iter()
        .zip(im.iter())
        .map(|(&r, &i)| C::new(r, i))
        .collect()
}

/// Assert two complex states (held as gpu re/im vs cpu complex) agree.
fn assert_state_close(re_gpu: &[f32], im_gpu: &[f32], cpu: &[C], rel: f32, abs: f32, what: &str) {
    let re_cpu: Vec<f32> = cpu.iter().map(|z| z.re).collect();
    let im_cpu: Vec<f32> = cpu.iter().map(|z| z.im).collect();
    let (rr, ra) = worst_diff(re_gpu, &re_cpu);
    let (ir, ia) = worst_diff(im_gpu, &im_cpu);
    for k in 0..cpu.len() {
        assert!(
            close(re_gpu[k], re_cpu[k], rel, abs) && close(im_gpu[k], im_cpu[k], rel, abs),
            "{what}: amp[{k}] mismatch gpu=({}, {}) cpu=({}, {}) \
             (worst re rel={rr:e} abs={ra:e}; im rel={ir:e} abs={ia:e})",
            re_gpu[k],
            im_gpu[k],
            re_cpu[k],
            im_cpu[k]
        );
    }
}

// ===========================================================================
// 1. statevec_apply_1q  —  CRATE ORACLE (statevec::apply_1q::apply_1q_inplace)
// ===========================================================================

#[test]
fn statevec_apply_1q_matches_cpu() {
    use crate::statevec::apply_1q::apply_1q_inplace;
    use crate::statevec::state::StateVector;

    let Some(fx) = gpu_fixture() else {
        return;
    };

    let n_qubits = 4_usize;
    let dim = 1usize << n_qubits;
    let qubit = 1_usize;
    let mask = 1u32 << qubit;
    let n_pairs = (dim / 2) as u32;

    let mut rng = LcgRng::new(0x1A00_u64 ^ 0xC0FFEE);
    let (re0, im0) = random_state(dim, &mut rng);

    // Arbitrary (non-unitary) 2x2 complex gate — a linear map is enough to
    // distinguish a correct kernel from a wrong index/arithmetic one.
    let g: [f32; 8] = std::array::from_fn(|_| rng.next_f32() * 2.0 - 1.0);
    let gate = [
        [C::new(g[0], g[1]), C::new(g[2], g[3])],
        [C::new(g[4], g[5]), C::new(g[6], g[7])],
    ];

    // ---- CPU reference ----
    let mut sv = StateVector {
        amps: join(&re0, &im0),
        n_qubits,
    };
    apply_1q_inplace(&mut sv, qubit, &gate).expect("cpu apply_1q");

    // ---- GPU ----
    let ptx = crate::ptx_kernels::statevec_apply_1q_ptx(fx.sm);
    let kernel = load_kernel(&ptx, "statevec_apply_1q");
    let stream = Stream::new(&fx.ctx).expect("stream");

    let d_re = DeviceBuffer::<f32>::from_host(&re0).expect("d_re");
    let d_im = DeviceBuffer::<f32>::from_host(&im0).expect("d_im");

    let block = 256_u32;
    let params = LaunchParams::new(grid_1d(n_pairs, block), block);
    kernel
        .launch(
            &params,
            &stream,
            &(
                d_re.as_device_ptr(),
                d_im.as_device_ptr(),
                mask,
                g[0],
                g[1],
                g[2],
                g[3],
                g[4],
                g[5],
                g[6],
                g[7],
                n_pairs,
            ),
        )
        .expect("launch statevec_apply_1q");
    stream.synchronize().expect("sync");

    let mut re_gpu = vec![0.0_f32; dim];
    let mut im_gpu = vec![0.0_f32; dim];
    d_re.copy_to_host(&mut re_gpu).expect("copy re");
    d_im.copy_to_host(&mut im_gpu).expect("copy im");

    assert_state_close(&re_gpu, &im_gpu, &sv.amps, 1e-4, 1e-5, "apply_1q");
}

// ===========================================================================
// 2. statevec_apply_2q  —  CRATE ORACLE (statevec::apply_2q::apply_2q_inplace)
// ===========================================================================

#[test]
fn statevec_apply_2q_matches_cpu() {
    use crate::statevec::apply_2q::apply_2q_inplace;
    use crate::statevec::state::StateVector;

    let Some(fx) = gpu_fixture() else {
        return;
    };

    let n_qubits = 4_usize;
    let dim = 1usize << n_qubits;
    let q0 = 0_usize;
    let q1 = 2_usize;
    let mask0 = 1u32 << q0;
    let mask1 = 1u32 << q1;
    let n_groups = (dim / 4) as u32;

    let mut rng = LcgRng::new(0x2A00_u64 ^ 0xBEEF);
    let (re0, im0) = random_state(dim, &mut rng);

    // Arbitrary 4x4 complex gate (row-major). gate[j][k] is the row-major entry.
    let gre: Vec<f32> = (0..16).map(|_| rng.next_f32() * 2.0 - 1.0).collect();
    let gim: Vec<f32> = (0..16).map(|_| rng.next_f32() * 2.0 - 1.0).collect();
    let mut gate = [[C::new(0.0, 0.0); 4]; 4];
    for j in 0..4 {
        for k in 0..4 {
            gate[j][k] = C::new(gre[j * 4 + k], gim[j * 4 + k]);
        }
    }

    // ---- CPU reference ----
    let mut sv = StateVector {
        amps: join(&re0, &im0),
        n_qubits,
    };
    apply_2q_inplace(&mut sv, q0, q1, &gate).expect("cpu apply_2q");

    // ---- GPU ----
    let ptx = crate::ptx_kernels::statevec_apply_2q_ptx(fx.sm);
    let kernel = load_kernel(&ptx, "statevec_apply_2q");
    let stream = Stream::new(&fx.ctx).expect("stream");

    let d_re = DeviceBuffer::<f32>::from_host(&re0).expect("d_re");
    let d_im = DeviceBuffer::<f32>::from_host(&im0).expect("d_im");
    let d_gre = DeviceBuffer::<f32>::from_host(&gre).expect("d_gre");
    let d_gim = DeviceBuffer::<f32>::from_host(&gim).expect("d_gim");

    let block = 256_u32;
    let params = LaunchParams::new(grid_1d(n_groups, block), block);
    kernel
        .launch(
            &params,
            &stream,
            &(
                d_re.as_device_ptr(),
                d_im.as_device_ptr(),
                mask0,
                mask1,
                n_groups,
                d_gre.as_device_ptr(),
                d_gim.as_device_ptr(),
            ),
        )
        .expect("launch statevec_apply_2q");
    stream.synchronize().expect("sync");

    let mut re_gpu = vec![0.0_f32; dim];
    let mut im_gpu = vec![0.0_f32; dim];
    d_re.copy_to_host(&mut re_gpu).expect("copy re");
    d_im.copy_to_host(&mut im_gpu).expect("copy im");

    assert_state_close(&re_gpu, &im_gpu, &sv.amps, 1e-4, 1e-5, "apply_2q");
}

// ===========================================================================
// 3. statevec_apply_cnot  —  CRATE ORACLE (gates::controlled::apply_cnot)
// ===========================================================================

#[test]
fn statevec_apply_cnot_matches_cpu() {
    use crate::gates::controlled::apply_cnot;
    use crate::statevec::state::StateVector;

    let Some(fx) = gpu_fixture() else {
        return;
    };

    let n_qubits = 3_usize;
    let dim = 1usize << n_qubits;
    let ctrl = 0_usize;
    let tgt = 1_usize;
    let ctrl_mask = 1u32 << ctrl;
    let tgt_mask = 1u32 << tgt;

    let mut rng = LcgRng::new(0xC07_u64 ^ 0x1234);
    let (re0, im0) = random_state(dim, &mut rng);

    // ---- CPU reference ----
    let mut sv = StateVector {
        amps: join(&re0, &im0),
        n_qubits,
    };
    apply_cnot(&mut sv, ctrl, tgt).expect("cpu cnot");

    // ---- GPU (one thread per amplitude) ----
    let ptx = crate::ptx_kernels::statevec_apply_cnot_ptx(fx.sm);
    let kernel = load_kernel(&ptx, "statevec_apply_cnot");
    let stream = Stream::new(&fx.ctx).expect("stream");

    let d_re = DeviceBuffer::<f32>::from_host(&re0).expect("d_re");
    let d_im = DeviceBuffer::<f32>::from_host(&im0).expect("d_im");

    let block = 256_u32;
    let n = dim as u32;
    let params = LaunchParams::new(grid_1d(n, block), block);
    kernel
        .launch(
            &params,
            &stream,
            &(
                d_re.as_device_ptr(),
                d_im.as_device_ptr(),
                ctrl_mask,
                tgt_mask,
                n,
            ),
        )
        .expect("launch statevec_apply_cnot");
    stream.synchronize().expect("sync");

    let mut re_gpu = vec![0.0_f32; dim];
    let mut im_gpu = vec![0.0_f32; dim];
    d_re.copy_to_host(&mut re_gpu).expect("copy re");
    d_im.copy_to_host(&mut im_gpu).expect("copy im");

    // CNOT is an exact permutation of amplitudes — must be bit-exact.
    assert_state_close(&re_gpu, &im_gpu, &sv.amps, 0.0, 0.0, "cnot");
}

// ===========================================================================
// 4. expval_pauli  —  CRATE ORACLE (pauli::expval::expval_z_string)
// ===========================================================================

#[test]
fn expval_pauli_matches_cpu() {
    use crate::pauli::expval::expval_z_string;
    use crate::pauli::pauli_string::PauliOp;
    use crate::statevec::state::StateVector;

    let Some(fx) = gpu_fixture() else {
        return;
    };

    // 256 amplitudes = exactly 8 full warps in one block, so every lane is
    // active through the warp-shuffle reduction (no partial-warp membermask UB).
    let n_qubits = 8_usize;
    let dim = 1usize << n_qubits;
    let z_positions = [0_usize, 3, 5];
    let zmask: u32 = z_positions.iter().fold(0u32, |a, &q| a | (1 << q));

    let mut rng = LcgRng::new(0xECA1_u64);
    let (re0, im0) = random_state(dim, &mut rng);

    // ---- CPU reference ----
    let amps = join(&re0, &im0);
    let sv = StateVector { amps, n_qubits };
    let ops: Vec<PauliOp> = (0..n_qubits)
        .map(|q| {
            if z_positions.contains(&q) {
                PauliOp::Z
            } else {
                PauliOp::I
            }
        })
        .collect();
    let e_cpu = expval_z_string(&sv, &ops).expect("cpu expval");

    // ---- GPU ----
    let ptx = crate::ptx_kernels::expval_pauli_ptx(fx.sm);
    let kernel = load_kernel(&ptx, "expval_pauli");
    let stream = Stream::new(&fx.ctx).expect("stream");

    let d_re = DeviceBuffer::<f32>::from_host(&re0).expect("d_re");
    let d_im = DeviceBuffer::<f32>::from_host(&im0).expect("d_im");
    let d_out = DeviceBuffer::<f32>::from_host(&[0.0_f32]).expect("d_out");

    let block = 256_u32;
    let n = dim as u32;
    let params = LaunchParams::new(grid_1d(n, block), block);
    kernel
        .launch(
            &params,
            &stream,
            &(
                d_re.as_device_ptr(),
                d_im.as_device_ptr(),
                zmask,
                n,
                d_out.as_device_ptr(),
            ),
        )
        .expect("launch expval_pauli");
    stream.synchronize().expect("sync");

    let mut out = vec![0.0_f32];
    d_out.copy_to_host(&mut out).expect("copy out");

    assert!(
        close(out[0], e_cpu, 1e-3, 1e-3),
        "expval_pauli mismatch: gpu={} cpu={}",
        out[0],
        e_cpu
    );
}

// ===========================================================================
// 5. partial_trace  —  INDEPENDENT HOST RE-DERIVATION (reduced-state diagonal)
// ===========================================================================

/// Compact the kept (non-trace) bits of `idx` into a dense reduced index,
/// matching the kernel's COMPACT_LOOP (skip bits where `trace_mask` is set).
fn compact_keep_bits(idx: u32, trace_mask: u32, n_bits: u32) -> usize {
    let mut dense = 0u32;
    let mut pos = 0u32;
    for b in 0..n_bits {
        if (trace_mask >> b) & 1 == 1 {
            continue;
        }
        let bit = (idx >> b) & 1;
        dense |= bit << pos;
        pos += 1;
    }
    dense as usize
}

#[test]
fn partial_trace_matches_host() {
    let Some(fx) = gpu_fixture() else {
        return;
    };

    let n_qubits = 4_u32;
    let dim = 1usize << n_qubits;
    // Trace out qubits 1 and 3; keep qubits 0 and 2.
    let trace_mask: u32 = (1 << 1) | (1 << 3);
    let n_keep = 2_u32;
    let reduced_dim = 1usize << n_keep;

    let mut rng = LcgRng::new(0x9747_u64);
    let (re0, im0) = random_state(dim, &mut rng);

    // ---- Host reference: out[dense] = Σ_{i: keep(i)=dense} |amp_i|^2 ----
    let mut out_host = vec![0.0_f32; reduced_dim];
    for i in 0..dim {
        let dense = compact_keep_bits(i as u32, trace_mask, n_qubits);
        out_host[dense] += re0[i] * re0[i] + im0[i] * im0[i];
    }

    // ---- GPU ----
    let ptx = crate::ptx_kernels::partial_trace_ptx(fx.sm);
    let kernel = load_kernel(&ptx, "partial_trace");
    let stream = Stream::new(&fx.ctx).expect("stream");

    let d_re = DeviceBuffer::<f32>::from_host(&re0).expect("d_re");
    let d_im = DeviceBuffer::<f32>::from_host(&im0).expect("d_im");
    let d_out = DeviceBuffer::<f32>::from_host(&vec![0.0_f32; reduced_dim]).expect("d_out");

    let block = 256_u32;
    let n_total = dim as u32;
    let params = LaunchParams::new(grid_1d(n_total, block), block);
    kernel
        .launch(
            &params,
            &stream,
            &(
                d_re.as_device_ptr(),
                d_im.as_device_ptr(),
                n_total,
                trace_mask,
                n_keep,
                d_out.as_device_ptr(),
            ),
        )
        .expect("launch partial_trace");
    stream.synchronize().expect("sync");

    let mut out_gpu = vec![0.0_f32; reduced_dim];
    d_out.copy_to_host(&mut out_gpu).expect("copy out");

    // Total probability is conserved (sanity) and each reduced diagonal matches.
    let total: f32 = out_gpu.iter().sum();
    assert!(
        (total - 1.0).abs() < 1e-3,
        "partial_trace: reduced diagonal does not sum to 1: {total}"
    );
    let (rel, abs) = worst_diff(&out_gpu, &out_host);
    for k in 0..reduced_dim {
        assert!(
            close(out_gpu[k], out_host[k], 1e-4, 1e-5),
            "partial_trace out[{k}] mismatch: gpu={} host={} (worst rel={rel:e} abs={abs:e})",
            out_gpu[k],
            out_host[k]
        );
    }
}

// ===========================================================================
// 6. trotter_step  —  INDEPENDENT HOST RE-DERIVATION (ZZ diagonal phase)
// ===========================================================================

#[test]
fn trotter_step_matches_host() {
    let Some(fx) = gpu_fixture() else {
        return;
    };

    let n_qubits = 4_usize;
    let dim = 1usize << n_qubits;
    let zz_mask: u32 = (1 << 0) | (1 << 2);
    // Small angle keeps the kernel's truncated cos/sin polynomial within ~1e-4
    // of the exact rotation used by the host oracle.
    let theta = 0.3_f32;

    let mut rng = LcgRng::new(0x70BB_u64);
    let (re0, im0) = random_state(dim, &mut rng);

    // ---- Host reference: amp_i *= exp(±iθ) with sign from the ZZ parity ----
    // exp(-iθ Z⊗Z): even parity (eigenvalue +1) -> exp(-iθ); odd -> exp(+iθ).
    let cos_t = theta.cos();
    let sin_t = theta.sin();
    let mut cpu = join(&re0, &im0);
    for (i, amp) in cpu.iter_mut().enumerate() {
        let parity = (i as u32 & zz_mask).count_ones() & 1;
        let phase = if parity == 1 {
            C::new(cos_t, sin_t)
        } else {
            C::new(cos_t, -sin_t)
        };
        *amp *= phase;
    }

    // ---- GPU ----
    let ptx = crate::ptx_kernels::trotter_step_ptx(fx.sm);
    let kernel = load_kernel(&ptx, "trotter_step");
    let stream = Stream::new(&fx.ctx).expect("stream");

    let d_re = DeviceBuffer::<f32>::from_host(&re0).expect("d_re");
    let d_im = DeviceBuffer::<f32>::from_host(&im0).expect("d_im");

    let block = 256_u32;
    let n = dim as u32;
    let params = LaunchParams::new(grid_1d(n, block), block);
    kernel
        .launch(
            &params,
            &stream,
            &(
                d_re.as_device_ptr(),
                d_im.as_device_ptr(),
                zz_mask,
                theta,
                n,
            ),
        )
        .expect("launch trotter_step");
    stream.synchronize().expect("sync");

    let mut re_gpu = vec![0.0_f32; dim];
    let mut im_gpu = vec![0.0_f32; dim];
    d_re.copy_to_host(&mut re_gpu).expect("copy re");
    d_im.copy_to_host(&mut im_gpu).expect("copy im");

    assert_state_close(&re_gpu, &im_gpu, &cpu, 1e-3, 1e-3, "trotter_step");
}

// ===========================================================================
// 7. measure_prob  —  INDEPENDENT HOST RE-DERIVATION (outcome probability)
// ===========================================================================

#[test]
fn measure_prob_matches_host() {
    let Some(fx) = gpu_fixture() else {
        return;
    };

    // 256 amplitudes = 8 full warps in one block. The measured qubit is bit 2,
    // which VARIES within each warp — so this test specifically exercises the
    // fixed (selp-masked) reduction that no longer diverges the warp.
    let n_qubits = 8_usize;
    let dim = 1usize << n_qubits;
    let qubit = 2_usize;
    let qubit_mask = 1u32 << qubit;
    let outcome = 1_u32;

    let mut rng = LcgRng::new(0x3EA5_u64);
    let (re0, im0) = random_state(dim, &mut rng);

    // ---- Host reference: P(outcome) = Σ_{i: bit==outcome} |amp_i|^2 ----
    let mut p_host = 0.0_f32;
    for i in 0..dim {
        let bit = u32::from((i as u32 & qubit_mask) != 0);
        if bit == outcome {
            p_host += re0[i] * re0[i] + im0[i] * im0[i];
        }
    }

    // ---- GPU ----
    let ptx = crate::ptx_kernels::measure_prob_ptx(fx.sm);
    let kernel = load_kernel(&ptx, "measure_prob");
    let stream = Stream::new(&fx.ctx).expect("stream");

    let d_re = DeviceBuffer::<f32>::from_host(&re0).expect("d_re");
    let d_im = DeviceBuffer::<f32>::from_host(&im0).expect("d_im");
    let d_out = DeviceBuffer::<f32>::from_host(&[0.0_f32]).expect("d_out");

    let block = 256_u32;
    let n = dim as u32;
    let params = LaunchParams::new(grid_1d(n, block), block);
    kernel
        .launch(
            &params,
            &stream,
            &(
                d_re.as_device_ptr(),
                d_im.as_device_ptr(),
                qubit_mask,
                outcome,
                n,
                d_out.as_device_ptr(),
            ),
        )
        .expect("launch measure_prob");
    stream.synchronize().expect("sync");

    let mut out = vec![0.0_f32];
    d_out.copy_to_host(&mut out).expect("copy out");

    assert!(
        close(out[0], p_host, 1e-3, 1e-3),
        "measure_prob mismatch: gpu={} host={}",
        out[0],
        p_host
    );
}

// ===========================================================================
// 8. qft_butterfly  —  INDEPENDENT HOST RE-DERIVATION (twiddled CT butterfly)
// ===========================================================================

#[test]
fn qft_butterfly_matches_host() {
    let Some(fx) = gpu_fixture() else {
        return;
    };

    let n_qubits = 4_usize;
    let dim = 1usize << n_qubits;
    let qubit = 1_usize;
    let mask = 1u32 << qubit;
    let n_pairs = (dim / 2) as u32;
    // Small angle keeps the kernel's truncated cos/sin within ~1e-4 of exact.
    let theta = 0.4_f32;

    let mut rng = LcgRng::new(0x9F70_u64);
    let (re0, im0) = random_state(dim, &mut rng);

    // ---- Host reference: y0=(x0+w·x1)/√2, y1=(x0-w·x1)/√2, w=exp(iθ) ----
    let w = C::new(theta.cos(), theta.sin());
    let inv_sqrt2 = std::f32::consts::FRAC_1_SQRT_2;
    let mut cpu = join(&re0, &im0);
    for i in 0..dim {
        if (i as u32 & mask) != 0 {
            continue;
        }
        let i0 = i;
        let i1 = i | (mask as usize);
        let x0 = cpu[i0];
        let x1 = cpu[i1];
        let wx1 = w * x1;
        cpu[i0] = (x0 + wx1) * inv_sqrt2;
        cpu[i1] = (x0 - wx1) * inv_sqrt2;
    }

    // ---- GPU ----
    let ptx = crate::ptx_kernels::qft_butterfly_ptx(fx.sm);
    let kernel = load_kernel(&ptx, "qft_butterfly");
    let stream = Stream::new(&fx.ctx).expect("stream");

    let d_re = DeviceBuffer::<f32>::from_host(&re0).expect("d_re");
    let d_im = DeviceBuffer::<f32>::from_host(&im0).expect("d_im");

    let block = 256_u32;
    let params = LaunchParams::new(grid_1d(n_pairs, block), block);
    kernel
        .launch(
            &params,
            &stream,
            &(
                d_re.as_device_ptr(),
                d_im.as_device_ptr(),
                mask,
                theta,
                n_pairs,
            ),
        )
        .expect("launch qft_butterfly");
    stream.synchronize().expect("sync");

    let mut re_gpu = vec![0.0_f32; dim];
    let mut im_gpu = vec![0.0_f32; dim];
    d_re.copy_to_host(&mut re_gpu).expect("copy re");
    d_im.copy_to_host(&mut im_gpu).expect("copy im");

    assert_state_close(&re_gpu, &im_gpu, &cpu, 1e-3, 1e-3, "qft_butterfly");
}