cpx-mat2by2 0.1.3

To power the rust-quantum project, this library provides a robust AltMat type for a single qubit, specifically optimized for the intensive complex number multiplications required for quantum gate applications and tensor product operations.
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
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
//! The `cpx-mat2by2` library provides structures for representing and manipulating
//! quantum states and operators in a 2-dimensional Hilbert space (qubit).
//! It leverages the `cpx-coords` crate for complex number arithmetic.

use crate::BraKet::{Bra, Ket};
use crate::ProNil::{Nilpotent, Projector};
use core::cmp::{max, min};
use core::hash::Hash;
use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use cpx_coords::FloatExt;
use cpx_coords::*;
use std::collections::BTreeMap;

/// Enum representing the side of a state vector.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BraKet {
    /// A bra vector ⟨ψ|.
    Bra,
    /// A ket vector |ψ⟩.
    Ket,
}

/// Canonical normalized amplitudes representing a single-qubit pure state.
///
/// This form enforces uniqueness for hashing and equality by fixing the global phase:
/// - `c0 ∈ ℝ≥0` (real and non-negative)
/// - `|c0|² + |c1|² = 1` (normalization)
/// - If `c0 == 0`, then `c1 == Cpx::One {}` (canonical phase)
///
/// This ensures a normalized state vector with a unique, phase-fixed representation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct NormPair<T: FloatExt + Hash> {
    /// Real, non-negative amplitude of the |0⟩ basis state.
    pub c0: T,
    /// Complex amplitude of the |1⟩ basis state.
    pub c1: Cpx<T>,
}

/// A normalized single-qubit state in canonical form with orientation.
///
/// Wraps a `NormPair<T>` with a `BraKet` tag to indicate whether the state is a ket or bra.
/// The state is normalized and represented uniquely up to global phase.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct NormQubit<T: FloatExt + Hash> {
    /// Indicates if the state is a ket or bra.
    pub bra_ket: BraKet,
    /// Canonical normalized amplitudes for the qubit state.
    pub norm_pair: NormPair<T>,
}

/// A mixed single-qubit state formed by a probabilistic mixture of a normalized state and its orthogonal complement.
///
/// The state represents the mixture:
/// `ρ = prob × |ψ⟩⟨ψ| + (1 - prob) × |ψ⊥⟩⟨ψ⊥|`
///
/// - `prob ∈ [0.5, 1.0]` is the probability weight on the primary state.
/// - `norm_pair` defines the normalized pure state |ψ⟩ in canonical form.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MixPair<T: FloatExt + Hash> {
    /// Probability assigned to the pure state |ψ⟩ (must satisfy 0.5 ≤ prob ≤ 1.0).
    pub prob: T,
    /// Canonical normalized amplitudes defining the pure state |ψ⟩.
    pub norm_pair: NormPair<T>,
}

/// A mixed single-qubit state with bra/ket orientation.
///
/// Wraps a `MixPair<T>` along with a `BraKet` tag to indicate state orientation of the `MixPair<T>`.
/// Represents a probabilistic mixture of a normalized qubit and its orthogonal complement.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MixQubit<T: FloatExt + Hash> {
    /// Indicates if the `MixPair<T>` is in a ket or bra representation.
    pub bra_ket: BraKet,
    /// The probabilistic mixture of a pure state and its orthogonal complement.
    pub mix_pair: MixPair<T>,
}

/// A separable multi-qubit product state defined over an index interval.
///
/// The state is interpreted as a tensor product of individual qubit states over a specified
/// inclusive `interval`. Each mapped entry provides a qubit's canonical normalized state.
/// Missing indices are filled in using the default `padding` value.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct IdxToNorm<T: FloatExt + Hash> {
    /// Inclusive interval of qubit indices this product state spans: [start, end].
    pub interval: (usize, usize),
    /// Default qubit state used for indices in the interval not explicitly mapped.
    pub padding: NormPair<T>,
    /// Mapping from selected qubit indices to the normalized single-qubit states.
    pub idx_to_norm: BTreeMap<usize, NormPair<T>>,
}

/// A separable multi-qubit product state with bra/ket orientation.
///
/// Encapsulates a product of single-qubit states using a canonical index-to-state mapping (`IdxToNorm<T>`),
/// along with a `BraKet` tag indicating whether the state is a ket or bra.
/// Assumes qubits are independent and ordered by index.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SepQubits<T: FloatExt + Hash> {
    /// Indicates if the full state is a ket or bra.
    pub bra_ket: BraKet,
    /// Mapping from indices to single-qubit normalized states, with padding.
    pub idx_to_norm: IdxToNorm<T>,
}

/// A separable multi-qubit mixed state, where each qubit is independently in a mixed state.
///
/// Each qubit is represented by a `MixPair<T>`, describing a probabilistic mixture of a normalized
/// pure state and its orthogonal complement. The `idx_to_mix` map assigns such states to selected
/// qubit indices, while `padding` supplies a default value for indices not explicitly mapped.
///
/// Assumptions:
/// - No entanglement between qubits (fully separable state).
/// - Local mixing occurs independently at each qubit site.
/// - The state is defined over the inclusive `interval` of indices, with canonical ordering.
/// - The `bra_ket` field indicates whether this is a bra or ket state vector.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct IdxToMixQubits<T: FloatExt + Hash> {
    /// Indicates if the overall `MixPair<T>`s are in a ket or bra representation.
    pub bra_ket: BraKet,
    /// Inclusive interval of qubit indices this product state spans: [start, end].
    pub interval: (usize, usize),
    /// Default mixed state used for indices in the interval not explicitly mapped.
    pub padding: MixPair<T>,
    /// Mapping from selected qubit indices to independent mixed single-qubit states.
    pub idx_to_mix: BTreeMap<usize, MixPair<T>>,
}

/// Enum representing a type of rank-1 operator: either a projector or a nilpotent.
///
/// - `Projector` corresponds to `|ψ⟩⟨ψ|`, a Hermitian, idempotent rank-1 operator.
/// - `Nilpotent` corresponds to either `|ψ⟩⟨ψ⊥|` or `|ψ⊥⟩⟨ψ|`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ProNil {
    /// A Hermitian rank-1 projector: `|ψ⟩⟨ψ|`.
    Projector,
    /// A rank-1 nilpotent operator: `|ψ⟩⟨ψ⊥|` or `|ψ⊥⟩⟨ψ|`.
    Nilpotent,
}

/// A rank-1 operator (projector or nilpotent) constructed from a normalized qubit state.
///
/// Combines a `NormPair<T>` with an operator type and orientation tag. The `BraKet` field determines
/// the side (bra or ket) of the defining vector. For `Nilpotent`, the orthogonal complement is implied
/// on the opposite side of the rank-1 outer product.
///
/// Examples:
/// - `Projector` + `BraKet::Ket` with `|ψ⟩` → `|ψ⟩⟨ψ|`
/// - `Nilpotent` + `BraKet::Bra` with `⟨ψ|` → `|ψ⊥⟩⟨ψ|`
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Rk1PN<T: FloatExt + Hash> {
    /// Operator type: projector or nilpotent.
    pub pro_nil: ProNil,
    /// Indicates whether the vector is a bra or ket.
    pub bra_ket: BraKet,
    /// Canonical normalized qubit state defining the operator.
    pub norm_pair: NormPair<T>,
}

/// A general rank-1 matrix of the form`M = scalar × |ket⟩⟨bra|`.
///
/// This representation explicitly separates the ket (column) and bra (row) components of the matrix.
/// The underlying vectors are normalized and canonically phase-fixed via `NormPair<T>`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Rk1KB<T: FloatExt + Hash> {
    /// The ket vector `|ψ⟩` defining the column part of the outer product.
    pub ket: NormPair<T>,
    /// The bra vector `⟨ϕ|` defining the row part of the outer product.
    pub bra: NormPair<T>,
}

/// Enum representing a general rank-1 matrix operator.
///
/// This can be either a structured operator (`ProNil`) such as a projector or nilpotent,
/// or a fully general rank-1 matrix formed from arbitrary normalized vectors.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Rank1<T: FloatExt + Hash> {
    /// A structured rank-1 operator: projector or nilpotent.
    ProNil(Rk1PN<T>),
    /// A general rank-1 matrix: `|ket⟩⟨bra|` with independent canonical vectors.
    Other(Rk1KB<T>),
}

/// Struct representing a raw 2x2 complex matrix.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RawMat<T: FloatExt + Hash> {
    /// A raw 2x2 complex matrix.
    pub mat: [[Cpx<T>; 2]; 2],
}

/// Represents a general 2×2 complex matrix using the Pauli (complex-quaternion) basis.
///
/// The matrix is expressed as a linear combination of Pauli matrices:
/// `M = a₀·I + a₁·X + a₂·Y + a₃·Z`, where each `aᵢ` is a complex coefficient (`Cpx<T>`).
///
/// This representation is useful for compact encode general 2×2 complex operators using the Pauli basis.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct CpxQuaternion<T: FloatExt + Hash> {
    /// Coefficients `[a₀, a₁, a₂, a₃]` corresponding to the Pauli basis `{I, X, Y, Z}`, in that order.
    pub coefficients: [Cpx<T>; 4],
}

/// Represents a 2×2 Hermitian matrix using the Pauli (complex-quaternion) basis.
///
/// The matrix is expressed as a real linear combination of Pauli matrices:
/// `M = a₀·I + a₁·X + a₂·Y + a₃·Z`, where each `aᵢ` is a real coefficient (`T`).
///
/// This form compactly encodes Hermitian 2×2 operators.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RealQuaternion<T: FloatExt + Hash> {
    /// Real coefficients `[a₀, a₁, a₂, a₃]` corresponding to the Pauli basis `{I, X, Y, Z}`, in that order.
    pub coefficients: [T; 4],
}

/// Represents a 2×2 complex matrix with unit determinant, expressed in the Pauli (complex-quaternion) basis.
///
/// The matrix is written as a linear combination of Pauli matrices:
/// `M = a₀·I + a₁·X + a₂·Y + a₃·Z`, where each `aᵢ` is a complex coefficient.
///
/// This struct stores only the non-identity coefficients `(a₁, a₂, a₃)`
/// as fields `x`, `y`, and `z`, corresponding to the Pauli matrices `X`, `Y`, and `Z`.
/// The identity coefficient `a₀` is computed implicitly to ensure `det(M) = 1`, using:
///
/// ```text
/// det(M) = a₀² − a₁² − a₂² − a₃² = 1
/// ⇒ a₀ = sqrt(1 + a₁² + a₂² + a₃²)
/// ```
///
/// This compact form is well suited for representing special unitary matrices (SU(2)),
/// rotations on the Bloch sphere, and Pauli-based gate decompositions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Det1CpxQuaternion<T: FloatExt + Hash> {
    /// Coefficient of the Pauli X component.
    pub x: Cpx<T>,

    /// Coefficient of the Pauli Y component.
    pub y: Cpx<T>,

    /// Coefficient of the Pauli Z component.
    pub z: Cpx<T>,
}

/// Represents a 2×2 Hermitian matrix with unit determinant, expressed in the Pauli (real-quaternion) basis.
///
/// As with the complex case, the matrix is expressed as:
/// `M = a₀·I + a₁·X + a₂·Y + a₃·Z`, but each `aᵢ` is real, and the identity component
/// `a₀` is computed implicitly to satisfy the determinant constraint:
///
/// ```text
/// det(M) = a₀² − a₁² − a₂² − a₃² = 1
/// ⇒ a₀ = sqrt(1 + a₁² + a₂² + a₃²)
/// ```
///
/// This form is useful for encoding Hermitian, unit-determinant operators, such as traceless generators
/// of real-valued quantum dynamics or normalized observables.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Det1RealQuaternion<T: FloatExt + Hash> {
    /// Coefficient of the Pauli X component.
    pub x: T,

    /// Coefficient of the Pauli Y component.
    pub y: T,

    /// Coefficient of the Pauli Z component.
    pub z: T,
}

/// Represents a 2×2 complex matrix with unit determinant, expressed in the Pauli basis.
///
/// This enum distinguishes between:
/// - `Hermitian`: real-valued Pauli coefficients (used for Hermitian unit-determinant matrices),
/// - `Other`: complex-valued Pauli coefficients (for general SU(2) matrices).
///
/// Both variants implicitly encode the identity component `a₀` to satisfy `det(M) = 1`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Det1<T: FloatExt + Hash> {
    /// A Hermitian matrix with real-valued Pauli components.
    Hermitian(Det1RealQuaternion<T>),

    /// A general complex matrix with potentially non-Hermitian structure.
    Other(Det1CpxQuaternion<T>),
}

/// Represents a 2×2 complex matrix of rank 1 or 2, in a compact structured form.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RankOneTwo<T: FloatExt + Hash> {
    /// A rank-1 matrix of the form`M = |ket⟩⟨bra|`.
    ///
    /// This variant encodes a matrix as the outer product of a normalized ket and bra.
    Rank1(Rank1<T>),

    /// A full-rank 2×2 complex matrix with unit determinant.
    ///
    /// The matrix is expressed in the Pauli (quaternion) basis, and may be Hermitian or non-Hermitian.
    Rank2(Det1<T>),
}

/// Represents a tensor product of local 2×2 complex operators acting on specific qubit indices.
///
/// This structure encodes a sparse tensor product of single-qubit operators. Each mapped entry
/// specifies a `RankOneTwo<T>` acting on a particular qubit index. The full operator is
/// constructed as an ordered tensor product over a specified interval of qubit indices. Indices
/// not explicitly mapped use the provided `padding` operator.
///
/// This is useful for compactly representing structured operators such as Kraus operators.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct LocalOps<T: FloatExt + Hash> {
    /// Inclusive interval of qubit indices spanned by the tensor product: `[start, end]`.
    pub interval: (usize, usize),

    /// Default `RankOneTwo<T>` used for unmapped qubit indices within the interval.
    pub padding: RankOneTwo<T>,

    /// Mapping from selected qubit indices to non-default local operators.
    pub idx_to_mat: BTreeMap<usize, RankOneTwo<T>>,
}

/// Returns the inclusive interval `[min, max]` spanned by the keys of a BTreeMap.
/// Falls back to `default` if the map is empty.
pub fn key_interval_or<K: Copy + Ord>(map: &BTreeMap<K, impl Sized>, default: (K, K)) -> (K, K) {
    match (map.keys().next().copied(), map.keys().next_back().copied()) {
        (Some(min), Some(max)) => (min, max),
        _ => default,
    }
}

/// Swaps the values at keys `i` and `j` in a BTreeMap if either is present.
/// No effect if both keys are absent.
pub fn swap_btree_entries<K: Ord + Copy, V>(map: &mut BTreeMap<K, V>, i: K, j: K) {
    if i == j {
        return;
    }

    let i_val = map.remove(&i);
    let j_val = map.remove(&j);

    if let Some(val) = j_val {
        map.insert(i, val);
    }
    if let Some(val) = i_val {
        map.insert(j, val);
    }
}

impl BraKet {
    /// Returns the Hermitian conjugate (dagger) of the current `BraKet` variant.
    ///
    /// - `Bra` becomes `Ket`
    /// - `Ket` becomes `Bra`
    ///
    /// This operation corresponds to taking the adjoint of a vector:
    /// - `|ψ⟩† = ⟨ψ|`
    /// - `⟨ψ|† = |ψ⟩`
    pub fn dag(&self) -> Self {
        match self {
            Bra => Ket,
            Ket => Bra,
        }
    }
}

impl<T: FloatExt + Hash> NormPair<T> {
    /// Conjugates the norm_pair.
    pub fn conj(&self) -> Self {
        Self {
            c0: self.c0,
            c1: self.c1.conj(),
        }
    }
    /// Returns an orthogonal normalized qubit state.
    ///
    /// Given a qubit `|ψ⟩ = c₀|0⟩ + c₁|1⟩`, this returns an orthogonal state
    /// `|ψ⊥⟩ = c₁*|0⟩ − c₀*|1⟩`, normalized and up to a global phase.
    pub fn orthogonal(&self) -> Self {
        let (c1_mag, c1_phase) = self.c1.factor_out();
        let new_c0 = c1_mag.re(); // This radius from c1 is always non-negative.

        match new_c0 {
            val if val == T::zero() => Self {
                c0: T::zero(),
                c1: Cpx::ONE,
            },
            val if val == T::one() => Self {
                c0: T::one(),
                c1: Cpx::ZERO,
            },
            _ => Self {
                c0: new_c0,
                c1: -c1_phase.conj() * self.c0,
            },
        }
    }
    /// Factors out a global amplitude and phase to produce a canonical form.
    ///
    /// This ensures a unique and hashable representation by:
    /// - Forcing `c0 ∈ ℝ≥0` (non-negative real),
    /// - Normalizing the pair to unit ℓ₂-norm,
    /// - Extracting any global phase into a separate complex scalar.
    ///
    /// Returns:
    /// - `None` if both amplitudes are zero.
    /// - Otherwise, returns a triple `(canonical, sqrt_prob, phase)` such that: original = sqrt_prob × phase × canonical
    ///   , where:
    /// - `canonical` is a `NormPair` with unit norm and canonical phase,
    /// - `sqrt_prob ∈ ℝ≥0` is the amplitude magnitude,
    /// - `phase ∈ ℂ` is the global phase factor.
    pub fn try_factor_out(&self) -> Option<(Self, T, Cpx<T>)> {
        let zero = T::zero();
        let one = T::one();

        match (self.c0 == zero, matches!(self.c1, Cpx::Zero {})) {
            (true, true) => None,
            (true, false) => {
                let (sqrt_prob, ph) = self.c1.factor_out();
                Some((
                    Self {
                        c0: zero,
                        c1: Cpx::ONE,
                    },
                    sqrt_prob.re(),
                    ph,
                ))
            }
            (false, true) => {
                let c0_sign = if self.c0 > zero {
                    Cpx::ONE
                } else {
                    Cpx::NEG_ONE
                };
                Some((
                    Self {
                        c0: one,
                        c1: Cpx::ZERO,
                    },
                    self.c0.abs(),
                    c0_sign,
                ))
            }
            (false, false) => {
                // Normalize and extract global phase from c0
                let norm = self.c0.hypot(self.c1.rad());
                let c0_sign = if self.c0 > zero { one } else { -one };
                let c0_real = (self.c0 / norm).abs();
                let c1_rel = self.c1 / norm * c0_sign;

                Some((
                    Self {
                        c0: c0_real,
                        c1: c1_rel,
                    },
                    norm,
                    Cpx::Real { re: c0_sign },
                ))
            }
        }
    }
}

impl<T: FloatExt + Hash> NormQubit<T> {
    /// Returns the Hermitian conjugate (dagger) of the qubit representation,
    /// flipping `Bra` ↔ `Ket` and complex-conjugating the internal coefficients.
    pub fn dag(&self) -> Self {
        Self {
            norm_pair: self.norm_pair.conj(),
            bra_ket: self.bra_ket.dag(),
        }
    }
    /// Converts the `NormQubit` to its bra representation.
    /// If it is currently a ket, then return the Hermitian conjugate (dagger).
    pub fn to_bra(&self) -> Self {
        match self.bra_ket {
            Bra => *self,
            Ket => self.dag(),
        }
    }
    /// Converts the `NormQubit` to its ket representation.
    /// If it is currently a bra, then return the Hermitian conjugate (dagger).
    pub fn to_ket(&self) -> Self {
        match self.bra_ket {
            Ket => *self,
            Bra => self.dag(),
        }
    }
    /// Returns a normalized qubit orthogonal to `self`.
    pub fn orthogonal(&self) -> Self {
        Self {
            bra_ket: self.bra_ket,
            norm_pair: self.norm_pair.orthogonal(),
        }
    }
    /// Factors out a global amplitude and phase from the normalized qubit state.
    ///
    /// This provides a canonical decomposition of the form: original = sqrt_prob × phase × canonical, where:
    /// - `canonical` is a `NormQubit` with unit ℓ₂-norm and canonical phase,
    /// - `sqrt_prob ∈ ℝ≥0` is the size of the original state vector,
    /// - `phase ∈ ℂ` is the global complex phase.
    ///
    /// The result ensures:
    /// - `norm_pair.c0 ∈ ℝ≥0`
    /// - `canonical` is uniquely determined up to this constraint
    ///
    /// Returns:
    /// - `None` if both amplitudes are zero.
    /// - `Some(( canonical, sqrt_prob, phase))` otherwise.
    pub fn try_factor_out(&self) -> Option<(Self, T, Cpx<T>)> {
        self.norm_pair
            .try_factor_out()
            .map(|(norm_pair, sqrt_prob, phase)| {
                (
                    Self {
                        norm_pair,
                        bra_ket: self.bra_ket,
                    },
                    sqrt_prob,
                    phase,
                )
            })
    }
    /// Computes the inner product ⟨self|ket⟩ between two normalized qubits.
    ///
    /// `self` is interpreted as a bra, and `ket` as a ket.
    /// Automatically converts representations if necessary.
    pub fn inner(&self, ket: &Self) -> Cpx<T> {
        let bra_pair = self.to_bra().norm_pair;
        let ket_pair = ket.to_ket().norm_pair;
        (bra_pair.c1 * ket_pair.c1) + (bra_pair.c0 * ket_pair.c0)
    }
    /// Computes the outer product `|self⟩⟨other|` between two normalized qubit states,
    /// returning a canonical rank-1 matrix representation.
    ///
    /// This method interprets:
    /// - `self` as a ket vector (`|ψ⟩`)
    /// - `bra` as a bra vector (`⟨φ|`)
    ///
    /// It automatically converts the internal orientations (`BraKet`) of each input
    /// to ensure proper `ket–bra` alignment. The resulting operator is classified as:
    ///
    /// - `Projector` if `⟨bra| == ⟨self|` (i.e., `|self⟩⟨self|`);
    /// - `Nilpotent` if `⟨bra| == ⟨self⊥|` (i.e., `|self⟩⟨self⊥|`);
    /// - `Rank1::Other` for a general outer product `|ket⟩⟨bra|`.
    ///
    /// This ensures canonical detection and representation of projector and nilpotent
    /// operators when applicable.
    pub fn outer(&self, bra: &Self) -> Rank1<T> {
        let ket = self.to_ket().norm_pair;
        let bra = bra.to_bra().norm_pair;
        let conj_ket = ket.conj();

        if conj_ket == bra {
            Rank1::ProNil(Rk1PN {
                pro_nil: Projector,
                bra_ket: Ket,
                norm_pair: ket,
            })
        } else if conj_ket.orthogonal() == bra {
            Rank1::ProNil(Rk1PN {
                pro_nil: Nilpotent,
                bra_ket: Ket,
                norm_pair: ket,
            })
        } else {
            Rank1::Other(Rk1KB { ket, bra })
        }
    }
}

impl<T: FloatExt + Hash> MixPair<T> {
    /// Returns the complex conjugate of this mixed state.
    ///
    /// Conjugates the underlying normalized state while preserving the probability weight.
    pub fn conj(&self) -> Self {
        Self {
            prob: self.prob,
            norm_pair: self.norm_pair.conj(),
        }
    }

    /// Returns the orthogonal mixed state with the same probability.
    ///
    /// Constructs a new mixed state whose primary component is orthogonal to the original.
    pub fn orthogonal(&self) -> Self {
        Self {
            prob: self.prob,
            norm_pair: self.norm_pair.orthogonal(),
        }
    }

    /// Computes the purity of the mixed state: `Trace(ρ²) = p² + (1 - p)²`.
    ///
    /// Purity ranges from `0.5` (maximally mixed) to `1.0` (pure state).
    pub fn purity(&self) -> T {
        let p = self.prob;
        p.powi(2) + (T::one() - p).powi(2)
    }

    /// Computes the von Neumann entropy of the mixed state.
    ///
    /// Returns `−p ln(p) − (1 − p) ln(1 − p)` if `p ∈ (0, 1)`, and `0` if pure.
    /// Entropy is zero for pure states and maximal (ln 2) when `p = 0.5`.
    pub fn entropy_vn(&self) -> T {
        let zero = T::zero();
        let one = T::one();
        let p = self.prob;
        if (p != one) && (p != zero) {
            -p * p.ln() - (one - p) * (one - p).ln()
        } else {
            zero
        }
    }
}

impl<T: FloatExt + Hash> MixQubit<T> {
    /// Returns the Hermitian conjugate (dagger) of this mixed qubit state.
    ///
    /// Flips the `BraKet` orientation while preserving the mixed content.
    pub fn dag(&self) -> Self {
        Self {
            bra_ket: self.bra_ket.dag(),
            mix_pair: self.mix_pair.conj(),
        }
    }

    /// Returns the orthogonal mixed state with the same orientation and probability.
    ///
    /// The primary and orthogonal components are swapped, maintaining the same weighting.
    pub fn orthogonal(&self) -> Self {
        Self {
            bra_ket: self.bra_ket,
            mix_pair: self.mix_pair.orthogonal(),
        }
    }

    /// Returns the purity `Trace(ρ²)` of the underlying mixed state.
    pub fn purity(&self) -> T {
        self.mix_pair.purity()
    }

    /// Returns the von Neumann entropy of the underlying mixed state.
    pub fn entropy_vn(&self) -> T {
        self.mix_pair.entropy_vn()
    }
}

impl<T: FloatExt + Hash> IdxToNorm<T> {
    /// Returns the complex conjugate of this `IdxToNorm`.
    ///
    /// Applies conjugation to all `NormPair<T>` entries and the padding.
    pub fn conj(&self) -> Self {
        Self {
            interval: self.interval,
            padding: self.padding.conj(),
            idx_to_norm: self
                .idx_to_norm
                .iter()
                .map(|(k, v)| (*k, v.conj()))
                .collect(),
        }
    }

    /// Factors out the global amplitude and phase from each `NormPair`,
    /// returning a new `IdxToNorm` with canonicalized entries,
    /// and extracting the total amplitude and phase.
    ///
    /// Returns:
    /// - `None` if any `NormPair` fails to factor (e.g., is zero),
    /// - Otherwise, returns `(canonicalized_self, total_sqrt_prob, total_phase)`.
    pub fn try_factor_out(&self) -> Option<(Self, T, Cpx<T>)> {
        let mut idx_to_norm = BTreeMap::new();
        let mut acc_sqrt_prob = T::one();
        let mut acc_phase = Cpx::ONE;

        for (&k, v) in &self.idx_to_norm {
            let (canon, sqrt_prob, phase) = v.try_factor_out()?;
            idx_to_norm.insert(k, canon);
            acc_sqrt_prob = acc_sqrt_prob * sqrt_prob;
            acc_phase *= phase;
        }

        Some((
            Self {
                interval: self.interval,
                padding: self.padding,
                idx_to_norm,
            },
            acc_sqrt_prob,
            acc_phase,
        ))
    }

    /// Returns the list of qubit indices that have explicitly assigned normalized states.
    pub fn indices_keys(&self) -> Vec<usize> {
        self.idx_to_norm.keys().copied().collect()
    }

    /// Returns the inclusive interval of explicitly assigned qubit indices, if any.
    ///
    /// This is equivalent to `(min_index, max_index)` over the `idx_to_norm` map.
    /// Returns `(None, None)` if the map is empty.
    pub fn try_interval(&self) -> (Option<usize>, Option<usize>) {
        let min = self.idx_to_norm.keys().next().copied();
        let max = self.idx_to_norm.keys().next_back().copied();
        (min, max)
    }

    /// Returns a new `IdxToNorm` with its interval adjusted to match the minimum and maximum keys
    /// present in the `idx_to_norm` map.
    ///
    /// This method is useful for automatically syncing the `interval` field with the actual
    /// span of explicitly defined qubit indices.
    ///
    /// # Returns
    /// - `Some(Self)` with the updated interval if `idx_to_norm` is non-empty.
    /// - `None` if `idx_to_norm` is empty, as there are no keys to determine an interval.
    pub fn try_fit_interval(&self) -> Option<Self> {
        let (min, max) = self.try_interval();
        Some(Self {
            interval: (min?, max?),
            padding: self.padding,
            idx_to_norm: self.idx_to_norm.clone(),
        })
    }

    /// Returns a new `IdxToNorm` where all indices in the interval are explicitly filled.
    ///
    /// For any index within `self.interval` that does not appear in `idx_to_norm`, this method
    /// inserts an entry with the `padding` value. The result is a fully populated BTreeMap with
    /// no missing indices in the defined interval.
    pub fn extract_padding(&self) -> Self {
        let mut idx_to_norm = self.idx_to_norm.clone();
        let (start, end) = self.interval;

        for i in start..=end {
            idx_to_norm.entry(i).or_insert(self.padding);
        }

        Self {
            interval: self.interval,
            padding: self.padding,
            idx_to_norm,
        }
    }

    /// Returns a new `IdxToNorm` with the specified interval, leaving all other fields unchanged.
    ///
    /// This method updates the `interval` field while cloning the existing `padding`
    /// and `idx_to_norm` map.
    pub fn set_interval(&self, interval: (usize, usize)) -> Self {
        Self {
            interval,
            padding: self.padding,
            idx_to_norm: self.idx_to_norm.clone(),
        }
    }

    /// Returns a new `IdxToNorm` with the specified padding value,
    /// leaving the interval and index-to-norm map unchanged.
    pub fn set_padding(&self, padding: NormPair<T>) -> Self {
        Self {
            interval: self.interval,
            padding,
            idx_to_norm: self.idx_to_norm.clone(),
        }
    }
    /// Returns a new `IdxToNorm` with the specified indices removed from `idx_to_norm`.
    ///
    /// This method creates a copy of the current structure and removes any entries whose keys
    /// match the provided indices. The original `interval` and `padding` are preserved.
    ///
    /// # Type Parameters
    /// - `I`: An iterable collection of `usize` indices to remove.
    ///
    /// # Arguments
    /// - `indices`: Any iterable over `usize` values (e.g., a `Vec`, array, range, or `HashSet`).
    ///
    /// # Returns
    /// A new `IdxToNorm` with the specified indices removed from the map.
    ///
    /// # Examples
    /// ```rust
    /// //let pruned = original.discard(vec![1, 2, 4]);
    /// //let pruned = original.discard([3, 5, 7]);
    /// //let pruned = original.discard(0..=10);
    /// ```
    pub fn discard<I: IntoIterator<Item = usize>>(&self, indices: I) -> Self {
        let mut new_map = self.idx_to_norm.clone();
        for idx in indices {
            new_map.remove(&idx);
        }

        Self {
            interval: self.interval,
            padding: self.padding,
            idx_to_norm: new_map,
        }
    }
    /// Returns a new `IdxToNorm` with additional entries merged into `idx_to_norm`.
    ///
    /// For each `(index, norm_pair)` in the provided `idx_to_norm` map, if the index is not already present
    /// in the current map, it is inserted. Existing entries are left unchanged.
    ///
    /// The resulting interval is updated to span the full range of indices in the combined map, unless the map
    /// becomes empty, in which case the original interval is retained.
    ///
    /// # Arguments
    ///
    /// * `idx_to_norm` - A `BTreeMap<usize, NormPair<T>>` containing new entries to insert.
    ///
    /// # Returns
    ///
    /// A new `IdxToNorm` with the merged map and updated interval.
    pub fn extend(&self, idx_to_norm: BTreeMap<usize, NormPair<T>>) -> Self {
        let mut new_map = self.idx_to_norm.clone();
        for (k, v) in idx_to_norm {
            new_map.entry(k).or_insert(v);
        }

        let interval = key_interval_or(&new_map, self.interval);

        Self {
            interval,
            padding: self.padding,
            idx_to_norm: new_map,
        }
    }

    /// Returns a new `IdxToNorm` with the given entries forcibly updated.
    ///
    /// For each `(index, norm_pair)` in the provided map:
    /// - Any existing entry at that index is removed.
    /// - The new entry is inserted unconditionally.
    ///
    /// The updated `idx_to_norm` map reflects all changes, and the interval is recomputed to
    /// span the full range of keys present in the new map (if non-empty).
    ///
    /// # Arguments
    ///
    /// * `idx_to_norm` - A `BTreeMap<usize, NormPair<T>>` containing entries to overwrite.
    ///
    /// # Returns
    ///
    /// A new `IdxToNorm` with the forcibly updated entries and recalculated interval.
    pub fn force_update(&self, idx_to_norm: BTreeMap<usize, NormPair<T>>) -> Self {
        let mut new_map = self.idx_to_norm.clone();

        for (k, v) in idx_to_norm {
            new_map.insert(k, v); // `insert` already replaces the value if key exists
        }

        let interval = key_interval_or(&new_map, self.interval);

        Self {
            interval,
            padding: self.padding,
            idx_to_norm: new_map,
        }
    }
    /// Returns a new `IdxToNorm` with the values at two indices swapped.
    ///
    /// If both indices exist in `idx_to_norm`, their values are swapped.
    /// If only one exists, its value is moved to the other index.
    /// If neither exist, the mapping is unchanged.
    ///
    /// # Arguments
    /// * `i` - The first index.
    /// * `j` - The second index.
    pub fn swap(&self, i: usize, j: usize) -> Self {
        if i == j {
            return self.clone(); // No-op
        }

        let mut new_map = self.idx_to_norm.clone();
        swap_btree_entries(&mut new_map, i, j);

        let interval = key_interval_or(&new_map, self.interval);

        Self {
            interval,
            padding: self.padding,
            idx_to_norm: new_map,
        }
    }
}

impl<T: FloatExt + Hash> SepQubits<T> {
    /// Returns the Hermitian conjugate of the separable multi-qubit state.
    pub fn dag(&self) -> Self {
        Self {
            bra_ket: self.bra_ket.dag(),
            idx_to_norm: self.idx_to_norm.conj(),
        }
    }

    /// Converts the `SepQubit` to its bra representation.
    /// If it is currently a ket, then return the Hermitian conjugate (dagger).
    pub fn to_bra(&self) -> Self {
        match self.bra_ket {
            Bra => self.clone(),
            Ket => self.dag(),
        }
    }

    /// Converts the `SepQubit` to its ket representation.
    /// If it is currently a bra, then return the Hermitian conjugate (dagger).
    pub fn to_ket(&self) -> Self {
        match self.bra_ket {
            Ket => self.clone(),
            Bra => self.dag(),
        }
    }

    /// Factors out a global amplitude and phase from all `NormPair`s in the `SepQubit`.
    ///
    /// Returns:
    /// - `None` if any entry fails to factor (e.g., all-zero),
    /// - Otherwise, the canonicalized structure and overall factor.
    ///
    /// Satisfies: `original = sqrt_prob × phase × canonical`.
    pub fn try_factor_out(&self) -> Option<(Self, T, Cpx<T>)> {
        let (idx_to_norm, sqrt_prob, phase) = self.idx_to_norm.try_factor_out()?;
        Some((
            Self {
                bra_ket: self.bra_ket,
                idx_to_norm,
            },
            sqrt_prob,
            phase,
        ))
    }

    /// Computes the inner product ⟨self|ket⟩ between two `SepQubits<T>`, using the union of their intervals.
    ///
    /// This method interprets `self` as a bra and `ket` as a ket, aligning their index intervals and
    /// applying padding where necessary. Only indices within the specified interval are used;
    /// entries outside the interval (even if present in the underlying `BTreeMap`) are ignored.
    ///
    /// The inner product is computed as a product of local inner products at each index:
    /// ⟨ψ|ϕ⟩ = ∏ᵢ ⟨ψᵢ|ϕᵢ⟩.
    /// Returns `Cpx::ZERO` early if any local inner product is zero, and skips multiplying by `1` for efficiency.
    pub fn inner(&self, ket: &Self) -> Cpx<T> {
        let bra = self.to_bra().idx_to_norm;
        let ket = ket.to_ket().idx_to_norm;

        let (min, max) = {
            let (bra_min, bra_max) = bra.interval;
            let (ket_min, ket_max) = ket.interval;
            (min(bra_min, ket_min), max(bra_max, ket_max))
        };

        let bra = bra.set_interval((min, max)).extract_padding();
        let ket = ket.set_interval((min, max)).extract_padding();

        let mut acc = Cpx::ONE;
        for idx in min..max {
            let NormPair { c0: b0, c1: b1 } = bra.idx_to_norm[&idx];
            let NormPair { c0: k0, c1: k1 } = ket.idx_to_norm[&idx];
            let inner = b1 * k1 + b0 * k0;
            if matches!(inner, Cpx::Zero {}) {
                return Cpx::ZERO;
            } else if matches!(inner, Cpx::One {}) {
                continue;
            }
            acc *= inner;
        }

        acc
    }

    /// Returns a vector of all explicit qubit indices.
    pub fn indices_keys(&self) -> Vec<usize> {
        self.idx_to_norm.indices_keys()
    }

    /// Returns the inclusive interval of explicitly assigned qubit indices, if any.
    ///
    /// This is equivalent to `(min_index, max_index)` over the `idx_to_norm` map.
    /// Returns `(None, None)` if the map is empty.
    pub fn try_interval(&self) -> (Option<usize>, Option<usize>) {
        let min = self.idx_to_norm.idx_to_norm.keys().next().copied();
        let max = self.idx_to_norm.idx_to_norm.keys().next_back().copied();
        (min, max)
    }

    /// Attempts to shrink the interval to fit the range of explicitly defined indices.
    /// Returns `None` if `idx_to_norm` is empty.
    pub fn try_fit_interval(&self) -> Option<Self> {
        self.idx_to_norm.try_fit_interval().map(|idx_to_norm| Self {
            bra_ket: self.bra_ket,
            idx_to_norm,
        })
    }

    /// Returns a new `SepQubits` where all missing entries in the interval are filled with padding.
    pub fn extract_padding(&self) -> Self {
        Self {
            bra_ket: self.bra_ket,
            idx_to_norm: self.idx_to_norm.extract_padding(),
        }
    }

    /// Returns a new `SepQubits` with the interval manually updated.
    pub fn set_interval(&self, interval: (usize, usize)) -> Self {
        Self {
            bra_ket: self.bra_ket,
            idx_to_norm: self.idx_to_norm.set_interval(interval),
        }
    }

    /// Returns a new `SepQubits` with a new padding value.
    pub fn set_padding(&self, padding: NormPair<T>) -> Self {
        Self {
            bra_ket: self.bra_ket,
            idx_to_norm: self.idx_to_norm.set_padding(padding),
        }
    }

    /// Returns a new `SepQubits` with specified indices removed from the explicit assignments.
    pub fn discard<I: IntoIterator<Item = usize>>(&self, indices: I) -> Self {
        Self {
            bra_ket: self.bra_ket,
            idx_to_norm: self.idx_to_norm.discard(indices),
        }
    }

    /// Returns a new `SepQubits` with additional explicit assignments added if not already present.
    pub fn extend(&self, idx_to_norm: BTreeMap<usize, NormPair<T>>) -> Self {
        Self {
            bra_ket: self.bra_ket,
            idx_to_norm: self.idx_to_norm.extend(idx_to_norm),
        }
    }

    /// Returns a new `SepQubits` with the provided explicit assignments forcibly updated.
    pub fn force_update(&self, idx_to_norm: BTreeMap<usize, NormPair<T>>) -> Self {
        Self {
            bra_ket: self.bra_ket,
            idx_to_norm: self.idx_to_norm.force_update(idx_to_norm),
        }
    }

    /// Returns a new `SepQubits` with entries at indices `i` and `j` swapped.
    ///
    /// If only one of the two exists in the current map, its value is moved to the other index.
    /// If neither exists, the map is unchanged.
    pub fn swap(&self, i: usize, j: usize) -> Self {
        Self {
            bra_ket: self.bra_ket,
            idx_to_norm: self.idx_to_norm.swap(i, j),
        }
    }
}

impl<T: FloatExt + Hash> IdxToMixQubits<T> {
    /// Returns the Hermitian conjugate of the separable multi-qubit mixed state.
    pub fn dag(&self) -> Self {
        Self {
            bra_ket: self.bra_ket.dag(),
            interval: self.interval,
            padding: self.padding.conj(),
            idx_to_mix: self
                .idx_to_mix
                .iter()
                .map(|(k, v)| (*k, v.conj()))
                .collect(),
        }
    }
    /// Returns a vector of all explicit qubit indices.
    pub fn indices_keys(&self) -> Vec<usize> {
        self.idx_to_mix.keys().copied().collect()
    }

    /// Returns the inclusive interval of explicitly assigned qubit indices, if any.
    ///
    /// This is equivalent to `(min_index, max_index)` over the `idx_to_mix` map.
    /// Returns `(None, None)` if the map is empty.
    pub fn try_interval(&self) -> (Option<usize>, Option<usize>) {
        let min = self.idx_to_mix.keys().next().copied();
        let max = self.idx_to_mix.keys().next_back().copied();
        (min, max)
    }

    /// Attempts to shrink the interval to fit the range of explicitly defined indices.
    /// Returns `None` if `idx_to_mix` is empty.
    pub fn try_fit_interval(&self) -> Option<Self> {
        let (min, max) = self.try_interval();
        Some(Self {
            bra_ket: self.bra_ket,
            interval: (min?, max?),
            padding: self.padding,
            idx_to_mix: self.idx_to_mix.clone(),
        })
    }

    /// Returns a new `IdxToMixQubits` where all missing entries in the interval are filled with padding.
    pub fn extract_padding(&self) -> Self {
        let mut idx_to_mix = self.idx_to_mix.clone();
        let (start, end) = self.interval;

        for i in start..=end {
            idx_to_mix.entry(i).or_insert(self.padding);
        }

        Self {
            bra_ket: self.bra_ket,
            interval: self.interval,
            padding: self.padding,
            idx_to_mix,
        }
    }

    /// Returns a new `IdxToMixQubits` with the interval manually updated.
    pub fn set_interval(&self, interval: (usize, usize)) -> Self {
        Self {
            bra_ket: self.bra_ket,
            interval,
            padding: self.padding,
            idx_to_mix: self.idx_to_mix.clone(),
        }
    }

    /// Returns a new `IdxToMixQubits` with a new padding value.
    pub fn set_padding(&self, padding: MixPair<T>) -> Self {
        Self {
            bra_ket: self.bra_ket,
            interval: self.interval,
            padding,
            idx_to_mix: self.idx_to_mix.clone(),
        }
    }

    /// Returns a new `IdxToMixQubits` with specified indices removed from the explicit assignments.
    pub fn discard<I: IntoIterator<Item = usize>>(&self, indices: I) -> Self {
        let mut new_map = self.idx_to_mix.clone();
        for idx in indices {
            new_map.remove(&idx);
        }

        Self {
            bra_ket: self.bra_ket,
            interval: self.interval,
            padding: self.padding,
            idx_to_mix: new_map,
        }
    }

    /// Returns a new `IdxToMixQubits` with additional explicit assignments added if not already present.
    pub fn extend(&self, idx_to_mix: BTreeMap<usize, MixPair<T>>) -> Self {
        let mut new_map = self.idx_to_mix.clone();
        for (k, v) in idx_to_mix {
            new_map.entry(k).or_insert(v);
        }

        let interval = key_interval_or(&new_map, self.interval);

        Self {
            bra_ket: self.bra_ket,
            interval,
            padding: self.padding,
            idx_to_mix: new_map,
        }
    }

    /// Returns a new `IdxToMixQubits` with the provided explicit assignments forcibly updated.
    pub fn force_update(&self, idx_to_mix: BTreeMap<usize, MixPair<T>>) -> Self {
        let mut new_map = self.idx_to_mix.clone();

        for (k, v) in idx_to_mix {
            new_map.insert(k, v); // `insert` already replaces the value if key exists
        }

        let interval = key_interval_or(&new_map, self.interval);

        Self {
            bra_ket: self.bra_ket,
            interval,
            padding: self.padding,
            idx_to_mix: new_map,
        }
    }

    /// Returns a new `IdxToMixQubits` with entries at indices `i` and `j` swapped.
    ///
    /// If only one of the two exists in the current map, its value is moved to the other index.
    /// If neither exists, the map is unchanged.
    pub fn swap(&self, i: usize, j: usize) -> Self {
        if i == j {
            return self.clone(); // No-op
        }

        let mut new_map = self.idx_to_mix.clone();
        swap_btree_entries(&mut new_map, i, j);

        let interval = key_interval_or(&new_map, self.interval);

        Self {
            bra_ket: self.bra_ket,
            interval,
            padding: self.padding,
            idx_to_mix: new_map,
        }
    }
}

impl<T: FloatExt + Hash> Rk1PN<T> {
    /// Factors out the global amplitude (probability weight) from the rank-1 projector or nilpotent operator.
    ///
    /// The underlying `norm_qubit` is decomposed into a canonical form and the scalar amplitude.
    /// This method returns a pair `(canonical, prob)` where:
    /// - `canonical` is a `Rk1PN` with a normalized pure qubit state (unit norm and canonical phase),
    /// - `prob` is the squared magnitude (`sqrt_prob²`) representing the overall probability weight.
    ///
    /// Returns:
    /// - `None` if the underlying state is zero (no meaningful decomposition).
    /// - `Some(( canonical, prob))` otherwise.
    pub fn try_factor_out(self) -> Option<(Self, T)> {
        self.norm_pair
            .try_factor_out()
            .map(|(norm, sqrt_prob, ..)| {
                (
                    Self {
                        bra_ket: self.bra_ket,
                        norm_pair: norm,
                        pro_nil: self.pro_nil,
                    },
                    sqrt_prob.powi(2),
                )
            })
    }
    /// Returns the Hermitian conjugate (dagger) of the nilpotent matrix representation.
    ///
    /// If the operator is a nilpotent matrix, it returns a new one with an orthogonal qubit.
    /// If it is a projector, it returns itself unchanged.
    pub fn dag(self) -> Self {
        match self.pro_nil {
            Nilpotent => {
                let norm_pair = self.norm_pair.orthogonal();
                Self {
                    bra_ket: self.bra_ket,
                    norm_pair,
                    pro_nil: Nilpotent,
                }
            }
            Projector => self,
        }
    }
    /// Returns the trace of a rank-1 projector or nilpotent matrix as a `Cpx<T>`.
    pub fn trace(self) -> Cpx<T> {
        let NormPair { c0, c1 } = self.norm_pair;

        let dual = match self.pro_nil {
            Projector => self.norm_pair.conj(),
            Nilpotent => self.norm_pair.orthogonal().conj(),
        };

        Cpx::Real { re: c0 * dual.c0 } + c1 * dual.c1
    }
}

impl<T: FloatExt + Hash> Rk1KB<T> {
    /// Factors out the combined amplitude and global phase from a general rank-1 operator.
    ///
    /// Validates that `ket` is a |ψ⟩ and `bra` is a ⟨ϕ|, then factors out their
    /// respective global phases and magnitudes, returning:
    ///
    /// - `None` if either component is the zero state or has incorrect role labels.
    /// - `Some(( canonical, prob, phase))` where:
    ///   - `canonical` is a normalized version of the operator with unit-norm states,
    ///   - `prob` is the product of the sqrt-probabilities,
    ///   - `phase` is the product of their global phases.
    pub fn try_factor_out(self) -> Option<(Self, T, Cpx<T>)> {
        let ket_factored = self.ket.try_factor_out();
        let bra_factored = self.bra.try_factor_out();

        match (ket_factored, bra_factored) {
            (Some((ket_norm, sqrt_ket, ph_ket)), Some((bra_norm, sqrt_bra, ph_bra))) => {
                let sqrt_prob = sqrt_ket * sqrt_bra;
                let phase = ph_ket * ph_bra;

                Some((
                    Self {
                        ket: ket_norm,
                        bra: bra_norm,
                    },
                    sqrt_prob,
                    phase,
                ))
            }
            _ => None,
        }
    }
    /// Returns the Hermitian conjugate (dagger) of the `Rk1KB` matrix representation.
    ///
    /// This swaps the bra and ket components and applies conjugation to each.
    pub fn dag(self) -> Self {
        Self {
            ket: self.bra.conj(),
            bra: self.ket.conj(),
        }
    }
    /// Returns the trace of a general rank-1 matrix that is neither a projector nor nilpotent, as a `Cpx<T>`.
    pub fn trace(self) -> Cpx<T> {
        let NormPair { c0: k0, c1: k1 } = self.ket;
        let NormPair { c0: b0, c1: b1 } = self.bra;

        Cpx::Real { re: k0 * b0 } + k1 * b1
    }
}

impl<T: FloatExt + Hash> Rank1<T> {
    /// Factors out amplitude and optional global phase from a rank-1 operator.
    ///
    /// - Returns `None` if underlying state(s) are zero.
    /// - `Some(( canonical, prob, phase_opt))` where:
    ///   - `canonical` is normalized (projector or outer product),
    ///   - `prob` is the total scalar amplitude (squared norm),
    ///   - `phase_opt` is `Some(phase)` for `Other`, `None` for `ProNil`.
    pub fn try_factor_out(self) -> Option<(Self, T, Option<Cpx<T>>)> {
        match self {
            Self::ProNil(pn) => pn
                .try_factor_out()
                .map(|(canon, prob)| (Self::ProNil(canon), prob, None)),
            Self::Other(other) => other
                .try_factor_out()
                .map(|(canon, prob, phase)| (Self::Other(canon), prob, Some(phase))),
        }
    }
    /// Returns the Hermitian conjugate (dagger) of the rank-1 matrix.
    ///
    /// For `ProNil`, the projector is preserved and the nilpotent vector is conjugated if applicable.
    /// For `Other`, the ket and bra are swapped and conjugated.
    pub fn dag(self) -> Self {
        match self {
            Rank1::ProNil(pn) => Rank1::ProNil(pn.dag()),
            Rank1::Other(other) => Rank1::Other(other.dag()),
        }
    }
    /// Returns the trace of a general rank-1 matrix as a `Cpx<T>`.
    pub fn trace(self) -> Cpx<T> {
        match self {
            Rank1::ProNil(pn) => pn.trace(),
            Rank1::Other(other) => other.trace(),
        }
    }
}

impl<T: FloatExt + Hash> RawMat<T> {
    /// Constructs a raw 2×2 complex matrix from a rank-1 representation.
    ///
    /// Computes the outer product `|ket⟩⟨bra|`, with `ket` and `bra` derived from
    /// either a projector/nilpotent pairing or explicit components.
    pub fn from_rank1(rk1: Rank1<T>) -> Self {
        let build_outer = |k0: Cpx<T>, k1: Cpx<T>, b0: Cpx<T>, b1: Cpx<T>| RawMat {
            mat: [[k0 * b0, k0 * b1], [k1 * b0, k1 * b1]],
        };

        let (ket, bra) = match rk1 {
            Rank1::ProNil(pn) => {
                let dual = pn.norm_pair.conj();
                let dual_out = match pn.pro_nil {
                    Projector => dual,
                    Nilpotent => dual.orthogonal(),
                };
                match pn.bra_ket {
                    Ket => (pn.norm_pair, dual_out),
                    Bra => (dual_out, pn.norm_pair),
                }
            }
            Rank1::Other(other) => (other.ket, other.bra),
        };

        build_outer(
            Cpx::Real { re: ket.c0 },
            ket.c1,
            Cpx::Real { re: bra.c0 },
            bra.c1,
        )
    }

    /// Constructs a raw 2×2 matrix representing the density matrix of a mixed qubit state.
    ///
    /// The result is a convex combination of projectors onto a qubit and its orthogonal complement:
    /// ρ = `prob` × |ψ⟩⟨ψ| + (1 − `prob`) × |ψ⊥⟩⟨ψ⊥|.
    pub fn from_mixed_qubit(mixed: MixQubit<T>) -> Self {
        let prob = mixed.mix_pair.prob;
        let comp_prob = T::one() - prob;
        let norm = mixed.mix_pair.norm_pair;
        let bra_ket = mixed.bra_ket;

        let mk_proj = |norm_pair| {
            Self::from_rank1(Rank1::ProNil(Rk1PN {
                bra_ket,
                norm_pair,
                pro_nil: Projector,
            }))
        };

        let rho1 = mk_proj(norm) * prob;
        let rho2 = mk_proj(norm.orthogonal()) * comp_prob;

        rho1 + rho2
    }

    /// Converts a (complex) quaternion in the Pauli basis back into a raw 2×2 complex matrix.
    ///
    /// The complex quaternion is of the form:  
    /// `M = c₀·I + c₁·X + c₂·Y + c₃·Z`, where `coefficients = [c₀, c₁, c₂, c₃]`.
    pub fn from_cpx_quaternion(cq: CpxQuaternion<T>) -> Self {
        let [c0, c1, c2, c3] = cq.coefficients;
        let mat = [[c0 + c3, c1 - c2 * Cpx::J], [c1 + c2 * Cpx::J, c0 - c3]];
        Self { mat }
    }

    /// Converts a (real) quaternion in the Pauli basis back into a raw 2×2 complex matrix.
    ///
    /// The real quaternion is of the form:  
    /// `M = r₀·I + r₁·X + r₂·Y + r₃·Z`, where `coefficients = [r₀, r₁, r₂, r₃]`.
    pub fn from_real_quaternion(rq: RealQuaternion<T>) -> Self {
        Self::from_cpx_quaternion(CpxQuaternion::from_real_quaternion(rq))
    }

    /// Converts a unit-determinant complex quaternion into a raw 2×2 matrix.
    ///
    /// Computes the implicit identity coefficient `a₀` to satisfy `det(M) = 1`,
    /// then reconstructs the matrix from the full Pauli decomposition.
    pub fn from_det1_cpx_quaternion(d1cq: Det1CpxQuaternion<T>) -> Self {
        Self::from_cpx_quaternion(CpxQuaternion::from_det1_cpx_quaternion(d1cq))
    }

    /// Converts a unit-determinant real quaternion into a raw 2×2 matrix.
    ///
    /// Internally lifts the real coefficients into the complex domain and reconstructs
    /// the full matrix while enforcing the unit-determinant constraint.
    pub fn from_det1_real_quaternion(d1rq: Det1RealQuaternion<T>) -> Self {
        Self::from_real_quaternion(RealQuaternion::from_det1_real_quaternion(d1rq))
    }

    /// Returns the Hermitian conjugate (dagger) of the Raw matrix representation.
    pub fn dag(self) -> Self {
        let m = self.mat;
        Self {
            mat: [
                [m[0][0].conj(), m[1][0].conj()],
                [m[0][1].conj(), m[1][1].conj()],
            ],
        }
    }
    /// Returns the trace of a raw matrix as a `Cpx<T>`.
    pub fn trace(self) -> Cpx<T> {
        let m = self.mat;
        m[0][0] + m[1][1]
    }
    /// Returns the determinant of the matrix.
    pub fn det(self) -> Cpx<T> {
        let m = self.mat;
        (m[0][0] * m[1][1]) - (m[0][1] * m[1][0])
    }
    /// Returns `true` if all entries of the matrix are zero.
    pub fn is_zero(&self) -> bool {
        self.mat.iter().flatten().all(|c| matches!(c, Cpx::Zero {}))
    }
    /// Returns the rank of the matrix (0, 1, or 2).
    pub fn rank(&self) -> u8 {
        if self.is_zero() {
            0
        } else if matches!(self.det(), Cpx::Zero {}) {
            1
        } else {
            2
        }
    }
}

impl<T: FloatExt + Hash> CpxQuaternion<T> {
    /// Constructs a Pauli-basis (cpx-quaternion) representation from a raw 2×2 complex matrix.
    ///
    /// Decomposes the matrix `M` into the Pauli basis:  
    /// `M = a₀·I + a₁·X + a₂·Y + a₃·Z`,  
    /// where `a₀ = (M₀₀ + M₁₁)/2`, `a₁ = (M₀₁ + M₁₀)/2`, etc.
    pub fn from_raw_mat(raw: RawMat<T>) -> Self {
        let half = T::from(0.5).unwrap();
        let [[a, b], [c, d]] = raw.mat;

        let coefficients = [
            (a + d) * half,          // I component
            (b + c) * half,          // X component
            (b - c) * half * Cpx::J, // Y component
            (a - d) * half,          // Z component
        ];

        Self { coefficients }
    }

    /// Constructs a Pauli-basis (cpx-quaternion) representation from a Hermitian 2×2 matrix.
    pub fn from_real_quaternion(rq: RealQuaternion<T>) -> Self {
        let mut coefficients = [Cpx::ZERO; 4];
        for (i, &real) in rq.coefficients.iter().enumerate() {
            coefficients[i] = Cpx::Real { re: real };
        }
        Self { coefficients }
    }

    /// Constructs a `CpxQuaternion` from a `Det1CpxQuaternion`, computing the identity coefficient
    /// to satisfy `det(M) = 1` under the Pauli basis expansion.
    pub fn from_det1_cpx_quaternion(d1cq: Det1CpxQuaternion<T>) -> Self {
        let x2 = d1cq.x.powi(2);
        let y2 = d1cq.y.powi(2);
        let z2 = d1cq.z.powi(2);

        let a0 = (x2 + y2 + z2 + Cpx::ONE).powf(T::from(0.5).unwrap()); // a₀ = sqrt(1 + x² + y² + z²)

        let coefficients = [a0, d1cq.x, d1cq.y, d1cq.z];
        Self { coefficients }
    }

    /// Counts how many coefficients are not the `Cpx::Zero` variant.
    ///
    /// Assumes zeros are exactly represented by `Cpx::Zero {}`.
    pub fn nonzero_count(&self) -> usize {
        self.coefficients
            .iter()
            .filter(|c| !matches!(c, Cpx::Zero {}))
            .count()
    }

    /// Checks whether this matrix is an identity matrix or a Pauli matrix.
    pub fn is_ixyz(&self) -> bool {
        self.nonzero_count() == 1
    }

    /// Returns the Hermitian conjugate (dagger) of the cpx-quaternion representation.
    pub fn dag(&self) -> Self {
        let mut coefficients = self.coefficients;
        for elem in &mut coefficients {
            *elem = elem.conj();
        }
        Self { coefficients }
    }

    /// Returns the trace of the cpx-quaternion representation as a `Cpx<T>`.
    pub fn trace(&self) -> Cpx<T> {
        self.coefficients[0] * T::from(2.0).unwrap()
    }

    /// Computes the determinant of the matrix represented by this quaternion:
    /// det(M) = (a₀)² − (a₁)² − (a₂)² − (a₃)², where M = a₀·I + a₁·X + a₂·Y + a₃·Z.
    pub fn det(&self) -> Cpx<T> {
        let [a0, a1, a2, a3] = self.coefficients;
        a0.powi(2) - a1.powi(2) - a2.powi(2) - a3.powi(2)
    }
}

impl<T: FloatExt + Hash> RealQuaternion<T> {
    /// Constructs a `RealQuaternion` from a `Det1RealQuaternion`, computing the identity coefficient
    /// to satisfy `det(M) = 1` under the Pauli basis expansion.
    pub fn from_det1_real_quaternion(d1rq: Det1RealQuaternion<T>) -> Self {
        let x2 = d1rq.x.powi(2);
        let y2 = d1rq.y.powi(2);
        let z2 = d1rq.z.powi(2);

        let a0 = (x2 + y2 + z2 + T::one()).sqrt(); // a₀ = sqrt(1 + x² + y² + z²)

        let coefficients = [a0, d1rq.x, d1rq.y, d1rq.z];
        Self { coefficients }
    }

    /// Counts how many coefficients are not the `T::zero()` variant.
    ///
    /// Assumes zeros are exactly represented by `T::zero()`.
    pub fn nonzero_count(&self) -> usize {
        self.coefficients
            .iter()
            .filter(|c| **c != T::zero())
            .count()
    }
    /// Checks whether this matrix is an identity matrix or a Pauli matrix.
    pub fn is_ixyz(&self) -> bool {
        self.nonzero_count() == 1
    }
    /// Returns the trace of the real-quaternion representation as a `T`.
    pub fn trace(&self) -> T {
        self.coefficients[0] * T::from(2.0).unwrap()
    }

    /// Computes the determinant of the matrix represented by this real quaternion:
    /// det(M) = (a₀)² − (a₁)² − (a₂)² − (a₃)², where M = a₀·I + a₁·X + a₂·Y + a₃·Z.
    pub fn det(&self) -> T {
        let [a0, a1, a2, a3] = self.coefficients;
        a0.powi(2) - a1.powi(2) - a2.powi(2) - a3.powi(2)
    }
}

impl<T: FloatExt + Hash> Det1CpxQuaternion<T> {
    /// Constructs a `Det1CpxQuaternion` from a `Det1RealQuaternion`, lifting real components
    /// into the complex domain while preserving the determinant-1 constraint.
    pub fn from_real(d1rq: Det1RealQuaternion<T>) -> Self {
        Self {
            x: Cpx::Real { re: d1rq.x },
            y: Cpx::Real { re: d1rq.y },
            z: Cpx::Real { re: d1rq.z },
        }
    }

    /// Returns the implicit identity component `a₀` such that:
    /// `a₀ = sqrt(1 + x² + y² + z²)`, ensuring `det(M) = 1`.
    pub fn a0(&self) -> Cpx<T> {
        (Cpx::ONE + self.x.powi(2) + self.y.powi(2) + self.z.powi(2)).powf(T::from(0.5).unwrap())
    }

    /// Returns the Hermitian conjugate (dagger) of the cpx-quaternion representation with unit determinant.
    pub fn dag(&self) -> Self {
        let x = self.x.conj();
        let y = self.y.conj();
        let z = self.z.conj();
        Self { x, y, z }
    }

    /// Returns the trace of the corresponding 2×2 matrix, which is `2·a₀`.
    pub fn trace(&self) -> Cpx<T> {
        self.a0() * T::from(2.0).unwrap()
    }

    /// Checks whether this matrix is an identity matrix or a Pauli matrix.
    pub fn is_ixyz(self) -> bool {
        CpxQuaternion::from_det1_cpx_quaternion(self).nonzero_count() == 1
    }
}

impl<T: FloatExt + Hash> Det1RealQuaternion<T> {
    /// Returns the implicit identity component `a₀` such that:
    /// `a₀ = sqrt(1 + x² + y² + z²)`, ensuring `det(M) = 1`.
    pub fn a0(&self) -> T {
        (T::one() + self.x.powi(2) + self.y.powi(2) + self.z.powi(2)).powf(T::from(0.5).unwrap())
    }

    /// Returns the trace of the corresponding 2×2 matrix, which is `2·a₀`.
    pub fn trace(&self) -> T {
        self.a0() * T::from(2.0).unwrap()
    }

    /// Checks whether this matrix is an identity matrix or a Pauli matrix.
    pub fn is_ixyz(self) -> bool {
        RealQuaternion::from_det1_real_quaternion(self).nonzero_count() == 1
    }
}

impl<T: FloatExt + Hash> Det1<T> {
    /// Returns the trace of the matrix: `tr(M) = 2·a₀`, where `a₀` is the identity component.
    pub fn trace(&self) -> Cpx<T> {
        match self {
            Self::Hermitian(d1rq) => Cpx::Real { re: d1rq.trace() },
            Self::Other(d1cq) => d1cq.trace(),
        }
    }

    /// Returns the Hermitian adjoint (dagger) of the matrix.
    ///
    /// For Hermitian matrices, this is the identity operation.
    /// For non-Hermitian matrices, conjugates all Pauli coefficients.
    pub fn dag(&self) -> Self {
        match self {
            Self::Hermitian(d1rq) => Self::Hermitian(*d1rq),
            Self::Other(d1cq) => Self::Other(d1cq.dag()),
        }
    }

    /// Returns `true` if the matrix is exactly one of `{I, X, Y, Z}`.
    pub fn is_ixyz(self) -> bool {
        match self {
            Self::Hermitian(d1rq) => d1rq.is_ixyz(),
            Self::Other(d1cq) => d1cq.is_ixyz(),
        }
    }
}

impl<T: FloatExt + Hash> RankOneTwo<T> {
    /// Returns the Hermitian conjugate (dagger) of the matrix.
    pub fn dag(self) -> Self {
        match self {
            Self::Rank1(rk1) => Self::Rank1(rk1.dag()),
            Self::Rank2(det1) => Self::Rank2(det1.dag()),
        }
    }

    /// Returns the trace of the matrix.
    pub fn trace(self) -> Cpx<T> {
        match self {
            Self::Rank1(rk1) => rk1.trace(),
            Self::Rank2(det1) => det1.trace(),
        }
    }

    /// Returns the determinant of the matrix.
    pub fn det(self) -> Cpx<T> {
        match self {
            Self::Rank1(..) => Cpx::ZERO,
            Self::Rank2(..) => Cpx::ONE,
        }
    }

    /// Returns the rank of this finite rank 2x2 matrix.
    pub fn rank(self) -> usize {
        match self {
            Self::Rank1(..) => 1,
            Self::Rank2(..) => 2,
        }
    }
}

impl<T: FloatExt + Hash> LocalOps<T> {
    /// Returns the list of qubit indices that have explicitly assigned `RankOneTwo`.
    pub fn indices_keys(&self) -> Vec<usize> {
        self.idx_to_mat.keys().copied().collect()
    }

    /// Returns the inclusive interval of explicitly assigned qubit indices, if any.
    ///
    /// This is equivalent to `(min_index, max_index)` over the `idx_to_mix` map.
    /// Returns `(None, None)` if the map is empty.
    pub fn try_interval(&self) -> (Option<usize>, Option<usize>) {
        let min = self.idx_to_mat.keys().next().copied();
        let max = self.idx_to_mat.keys().next_back().copied();
        (min, max)
    }

    /// Returns a new `LocalOps` with its interval adjusted to match the minimum and maximum keys
    /// present in the `idx_to_mat` map.
    ///
    /// This method is useful for automatically syncing the `interval` field with the actual
    /// span of explicitly defined qubit indices.
    ///
    /// # Returns
    /// - `Some(Self)` with the updated interval if `idx_to_mat` is non-empty.
    /// - `None` if `idx_to_mat` is empty, as there are no keys to determine an interval.
    pub fn try_fit_interval(&self) -> Option<Self> {
        let (min, max) = self.try_interval();
        Some(Self {
            interval: (min?, max?),
            padding: self.padding,
            idx_to_mat: self.idx_to_mat.clone(),
        })
    }

    /// Returns a new `LocalOps` where all indices in the interval are explicitly filled.
    ///
    /// For any index within `self.interval` that does not appear in `idx_to_mat`, this method
    /// inserts an entry with the `padding` value. The result is a fully populated BTreeMap with
    /// no missing indices in the defined interval.
    pub fn extract_padding(&self) -> Self {
        let mut idx_to_mat = self.idx_to_mat.clone();
        let (start, end) = self.interval;

        for i in start..=end {
            idx_to_mat.entry(i).or_insert(self.padding);
        }

        Self {
            interval: self.interval,
            padding: self.padding,
            idx_to_mat,
        }
    }

    /// Returns a new `LocalOps` with the specified interval, leaving all other fields unchanged.
    ///
    /// This method updates the `interval` field while cloning the existing `padding`
    /// and `idx_to_mat` map.
    pub fn set_interval(&self, interval: (usize, usize)) -> Self {
        Self {
            interval,
            padding: self.padding,
            idx_to_mat: self.idx_to_mat.clone(),
        }
    }

    /// Returns a new `LocalOps` with the specified padding value,
    /// leaving the interval and index-to-mat map unchanged.
    pub fn set_padding(&self, padding: RankOneTwo<T>) -> Self {
        Self {
            interval: self.interval,
            padding,
            idx_to_mat: self.idx_to_mat.clone(),
        }
    }
    /// Returns a new `LocalOps` with the specified indices removed from `idx_to_mat`.
    ///
    /// This method creates a copy of the current structure and removes any entries whose keys
    /// match the provided indices. The original `interval` and `padding` are preserved.
    ///
    /// # Type Parameters
    /// - `I`: An iterable collection of `usize` indices to remove.
    ///
    /// # Arguments
    /// - `indices`: Any iterable over `usize` values (e.g., a `Vec`, array, range, or `HashSet`).
    ///
    /// # Returns
    /// A new `LocalOps` with the specified indices removed from the map.
    ///
    /// # Examples
    /// ```rust
    /// //let pruned = original.discard(vec![1, 2, 4]);
    /// //let pruned = original.discard([3, 5, 7]);
    /// //let pruned = original.discard(0..=10);
    /// ```
    pub fn discard<I: IntoIterator<Item = usize>>(&self, indices: I) -> Self {
        let mut new_map = self.idx_to_mat.clone();
        for idx in indices {
            new_map.remove(&idx);
        }

        Self {
            interval: self.interval,
            padding: self.padding,
            idx_to_mat: new_map,
        }
    }
    /// Returns a new `LocalOps` with additional entries merged into `idx_to_mat`.
    ///
    /// For each `(index, RankOneTwo<T>)` in the provided `idx_to_mat` map, if the index is not already present
    /// in the current map, it is inserted. Existing entries are left unchanged.
    ///
    /// The resulting interval is updated to span the full range of indices in the combined map, unless the map
    /// becomes empty, in which case the original interval is retained.
    ///
    /// # Arguments
    ///
    /// * `idx_to_mat` - A `BTreeMap<usize, RankOneTwo<T>>` containing new entries to insert.
    ///
    /// # Returns
    ///
    /// A new `LocalOps` with the merged map and updated interval.
    pub fn extend(&self, idx_to_mat: BTreeMap<usize, RankOneTwo<T>>) -> Self {
        let mut new_map = self.idx_to_mat.clone();
        for (k, v) in idx_to_mat.iter() {
            new_map.entry(*k).or_insert(*v);
        }

        let interval = key_interval_or(&new_map, self.interval);

        Self {
            interval,
            padding: self.padding,
            idx_to_mat: new_map,
        }
    }

    /// Returns a new `LocalOps` with the given entries forcibly updated.
    ///
    /// For each `(index, RankOneTwo<T>)` in the provided map:
    /// - Any existing entry at that index is removed.
    /// - The new entry is inserted unconditionally.
    ///
    /// The updated `idx_to_mat` map reflects all changes, and the interval is recomputed to
    /// span the full range of keys present in the new map (if non-empty).
    ///
    /// # Arguments
    ///
    /// * `idx_to_mat` - A `BTreeMap<usize, RankOneTwo<T>>` containing entries to overwrite.
    ///
    /// # Returns
    ///
    /// A new `LocalOps` with the forcibly updated entries and recalculated interval.
    pub fn force_update(&self, idx_to_mat: BTreeMap<usize, RankOneTwo<T>>) -> Self {
        let mut new_map = self.idx_to_mat.clone();

        for (k, v) in idx_to_mat {
            new_map.insert(k, v); // `insert` already replaces the value if key exists
        }

        let interval = key_interval_or(&new_map, self.interval);

        Self {
            interval,
            padding: self.padding,
            idx_to_mat: new_map,
        }
    }
    /// Returns a new `LocalOps` with the values at two indices swapped.
    ///
    /// If both indices exist in `idx_to_mat`, their values are swapped.
    /// If only one exists, its value is moved to the other index.
    /// If neither exist, the mapping is unchanged.
    ///
    /// # Arguments
    /// * `i` - The first index.
    /// * `j` - The second index.
    pub fn swap(&self, i: usize, j: usize) -> Self {
        if i == j {
            return self.clone(); // No-op
        }

        let mut new_map = self.idx_to_mat.clone();
        swap_btree_entries(&mut new_map, i, j);

        let interval = key_interval_or(&new_map, self.interval);

        Self {
            interval,
            padding: self.padding,
            idx_to_mat: new_map,
        }
    }
}

impl<T: FloatExt + Hash> Add for RawMat<T> {
    type Output = Self;

    fn add(self, other: Self) -> Self {
        let mut mat = self.mat;
        for (i, row) in mat.iter_mut().enumerate() {
            for (j, elem) in row.iter_mut().enumerate() {
                *elem += other.mat[i][j];
            }
        }
        Self { mat }
    }
}

impl<T: FloatExt + Hash> AddAssign for RawMat<T> {
    fn add_assign(&mut self, other: Self) {
        for (i, row) in self.mat.iter_mut().enumerate() {
            for (j, elem) in row.iter_mut().enumerate() {
                *elem += other.mat[i][j];
            }
        }
    }
}

impl<T: FloatExt + Hash> Neg for RawMat<T> {
    type Output = Self;
    fn neg(self) -> Self {
        let mut mat = self.mat;
        for row in mat.iter_mut() {
            for elem in row.iter_mut() {
                *elem = -*elem;
            }
        }
        Self { mat }
    }
}

impl<T: FloatExt + Hash> Sub for RawMat<T> {
    type Output = Self;
    fn sub(self, other: Self) -> Self {
        self + (-other)
    }
}

impl<T: FloatExt + Hash> SubAssign for RawMat<T> {
    fn sub_assign(&mut self, other: Self) {
        *self += -other;
    }
}

impl<T: FloatExt + Hash> Mul for RawMat<T> {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self::Output {
        let [[a, b], [c, d]] = self.mat;
        let [[e, f], [g, h]] = rhs.mat;

        Self {
            mat: [
                [a * e + b * g, a * f + b * h],
                [c * e + d * g, c * f + d * h],
            ],
        }
    }
}

impl<T: FloatExt + Hash> MulAssign for RawMat<T> {
    fn mul_assign(&mut self, rhs: Self) {
        *self = *self * rhs;
    }
}
impl<T: FloatExt + Hash> Mul<T> for RawMat<T> {
    type Output = Self;
    fn mul(self, rhs: T) -> Self::Output {
        let mut mat = self.mat;
        for row in mat.iter_mut() {
            for elem in row.iter_mut() {
                *elem *= rhs;
            }
        }
        Self { mat }
    }
}

impl<T: FloatExt + Hash> MulAssign<T> for RawMat<T> {
    fn mul_assign(&mut self, rhs: T) {
        *self = *self * rhs;
    }
}

impl<T: FloatExt + Hash> Mul<Cpx<T>> for RawMat<T> {
    type Output = Self;
    fn mul(self, rhs: Cpx<T>) -> Self::Output {
        let mut mat = self.mat;
        for row in mat.iter_mut() {
            for elem in row.iter_mut() {
                *elem *= rhs;
            }
        }
        Self { mat }
    }
}

impl<T: FloatExt + Hash> MulAssign<Cpx<T>> for RawMat<T> {
    fn mul_assign(&mut self, rhs: Cpx<T>) {
        *self = *self * rhs;
    }
}

impl<T: FloatExt + Hash> Add for CpxQuaternion<T> {
    type Output = Self;
    fn add(self, other: Self) -> Self {
        let mut coefficients = self.coefficients;
        for (i, elem) in coefficients.iter_mut().enumerate() {
            *elem += other.coefficients[i];
        }
        Self { coefficients }
    }
}

impl<T: FloatExt + Hash> AddAssign for CpxQuaternion<T> {
    fn add_assign(&mut self, other: Self) {
        let mut coefficients = self.coefficients;
        for (i, elem) in coefficients.iter_mut().enumerate() {
            *elem += other.coefficients[i];
        }
    }
}

impl<T: FloatExt + Hash> Neg for CpxQuaternion<T> {
    type Output = Self;
    fn neg(self) -> Self::Output {
        let mut coefficients = self.coefficients;
        for elem in coefficients.iter_mut() {
            *elem = -*elem;
        }
        Self { coefficients }
    }
}

impl<T: FloatExt + Hash> Sub for CpxQuaternion<T> {
    type Output = Self;
    fn sub(self, other: Self) -> Self {
        self + (-other)
    }
}

impl<T: FloatExt + Hash> SubAssign for CpxQuaternion<T> {
    fn sub_assign(&mut self, other: Self) {
        *self += -other;
    }
}

impl<T: FloatExt + Hash> Mul for CpxQuaternion<T> {
    type Output = Self;
    fn mul(self, rhs: Self) -> Self {
        let mat_lhs = RawMat::from_cpx_quaternion(self);
        let mat_rhs = RawMat::from_cpx_quaternion(rhs);
        let mat_new = mat_lhs * mat_rhs;
        Self::from_raw_mat(mat_new)
    }
}

impl<T: FloatExt + Hash> MulAssign for CpxQuaternion<T> {
    fn mul_assign(&mut self, rhs: Self) {
        *self = *self * rhs;
    }
}

impl<T: FloatExt + Hash> Mul<T> for CpxQuaternion<T> {
    type Output = Self;
    fn mul(self, rhs: T) -> Self::Output {
        let mut coefficients = self.coefficients;
        for elem in coefficients.iter_mut() {
            *elem *= rhs;
        }
        Self { coefficients }
    }
}

impl<T: FloatExt + Hash> MulAssign<T> for CpxQuaternion<T> {
    fn mul_assign(&mut self, rhs: T) {
        *self = *self * rhs;
    }
}

impl<T: FloatExt + Hash> Mul<Cpx<T>> for CpxQuaternion<T> {
    type Output = Self;
    fn mul(self, rhs: Cpx<T>) -> Self::Output {
        let mut coefficients = self.coefficients;
        for elem in coefficients.iter_mut() {
            *elem *= rhs;
        }
        Self { coefficients }
    }
}

impl<T: FloatExt + Hash> MulAssign<Cpx<T>> for CpxQuaternion<T> {
    fn mul_assign(&mut self, rhs: Cpx<T>) {
        *self = *self * rhs;
    }
}