Skip to main content

tensor4all_treetn/operator/
mod.rs

1//! Operator composition for Tree Tensor Networks.
2//!
3//! This module provides traits and functions for working with operators on TreeTN states,
4//! including composing multiple non-overlapping operators into a single operator.
5//!
6//! # Key Types
7//!
8//! - [`Operator`]: Trait for objects that can act as operators on tensor network states
9//! - [`LinearOperator`]: MPO wrapper with index mapping for automatic transformations
10//! - [`IndexMapping`]: Mapping between true site indices and internal MPO indices
11//! - [`compose_exclusive_linear_operators`]: Compose non-overlapping LinearOperators into a single operator
12//!
13//! # Example
14//!
15//! ```
16//! use tensor4all_core::{DynIndex, IndexLike};
17//! use tensor4all_treetn::{apply_linear_operator, compose_exclusive_linear_operators, IndexMapping};
18//!
19//! let mapping = IndexMapping {
20//!     true_index: DynIndex::new_dyn(2),
21//!     internal_index: DynIndex::new_dyn(2),
22//! };
23//!
24//! assert_eq!(mapping.true_index.dim(), mapping.internal_index.dim());
25//! let _apply = apply_linear_operator::<tensor4all_core::TensorDynLen, usize>;
26//! let _compose = compose_exclusive_linear_operators::<tensor4all_core::TensorDynLen, usize>;
27//! ```
28
29mod apply;
30mod compose;
31mod identity;
32mod index_mapping;
33mod linear_operator;
34
35pub use apply::{apply_linear_operator, ApplyOptions, ArcLinearOperator};
36pub use compose::{are_exclusive_operators, compose_exclusive_linear_operators};
37pub use identity::build_identity_operator_tensor;
38pub use index_mapping::IndexMapping;
39pub use linear_operator::LinearOperator;
40
41use std::collections::HashSet;
42use std::fmt::Debug;
43use std::hash::Hash;
44
45use tensor4all_core::TensorLike;
46
47use crate::SiteIndexNetwork;
48
49/// Trait for operators that can act on tensor network states.
50///
51/// An operator has:
52/// - A set of site indices it acts on
53/// - A site index network describing its structure
54///
55/// This trait is used for composing multiple operators into a single operator
56/// and for validating operator compatibility.
57///
58/// # Type Parameters
59///
60/// - `T`: Tensor type implementing `TensorLike`
61/// - `V`: Node name type
62pub trait Operator<T, V>
63where
64    T: TensorLike,
65    V: Clone + Hash + Eq + Send + Sync + Debug,
66{
67    /// Get all site indices this operator acts on.
68    ///
69    /// Returns the union of site indices across all nodes.
70    fn site_indices(&self) -> HashSet<T::Index>;
71
72    /// Get the site index network describing this operator's structure.
73    ///
74    /// The site index network contains:
75    /// - Topology: which nodes connect to which
76    /// - Site space: which site indices belong to each node
77    fn site_index_network(&self) -> &SiteIndexNetwork<V, T::Index>;
78
79    /// Get the set of node names this operator covers.
80    ///
81    /// Default implementation extracts node names from the site index network.
82    fn node_names(&self) -> HashSet<V> {
83        self.site_index_network()
84            .node_names()
85            .into_iter()
86            .cloned()
87            .collect()
88    }
89}