prism-q 0.14.1

PRISM-Q: Performance Rust Interoperable Simulator for Quantum
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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
//! Stabilizer tableau kernels. CUDA C source compiled to PTX at runtime, plus launch
//! helpers in Rust.
//!
//! Tableau layout mirrors the CPU `StabilizerBackend`
//! (`src/backend/stabilizer/mod.rs`):
//!
//! - `xz`: `(2n+1)` rows × `2 * num_words` u64 words 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). Bytewise rather than
//!   bit-packed so rowmul phase writes do not require atomic RMW.
//! - Scratch row sits at index `2n` and is used only during measurement.
//!
//! Every entry-point name is prefixed `stab_` so it cannot collide with the dense
//! statevector kernels when both sources are concatenated into a single PTX module.
//!
//! Landed kernels:
//!
//! - `stab_set_initial_tableau`: identity init.
//! - `stab_apply_batch`: batched dispatch of all eleven Clifford gates
//!   (H, S, Sdg, X, Y, Z, SX, SXdg, CX, CZ, SWAP) over a host-provided
//!   op list, one kernel launch per flush.
//! - `stab_rowmul_words`: XOR source row into destination row with
//!   Aaronson-Gottesman phase update.
//! - `stab_measure_find_pivot`, `stab_measure_cascade`,
//!   `stab_measure_fixup`, `stab_measure_deterministic`: on-device Z-basis
//!   measurement. Eliminates the tableau copy-back previously needed per
//!   measure or reset.
//!
//! `stab_apply_batch` uses a one-block-per-row strategy across the full
//! `2n+1`-row tableau. Threads stripe over independent word groups inside the
//! row, which keeps reads and writes local to one row instead of striding the
//! same word across many rows. The cross-word tail stays serial on thread 0,
//! because different ops may still touch different bits in the same packed
//! u64 word.
//!
//! `stab_rowmul_words` launches a single block per call; threads partition
//! the `num_words` word loop and reduce their per-word phase contributions
//! via warp-shuffle plus shared memory.
//!
//! Measurement orchestrates four small kernels: a pivot search with an
//! atomicMin sentinel, a cascade that rowmul's the pivot into every row
//! carrying an X at the target (one block per row, most blocks early-exit),
//! a single-block fixup that moves pivot data into the paired destabilizer
//! and installs the measured Z_q, and a deterministic-branch kernel that
//! serialises rowmul's of stabilisers whose paired destabilisers anticommute
//! with Z_q into the scratch row and reads its phase.

use cudarc::driver::{CudaSlice, LaunchConfig, PushKernelArg};

use crate::error::{PrismError, Result};

use super::super::{GpuContext, GpuTableau};

const BLOCK_SIZE: u32 = 128;

/// Stabilizer CUDA C source. Returned by `kernel_source()` and concatenated into the
/// combined PTX module alongside the dense kernels. No template substitutions needed:
/// all tableau shapes are passed as kernel arguments rather than compile-time constants.
const KERNEL_SOURCE: &str = r#"
// ============================================================================
// Stabilizer tableau kernels
// ============================================================================
//
// CPU reference: src/backend/stabilizer/mod.rs (init, layout) and
// src/backend/stabilizer/kernels/simd.rs (rowmul g-function).
//
// xz is laid out as (2n+1) rows × 2*num_words u64s per row, with X-bits in the
// first num_words words of each row and Z-bits in the second num_words words.
// phase is 2n+1 bytes.

extern "C" __global__ void stab_set_initial_tableau(
    unsigned long long *xz,
    int num_qubits,
    int num_words
) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i >= num_qubits) return;

    int word_idx = i / 64;
    unsigned long long bit = 1ULL << (i % 64);
    int stride = 2 * num_words;

    // Destabilizer row i: X-bit i set in the X-half.
    xz[i * stride + word_idx] = bit;

    // Stabilizer row (num_qubits + i): Z-bit i set in the Z-half.
    xz[(num_qubits + i) * stride + num_words + word_idx] = bit;
}

// ============================================================================
// Word-grouped Clifford dispatch
// ============================================================================
//
// `stab_apply_word_grouped` mirrors the CPU word-group batching pattern in
// `src/backend/stabilizer/kernels/batch.rs`. The host sorts queued ops into
// groups keyed by the target word (and accumulates cross-word 2q gates
// separately). Inside the kernel each block owns one tableau row:
//
//   1. Threads stripe over the word-group list. Each thread loads one target
//      word, applies every op in that group against register-held `(xw, zw)`,
//      then writes the word back. Groups are word-disjoint, so this is safe.
//   2. The block XOR-reduces the per-group phase contributions.
//   3. Thread 0 walks the cross-word list serially. Those ops commute on
//      qubits but may still touch different bits in the same packed word, so
//      serial execution avoids shared-word RMW races.
//   4. Thread 0 writes the final row phase byte once at the end.
//
// Opcodes (kept in sync with ClifOp in src/backend/stabilizer/mod.rs):
//   0  H     1  S    2  Sdg   3  X    4  Y    5  Z    6  SX   7  SXdg
//   8  Cx   9  Cz   10  Swap
//
// Commutation discipline (enforced on the host before the launch, mirrors
// `apply_instructions_word_batch` in the CPU path):
//   - Different word groups always commute on a row (disjoint target words
//     mean disjoint qubits), so they can be applied in word-index order.
//   - A cross-word op commutes with earlier-issued word-group ops only when
//     their qubits are disjoint. The host tracks a per-word "cross-word
//     qubit mask" and issues a partial launch as soon as a newly-enqueued
//     op would violate the invariant.
//
// Layout:
//   group_words[g]     : word index for group g (strictly increasing).
//   group_offsets[g]   : start of group g's ops in ops_flat. Length
//                         num_groups + 1; group_offsets[num_groups] is the
//                         total op count.
//   ops_flat[4*i ..]   : quad (opcode, a, b, pad) for op i. a and b are
//                         absolute qubit indices; the kernel masks them with
//                         63 to get the bit position within the group's word.
//   cross_word_flat    : quad (opcode, a, b, pad) for each cross-word 2q op,
//                         in insertion order.
__device__ __forceinline__ unsigned int stab_reduce_phase_xor(
    unsigned int local_xor,
    unsigned int tid,
    unsigned int bsz,
    unsigned int *warp_xors
) {
    for (int off = 16; off > 0; off /= 2) {
        local_xor ^= __shfl_down_sync(0xffffffffu, local_xor, off);
    }
    unsigned int lane = tid & 31u;
    unsigned int warp = tid >> 5;
    if (lane == 0u) warp_xors[warp] = local_xor;
    __syncthreads();
    if (warp == 0u) {
        unsigned int nwarps = (bsz + 31u) >> 5;
        unsigned int s = (lane < nwarps) ? warp_xors[lane] : 0u;
        for (int off = 16; off > 0; off /= 2) {
            s ^= __shfl_down_sync(0xffffffffu, s, off);
        }
        if (lane == 0u) warp_xors[0] = s;
    }
    __syncthreads();
    return warp_xors[0];
}

extern "C" __global__ void stab_apply_word_grouped(
    unsigned long long *xz,
    unsigned char *phase,
    int num_rows,
    int num_words,
    const unsigned int *group_words,
    const unsigned int *group_offsets,
    const unsigned int *ops_flat,
    int num_groups,
    const unsigned int *cross_word_flat,
    int num_cross_word
) {
    int row = blockIdx.x;
    if (row >= num_rows) return;
    unsigned int tid = threadIdx.x;
    unsigned int bsz = blockDim.x;
    int stride = 2 * num_words;
    unsigned long long *rx = xz + row * stride;
    unsigned long long *rz = rx + num_words;
    unsigned int local_phase_xor = 0u;

    for (int g = (int)tid; g < num_groups; g += (int)bsz) {
        int w = (int)group_words[g];
        unsigned int start = group_offsets[g];
        unsigned int end = group_offsets[g + 1];
        unsigned long long xw = rx[w];
        unsigned long long zw = rz[w];
        unsigned int p = 0u;

        // Poor-man's SGI. When both words are zero, every op in the group
        // reads zero bits, applies identity, and would write zero back.
        // Skip the inner loop and the write entirely. At sparse tableaus
        // (freshly initialised or shallow circuits) this eliminates the
        // bulk of per-row work.
        if ((xw | zw) == 0ULL) {
            continue;
        }

        for (unsigned int i = start; i < end; ++i) {
            unsigned int opcode = ops_flat[i * 4];
            unsigned int a = ops_flat[i * 4 + 1];
            unsigned int b = ops_flat[i * 4 + 2];
            unsigned int abit = a & 63u;
            unsigned long long mask_a = 1ULL << abit;

            switch (opcode) {
                case 0: {
                    unsigned long long x = xw & mask_a;
                    unsigned long long z = zw & mask_a;
                    if (x != 0ULL && z != 0ULL) p ^= 1u;
                    xw = (xw & ~mask_a) | z;
                    zw = (zw & ~mask_a) | x;
                    break;
                }
                case 1: {
                    unsigned long long x = xw & mask_a;
                    unsigned long long z = zw & mask_a;
                    if (x != 0ULL && z != 0ULL) p ^= 1u;
                    zw ^= x;
                    break;
                }
                case 2: {
                    unsigned long long x = xw & mask_a;
                    zw ^= x;
                    unsigned long long z_new = zw & mask_a;
                    if (x != 0ULL && z_new != 0ULL) p ^= 1u;
                    break;
                }
                case 3: {
                    if ((zw & mask_a) != 0ULL) p ^= 1u;
                    break;
                }
                case 4: {
                    unsigned long long x = xw & mask_a;
                    unsigned long long z = zw & mask_a;
                    if ((x ^ z) != 0ULL) p ^= 1u;
                    break;
                }
                case 5: {
                    if ((xw & mask_a) != 0ULL) p ^= 1u;
                    break;
                }
                case 6: {
                    unsigned long long x = xw & mask_a;
                    unsigned long long z = zw & mask_a;
                    if (z != 0ULL && x == 0ULL) p ^= 1u;
                    xw ^= z;
                    break;
                }
                case 7: {
                    unsigned long long x = xw & mask_a;
                    unsigned long long z = zw & mask_a;
                    if (x != 0ULL && z != 0ULL) p ^= 1u;
                    xw ^= z;
                    break;
                }
                case 8: {
                    unsigned int bbit = b & 63u;
                    unsigned long long mask_b = 1ULL << bbit;
                    unsigned long long xa = (xw >> abit) & 1ULL;
                    unsigned long long za = (zw >> abit) & 1ULL;
                    unsigned long long xb = (xw >> bbit) & 1ULL;
                    unsigned long long zb = (zw >> bbit) & 1ULL;
                    if ((xa & zb & (xb ^ za ^ 1ULL)) == 1ULL) p ^= 1u;
                    if (xa == 1ULL) xw ^= mask_b;
                    if (zb == 1ULL) zw ^= mask_a;
                    break;
                }
                case 9: {
                    unsigned int bbit = b & 63u;
                    unsigned long long mask_b = 1ULL << bbit;
                    unsigned long long xa = (xw >> abit) & 1ULL;
                    unsigned long long xb = (xw >> bbit) & 1ULL;
                    unsigned long long za = (zw >> abit) & 1ULL;
                    unsigned long long zb = (zw >> bbit) & 1ULL;
                    if ((xa & xb & (za ^ zb)) == 1ULL) p ^= 1u;
                    if (xb == 1ULL) zw ^= mask_a;
                    if (xa == 1ULL) zw ^= mask_b;
                    break;
                }
                case 10: {
                    unsigned int bbit = b & 63u;
                    unsigned long long mask_b = 1ULL << bbit;
                    unsigned long long xa = (xw >> abit) & 1ULL;
                    unsigned long long xb = (xw >> bbit) & 1ULL;
                    if (xa != xb) { xw ^= mask_a; xw ^= mask_b; }
                    unsigned long long za = (zw >> abit) & 1ULL;
                    unsigned long long zb = (zw >> bbit) & 1ULL;
                    if (za != zb) { zw ^= mask_a; zw ^= mask_b; }
                    break;
                }
                default: break;
            }
        }

        rx[w] = xw;
        rz[w] = zw;
        local_phase_xor ^= p;
    }

    __shared__ unsigned int warp_xors[32];
    unsigned int phase_xor = stab_reduce_phase_xor(local_phase_xor, tid, bsz, warp_xors);
    if (tid != 0u) {
        return;
    }

    unsigned char p = phase[row] ^ (unsigned char)(phase_xor & 1u);
    for (int c = 0; c < num_cross_word; ++c) {
        unsigned int opcode = cross_word_flat[c * 4];
        unsigned int a = cross_word_flat[c * 4 + 1];
        unsigned int b = cross_word_flat[c * 4 + 2];
        int aw = (int)(a >> 6);
        int abit = (int)(a & 63u);
        unsigned long long mask_a = 1ULL << abit;
        int bw = (int)(b >> 6);
        int bbit = (int)(b & 63u);
        unsigned long long mask_b = 1ULL << bbit;
        // Poor-man's SGI for cross-word 2q. When all four target words are
        // zero, neither qubit is active in this row and the op is a no-op.
        if ((rx[aw] | rz[aw] | rx[bw] | rz[bw]) == 0ULL) {
            continue;
        }
        unsigned long long xa = (rx[aw] >> abit) & 1ULL;
        unsigned long long za = (rz[aw] >> abit) & 1ULL;
        unsigned long long xb = (rx[bw] >> bbit) & 1ULL;
        unsigned long long zb = (rz[bw] >> bbit) & 1ULL;
        switch (opcode) {
            case 8: {
                if ((xa & zb & (xb ^ za ^ 1ULL)) == 1ULL) p ^= 1;
                if (xa == 1ULL) rx[bw] ^= mask_b;
                if (zb == 1ULL) rz[aw] ^= mask_a;
                break;
            }
            case 9: {
                if ((xa & xb & (za ^ zb)) == 1ULL) p ^= 1;
                if (xb == 1ULL) rz[aw] ^= mask_a;
                if (xa == 1ULL) rz[bw] ^= mask_b;
                break;
            }
            case 10: {
                if (xa != xb) { rx[aw] ^= mask_a; rx[bw] ^= mask_b; }
                if (za != zb) { rz[aw] ^= mask_a; rz[bw] ^= mask_b; }
                break;
            }
            default: break;
        }
    }

    phase[row] = p;
}

// ============================================================================
// rowmul_words
// ============================================================================
//
// XOR source row bits into destination row and update destination phase by
// the Aaronson-Gottesman g-function. Mirrors the scalar CPU implementation at
// src/backend/stabilizer/kernels/simd.rs:86 exactly:
//
//     let nonzero = (new_x | new_z) & (x1 | z1) & (x2 | z2);
//     let pos = (x1 & z1 & !x2 & z2)
//             | (x1 & !z1 & x2 & z2)
//             | (!x1 & z1 & x2 & !z2);
//     sum += 2 * popcount(pos)
//     sum -= popcount(nonzero)
//
// The per-word sum contributions are aggregated modulo 4 via unsigned wrapping
// arithmetic, matching CPU `u64::wrapping_add` / `wrapping_sub`. Phase update:
//     phase[dst] = ((sum + 2*src_phase + 2*dst_phase) & 3) >= 2.
//
// Launched as a single block per call. Threads partition the word loop by
// `blockDim.x` stride. Reduction proceeds in two stages:
//   1. Per-warp via __shfl_down_sync.
//   2. Across warps via __shared__ memory + a single-warp reduction.
// No cross-block reduction needed; measurement cascades issue one kernel
// launch per (src, dst) pair, so each rowmul is a distinct launch.
// Device helper shared by `stab_rowmul_words`, `stab_measure_cascade`, and
// `stab_measure_deterministic`. XORs `src_row` into `dst_row` word by word
// and computes the g-function phase contribution. Returns the contribution
// modulo 4 on every thread (broadcast via `warp_sums[0]`). Callers supply a
// 32-slot __shared__ buffer and handle the final phase byte update.
__device__ __forceinline__ unsigned long long stab_rowmul_block(
    unsigned long long *xz,
    int num_words,
    int src_row,
    int dst_row,
    int tid,
    int bsz,
    unsigned long long *warp_sums
) {
    int stride = 2 * num_words;
    unsigned long long *src_x = xz + src_row * stride;
    unsigned long long *src_z = src_x + num_words;
    unsigned long long *dst_x = xz + dst_row * stride;
    unsigned long long *dst_z = dst_x + num_words;

    unsigned long long local_sum = 0ULL;
    for (int w = tid; w < num_words; w += bsz) {
        unsigned long long x1 = src_x[w];
        unsigned long long z1 = src_z[w];
        unsigned long long x2 = dst_x[w];
        unsigned long long z2 = dst_z[w];
        unsigned long long new_x = x1 ^ x2;
        unsigned long long new_z = z1 ^ z2;
        dst_x[w] = new_x;
        dst_z[w] = new_z;

        if ((x1 | z1 | x2 | z2) != 0ULL) {
            unsigned long long nonzero = (new_x | new_z) & (x1 | z1) & (x2 | z2);
            unsigned long long pos = (x1 & z1 & ~x2 & z2)
                                   | (x1 & ~z1 & x2 & z2)
                                   | (~x1 & z1 & x2 & ~z2);
            local_sum += 2ULL * (unsigned long long)__popcll((long long)pos);
            local_sum -= (unsigned long long)__popcll((long long)nonzero);
        }
    }

    for (int off = 16; off > 0; off /= 2) {
        local_sum += __shfl_down_sync(0xffffffffu, local_sum, off);
    }
    int lane = tid & 31;
    int warp = tid >> 5;
    if (lane == 0) warp_sums[warp] = local_sum;
    __syncthreads();
    if (warp == 0) {
        int nwarps = (bsz + 31) >> 5;
        unsigned long long s = (lane < nwarps) ? warp_sums[lane] : 0ULL;
        for (int off = 16; off > 0; off /= 2) {
            s += __shfl_down_sync(0xffffffffu, s, off);
        }
        if (lane == 0) warp_sums[0] = s;
    }
    __syncthreads();
    return warp_sums[0];
}

extern "C" __global__ void stab_rowmul_words(
    unsigned long long *xz,
    unsigned char *phase,
    int num_words,
    int src_row,
    int dst_row
) {
    int tid = threadIdx.x;
    int bsz = blockDim.x;
    __shared__ unsigned long long warp_sums[32];
    unsigned long long sum =
        stab_rowmul_block(xz, num_words, src_row, dst_row, tid, bsz, warp_sums);
    if (tid == 0) {
        unsigned long long initial = 2ULL * (unsigned long long)phase[src_row]
                                    + 2ULL * (unsigned long long)phase[dst_row];
        unsigned long long total = (sum + initial) & 3ULL;
        phase[dst_row] = (total >= 2ULL) ? 1 : 0;
    }
}

// ============================================================================
// On-device Z-basis measurement
// ============================================================================
//
// Host orchestration (src/backend/stabilizer/mod.rs:apply_measure_gpu):
//   1. Launch stab_measure_find_pivot, dtoh the 1 i32 sentinel.
//   2. If pivot < 2n (random branch):
//        a. Launch stab_measure_cascade (one block per row, 2n blocks).
//        b. Launch stab_measure_fixup (single block).
//      else (deterministic branch):
//        a. Launch stab_measure_deterministic (single block).
//        b. dtoh the 1 u8 outcome.
//
// Random-branch RNG is picked on the host so no cuRAND wiring is required.
// Reset reuses the same path and queues an X onto the Clifford batch queue
// when the outcome is 1.

// Scan stabilizer rows n..2n for the minimum row index with an X-bit at
// `target`. `out_pivot` must be initialised to a sentinel >= 2n on the host;
// atomicMin leaves the sentinel untouched when no row carries the X-bit.
extern "C" __global__ void stab_measure_find_pivot(
    const unsigned long long *xz,
    int num_qubits,
    int num_words,
    int target,
    int *out_pivot
) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i >= num_qubits) return;
    int row = num_qubits + i;
    int stride = 2 * num_words;
    int word_idx = target / 64;
    unsigned long long bit = 1ULL << (target % 64);
    if ((xz[row * stride + word_idx] & bit) != 0ULL) {
        atomicMin(out_pivot, row);
    }
}

// Rowmul `pivot_row` into every row in [0, 2n) that carries an X-bit at
// `target`, skipping `pivot_row` itself. One block per candidate row; blocks
// that do not carry the X-bit early-exit. `pivot_row` is read-only throughout
// (the fixup kernel runs afterwards and is the only writer).
extern "C" __global__ void stab_measure_cascade(
    unsigned long long *xz,
    unsigned char *phase,
    int num_qubits,
    int num_words,
    int target,
    int pivot_row
) {
    int r = blockIdx.x;
    int total_rows = 2 * num_qubits;
    if (r >= total_rows) return;
    if (r == pivot_row) return;

    int stride = 2 * num_words;
    int word_idx = target / 64;
    unsigned long long bit = 1ULL << (target % 64);
    if ((xz[r * stride + word_idx] & bit) == 0ULL) return;

    int tid = threadIdx.x;
    int bsz = blockDim.x;
    __shared__ unsigned long long warp_sums[32];
    unsigned long long sum =
        stab_rowmul_block(xz, num_words, pivot_row, r, tid, bsz, warp_sums);
    if (tid == 0) {
        unsigned long long initial = 2ULL * (unsigned long long)phase[pivot_row]
                                    + 2ULL * (unsigned long long)phase[r];
        unsigned long long total = (sum + initial) & 3ULL;
        phase[r] = (total >= 2ULL) ? 1 : 0;
    }
}

// After the cascade: copy pivot row into the paired destabiliser row
// (`pivot_row - num_qubits`), zero the pivot row, set its Z-bit at `target`,
// and store the measured outcome in the pivot's phase. Single-block launch
// so thread 0 can hand-off the pre-zero phase value via shared memory.
extern "C" __global__ void stab_measure_fixup(
    unsigned long long *xz,
    unsigned char *phase,
    int num_qubits,
    int num_words,
    int target,
    int pivot_row,
    unsigned char outcome
) {
    int tid = threadIdx.x;
    int bsz = blockDim.x;
    int stride = 2 * num_words;
    int destab_row = pivot_row - num_qubits;

    __shared__ unsigned char pivot_phase_saved;
    if (tid == 0) pivot_phase_saved = phase[pivot_row];
    __syncthreads();

    for (int w = tid; w < stride; w += bsz) {
        unsigned long long pv = xz[pivot_row * stride + w];
        xz[destab_row * stride + w] = pv;
        xz[pivot_row * stride + w] = 0ULL;
    }
    __syncthreads();

    if (tid == 0) {
        phase[destab_row] = pivot_phase_saved;
        phase[pivot_row] = outcome;
        int word_idx = target / 64;
        unsigned long long bit = 1ULL << (target % 64);
        xz[pivot_row * stride + num_words + word_idx] = bit;
    }
}

// Deterministic branch: build the scratch row at index 2n by serially
// rowmul'ing stabiliser row n+i into it whenever destabiliser row i carries
// an X-bit at `target`. Single block; the serial loop keeps scratch's phase
// consistent across rowmul iterations. Thread 0 writes the scratch phase to
// `out_outcome` on exit.
extern "C" __global__ void stab_measure_deterministic(
    unsigned long long *xz,
    unsigned char *phase,
    int num_qubits,
    int num_words,
    int target,
    unsigned char *out_outcome
) {
    int tid = threadIdx.x;
    int bsz = blockDim.x;
    int scratch = 2 * num_qubits;
    int stride = 2 * num_words;

    for (int w = tid; w < stride; w += bsz) {
        xz[scratch * stride + w] = 0ULL;
    }
    if (tid == 0) phase[scratch] = 0;
    __syncthreads();

    int word_idx = target / 64;
    unsigned long long bit = 1ULL << (target % 64);
    __shared__ unsigned long long warp_sums[32];

    for (int i = 0; i < num_qubits; ++i) {
        if ((xz[i * stride + word_idx] & bit) == 0ULL) continue;
        int src_row = num_qubits + i;
        unsigned long long sum =
            stab_rowmul_block(xz, num_words, src_row, scratch, tid, bsz, warp_sums);
        if (tid == 0) {
            unsigned long long initial = 2ULL * (unsigned long long)phase[src_row]
                                        + 2ULL * (unsigned long long)phase[scratch];
            unsigned long long total = (sum + initial) & 3ULL;
            phase[scratch] = (total >= 2ULL) ? 1 : 0;
        }
        __syncthreads();
    }

    if (tid == 0) *out_outcome = phase[scratch];
}
"#;

/// Return the stabilizer CUDA C source for concatenation into the shared PTX module.
pub(crate) fn kernel_source() -> String {
    KERNEL_SOURCE.to_string()
}

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

fn launch_limit_err(op: &str, name: &str, value: usize, limit: &str) -> PrismError {
    PrismError::BackendUnsupported {
        backend: "gpu".to_string(),
        operation: format!("{op}: {name}={value} exceeds {limit} kernel limit"),
    }
}

fn require_i32(op: &str, name: &str, value: usize) -> Result<i32> {
    i32::try_from(value).map_err(|_| launch_limit_err(op, name, value, "i32"))
}

fn require_u32(op: &str, name: &str, value: usize) -> Result<u32> {
    u32::try_from(value).map_err(|_| launch_limit_err(op, name, value, "u32"))
}

fn div_ceil_grid(op: &str, name: &str, value: usize, block: u32) -> Result<u32> {
    Ok(require_u32(op, name, value)?.div_ceil(block).max(1))
}

/// Initialise a freshly-allocated `GpuTableau` to the identity tableau: destabilizer
/// rows are X_i, stabilizer rows are Z_i, scratch row is all zero, phase is all zero.
///
/// Assumes `xz` and `phase` were allocated via `GpuBuffer::alloc_zeros` (so everything
/// else is already zero); this kernel only writes the identity bits.
pub(crate) fn launch_set_initial_tableau(ctx: &GpuContext, tableau: &mut GpuTableau) -> Result<()> {
    let device = ctx.device();
    let stream = device.stream()?;
    let func = device.function("stab_set_initial_tableau")?;

    let n_usize = tableau.num_qubits();
    if n_usize == 0 {
        return Ok(());
    }
    let n = require_i32("stab_set_initial_tableau", "num_qubits", n_usize)?;
    let nw = require_i32("stab_set_initial_tableau", "num_words", tableau.num_words())?;
    let blocks = div_ceil_grid(
        "stab_set_initial_tableau",
        "num_qubits",
        n_usize,
        BLOCK_SIZE,
    )?;
    let cfg = LaunchConfig {
        grid_dim: (blocks, 1, 1),
        block_dim: (BLOCK_SIZE, 1, 1),
        shared_mem_bytes: 0,
    };

    let mut builder = stream.launch_builder(&func);
    let xz = tableau.xz_mut().raw_mut();
    builder.arg(xz).arg(&n).arg(&nw);
    // SAFETY: kernel signature is (u64*, i32, i32); xz buffer is at least
    // (2n+1) * 2 * num_words u64s and each thread writes two disjoint words.
    unsafe {
        builder
            .launch(cfg)
            .map_err(|e| launch_err("stab_set_initial_tableau", e))?;
    }
    Ok(())
}

/// Clifford opcodes consumed by `stab_apply_batch`. Values are part of the ABI
/// between the host queue and the batch kernel and must stay in sync with the
/// switch inside the kernel source.
pub(crate) mod op {
    pub const H: u32 = 0;
    pub const S: u32 = 1;
    pub const SDG: u32 = 2;
    pub const X: u32 = 3;
    pub const Y: u32 = 4;
    pub const Z: u32 = 5;
    pub const SX: u32 = 6;
    pub const SXDG: u32 = 7;
    pub const CX: u32 = 8;
    pub const CZ: u32 = 9;
    pub const SWAP: u32 = 10;
}

/// Number of u32 slots per queued op: `[opcode, a, b, pad]`.
pub(crate) const CLIFOP_STRIDE: usize = 4;

const ZERO_U32: [u32; 1] = [0];

/// Apply a batch of queued Clifford ops to the device tableau in a single
/// launch.
///
/// `ops` is a flat `u32` buffer of length `CLIFOP_STRIDE * num_ops` laid out
/// as `[opcode, a, b, pad]` quads. Opcodes are the constants in [`op`]. The
/// kernel maps one block to each tableau row, parallelises the disjoint
/// same-word groups within that row, and leaves the cross-word tail serial
/// on thread 0 to avoid shared-word races. Apply a flat op list to the device
/// tableau via the word-grouped kernel.
///
/// Host-side, this streams through `ops` (quads `[opcode, a, b, pad]`),
/// sorting into word groups keyed by target-word plus a cross-word 2q list.
/// Conflicts between a newly-enqueued op and the running cross-word qubit
/// set trigger a partial launch, mirroring the CPU `flush_all_with_cross_word`
/// discipline in `src/backend/stabilizer/kernels/batch.rs`. The kernel then
/// amortises memory traffic across every op in a group: one `rx[w]`/`rz[w]`
/// read and one write per thread per group regardless of how many ops the
/// group contains.
pub(crate) fn launch_clifford_batch(
    ctx: &GpuContext,
    tableau: &mut GpuTableau,
    ops: &[u32],
    scratch: &mut CliffordBatchScratch,
) -> Result<()> {
    if ops.is_empty() {
        return Ok(());
    }
    debug_assert!(
        ops.len() % CLIFOP_STRIDE == 0,
        "ClifOp buffer length must be a multiple of {CLIFOP_STRIDE}"
    );
    let num_rows = tableau.total_rows();
    let num_words = tableau.num_words();
    if num_rows == 0 || num_words == 0 {
        return Ok(());
    }
    scratch.launch_ops(ctx, tableau, ops)
}

/// Reusable host and device scratch for GPU Clifford batch launches.
#[derive(Default)]
pub(crate) struct CliffordBatchScratch {
    num_words: usize,
    /// Per-word queued ops, flat `[opcode, a, b, pad]` quads.
    per_word_ops: Vec<Vec<u32>>,
    /// Cross-word 2q ops, flat quads in insertion order.
    cross_word: Vec<u32>,
    /// Bitmask per word of qubits already touched by a cross-word op in the
    /// current launch window. A new 1q or same-word 2q gate on a qubit in
    /// this mask forces a flush to preserve ingestion order.
    cross_word_qubits: Vec<u64>,
    group_words: Vec<u32>,
    group_offsets: Vec<u32>,
    ops_flat: Vec<u32>,
    group_words_dev: Option<CudaSlice<u32>>,
    group_offsets_dev: Option<CudaSlice<u32>>,
    ops_dev: Option<CudaSlice<u32>>,
    cross_dev: Option<CudaSlice<u32>>,
}

impl CliffordBatchScratch {
    pub(crate) fn clear(&mut self) {
        let used = self.num_words.min(self.per_word_ops.len());
        for v in &mut self.per_word_ops[..used] {
            v.clear();
        }
        self.cross_word.clear();
        let cross_used = self.cross_word_qubits.len().min(self.num_words);
        self.cross_word_qubits[..cross_used].fill(0);
        self.group_words.clear();
        self.group_offsets.clear();
        self.ops_flat.clear();
    }

    fn prepare(&mut self, num_words: usize) {
        self.num_words = num_words;
        if self.per_word_ops.len() < num_words {
            self.per_word_ops.resize_with(num_words, Vec::new);
        } else if self.per_word_ops.len() > num_words {
            self.per_word_ops.truncate(num_words);
        }
        self.cross_word_qubits.resize(num_words, 0);
        self.clear();
    }

    fn is_empty(&self) -> bool {
        self.cross_word.is_empty()
            && self.per_word_ops[..self.num_words]
                .iter()
                .all(|v| v.is_empty())
    }

    fn would_conflict(&self, opcode: u32, a: u32, b: u32) -> bool {
        if opcode <= op::SXDG {
            let w = (a as usize) >> 6;
            let bit = 1u64 << (a & 63);
            self.cross_word_qubits[w] & bit != 0
        } else {
            let aw = (a as usize) >> 6;
            let bw = (b as usize) >> 6;
            let abit = 1u64 << (a & 63);
            let bbit = 1u64 << (b & 63);
            if aw == bw {
                let bits = abit | bbit;
                self.cross_word_qubits[aw] & bits != 0
            } else {
                self.cross_word_qubits[aw] & abit != 0 || self.cross_word_qubits[bw] & bbit != 0
            }
        }
    }

    fn push(&mut self, opcode: u32, a: u32, b: u32) {
        if opcode <= op::SXDG {
            let w = (a as usize) >> 6;
            self.per_word_ops[w].extend_from_slice(&[opcode, a, b, 0]);
        } else {
            let aw = (a as usize) >> 6;
            let bw = (b as usize) >> 6;
            if aw == bw {
                self.per_word_ops[aw].extend_from_slice(&[opcode, a, b, 0]);
            } else {
                self.cross_word.extend_from_slice(&[opcode, a, b, 0]);
                self.cross_word_qubits[aw] |= 1u64 << (a & 63);
                self.cross_word_qubits[bw] |= 1u64 << (b & 63);
            }
        }
    }

    fn launch_pending(&mut self, ctx: &GpuContext, tableau: &mut GpuTableau) -> Result<()> {
        if self.is_empty() {
            return Ok(());
        }

        self.group_words.clear();
        self.group_offsets.clear();
        self.ops_flat.clear();
        self.group_offsets.push(0);
        for (w, v) in self.per_word_ops[..self.num_words].iter().enumerate() {
            if v.is_empty() {
                continue;
            }
            debug_assert!(v.len() % CLIFOP_STRIDE == 0);
            self.group_words
                .push(require_u32("stab_apply_word_grouped", "group_word", w)?);
            self.ops_flat.extend_from_slice(v);
            let offset = self.ops_flat.len() / CLIFOP_STRIDE;
            self.group_offsets.push(require_u32(
                "stab_apply_word_grouped",
                "group_offset",
                offset,
            )?);
        }

        let num_groups = self.group_words.len();
        let num_cross_word = self.cross_word.len() / CLIFOP_STRIDE;
        launch_word_grouped_kernel(ctx, tableau, self, num_groups, num_cross_word)?;
        self.clear();
        Ok(())
    }

    fn launch_ops(
        &mut self,
        ctx: &GpuContext,
        tableau: &mut GpuTableau,
        ops: &[u32],
    ) -> Result<()> {
        self.prepare(tableau.num_words());
        for chunk in ops.chunks_exact(CLIFOP_STRIDE) {
            let opcode = chunk[0];
            let a = chunk[1];
            let b = chunk[2];
            if self.would_conflict(opcode, a, b) {
                self.launch_pending(ctx, tableau)?;
            }
            self.push(opcode, a, b);
        }
        self.launch_pending(ctx, tableau)
    }
}

#[allow(clippy::too_many_arguments)]
fn launch_word_grouped_kernel(
    ctx: &GpuContext,
    tableau: &mut GpuTableau,
    scratch: &mut CliffordBatchScratch,
    num_groups: usize,
    num_cross_word: usize,
) -> Result<()> {
    if num_groups == 0 && num_cross_word == 0 {
        return Ok(());
    }
    let num_rows_usize = tableau.total_rows();
    if num_rows_usize == 0 {
        return Ok(());
    }
    let num_rows = require_i32("stab_apply_word_grouped", "num_rows", num_rows_usize)?;
    let num_words_i = require_i32("stab_apply_word_grouped", "num_words", tableau.num_words())?;

    let device = ctx.device();
    let stream = device.stream()?;
    let func = device.function("stab_apply_word_grouped")?;

    let group_words_src: &[u32] = if scratch.group_words.is_empty() {
        &ZERO_U32
    } else {
        &scratch.group_words
    };
    let group_offsets_src: &[u32] = if scratch.group_offsets.is_empty() {
        &ZERO_U32
    } else {
        &scratch.group_offsets
    };
    let ops_src: &[u32] = if scratch.ops_flat.is_empty() {
        &ZERO_U32
    } else {
        &scratch.ops_flat
    };
    let cross_src: &[u32] = if scratch.cross_word.is_empty() {
        &ZERO_U32
    } else {
        &scratch.cross_word
    };

    let ensure_u32_buffer =
        |slot: &mut Option<CudaSlice<u32>>, len: usize, op: &str| -> Result<()> {
            let needed = len.max(1);
            if slot.as_ref().map_or(true, |buf| buf.len() < needed) {
                *slot = Some(
                    stream
                        .alloc_zeros::<u32>(needed)
                        .map_err(|e| launch_err(op, e))?,
                );
            }
            Ok(())
        };
    ensure_u32_buffer(
        &mut scratch.group_words_dev,
        group_words_src.len(),
        "alloc group_words",
    )?;
    ensure_u32_buffer(
        &mut scratch.group_offsets_dev,
        group_offsets_src.len(),
        "alloc group_offsets",
    )?;
    ensure_u32_buffer(&mut scratch.ops_dev, ops_src.len(), "alloc ops_flat")?;
    ensure_u32_buffer(&mut scratch.cross_dev, cross_src.len(), "alloc cross_word")?;

    {
        let dev = scratch
            .group_words_dev
            .as_mut()
            .expect("group_words_dev allocated above");
        let mut view = dev.slice_mut(0..group_words_src.len());
        stream
            .memcpy_htod(group_words_src, &mut view)
            .map_err(|e| launch_err("upload group_words", e))?;
    }
    {
        let dev = scratch
            .group_offsets_dev
            .as_mut()
            .expect("group_offsets_dev allocated above");
        let mut view = dev.slice_mut(0..group_offsets_src.len());
        stream
            .memcpy_htod(group_offsets_src, &mut view)
            .map_err(|e| launch_err("upload group_offsets", e))?;
    }
    {
        let dev = scratch.ops_dev.as_mut().expect("ops_dev allocated above");
        let mut view = dev.slice_mut(0..ops_src.len());
        stream
            .memcpy_htod(ops_src, &mut view)
            .map_err(|e| launch_err("upload ops_flat", e))?;
    }
    {
        let dev = scratch
            .cross_dev
            .as_mut()
            .expect("cross_dev allocated above");
        let mut view = dev.slice_mut(0..cross_src.len());
        stream
            .memcpy_htod(cross_src, &mut view)
            .map_err(|e| launch_err("upload cross_word", e))?;
    }

    let num_groups_i = require_i32("stab_apply_word_grouped", "num_groups", num_groups)?;
    let num_cross_word_i =
        require_i32("stab_apply_word_grouped", "num_cross_word", num_cross_word)?;
    let block_threads = num_groups
        .next_power_of_two()
        .clamp(32, BLOCK_SIZE as usize) as u32;
    let num_rows_grid = require_u32("stab_apply_word_grouped", "num_rows", num_rows_usize)?;
    let cfg = LaunchConfig {
        grid_dim: (num_rows_grid, 1, 1),
        block_dim: (block_threads, 1, 1),
        shared_mem_bytes: 0,
    };

    let group_words_dev = scratch
        .group_words_dev
        .as_ref()
        .expect("group_words_dev uploaded above")
        .slice(0..group_words_src.len());
    let group_offsets_dev = scratch
        .group_offsets_dev
        .as_ref()
        .expect("group_offsets_dev uploaded above")
        .slice(0..group_offsets_src.len());
    let ops_dev = scratch
        .ops_dev
        .as_ref()
        .expect("ops_dev uploaded above")
        .slice(0..ops_src.len());
    let cross_dev = scratch
        .cross_dev
        .as_ref()
        .expect("cross_dev uploaded above")
        .slice(0..cross_src.len());

    let mut builder = stream.launch_builder(&func);
    let (xz_buf, phase_buf) = tableau.xz_phase_mut();
    let xz = xz_buf.raw_mut();
    let phase = phase_buf.raw_mut();
    builder
        .arg(xz)
        .arg(phase)
        .arg(&num_rows)
        .arg(&num_words_i)
        .arg(&group_words_dev)
        .arg(&group_offsets_dev)
        .arg(&ops_dev)
        .arg(&num_groups_i)
        .arg(&cross_dev)
        .arg(&num_cross_word_i);
    // SAFETY: signature matches the kernel declaration. Each block owns one
    // row. Threads stripe over word-disjoint groups within that row, and
    // thread 0 alone handles the cross-word tail plus final phase write.
    unsafe {
        builder
            .launch(cfg)
            .map_err(|e| launch_err("stab_apply_word_grouped", e))?;
    }
    Ok(())
}

/// Block size for `stab_rowmul_words`. Chosen so a single warp-shuffle round
/// followed by one shared-memory reduction covers every supported num_words value
/// (≤ 5000 qubits ⇒ num_words ≤ 79 ⇒ one thread per word fits in a block).
const ROWMUL_BLOCK_SIZE: u32 = 128;

/// XOR `src_row` into `dst_row` and update `dst_row`'s phase per the
/// Aaronson-Gottesman g-function. Launched as a single block per call.
pub(crate) fn launch_rowmul_words(
    ctx: &GpuContext,
    tableau: &mut GpuTableau,
    src_row: usize,
    dst_row: usize,
) -> Result<()> {
    let device = ctx.device();
    let stream = device.stream()?;
    let func = device.function("stab_rowmul_words")?;

    let nw_usize = tableau.num_words();
    if nw_usize == 0 {
        return Ok(());
    }
    let nw = require_i32("stab_rowmul_words", "num_words", nw_usize)?;
    let src_i = require_i32("stab_rowmul_words", "src_row", src_row)?;
    let dst_i = require_i32("stab_rowmul_words", "dst_row", dst_row)?;
    let cfg = LaunchConfig {
        grid_dim: (1, 1, 1),
        block_dim: (ROWMUL_BLOCK_SIZE, 1, 1),
        shared_mem_bytes: 0,
    };

    let mut builder = stream.launch_builder(&func);
    let (xz_buf, phase_buf) = tableau.xz_phase_mut();
    let xz = xz_buf.raw_mut();
    let phase = phase_buf.raw_mut();
    builder.arg(xz).arg(phase).arg(&nw).arg(&src_i).arg(&dst_i);
    // SAFETY: signature (u64*, u8*, i32, i32, i32); single block operates on
    // one (src, dst) row pair. All threads of the block write to disjoint
    // words of dst_row and a single phase byte; no inter-block hazard.
    unsafe {
        builder
            .launch(cfg)
            .map_err(|e| launch_err("stab_rowmul_words", e))?;
    }
    Ok(())
}

/// Block size for the measurement kernels that use one block per row
/// (`stab_measure_cascade`) and the single-block measurement kernels
/// (`stab_measure_fixup`, `stab_measure_deterministic`). Chosen so one warp-
/// shuffle round plus one shared-memory reduction cover every num_words
/// encountered in practice (≤ 5000 qubits ⇒ num_words ≤ 79).
const MEASURE_BLOCK_SIZE: u32 = 128;

/// Scan stabilizer rows `n..2n` for the minimum row index whose X-bit at
/// `target` is set. Returns `Some(row)` when a pivot exists (random branch),
/// `None` when every stabilizer commutes with `Z_target` (deterministic
/// branch). One i32 d2h roundtrip on the sentinel.
pub(crate) fn launch_measure_find_pivot(
    ctx: &GpuContext,
    tableau: &mut GpuTableau,
    target: usize,
) -> Result<Option<usize>> {
    let n = tableau.num_qubits();
    if n == 0 {
        return Ok(None);
    }
    let device = ctx.device();
    let stream = device.stream()?;
    let func = device.function("stab_measure_find_pivot")?;

    let sentinel = require_i32(
        "stab_measure_find_pivot",
        "sentinel",
        2usize.saturating_mul(n),
    )?;
    let num_qubits_i = require_i32("stab_measure_find_pivot", "num_qubits", n)?;
    let nw = require_i32("stab_measure_find_pivot", "num_words", tableau.num_words())?;
    let target_i = require_i32("stab_measure_find_pivot", "target", target)?;
    let blocks = div_ceil_grid(
        "stab_measure_find_pivot",
        "num_qubits",
        n,
        MEASURE_BLOCK_SIZE,
    )?;
    let cfg = LaunchConfig {
        grid_dim: (blocks, 1, 1),
        block_dim: (MEASURE_BLOCK_SIZE, 1, 1),
        shared_mem_bytes: 0,
    };

    let mut host_pivot = [0_i32; 1];
    {
        let (xz_buf, pivot_buf) = tableau.xz_pivot_mut();
        let xz = xz_buf.raw_mut();
        let out_pivot = pivot_buf.raw_mut();
        stream
            .memcpy_htod(&[sentinel], out_pivot)
            .map_err(|e| launch_err("reset find_pivot sentinel", e))?;
        let mut builder = stream.launch_builder(&func);
        builder
            .arg(xz)
            .arg(&num_qubits_i)
            .arg(&nw)
            .arg(&target_i)
            .arg(&mut *out_pivot);
        // SAFETY: signature (const u64*, i32, i32, i32, int*). The kernel only
        // reads xz at (row, target-word) positions and atomicMin's into
        // out_pivot.
        unsafe {
            builder
                .launch(cfg)
                .map_err(|e| launch_err("stab_measure_find_pivot", e))?;
        }
        stream
            .memcpy_dtoh(out_pivot, &mut host_pivot)
            .map_err(|e| launch_err("find_pivot dtoh", e))?;
    }
    if host_pivot[0] >= sentinel {
        Ok(None)
    } else {
        Ok(Some(host_pivot[0] as usize))
    }
}

/// Rowmul the pivot row into every non-pivot row that carries an X-bit at
/// `target`. One block per non-scratch row; blocks not participating in the
/// cascade early-exit with negligible driver overhead.
pub(crate) fn launch_measure_cascade(
    ctx: &GpuContext,
    tableau: &mut GpuTableau,
    target: usize,
    pivot_row: usize,
) -> Result<()> {
    let n = tableau.num_qubits();
    if n == 0 {
        return Ok(());
    }
    let device = ctx.device();
    let stream = device.stream()?;
    let func = device.function("stab_measure_cascade")?;

    let num_qubits_i = require_i32("stab_measure_cascade", "num_qubits", n)?;
    let nw = require_i32("stab_measure_cascade", "num_words", tableau.num_words())?;
    let target_i = require_i32("stab_measure_cascade", "target", target)?;
    let pivot_i = require_i32("stab_measure_cascade", "pivot_row", pivot_row)?;
    let blocks = require_u32("stab_measure_cascade", "num_rows", 2usize.saturating_mul(n))?;
    let cfg = LaunchConfig {
        grid_dim: (blocks, 1, 1),
        block_dim: (MEASURE_BLOCK_SIZE, 1, 1),
        shared_mem_bytes: 0,
    };

    let mut builder = stream.launch_builder(&func);
    let (xz_buf, phase_buf) = tableau.xz_phase_mut();
    let xz = xz_buf.raw_mut();
    let phase = phase_buf.raw_mut();
    builder
        .arg(xz)
        .arg(phase)
        .arg(&num_qubits_i)
        .arg(&nw)
        .arg(&target_i)
        .arg(&pivot_i);
    // SAFETY: signature (u64*, u8*, i32, i32, i32, i32). Each block owns a
    // unique destination row (blockIdx.x); pivot_row is read-only throughout.
    // No inter-block hazard on xz or phase because writes target disjoint
    // rows.
    unsafe {
        builder
            .launch(cfg)
            .map_err(|e| launch_err("stab_measure_cascade", e))?;
    }
    Ok(())
}

/// Post-cascade fixup: move pivot data into the paired destabiliser, install
/// `Z_target` with the measured outcome at the pivot row. Single block.
pub(crate) fn launch_measure_fixup(
    ctx: &GpuContext,
    tableau: &mut GpuTableau,
    target: usize,
    pivot_row: usize,
    outcome: bool,
) -> Result<()> {
    let n = tableau.num_qubits();
    if n == 0 {
        return Ok(());
    }
    let device = ctx.device();
    let stream = device.stream()?;
    let func = device.function("stab_measure_fixup")?;

    let num_qubits_i = require_i32("stab_measure_fixup", "num_qubits", n)?;
    let nw = require_i32("stab_measure_fixup", "num_words", tableau.num_words())?;
    let target_i = require_i32("stab_measure_fixup", "target", target)?;
    let pivot_i = require_i32("stab_measure_fixup", "pivot_row", pivot_row)?;
    let outcome_u8: u8 = outcome as u8;
    let cfg = LaunchConfig {
        grid_dim: (1, 1, 1),
        block_dim: (MEASURE_BLOCK_SIZE, 1, 1),
        shared_mem_bytes: 0,
    };

    let mut builder = stream.launch_builder(&func);
    let (xz_buf, phase_buf) = tableau.xz_phase_mut();
    let xz = xz_buf.raw_mut();
    let phase = phase_buf.raw_mut();
    builder
        .arg(xz)
        .arg(phase)
        .arg(&num_qubits_i)
        .arg(&nw)
        .arg(&target_i)
        .arg(&pivot_i)
        .arg(&outcome_u8);
    // SAFETY: signature (u64*, u8*, i32, i32, i32, i32, u8). Single block
    // writes pivot_row and destab_row disjointly; runs only after the cascade
    // launch completes (single stream, serial ordering).
    unsafe {
        builder
            .launch(cfg)
            .map_err(|e| launch_err("stab_measure_fixup", e))?;
    }
    Ok(())
}

/// Deterministic branch: rowmul stabiliser rows `n+i` for every `i` whose
/// destabiliser has an X at `target` into the scratch row, then read back the
/// scratch row's phase as the measurement outcome. One u8 d2h roundtrip.
pub(crate) fn launch_measure_deterministic(
    ctx: &GpuContext,
    tableau: &mut GpuTableau,
    target: usize,
) -> Result<bool> {
    let n = tableau.num_qubits();
    if n == 0 {
        return Ok(false);
    }
    let device = ctx.device();
    let stream = device.stream()?;
    let func = device.function("stab_measure_deterministic")?;

    let num_qubits_i = require_i32("stab_measure_deterministic", "num_qubits", n)?;
    let nw = require_i32(
        "stab_measure_deterministic",
        "num_words",
        tableau.num_words(),
    )?;
    let target_i = require_i32("stab_measure_deterministic", "target", target)?;
    let cfg = LaunchConfig {
        grid_dim: (1, 1, 1),
        block_dim: (MEASURE_BLOCK_SIZE, 1, 1),
        shared_mem_bytes: 0,
    };

    let mut host_out = [0u8; 1];
    {
        let (xz_buf, phase_buf, outcome_buf) = tableau.xz_phase_outcome_mut();
        let xz = xz_buf.raw_mut();
        let phase = phase_buf.raw_mut();
        let out_outcome = outcome_buf.raw_mut();
        stream
            .memcpy_htod(&[0u8], out_outcome)
            .map_err(|e| launch_err("reset deterministic outcome", e))?;
        let mut builder = stream.launch_builder(&func);
        builder
            .arg(xz)
            .arg(phase)
            .arg(&num_qubits_i)
            .arg(&nw)
            .arg(&target_i)
            .arg(&mut *out_outcome);
        // SAFETY: signature (u64*, u8*, i32, i32, i32, u8*). Single block runs
        // a serial loop over i=0..n; scratch row (index 2n) is the only
        // destination.
        unsafe {
            builder
                .launch(cfg)
                .map_err(|e| launch_err("stab_measure_deterministic", e))?;
        }
        stream
            .memcpy_dtoh(out_outcome, &mut host_out)
            .map_err(|e| launch_err("deterministic outcome dtoh", e))?;
    }
    Ok(host_out[0] != 0)
}