tnc/contractionpath/
paths.rs1use crate::contractionpath::ContractionPath;
4
5pub mod branchbound;
6pub mod cotengrust;
7pub mod hyperoptimization;
8#[cfg(feature = "cotengra")]
9pub mod tree_annealing;
10#[cfg(feature = "cotengra")]
11pub mod tree_reconfiguration;
12#[cfg(feature = "cotengra")]
13pub mod tree_tempering;
14pub mod weighted_branchbound;
15
16pub trait FindPath {
18 fn find_path(&mut self);
20
21 fn get_best_path(&self) -> &ContractionPath;
23
24 fn get_best_replace_path(&self) -> ContractionPath;
26
27 fn get_best_flops(&self) -> f64;
29
30 fn get_best_size(&self) -> f64;
32}
33
34#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
36pub enum CostType {
37 Flops,
39 Size,
41}
42
43pub(crate) fn validate_path(path: &ContractionPath) {
44 let mut contracted = Vec::<usize>::new();
45 for nested in path.nested.values() {
46 validate_path(nested);
47 }
48
49 for (u, v) in &path.toplevel {
50 assert!(
51 !contracted.contains(u),
52 "Contracting already contracted tensors: {u:?}, path: {path:?}"
53 );
54 contracted.push(*v);
55 }
56}
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61
62 use crate::path;
63
64 #[test]
65 #[should_panic(
66 expected = "Contracting already contracted tensors: 1, path: ContractionPath { nested: {}, toplevel: [(0, 1), (1, 2)] }"
67 )]
68 fn test_validate_paths() {
69 let invalid_path = path![(0, 1), (1, 2)];
70 validate_path(&invalid_path);
71 }
72}