opt-einsum-path 0.2.0

Einsum path optimization for tensor contraction (opt_einsum in Rust, without contraction)
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
// src/paths/dp.rs
use crate::*;
use std::collections::VecDeque;

// Define our tree type
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ContractionTree {
    Leaf(usize),
    Node(Vec<ContractionTree>),
}

impl From<usize> for ContractionTree {
    fn from(value: usize) -> Self {
        ContractionTree::Leaf(value)
    }
}

impl From<Vec<ContractionTree>> for ContractionTree {
    fn from(value: Vec<ContractionTree>) -> Self {
        ContractionTree::Node(value)
    }
}

impl From<Vec<usize>> for ContractionTree {
    fn from(value: Vec<usize>) -> Self {
        ContractionTree::Node(value.into_iter().map(ContractionTree::Leaf).collect())
    }
}

/// Converts a contraction tree to a contraction path.
///
/// A contraction tree can either be a leaf node containing an integer (representingno contraction)
/// or a node containing a sequence of subtrees to be contracted.Contractions are commutative
/// (order-independent) and solutions are not unique.
///
/// # Parameters
///
/// * `tree` - The contraction tree to convert, represented as a `ContractionTree` enum where leaves
///   are integers and nodes contain sequences of subtrees.
///
/// # Returns
///
/// A [`PathType`] (`Vec<Vec<usize>>`) representing the contraction path, where each inner
/// `Vec<usize>` represents a single contraction step with the indices of tensors to contract.
///
/// The conversion process works by:
/// 1. Processing leaf nodes (integers) first to determine their positions
/// 2. Building the contraction sequence by tracking elementary tensors and remaining contractions
/// 3. Maintaining proper index accounting throughout the conversion
///
/// Note: This implementation matches the behavior of Python's `_tree_to_sequence` function
/// from opt_einsum, producing equivalent output for equivalent input trees.
pub fn tree_to_sequence(tree: &ContractionTree) -> PathType {
    // Handle leaf case (equivalent to Python's int case)
    if let ContractionTree::Leaf(_) = tree {
        return Vec::new();
    }

    let mut c: VecDeque<&ContractionTree> = VecDeque::new(); // list of remaining contractions
    c.push_back(tree);

    let mut t: Vec<usize> = Vec::new(); // list of elementary tensors
    let mut s: VecDeque<Vec<usize>> = VecDeque::new(); // resulting contraction sequence

    while !c.is_empty() {
        let j = c.pop_back().unwrap();
        s.push_front(Vec::new());

        // First process the integer leaves
        if let ContractionTree::Node(children) = j {
            // Collect integer leaves first
            let mut int_children: Vec<usize> = children
                .iter()
                .filter_map(|child| match child {
                    ContractionTree::Leaf(i) => Some(*i),
                    _ => None,
                })
                .collect();

            // Sort them as in Python
            int_children.sort_unstable();

            for i in int_children {
                // Calculate the position as in Python: sum(1 for q in t if q < i)
                let pos = t.iter().filter(|&&q| q < i).count();
                s[0].push(pos);
                t.insert(pos, i);
            }

            // Then process the non-integer children (other nodes)
            for i_tup in children.iter().filter(|child| matches!(child, ContractionTree::Node(_))) {
                let pos = t.len() + c.len();
                s[0].push(pos);
                c.push_back(i_tup);
            }
        }
    }

    s.into_iter().collect()
}

/// Finds disconnected subgraphs in a list of input tensor dimensions.
///
/// Input tensors are considered connected if they share summation indices (indices not
/// present in the output). Disconnected subgraphs can be contracted independently
/// before forming outer products, which is useful for optimization.
///
/// # Parameters
///
/// * `inputs` - Slice of sets representing input tensor dimensions (lhs of einsum)
/// * `output` - Set representing output tensor dimensions (rhs of einsum)
///
/// # Returns
///
/// Vector of sets where each set contains indices of connected input tensors.
///
/// # Note
///
/// - Summation indices are determined as `(union of all inputs) \ output`
/// - The order of returned subgraphs is implementation-defined
/// - Within each subgraph, the order of tensor indices is sorted
pub fn find_disconnected_subgraphs(inputs: &[ArrayIndexType], output: &ArrayIndexType) -> Vec<BTreeSet<usize>> {
    let mut subgraphs = Vec::new();
    let mut unused_inputs: BTreeSet<usize> = (0..inputs.len()).collect();

    // Calculate all summation indices (union of all inputs minus output)
    let input_indices: ArrayIndexType = inputs.iter().flat_map(|set| set.iter()).cloned().collect();
    let i_sum = &input_indices - output;

    while !unused_inputs.is_empty() {
        let mut g = BTreeSet::new();
        let mut queue = VecDeque::new();

        // Start with any remaining input
        queue.push_back(*unused_inputs.iter().next().unwrap());
        unused_inputs.remove(&queue[0]);

        while !queue.is_empty() {
            let j = queue.pop_front().unwrap();
            g.insert(j);

            // Get summation indices for current input
            let i_tmp: ArrayIndexType = &i_sum & &inputs[j];

            // Find connected inputs
            let neighbors = unused_inputs.iter().filter(|&&k| !inputs[k].is_disjoint(&i_tmp)).cloned().collect_vec();

            for neighbor in neighbors {
                queue.push_back(neighbor);
                unused_inputs.remove(&neighbor);
            }
        }
        subgraphs.push(g);
    }
    subgraphs
}

/// Select elements of `seq` which are marked by the bitmap `s`.
///
/// # Parameters
///
/// * `s` - Bitmap where each bit represents whether to select the corresponding element
/// * `seq` - Sequence of items to select from
///
/// # Returns
///
/// An iterator yielding selected elements from `seq` where the corresponding bit in `s` is set.
pub fn bitmap_select<'t, T>(s: &'t BigUint, seq: &'t [T]) -> impl Iterator<Item = &'t T> {
    let uint_1 = BigUint::from_u32(1).unwrap();
    seq.iter().enumerate().filter(move |(i, _)| (s >> i) & &uint_1 == uint_1).map(move |(_, x)| x)
}

// Calculates the effective outer indices of the intermediate tensor
/// corresponding to the subgraph `s`.
///
/// # Parameters
///
/// * `g` - Bitmap representing all tensors in the current graph
/// * `all_tensors` - Bitmap representing all possible tensors
/// * `s` - Bitmap representing the subgraph to calculate legs for
/// * `inputs` - Slice of input tensor dimension sets
/// * `i1_cut_i2_wo_output` - Precomputed intersection of indices (i1 ∩ i2) \ output
/// * `i1_union_i2` - Precomputed union of indices (i1 ∪ i2)
///
/// # Returns
///
/// The effective outer indices of the intermediate tensor
pub fn dp_calc_legs(
    g: &BigUint,
    all_tensors: &BigUint,
    s: &BigUint,
    inputs: &[&ArrayIndexType],
    i1_cut_i2_wo_output: &ArrayIndexType,
    i1_union_i2: &ArrayIndexType,
) -> ArrayIndexType {
    // set of remaining tensors (= g & (!s))
    let r = g & (all_tensors ^ s);

    // indices of remaining indices:
    let i_r = if r != BigUint::ZERO {
        bitmap_select(&r, inputs).flat_map(|x| x.iter()).collect_vec().into_iter().copied().collect()
    } else {
        ArrayIndexType::new()
    };

    // contraction indices:
    let i_contract = i1_cut_i2_wo_output - &i_r;
    i1_union_i2 - &i_contract
}

#[derive(Debug, Clone)]
pub struct DpTerm {
    pub indices: ArrayIndexType,
    pub cost: SizeType,
    pub contract: ContractionTree,
}

pub struct DpCompareArgs<'a> {
    // parameters
    pub minimize: &'a str,
    pub combo_factor: SizeType,
    // inputs
    pub inputs: &'a [&'a ArrayIndexType],
    pub size_dict: &'a SizeDictType,
    pub all_tensors: BigUint,
    pub memory_limit: Option<SizeType>,
    pub cost_cap: SizeType,
    pub bitmap_g: BigUint,
}

impl<'a> DpCompareArgs<'a> {
    /// Performs the inner comparison of whether the two subgraphs (the bitmaps `s1` and `s2`)
    /// should be merged and added to the dynamic programming search. Will skip for a number of
    /// reasons:
    /// 1. If the number of operations to form `s = s1 | s2` including previous contractions is
    ///    above the cost-cap.
    /// 2. If we've already found a better way of making `s`.
    /// 3. If the intermediate tensor corresponding to `s` is going to break the memory limit.
    pub fn compare_flops(
        &self,
        xn: &mut BTreeMap<BigUint, DpTerm>,
        s1: &BigUint,
        s2: &BigUint,
        term1: &DpTerm,
        term2: &DpTerm,
        i1_cut_i2_wo_output: &ArrayIndexType,
    ) {
        let DpTerm { indices: i1, cost: cost1, contract: contract1 } = term1;
        let DpTerm { indices: i2, cost: cost2, contract: contract2 } = term2;
        let i1_union_i2 = i1 | i2;

        let cost = cost1 + cost2 + helpers::compute_size_by_dict(i1_union_i2.iter(), self.size_dict);
        if cost <= self.cost_cap {
            let s = s1 | s2;
            if xn.get(&s).is_none_or(|term| cost < term.cost) {
                let indices =
                    dp_calc_legs(&self.bitmap_g, &self.all_tensors, &s, self.inputs, i1_cut_i2_wo_output, &i1_union_i2);
                let mem = helpers::compute_size_by_dict(indices.iter(), self.size_dict);
                if self.memory_limit.is_none_or(|limit| mem <= limit) {
                    let contract = vec![contract1.clone(), contract2.clone()].into();
                    xn.insert(s, DpTerm { indices, cost, contract });
                }
            }
        }
    }

    /// Like `compare_flops` but sieves the potential contraction based on the size of the
    /// intermediate tensor created, rather than the number of operations, and so calculates that
    /// first.
    pub fn compare_size(
        &self,
        xn: &mut BTreeMap<BigUint, DpTerm>,
        s1: &BigUint,
        s2: &BigUint,
        term1: &DpTerm,
        term2: &DpTerm,
        i1_cut_i2_wo_output: &ArrayIndexType,
    ) {
        let DpTerm { indices: i1, cost: cost1, contract: contract1 } = term1;
        let DpTerm { indices: i2, cost: cost2, contract: contract2 } = term2;
        let i1_union_i2 = i1 | i2;
        let s = s1 | s2;
        let indices =
            dp_calc_legs(&self.bitmap_g, &self.all_tensors, &s, self.inputs, i1_cut_i2_wo_output, &i1_union_i2);

        let mem = helpers::compute_size_by_dict(indices.iter(), self.size_dict);
        let cost = (*cost1).max(*cost2).max(mem);
        if cost <= self.cost_cap
            && xn.get(&s).is_none_or(|term| cost < term.cost)
            && self.memory_limit.is_none_or(|limit| mem <= limit)
        {
            let contract = vec![contract1.clone(), contract2.clone()].into();
            xn.insert(s, DpTerm { indices, cost, contract });
        }
    }
    /// Like `compare_flops` but sieves the potential contraction based on the total size of memory
    /// created, rather than the number of operations, and so calculates that first.
    pub fn compare_write(
        &self,
        xn: &mut BTreeMap<BigUint, DpTerm>,
        s1: &BigUint,
        s2: &BigUint,
        term1: &DpTerm,
        term2: &DpTerm,
        i1_cut_i2_wo_output: &ArrayIndexType,
    ) {
        let DpTerm { indices: i1, cost: cost1, contract: contract1 } = term1;
        let DpTerm { indices: i2, cost: cost2, contract: contract2 } = term2;
        let i1_union_i2 = i1 | i2;
        let s = s1 | s2;
        let indices =
            dp_calc_legs(&self.bitmap_g, &self.all_tensors, &s, self.inputs, i1_cut_i2_wo_output, &i1_union_i2);

        let mem = helpers::compute_size_by_dict(indices.iter(), self.size_dict);
        let cost = cost1 + cost2 + mem;

        if cost <= self.cost_cap
            && xn.get(&s).is_none_or(|term| cost < term.cost)
            && self.memory_limit.is_none_or(|limit| mem <= limit)
        {
            let contract = vec![contract1.clone(), contract2.clone()].into();
            xn.insert(s, DpTerm { indices, cost, contract });
        }
    }

    /// Like `compare_flops` but sieves the potential contraction based
    /// on some combination of both the flops and size.
    pub fn compare_combo(
        &self,
        xn: &mut BTreeMap<BigUint, DpTerm>,
        s1: &BigUint,
        s2: &BigUint,
        term1: &DpTerm,
        term2: &DpTerm,
        i1_cut_i2_wo_output: &ArrayIndexType,
    ) {
        let DpTerm { indices: i1, cost: cost1, contract: contract1 } = term1;
        let DpTerm { indices: i2, cost: cost2, contract: contract2 } = term2;
        let i1_union_i2 = i1 | i2;
        let s = s1 | s2;
        let indices =
            dp_calc_legs(&self.bitmap_g, &self.all_tensors, &s, self.inputs, i1_cut_i2_wo_output, &i1_union_i2);

        let mem = helpers::compute_size_by_dict(indices.iter(), self.size_dict);
        let f = helpers::compute_size_by_dict(i1_union_i2.iter(), self.size_dict);

        // Hardcoded to sum: f + self.combo_factor * mem
        let combined = match self.minimize {
            "combo" => f + self.combo_factor * mem,
            "limit" => f.max(self.combo_factor * mem),
            _ => panic!("Unknown minimize type for combo mode: {}", self.minimize),
        };
        let cost = cost1 + cost2 + combined;

        if cost <= self.cost_cap
            && xn.get(&s).is_none_or(|term| cost < term.cost)
            && self.memory_limit.is_none_or(|limit| mem <= limit)
        {
            let contract = vec![contract1.clone(), contract2.clone()].into();
            xn.insert(s, DpTerm { indices, cost, contract });
        }
    }

    pub fn scale(&self) -> SizeType {
        get_scale_from_minimize(self.minimize)
    }

    pub fn compare(
        &self,
        xn: &mut BTreeMap<BigUint, DpTerm>,
        s1: &BigUint,
        s2: &BigUint,
        term1: &DpTerm,
        term2: &DpTerm,
        i1_cut_i2_wo_output: &ArrayIndexType,
    ) {
        let minimize_split = self.minimize.split('-').collect_vec();
        if minimize_split.is_empty() {
            panic!("Unknown minimize type: {}", self.minimize);
        }
        match minimize_split[0] {
            "flops" => self.compare_flops(xn, s1, s2, term1, term2, i1_cut_i2_wo_output),
            "size" => self.compare_size(xn, s1, s2, term1, term2, i1_cut_i2_wo_output),
            "write" => self.compare_write(xn, s1, s2, term1, term2, i1_cut_i2_wo_output),
            "combo" | "limit" => self.compare_combo(xn, s1, s2, term1, term2, i1_cut_i2_wo_output),
            _ => panic!("Unknown minimize type: {}", self.minimize),
        }
    }
}

pub fn get_scale_from_minimize(minimize: &str) -> SizeType {
    match minimize {
        "flops" | "size" | "write" => SizeType::one(),
        "combo" | "limit" => SizeType::MAX,
        _ => panic!("Unknown minimize type: {minimize}"),
    }
}

/// Makes a simple left-to-right binary tree out of a sequence of terms.
///
/// # Arguments
/// * `seq` - Sequence of terms to nest
///
/// # Returns
/// A `ContractionTree` representing the left-nested binary tree
pub fn simple_tree_tuple(seq: &[ContractionTree]) -> ContractionTree {
    seq.iter().cloned().reduce(|left, right| ContractionTree::Node(vec![left, right])).unwrap()
}
use std::collections::{BTreeMap, BTreeSet};

/// Parses inputs for single term index operations (indices appearing on one tensor).
///
/// Returns:
/// - Parsed inputs with single indices removed
/// - Inputs that were reduced to scalars
/// - Contractions needed for the reductions
pub fn dp_parse_out_single_term_ops(
    inputs: &[&ArrayIndexType],
    all_inds: &[char],
    ind_counts: &SizeDictType,
) -> (Vec<ArrayIndexType>, Vec<ContractionTree>, Vec<ContractionTree>) {
    let i_single: BTreeSet<char> = all_inds.iter().filter(|&c| ind_counts.get(c) == Some(&1)).cloned().collect();

    let mut inputs_parsed = Vec::new();
    let mut inputs_done = Vec::new();
    let mut inputs_contractions = Vec::new();

    for (j, input) in inputs.iter().enumerate() {
        let i_reduced: ArrayIndexType = *input - &i_single;
        if i_reduced.is_empty() && !input.is_empty() {
            // Input reduced to scalar - remove
            inputs_done.push(vec![j].into());
        } else {
            // Add single contraction if indices were reduced
            inputs_contractions.push(if i_reduced.len() != input.len() { vec![j].into() } else { j.into() });
            inputs_parsed.push(i_reduced);
        }
    }

    (inputs_parsed, inputs_done, inputs_contractions)
}

#[derive(Debug, Clone)]
pub struct DynamicProgramming {
    pub minimize: String,
    pub search_outer: bool,
    pub cost_cap: SizeLimitType,
    pub combo_factor: SizeType,
}

impl Default for DynamicProgramming {
    fn default() -> Self {
        Self {
            minimize: "flops".into(),
            search_outer: false,
            cost_cap: true.into(),
            combo_factor: SizeType::from_usize(64).unwrap(),
        }
    }
}

impl DynamicProgramming {
    pub fn find_optimal_path(
        &self,
        inputs: &[&ArrayIndexType],
        output: &ArrayIndexType,
        size_dict: &SizeDictType,
        memory_limit: Option<SizeType>,
    ) -> Result<PathType, String> {
        let uint_1 = BigUint::from(1u32);
        let uint_0 = BigUint::from(0u32);

        // Count index occurrences
        let ind_counts: BTreeMap<char, usize> =
            inputs.iter().flat_map(|inds| inds.iter()).chain(output.iter()).fold(BTreeMap::new(), |mut counts, &c| {
                *counts.entry(c).or_default() += 1;
                counts
            });

        let all_inds: Vec<char> = ind_counts.keys().copied().collect();

        // Parse single-term operations
        let (inputs, inputs_done, inputs_contractions) = dp_parse_out_single_term_ops(inputs, &all_inds, &ind_counts);
        let inputs_ref = inputs.iter().collect_vec();

        if inputs.is_empty() {
            return Ok(tree_to_sequence(&simple_tree_tuple(&inputs_done)));
        }

        // Initialize subgraph tracking
        let mut subgraph_contractions = inputs_done;
        let mut subgraph_sizes: Vec<SizeType> = vec![SizeType::one(); subgraph_contractions.len()];

        // Find disconnected subgraphs
        let subgraphs = if self.search_outer {
            vec![(0..inputs.len()).collect_vec()]
        } else {
            find_disconnected_subgraphs(&inputs, output).into_iter().map(|s| s.into_iter().collect()).collect()
        };

        let all_tensors = (&uint_1 << inputs.len()) - &uint_1;
        let naive_scale = get_scale_from_minimize(&self.minimize);
        let naive_cost = naive_scale
            * SizeType::from_usize(inputs.len()).unwrap()
            * size_dict.values().map(|v| SizeType::from_usize(*v).unwrap()).product::<SizeType>();

        for g in subgraphs {
            let bitmap_g = g.iter().fold(uint_0.clone(), |acc, &j| acc | (&uint_1 << j));

            // Initialize DP table
            let mut x: Vec<BTreeMap<BigUint, DpTerm>> = vec![BTreeMap::new(); g.len() + 1];
            x[1] = g
                .iter()
                .map(|&j| {
                    (&uint_1 << j, DpTerm {
                        indices: inputs[j].clone(),
                        cost: SizeType::zero(),
                        contract: inputs_contractions[j].clone(),
                    })
                })
                .collect();

            // Initialize cost cap
            let subgraph_inds = bitmap_select(&bitmap_g, &inputs).flat_map(|inds| inds.iter().copied()).collect();

            let mut cost_cap = match self.cost_cap {
                SizeLimitType::Size(cap) => cap,
                SizeLimitType::None => SizeType::MAX,
                SizeLimitType::MaxInput => helpers::compute_size_by_dict((&subgraph_inds & output).iter(), size_dict),
            };

            let cost_increment = if subgraph_inds.is_empty() {
                SizeType::from_usize(2).unwrap()
            } else {
                subgraph_inds
                    .iter()
                    .map(|c| size_dict[c] as SizeType)
                    .fold(SizeType::MAX, SizeType::min)
                    .max(SizeType::from_usize(2).unwrap())
            };

            let mut dp_comp_args = DpCompareArgs {
                inputs: &inputs_ref,
                size_dict,
                all_tensors: all_tensors.clone(),
                memory_limit,
                cost_cap,
                bitmap_g,
                combo_factor: self.combo_factor,
                minimize: &self.minimize,
            };

            fn has_common_bits(s1: &BigUint, s2: &BigUint) -> bool {
                let digits1 = s1.iter_u64_digits();
                let digits2 = s2.iter_u64_digits();
                digits1.zip(digits2).any(|(d1, d2)| d1 & d2 != 0)
            }

            while x.last().unwrap().is_empty() {
                for n in 2..=g.len() {
                    let (xn_left, xn_right) = x.split_at_mut(n);
                    let xn = &mut xn_right[0];
                    for m in 1..=(n / 2) {
                        for (s1, term1) in &xn_left[m] {
                            for (s2, term2) in &xn_left[n - m] {
                                // EFFICIENCY: `s1 & s2 != 0` changes to `!has_common_bits(s1, s2)`
                                if !has_common_bits(s1, s2) && (m != n - m || s1 < s2) {
                                    let i1 = &term1.indices;
                                    let i2 = &term2.indices;
                                    // EFFICIENCY: use iterators instead of `&` and `-` for set operations
                                    // let i1_cut_i2_wo_output = &(i1 & i2) - output;
                                    let i1_cut_i2_wo_output: ArrayIndexType = i1
                                        .iter()
                                        .filter(|&&c| i2.contains(&c) && !output.contains(&c))
                                        .cloned()
                                        .collect();
                                    if self.search_outer || !i1_cut_i2_wo_output.is_empty() {
                                        dp_comp_args.compare(xn, s1, s2, term1, term2, &i1_cut_i2_wo_output);
                                    }
                                }
                            }
                        }
                    }
                }

                // avoid overflow
                cost_cap = match cost_cap >= SizeType::MAX / cost_increment {
                    true => SizeType::MAX,
                    false => cost_cap * cost_increment,
                };
                dp_comp_args.cost_cap = cost_cap;

                if cost_cap > naive_cost && x.last().unwrap().is_empty() {
                    return Err("No contraction found for given memory_limit".into());
                }
            }

            let (_, term) = x.last().unwrap().iter().next().unwrap();
            subgraph_contractions.push(term.contract.clone());
            subgraph_sizes.push(helpers::compute_size_by_dict(term.indices.iter(), size_dict));
        }

        // Sort subgraphs by size
        let sorted_indices =
            (0..subgraph_sizes.len()).sorted_by(|&a, &b| subgraph_sizes[a].partial_cmp(&subgraph_sizes[b]).unwrap());
        let sorted_contractions = sorted_indices.map(|i| subgraph_contractions[i].clone()).collect_vec();

        Ok(tree_to_sequence(&simple_tree_tuple(&sorted_contractions)))
    }
}

impl PathOptimizer for DynamicProgramming {
    fn optimize_path(
        &mut self,
        inputs: &[&ArrayIndexType],
        output: &ArrayIndexType,
        size_dict: &SizeDictType,
        memory_limit: Option<SizeType>,
    ) -> Result<PathType, String> {
        self.find_optimal_path(inputs, output, size_dict, memory_limit)
    }
}

impl From<&str> for DynamicProgramming {
    fn from(s: &str) -> Self {
        let s = s.replace(['_', ' '], "-").to_lowercase();
        if s == "dp" || s == "dynamic-programming" {
            return DynamicProgramming::default();
        }
        if s.starts_with("dp-") {
            let minimize = s.strip_prefix("dp-").unwrap();
            // sanity of minimize
            if minimize.starts_with("combo") || minimize.starts_with("limit") {
                let minimize_split = minimize.split('-').collect_vec();
                if minimize_split.len() > 2 {
                    panic!("Unknown dynamic programming optimizer: {s}");
                }
                match minimize_split.len() {
                    1 => {
                        let minimize = minimize_split[0];
                        if minimize != "combo" && minimize != "limit" {
                            panic!("Unknown dynamic programming optimizer: {s}");
                        }
                        return DynamicProgramming { minimize: minimize.into(), ..Default::default() };
                    },
                    2 => {
                        let minimize = minimize_split[0];
                        if minimize != "combo" && minimize != "limit" {
                            panic!("Unknown dynamic programming optimizer: {s}");
                        }
                        let combo_factor = match minimize_split[1].parse::<SizeType>() {
                            Ok(factor) => factor,
                            Err(_) => panic!("Invalid combo factor in dynamic programming optimizer: {s}"),
                        };
                        return DynamicProgramming { minimize: minimize.into(), combo_factor, ..Default::default() };
                    },
                    _ => panic!("Unknown dynamic programming optimizer: {s}"),
                };
            } else if minimize == "flops" || minimize == "size" || minimize == "write" {
                return DynamicProgramming { minimize: minimize.into(), ..Default::default() };
            } else {
                panic!("Unknown dynamic programming optimizer: {s}");
            }
        }
        panic!("Unknown dynamic programming optimizer: {s}");
    }
}

#[test]
fn test_tree_to_sequence() {
    let tree: ContractionTree = ContractionTree::from(vec![
        ContractionTree::from(vec![1, 2]),
        vec![ContractionTree::from(0), ContractionTree::from(vec![4, 5, 3])].into(),
    ]);

    let path = tree_to_sequence(&tree);
    println!("{path:?}");
    assert_eq!(path, vec![vec![1, 2], vec![1, 2, 3], vec![0, 2], vec![0, 1]]);
}

#[test]
fn test_find_disconnected_subgraphs() {
    use crate::helpers::setify;
    // First test case
    let inputs1 = vec![setify("ab"), setify("c"), setify("ad")];
    let output1 = setify("bd");
    let result1 = find_disconnected_subgraphs(&inputs1, &output1);
    assert_eq!(result1, vec![setify([0, 2]), setify([1])]);

    // Second test case
    let inputs2 = vec![setify("ab"), setify("c"), setify("ad")];
    let output2 = setify("abd");
    let result2 = find_disconnected_subgraphs(&inputs2, &output2);
    assert_eq!(result2, vec![setify([0]), setify([1]), setify([2])]);
}

#[test]
fn test_bitmap_select() {
    use crate::helpers::setify;
    let seq = vec![setify("A"), setify("B"), setify("C"), setify("D"), setify("E")];

    // Test case from Python example
    let s = BigUint::from(0b11010_u32);
    let selected = bitmap_select(&s, &seq).collect_vec();
    assert_eq!(selected, vec![&setify("B"), &setify("D"), &setify("E")]);

    // Additional test cases
    assert_eq!(bitmap_select(&BigUint::from(0b00000_u32), &seq).count(), 0);
    assert_eq!(bitmap_select(&BigUint::from(0b11111_u32), &seq).count(), 5);
    assert_eq!(bitmap_select(&BigUint::from(0b00001_u32), &seq).collect_vec(), vec![&setify("A")]);
}

#[test]
fn test_simple_tree_tuple() {
    let tree = simple_tree_tuple(&[1.into(), 2.into(), 3.into(), 4.into()]);
    assert_eq!(
        tree,
        ContractionTree::Node(vec![
            ContractionTree::Node(vec![ContractionTree::Node(vec![1.into(), 2.into()]), 3.into()]),
            4.into()
        ])
    );
}