ppvm-tableau 0.1.1

Generalized stabilizer-tableau simulator for ppvm.
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
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
// SPDX-FileCopyrightText: 2026 The PPVM Authors
// SPDX-License-Identifier: Apache-2.0

use std::{fmt::Debug, marker::PhantomData};

// Rayon branch-coefficient helpers coalesce terms by branch index.
#[cfg(feature = "rayon")]
use ppvm_traits::FoldHashMap as HashMap;

use bitvec::array::BitArray;
use bitvec::view::{BitView, BitViewSized};
use num::PrimInt;

use crate::prelude::*;
use num::{
    One, Zero,
    complex::{Complex, Complex64, ComplexFloat},
};
use rand::SeedableRng;
use rand::rngs::SmallRng;

type PhasedPauliWordNoHash<A, H> = PhasedPauliWord<A, H, PauliWord<A, H, false>>;

pub use crate::qubit_status::QubitStatus;

/// A `2n`-row stabilizer / destabilizer tableau.
///
/// Rows `0..n` hold the destabilizers; rows `n..2n` hold the
/// stabilizers. Each row is a [`PhasedPauliWord`] tracking both its
/// `X`/`Z` bits and a phase in `{±1, ±i}`. Implements every
/// Clifford-only operation natively (Hadamard, phase, CNOT, CZ, etc.).
///
/// # Examples
///
/// ```
/// use ppvm_pauli_sum::config::fxhash::ByteF64;
/// use ppvm_traits::traits::Clifford;
/// use ppvm_tableau::data::Tableau;
///
/// let mut tab: Tableau<ByteF64<1>> = Tableau::new(2);
/// tab.h(0);
/// tab.cnot(0, 1);
/// assert_eq!(tab.n_qubits, 2);
/// assert_eq!(tab.stabilizers().len(), 2);
/// ```
#[derive(Clone, Debug)]
pub struct Tableau<T: Config> {
    /// Number of qubits.
    pub n_qubits: usize,
    /// Destabilizer / Stabilizer tableau
    /// * Entries 0..n are the destabilizers
    /// * Entries n..2n are the stabilizers
    pub data: Vec<PhasedPauliWordNoHash<T::Storage, T::BuildHasher>>,
    pub(crate) rng: SmallRng,
}

impl<T: Config> Tableau<T> {
    fn new_data(n_qubits: usize) -> Vec<PhasedPauliWordNoHash<T::Storage, T::BuildHasher>> {
        // Initialize tableau for 0 state
        let mut data: Vec<PhasedPauliWordNoHash<T::Storage, T::BuildHasher>> =
            Vec::with_capacity(2 * n_qubits);
        let pw_cache = PhasedPauliWordNoHash::<T::Storage, T::BuildHasher>::new(n_qubits);
        for i in 0..n_qubits {
            // destabilizer
            let mut pw = pw_cache;
            pw.set(i, Pauli::X);
            data.push(pw);
        }
        for i in 0..n_qubits {
            // stabilizer
            let mut pw = pw_cache;
            pw.set(i, Pauli::Z);
            data.push(pw);
        }
        data
    }

    /// Construct a fresh tableau initialised to `|0…0⟩`.
    pub fn new(n_qubits: usize) -> Self {
        let data = Tableau::<T>::new_data(n_qubits);
        Self {
            n_qubits,
            data,
            rng: rand::make_rng(),
        }
    }

    /// Same as [`Tableau::new`], but seed the RNG deterministically.
    pub fn new_with_seed(n_qubits: usize, seed: u64) -> Self {
        let mut t = Self::new(n_qubits);
        t.rng = SmallRng::seed_from_u64(seed);
        t
    }

    pub fn reset_all(&mut self) {
        let data = Tableau::<T>::new_data(self.n_qubits);
        self.data = data;
    }

    /// View of the stabilizer rows (the upper half of the tableau).
    #[inline]
    pub fn stabilizers(&self) -> &[PhasedPauliWordNoHash<T::Storage, T::BuildHasher>] {
        &self.data[self.n_qubits..]
    }

    /// Mutable view of the stabilizer rows.
    #[inline]
    pub fn stabilizers_mut(&mut self) -> &mut [PhasedPauliWordNoHash<T::Storage, T::BuildHasher>] {
        &mut self.data[self.n_qubits..]
    }

    /// View of the destabilizer rows (the lower half of the tableau).
    #[inline]
    pub fn destabilizers(&self) -> &[PhasedPauliWordNoHash<T::Storage, T::BuildHasher>] {
        &self.data[..self.n_qubits]
    }

    /// Mutable view of the destabilizer rows.
    #[inline]
    pub fn destabilizers_mut(
        &mut self,
    ) -> &mut [PhasedPauliWordNoHash<T::Storage, T::BuildHasher>] {
        &mut self.data[..self.n_qubits]
    }

    // some helper functions for measurement impl
    pub(crate) fn find_z_anticommuting_stabilizer(&self, addr0: usize) -> Option<usize> {
        // Find first stabilizer that anticommutes with Z_addr0
        self.stabilizers()
            .iter()
            .position(|stab| stab.word.anticommutes_at(addr0, (false, true)))
    }

    pub(crate) fn get_deterministic_outcome(&self, addr0: usize) -> bool
    where
        <<T as Config>::Storage as BitView>::Store: PrimInt,
    {
        // find the outcome: either Z_addr0 or -Z_addr0 is a stabilizer
        // the stabilizer can be computed as the product of all stabilizers S_i
        // whose corresponding destabilizer D_i anticommutes with Z_addr0 (has X at addr0).
        // We have to actually multiply Paulis to also account for products of +i/-i
        let destabilizers = self.destabilizers();
        let stabilizers = self.stabilizers();
        let n = self.n_qubits;
        let mut result = PhasedPauliWordNoHash::<T::Storage, T::BuildHasher>::new(n);
        for (i, destab) in destabilizers.iter().enumerate() {
            if destab.word.xbits[addr0] {
                result *= &stabilizers[i];
            }
        }

        // phase >= 2 means -Z eigenvalue → outcome |1⟩ (true)
        debug_assert!(
            result.phase == 0 || result.phase == 2,
            "Measurement result cannot be imaginary!"
        );
        result.phase >= 2
    }

    pub(crate) fn update_tableau_according_to_outcome(
        &mut self,
        addr0: usize,
        q_idx: usize,
        outcome: bool,
    ) where
        <<T as Config>::Storage as BitView>::Store: PrimInt,
    {
        let n = self.n_qubits;
        let (destabilizers, stabilizers) = self.data.split_at_mut(n);

        // Copy g_q once before the loop
        let g_q = stabilizers[q_idx];

        // Check if there are other stabilizers that anticommute with Z_addr0
        // If so, replace with g_j = g_j * g_q
        for i in 0..n {
            if i == q_idx {
                continue;
            }
            if stabilizers[i].word.xbits[addr0] {
                // Stabilizer i also anticommutes, so multiply by g_q to eliminate
                stabilizers[i] *= &g_q;
            }
            if destabilizers[i].word.xbits[addr0] {
                destabilizers[i] *= &g_q;
            }
        }

        // Update destabilizer q to be the old stabilizer q (before replacement)
        destabilizers[q_idx] = g_q;

        // Finally, replace g_q by ±Z
        let stab_q = &mut stabilizers[q_idx];
        stab_q.word.xbits = BitArray::ZERO;
        stab_q.word.zbits = BitArray::ZERO;
        stab_q.word.zbits.set(addr0, true);
        stab_q.phase = if outcome { 2 } else { 0 };
    }

    /// Apply CZ to N pairs with constant offset: (base+i, base+offset+i) for i in 0..count.
    /// All pairs must be in the same u64 word. This replaces N individual CZ calls
    /// with a single word-level shift+XOR operation per row.
    ///
    /// # Preconditions
    ///
    /// The fused kernel needs `offset >= count`, i.e. pairwise-disjoint
    /// supports (pairs `i != j` collide iff `|i - j| == offset`, which needs
    /// `offset < count`, or `offset == 0`). Both fused updates need it: the
    /// single `count_ones() & 1` phase reads the pre-update `z` plane for
    /// every pair at once (`Batch.lean::czSeq_phase` proves it equal to the
    /// sequential loop only under pairwise disjointness, and
    /// `czSeq_phase_needs_disjoint` exhibits a counterexample), and the `z`
    /// delta `OR`s the two shifted planes, so a `z` bit written by two pairs
    /// is set once instead of XOR-cancelled.
    ///
    /// Overlapping pairs (`offset < count`, including `offset == 0`) fall
    /// back to the per-pair `cz` loop, so this method is safe in release.
    ///
    /// # Panics
    /// Debug-asserts that all bits are within the same word.
    #[inline]
    pub fn cz_block_pairs(&mut self, base: usize, offset: usize, count: usize)
    where
        <<T::Storage as BitView>::Store as TryFrom<usize>>::Error: Debug,
        <T::Storage as BitView>::Store: PrimInt + TryFrom<usize>,
    {
        if count == 0 {
            return;
        }
        if offset < count {
            return self.cz_pairs_each(base, offset, count);
        }
        let bits_per_word = std::mem::size_of::<<T::Storage as BitView>::Store>() * 8;
        let base_bit = base % bits_per_word;
        let word_idx = base / bits_per_word;

        debug_assert_eq!(
            (base + offset + count - 1) / bits_per_word,
            word_idx,
            "All CZ pairs must be in the same word"
        );

        let one = <T::Storage as BitView>::Store::one();
        let zero = <T::Storage as BitView>::Store::zero();
        let count_mask = if count >= bits_per_word {
            !zero
        } else {
            (one << count) - one
        };
        let mask_c = count_mask << base_bit;
        let mask_t = count_mask << (base_bit + offset);

        self.data.iter_mut().for_each(|pw| {
            let xp = pw.word.xbits.data.as_raw_mut_slice();
            let zp = pw.word.zbits.data.as_raw_mut_slice();
            let x = xp[word_idx];
            let z = zp[word_idx];

            // Phase computation (must use original z before update)
            let xc = (x >> base_bit) & count_mask;
            let xt = (x >> (base_bit + offset)) & count_mask;
            let zc = (z >> base_bit) & count_mask;
            let zt = (z >> (base_bit + offset)) & count_mask;
            let phase_bits = xc & xt & (zc ^ zt);
            pw.phase ^= ((phase_bits.count_ones() & 1) as u8) << 1;

            // Z update: z[c] ^= x[t], z[t] ^= x[c]
            let z_delta = ((x >> offset) & mask_c) | ((x << offset) & mask_t);
            zp[word_idx] = z ^ z_delta;
        });
    }

    /// Per-pair `cz` fallback for overlapping [`Self::cz_block_pairs`] runs.
    /// Outlined so the fused kernel keeps a single call on the disjoint path.
    #[cold]
    #[inline(never)]
    fn cz_pairs_each(&mut self, base: usize, offset: usize, count: usize)
    where
        <T::Storage as BitView>::Store: PrimInt,
    {
        for i in 0..count {
            Clifford::cz(self, base + i, base + offset + i);
        }
    }

    /// Apply CZ to N pairs with constant offset across two different words.
    /// Controls at (word_c, base_bit_c+i) and targets at (word_t, base_bit_t+i) for i in 0..count.
    /// word_c and word_t must be different.
    #[inline]
    pub fn cz_block_pairs_cross_word(
        &mut self,
        word_c: usize,
        base_bit_c: usize,
        word_t: usize,
        base_bit_t: usize,
        count: usize,
    ) where
        <T::Storage as BitView>::Store: PrimInt,
    {
        if count == 0 {
            return;
        }
        let one = <T::Storage as BitView>::Store::one();
        let zero = <T::Storage as BitView>::Store::zero();
        let bits_per_word = std::mem::size_of::<<T::Storage as BitView>::Store>() * 8;

        debug_assert!(base_bit_c + count <= bits_per_word);
        debug_assert!(base_bit_t + count <= bits_per_word);
        debug_assert_ne!(word_c, word_t);

        let count_mask = if count >= bits_per_word {
            !zero
        } else {
            (one << count) - one
        };

        self.data.iter_mut().for_each(|pw| {
            let xp = pw.word.xbits.data.as_raw_mut_slice();
            let zp = pw.word.zbits.data.as_raw_mut_slice();

            // Extract aligned bits (shifted to 0..count-1)
            let xc = (xp[word_c] >> base_bit_c) & count_mask;
            let xt = (xp[word_t] >> base_bit_t) & count_mask;
            let zc = (zp[word_c] >> base_bit_c) & count_mask;
            let zt = (zp[word_t] >> base_bit_t) & count_mask;

            // Phase: x[c] & x[t] & (z[c] ^ z[t]) per pair
            let phase_bits = xc & xt & (zc ^ zt);
            pw.phase ^= ((phase_bits.count_ones() & 1) as u8) << 1;

            // z[c] ^= x[t]: place target x-bits at control positions
            zp[word_c] = zp[word_c] ^ (xt << base_bit_c);
            // z[t] ^= x[c]: place control x-bits at target positions
            zp[word_t] = zp[word_t] ^ (xc << base_bit_t);
        });
    }
}

const COMPLEX_PHASE_CONVERSION: [Complex64; 4] = [
    Complex64 { re: 1.0, im: 0.0 },  // +1
    Complex64 { re: 0.0, im: 1.0 },  // +i
    Complex64 { re: -1.0, im: 0.0 }, // -1
    Complex64 { re: 0.0, im: -1.0 }, // -i
];

/// Symplectic inner product of two tableau index values — the count of
/// shared set bits, used in stabilizer phase calculations.
#[inline]
pub fn symplectic_inner<I>(alpha: I, beta: I) -> u32
where
    I: TableauIndex,
{
    (alpha & beta).count_ones()
}

/// Compute the phase contribution from destabilizer anticommutation and odd-phase masks.
/// This is a pure function of its arguments (no self access needed), extracted to enable
/// use in parallel contexts where borrowing self is not possible.
#[inline]
pub(crate) fn compute_phase_with_mask_static<I: TableauIndex>(
    destab_anticomm_bits: I,
    basis_index: I,
    stab_anticomm_bits: I,
    odd_phase_mask: I,
) -> u8 {
    let mut phase = (2 * symplectic_inner(destab_anticomm_bits, basis_index) as u8) % 4;
    let active = basis_index & stab_anticomm_bits;
    let parity = (active & odd_phase_mask).count_ones() % 2;
    phase = (phase + 2 * parity as u8) % 4;
    phase
}

/// Minimum number of coefficients before engaging rayon parallelism.
/// Below this threshold, the sequential path is always used even with the `rayon` feature.
/// Benchmarked: at 8K coefficients rayon has ~24% overhead; at 32K it's 35% faster.
/// Set to 16384 to avoid regressions while capturing the large-coefficient wins.
#[cfg(feature = "rayon")]
pub(crate) const RAYON_COEFF_THRESHOLD: usize = 16384;

/// Parameters describing how a Pauli branch splits each coefficient.
///
/// Bundles the four anticommutation/phase-mask fields produced by
/// [`compute_decomposition`](GeneralizedTableau::compute_decomposition) with the
/// two scaling factors applied to the branch and non-branch contributions. They
/// always travel together, so passing them as one value keeps the coefficient
/// accumulation helpers under clippy's argument-count limit without an
/// `#[allow]`.
///
/// Only the rayon coefficient helpers consume this; the default-feature build
/// uses the inlined sort-merge path in
/// [`branch_with_coefficients`](GeneralizedTableau::branch_with_coefficients).
#[cfg(feature = "rayon")]
#[derive(Clone, Copy)]
pub(crate) struct BranchParams<I, CoeffType> {
    pub stab_anticomm_bits: I,
    pub destab_anticomm_bits: I,
    pub odd_phase_mask: I,
    pub phase_decomp: u8,
    pub coefficient_factor: Complex<CoeffType>,
    pub branch_factor: Complex<CoeffType>,
}

/// Sequential accumulation of branch coefficients.
#[cfg(feature = "rayon")]
fn branch_coefficients_seq<I, CoeffType>(
    items: impl IntoIterator<Item = (Complex<CoeffType>, I)>,
    capacity: usize,
    params: BranchParams<I, CoeffType>,
) -> HashMap<I, Complex<CoeffType>>
where
    I: TableauIndex,
    CoeffType: One + Zero + Clone + num::Num,
    Complex<CoeffType>:
        std::ops::Mul<Output = Complex<CoeffType>> + std::ops::AddAssign + From<Complex64> + Copy,
{
    let BranchParams {
        stab_anticomm_bits,
        destab_anticomm_bits,
        odd_phase_mask,
        phase_decomp,
        coefficient_factor,
        branch_factor,
    } = params;
    let mut map: HashMap<I, Complex<CoeffType>> =
        HashMap::with_capacity_and_hasher(capacity, Default::default());
    for (coeff, idx) in items {
        debug_assert!(
            !(coeff.re == CoeffType::zero() && coeff.im == CoeffType::zero()),
            "Coefficient should not be zero"
        );
        let branch_index = idx ^ stab_anticomm_bits;
        let branch_phase_contribution = compute_phase_with_mask_static(
            destab_anticomm_bits,
            idx,
            stab_anticomm_bits,
            odd_phase_mask,
        );
        let branch_phase = (branch_phase_contribution + phase_decomp) % 4;
        let phase_factor: Complex<CoeffType> =
            COMPLEX_PHASE_CONVERSION[branch_phase as usize].into();
        let branch_coefficient = phase_factor * coeff * branch_factor;
        let nonbranch_coefficient = coeff * coefficient_factor;
        *map.entry(branch_index).or_insert(Complex::zero()) += branch_coefficient;
        *map.entry(idx).or_insert(Complex::zero()) += nonbranch_coefficient;
    }
    map
}

/// Accumulate branch coefficients. When the coefficient count exceeds
/// `RAYON_COEFF_THRESHOLD`, uses parallel map/collect into a Vec followed
/// by sequential accumulation. Below the threshold, falls back to sequential.
#[cfg(feature = "rayon")]
fn branch_coefficients_parallel<I, CoeffType>(
    items: &[(Complex<CoeffType>, I)],
    params: BranchParams<I, CoeffType>,
) -> HashMap<I, Complex<CoeffType>>
where
    I: TableauIndex + Send + Sync,
    CoeffType: One + Zero + Clone + Send + Sync + num::Num,
    Complex<CoeffType>:
        std::ops::Mul<Output = Complex<CoeffType>> + std::ops::AddAssign + From<Complex64> + Copy,
{
    let BranchParams {
        stab_anticomm_bits,
        destab_anticomm_bits,
        odd_phase_mask,
        phase_decomp,
        coefficient_factor,
        branch_factor,
    } = params;
    if items.len() >= RAYON_COEFF_THRESHOLD {
        use rayon::prelude::*;

        // Parallel phase: compute all (branch_idx, branch_coeff, idx, nonbranch_coeff) tuples.
        // This is pure math with no shared mutable state.
        let pairs: Vec<(I, Complex<CoeffType>, I, Complex<CoeffType>)> = items
            .par_iter()
            .map(|&(coeff, idx)| {
                let branch_index = idx ^ stab_anticomm_bits;
                let branch_phase_contribution = compute_phase_with_mask_static(
                    destab_anticomm_bits,
                    idx,
                    stab_anticomm_bits,
                    odd_phase_mask,
                );
                let branch_phase = (branch_phase_contribution + phase_decomp) % 4;
                let phase_factor: Complex<CoeffType> =
                    COMPLEX_PHASE_CONVERSION[branch_phase as usize].into();
                (
                    branch_index,
                    phase_factor * coeff * branch_factor,
                    idx,
                    coeff * coefficient_factor,
                )
            })
            .collect();

        // Sequential phase: accumulate into a pre-sized HashMap.
        // HashMap inserts dominate the cost but benefit from cache locality.
        let mut map: HashMap<I, Complex<CoeffType>> =
            HashMap::with_capacity_and_hasher(2 * pairs.len(), Default::default());
        for (branch_idx, branch_coeff, idx, nonbranch_coeff) in pairs {
            *map.entry(branch_idx).or_insert(Complex::zero()) += branch_coeff;
            *map.entry(idx).or_insert(Complex::zero()) += nonbranch_coeff;
        }
        return map;
    }

    branch_coefficients_seq(items.iter().copied(), 2 * items.len(), params)
}

/// Sequential relabel of apply coefficients.
///
/// Pauli application sends every branch to `branch_index = idx ^ stab_anticomm_bits`.
/// XOR by a fixed constant is a bijection, so distinct input indices always map
/// to distinct branch indices: unlike the T-gate branch split (which emits two
/// streams that genuinely collide), the apply path produces no index collisions
/// at all. A per-index coalesce can therefore never merge two entries — the
/// `entry()`-keyed map was pure overhead (hash every key + table allocation) for
/// what is a straight relabel. We instead build a flat `Vec` in one sequential,
/// prefetch-friendly pass and let the caller apply the magnitude cutoff. (The
/// returned keys are unique by the bijection above; the `Vec` backing relies on
/// that, exactly as the old map did implicitly.)
fn apply_coefficients_seq<I, CoeffType>(
    items: impl IntoIterator<Item = (Complex<CoeffType>, I)>,
    capacity: usize,
    stab_anticomm_bits: I,
    destab_anticomm_bits: I,
    odd_phase_mask: I,
    phase_decomp: u8,
) -> Vec<(I, Complex<CoeffType>)>
where
    I: TableauIndex,
    CoeffType: One + Zero + Clone + num::Num,
    Complex<CoeffType>:
        std::ops::Mul<Output = Complex<CoeffType>> + std::ops::AddAssign + From<Complex64> + Copy,
{
    let mut out: Vec<(I, Complex<CoeffType>)> = Vec::with_capacity(capacity);
    for (coeff, idx) in items {
        debug_assert!(
            !(coeff.re == CoeffType::zero() && coeff.im == CoeffType::zero()),
            "Coefficient should not be zero"
        );
        let branch_index = idx ^ stab_anticomm_bits;
        let branch_phase_contribution = compute_phase_with_mask_static(
            destab_anticomm_bits,
            idx,
            stab_anticomm_bits,
            odd_phase_mask,
        );
        let branch_phase = (branch_phase_contribution + phase_decomp) % 4;
        let phase_factor: Complex<CoeffType> =
            COMPLEX_PHASE_CONVERSION[branch_phase as usize].into();
        out.push((branch_index, phase_factor * coeff));
    }
    out
}

/// Relabel coefficients for pauli application. When the coefficient count
/// exceeds `RAYON_COEFF_THRESHOLD`, the per-element relabel runs as a parallel
/// map; below the threshold it falls back to the sequential relabel.
///
/// Because the relabel `idx ^ stab_anticomm_bits` is a bijection (see
/// [`apply_coefficients_seq`]), the parallel map's output already has unique
/// keys — there is nothing to coalesce, so the result is collected straight into
/// a flat `Vec` with no sequential fold afterwards.
#[cfg(feature = "rayon")]
fn apply_coefficients_parallel<I, CoeffType>(
    items: &[(Complex<CoeffType>, I)],
    stab_anticomm_bits: I,
    destab_anticomm_bits: I,
    odd_phase_mask: I,
    phase_decomp: u8,
) -> Vec<(I, Complex<CoeffType>)>
where
    I: TableauIndex + Send + Sync,
    CoeffType: One + Zero + Clone + Send + Sync + num::Num,
    Complex<CoeffType>:
        std::ops::Mul<Output = Complex<CoeffType>> + std::ops::AddAssign + From<Complex64> + Copy,
{
    // See `branch_coefficients_parallel`: avoid nesting rayon inside shot-level
    // parallelism; the main-thread (single-shot) path is unaffected.
    if items.len() >= RAYON_COEFF_THRESHOLD && rayon::current_thread_index().is_none() {
        use rayon::prelude::*;

        return items
            .par_iter()
            .map(|&(coeff, idx)| {
                let branch_index = idx ^ stab_anticomm_bits;
                let branch_phase_contribution = compute_phase_with_mask_static(
                    destab_anticomm_bits,
                    idx,
                    stab_anticomm_bits,
                    odd_phase_mask,
                );
                let branch_phase = (branch_phase_contribution + phase_decomp) % 4;
                let phase_factor: Complex<CoeffType> =
                    COMPLEX_PHASE_CONVERSION[branch_phase as usize].into();
                (branch_index, phase_factor * coeff)
            })
            .collect();
    }

    apply_coefficients_seq(
        items.iter().copied(),
        items.len(),
        stab_anticomm_bits,
        destab_anticomm_bits,
        odd_phase_mask,
        phase_decomp,
    )
}

/// A [`Tableau`] extended with sparse coefficient tracking to handle
/// non-Clifford gates.
///
/// Non-Clifford gates (T, rotations) split a single tableau into a sum
/// of weighted branches indexed by bitstrings. `GeneralizedTableau`
/// stores those weights in a [`SparseVector`] keyed by an
/// [`IndexType`](TableauIndex). Choose:
/// * `IndexType = usize` for up to 64 qubits,
/// * `IndexType = u128` for up to 128,
/// * `IndexType = bnum::types::U256` and friends for the very wide
///   regime.
///
/// Per-qubit status (loss / leakage) is tracked in
/// [`qubit_status`](GeneralizedTableau::qubit_status); gates skip any qubit that is
/// not [`QubitStatus::Live`].
///
/// # Examples
///
/// Prepare a Bell pair and sample one shot. With a fixed seed the two
/// measurements are perfectly correlated on every shot:
///
/// ```
/// use ppvm_pauli_sum::config::fxhash::ByteF64;
/// use ppvm_traits::traits::{Clifford, LossyMeasure};
/// use ppvm_tableau::data::GeneralizedTableau;
///
/// let mut tab: GeneralizedTableau<ByteF64<1>> =
///     GeneralizedTableau::new_with_seed(2, 1e-12, 0);
/// tab.h(0);
/// tab.cnot(0, 1);
///
/// let r0 = LossyMeasure::measure(&mut tab, 0);
/// let r1 = LossyMeasure::measure(&mut tab, 1);
/// assert_eq!(r0, r1);
/// ```
///
/// Non-Clifford gates work through the same interface — apply a `T` gate
/// followed by `T†` and the state is unchanged:
///
/// ```
/// use ppvm_pauli_sum::config::fxhash::ByteF64;
/// use ppvm_traits::traits::{Clifford, TGate};
/// use ppvm_tableau::data::GeneralizedTableau;
///
/// let mut tab: GeneralizedTableau<ByteF64<1>> =
///     GeneralizedTableau::new_with_seed(1, 1e-12, 0);
/// tab.h(0);
/// tab.t(0);
/// tab.t_dag(0);
/// // T followed by T† is the identity; the |+⟩ state is restored.
/// ```
#[derive(Clone)]
pub struct GeneralizedTableau<
    T: Config,
    IndexType = usize,
    SparseVectorType: SparseVector<Complex<T::Coeff>, IndexType> = Vec<(Complex64, IndexType)>,
> {
    /// Underlying Clifford tableau.
    pub tableau: Tableau<T>,
    /// Sparse coefficient vector indexed by bitstrings.
    pub coefficients: SparseVectorType,
    /// Per-qubit status relative to the computational subspace.
    pub qubit_status: Vec<QubitStatus>,
    /// Coefficient-magnitude threshold below which branches are dropped.
    pub coefficient_threshold: T::Coeff,
    /// Ordered log of every measurement performed (mirrors stim's record).
    pub measurement_record: Vec<Option<bool>>,
    _index_phantom: PhantomData<IndexType>,
}

impl<T: Config, I, C: SparseVector<Complex<T::Coeff>, I>> GeneralizedTableau<T, I, C>
where
    T::Coeff: One + Zero + Clone + num::Num,
    Complex<T::Coeff>: std::ops::Mul<Output = Complex<T::Coeff>>
        + std::ops::AddAssign
        + From<Complex64>
        + ComplexFloat
        + Copy,
    I: TableauIndex,
{
    /// Construct a generalized tableau in the `|0…0⟩` state.
    ///
    /// Branches whose coefficient magnitude falls below
    /// `coefficient_threshold` are dropped during gate application.
    pub fn new(n_qubits: usize, coefficient_threshold: T::Coeff) -> Self {
        let mut coefficients = C::new();
        let complex_one = Complex {
            re: T::Coeff::one(),
            im: T::Coeff::zero(),
        };
        coefficients.unsafe_insert(I::zero(), complex_one);
        Self {
            tableau: Tableau::new(n_qubits),
            coefficients,
            qubit_status: vec![QubitStatus::Live; n_qubits],
            coefficient_threshold,
            measurement_record: Vec::new(),
            _index_phantom: PhantomData,
        }
    }

    /// Same as [`GeneralizedTableau::new`], but seed the RNG deterministically.
    pub fn new_with_seed(n_qubits: usize, coefficient_threshold: T::Coeff, seed: u64) -> Self {
        let mut s = Self::new(n_qubits, coefficient_threshold);
        s.tableau.rng = SmallRng::seed_from_u64(seed);
        s
    }

    pub fn reset_all(&mut self) {
        self.tableau.reset_all();

        let mut coefficients = C::new();
        let complex_one = Complex {
            re: T::Coeff::one(),
            im: T::Coeff::zero(),
        };
        coefficients.unsafe_insert(I::zero(), complex_one);
        self.coefficients = coefficients;
        for occ in self.qubit_status.iter_mut() {
            *occ = QubitStatus::Live;
        }
        self.measurement_record.clear();
    }

    /// Clone the quantum state but reinitialize the RNG, producing an independent simulation
    /// branch. If `seed` is `Some`, the new RNG is seeded deterministically; if `None`, it is
    /// seeded from OS entropy.
    pub fn fork(&self, seed: Option<u64>) -> Self {
        let mut cloned = self.clone();
        cloned.tableau.rng = match seed {
            Some(s) => SmallRng::seed_from_u64(s),
            None => rand::make_rng(),
        };
        cloned
    }

    /// Number of qubits.
    pub fn n_qubits(&self) -> usize {
        self.tableau.n_qubits
    }

    /// All measurement outcomes recorded so far, in order.
    pub fn current_measurement_record(&self) -> &[Option<bool>] {
        &self.measurement_record
    }

    /// Append an externally defined measurement result to the record.
    ///
    /// Used by Stim instructions such as `MPAD`, which append measurement
    /// record bits without measuring a qubit.
    pub fn append_measurement_record(&mut self, result: Option<bool>) {
        self.measurement_record.push(result);
    }

    /// Replace the most recent measurement record entry.
    ///
    /// Used by noisy measurement paths where the quantum state follows the
    /// true outcome but the public record should hold the reported bit.
    pub fn overwrite_last_measurement_record(&mut self, result: Option<bool>) {
        if let Some(last) = self.measurement_record.last_mut() {
            *last = result;
        }
    }

    /// Apply CZ to N pairs with constant offset: (base+i, base+offset+i) for i in 0..count.
    /// Falls back to individual CZ calls if any qubit in the range is lost or leaked.
    /// Overlap (`offset < count`) is owned by [`Tableau::cz_block_pairs`].
    pub fn cz_block_pairs(&mut self, base: usize, offset: usize, count: usize)
    where
        <<T::Storage as BitView>::Store as TryFrom<usize>>::Error: Debug,
        <T::Storage as BitView>::Store: PrimInt + TryFrom<usize>,
    {
        let any_inactive =
            (0..count).any(|i| self.is_inactive(base + i) || self.is_inactive(base + offset + i));
        if !any_inactive {
            self.tableau.cz_block_pairs(base, offset, count);
        } else {
            self.cz_pairs_each(base, offset, count);
        }
    }

    /// The [`Self::cz_block_pairs`] fallback: one scalar `cz` per surviving
    /// pair, in order. Outlined `#[cold]`/`#[inline(never)]` so this `count`-way
    /// loop of full `2n`-row sweeps stays out of the fused kernel's register
    /// budget and cache lines — the fused path keeps a single call.
    #[cold]
    #[inline(never)]
    fn cz_pairs_each(&mut self, base: usize, offset: usize, count: usize)
    where
        <T::Storage as BitView>::Store: PrimInt,
    {
        for i in 0..count {
            let c = base + i;
            let t = base + offset + i;
            if !self.is_inactive(c) && !self.is_inactive(t) {
                Clifford::cz(&mut self.tableau, c, t);
            }
        }
    }

    /// Apply CZ to N cross-word pairs. Controls at word_c, targets at word_t.
    /// Falls back to individual CZ calls if any qubit is lost or leaked.
    pub fn cz_block_pairs_cross_word(
        &mut self,
        word_c: usize,
        base_bit_c: usize,
        word_t: usize,
        base_bit_t: usize,
        count: usize,
    ) where
        <T::Storage as BitView>::Store: PrimInt,
    {
        let bits_per_word = std::mem::size_of::<<T::Storage as BitView>::Store>() * 8;
        let any_inactive = (0..count).any(|i| {
            let c = word_c * bits_per_word + base_bit_c + i;
            let t = word_t * bits_per_word + base_bit_t + i;
            self.is_inactive(c) || self.is_inactive(t)
        });
        if !any_inactive {
            self.tableau
                .cz_block_pairs_cross_word(word_c, base_bit_c, word_t, base_bit_t, count);
        } else {
            for i in 0..count {
                let c = word_c * bits_per_word + base_bit_c + i;
                let t = word_t * bits_per_word + base_bit_t + i;
                if !self.is_inactive(c) && !self.is_inactive(t) {
                    Clifford::cz(&mut self.tableau, c, t);
                }
            }
        }
    }

    /// Apply CZ to `count` pairs with a constant offset, given in qubit-index
    /// terms: `(control_base + i, target_base + i)` for `i in 0..count`.
    ///
    /// This is the high-level entry point for a fused block of CZs: it splits
    /// the run at storage-word boundaries internally and dispatches each
    /// segment to [`Self::cz_block_pairs`] (control and target in the same
    /// word) or [`Self::cz_block_pairs_cross_word`] (straddling two words), so
    /// callers never need to reason about the `u64` packing. CZ is symmetric,
    /// so the two bases may be passed in either order.
    ///
    /// Overlapping pairs — `|target_base - control_base| < count`, of which
    /// adjacent-pair brickwork `cz_block(0, 1, n)` is the extreme case — are
    /// handled: the same-word segments route through [`Self::cz_block_pairs`]
    /// into [`Tableau::cz_block_pairs`], which falls back to the per-pair
    /// loop. A cross-word segment is always disjoint (its `run` never exceeds
    /// the offset), so the fused kernel stays live there.
    pub fn cz_block(&mut self, control_base: usize, target_base: usize, count: usize)
    where
        <<T::Storage as BitView>::Store as TryFrom<usize>>::Error: Debug,
        <T::Storage as BitView>::Store: PrimInt + TryFrom<usize>,
    {
        if count == 0 {
            return;
        }
        // cz_block_pairs needs a non-negative offset; CZ is symmetric, so order
        // the two bases.
        let (lo, hi) = if control_base <= target_base {
            (control_base, target_base)
        } else {
            (target_base, control_base)
        };
        let bits_per_word = std::mem::size_of::<<T::Storage as BitView>::Store>() * 8;
        let mut i = 0;
        while i < count {
            let (c, t) = (lo + i, hi + i);
            let (wc, bc) = (c / bits_per_word, c % bits_per_word);
            let (wt, bt) = (t / bits_per_word, t % bits_per_word);
            // Longest run before either index crosses into the next word.
            let run = (bits_per_word - bc).min(bits_per_word - bt).min(count - i);
            if wc == wt {
                self.cz_block_pairs(c, t - c, run);
            } else {
                self.cz_block_pairs_cross_word(wc, bc, wt, bt, run);
            }
            i += run;
        }
    }

    // helper functions

    /// Compute the decomposition of a pauli into stabilizer destabilizer products
    /// Any Pauli can be written as P_addr0 = phase * prod(d_k ^ gamma_k) * prod(s_l ^ lambda_l)
    /// where: gamma_k == 1 iff {P_addr0, s_k} = 0
    /// lambda_l == 1 iff {P_addr0, d_l} = 0
    /// Lemma 5. from T. J. Yoder (2012)
    /// NOTE: this is O(n^2)
    ///
    /// The function returns `(phase, stab_anticomm_bits, destab_anticomm_bits)`, where
    /// `stab_anticomm_bits[k] = 1` iff P_addr0 anticommutes with stabilizer s_k, and
    /// `destab_anticomm_bits[l] = 1` iff P_addr0 anticommutes with destabilizer d_l.
    /// Note that stab_anticomm_bits is equal to the shift of the index when branching
    /// (`beta` in Eq(4) of the SOFT paper).
    pub fn compute_decomposition(&self, addr0: usize, pauli: Pauli) -> (u8, I, I)
    where
        <<T as Config>::Storage as BitView>::Store: PrimInt,
    {
        let n = self.n_qubits();

        // the actual decomposition, which we need to track the phase
        let mut p_word = PhasedPauliWordNoHash::<T::Storage, T::BuildHasher>::new(n);
        p_word.set(addr0, pauli);

        // the bit strings defining the contributions
        let mut destab_anticomm_bits = I::zero();
        let mut stab_anticomm_bits = I::zero();

        debug_assert_ne!(pauli, Pauli::I);
        let pauli_bits = match pauli {
            Pauli::I => (false, false),
            Pauli::X => (true, false),
            Pauli::Y => (true, true),
            Pauli::Z => (false, true),
            _ => unreachable!("Pauli L cannot occur in tableau"),
        };

        let stabilizers = self.tableau.stabilizers();
        let destabilizers = self.tableau.destabilizers();
        let one = I::one();

        for (i, stab) in stabilizers.iter().enumerate() {
            if !destabilizers[i].word.anticommutes_at(addr0, pauli_bits) {
                // commutes
                continue;
            }

            // contributes, so set the corresponding bit in destab_anticomm_bits to 1
            destab_anticomm_bits |= one << i;

            // destabilizer anti-commutes, so the stabilizer contributes
            // the stabilizer is its own inverse up to its phase
            // to avoid inverting the stabilizer, we just multiply by it
            // and then divide by its phase squared
            p_word *= stab;
            p_word.add_phase(8 - 2 * stab.phase);
        }

        // NOTE: destabilizers also commute with one another in a valid tableau
        // since the form a basis together with stabilizers
        for (i, destab) in destabilizers.iter().enumerate() {
            if !stabilizers[i].word.anticommutes_at(addr0, pauli_bits) {
                // commutes
                continue;
            }

            // contributes, so set the corresponding bit in stab_anticomm_bits to 1
            stab_anticomm_bits |= one << i;

            // stabilizer anti-commutes, so the destabilizer contributes
            p_word *= destab;
            p_word.add_phase(8 - 2 * destab.phase);
        }

        (p_word.phase, stab_anticomm_bits, destab_anticomm_bits)
    }

    /// Multi-qubit generalization of [`compute_decomposition`]: conjugate an
    /// arbitrary `PauliWord` through the tableau and return the same triple
    /// `(phase, stab_anticomm_bits, destab_anticomm_bits)`.
    ///
    /// Algorithm: call [`compute_decomposition`] for each non-identity qubit
    /// in the input, then multiply the resulting single-qubit conjugates in
    /// canonical-basis form `i^φ X^x Z^z`. Pauli multiplication picks up a
    /// `(-1)^{popcount(z_running & x_new)}` cross-phase from
    /// `Z^z_a X^x_b = (-1)^{z_a · x_b} X^x_b Z^z_a`.
    pub(crate) fn compute_decomposition_word<W: PauliWordTrait>(&self, word: &W) -> (u8, I, I)
    where
        <<T as Config>::Storage as BitView>::Store: PrimInt,
    {
        let mut phase = 0u8;
        let mut stab_anticomm = I::zero();
        let mut destab_anticomm = I::zero();
        for q in 0..self.n_qubits() {
            let p_q = word.get(q);
            if p_q == Pauli::I {
                continue;
            }
            let (q_phase, q_stab, q_destab) = self.compute_decomposition(q, p_q);
            let cross = 2 * (symplectic_inner(destab_anticomm, q_stab) as u8 % 2);
            phase = (phase + q_phase + cross) % 4;
            stab_anticomm = stab_anticomm ^ q_stab;
            destab_anticomm = destab_anticomm ^ q_destab;
        }
        (phase, stab_anticomm, destab_anticomm)
    }

    /// every basis index is a bit string alpha defining the basis state
    /// the phase when applying a Pauli is the product of all destabilizer phases
    /// and the phase contributions from the commutation relations
    /// we need to check every destabilizer where the basis index has a 1 bit.
    #[cfg(test)]
    pub(crate) fn compute_phase(
        &self,
        destab_anticomm_bits: I,
        basis_index: I,
        stab_anticomm_bits: I,
    ) -> u8 {
        // phase convention: 0: +1, 1: +i, 2: -1, 3: -i
        let one = I::one();
        let zero = I::zero();

        // contribution 1: each destabilizer D_i with basis_index[i]=1 that anticommutes
        // with P (destab_anticomm_bits[i]=1) contributes a -1 sign; this is the symplectic inner product
        let mut phase = (2 * symplectic_inner(destab_anticomm_bits, basis_index) as u8) % 4;

        // contribution 2: destabilizers that appear twice (basis_index[i]=1 and stab_anticomm_bits[i]=1)
        // contribute an extra -1 if their phase is imaginary
        let active = basis_index & stab_anticomm_bits;
        for (i, destab) in self.tableau.destabilizers().iter().enumerate() {
            if active & (one << i) == zero {
                continue;
            }
            if destab.phase % 2 != 0 {
                phase = (phase + 2) % 4;
            }
        }

        phase
    }

    /// Build a bitmask where bit i is set if destabilizer i has odd phase (phase % 2 != 0).
    pub fn odd_phase_destabilizer_mask(&self) -> I {
        let mut mask = I::zero();
        let one = I::one();
        for (i, destab) in self.tableau.destabilizers().iter().enumerate() {
            if destab.phase % 2 != 0 {
                mask |= one << i;
            }
        }
        mask
    }
}

impl<T: Config, I, C: SparseVector<Complex<T::Coeff>, I>> GeneralizedTableau<T, I, C>
where
    T::Coeff: One + Zero + Clone + Send + Sync + num::Num + PartialOrd,
    Complex<T::Coeff>: std::ops::Mul<Output = Complex<T::Coeff>>
        + std::ops::AddAssign
        + From<Complex64>
        + ComplexFloat
        + Copy,
    I: TableauIndex + Send + Sync,
{
    /// Split every coefficient into a branch and non-branch term for a
    /// single-qubit Pauli measurement/rotation, in one pass over the whole
    /// coefficient array.
    ///
    /// This is a whole-batch primitive over the coefficient array — see the
    /// crate-level "Data-parallel / GPU-offloadable surface" section. The
    /// `#[cfg(feature = "rayon")]` path computes each entry's branch/non-branch
    /// coefficients in parallel; only the final accumulation into the
    /// coefficient map is sequential.
    pub(crate) fn branch_with_coefficients(
        &mut self,
        addr0: usize,
        pauli: Pauli,
        coefficient_factor: Complex<T::Coeff>,
        branch_factor: Complex<T::Coeff>,
    ) where
        <<T as Config>::Storage as BitView>::Store: PrimInt,
    {
        if self.is_inactive(addr0) {
            return;
        }

        let (phase_decomp, stab_anticomm_bits, destab_anticomm_bits) =
            self.compute_decomposition(addr0, pauli);

        let odd_phase_mask = self.odd_phase_destabilizer_mask();
        let old_coefficients = std::mem::replace(&mut self.coefficients, C::new());
        let n_coefficients = old_coefficients.len();

        let cutoff_sq = self.coefficient_threshold.clone() * self.coefficient_threshold.clone();

        // When rayon is enabled and above the threshold, use parallel map then apply cutoff
        // directly. Early-return so old_coefficients stays valid for the sequential path below.
        #[cfg(feature = "rayon")]
        if n_coefficients >= RAYON_COEFF_THRESHOLD {
            let items: Vec<_> = old_coefficients.into_iter().collect();
            let map = branch_coefficients_parallel(
                &items,
                BranchParams {
                    stab_anticomm_bits,
                    destab_anticomm_bits,
                    odd_phase_mask,
                    phase_decomp,
                    coefficient_factor,
                    branch_factor,
                },
            );
            self.coefficients.reserve(map.len());
            for (idx, coeff) in map {
                if coeff.norm_sqr() > cutoff_sq {
                    self.coefficients.unsafe_insert(idx, coeff);
                }
            }
            return;
        }

        // Sequential sort-merge path: build nonbranch (nb), branch-values (brv), and a
        // packed branch-key stream, sort if needed, then 2-way merge directly into
        // self.coefficients with inline cutoff — no intermediate output Vec.
        //
        // Fast path: pack each (branch_key, build_pos) as a single u64
        // `(key << 16) | pos` and sort the u64 array. Half the data movement vs the
        // generic (I, u32) elements, and the most-optimised sort_unstable path.
        // Preconditions for packable: n_coefficients ≤ 0xFFFF (pos fits 16 bits) and
        // every branch key fits in 47 bits. Both hold for cultivation_d5 (≤13 active
        // bits, small coefficient counts). The fallback reproduces the prior behaviour
        // exactly for wide index types or large keys.
        let mut nb: Vec<(I, Complex<T::Coeff>)> = Vec::with_capacity(n_coefficients);
        let mut brv: Vec<Complex<T::Coeff>> = Vec::with_capacity(n_coefficients);
        let mut packed: Vec<u64> = Vec::with_capacity(n_coefficients);
        let mut packable = n_coefficients <= 0xFFFF;
        let mut nb_sorted = true;
        let mut prev: Option<I> = None;
        for (pos, (coeff, idx)) in (0_u32..).zip(old_coefficients) {
            let branch_index = idx ^ stab_anticomm_bits;
            let bpc = compute_phase_with_mask_static(
                destab_anticomm_bits,
                idx,
                stab_anticomm_bits,
                odd_phase_mask,
            );
            let branch_phase = (bpc + phase_decomp) % 4;
            let pf: Complex<T::Coeff> = COMPLEX_PHASE_CONVERSION[branch_phase as usize].into();
            brv.push(pf * coeff * branch_factor);
            match <u64 as num::NumCast>::from(branch_index) {
                Some(k) if k < (1u64 << 47) => packed.push((k << 16) | (pos as u64)),
                _ => {
                    packable = false;
                    packed.push(pos as u64);
                }
            }
            nb.push((idx, coeff * coefficient_factor));
            if let Some(p) = prev
                && idx < p
            {
                nb_sorted = false;
            }
            prev = Some(idx);
        }

        self.coefficients.reserve(nb.len() + brv.len());
        let mut i = 0;
        if packable {
            if !nb_sorted {
                nb.sort_unstable_by_key(|a| a.0);
            }
            packed.sort_unstable();
            // Decode the 47-bit key from the high bits of a packed entry using byte-by-byte
            // construction from I::from(u8) — avoids num::NumCast which panics for bnum types.
            let decode_key = |w: u64| -> I {
                let k = w >> 16; // k < 2^47; 6 bytes suffice
                let mut v = I::zero();
                for b in 0..6usize {
                    let byte = ((k >> (b * 8)) & 0xFF) as u8;
                    v |= <I as From<u8>>::from(byte) << (b * 8);
                }
                v
            };
            let mut j = 0;
            while i < nb.len() && j < packed.len() {
                let bp = (packed[j] & 0xFFFF) as usize;
                let bk = decode_key(packed[j]);
                match nb[i].0.cmp(&bk) {
                    std::cmp::Ordering::Less => {
                        if nb[i].1.norm_sqr() > cutoff_sq {
                            self.coefficients.unsafe_insert(nb[i].0, nb[i].1);
                        }
                        i += 1;
                    }
                    std::cmp::Ordering::Greater => {
                        let v = brv[bp];
                        if v.norm_sqr() > cutoff_sq {
                            self.coefficients.unsafe_insert(bk, v);
                        }
                        j += 1;
                    }
                    std::cmp::Ordering::Equal => {
                        let mut sv = nb[i].1;
                        sv += brv[bp];
                        if sv.norm_sqr() > cutoff_sq {
                            self.coefficients.unsafe_insert(nb[i].0, sv);
                        }
                        i += 1;
                        j += 1;
                    }
                }
            }
            while j < packed.len() {
                let bp = (packed[j] & 0xFFFF) as usize;
                let bk = decode_key(packed[j]);
                let v = brv[bp];
                if v.norm_sqr() > cutoff_sq {
                    self.coefficients.unsafe_insert(bk, v);
                }
                j += 1;
            }
        } else {
            // Fallback for wide index types, large keys (≥ 2^47), or many coefficients
            // (> 65535). nb is still in build order here; reconstruct brk from it before
            // sorting nb, so build-position p correctly indexes brv[p].
            let mut brk: Vec<(I, u32)> = (0_u32..)
                .zip(nb.iter())
                .map(|(p, &(idx, _))| (idx ^ stab_anticomm_bits, p))
                .collect();
            if !nb_sorted {
                nb.sort_unstable_by_key(|a| a.0);
            }
            brk.sort_unstable_by_key(|a| a.0);
            let mut j = 0;
            while i < nb.len() && j < brk.len() {
                let (bk, bp) = brk[j];
                match nb[i].0.cmp(&bk) {
                    std::cmp::Ordering::Less => {
                        if nb[i].1.norm_sqr() > cutoff_sq {
                            self.coefficients.unsafe_insert(nb[i].0, nb[i].1);
                        }
                        i += 1;
                    }
                    std::cmp::Ordering::Greater => {
                        let v = brv[bp as usize];
                        if v.norm_sqr() > cutoff_sq {
                            self.coefficients.unsafe_insert(bk, v);
                        }
                        j += 1;
                    }
                    std::cmp::Ordering::Equal => {
                        let mut sv = nb[i].1;
                        sv += brv[bp as usize];
                        if sv.norm_sqr() > cutoff_sq {
                            self.coefficients.unsafe_insert(nb[i].0, sv);
                        }
                        i += 1;
                        j += 1;
                    }
                }
            }
            while j < brk.len() {
                let (bk, bp) = brk[j];
                let v = brv[bp as usize];
                if v.norm_sqr() > cutoff_sq {
                    self.coefficients.unsafe_insert(bk, v);
                }
                j += 1;
            }
        }
        while i < nb.len() {
            if nb[i].1.norm_sqr() > cutoff_sq {
                self.coefficients.unsafe_insert(nb[i].0, nb[i].1);
            }
            i += 1;
        }
    }

    pub(crate) fn compute_coefficients_after_pauli_apply(
        &self,
        coefficients: &mut C,
        addr0: usize,
        pauli: Pauli,
    ) where
        <<T as Config>::Storage as BitView>::Store: PrimInt,
    {
        if self.is_inactive(addr0) {
            return;
        }

        let (phase_decomp, stab_anticomm_bits, destab_anticomm_bits) =
            self.compute_decomposition(addr0, pauli);

        let odd_phase_mask = self.odd_phase_destabilizer_mask();
        let n_coefficients = coefficients.len();
        let old_coefficients = std::mem::replace(coefficients, C::new());

        #[cfg(feature = "rayon")]
        let new_coefficients = if n_coefficients >= RAYON_COEFF_THRESHOLD {
            let items: Vec<_> = old_coefficients.into_iter().collect();
            apply_coefficients_parallel(
                &items,
                stab_anticomm_bits,
                destab_anticomm_bits,
                odd_phase_mask,
                phase_decomp,
            )
        } else {
            apply_coefficients_seq(
                old_coefficients,
                n_coefficients,
                stab_anticomm_bits,
                destab_anticomm_bits,
                odd_phase_mask,
                phase_decomp,
            )
        };

        #[cfg(not(feature = "rayon"))]
        let new_coefficients = apply_coefficients_seq(
            old_coefficients,
            n_coefficients,
            stab_anticomm_bits,
            destab_anticomm_bits,
            odd_phase_mask,
            phase_decomp,
        );

        let cutoff_sq = self.coefficient_threshold.clone() * self.coefficient_threshold.clone();
        coefficients.reserve(new_coefficients.len());
        for (idx, coeff) in new_coefficients {
            if coeff.norm_sqr() > cutoff_sq {
                coefficients.unsafe_insert(idx, coeff);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bnum::BUint;
    use ppvm_pauli_sum::config::fxhash::ByteF64;

    type TestConfig = ByteF64<1>;
    type TestTableau = GeneralizedTableau<TestConfig>;
    type TestTableauBUint = GeneralizedTableau<TestConfig, BUint<1>>;

    #[test]
    fn test_compute_phase_z_2_single_qubit_plus_state() {
        let mut tab: TestTableau = GeneralizedTableau::new(1, 1e-12);
        tab.tableau.h(0);

        // After H: stabilizer = +X, destabilizer = +Z
        // stab_anticomm_bits = 1 (stabilizer has xbit[0]=true)
        // both phases should be 0
        let (phase_decomp, stab_anticomm_bits, destab_anticomm_bits) =
            tab.compute_decomposition(0, Pauli::Z);
        let phase0 = phase_decomp + tab.compute_phase(destab_anticomm_bits, 0, stab_anticomm_bits);
        assert_eq!(phase0, 0);
        let phase1 = phase_decomp + tab.compute_phase(destab_anticomm_bits, 1, stab_anticomm_bits);
        assert_eq!(phase1, 0);
    }

    #[test]
    fn test_compute_phase_z_2_y_stabilizer() {
        let mut tab: TestTableau = GeneralizedTableau::new(1, 1e-12);
        tab.tableau.h(0);
        tab.tableau.s(0);

        let (phase_decomp, stab_anticomm_bits, destab_anticomm_bits) =
            tab.compute_decomposition(0, Pauli::Z);
        let phase0 = phase_decomp + tab.compute_phase(destab_anticomm_bits, 0, stab_anticomm_bits);
        assert_eq!(phase0, 0);
        let phase1 = phase_decomp + tab.compute_phase(destab_anticomm_bits, 1, stab_anticomm_bits);
        assert_eq!(phase1, 0);
    }

    #[test]
    fn test_compute_phase_z_2_mx_stabilizer() {
        let mut tab: TestTableau = GeneralizedTableau::new(1, 1e-12);
        tab.tableau.h(0);
        tab.tableau.z(0);

        let (phase_decomp, stab_anticomm_bits, destab_anticomm_bits) =
            tab.compute_decomposition(0, Pauli::Z);
        let phase0 = phase_decomp + tab.compute_phase(destab_anticomm_bits, 0, stab_anticomm_bits);
        assert_eq!(phase0, 0);
        let phase1 = phase_decomp + tab.compute_phase(destab_anticomm_bits, 1, stab_anticomm_bits);
        assert_eq!(phase1, 0);
    }

    #[test]
    fn test_compute_phase_z_2_y_stabilizer_2() {
        let mut tab: TestTableau = GeneralizedTableau::new(1, 1e-12);
        tab.tableau.h(0);
        tab.tableau.s(0);
        tab.tableau.h(0);

        let (phase_decomp, stab_anticomm_bits, destab_anticomm_bits) =
            tab.compute_decomposition(0, Pauli::Z);
        let phase0 =
            (phase_decomp + tab.compute_phase(destab_anticomm_bits, 0, stab_anticomm_bits)) % 4;
        assert_eq!(phase0, 1);
        let phase1 =
            (phase_decomp + tab.compute_phase(destab_anticomm_bits, 1, stab_anticomm_bits)) % 4;
        assert_eq!(phase1, 3);
    }

    #[test]
    fn test_index_type() {
        let mut tab: TestTableauBUint = GeneralizedTableau::new(1, 1e-12);
        tab.tableau.h(0);
    }

    /// Snapshot all rows (xbits, zbits, phase) for comparison.
    fn snapshot_tableau<C: Config>(tab: &Tableau<C>) -> Vec<(C::Storage, C::Storage, u8)> {
        tab.data
            .iter()
            .map(|pw| (pw.word.xbits.data, pw.word.zbits.data, pw.phase))
            .collect()
    }

    #[test]
    fn test_cz_block_pairs_matches_individual() {
        // Test CZ pairs (0,4), (1,5), (2,6), (3,7) — offset=4, count=4
        type TTab = Tableau<ByteF64<1>>;
        let n = 8;
        let base = 0;
        let offset = 4;
        let count = 4;

        let mut tab1 = TTab::new(n);
        Clifford::h(&mut tab1, 0);
        Clifford::h(&mut tab1, 3);
        Clifford::s(&mut tab1, 1);
        let mut tab2 = tab1.clone();

        // Individual
        for i in 0..count {
            Clifford::cz(&mut tab1, base + i, base + offset + i);
        }

        // Batch
        tab2.cz_block_pairs(base, offset, count);

        assert_eq!(snapshot_tableau(&tab1), snapshot_tableau(&tab2));
    }

    #[test]
    fn test_cz_block_pairs_offset_17() {
        // Simulate MSD-like CZ: (0,17), (1,18), ..., (16,33) — all in one u64 word
        use ppvm_pauli_sum::config::fx64hash::Byte8F64;
        type LargeTab = Tableau<Byte8F64<2>>;
        let n = 34;
        let mut tab1 = LargeTab::new(n);
        // Apply some gates to create non-trivial state
        for i in 0..n {
            Clifford::h(&mut tab1, i);
        }
        let mut tab2 = tab1.clone();

        // Individual
        for i in 0..17 {
            Clifford::cz(&mut tab1, i, 17 + i);
        }

        // Batch
        tab2.cz_block_pairs(0, 17, 17);

        assert_eq!(snapshot_tableau(&tab1), snapshot_tableau(&tab2));
    }

    #[test]
    fn test_cz_block_pairs_nonzero_base() {
        // Test CZ pairs starting from a non-zero base: (10,27), (11,28), ..., (14,31)
        // All within one u64 word (bits 0-63)
        use ppvm_pauli_sum::config::fx64hash::Byte8F64;
        type LargeTab = Tableau<Byte8F64<2>>;
        let n = 32;
        let base = 10;
        let offset = 17;
        let count = 5;

        let mut tab1 = LargeTab::new(n);
        for i in 0..n {
            Clifford::h(&mut tab1, i);
        }
        Clifford::s(&mut tab1, 12);
        Clifford::s(&mut tab1, 28);
        let mut tab2 = tab1.clone();

        for i in 0..count {
            Clifford::cz(&mut tab1, base + i, base + offset + i);
        }

        tab2.cz_block_pairs(base, offset, count);

        assert_eq!(snapshot_tableau(&tab1), snapshot_tableau(&tab2));
    }

    #[test]
    fn test_cz_block_pairs_single_pair() {
        // Degenerate case: count=1 should be same as one CZ
        type TTab = Tableau<ByteF64<1>>;
        let n = 8;
        let mut tab1 = TTab::new(n);
        Clifford::h(&mut tab1, 2);
        Clifford::s(&mut tab1, 5);
        let mut tab2 = tab1.clone();

        Clifford::cz(&mut tab1, 2, 5);
        tab2.cz_block_pairs(2, 3, 1);

        assert_eq!(snapshot_tableau(&tab1), snapshot_tableau(&tab2));
    }

    #[test]
    fn test_cz_block_pairs_zero_count() {
        // count=0 should be a no-op
        type TTab = Tableau<ByteF64<1>>;
        let n = 8;
        let mut tab1 = TTab::new(n);
        Clifford::h(&mut tab1, 0);
        let before = snapshot_tableau(&tab1);
        tab1.cz_block_pairs(0, 4, 0);
        assert_eq!(before, snapshot_tableau(&tab1));
    }

    #[test]
    fn test_generalized_tableau_cz_block_pairs() {
        // Test through GeneralizedTableau wrapper
        use ppvm_pauli_sum::config::fx64hash::Byte8F64;
        type GTab = GeneralizedTableau<Byte8F64<2>>;
        let n = 34;
        let mut tab1: GTab = GeneralizedTableau::new(n, 1e-12);
        for i in 0..n {
            Clifford::h(&mut tab1.tableau, i);
        }
        let mut tab2 = tab1.clone();

        // Individual via Clifford trait
        for i in 0..17 {
            Clifford::cz(&mut tab1, i, 17 + i);
        }

        // Batch
        tab2.cz_block_pairs(0, 17, 17);

        assert_eq!(
            snapshot_tableau(&tab1.tableau),
            snapshot_tableau(&tab2.tableau)
        );
    }

    #[test]
    fn test_generalized_tableau_cz_block_pairs_with_loss() {
        // When a qubit is lost, should fall back to individual CZ (skipping lost ones)
        type GTab = GeneralizedTableau<ByteF64<1>>;
        let n = 8;
        let mut tab1: GTab = GeneralizedTableau::new(n, 1e-12);
        for i in 0..n {
            Clifford::h(&mut tab1.tableau, i);
        }
        tab1.qubit_status[2] = QubitStatus::Lost; // Mark qubit 2 as lost
        let mut tab2 = tab1.clone();

        // Individual, skipping lost qubits
        for i in 0..4 {
            let c = i;
            let t = 4 + i;
            if !tab1.is_lost(c) && !tab1.is_lost(t) {
                Clifford::cz(&mut tab1.tableau, c, t);
            }
        }

        // Batch (should fall back internally)
        tab2.cz_block_pairs(0, 4, 4);

        assert_eq!(
            snapshot_tableau(&tab1.tableau),
            snapshot_tableau(&tab2.tableau)
        );
    }

    #[test]
    fn test_generalized_tableau_cz_block_pairs_with_leakage() {
        // Leaked qubits take the same inactive fallback as lost ones.
        type GTab = GeneralizedTableau<ByteF64<1>>;
        let n = 8;
        let mut tab1: GTab = GeneralizedTableau::new(n, 1e-12);
        for i in 0..n {
            Clifford::h(&mut tab1.tableau, i);
        }
        tab1.qubit_status[2] = QubitStatus::Leaked;
        let mut tab2 = tab1.clone();

        for i in 0..4 {
            let c = i;
            let t = 4 + i;
            if !tab1.is_inactive(c) && !tab1.is_inactive(t) {
                Clifford::cz(&mut tab1.tableau, c, t);
            }
        }

        tab2.cz_block_pairs(0, 4, 4);

        assert_eq!(
            snapshot_tableau(&tab1.tableau),
            snapshot_tableau(&tab2.tableau)
        );
    }

    #[test]
    fn test_cz_block_matches_individual_across_word_boundary() {
        // cz_block must split a run that straddles the u64 boundary into the
        // right within-word + cross-word segments. control_base=34,
        // target_base=51, count=17 reproduces the MSD ql[2]xql[3] sweep:
        // (34,51)..(46,63) in word 0, then (47,64)..(50,67) cross-word.
        use ppvm_pauli_sum::config::fx64hash::Byte8F64;
        type GTab = GeneralizedTableau<Byte8F64<2>>;
        let n = 85;
        let mut tab1: GTab = GeneralizedTableau::new(n, 1e-12);
        for i in 0..n {
            Clifford::h(&mut tab1.tableau, i);
        }
        let mut tab2 = tab1.clone();

        let (control_base, target_base, count) = (34, 51, 17);
        for i in 0..count {
            Clifford::cz(&mut tab1, control_base + i, target_base + i);
        }
        tab2.cz_block(control_base, target_base, count);

        assert_eq!(
            snapshot_tableau(&tab1.tableau),
            snapshot_tableau(&tab2.tableau)
        );

        // Reversed bases (CZ is symmetric) must give the same result.
        let mut tab3 = GeneralizedTableau::<Byte8F64<2>>::new(n, 1e-12);
        for i in 0..n {
            Clifford::h(&mut tab3.tableau, i);
        }
        tab3.cz_block(target_base, control_base, count);
        assert_eq!(
            snapshot_tableau(&tab1.tableau),
            snapshot_tableau(&tab3.tableau)
        );
    }

    /// Deterministic pseudo-random Clifford frame. `s`/`z` guarantee the rows
    /// carry pre-existing `z` bits *and* non-zero phases, which is what makes
    /// the fused block's `OR`ed `z` update and its single `count_ones() & 1`
    /// parity observable.
    fn prepare_frame<C: Config>(tab: &mut Tableau<C>, n: usize, seed: u64)
    where
        <C::Storage as BitView>::Store: PrimInt,
    {
        let mut s = seed.wrapping_mul(0x9E37_79B9_7F4A_7C15) | 1;
        let mut next = move || {
            s ^= s << 13;
            s ^= s >> 7;
            s ^= s << 17;
            (s >> 11) as usize
        };
        for _ in 0..6 * n {
            let q = next() % n;
            match next() % 6 {
                0 => Clifford::h(tab, q),
                1 => Clifford::s(tab, q),
                2 => Clifford::z(tab, q),
                3 => Clifford::x(tab, q),
                4 => Clifford::cnot(tab, q, (q + 1 + next() % (n - 1)) % n),
                _ => Clifford::cz(tab, q, (q + 1 + next() % (n - 1)) % n),
            }
        }
    }

    /// The fused CZ block must agree with the sequential per-pair `cz` loop
    /// even when the pairs' supports **overlap** (`offset < count`, or the
    /// degenerate `offset == 0`) — bits *and* sign.
    ///
    /// `czSeq_phase` proves the fused `count_ones() & 1` phase equals the
    /// sequential loop only under pairwise-disjoint supports, and
    /// `czSeq_phase_needs_disjoint` exhibits the counterexample replayed below.
    /// Adjacent-pair brickwork — `cz_block(0, 1, n)` — is precisely the
    /// overlapping shape, so the most natural call is the broken one (G-060).
    #[test]
    fn test_cz_block_matches_per_pair_loop_on_overlapping_pairs() {
        use ppvm_pauli_sum::config::fx64hash::Byte8F64;
        type GTab = GeneralizedTableau<Byte8F64<1>>;
        let n = 16;
        // (control_base, target_base, count); offset = target - control.
        let configs = [
            (0usize, 1usize, 2usize), // czSeq_phase_needs_disjoint's witness
            (0, 1, 3),
            (0, 1, 15), // adjacent-pair brickwork over the whole register
            (3, 4, 8),
            (0, 2, 5), // the ledger's second witness
            (2, 5, 9),
            (4, 4, 3), // degenerate: offset == 0, i.e. pairs (q, q)
            (0, 1, 1), // a single pair is disjoint: must stay on the fused path
            (0, 8, 8), // offset == count: the disjoint boundary
        ];
        for seed in 0..8u64 {
            let mut prepared: GTab = GeneralizedTableau::new(n, 1e-12);
            prepare_frame(&mut prepared.tableau, n, seed);
            // The frame must actually exercise the z plane and the phase.
            assert!(
                snapshot_tableau(&prepared.tableau)
                    .iter()
                    .any(|&(_, z, _)| z != [0]),
                "frame (seed {seed}) has no pre-existing z bits"
            );
            assert!(
                snapshot_tableau(&prepared.tableau)
                    .iter()
                    .any(|&(.., phase)| phase != 0),
                "frame (seed {seed}) has no non-zero phase"
            );

            for &(cb, tb, count) in &configs {
                let mut seq = prepared.clone();
                let mut fused = prepared.clone();
                let mut fused_pairs = prepared.clone();

                for i in 0..count {
                    Clifford::cz(&mut seq.tableau, cb + i, tb + i);
                }
                fused.cz_block(cb, tb, count);
                fused_pairs.cz_block_pairs(cb, tb - cb, count);

                assert_eq!(
                    snapshot_tableau(&seq.tableau),
                    snapshot_tableau(&fused.tableau),
                    "cz_block({cb}, {tb}, {count}) disagrees with the per-pair loop (seed {seed})"
                );
                assert_eq!(
                    snapshot_tableau(&seq.tableau),
                    snapshot_tableau(&fused_pairs.tableau),
                    "cz_block_pairs({cb}, {}, {count}) disagrees with the per-pair loop (seed {seed})",
                    tb - cb
                );
            }
        }
    }

    /// `czSeq_phase_needs_disjoint`'s exact witness, with the absolute answer
    /// derived from the Pauli algebra rather than from the loop: `CZ`
    /// conjugates `X⊗X ↦ +Y⊗Y` and `Y⊗X ↦ −X⊗Y`, so `CZ₁₂·CZ₀₁` sends
    /// `X₀X₁X₂` to `−Y₀X₁Y₂` — row coordinates `x = 111`, `z = 101`, phase `2`.
    #[test]
    fn test_cz_block_overlapping_pairs_hits_the_algebraic_answer() {
        use ppvm_pauli_sum::config::fx64hash::Byte8F64;
        type GTab = GeneralizedTableau<Byte8F64<1>>;
        let mut fused: GTab = GeneralizedTableau::new(3, 1e-12);
        // Row 0 is the destabilizer X₀; the two CNOTs spread it to X₀X₁X₂.
        Clifford::cnot(&mut fused.tableau, 0, 1);
        Clifford::cnot(&mut fused.tableau, 0, 2);
        let mut seq = fused.clone();
        assert_eq!(snapshot_tableau(&fused.tableau)[0], ([0b111], [0b000], 0));

        Clifford::cz(&mut seq.tableau, 0, 1);
        Clifford::cz(&mut seq.tableau, 1, 2);
        fused.cz_block(0, 1, 2);

        assert_eq!(snapshot_tableau(&seq.tableau)[0], ([0b111], [0b101], 2));
        assert_eq!(snapshot_tableau(&fused.tableau)[0], ([0b111], [0b101], 2));
    }

    /// Same algebraic witness as above, but through the public `Tableau`
    /// kernel — overlapping pairs must fall back, not `debug_assert` and
    /// corrupt in release.
    #[test]
    fn test_tableau_cz_block_pairs_overlapping_hits_the_algebraic_answer() {
        use ppvm_pauli_sum::config::fx64hash::Byte8F64;
        type TTab = Tableau<Byte8F64<1>>;
        let mut fused = TTab::new(3);
        Clifford::cnot(&mut fused, 0, 1);
        Clifford::cnot(&mut fused, 0, 2);
        assert_eq!(snapshot_tableau(&fused)[0], ([0b111], [0b000], 0));

        fused.cz_block_pairs(0, 1, 2);

        assert_eq!(snapshot_tableau(&fused)[0], ([0b111], [0b101], 2));
    }

    /// `Tableau::cz_block_pairs` itself, not only the `GeneralizedTableau`
    /// wrapper, must match the sequential loop on overlapping pairs.
    #[test]
    fn test_tableau_cz_block_pairs_matches_per_pair_loop_on_overlapping_pairs() {
        use ppvm_pauli_sum::config::fx64hash::Byte8F64;
        type TTab = Tableau<Byte8F64<1>>;
        let n = 16;
        let configs = [
            (0usize, 1usize, 2usize),
            (0, 1, 15),
            (0, 2, 5),
            (4, 4, 3),
            (0, 8, 8),
        ];
        for seed in 0..8u64 {
            let mut prepared = TTab::new(n);
            prepare_frame(&mut prepared, n, seed);
            for &(cb, tb, count) in &configs {
                let mut seq = prepared.clone();
                let mut fused = prepared.clone();
                for i in 0..count {
                    Clifford::cz(&mut seq, cb + i, tb + i);
                }
                fused.cz_block_pairs(cb, tb - cb, count);
                assert_eq!(
                    snapshot_tableau(&seq),
                    snapshot_tableau(&fused),
                    "Tableau::cz_block_pairs({cb}, {}, {count}) disagrees \
                     with the per-pair loop (seed {seed})",
                    tb - cb
                );
            }
        }
    }

    /// Overlapping brickwork that straddles a storage-word boundary: the
    /// segment split must not turn an overlapping run into a fused one.
    #[test]
    fn test_cz_block_overlapping_pairs_across_word_boundary() {
        use ppvm_pauli_sum::config::fx64hash::Byte8F64;
        type GTab = GeneralizedTableau<Byte8F64<2>>;
        let n = 85;
        let (control_base, target_base, count) = (60, 61, 10);
        for seed in 0..8u64 {
            let mut seq: GTab = GeneralizedTableau::new(n, 1e-12);
            prepare_frame(&mut seq.tableau, n, seed);
            let mut fused = seq.clone();

            for i in 0..count {
                Clifford::cz(&mut seq.tableau, control_base + i, target_base + i);
            }
            fused.cz_block(control_base, target_base, count);

            assert_eq!(
                snapshot_tableau(&seq.tableau),
                snapshot_tableau(&fused.tableau),
                "cz_block({control_base}, {target_base}, {count}) disagrees \
                 with the per-pair loop (seed {seed})"
            );
        }
    }

    // ─── reset_all ────────────────────────────────────────────────────

    /// `GeneralizedTableau::reset_all` restores the full state to a fresh
    /// `|0…0⟩` tableau: identical stabilizer/destabilizer rows and a single
    /// identity coefficient, even after non-Clifford branching.
    #[test]
    fn reset_all_restores_fresh_state() {
        let mut tab: TestTableau = GeneralizedTableau::new(3, 1e-12);
        let fresh: TestTableau = GeneralizedTableau::new(3, 1e-12);

        tab.h(0);
        tab.cnot(0, 1);
        tab.ry(2, 0.7); // non-Clifford: branches the coefficient vector
        assert!(
            tab.coefficients.len() > 1,
            "rotation should branch the coefficient vector"
        );

        tab.reset_all();

        assert_eq!(
            snapshot_tableau(&tab.tableau),
            snapshot_tableau(&fresh.tableau)
        );
        let coeffs: Vec<_> = tab.coefficients.to_vec();
        let fresh_coeffs: Vec<_> = fresh.coefficients.to_vec();
        assert_eq!(coeffs, fresh_coeffs);
    }

    /// A full reset clears the measurement record. Regression guard: an earlier
    /// version left it intact, so `current_measurement_record` returned stale
    /// outcomes after a reset.
    #[test]
    fn reset_all_clears_measurement_record() {
        let mut tab: TestTableau = GeneralizedTableau::new(2, 1e-12);
        tab.append_measurement_record(Some(true));
        tab.append_measurement_record(None);
        assert_eq!(tab.current_measurement_record().len(), 2);

        tab.reset_all();

        assert!(tab.current_measurement_record().is_empty());
    }

    /// A full reset clears per-qubit loss flags.
    #[test]
    fn reset_all_clears_loss_flags() {
        let mut tab: TestTableau = GeneralizedTableau::new(3, 1e-12);
        tab.qubit_status[0] = QubitStatus::Lost;
        tab.qubit_status[2] = QubitStatus::Lost;

        tab.reset_all();

        assert!(tab.qubit_status.iter().all(|&occ| occ == QubitStatus::Live));
    }

    /// A full reset clears per-qubit leakage flags.
    #[test]
    fn reset_all_clears_leak_flags() {
        let mut tab: TestTableau = GeneralizedTableau::new(3, 1e-12);
        tab.qubit_status[0] = QubitStatus::Leaked;
        tab.qubit_status[2] = QubitStatus::Leaked;

        tab.reset_all();

        assert!(tab.qubit_status.iter().all(|&occ| occ == QubitStatus::Live));
    }

    /// `Tableau::reset_all` restores the fresh identity tableau rows.
    #[test]
    fn tableau_reset_all_restores_fresh_rows() {
        let mut tab: Tableau<TestConfig> = Tableau::new(4);
        let fresh: Tableau<TestConfig> = Tableau::new(4);

        tab.h(0);
        tab.s(1);
        tab.h(3);

        tab.reset_all();

        assert_eq!(snapshot_tableau(&tab), snapshot_tableau(&fresh));
    }
}