Tensor

Struct Tensor 

Source
pub struct Tensor { /* private fields */ }
Expand description

Abstract representation of a tensor. Can be a composite tensor (that is, a tensor network) or a leaf tensor.

Implementations§

Source§

impl Tensor

Source

pub fn new_from_map( legs: Vec<EdgeIndex>, bond_dims_map: &FxHashMap<EdgeIndex, u64>, ) -> Self

Constructs a Tensor using with the given edge ids and a mapping of edge ids to corresponding bond dimension.

§Examples
let bond_dims = FxHashMap::from_iter([(1, 2), (2, 4), (3, 6)]);
let tensor = Tensor::new_from_map(vec![1, 2, 3], &bond_dims);
assert_eq!(tensor.legs(), &[1, 2, 3]);
assert_eq!(tensor.bond_dims(), &[2, 4, 6]);
Source

pub fn new_from_const(legs: Vec<EdgeIndex>, bond_dim: u64) -> Self

Constructs a Tensor with the given edge ids and the same bond dimension for all edges.

§Examples
let tensor = Tensor::new_from_const(vec![1, 2, 3], 2);
assert_eq!(tensor.legs(), &[1, 2, 3]);
assert_eq!(tensor.bond_dims(), &[2, 2, 2]);
Source

pub fn new_composite(tensors: Vec<Self>) -> Self

Creates a new composite tensor with the given nested tensors.

Source

pub fn legs(&self) -> &Vec<EdgeIndex>

Returns edge ids of Tensor object.

§Examples
let tensor = Tensor::new_from_const(vec![1, 2, 3], 3);
assert_eq!(tensor.legs(), &[1, 2, 3]);
Source

pub fn edges(&self) -> impl Iterator<Item = (&EdgeIndex, &u64)> + '_

Returns an iterator of tuples of leg ids and their corresponding bond size.

Source

pub fn tensors(&self) -> &Vec<Self>

Returns the nested tensors of a composite tensor.

§Examples
let bond_dims = FxHashMap::from_iter([(0, 17), (1, 19), (2, 8)]);
let v1 = Tensor::new_from_map(vec![0, 1], &bond_dims);
let v2 = Tensor::new_from_map(vec![1, 2], &bond_dims);
let tn = Tensor::new_composite(vec![v1.clone(), v2.clone()]);
for (tensor, ref_tensor) in std::iter::zip(tn.tensors(), vec![v1, v2]){
   assert_eq!(tensor.legs(), ref_tensor.legs());
}
Source

pub fn nested_tensor(&self, nested_indices: &[usize]) -> &Tensor

Gets a nested Tensor based on the nested_indices which specify the index of the tensor at each level of the hierarchy.

§Examples
let bond_dims = FxHashMap::from_iter([(0, 17), (1, 19), (2, 8), (3, 2), (4, 1)]);
let mut v1 = Tensor::new_from_map(vec![0, 1], &bond_dims);
let mut v2 = Tensor::new_from_map(vec![1, 2], &bond_dims);
let mut v3 = Tensor::new_from_map(vec![2, 3], &bond_dims);
let mut v4 = Tensor::new_from_map(vec![3, 4], &bond_dims);
let tn1 = Tensor::new_composite(vec![v1, v2]);
let tn2 = Tensor::new_composite(vec![v3.clone(), v4]);
let nested_tn = Tensor::new_composite(vec![tn1, tn2]);

assert_eq!(nested_tn.nested_tensor(&[1, 0]).legs(), v3.legs());
Source

pub fn total_num_tensors(&self) -> usize

Returns the total number of leaf tensors in the hierarchy.

Source

pub fn tensor(&self, i: TensorIndex) -> &Self

Get ith Tensor.

§Examples
let bond_dims = FxHashMap::from_iter([(0, 17), (1, 19), (2, 8)]);
let v1 = Tensor::new_from_map(vec![0, 1], &bond_dims);
let v2 = Tensor::new_from_map(vec![1, 2], &bond_dims);
let tn = Tensor::new_composite(vec![v1.clone(), v2]);
assert_eq!(tn.tensor(0).legs(), v1.legs());
Source

pub fn bond_dims(&self) -> &Vec<u64>

Getter for bond dimensions.

Source

pub fn shape(&self) -> Result<Vec<usize>, TryFromIntError>

Returns the shape of tensor. This is the same as the bond dimensions, but as usize. The conversion can fail, hence a Result is returned.

Source

pub fn dims(&self) -> usize

Returns the number of dimensions.

§Examples
let bond_dims = FxHashMap::from_iter([(1, 4), (2, 6), (3, 2)]);
let tensor = Tensor::new_from_map(vec![1, 2, 3], &bond_dims);
assert_eq!(tensor.dims(), 3);
Source

pub fn size(&self) -> f64

Returns the number of elements. This is a f64 to avoid overflow in large tensors.

§Examples
let bond_dims = FxHashMap::from_iter([(1, 5), (2, 15), (3, 8)]);
let tensor = Tensor::new_from_map(vec![1, 2, 3], &bond_dims);
assert_eq!(tensor.size(), 600.0);
Source

pub fn is_leaf(&self) -> bool

Returns true if Tensor is a leaf tensor, without any nested tensors.

§Examples
let bond_dims = FxHashMap::from_iter([(1, 2), (2, 4), (3, 6)]);
let tensor = Tensor::new_from_map(vec![1, 2, 3], &bond_dims);
assert_eq!(tensor.is_leaf(), true);
let comp = Tensor::new_composite(vec![tensor]);
assert_eq!(comp.is_leaf(), false);
Source

pub fn is_composite(&self) -> bool

Returns true if Tensor is composite.

§Examples
let bond_dims = FxHashMap::from_iter([(1, 2), (2, 4), (3, 6)]);
let tensor = Tensor::new_from_map(vec![1, 2, 3], &bond_dims);
assert_eq!(tensor.is_composite(), false);
let comp = Tensor::new_composite(vec![tensor]);
assert_eq!(comp.is_composite(), true);
Source

pub fn is_empty(&self) -> bool

Returns true if Tensor is empty. This means, it doesn’t have any subtensors, has no legs and is doesn’t have any data (e.g., is not a scalar).

§Examples
let tensor = Tensor::default();
assert_eq!(tensor.is_empty(), true);
Source

pub fn push_tensor(&mut self, tensor: Self)

Pushes additional tensor into this tensor, which must be a composite tensor.

Source

pub fn push_tensors(&mut self, tensors: Vec<Self>)

Pushes additional tensors into this tensor, which must be a composite tensor.

Source

pub fn tensor_data(&self) -> &TensorData

Getter for tensor data.

Source

pub fn set_tensor_data(&mut self, tensordata: TensorData)

Setter for tensor data.

§Examples
let mut tensor = Tensor::new_from_const(vec![0, 1], 2);
let tensordata = TensorData::Gate((String::from("x"), vec![], false));
tensor.set_tensor_data(tensordata);
Source

pub fn is_connected(&self) -> bool

Returns whether all tensors inside this tensor are connected. This only checks the top-level, not recursing into composite tensors.

§Examples
// Create a tensor network with two connected tensors
let bond_dims = FxHashMap::from_iter([(0, 17), (1, 19), (2, 8), (3, 5)]);
let v1 = Tensor::new_from_map(vec![0, 1], &bond_dims);
let v2 = Tensor::new_from_map(vec![1, 2], &bond_dims);
let mut tn = Tensor::new_composite(vec![v1, v2]);
assert!(tn.is_connected());

// Introduce a new tensor that is not connected
let v3 = Tensor::new_from_map(vec![3], &bond_dims);
tn.push_tensor(v3);
assert!(!tn.is_connected());
Source

pub fn difference(&self, other: &Self) -> Self

Returns Tensor with legs in self that are not in other.

§Examples
let bond_dims = FxHashMap::from_iter([(1, 2), (2, 4), (3, 6), (4, 3), (5, 9)]);
let tensor1 = Tensor::new_from_map(vec![1, 2, 3], &bond_dims);
let tensor2 = Tensor::new_from_map(vec![4, 2, 5], &bond_dims);
let diff_tensor = &tensor1 - &tensor2;
assert_eq!(diff_tensor.legs(), &[1, 3]);
assert_eq!(diff_tensor.bond_dims(), &[2, 6]);
Source

pub fn union(&self, other: &Self) -> Self

Returns Tensor with union of legs in both self and other.

§Examples
let bond_dims = FxHashMap::from_iter([(1, 2), (2, 4), (3, 6), (4, 3), (5, 9)]);
let tensor1 = Tensor::new_from_map(vec![1, 2, 3], &bond_dims);
let tensor2 = Tensor::new_from_map(vec![4, 2, 5], &bond_dims);
let union_tensor = &tensor1 | &tensor2;
assert_eq!(union_tensor.legs(), &[1, 2, 3, 4, 5]);
assert_eq!(union_tensor.bond_dims(), &[2, 4, 6, 3, 9]);
Source

pub fn intersection(&self, other: &Self) -> Self

Returns Tensor with intersection of legs in self and other.

§Examples
let bond_dims = FxHashMap::from_iter([(1, 2), (2, 4), (3, 6), (4, 3), (5, 9)]);
let tensor1 = Tensor::new_from_map(vec![1, 2, 3], &bond_dims);
let tensor2 = Tensor::new_from_map(vec![4, 2, 5], &bond_dims);
let intersection_tensor = &tensor1 & &tensor2;
assert_eq!(intersection_tensor.legs(), &[2]);
assert_eq!(intersection_tensor.bond_dims(), &[4]);
Source

pub fn symmetric_difference(&self, other: &Self) -> Self

Returns Tensor with symmetrical difference of legs in self and other.

§Examples
let bond_dims = FxHashMap::from_iter([(1, 2), (2, 4), (3, 6), (4, 3), (5, 9)]);
let tensor1 = Tensor::new_from_map(vec![1, 2, 3], &bond_dims);
let tensor2 = Tensor::new_from_map(vec![4, 2, 5], &bond_dims);
let sym_dif_tensor = &tensor1 ^ &tensor2;
assert_eq!(sym_dif_tensor.legs(), &[1, 3, 4, 5]);
assert_eq!(sym_dif_tensor.bond_dims(), &[2, 6, 3, 9]);
Source

pub fn external_tensor(&self) -> Tensor

Get output legs after tensor contraction

Trait Implementations§

Source§

impl ApproxEq for &Tensor

Source§

type Margin = F64Margin

This type type defines a margin within which two values are to be considered approximately equal. It must implement Default so that approx_eq() can be called on unknown types.
Source§

fn approx_eq<M: Into<Self::Margin>>(self, other: Self, margin: M) -> bool

This method tests that the self and other values are equal within margin of each other.
§

fn approx_ne<M>(self, other: Self, margin: M) -> bool
where M: Into<Self::Margin>,

This method tests that the self and other values are not within margin of each other.
Source§

impl BitAnd for &Tensor

Source§

type Output = Tensor

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: &Tensor) -> Tensor

Performs the & operation. Read more
Source§

impl BitOr for &Tensor

Source§

type Output = Tensor

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: &Tensor) -> Tensor

Performs the | operation. Read more
Source§

impl BitXor for &Tensor

Source§

type Output = Tensor

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: &Tensor) -> Tensor

Performs the ^ operation. Read more
Source§

impl BitXorAssign<&Tensor> for Tensor

Source§

fn bitxor_assign(&mut self, rhs: &Tensor)

Performs the ^= operation. Read more
Source§

impl Clone for Tensor

Source§

fn clone(&self) -> Tensor

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 Debug for Tensor

Source§

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

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

impl Default for Tensor

Source§

fn default() -> Tensor

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Tensor

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 Hash for Tensor

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Serialize for Tensor

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 Sub for &Tensor

Source§

type Output = Tensor

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &Tensor) -> Tensor

Performs the - operation. Read more

Auto Trait Implementations§

§

impl Freeze for Tensor

§

impl RefUnwindSafe for Tensor

§

impl Send for Tensor

§

impl Sync for Tensor

§

impl Unpin for Tensor

§

impl UnwindSafe for Tensor

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
§

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

§

fn downcast_ref<T>(this: &Self) -> Option<&T>
where T: Any,

Attempts to downcast this to T behind reference
§

fn downcast_mut<T>(this: &mut Self) -> Option<&mut T>
where T: Any,

Attempts to downcast this to T behind mutable reference
§

fn downcast_rc<T>(this: Rc<Self>) -> Result<Rc<T>, Rc<Self>>
where T: Any,

Attempts to downcast this to T behind Rc pointer
§

fn downcast_arc<T>(this: Arc<Self>) -> Result<Arc<T>, Arc<Self>>
where T: Any,

Attempts to downcast this to T behind Arc pointer
§

fn downcast_box<T>(this: Box<Self>) -> Result<Box<T>, Box<Self>>
where T: Any,

Attempts to downcast this to T behind Box pointer
§

fn downcast_move<T>(this: Self) -> Option<T>
where T: Any, Self: Sized,

Attempts to downcast owned Self to T, useful only in generic context as a workaround for specialization
§

impl<Src, Scheme> ApproxFrom<Src, Scheme> for Src
where Scheme: ApproxScheme,

§

type Err = NoError

The error type produced by a failed conversion.
§

fn approx_from(src: Src) -> Result<Src, <Src as ApproxFrom<Src, Scheme>>::Err>

Convert the given value into an approximately equivalent representation.
§

impl<Dst, Src, Scheme> ApproxInto<Dst, Scheme> for Src
where Dst: ApproxFrom<Src, Scheme>, Scheme: ApproxScheme,

§

type Err = <Dst as ApproxFrom<Src, Scheme>>::Err

The error type produced by a failed conversion.
§

fn approx_into(self) -> Result<Dst, <Src as ApproxInto<Dst, Scheme>>::Err>

Convert the subject into an approximately equivalent representation.
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
§

impl<T, X> CoerceTo<T> for X
where T: CoerceFrom<X> + ?Sized,

§

fn coerce_rc_to(self: Rc<X>) -> Rc<T>

§

fn coerce_box_to(self: Box<X>) -> Box<T>

§

fn coerce_ref_to(&self) -> &T

§

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

§

impl<T, Dst> ConvAsUtil<Dst> for T

§

fn approx(self) -> Result<Dst, Self::Err>
where Self: Sized + ApproxInto<Dst>,

Approximate the subject with the default scheme.
§

fn approx_by<Scheme>(self) -> Result<Dst, Self::Err>
where Self: Sized + ApproxInto<Dst, Scheme>, Scheme: ApproxScheme,

Approximate the subject with a specific scheme.
§

impl<T> ConvUtil for T

§

fn approx_as<Dst>(self) -> Result<Dst, Self::Err>
where Self: Sized + ApproxInto<Dst>,

Approximate the subject to a given type with the default scheme.
§

fn approx_as_by<Dst, Scheme>(self) -> Result<Dst, Self::Err>
where Self: Sized + ApproxInto<Dst, Scheme>, Scheme: ApproxScheme,

Approximate the subject to a given type with a specific scheme.
§

fn into_as<Dst>(self) -> Dst
where Self: Sized + Into<Dst>,

Convert the subject to a given type.
§

fn try_as<Dst>(self) -> Result<Dst, Self::Err>
where Self: Sized + TryInto<Dst>,

Attempt to convert the subject to a given type.
§

fn value_as<Dst>(self) -> Result<Dst, Self::Err>
where Self: Sized + ValueInto<Dst>,

Attempt a value conversion of the subject to a given type.
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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

impl<T> Serialize for T
where T: Serialize + ?Sized,

Source§

fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<(), Error>

Source§

fn do_erased_serialize( &self, serializer: &mut dyn Serializer, ) -> Result<(), ErrorImpl>

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
§

impl<Src> TryFrom<Src> for Src

§

type Err = NoError

The error type produced by a failed conversion.
§

fn try_from(src: Src) -> Result<Src, <Src as TryFrom<Src>>::Err>

Convert the given value into the subject type.
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.
§

impl<Src, Dst> TryInto<Dst> for Src
where Dst: TryFrom<Src>,

§

type Err = <Dst as TryFrom<Src>>::Err

The error type produced by a failed conversion.
§

fn try_into(self) -> Result<Dst, <Src as TryInto<Dst>>::Err>

Convert the subject into the destination type.
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.
§

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

§

fn vzip(self) -> V

§

impl<Src> ValueFrom<Src> for Src

§

type Err = NoError

The error type produced by a failed conversion.
§

fn value_from(src: Src) -> Result<Src, <Src as ValueFrom<Src>>::Err>

Convert the given value into an exactly equivalent representation.
§

impl<Src, Dst> ValueInto<Dst> for Src
where Dst: ValueFrom<Src>,

§

type Err = <Dst as ValueFrom<Src>>::Err

The error type produced by a failed conversion.
§

fn value_into(self) -> Result<Dst, <Src as ValueInto<Dst>>::Err>

Convert the subject into an exactly equivalent representation.
Source§

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

§

impl<T> Ungil for T
where T: Send,