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
impl Tensor
Sourcepub fn new_from_map(
legs: Vec<EdgeIndex>,
bond_dims_map: &FxHashMap<EdgeIndex, u64>,
) -> Self
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]);Sourcepub fn new_from_const(legs: Vec<EdgeIndex>, bond_dim: u64) -> Self
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]);Sourcepub fn new_composite(tensors: Vec<Self>) -> Self
pub fn new_composite(tensors: Vec<Self>) -> Self
Creates a new composite tensor with the given nested tensors.
Sourcepub fn legs(&self) -> &Vec<EdgeIndex> ⓘ
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]);Sourcepub fn edges(&self) -> impl Iterator<Item = (&EdgeIndex, &u64)> + '_
pub fn edges(&self) -> impl Iterator<Item = (&EdgeIndex, &u64)> + '_
Returns an iterator of tuples of leg ids and their corresponding bond size.
Sourcepub fn tensors(&self) -> &Vec<Self>
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());
}Sourcepub fn nested_tensor(&self, nested_indices: &[usize]) -> &Tensor
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());Sourcepub fn total_num_tensors(&self) -> usize
pub fn total_num_tensors(&self) -> usize
Returns the total number of leaf tensors in the hierarchy.
Sourcepub fn tensor(&self, i: TensorIndex) -> &Self
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());Sourcepub fn shape(&self) -> Result<Vec<usize>, TryFromIntError>
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.
Sourcepub fn dims(&self) -> usize
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);Sourcepub fn size(&self) -> f64
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);Sourcepub fn is_leaf(&self) -> bool
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);Sourcepub fn is_composite(&self) -> bool
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);Sourcepub fn is_empty(&self) -> bool
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);Sourcepub fn into_tensor_data(self) -> TensorData
pub fn into_tensor_data(self) -> TensorData
Consumes the Tensor and returns its TensorData.
§Examples
let tensor = Tensor::new_from_const(vec![1, 2], 2);
let tensordata = tensor.into_tensor_data();
assert_eq!(tensordata, TensorData::Uncontracted);Sourcepub fn push_tensor(&mut self, tensor: Self)
pub fn push_tensor(&mut self, tensor: Self)
Pushes additional tensor into this tensor, which must be a composite tensor.
Sourcepub fn push_tensors(&mut self, tensors: Vec<Self>)
pub fn push_tensors(&mut self, tensors: Vec<Self>)
Pushes additional tensors into this tensor, which must be a composite tensor.
Sourcepub fn tensor_data(&self) -> &TensorData
pub fn tensor_data(&self) -> &TensorData
Getter for tensor data.
Sourcepub fn set_tensor_data(&mut self, tensordata: TensorData)
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);Sourcepub fn is_connected(&self) -> bool
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());Sourcepub fn difference(&self, other: &Self) -> Self
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]);Sourcepub fn union(&self, other: &Self) -> Self
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]);Sourcepub fn intersection(&self, other: &Self) -> Self
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]);Sourcepub fn symmetric_difference(&self, other: &Self) -> Self
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]);Sourcepub fn external_tensor(&self) -> Tensor
pub fn external_tensor(&self) -> Tensor
Get output legs after tensor contraction
Trait Implementations§
Source§impl AbsDiffEq for Tensor
impl AbsDiffEq for Tensor
Source§fn default_epsilon() -> Self::Epsilon
fn default_epsilon() -> Self::Epsilon
Source§fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool
fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool
§fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool
fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool
AbsDiffEq::abs_diff_eq].Source§impl BitXorAssign<&Tensor> for Tensor
impl BitXorAssign<&Tensor> for Tensor
Source§fn bitxor_assign(&mut self, rhs: &Tensor)
fn bitxor_assign(&mut self, rhs: &Tensor)
^= operation. Read moreSource§impl<'de> Deserialize<'de> for Tensor
impl<'de> Deserialize<'de> for Tensor
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for Tensor
impl RefUnwindSafe for Tensor
impl Send for Tensor
impl Sync for Tensor
impl Unpin for Tensor
impl UnsafeUnpin for Tensor
impl UnwindSafe for Tensor
Blanket Implementations§
§impl<T> AnyExt for T
impl<T> AnyExt for T
§fn downcast_ref<T>(this: &Self) -> Option<&T>where
T: Any,
fn downcast_ref<T>(this: &Self) -> Option<&T>where
T: Any,
T behind reference§fn downcast_mut<T>(this: &mut Self) -> Option<&mut T>where
T: Any,
fn downcast_mut<T>(this: &mut Self) -> Option<&mut T>where
T: Any,
T behind mutable reference§fn downcast_rc<T>(this: Rc<Self>) -> Result<Rc<T>, Rc<Self>>where
T: Any,
fn downcast_rc<T>(this: Rc<Self>) -> Result<Rc<T>, Rc<Self>>where
T: Any,
T behind Rc pointer§fn downcast_arc<T>(this: Arc<Self>) -> Result<Arc<T>, Arc<Self>>where
T: Any,
fn downcast_arc<T>(this: Arc<Self>) -> Result<Arc<T>, Arc<Self>>where
T: Any,
T behind Arc pointer§fn downcast_box<T>(this: Box<Self>) -> Result<Box<T>, Box<Self>>where
T: Any,
fn downcast_box<T>(this: Box<Self>) -> Result<Box<T>, Box<Self>>where
T: Any,
T behind Box pointer§fn downcast_move<T>(this: Self) -> Option<T>
fn downcast_move<T>(this: Self) -> Option<T>
Self to T,
useful only in generic context as a workaround for specialization§impl<Src, Scheme> ApproxFrom<Src, Scheme> for Srcwhere
Scheme: ApproxScheme,
impl<Src, Scheme> ApproxFrom<Src, Scheme> for Srcwhere
Scheme: ApproxScheme,
§fn approx_from(src: Src) -> Result<Src, <Src as ApproxFrom<Src, Scheme>>::Err>
fn approx_from(src: Src) -> Result<Src, <Src as ApproxFrom<Src, Scheme>>::Err>
§impl<Dst, Src, Scheme> ApproxInto<Dst, Scheme> for Srcwhere
Dst: ApproxFrom<Src, Scheme>,
Scheme: ApproxScheme,
impl<Dst, Src, Scheme> ApproxInto<Dst, Scheme> for Srcwhere
Dst: ApproxFrom<Src, Scheme>,
Scheme: ApproxScheme,
§fn approx_into(self) -> Result<Dst, <Src as ApproxInto<Dst, Scheme>>::Err>
fn approx_into(self) -> Result<Dst, <Src as ApproxInto<Dst, Scheme>>::Err>
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T, X> CoerceTo<T> for Xwhere
T: CoerceFrom<X> + ?Sized,
impl<T, X> CoerceTo<T> for Xwhere
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
impl<T, Dst> ConvAsUtil<Dst> for T
§impl<T> ConvUtil for T
impl<T> ConvUtil for T
§fn approx_as<Dst>(self) -> Result<Dst, Self::Err>where
Self: Sized + ApproxInto<Dst>,
fn approx_as<Dst>(self) -> Result<Dst, Self::Err>where
Self: Sized + ApproxInto<Dst>,
§fn approx_as_by<Dst, Scheme>(self) -> Result<Dst, Self::Err>where
Self: Sized + ApproxInto<Dst, Scheme>,
Scheme: ApproxScheme,
fn approx_as_by<Dst, Scheme>(self) -> Result<Dst, Self::Err>where
Self: Sized + ApproxInto<Dst, Scheme>,
Scheme: ApproxScheme,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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