omeco 0.2.4

Tensor network contraction order optimization
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
//! Core data structures for einsum expressions and contraction trees.
//!
//! This module provides:
//! - [`EinCode`]: Representation of an einsum expression
//! - [`NestedEinsum`]: A binary tree encoding the contraction order
//! - [`SlicedEinsum`]: An einsum with sliced indices for reduced space complexity

use crate::Label;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fmt;

/// An einsum expression specifying how tensors are contracted.
///
/// The notation follows Einstein summation convention:
/// - `ixs` contains the index labels for each input tensor
/// - `iy` contains the index labels for the output tensor
/// - Repeated indices (appearing in multiple inputs but not output) are summed over
///
/// # Example
/// ```
/// use omeco::EinCode;
///
/// // Matrix multiplication: C[i,k] = A[i,j] * B[j,k]
/// // In einsum notation: "ij,jk->ik"
/// let code = EinCode::new(
///     vec![vec!['i', 'j'], vec!['j', 'k']],
///     vec!['i', 'k']
/// );
///
/// assert_eq!(code.num_tensors(), 2);
/// assert_eq!(code.contracted_indices(), vec!['j']);
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EinCode<L: Label> {
    /// Input tensor indices (vector of index vectors for each tensor)
    pub ixs: Vec<Vec<L>>,
    /// Output indices
    pub iy: Vec<L>,
}

impl<L: Label> EinCode<L> {
    /// Create a new EinCode from input indices and output indices.
    pub fn new(ixs: Vec<Vec<L>>, iy: Vec<L>) -> Self {
        Self { ixs, iy }
    }

    /// Create an EinCode for a trace operation (no output indices).
    pub fn trace(ixs: Vec<Vec<L>>) -> Self {
        Self {
            ixs,
            iy: Vec::new(),
        }
    }

    /// Get the number of input tensors.
    #[inline]
    pub fn num_tensors(&self) -> usize {
        self.ixs.len()
    }

    /// Get all unique labels in this einsum.
    pub fn unique_labels(&self) -> Vec<L> {
        let mut seen = HashSet::new();
        let mut result = Vec::new();
        for ix in &self.ixs {
            for l in ix {
                if seen.insert(l.clone()) {
                    result.push(l.clone());
                }
            }
        }
        for l in &self.iy {
            if seen.insert(l.clone()) {
                result.push(l.clone());
            }
        }
        result
    }

    /// Get indices that are contracted (summed over).
    ///
    /// These are indices that appear in inputs but not in the output.
    pub fn contracted_indices(&self) -> Vec<L> {
        let output_set: HashSet<_> = self.iy.iter().cloned().collect();
        let mut contracted = Vec::new();
        let mut seen = HashSet::new();

        for ix in &self.ixs {
            for l in ix {
                if !output_set.contains(l) && seen.insert(l.clone()) {
                    contracted.push(l.clone());
                }
            }
        }
        contracted
    }

    /// Get indices that appear in the output (free indices).
    pub fn output_indices(&self) -> &[L] {
        &self.iy
    }

    /// Get the indices for a specific input tensor.
    pub fn input_indices(&self, tensor_idx: usize) -> Option<&[L]> {
        self.ixs.get(tensor_idx).map(|v| v.as_slice())
    }

    /// Check if this is a valid einsum (all output indices appear in inputs).
    pub fn is_valid(&self) -> bool {
        let input_set: HashSet<_> = self.ixs.iter().flatten().cloned().collect();
        self.iy.iter().all(|l| input_set.contains(l))
    }
}

impl<L: Label + fmt::Display> fmt::Display for EinCode<L> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let inputs: Vec<String> = self
            .ixs
            .iter()
            .map(|ix| ix.iter().map(|l| l.to_string()).collect::<String>())
            .collect();
        let output: String = self.iy.iter().map(|l| l.to_string()).collect();
        write!(f, "{}->{}", inputs.join(","), output)
    }
}

/// A binary tree representing the contraction order for an einsum.
///
/// Each internal node represents a contraction of its two children,
/// and each leaf represents an input tensor.
///
/// # Example
/// ```
/// use omeco::{EinCode, NestedEinsum};
///
/// // Create leaves for input tensors
/// let leaf0 = NestedEinsum::<char>::leaf(0);
/// let leaf1 = NestedEinsum::<char>::leaf(1);
///
/// // Contract tensors 0 and 1
/// let contraction = EinCode::new(
///     vec![vec!['i', 'j'], vec!['j', 'k']],
///     vec!['i', 'k']
/// );
/// let tree = NestedEinsum::node(vec![leaf0, leaf1], contraction);
///
/// assert!(tree.is_binary());
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum NestedEinsum<L: Label> {
    /// A leaf node referencing an input tensor by index.
    Leaf {
        /// Index of the input tensor (0-indexed)
        tensor_index: usize,
    },
    /// An internal node representing a contraction.
    Node {
        /// Child nodes to contract
        args: Vec<NestedEinsum<L>>,
        /// The einsum operation for this contraction
        eins: EinCode<L>,
    },
}

impl<L: Label> NestedEinsum<L> {
    /// Create a leaf node for an input tensor.
    pub fn leaf(tensor_index: usize) -> Self {
        Self::Leaf { tensor_index }
    }

    /// Create an internal node for a contraction.
    pub fn node(args: Vec<NestedEinsum<L>>, eins: EinCode<L>) -> Self {
        Self::Node { args, eins }
    }

    /// Check if this node is a leaf.
    #[inline]
    pub fn is_leaf(&self) -> bool {
        matches!(self, Self::Leaf { .. })
    }

    /// Get the tensor index if this is a leaf node.
    pub fn tensor_index(&self) -> Option<usize> {
        match self {
            Self::Leaf { tensor_index } => Some(*tensor_index),
            Self::Node { .. } => None,
        }
    }

    /// Check if the tree is strictly binary (each internal node has exactly 2 children).
    pub fn is_binary(&self) -> bool {
        match self {
            Self::Leaf { .. } => true,
            Self::Node { args, .. } => args.len() == 2 && args.iter().all(|a| a.is_binary()),
        }
    }

    /// Count the total number of nodes in the tree.
    pub fn node_count(&self) -> usize {
        match self {
            Self::Leaf { .. } => 1,
            Self::Node { args, .. } => 1 + args.iter().map(|a| a.node_count()).sum::<usize>(),
        }
    }

    /// Count the number of leaf nodes (input tensors).
    pub fn leaf_count(&self) -> usize {
        match self {
            Self::Leaf { .. } => 1,
            Self::Node { args, .. } => args.iter().map(|a| a.leaf_count()).sum(),
        }
    }

    /// Get all leaf tensor indices in depth-first order.
    pub fn leaf_indices(&self) -> Vec<usize> {
        let mut result = Vec::new();
        self.collect_leaves(&mut result);
        result
    }

    fn collect_leaves(&self, result: &mut Vec<usize>) {
        match self {
            Self::Leaf { tensor_index } => result.push(*tensor_index),
            Self::Node { args, .. } => {
                for arg in args {
                    arg.collect_leaves(result);
                }
            }
        }
    }

    /// Get the output labels for this subtree.
    ///
    /// Requires the original input tensor labels to compute leaf outputs.
    pub fn output_labels(&self, input_labels: &[Vec<L>]) -> Vec<L> {
        match self {
            Self::Leaf { tensor_index } => {
                input_labels.get(*tensor_index).cloned().unwrap_or_default()
            }
            Self::Node { eins, .. } => eins.iy.clone(),
        }
    }

    /// Get the depth of the tree (longest path from root to leaf).
    pub fn depth(&self) -> usize {
        match self {
            Self::Leaf { .. } => 0,
            Self::Node { args, .. } => 1 + args.iter().map(|a| a.depth()).max().unwrap_or(0),
        }
    }

    /// Convert a NestedEinsum back to a flat EinCode.
    ///
    /// This "flattens" the contraction tree by collecting all the input tensors
    /// in order and using the final output labels from the root.
    ///
    /// # Arguments
    /// * `input_labels` - The original labels for each input tensor, indexed by tensor_index
    ///
    /// # Returns
    /// A flat EinCode representing the same computation
    ///
    /// # Example
    /// ```rust
    /// use omeco::{EinCode, NestedEinsum};
    ///
    /// let original_ixs = vec![vec!['i', 'j'], vec!['j', 'k']];
    /// let original_output = vec!['i', 'k'];
    /// let code = EinCode::new(original_ixs.clone(), original_output.clone());
    ///
    /// // After optimization
    /// let nested = omeco::optimize_code(&code, &omeco::uniform_size_dict(&code, 2), &omeco::GreedyMethod::default()).unwrap();
    ///
    /// // Convert back to flat EinCode
    /// let flat = nested.to_eincode(&original_ixs);
    ///
    /// assert_eq!(flat.ixs, original_ixs);
    /// assert_eq!(flat.iy, original_output);
    /// ```
    pub fn to_eincode(&self, input_labels: &[Vec<L>]) -> EinCode<L> {
        // Get the indices of all leaf tensors in order
        let tensor_indices = self.leaf_indices();

        // Collect the labels for each input tensor
        let ixs: Vec<Vec<L>> = tensor_indices
            .iter()
            .map(|&idx| input_labels.get(idx).cloned().unwrap_or_default())
            .collect();

        // Get the final output labels
        let iy = self.output_labels(input_labels);

        EinCode::new(ixs, iy)
    }

    /// Check if this tree forms a valid path decomposition.
    ///
    /// A path decomposition is a binary tree where each internal node has at most
    /// one internal child (the other must be a leaf). This forms a linear "path"
    /// structure that guarantees bounded treewidth.
    ///
    /// # Returns
    /// `true` if the tree is a valid path decomposition, `false` otherwise.
    ///
    /// # Example
    /// ```rust
    /// use omeco::{EinCode, NestedEinsum};
    ///
    /// // Valid path decomposition: ((leaf0, leaf1), leaf2)
    /// let leaf0 = NestedEinsum::<char>::leaf(0);
    /// let leaf1 = NestedEinsum::<char>::leaf(1);
    /// let leaf2 = NestedEinsum::<char>::leaf(2);
    ///
    /// let eins1 = EinCode::new(vec![vec!['i', 'j'], vec!['j', 'k']], vec!['i', 'k']);
    /// let node1 = NestedEinsum::node(vec![leaf0, leaf1], eins1);
    ///
    /// let eins2 = EinCode::new(vec![vec!['i', 'k'], vec!['k', 'l']], vec!['i', 'l']);
    /// let path_tree = NestedEinsum::node(vec![node1, leaf2], eins2);
    ///
    /// assert!(path_tree.is_path_decomposition());
    /// ```
    pub fn is_path_decomposition(&self) -> bool {
        match self {
            Self::Leaf { .. } => true,
            Self::Node { args, .. } => {
                if args.len() != 2 {
                    // Path decomposition requires binary nodes
                    return false;
                }

                // Count internal children (non-leaf nodes)
                let internal_children = args.iter().filter(|arg| !arg.is_leaf()).count();

                if internal_children > 1 {
                    // At most one internal child allowed
                    return false;
                }

                // Recursively check all children
                args.iter().all(|arg| arg.is_path_decomposition())
            }
        }
    }
}

/// An einsum with sliced indices for reduced space complexity.
///
/// Slicing trades space for time: instead of computing the full contraction,
/// we iterate over the sliced indices and accumulate partial results.
///
/// # Example
/// ```
/// use omeco::{EinCode, NestedEinsum, SlicedEinsum};
///
/// let leaf0 = NestedEinsum::<char>::leaf(0);
/// let leaf1 = NestedEinsum::<char>::leaf(1);
/// let eins = EinCode::new(
///     vec![vec!['i', 'j'], vec!['j', 'k']],
///     vec!['i', 'k']
/// );
/// let tree = NestedEinsum::node(vec![leaf0, leaf1], eins);
///
/// // Slice over index 'j' to reduce intermediate tensor size
/// let sliced = SlicedEinsum::new(vec!['j'], tree);
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SlicedEinsum<L: Label> {
    /// Indices to slice over (iterate in outer loop)
    pub slicing: Vec<L>,
    /// The nested einsum to execute for each slice
    pub eins: NestedEinsum<L>,
}

impl<L: Label> SlicedEinsum<L> {
    /// Create a new sliced einsum.
    pub fn new(slicing: Vec<L>, eins: NestedEinsum<L>) -> Self {
        Self { slicing, eins }
    }

    /// Create a sliced einsum with no slicing (equivalent to the original).
    pub fn unsliced(eins: NestedEinsum<L>) -> Self {
        Self {
            slicing: Vec::new(),
            eins,
        }
    }

    /// Check if any slicing is applied.
    #[inline]
    pub fn is_sliced(&self) -> bool {
        !self.slicing.is_empty()
    }

    /// Get the number of sliced indices.
    #[inline]
    pub fn num_slices(&self) -> usize {
        self.slicing.len()
    }
}

/// Helper to create a size dictionary with uniform sizes.
///
/// # Example
/// ```
/// use omeco::{EinCode, uniform_size_dict};
///
/// let code = EinCode::new(
///     vec![vec!['i', 'j'], vec!['j', 'k']],
///     vec!['i', 'k']
/// );
/// let sizes = uniform_size_dict(&code, 10);
/// assert_eq!(sizes.get(&'i'), Some(&10));
/// ```
pub fn uniform_size_dict<L: Label>(code: &EinCode<L>, size: usize) -> HashMap<L, usize> {
    code.unique_labels()
        .into_iter()
        .map(|l| (l, size))
        .collect()
}

/// Convert a size dictionary to log2 sizes.
pub fn log2_size_dict<L: Label>(size_dict: &HashMap<L, usize>) -> HashMap<L, f64> {
    size_dict
        .iter()
        .map(|(k, &v)| (k.clone(), (v as f64).log2()))
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_eincode_new() {
        let code: EinCode<char> =
            EinCode::new(vec![vec!['i', 'j'], vec!['j', 'k']], vec!['i', 'k']);
        assert_eq!(code.num_tensors(), 2);
        assert!(code.is_valid());
    }

    #[test]
    fn test_eincode_trace() {
        let code: EinCode<char> = EinCode::trace(vec![vec!['i', 'j'], vec!['j', 'i']]);
        assert_eq!(code.num_tensors(), 2);
        assert!(code.iy.is_empty());
    }

    #[test]
    fn test_eincode_unique_labels() {
        let code: EinCode<char> =
            EinCode::new(vec![vec!['i', 'j'], vec!['j', 'k']], vec!['i', 'k']);
        let labels = code.unique_labels();
        assert_eq!(labels.len(), 3);
        assert!(labels.contains(&'i'));
        assert!(labels.contains(&'j'));
        assert!(labels.contains(&'k'));
    }

    #[test]
    fn test_eincode_unique_labels_with_output_only() {
        // Output label not in inputs (edge case)
        let code: EinCode<char> = EinCode::new(vec![vec!['i', 'j']], vec!['i', 'j', 'k']);
        let labels = code.unique_labels();
        assert_eq!(labels.len(), 3);
        assert!(labels.contains(&'k'));
    }

    #[test]
    fn test_eincode_contracted_indices() {
        let code: EinCode<char> =
            EinCode::new(vec![vec!['i', 'j'], vec!['j', 'k']], vec!['i', 'k']);
        let contracted = code.contracted_indices();
        assert_eq!(contracted, vec!['j']);
    }

    #[test]
    fn test_eincode_contracted_indices_none() {
        // All indices are kept
        let code: EinCode<char> = EinCode::new(
            vec![vec!['i', 'j'], vec!['k', 'l']],
            vec!['i', 'j', 'k', 'l'],
        );
        let contracted = code.contracted_indices();
        assert!(contracted.is_empty());
    }

    #[test]
    fn test_eincode_output_indices() {
        let code: EinCode<char> =
            EinCode::new(vec![vec!['i', 'j'], vec!['j', 'k']], vec!['i', 'k']);
        assert_eq!(code.output_indices(), &['i', 'k']);
    }

    #[test]
    fn test_eincode_input_indices() {
        let code: EinCode<char> =
            EinCode::new(vec![vec!['i', 'j'], vec!['j', 'k']], vec!['i', 'k']);
        assert_eq!(code.input_indices(0), Some(&['i', 'j'][..]));
        assert_eq!(code.input_indices(1), Some(&['j', 'k'][..]));
        assert_eq!(code.input_indices(99), None); // Invalid index
    }

    #[test]
    fn test_eincode_is_valid() {
        let valid_code: EinCode<char> =
            EinCode::new(vec![vec!['i', 'j'], vec!['j', 'k']], vec!['i', 'k']);
        assert!(valid_code.is_valid());

        // Invalid: output contains index not in inputs
        let invalid_code: EinCode<char> = EinCode::new(vec![vec!['i', 'j']], vec!['i', 'k']);
        assert!(!invalid_code.is_valid());
    }

    #[test]
    fn test_eincode_display() {
        let code: EinCode<char> =
            EinCode::new(vec![vec!['i', 'j'], vec!['j', 'k']], vec!['i', 'k']);
        assert_eq!(format!("{}", code), "ij,jk->ik");
    }

    #[test]
    fn test_eincode_display_empty_output() {
        let code: EinCode<char> = EinCode::trace(vec![vec!['i', 'j'], vec!['j', 'i']]);
        assert_eq!(format!("{}", code), "ij,ji->");
    }

    #[test]
    fn test_nested_einsum_leaf() {
        let leaf: NestedEinsum<char> = NestedEinsum::leaf(0);
        assert!(leaf.is_leaf());
        assert_eq!(leaf.tensor_index(), Some(0));
        assert!(leaf.is_binary());
    }

    #[test]
    fn test_nested_einsum_node() {
        let leaf0: NestedEinsum<char> = NestedEinsum::leaf(0);
        let leaf1: NestedEinsum<char> = NestedEinsum::leaf(1);
        let eins = EinCode::new(vec![vec!['i', 'j'], vec!['j', 'k']], vec!['i', 'k']);
        let tree = NestedEinsum::node(vec![leaf0, leaf1], eins);

        assert!(!tree.is_leaf());
        assert!(tree.is_binary());
        assert_eq!(tree.leaf_count(), 2);
        assert_eq!(tree.leaf_indices(), vec![0, 1]);
    }

    #[test]
    fn test_nested_einsum_tensor_index_on_node() {
        let leaf0: NestedEinsum<char> = NestedEinsum::leaf(0);
        let leaf1: NestedEinsum<char> = NestedEinsum::leaf(1);
        let eins = EinCode::new(vec![vec!['i', 'j'], vec!['j', 'k']], vec!['i', 'k']);
        let tree = NestedEinsum::node(vec![leaf0, leaf1], eins);

        // Node should return None for tensor_index
        assert_eq!(tree.tensor_index(), None);
    }

    #[test]
    fn test_nested_einsum_node_count() {
        let leaf0: NestedEinsum<char> = NestedEinsum::leaf(0);
        let leaf1: NestedEinsum<char> = NestedEinsum::leaf(1);
        let leaf2: NestedEinsum<char> = NestedEinsum::leaf(2);

        let eins1 = EinCode::new(vec![vec!['i', 'j'], vec!['j', 'k']], vec!['i', 'k']);
        let tree1 = NestedEinsum::node(vec![leaf0, leaf1], eins1);

        let eins2 = EinCode::new(vec![vec!['i', 'k'], vec!['k', 'l']], vec!['i', 'l']);
        let tree2 = NestedEinsum::node(vec![tree1, leaf2], eins2);

        // node_count: 2 internal nodes + 3 leaves = 5
        assert_eq!(tree2.node_count(), 5);
    }

    #[test]
    fn test_nested_einsum_output_labels_leaf() {
        let leaf: NestedEinsum<char> = NestedEinsum::leaf(0);
        let input_labels = vec![vec!['i', 'j'], vec!['j', 'k']];

        let output = leaf.output_labels(&input_labels);
        assert_eq!(output, vec!['i', 'j']);
    }

    #[test]
    fn test_nested_einsum_output_labels_node() {
        let leaf0: NestedEinsum<char> = NestedEinsum::leaf(0);
        let leaf1: NestedEinsum<char> = NestedEinsum::leaf(1);
        let eins = EinCode::new(vec![vec!['i', 'j'], vec!['j', 'k']], vec!['i', 'k']);
        let tree = NestedEinsum::node(vec![leaf0, leaf1], eins);

        let input_labels = vec![vec!['i', 'j'], vec!['j', 'k']];
        let output = tree.output_labels(&input_labels);
        assert_eq!(output, vec!['i', 'k']);
    }

    #[test]
    fn test_nested_einsum_output_labels_invalid_index() {
        let leaf: NestedEinsum<char> = NestedEinsum::leaf(99); // Invalid index
        let input_labels = vec![vec!['i', 'j']];

        let output = leaf.output_labels(&input_labels);
        assert!(output.is_empty()); // Should return empty for invalid index
    }

    #[test]
    fn test_nested_einsum_is_binary_non_binary() {
        let leaf0: NestedEinsum<char> = NestedEinsum::leaf(0);
        let leaf1: NestedEinsum<char> = NestedEinsum::leaf(1);
        let leaf2: NestedEinsum<char> = NestedEinsum::leaf(2);

        // Non-binary: 3 children
        let eins = EinCode::new(
            vec![vec!['i', 'j'], vec!['j', 'k'], vec!['k', 'l']],
            vec!['i', 'l'],
        );
        let tree = NestedEinsum::node(vec![leaf0, leaf1, leaf2], eins);

        assert!(!tree.is_binary());
    }

    #[test]
    fn test_nested_einsum_depth() {
        let leaf0: NestedEinsum<char> = NestedEinsum::leaf(0);
        let leaf1: NestedEinsum<char> = NestedEinsum::leaf(1);
        let leaf2: NestedEinsum<char> = NestedEinsum::leaf(2);

        let eins1 = EinCode::new(vec![vec!['i', 'j'], vec!['j', 'k']], vec!['i', 'k']);
        let tree1 = NestedEinsum::node(vec![leaf0, leaf1], eins1);

        let eins2 = EinCode::new(vec![vec!['i', 'k'], vec!['k', 'l']], vec!['i', 'l']);
        let tree2 = NestedEinsum::node(vec![tree1, leaf2], eins2);

        assert_eq!(tree2.depth(), 2);
        assert_eq!(tree2.leaf_count(), 3);
    }

    #[test]
    fn test_nested_einsum_depth_leaf() {
        let leaf: NestedEinsum<char> = NestedEinsum::leaf(0);
        assert_eq!(leaf.depth(), 0);
    }

    #[test]
    fn test_sliced_einsum() {
        let leaf0: NestedEinsum<char> = NestedEinsum::leaf(0);
        let sliced = SlicedEinsum::new(vec!['j'], leaf0);

        assert!(sliced.is_sliced());
        assert_eq!(sliced.num_slices(), 1);
    }

    #[test]
    fn test_sliced_einsum_unsliced() {
        let leaf: NestedEinsum<char> = NestedEinsum::leaf(0);
        let sliced = SlicedEinsum::unsliced(leaf);

        assert!(!sliced.is_sliced());
        assert_eq!(sliced.num_slices(), 0);
    }

    #[test]
    fn test_uniform_size_dict() {
        let code: EinCode<char> =
            EinCode::new(vec![vec!['i', 'j'], vec!['j', 'k']], vec!['i', 'k']);
        let sizes = uniform_size_dict(&code, 10);
        assert_eq!(sizes.len(), 3);
        assert_eq!(sizes.get(&'i'), Some(&10));
        assert_eq!(sizes.get(&'j'), Some(&10));
        assert_eq!(sizes.get(&'k'), Some(&10));
    }

    #[test]
    fn test_log2_size_dict() {
        let mut size_dict = HashMap::new();
        size_dict.insert('i', 4);
        size_dict.insert('j', 8);
        size_dict.insert('k', 16);

        let log2_sizes = log2_size_dict(&size_dict);

        assert!((log2_sizes.get(&'i').unwrap() - 2.0).abs() < 1e-10);
        assert!((log2_sizes.get(&'j').unwrap() - 3.0).abs() < 1e-10);
        assert!((log2_sizes.get(&'k').unwrap() - 4.0).abs() < 1e-10);
    }

    #[test]
    fn test_eincode_serialization() {
        let code: EinCode<char> =
            EinCode::new(vec![vec!['i', 'j'], vec!['j', 'k']], vec!['i', 'k']);

        // Test serialization/deserialization
        let json = serde_json::to_string(&code).unwrap();
        let decoded: EinCode<char> = serde_json::from_str(&json).unwrap();

        assert_eq!(code, decoded);
    }

    #[test]
    fn test_nested_einsum_serialization() {
        let leaf0: NestedEinsum<char> = NestedEinsum::leaf(0);
        let leaf1: NestedEinsum<char> = NestedEinsum::leaf(1);
        let eins = EinCode::new(vec![vec!['i', 'j'], vec!['j', 'k']], vec!['i', 'k']);
        let tree = NestedEinsum::node(vec![leaf0, leaf1], eins);

        let json = serde_json::to_string(&tree).unwrap();
        let decoded: NestedEinsum<char> = serde_json::from_str(&json).unwrap();

        assert_eq!(tree, decoded);
    }

    #[test]
    fn test_sliced_einsum_serialization() {
        let leaf: NestedEinsum<char> = NestedEinsum::leaf(0);
        let sliced = SlicedEinsum::new(vec!['j'], leaf);

        let json = serde_json::to_string(&sliced).unwrap();
        let decoded: SlicedEinsum<char> = serde_json::from_str(&json).unwrap();

        assert_eq!(sliced, decoded);
    }

    #[test]
    fn test_nested_einsum_depth_no_children() {
        // Test depth when a node has no args (edge case)
        let leaf0: NestedEinsum<char> = NestedEinsum::leaf(0);
        let leaf1: NestedEinsum<char> = NestedEinsum::leaf(1);
        let eins = EinCode::new(vec![vec!['i', 'j'], vec!['j', 'k']], vec!['i', 'k']);
        let tree = NestedEinsum::node(vec![leaf0, leaf1], eins);

        // depth of (leaf, leaf) should be 1
        assert_eq!(tree.depth(), 1);
    }

    #[test]
    fn test_nested_einsum_to_eincode_simple() {
        // Test converting a simple binary tree back to flat EinCode
        let original_ixs = vec![vec!['i', 'j'], vec!['j', 'k']];
        let original_output = vec!['i', 'k'];

        let leaf0 = NestedEinsum::leaf(0);
        let leaf1 = NestedEinsum::leaf(1);
        let eins = EinCode::new(vec![vec!['i', 'j'], vec!['j', 'k']], vec!['i', 'k']);
        let nested = NestedEinsum::node(vec![leaf0, leaf1], eins);

        let flat = nested.to_eincode(&original_ixs);

        assert_eq!(flat.ixs, original_ixs);
        assert_eq!(flat.iy, original_output);
    }

    #[test]
    fn test_nested_einsum_to_eincode_chain() {
        // Test with a chain: A*B, then *C
        let original_ixs = vec![vec!['i', 'j'], vec!['j', 'k'], vec!['k', 'l']];
        let original_output = vec!['i', 'l'];

        // Build tree: ((0, 1), 2)
        let leaf0 = NestedEinsum::leaf(0);
        let leaf1 = NestedEinsum::leaf(1);
        let leaf2 = NestedEinsum::leaf(2);

        let eins01 = EinCode::new(vec![vec!['i', 'j'], vec!['j', 'k']], vec!['i', 'k']);
        let node01 = NestedEinsum::node(vec![leaf0, leaf1], eins01);

        let eins_final = EinCode::new(vec![vec!['i', 'k'], vec!['k', 'l']], vec!['i', 'l']);
        let nested = NestedEinsum::node(vec![node01, leaf2], eins_final);

        let flat = nested.to_eincode(&original_ixs);

        assert_eq!(flat.ixs, original_ixs);
        assert_eq!(flat.iy, original_output);
    }

    #[test]
    fn test_nested_einsum_to_eincode_leaf() {
        // Test that a single leaf returns just that tensor
        let original_ixs = vec![vec!['i', 'j']];
        let leaf = NestedEinsum::<char>::leaf(0);

        let flat = leaf.to_eincode(&original_ixs);

        assert_eq!(flat.ixs, vec![vec!['i', 'j']]);
        assert_eq!(flat.iy, vec!['i', 'j']);
    }

    #[test]
    fn test_nested_einsum_to_eincode_roundtrip() {
        // Test that optimize -> to_eincode preserves the computation
        use crate::greedy::{optimize_greedy, GreedyMethod};
        use crate::uniform_size_dict;

        let original_ixs = vec![vec!['i', 'j'], vec!['j', 'k'], vec!['k', 'l']];
        let original_output = vec!['i', 'l'];
        let code = EinCode::new(original_ixs.clone(), original_output.clone());

        let sizes = uniform_size_dict(&code, 4);
        let nested = optimize_greedy(&code, &sizes, &GreedyMethod::default()).unwrap();

        let flat = nested.to_eincode(&original_ixs);

        // Should have same inputs and outputs (though potentially reordered)
        assert_eq!(flat.ixs.len(), original_ixs.len());

        // Output should contain the same labels (order might differ due to optimization)
        assert_eq!(flat.iy.len(), original_output.len());
        for &label in &original_output {
            assert!(flat.iy.contains(&label), "Missing output label {:?}", label);
        }

        // All original tensors should appear in the flattened version
        for original_ix in &original_ixs {
            assert!(
                flat.ixs.contains(original_ix),
                "Missing tensor {:?}",
                original_ix
            );
        }
    }

    #[test]
    fn test_is_path_decomposition_leaf() {
        // A single leaf is a valid path decomposition
        let leaf = NestedEinsum::<char>::leaf(0);
        assert!(leaf.is_path_decomposition());
    }

    #[test]
    fn test_is_path_decomposition_two_leaves() {
        // Two leaves contracted is a valid path decomposition
        let leaf0 = NestedEinsum::<char>::leaf(0);
        let leaf1 = NestedEinsum::<char>::leaf(1);
        let eins = EinCode::new(vec![vec!['i', 'j'], vec!['j', 'k']], vec!['i', 'k']);
        let tree = NestedEinsum::node(vec![leaf0, leaf1], eins);

        assert!(tree.is_path_decomposition());
    }

    #[test]
    fn test_is_path_decomposition_valid_path() {
        // Valid path: ((leaf0, leaf1), leaf2) - one internal child, one leaf
        let leaf0 = NestedEinsum::<char>::leaf(0);
        let leaf1 = NestedEinsum::<char>::leaf(1);
        let leaf2 = NestedEinsum::<char>::leaf(2);

        let eins1 = EinCode::new(vec![vec!['i', 'j'], vec!['j', 'k']], vec!['i', 'k']);
        let node1 = NestedEinsum::node(vec![leaf0, leaf1], eins1);

        let eins2 = EinCode::new(vec![vec!['i', 'k'], vec!['k', 'l']], vec!['i', 'l']);
        let path_tree = NestedEinsum::node(vec![node1, leaf2], eins2);

        assert!(path_tree.is_path_decomposition());
    }

    #[test]
    fn test_is_path_decomposition_long_path() {
        // Longer path: (((leaf0, leaf1), leaf2), leaf3)
        let leaf0 = NestedEinsum::<char>::leaf(0);
        let leaf1 = NestedEinsum::<char>::leaf(1);
        let leaf2 = NestedEinsum::<char>::leaf(2);
        let leaf3 = NestedEinsum::<char>::leaf(3);

        let eins1 = EinCode::new(vec![vec!['a', 'b'], vec!['b', 'c']], vec!['a', 'c']);
        let node1 = NestedEinsum::node(vec![leaf0, leaf1], eins1);

        let eins2 = EinCode::new(vec![vec!['a', 'c'], vec!['c', 'd']], vec!['a', 'd']);
        let node2 = NestedEinsum::node(vec![node1, leaf2], eins2);

        let eins3 = EinCode::new(vec![vec!['a', 'd'], vec!['d', 'e']], vec!['a', 'e']);
        let path_tree = NestedEinsum::node(vec![node2, leaf3], eins3);

        assert!(path_tree.is_path_decomposition());
    }

    #[test]
    fn test_is_path_decomposition_invalid_two_internal_children() {
        // Invalid: ((leaf0, leaf1), (leaf2, leaf3)) - both children are internal
        let leaf0 = NestedEinsum::<char>::leaf(0);
        let leaf1 = NestedEinsum::<char>::leaf(1);
        let leaf2 = NestedEinsum::<char>::leaf(2);
        let leaf3 = NestedEinsum::<char>::leaf(3);

        let eins1 = EinCode::new(vec![vec!['i', 'j'], vec!['j', 'k']], vec!['i', 'k']);
        let node1 = NestedEinsum::node(vec![leaf0, leaf1], eins1);

        let eins2 = EinCode::new(vec![vec!['k', 'l'], vec!['l', 'm']], vec!['k', 'm']);
        let node2 = NestedEinsum::node(vec![leaf2, leaf3], eins2);

        let eins3 = EinCode::new(vec![vec!['i', 'k'], vec!['k', 'm']], vec!['i', 'm']);
        let invalid_tree = NestedEinsum::node(vec![node1, node2], eins3);

        assert!(!invalid_tree.is_path_decomposition());
    }

    #[test]
    fn test_is_path_decomposition_greedy_vs_treesa_path() {
        // Test that greedy doesn't guarantee path decomposition, but TreeSA with PathDecomp does
        use crate::expr_tree::DecompositionType;
        use crate::greedy::{optimize_greedy, GreedyMethod};
        use crate::treesa::{optimize_treesa, Initializer, TreeSA};
        use crate::uniform_size_dict;

        let code = EinCode::new(
            vec![vec!['i', 'j'], vec!['j', 'k'], vec!['k', 'l']],
            vec!['i', 'l'],
        );
        let sizes = uniform_size_dict(&code, 4);

        // Greedy doesn't guarantee path decomposition
        let _greedy_result = optimize_greedy(&code, &sizes, &GreedyMethod::default()).unwrap();
        // May or may not be path decomposition depending on greedy choices

        // TreeSA with Path decomposition should produce a valid path
        let treesa_config = TreeSA {
            ntrials: 1,
            niters: 10,
            betas: vec![1.0, 2.0],
            initializer: Initializer::Greedy,
            decomposition_type: DecompositionType::Path,
            score: Default::default(),
        };
        let treesa_result = optimize_treesa(&code, &sizes, &treesa_config).unwrap();

        // TreeSA with PathDecomp should always produce a path decomposition
        assert!(
            treesa_result.is_path_decomposition(),
            "TreeSA with PathDecomp should produce a valid path decomposition"
        );
    }

    #[test]
    fn test_is_path_decomposition_sliced_einsum() {
        // Test that SlicedEinsum preserves path decomposition property
        use crate::SlicedEinsum;

        let leaf0 = NestedEinsum::<char>::leaf(0);
        let leaf1 = NestedEinsum::<char>::leaf(1);
        let leaf2 = NestedEinsum::<char>::leaf(2);

        let eins1 = EinCode::new(vec![vec!['i', 'j'], vec!['j', 'k']], vec!['i', 'k']);
        let node1 = NestedEinsum::node(vec![leaf0, leaf1], eins1);

        let eins2 = EinCode::new(vec![vec!['i', 'k'], vec!['k', 'l']], vec!['i', 'l']);
        let path_tree = NestedEinsum::node(vec![node1, leaf2], eins2);

        // Create a sliced einsum
        let sliced = SlicedEinsum::new(vec!['i'], path_tree.clone());

        // The underlying einsum should still be a path decomposition
        assert!(sliced.eins.is_path_decomposition());
    }
}