struqture 2.5.1

HQS tool for representing operators, Hamiltonians and open systems.
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
// Copyright © 2021-2023 HQS Quantum Simulations GmbH. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing permissions and
// limitations under the License.

use super::FermionIndex;
use crate::mappings::JordanWignerFermionToSpin;
use crate::prelude::*;
use crate::spins::{PauliHamiltonian, PauliOperator, PauliProduct, SinglePauliOperator};
use crate::{
    CorrespondsTo, CreatorsAnnihilators, GetValue, ModeIndex, StruqtureError, SymmetricIndex,
};

use qoqo_calculator::*;
use serde::{
    de::{Error, SeqAccess, Visitor},
    ser::SerializeTuple,
    Deserialize, Deserializer, Serialize, Serializer,
};
use std::cmp::Ordering;
use std::{ops::Mul, str::FromStr};
use tinyvec::TinyVec;

/// A product of fermionic creation and annihilation operators
///
/// The FermionProduct is used as an index for non-hermitian, normal ordered fermionic operators.
/// A fermionic operator can be written as a sum over normal ordered products of creation and annihilation operators.
/// The FermionProduct is used as an index when setting or adding new summands to a fermionic operator and when querrying the
/// weight of a product of operators in the sum.
///
/// # Example
///
/// ```rust
/// use struqture::prelude::*;
/// use struqture::fermions::FermionProduct;
///
/// let b_product = FermionProduct::new([0, 1], [0, 1]).unwrap();
/// println!("{}", b_product);
///
/// ```
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Default)]
pub struct FermionProduct {
    /// The ordered list of creator indices.
    creators: TinyVec<[usize; 2]>,
    /// The ordered list of annihilator indices.
    annihilators: TinyVec<[usize; 2]>,
}

#[cfg(feature = "json_schema")]
impl schemars::JsonSchema for FermionProduct {
    fn schema_name() -> std::borrow::Cow<'static, str> {
        "FermionProduct".into()
    }

    fn json_schema(_generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
        schemars::json_schema!({
            "type": "string",
            "description": "Represents products of Fermionic creators and annhilators by a string creators (c) or annihilators (a) followed by the modes they are acting on. E.g. c0a1."
        })
    }
}

impl crate::SerializationSupport for FermionProduct {
    fn struqture_type() -> crate::StruqtureType {
        crate::StruqtureType::FermionProduct
    }
}

/// Implementing serde serialization writing directly to string.
///
impl Serialize for FermionProduct {
    /// Serialization function for FermionProduct according to string type.
    ///
    /// # Arguments
    ///
    /// * `self` - FermionProduct to be serialized.
    /// * `serializer` - Serializer used for serialization.
    ///
    /// # Returns
    ///
    /// `S::Ok` - Serialized instance of FermionProduct.
    /// `S::Error` - Error in the serialization process.
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let readable = serializer.is_human_readable();
        if readable {
            serializer.serialize_str(&self.to_string())
        } else {
            let mut tuple = serializer.serialize_tuple(2)?;
            tuple.serialize_element(&self.creators)?;
            tuple.serialize_element(&self.annihilators)?;
            tuple.end()
        }
    }
}

/// Deserializing directly from string.
///
impl<'de> Deserialize<'de> for FermionProduct {
    /// Deserialization function for FermionProduct.
    ///
    /// # Arguments
    ///
    /// * `self` - Serialized instance of FermionProduct to be deserialized.
    /// * `deserializer` - Deserializer used for deserialization.
    ///
    /// # Returns
    ///
    /// `DecoherenceProduct` - Deserialized instance of FermionProduct.
    /// `D::Error` - Error in the deserialization process.
    fn deserialize<D>(deserializer: D) -> Result<FermionProduct, D::Error>
    where
        D: Deserializer<'de>,
    {
        let human_readable = deserializer.is_human_readable();
        if human_readable {
            struct TemporaryVisitor;
            impl<'de> Visitor<'de> for TemporaryVisitor {
                type Value = FermionProduct;

                fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                    formatter.write_str("String")
                }

                fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
                where
                    E: serde::de::Error,
                {
                    FermionProduct::from_str(v).map_err(|err| E::custom(format!("{err:?}")))
                }

                fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
                where
                    E: serde::de::Error,
                {
                    FermionProduct::from_str(v).map_err(|err| E::custom(format!("{err:?}")))
                }
            }

            deserializer.deserialize_str(TemporaryVisitor)
        } else {
            struct FermionProductVisitor;
            impl<'de> serde::de::Visitor<'de> for FermionProductVisitor {
                type Value = FermionProduct;
                fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                    std::fmt::Formatter::write_str(
                        formatter,
                        "Tuple of two sequences of unsigned integers",
                    )
                }
                // when variants are marked by String values
                fn visit_seq<M>(self, mut access: M) -> Result<Self::Value, M::Error>
                where
                    M: SeqAccess<'de>,
                {
                    let creators: TinyVec<[usize; 2]> = match access.next_element()? {
                        Some(x) => x,
                        None => {
                            return Err(M::Error::custom("Missing creator sequence".to_string()));
                        }
                    };
                    let annihilators: TinyVec<[usize; 2]> = match access.next_element()? {
                        Some(x) => x,
                        None => {
                            return Err(M::Error::custom(
                                "Missing annihilator sequence".to_string(),
                            ));
                        }
                    };

                    FermionProduct::new(creators, annihilators).map_err(M::Error::custom)
                }
            }
            let pp_visitor = FermionProductVisitor;

            deserializer.deserialize_tuple(2, pp_visitor)
        }
    }
}

impl ModeIndex for FermionProduct {
    /// Creates a new FermionProduct.
    ///
    /// # Arguments
    ///
    /// * `creators` - The creator indices to have in the FermionProduct.
    /// * `annihilators` - The annihilators indices to have in the FermionProduct.
    ///
    /// # Returns
    ///
    /// * `Ok(FermionProduct)` - The new FermionProduct with the given creators and annihilators.
    /// * `Err(StruqtureError::IncorrectlyOrderedIndices)` - Indices given in creators/annihilators are either not normal ordered, or contain a double index specification.
    fn new(
        creators: impl IntoIterator<Item = usize>,
        annihilators: impl IntoIterator<Item = usize>,
    ) -> Result<Self, StruqtureError> {
        let creators: TinyVec<[usize; 2]> = creators.into_iter().collect();
        match creators.windows(2).all(|w| w[0] < w[1]) {
            true => {}
            false => return Err(StruqtureError::IncorrectlyOrderedIndices),
        }

        let annihilators: TinyVec<[usize; 2]> = annihilators.into_iter().collect();
        match annihilators.windows(2).all(|w| w[0] < w[1]) {
            true => {}
            false => return Err(StruqtureError::IncorrectlyOrderedIndices),
        }

        Ok(Self {
            creators,
            annihilators,
        })
    }

    // From trait
    fn creators(&self) -> std::slice::Iter<'_, usize> {
        self.creators.iter()
    }

    // From trait
    fn annihilators(&self) -> std::slice::Iter<'_, usize> {
        self.annihilators.iter()
    }

    /// Creates a pair (FermionProduct, CalculatorComplex).
    ///
    /// The first item is the valid FermionProduct created from the input creators and annihilators.
    /// The second term is the input CalculatorComplex transformed according to the valid order of creators and annihilators.
    ///
    /// # Arguments
    ///
    /// * `creators` - The creator indices to have in the FermionProduct.
    /// * `annihilators` - The annihilators indices to have in the FermionProduct.
    /// * `value` - The CalculatorComplex to transform.
    ///
    /// # Returns
    ///
    /// * `Ok((FermionProduct, CalculatorComplex))` - The valid FermionProduct and the corresponding transformed CalculatorComplex.
    /// * `Err(StruqtureError::IndicesContainDoubles)` - Indices given in either creators or annihilators contain a double index specification.
    fn create_valid_pair(
        creators: impl IntoIterator<Item = usize>,
        annihilators: impl IntoIterator<Item = usize>,
        value: qoqo_calculator::CalculatorComplex,
    ) -> Result<(Self, qoqo_calculator::CalculatorComplex), StruqtureError> {
        let creators: TinyVec<[usize; 2]> = creators.into_iter().collect();
        let (new_creators, contains_double, parity_c) = sort_and_signal(creators);
        if contains_double {
            return Err(StruqtureError::IndicesContainDoubles {});
        }

        let annihilators: TinyVec<[usize; 2]> = annihilators.into_iter().collect();
        let (new_annihilators, contains_double, parity_a) = sort_and_signal(annihilators);
        if contains_double {
            return Err(StruqtureError::IndicesContainDoubles {});
        }

        let value = if (parity_c + parity_a) % 2 != 0 {
            value * -1.0
        } else {
            value
        };
        Ok((
            Self {
                creators: new_creators,
                annihilators: new_annihilators,
            },
            value,
        ))
    }
}

impl FermionIndex for FermionProduct {}

impl FermionProduct {
    /// Export to struqture_1 format.
    #[cfg(feature = "struqture_1_export")]
    pub fn to_struqture_1(&self) -> Result<struqture_1::fermions::FermionProduct, StruqtureError> {
        let self_string = self.to_string();
        let struqture_1_product = struqture_1::fermions::FermionProduct::from_str(&self_string)
            .map_err(|err| StruqtureError::GenericError {
                msg: format!("{err}"),
            })?;
        Ok(struqture_1_product)
    }

    /// Export to struqture_1 format.
    #[cfg(feature = "struqture_1_import")]
    pub fn from_struqture_1(
        value: &struqture_1::fermions::FermionProduct,
    ) -> Result<Self, StruqtureError> {
        let value_string = value.to_string();
        let pauli_product = Self::from_str(&value_string)?;
        Ok(pauli_product)
    }
}

impl CorrespondsTo<FermionProduct> for FermionProduct {
    /// Gets the FermionProduct corresponding to self (here, itself).
    ///
    /// # Returns
    ///
    /// * `FermionProduct` - The FermionProduct corresponding to Self.
    fn corresponds_to(&self) -> FermionProduct {
        self.clone()
    }
}

impl CorrespondsTo<HermitianFermionProduct> for FermionProduct {
    /// Gets the HermitianFermionProduct corresponding Self.
    ///
    /// # Returns
    ///
    /// * `HermitianFermionProduct` - The HermitianFermionProduct corresponding to Self.
    fn corresponds_to(&self) -> HermitianFermionProduct {
        if self.creators().min() > self.annihilators().min() {
            HermitianFermionProduct {
                creators: self.annihilators.clone(),
                annihilators: self.creators.clone(),
            }
        } else {
            HermitianFermionProduct {
                creators: self.creators.clone(),
                annihilators: self.annihilators.clone(),
            }
        }
    }
}

impl SymmetricIndex for FermionProduct {
    // From trait
    fn hermitian_conjugate(&self) -> (Self, f64) {
        let mut creators = self.annihilators.clone();
        creators.reverse();
        let mut annihilators = self.creators.clone();
        annihilators.reverse();
        let (new, value) = FermionProduct::create_valid_pair(creators, annihilators, 1.0.into())
            .expect("Bug: somehow commuted through and got a complex value");

        (
            new,
            *value
                .re
                .float()
                .expect("Bug: somehow commuted through and got a complex value"),
        )
    }

    // From trait
    fn is_natural_hermitian(&self) -> bool {
        self.creators == self.annihilators
    }
}

/// Implements the multiplication function of FermionProduct by FermionProduct.
///
impl Mul<FermionProduct> for FermionProduct {
    type Output = Vec<(FermionProduct, f64)>;
    /// Implement `*` for FermionProduct and FermionProduct.
    ///
    /// # Arguments
    ///
    /// * `other` - The FermionProduct to multiply by.
    ///
    /// # Returns
    ///
    /// * `Vec<(FermionProduct, f64)>` - The two FermionProducts multiplied.
    ///
    /// # Panics
    ///
    /// * Unexpectedly failed construction of FermionProduct creation internal struqture bug.
    /// * Bug: somehow commuted through and got a complex value.
    /// * Internal bug in `create_valid_pair`.
    fn mul(self, rhs: FermionProduct) -> Self::Output {
        let mut output_vec: Vec<(FermionProduct, f64)> = Vec::new();

        let commuted_creators_annihilators =
            commute_creator_annihilator_fermionic(&self.annihilators, &rhs.creators);
        for ((new_creators, mut new_annihilators), prefac) in commuted_creators_annihilators {
            let mut tmp_creators = self.creators.clone();
            tmp_creators.extend(new_creators);
            new_annihilators.extend(rhs.annihilators().copied());
            match FermionProduct::create_valid_pair(tmp_creators, new_annihilators, prefac.into()) {
                Ok((tmp_fermion_product, sign)) => {
                    output_vec.push((
                        tmp_fermion_product,
                        *sign
                            .re
                            .float()
                            .expect("Bug: somehow commuted through and got a complex value"),
                    ));
                }
                Err(StruqtureError::IndicesContainDoubles) => continue,
                _ => panic!("Internal bug in `create_valid_pair`"),
            }
        }
        output_vec
    }
}

impl Mul<Vec<FermionProduct>> for FermionProduct {
    type Output = Vec<(FermionProduct, f64)>;
    /// Implement `*` for FermionProduct and a vector of FermionProducts.
    ///
    /// # Arguments
    ///
    /// * `other` - The vector of FermionProducts to multiply by.
    ///
    /// # Returns
    ///
    /// * `Self` - The result of the multiplication.
    ///
    /// # Panics
    ///
    /// * Unexpectedly failed construction of FermionProduct creation internal struqture bug.
    /// * Bug: somehow commuted through and got a complex value.
    /// * Internal bug in `create_valid_pair`.
    fn mul(self, rhs: Vec<FermionProduct>) -> Self::Output {
        let mut output_vec: Vec<(FermionProduct, f64)> = Vec::new();
        for rh_bp in rhs.iter() {
            output_vec.append(&mut (self.clone() * rh_bp.clone()))
        }
        output_vec
    }
}

impl Mul<FermionProduct> for Vec<FermionProduct> {
    type Output = Vec<(FermionProduct, f64)>;
    /// Implement `*` for a vector of FermionProducts and a FermionProduct.
    ///
    /// # Arguments
    ///
    /// * `other` - The FermionProduct to multiply by.
    ///
    /// # Returns
    ///
    /// * `Self` - The result of the multiplication.
    ///
    /// # Panics
    ///
    /// * Unexpectedly failed construction of FermionProduct creation internal struqture bug.
    /// * Bug: somehow commuted through and got a complex value.
    /// * Internal bug in `create_valid_pair`.
    fn mul(self, rhs: FermionProduct) -> Self::Output {
        let mut output_vec: Vec<(FermionProduct, f64)> = Vec::new();
        for lh_bp in self {
            output_vec.append(&mut (lh_bp * rhs.clone()))
        }
        output_vec
    }
}

impl Mul<HermitianFermionProduct> for FermionProduct {
    type Output = Vec<(FermionProduct, f64)>;
    /// Implement `*` for a FermionProduct and a HermitianFermionProduct.
    ///
    /// # Arguments
    ///
    /// * `other` - The HermitianFermionProduct to multiply by.
    ///
    /// # Returns
    ///
    /// * `Self` - The result of the multiplication.
    ///
    /// # Panics
    ///
    /// * Could not convert rhs to FermionProduct.
    /// * Unexpectedly failed construction of FermionProduct creation internal struqture bug.
    /// * Bug: somehow commuted through and got a complex value.
    /// * Internal bug in `create_valid_pair`.
    fn mul(self, rhs: HermitianFermionProduct) -> Self::Output {
        let mut output_vec: Vec<(FermionProduct, f64)> = Vec::new();

        let mut right_to_mul: Vec<(FermionProduct, f64)> = Vec::new();
        let hfp_to_fp = FermionProduct::new(rhs.creators, rhs.annihilators)
            .expect("Could not convert rhs into a FermionProduct");
        right_to_mul.push((hfp_to_fp.clone(), 1.0));
        if !hfp_to_fp.is_natural_hermitian() {
            right_to_mul.push(hfp_to_fp.hermitian_conjugate());
        }
        for (right, rsign) in right_to_mul {
            let res_vec: Vec<(FermionProduct, f64)> = self.clone() * right;
            for (fp, val) in res_vec.iter() {
                output_vec.push((fp.clone(), val * rsign * 1.0));
            }
        }
        output_vec
    }
}

impl GetValue<FermionProduct> for FermionProduct {
    type ValueIn = CalculatorComplex;
    type ValueOut = CalculatorComplex;

    /// Gets the key corresponding to the input index (here, itself).
    ///
    /// # Arguments
    ///
    /// * `index` - The index for which to get the corresponding Product.
    ///
    /// # Returns
    ///
    /// * `Self` - The corresponding FermionProduct.
    fn get_key(index: &FermionProduct) -> Self {
        index.clone()
    }

    /// Gets the transformed value corresponding to the input index and value (here, itself).
    ///
    /// # Arguments
    ///
    /// * `index` - The index to transform the value by.
    /// * `value` - The value to be transformed.
    ///
    /// # Returns
    ///
    /// * `CalculatorComplex` - The transformed value.
    fn get_transform(
        _index: &FermionProduct,
        value: qoqo_calculator::CalculatorComplex,
    ) -> qoqo_calculator::CalculatorComplex {
        value
    }
}

/// Implements the format function (Display trait) of FermionProduct.
///
impl std::fmt::Display for FermionProduct {
    /// Formats the FermionProduct using the given formatter.
    ///
    /// # Arguments
    ///
    /// * `f` - The formatter to use.
    ///
    /// # Returns
    ///
    /// * `std::fmt::Result` - The formatted FermionProduct.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut string: String = String::new();
        if self.creators.is_empty() & self.annihilators.is_empty() {
            string.push('I'); // empty is just identity
        } else {
            for index in self.creators() {
                string.push_str(format!("c{index}").as_str());
            }
            for index in self.annihilators() {
                string.push_str(format!("a{index}").as_str());
            }
        }
        write!(f, "{string}")
    }
}

impl FromStr for FermionProduct {
    type Err = StruqtureError;
    /// Constructs a FermionProduct from a string.
    ///
    /// # Arguments
    ///
    /// * `s` - The string to convert.
    ///
    /// # Returns
    ///
    /// * `Ok(Self)` - The successfully converted FermionProduct.
    /// * `Err(StruqtureError::IndicesNotNormalOrdered)` - Indices are not normal ordered.
    /// * `Err(StruqtureError::FromStringFailed)` - Used operator that is neither 'c' nor 'a'.
    /// * `Err(StruqtureError::FromStringFailed)` - Index in given creators or annihilators is not an integer.
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s == "I" {
            Self::new([], [])
        } else {
            let mut creators: TinyVec<[usize; 2]> = TinyVec::<[usize; 2]>::with_capacity(2);
            let mut annihilators: TinyVec<[usize; 2]> = TinyVec::<[usize; 2]>::with_capacity(2);

            let operators = s.split(char::is_numeric).filter(|s| !s.is_empty());
            let indices = s.split(char::is_alphabetic).filter(|s| !s.is_empty());
            let mut parsing_creators: bool = true;
            for (index, op) in indices.zip(operators) {
                match index.parse() {
                    Ok(num) => {
                        match op{
                            "c" => {if parsing_creators{ creators.push(num);} else{return Err(StruqtureError::IndicesNotNormalOrdered{index_i: num, index_j: num+1})}}
                            "a" => {annihilators.push(num); parsing_creators = false;}
                            _ => return Err(StruqtureError::FromStringFailed{msg: format!("Used operator {op} that is neither 'c' nor 'a' in FermionProduct::from_str")})
                        }
                    }
                    Err(_) => return Err(StruqtureError::FromStringFailed{msg: format!("Index of Fermion operator {index} is not a FermionProduct::from_str")}),
                }
            }
            Self::new(creators, annihilators)
        }
    }
}

/// A hermitian product of fermionic creation and annihilation operators
///
/// The HermitianFermionProduct is used as an index for hermitian, normal ordered fermionic operators.
/// A fermionic operator can be written as a sum over normal ordered products of creation and annihilation operators.
/// The FermionProduct is used as an index when setting or adding new summands to a fermionic operator and when querying the
/// weight of a product of operators in the sum.
///
/// # Example
///
/// ```rust
/// use struqture::prelude::*;
/// use struqture::fermions::HermitianFermionProduct;
///
/// let f_product = HermitianFermionProduct::new([0, 1], [0, 1]).unwrap();
/// println!("{}", f_product);
///
/// ```
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Default)]
pub struct HermitianFermionProduct {
    /// The ordered list of creator indices.
    creators: TinyVec<[usize; 2]>,
    /// The ordered list of annihilator indices.
    annihilators: TinyVec<[usize; 2]>,
}

#[cfg(feature = "json_schema")]
impl schemars::JsonSchema for HermitianFermionProduct {
    fn schema_name() -> std::borrow::Cow<'static, str> {
        "HermitianFermionProduct".into()
    }

    fn json_schema(_generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
        schemars::json_schema!({
            "type": "string",
            "description": "Represents products of Fermionic creators and annhilators by a string creators (c) or annihilators (a) followed by the modes they are acting on. E.g. c0a1."
        })
    }
}

impl crate::SerializationSupport for HermitianFermionProduct {
    fn struqture_type() -> crate::StruqtureType {
        crate::StruqtureType::HermitianFermionProduct
    }
}

/// Implementing serde serialization writing directly to string.
///
impl Serialize for HermitianFermionProduct {
    /// Serialization function for FermionProduct according to string type.
    ///
    /// # Arguments
    ///
    /// * `self` - FermionProduct to be serialized.
    /// * `serializer` - Serializer used for serialization.
    ///
    /// # Returns
    ///
    /// `S::Ok` - Serialized instance of FermionProduct.
    /// `S::Error` - Error in the serialization process.
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let readable = serializer.is_human_readable();
        if readable {
            serializer.serialize_str(&self.to_string())
        } else {
            let mut tuple = serializer.serialize_tuple(2)?;
            tuple.serialize_element(&self.creators)?;
            tuple.serialize_element(&self.annihilators)?;
            tuple.end()
        }
    }
}

/// Deserializing directly from string.
///
impl<'de> Deserialize<'de> for HermitianFermionProduct {
    /// Deserialization function for FermionProduct.
    ///
    /// # Arguments
    ///
    /// * `self` - Serialized instance of FermionProduct to be deserialized.
    /// * `deserializer` - Deserializer used for deserialization.
    ///
    /// # Returns
    ///
    /// `DecoherenceProduct` - Deserialized instance of FermionProduct.
    /// `D::Error` - Error in the deserialization process.
    fn deserialize<D>(deserializer: D) -> Result<HermitianFermionProduct, D::Error>
    where
        D: Deserializer<'de>,
    {
        let human_readable = deserializer.is_human_readable();
        if human_readable {
            struct TemporaryVisitor;
            impl<'de> Visitor<'de> for TemporaryVisitor {
                type Value = HermitianFermionProduct;

                fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                    formatter.write_str("String")
                }

                fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
                where
                    E: serde::de::Error,
                {
                    HermitianFermionProduct::from_str(v)
                        .map_err(|err| E::custom(format!("{err:?}")))
                }

                fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
                where
                    E: serde::de::Error,
                {
                    HermitianFermionProduct::from_str(v)
                        .map_err(|err| E::custom(format!("{err:?}")))
                }
            }

            deserializer.deserialize_str(TemporaryVisitor)
        } else {
            struct FermionProductVisitor;
            impl<'de> serde::de::Visitor<'de> for FermionProductVisitor {
                type Value = HermitianFermionProduct;
                fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                    std::fmt::Formatter::write_str(
                        formatter,
                        "Tuple of two sequences of unsigned integers",
                    )
                }
                // when variants are marked by String values
                fn visit_seq<M>(self, mut access: M) -> Result<Self::Value, M::Error>
                where
                    M: SeqAccess<'de>,
                {
                    let creators: TinyVec<[usize; 2]> = match access.next_element()? {
                        Some(x) => x,
                        None => {
                            return Err(M::Error::custom("Missing creator sequence".to_string()));
                        }
                    };
                    let annihilators: TinyVec<[usize; 2]> = match access.next_element()? {
                        Some(x) => x,
                        None => {
                            return Err(M::Error::custom(
                                "Missing annihilator sequence".to_string(),
                            ));
                        }
                    };

                    HermitianFermionProduct::new(creators, annihilators).map_err(M::Error::custom)
                }
            }
            let pp_visitor = FermionProductVisitor;

            deserializer.deserialize_tuple(2, pp_visitor)
        }
    }
}

impl ModeIndex for HermitianFermionProduct {
    /// Creates a new HermitianFermionProduct.
    ///
    /// # Arguments
    ///
    /// * `creators` - The creator indices to have in the HermitianFermionProduct.
    /// * `annihilators` - The annihilators indices to have in the HermitianFermionProduct.
    ///
    /// # Returns
    ///
    /// * `Ok(HermitianFermionProduct)` - The new HermitianFermionProduct with the given creators and annihilators.
    /// * `Err(StruqtureError::IncorrectlyOrderedIndices)` - Indices given in creators/annihilators are either not normal ordered, or contain a double index specification.
    /// * `Err(StruqtureError::CreatorsAnnihilatorsMinimumIndex)` - The minimum index of the creators is larger than the minimum index of the annihilators.
    fn new(
        creators: impl IntoIterator<Item = usize>,
        annihilators: impl IntoIterator<Item = usize>,
    ) -> Result<Self, StruqtureError> {
        let creators: TinyVec<[usize; 2]> = creators.into_iter().collect();
        match creators.windows(2).all(|w| w[0] < w[1]) {
            true => {}
            false => return Err(StruqtureError::IncorrectlyOrderedIndices),
        }

        let annihilators: TinyVec<[usize; 2]> = annihilators.into_iter().collect();
        match annihilators.windows(2).all(|w| w[0] < w[1]) {
            true => {}
            false => return Err(StruqtureError::IncorrectlyOrderedIndices),
        }

        let mut number_equal_indices = 0;

        for (creator, annihilator) in creators.iter().zip(annihilators.iter()) {
            match annihilator.cmp(creator) {
                std::cmp::Ordering::Less => {
                    return Err(StruqtureError::CreatorsAnnihilatorsMinimumIndex {
                        creators_min: Some(*creator),
                        annihilators_min: Some(*annihilator),
                    });
                }
                std::cmp::Ordering::Greater => break,
                _ => {
                    number_equal_indices += 1;
                }
            }
        }
        if creators.len() > number_equal_indices && annihilators.len() == number_equal_indices {
            return Err(StruqtureError::CreatorsAnnihilatorsMinimumIndex {
                creators_min: creators.get(number_equal_indices).copied(),
                annihilators_min: None,
            });
        }

        Ok(Self {
            creators,
            annihilators,
        })
    }

    /// Gets the creator indices of the HermitianFermionProduct.
    ///
    /// # Returns
    ///
    /// * `usize` - The creator indices in the HermitianFermionProduct.
    fn creators(&self) -> std::slice::Iter<'_, usize> {
        self.creators.iter()
    }

    /// Gets the annihilator indices of the HermitianFermionProduct.
    ///
    /// # Returns
    ///
    /// * `usize` - The annihilator indices in the HermitianFermionProduct.
    fn annihilators(&self) -> std::slice::Iter<'_, usize> {
        self.annihilators.iter()
    }

    /// Creates a pair (HermitianFermionProduct, CalculatorComplex).
    ///
    /// The first item is the valid HermitianFermionProduct created from the input creators and annihilators.
    /// The second term is the input CalculatorComplex transformed according to the valid order of creators and annihilators.
    ///
    /// # Arguments
    ///
    /// * `creators` - The creator indices to have in the HermitianFermionProduct.
    /// * `annihilators` - The annihilators indices to have in the HermitianFermionProduct.
    /// * `value` - The CalculatorComplex to transform.
    ///
    /// # Returns
    ///
    /// * `Ok((HermitianFermionProduct, CalculatorComplex))` - The valid HermitianFermionProduct and the corresponding transformed CalculatorComplex.
    /// * `Err(StruqtureError::IndicesContainDoubles)` - Indices given in either creators or annihilators contain a double index specification.
    fn create_valid_pair(
        creators: impl IntoIterator<Item = usize>,
        annihilators: impl IntoIterator<Item = usize>,
        value: qoqo_calculator::CalculatorComplex,
    ) -> Result<(Self, qoqo_calculator::CalculatorComplex), StruqtureError> {
        let creators: TinyVec<[usize; 2]> = creators.into_iter().collect();
        let (new_creators, contains_double, parity_c) = sort_and_signal(creators);
        if contains_double {
            return Err(StruqtureError::IndicesContainDoubles {});
        }

        let annihilators: TinyVec<[usize; 2]> = annihilators.into_iter().collect();
        let (new_annihilators, contains_double, parity_a) = sort_and_signal(annihilators);
        if contains_double {
            return Err(StruqtureError::IndicesContainDoubles {});
        }

        let value = if (parity_c + parity_a) % 2 != 0 {
            value * -1.0
        } else {
            value
        };
        let mut hermitian_conjugate = false;
        let mut number_equal_indices = 0;
        for (creator, annihilator) in new_creators.iter().zip(new_annihilators.iter()) {
            match annihilator.cmp(creator) {
                std::cmp::Ordering::Less => {
                    hermitian_conjugate = true;
                    break;
                }
                std::cmp::Ordering::Greater => break,
                _ => {
                    number_equal_indices += 1;
                }
            }
        }
        if new_creators.len() > number_equal_indices
            && new_annihilators.len() == number_equal_indices
        {
            hermitian_conjugate = true;
        }
        if hermitian_conjugate {
            Ok((
                Self {
                    creators: new_annihilators,
                    annihilators: new_creators,
                },
                value.conj(),
            ))
        } else {
            Ok((
                Self {
                    creators: new_creators,
                    annihilators: new_annihilators,
                },
                value,
            ))
        }
    }
}

impl FermionIndex for HermitianFermionProduct {}

impl HermitianFermionProduct {
    /// Export to struqture_1 format.
    #[cfg(feature = "struqture_1_export")]
    pub fn to_struqture_1(
        &self,
    ) -> Result<struqture_1::fermions::HermitianFermionProduct, StruqtureError> {
        let self_string = self.to_string();
        let struqture_1_product = struqture_1::fermions::HermitianFermionProduct::from_str(
            &self_string,
        )
        .map_err(|err| StruqtureError::GenericError {
            msg: format!("{err}"),
        })?;
        Ok(struqture_1_product)
    }

    /// Export to struqture_1 format.
    #[cfg(feature = "struqture_1_import")]
    pub fn from_struqture_1(
        value: &struqture_1::fermions::HermitianFermionProduct,
    ) -> Result<Self, StruqtureError> {
        let value_string = value.to_string();
        let pauli_product = Self::from_str(&value_string)?;
        Ok(pauli_product)
    }
}

impl CorrespondsTo<HermitianFermionProduct> for HermitianFermionProduct {
    /// Gets the HermitianFermionProduct corresponding to self (here, itself).
    ///
    /// # Returns
    ///
    /// * `HermitianFermionProduct` - The HermitianFermionProduct corresponding to Self.
    fn corresponds_to(&self) -> HermitianFermionProduct {
        self.clone()
    }
}

impl CorrespondsTo<FermionProduct> for HermitianFermionProduct {
    /// Gets the FermionProduct corresponding to Self.
    ///
    /// # Returns
    ///
    /// * `FermionProduct` - The FermionProduct corresponding to Self.
    fn corresponds_to(&self) -> FermionProduct {
        FermionProduct {
            creators: self.creators.clone(),
            annihilators: self.annihilators.clone(),
        }
    }
}

impl SymmetricIndex for HermitianFermionProduct {
    // From trait
    fn hermitian_conjugate(&self) -> (Self, f64) {
        (self.clone(), 1.0)
    }

    // From trait
    fn is_natural_hermitian(&self) -> bool {
        self.creators == self.annihilators
    }
}

/// Implements the multiplication function of HermitianFermionProduct by HermitianFermionProduct.
///
impl Mul<HermitianFermionProduct> for HermitianFermionProduct {
    type Output = Vec<(FermionProduct, f64)>;

    /// Implement `*` for HermitianFermionProduct and HermitianFermionProduct.
    ///
    /// # Arguments
    ///
    /// * `other` - The HermitianFermionProduct to multiply by.
    ///
    /// # Returns
    ///
    /// * `Self` - The two HermitianFermionProducts multiplied.
    ///
    /// # Panics
    ///
    /// * Could not convert self to FermionProduct.
    /// * Could not convert rhs to FermionProduct.
    /// * Unexpectedly failed construction of HermitianFermionProduct creation internal struqture bug.
    /// * Bug: somehow commuted through and got a complex value.
    /// * Internal bug in `create_valid_pair`.
    fn mul(self, rhs: HermitianFermionProduct) -> Self::Output {
        let mut output_vec: Vec<(FermionProduct, f64)> = Vec::new();

        let mut left_to_mul: Vec<(FermionProduct, f64)> = Vec::new();
        let fp_left = FermionProduct::new(self.creators, self.annihilators)
            .expect("Could not convert self into a FermionProduct");
        left_to_mul.push((fp_left.clone(), 1.0));
        if !fp_left.is_natural_hermitian() {
            left_to_mul.push(fp_left.hermitian_conjugate());
        }

        let mut right_to_mul: Vec<(FermionProduct, f64)> = Vec::new();
        let fp_right = FermionProduct::new(rhs.creators, rhs.annihilators)
            .expect("Could not convert rhs into a FermionProduct");
        right_to_mul.push((fp_right.clone(), 1.0));
        if !fp_right.is_natural_hermitian() {
            right_to_mul.push(fp_right.hermitian_conjugate());
        }

        for (left, lsign) in left_to_mul {
            for (right, rsign) in right_to_mul.clone() {
                let res_vec: Vec<(FermionProduct, f64)> = left.clone() * right;
                for (fp, val) in res_vec.iter() {
                    output_vec.push((fp.clone(), val * rsign * lsign));
                }
            }
        }
        output_vec
    }
}

/// Implements the multiplication function of a vector of FermionProducts by a HermitianFermionProduct.
///
impl Mul<HermitianFermionProduct> for Vec<FermionProduct> {
    type Output = Vec<(FermionProduct, f64)>;

    /// Implement `*` for a vector of FermionProducts and a HermitianFermionProduct.
    ///
    /// # Arguments
    ///
    /// * `other` - The HermitianFermionProduct to multiply by.
    ///
    /// # Returns
    ///
    /// * `Self` - The result of the multiplication.
    ///
    /// # Panics
    ///
    /// * Could not convert rhs to FermionProduct.
    /// * Unexpectedly failed construction of HermitianFermionProduct creation internal struqture bug.
    /// * Bug: somehow commuted through and got a complex value.
    /// * Internal bug in `create_valid_pair`.
    fn mul(self, rhs: HermitianFermionProduct) -> Self::Output {
        let mut output_vec: Vec<(FermionProduct, f64)> = Vec::new();
        for lh_bp in self {
            output_vec.append(&mut (lh_bp * rhs.clone()))
        }
        output_vec
    }
}

/// Implements the multiplication function of a HermitianFermionProduct by a reference FermionProduct.
///
impl Mul<&FermionProduct> for HermitianFermionProduct {
    type Output = Vec<(FermionProduct, f64)>;

    /// Implement `*` for a HermitianFermionProduct and a reference FermionProduct.
    ///
    /// # Arguments
    ///
    /// * `other` - The reference FermionProduct to multiply by.
    ///
    /// # Returns
    ///
    /// * `Self` - The result of the multiplication.
    ///
    /// # Panics
    ///
    /// * Could not convert self to FermionProduct.
    /// * Unexpectedly failed construction of HermitianFermionProduct creation internal struqture bug.
    /// * Bug: somehow commuted through and got a complex value.
    /// * Internal bug in `create_valid_pair`.
    fn mul(self, rhs: &FermionProduct) -> Self::Output {
        let mut output_vec: Vec<(FermionProduct, f64)> = Vec::new();

        let mut left_to_mul: Vec<(FermionProduct, f64)> = Vec::new();
        let hfp_to_fp = FermionProduct::new(self.creators, self.annihilators)
            .expect("Could not convert self into a FermionProduct");
        left_to_mul.push((hfp_to_fp.clone(), 1.0));
        if !hfp_to_fp.is_natural_hermitian() {
            left_to_mul.push(hfp_to_fp.hermitian_conjugate());
        }
        for (left, lsign) in left_to_mul {
            let res_vec: Vec<(FermionProduct, f64)> = left.clone() * rhs.clone();
            for (fp, val) in res_vec.iter() {
                output_vec.push((fp.clone(), val * 1.0 * lsign));
            }
        }
        output_vec
    }
}

/// Implements the multiplication function of a HermitianFermionProduct by a vector of FermionProducts.
///
impl Mul<Vec<FermionProduct>> for HermitianFermionProduct {
    type Output = Vec<(FermionProduct, f64)>;

    /// Implement `*` for a HermitianFermionProduct and a vector of FermionProducts.
    ///
    /// # Arguments
    ///
    /// * `other` - The vector of FermionProducts to multiply by.
    ///
    /// # Returns
    ///
    /// * `Self` - The result of the multiplication.
    ///
    /// # Panics
    ///
    /// * Could not convert self to FermionProduct.
    /// * Unexpectedly failed construction of HermitianFermionProduct creation internal struqture bug.
    /// * Bug: somehow commuted through and got a complex value.
    /// * Internal bug in `create_valid_pair`.
    fn mul(self, rhs: Vec<FermionProduct>) -> Self::Output {
        let mut output_vec: Vec<(FermionProduct, f64)> = Vec::new();
        for rh_bp in rhs.iter() {
            output_vec.append(&mut (self.clone() * rh_bp))
        }
        output_vec
    }
}

/// Trait for transforming value stored at index I when using index of different type T to read out value
///
impl GetValue<HermitianFermionProduct> for HermitianFermionProduct {
    type ValueIn = CalculatorComplex;
    type ValueOut = CalculatorComplex;

    /// Gets the key corresponding to the input index (here, itself).
    ///
    /// # Arguments
    ///
    /// * `index` - The index for which to get the corresponding Product.
    ///
    /// # Returns
    ///
    /// * `Self` - The corresponding HermitianFermionProduct.
    fn get_key(index: &HermitianFermionProduct) -> Self {
        index.clone()
    }

    /// Gets the transformed value corresponding to the input index and value (here, itself).
    ///
    /// # Arguments
    ///
    /// * `index` - The index to transform the value by.
    /// * `value` - The value to be transformed.
    ///
    /// # Returns
    ///
    /// * `CalculatorComplex` - The transformed value.
    fn get_transform(
        _index: &HermitianFermionProduct,
        value: qoqo_calculator::CalculatorComplex,
    ) -> qoqo_calculator::CalculatorComplex {
        value
    }
}

impl GetValue<FermionProduct> for HermitianFermionProduct {
    type ValueIn = CalculatorComplex;
    type ValueOut = CalculatorComplex;

    /// Gets the HermitianFermionProduct corresponding to the input FermionProduct.
    ///
    /// # Arguments
    ///
    /// * `index` - The FermionProduct of which to get the corresponding HermitianFermionProduct.
    ///
    /// # Returns
    ///
    /// * `Self` - The corresponding HermitianFermionProduct.
    fn get_key(index: &FermionProduct) -> Self {
        if index.creators().min() > index.annihilators().min() {
            Self {
                creators: index.annihilators.clone(),
                annihilators: index.creators.clone(),
            }
        } else {
            Self {
                creators: index.creators.clone(),
                annihilators: index.annihilators.clone(),
            }
        }
    }

    /// Gets the transformed value corresponding to the input FermionProduct and value.
    ///
    /// # Arguments
    ///
    /// * `index` - The FermionProduct to transform the value by.
    /// * `value` - The value to be transformed.
    ///
    /// # Returns
    ///
    /// * `CalculatorComplex` - The transformed value.
    fn get_transform(
        index: &FermionProduct,
        value: qoqo_calculator::CalculatorComplex,
    ) -> qoqo_calculator::CalculatorComplex {
        if index.creators().min() > index.annihilators().min() {
            value.conj()
        } else {
            value
        }
    }
}

impl GetValue<HermitianFermionProduct> for FermionProduct {
    type ValueIn = CalculatorComplex;
    type ValueOut = CalculatorComplex;

    /// Gets the FermionProduct corresponding to the input HermitianFermionProduct.
    ///
    /// # Arguments
    ///
    /// * `index` - The index for which to get the corresponding Product.
    ///
    /// # Returns
    ///
    /// * `Self` - The corresponding FermionProduct.
    fn get_key(index: &HermitianFermionProduct) -> Self {
        Self {
            creators: index.creators.clone(),
            annihilators: index.annihilators.clone(),
        }
    }

    /// Gets the transformed value corresponding to the input HermitianFermionProduct and value (here, itself).
    ///
    /// # Arguments
    ///
    /// * `index` - The HermitianFermionProduct to transform the value by.
    /// * `value` - The value to be transformed.
    ///
    /// # Returns
    ///
    /// * `CalculatorComplex` - The transformed value.
    fn get_transform(
        _index: &HermitianFermionProduct,
        value: qoqo_calculator::CalculatorComplex,
    ) -> qoqo_calculator::CalculatorComplex {
        value
    }
}

/// Implements the format function (Display trait) of HermitianFermionProduct.
///
impl std::fmt::Display for HermitianFermionProduct {
    /// Formats the HermitianFermionProduct using the given formatter.
    ///
    /// # Arguments
    ///
    /// * `f` - The formatter to use.
    ///
    /// # Returns
    ///
    /// * `std::fmt::Result` - The formatted HermitianFermionProduct.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut string: String = String::new();
        if self.creators.is_empty() & self.annihilators.is_empty() {
            string.push('I'); // empty modes is just the identity
        } else {
            for index in self.creators() {
                string.push_str(format!("c{index}").as_str());
            }
            for index in self.annihilators() {
                string.push_str(format!("a{index}").as_str());
            }
        }
        write!(f, "{string}")
    }
}

impl FromStr for HermitianFermionProduct {
    type Err = StruqtureError;

    /// Constructs a HermitianFermionProduct from a string.
    ///
    /// # Arguments
    ///
    /// * `s` - The string to convert.
    ///
    /// # Returns
    ///
    /// * `Ok(Self)` - The successfully converted HermitianFermionProduct.
    /// * `Err(StruqtureError::IndicesNotNormalOrdered)` - Indices are not normal ordered.
    /// * `Err(StruqtureError::FromStringFailed)` - Used operator that is neither 'c' nor 'a'.
    /// * `Err(StruqtureError::FromStringFailed)` - Index in given creators or annihilators is not an integer.
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s == "I" {
            Self::new([], [])
        } else {
            let mut creators: TinyVec<[usize; 2]> = TinyVec::<[usize; 2]>::with_capacity(2);
            let mut annihilators: TinyVec<[usize; 2]> = TinyVec::<[usize; 2]>::with_capacity(2);

            let operators = s.split(char::is_numeric).filter(|s| !s.is_empty());
            let indices = s.split(char::is_alphabetic).filter(|s| !s.is_empty());
            let mut parsing_creators: bool = true;
            for (index, op) in indices.zip(operators) {
                match index.parse() {
                    Ok(num) => {
                        match op{
                            "c" => {if parsing_creators{ creators.push(num);} else{return Err(StruqtureError::IndicesNotNormalOrdered{index_i: num, index_j: num+1})}}
                            "a" => {annihilators.push(num); parsing_creators = false;}
                            _ => return Err(StruqtureError::FromStringFailed{msg: format!("Used operator {op} that is neither 'c' nor 'a' in HermitianFermionProduct::from_str")})
                        }
                    }
                    Err(_) => return Err(StruqtureError::FromStringFailed{msg: format!("Index of Fermion operator {index} is not a HermitianFermionProduct::from_str")}),
                }
            }
            Self::new(creators, annihilators)
        }
    }
}

// Helper functions
/// Re-sorts indices for creators or annihilators for normal ordering and signals parity of the reordering and whether any term occurs twice
fn sort_and_signal(indices: TinyVec<[usize; 2]>) -> (TinyVec<[usize; 2]>, bool, usize) {
    let mut parity: usize = 0;
    let mut contain_double = false;
    let mut local_indices = indices;
    for outer_counter in 0..local_indices.len() {
        for inner_counter in (0..outer_counter).rev() {
            match local_indices[inner_counter].cmp(&local_indices[inner_counter + 1]) {
                Ordering::Greater => {
                    local_indices.swap(inner_counter, inner_counter + 1);
                    parity += 1;
                }
                Ordering::Equal => {
                    contain_double = true;
                    break;
                }
                Ordering::Less => {
                    break;
                }
            }
        }
    }
    (local_indices, contain_double, parity)
}

impl JordanWignerFermionToSpin for FermionProduct {
    type Output = PauliOperator;

    /// Implements JordanWignerFermionToSpin for a FermionProduct.
    ///
    /// The convention used is that |0> represents an empty fermionic state (spin-orbital),
    /// and |1> represents an occupied fermionic state.
    ///
    /// # Returns
    ///
    /// `PauliOperator` - The spin operator that results from the transformation.
    ///
    /// # Panics
    ///
    /// * Unexpectedly failed to add a PauliProduct to a PauliOperator internal struqture bug.
    /// * Internal bug in `add_operator_product`.
    fn jordan_wigner(&self) -> Self::Output {
        let number_creators = self.number_creators();
        let number_annihilators = self.number_annihilators();
        let mut qubit_operator = PauliOperator::new();

        let mut id = PauliProduct::new();
        id = id.set_pauli(0, SinglePauliOperator::Identity);
        qubit_operator
            .add_operator_product(id, CalculatorComplex::new(1.0, 0.0))
            .expect("Internal bug in add_operator_product.");

        // Jordan-Wigner strings are inserted every second lowering (raising) operator, in even or
        // odd positions depending on the parity of the total number of creation (annihilation)
        // operators.
        let mut previous = 0;
        for (index, site) in self.creators().enumerate() {
            if index % 2 != number_creators % 2 {
                for i in previous..*site {
                    qubit_operator = qubit_operator * PauliProduct::new().z(i)
                }
            }
            qubit_operator = qubit_operator * _lowering_operator(site);
            previous = *site;
        }

        previous = 0;
        for (index, site) in self.annihilators().enumerate() {
            if index % 2 != number_annihilators % 2 {
                for i in previous..*site {
                    qubit_operator = qubit_operator * PauliProduct::new().z(i)
                }
            }
            qubit_operator = qubit_operator * _raising_operator(site);
            previous = *site;
        }
        qubit_operator
    }
}

impl JordanWignerFermionToSpin for HermitianFermionProduct {
    type Output = PauliHamiltonian;

    /// Implements JordanWignerFermionToSpin for a HermitianFermionProduct.
    ///
    /// The convention used is that |0> represents an empty fermionic state (spin-orbital),
    /// and |1> represents an occupied fermionic state.
    ///
    /// # Returns
    ///
    /// `PauliHamiltonian` - The spin operator that results from the transformation.
    ///
    /// # Panics
    ///
    /// * Internal bug in `add_operator_product`.
    fn jordan_wigner(&self) -> Self::Output {
        let number_creators = self.number_creators();
        let number_annihilators = self.number_annihilators();
        let mut qubit_operator = PauliOperator::new();

        let mut id = PauliProduct::new();
        id = id.set_pauli(0, SinglePauliOperator::Identity);
        qubit_operator
            .add_operator_product(id, CalculatorComplex::new(1.0, 0.0))
            .expect("Internal bug in add_operator_product.");

        // Jordan-Wigner strings are inserted every second lowering (raising) operator, in even or
        // odd positions depending on the parity of the total number of creation (annihilation)
        // operators.
        let mut previous = 0;
        for (index, site) in self.creators().enumerate() {
            if index % 2 != number_creators % 2 {
                for i in previous..*site {
                    qubit_operator = qubit_operator * PauliProduct::new().z(i)
                }
            }
            qubit_operator = qubit_operator * _lowering_operator(site);
            previous = *site;
        }

        previous = 0;
        for (index, site) in self.annihilators().enumerate() {
            if index % 2 != number_annihilators % 2 {
                for i in previous..*site {
                    qubit_operator = qubit_operator * PauliProduct::new().z(i)
                }
            }
            qubit_operator = qubit_operator * _raising_operator(site);
            previous = *site;
        }

        // Spin terms with imaginary coefficients are dropped, and
        // real coefficients are doubled.
        if !self.is_natural_hermitian() {
            let mut out = PauliHamiltonian::new();
            for (product, coeff) in qubit_operator.iter() {
                if coeff.im == 0.0.into() {
                    out.add_operator_product(product.clone(), coeff.re.clone() * 2)
                        .expect("Internal bug in add_operator_product.");
                }
            }
            return out;
        }
        PauliHamiltonian::try_from(qubit_operator).expect(
            "Error in conversion from PauliOperator to
PauliHamiltonian, despite the internal check that the HermitianFermionProduct in the jordan-wigner
transform is hermitian.",
        )
    }
}

fn _lowering_operator(i: &usize) -> PauliOperator {
    let mut out = PauliOperator::new();
    out.add_operator_product(PauliProduct::new().x(*i), CalculatorComplex::new(0.5, 0.0))
        .expect("Internal bug in add_operator_product.");
    out.add_operator_product(PauliProduct::new().y(*i), CalculatorComplex::new(0.0, -0.5))
        .expect("Internal bug in add_operator_product.");
    out
}
fn _raising_operator(i: &usize) -> PauliOperator {
    let mut out = PauliOperator::new();
    out.add_operator_product(PauliProduct::new().x(*i), CalculatorComplex::new(0.5, 0.0))
        .expect("Internal bug in add_operator_product.");
    out.add_operator_product(PauliProduct::new().y(*i), CalculatorComplex::new(0.0, 0.5))
        .expect("Internal bug in add_operator_product.");
    out
}

// When constructing multiplication with commute_creator remember to skip all products with double creators or double annihilators
/// Assumes both annihilators_left and creators_right are sorted.
type MulVec = Vec<((TinyVec<[usize; 2]>, TinyVec<[usize; 2]>), f64)>;
#[allow(unused)]
fn commute_creator_annihilator_fermionic(
    annihilators_left: &[usize],
    creators_right: &[usize],
) -> Vec<(CreatorsAnnihilators, f64)> {
    let mut result: Vec<(CreatorsAnnihilators, f64)> = Vec::new();
    // Total parity swapping all creators past all annihilators (multiplication)
    let orig_parity = if (creators_right.len() * annihilators_left.len()) % 2 == 0 {
        1.0
    } else {
        -1.0
    };
    let mut found = false;

    for (cindex, creator) in creators_right.iter().enumerate() {
        if let Some((aindex, _)) = annihilators_left
            .iter()
            .enumerate()
            .find(|(_, an)| *an == creator)
        {
            // need to swap past all annihilators before (aindex) and all creators after fitting creator (creators_right.len() - cindex - 1)
            let offset_parity = if (annihilators_left.len() - aindex + cindex - 1) % 2 == 0 {
                1.0
            } else {
                -1.0
            };
            let recurse_creators: TinyVec<[usize; 2]> = creators_right
                .iter()
                .enumerate()
                .filter(|(index, _)| *index != cindex)
                .map(|(_, rc)| rc)
                .copied()
                .collect();
            let recurse_annihilators: TinyVec<[usize; 2]> = annihilators_left
                .iter()
                .enumerate()
                .filter(|(index, _)| *index != aindex)
                .map(|(_, rc)| rc)
                .copied()
                .collect();
            let recursed_result: MulVec =
                commute_creator_annihilator_fermionic(&recurse_annihilators, &recurse_creators)
                    .into_iter()
                    .map(|((c, a), parity)| ((c, a), parity * offset_parity))
                    .collect();
            result.extend(recursed_result.iter().cloned());
            // The parity for the case where `creator` and the corresponding annihilator are commuted (with extra minus sign)
            // and not eliminated. creator is swapped with all creators in front of it plus all annihilators and annihilator is
            // swapped with all annihilators past it + all creators
            let commuted_parity =
                if (2 * annihilators_left.len() + creators_right.len() - aindex + cindex - 2) % 2
                    == 0
                {
                    1.0
                } else {
                    -1.0
                };
            for ((mut c, mut a), p) in recursed_result.clone() {
                c.insert(0, *creator);
                a.push(*creator);
                result.push(((c, a), p * offset_parity * commuted_parity))
            }
            found = true;
            break;
        }
    }
    if !found {
        result.push((
            (
                creators_right.iter().copied().collect(),
                annihilators_left.iter().copied().collect(),
            ),
            orig_parity,
        ));
    }
    result
}

#[cfg(test)]
mod test {
    use crate::ModeTinyVec;

    use super::*;
    use test_case::test_case;
    use tinyvec::tiny_vec;

    #[test_case(tiny_vec!([usize; 2] => 0, 2, 4), tiny_vec!([usize; 2] => 1, 3, 5),
     vec![((tiny_vec!([usize; 2] => 1, 3, 5), tiny_vec!([usize; 2] => 0, 2, 4)), -1.0)]; "0,2,4 - 1,3,5")]
    #[test_case(tiny_vec!([usize; 2] => 0), tiny_vec!([usize; 2] => 0),
     vec![((tiny_vec!([usize; 2] => 0), tiny_vec!([usize; 2] => 0)), -1.0), ((tiny_vec!([usize; 2]), tiny_vec!([usize; 2])), 1.0)]; "0, - 0")]
    // `commute_creator_annihilator_fermionic` will not reorder the indices in creators or in annihilators, hence one of the results here being [30, 1] with a parity of -1
    #[test_case(tiny_vec!([usize; 2] => 20), tiny_vec!([usize; 2]),
     vec![((tiny_vec!([usize; 2]), tiny_vec!([usize; 2] => 20)), 1.0)]; "20 - empty")]
    #[test_case(tiny_vec!([usize; 2] => 1,20), tiny_vec!([usize; 2] => 1,30),
     vec![((tiny_vec!([usize; 2] => 1,30), tiny_vec!([usize; 2] => 20,1)), -1.0), ((tiny_vec!([usize; 2] => 30), tiny_vec!([usize; 2] => 20)), 1.0)]; "1,20 - 1,30")]
    #[test_case(tiny_vec!([usize; 2] => 1,2,20), tiny_vec!([usize; 2] => 1,2,30),
     vec![((tiny_vec!([usize; 2] => 30), tiny_vec!([usize; 2] => 20)), 1.0), ((tiny_vec!([usize; 2] => 2,30), tiny_vec!([usize; 2] => 20,2)), -1.0),
          ((tiny_vec!([usize; 2] => 1,30), tiny_vec!([usize; 2] => 20,1)), -1.0), ((tiny_vec!([usize; 2] => 1,2,30), tiny_vec!([usize; 2] => 20,2,1)), 1.0)]; "1,2,20 - 1,2,30")]
    #[test_case(tiny_vec!([usize; 2] => 10,20,30), tiny_vec!([usize; 2] => 10,30),
    vec![((tiny_vec!([usize; 2]), tiny_vec!([usize; 2] => 20)), 1.0), ((tiny_vec!([usize; 2] => 30), tiny_vec!([usize; 2] => 20,30)), 1.0),
        ((tiny_vec!([usize; 2] => 10), tiny_vec!([usize; 2] => 20,10)), 1.0), ((tiny_vec!([usize; 2] => 10,30), tiny_vec!([usize; 2] => 20,30,10)), 1.0)]; "10,20,30 - 10,30")]
    #[test_case(tiny_vec!([usize; 2] => 10,20,30), tiny_vec!([usize; 2] => 10,30,40),
    vec![((tiny_vec!([usize; 2] => 40), tiny_vec!([usize; 2] => 20)), -1.0), ((tiny_vec!([usize; 2] => 30,40), tiny_vec!([usize; 2] => 20,30)), 1.0),
        ((tiny_vec!([usize; 2] => 10,40), tiny_vec!([usize; 2] => 20,10)), 1.0), ((tiny_vec!([usize; 2] => 10,30,40), tiny_vec!([usize; 2] => 20,30,10)), -1.0)]; "10,20,30 - 10,30,40")]
    fn commute_fermionic(
        annihilators_left: TinyVec<[usize; 2]>,
        creators_right: TinyVec<[usize; 2]>,
        expected: Vec<((ModeTinyVec, ModeTinyVec), f64)>,
    ) {
        let result = commute_creator_annihilator_fermionic(&annihilators_left, &creators_right);
        assert_eq!(result.len(), expected.len());
        for pair in expected {
            assert!(result.contains(&pair));
        }
    }
}