Skip to main content

NestedEinsum

Enum NestedEinsum 

Source
pub enum NestedEinsum<L: Label> {
    Leaf {
        tensor_index: usize,
    },
    Node {
        args: Vec<NestedEinsum<L>>,
        eins: EinCode<L>,
    },
}
Expand description

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());

Variants§

§

Leaf

A leaf node referencing an input tensor by index.

Fields

§tensor_index: usize

Index of the input tensor (0-indexed)

§

Node

An internal node representing a contraction.

Fields

§args: Vec<NestedEinsum<L>>

Child nodes to contract

§eins: EinCode<L>

The einsum operation for this contraction

Implementations§

Source§

impl<L: Label> NestedEinsum<L>

Source

pub fn leaf(tensor_index: usize) -> Self

Create a leaf node for an input tensor.

Source

pub fn node(args: Vec<NestedEinsum<L>>, eins: EinCode<L>) -> Self

Create an internal node for a contraction.

Source

pub fn is_leaf(&self) -> bool

Check if this node is a leaf.

Source

pub fn tensor_index(&self) -> Option<usize>

Get the tensor index if this is a leaf node.

Source

pub fn is_binary(&self) -> bool

Check if the tree is strictly binary (each internal node has exactly 2 children).

Source

pub fn node_count(&self) -> usize

Count the total number of nodes in the tree.

Source

pub fn leaf_count(&self) -> usize

Count the number of leaf nodes (input tensors).

Source

pub fn leaf_indices(&self) -> Vec<usize>

Get all leaf tensor indices in depth-first order.

Source

pub fn output_labels(&self, input_labels: &[Vec<L>]) -> Vec<L>

Get the output labels for this subtree.

Requires the original input tensor labels to compute leaf outputs.

Source

pub fn depth(&self) -> usize

Get the depth of the tree (longest path from root to leaf).

Source

pub fn to_eincode(&self, input_labels: &[Vec<L>]) -> EinCode<L>

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
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);
Source

pub fn is_path_decomposition(&self) -> bool

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
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());

Trait Implementations§

Source§

impl<L: Clone + Label> Clone for NestedEinsum<L>

Source§

fn clone(&self) -> NestedEinsum<L>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<L: Debug + Label> Debug for NestedEinsum<L>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de, L> Deserialize<'de> for NestedEinsum<L>
where L: Deserialize<'de> + Label,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<L: PartialEq + Label> PartialEq for NestedEinsum<L>

Source§

fn eq(&self, other: &NestedEinsum<L>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<L> Serialize for NestedEinsum<L>
where L: Serialize + Label,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<L: Eq + Label> Eq for NestedEinsum<L>

Source§

impl<L: Label> StructuralPartialEq for NestedEinsum<L>

Auto Trait Implementations§

§

impl<L> Freeze for NestedEinsum<L>

§

impl<L> RefUnwindSafe for NestedEinsum<L>
where L: RefUnwindSafe,

§

impl<L> Send for NestedEinsum<L>

§

impl<L> Sync for NestedEinsum<L>

§

impl<L> Unpin for NestedEinsum<L>
where L: Unpin,

§

impl<L> UnwindSafe for NestedEinsum<L>
where L: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,