pub enum GenerationShape {
Scalar,
Vector(usize),
Matrix(usize, usize),
Tensor3D(usize, usize, usize),
Tensor4D(usize, usize, usize, usize),
}Expand description
Represents the shape of a tensor as it will be generated.
While tensors can conceptually have rank larger than four, even infinite, tensors in the OpenQudit Expression library are generated into a buffer indexed by 0, 1, 2, 3, or 4 physical dimensions.
Variants§
Scalar
A 0-dimensional tensor (a single value).
Vector(usize)
A 1-dimensional tensor with nelems elements.
Matrix(usize, usize)
A 2-dimensional tensor (matrix) with nrows rows and ncols columns.
Tensor3D(usize, usize, usize)
A 3-dimensional tensor with nmats matrices, each of nrows rows and ncols columns.
Tensor4D(usize, usize, usize, usize)
A 4-dimensional tensor usually for derivatives (ntens, nmats, nrows, ncols)
Implementations§
Source§impl GenerationShape
impl GenerationShape
Sourcepub fn num_elements(&self) -> usize
pub fn num_elements(&self) -> usize
Calculates the total number of elements in a tensor with this shape.
§Returns
The total number of elements as usize.
Sourcepub fn gradient_shape(&self, num_params: usize) -> Self
pub fn gradient_shape(&self, num_params: usize) -> Self
Determines the shape of the derivative of a tensor with respect to num_params parameters.
This method effectively prepends num_params to the current tensor’s dimensions.
For example, the derivative of a Scalar with respect to num_params becomes a Vector(num_params).
The derivative of a Matrix(R, C) becomes a Tensor3D(num_params, R, C).
§Arguments
num_params- The number of parameters in the gradient.
§Returns
A new GenerationShape representing the shape of the gradient.
§See Also
[hessian_shape]For the shape of a hessian tensor.
Sourcepub fn hessian_shape(&self, num_params: usize) -> Self
pub fn hessian_shape(&self, num_params: usize) -> Self
Sourcepub fn to_vec(&self) -> Vec<usize>
pub fn to_vec(&self) -> Vec<usize>
Converts the tensor shape object to a vector of integers.
§Returns
A Vec<usize> containing the dimensions of the shape.
§Examples
use qudit_expr::GenerationShape;
let scalar_shape = GenerationShape::Scalar;
assert_eq!(scalar_shape.to_vec(), Vec::<usize>::new());
let vector_shape = GenerationShape::Vector(5);
assert_eq!(vector_shape.to_vec(), vec![5]);
let matrix_shape = GenerationShape::Matrix(2, 3);
assert_eq!(matrix_shape.to_vec(), vec![2, 3]);Sourcepub fn is_scalar(&self) -> bool
pub fn is_scalar(&self) -> bool
Checks if the current GenerationShape is strictly a scalar variant.
Sourcepub fn is_vector(&self) -> bool
pub fn is_vector(&self) -> bool
Checks if the current GenerationShape is strictly a vector variant.
Sourcepub fn is_matrix(&self) -> bool
pub fn is_matrix(&self) -> bool
Checks if the current GenerationShape is strictly a matrix variant.
Sourcepub fn is_tensor3d(&self) -> bool
pub fn is_tensor3d(&self) -> bool
Checks if the current GenerationShape is strictly a tensor3D variant.
Sourcepub fn is_tensor4d(&self) -> bool
pub fn is_tensor4d(&self) -> bool
Checks if the current GenerationShape is strictly a tensor4D variant.
Sourcepub fn is_0d(&self) -> bool
pub fn is_0d(&self) -> bool
Check if there is only one element.
§Returns
true if the shape can be treated as a scalar, false otherwise.
§Examples
use qudit_expr::GenerationShape;
let test_scalar = GenerationShape::Scalar;
let test_vector = GenerationShape::Vector(1);
let test_matrix = GenerationShape::Matrix(1, 1);
let test_tensor3d = GenerationShape::Tensor3D(1, 1, 1);
let test_vector_2 = GenerationShape::Vector(9);
let test_matrix_2 = GenerationShape::Matrix(9, 9);
let test_tensor3d_2 = GenerationShape::Tensor3D(1, 9, 9);
assert!(test_scalar.is_0d());
assert!(test_vector.is_0d());
assert!(test_matrix.is_0d());
assert!(test_tensor3d.is_0d());
assert_eq!(test_vector_2.is_0d(), false);
assert_eq!(test_matrix_2.is_0d(), false);
assert_eq!(test_tensor3d_2.is_0d(), false);Sourcepub fn is_1d(&self) -> bool
pub fn is_1d(&self) -> bool
Check if the shape can be conceptually treated as a 1d tensor.
§Returns
true if the shape has exactly one dimension with 1 or more elements.
§Examples
use qudit_expr::GenerationShape;
let test_vector = GenerationShape::Vector(9);
let test_matrix = GenerationShape::Matrix(1, 9);
let test_tensor3d = GenerationShape::Tensor3D(1, 1, 9);
let test_scalar = GenerationShape::Scalar;
let test_matrix_2 = GenerationShape::Matrix(9, 9);
let test_tensor3d_2 = GenerationShape::Tensor3D(1, 9, 9);
assert!(test_vector.is_1d());
assert!(test_matrix.is_1d());
assert!(test_tensor3d.is_1d());
assert_eq!(test_scalar.is_1d(), false);
assert_eq!(test_matrix_2.is_1d(), false);
assert_eq!(test_tensor3d_2.is_1d(), false);Sourcepub fn is_2d(&self) -> bool
pub fn is_2d(&self) -> bool
Checks if the current GenerationShape can be conceptually treated as a 2-dimensional matrix.
This is true for GenerationShape variants with a dimensionality of at least 2, with
any additional dimensions having size 1.
§Returns
true if the shape can be treated as a matrix, false otherwise.
§Examples
use qudit_expr::GenerationShape;
let test_scalar = GenerationShape::Scalar;
let test_vector = GenerationShape::Vector(1);
let test_tensor3d_2 = GenerationShape::Tensor3D(9, 9, 9);
let test_tensor_nd_2 = GenerationShape::Tensor4D(1, 9, 9, 9);
let test_matrix = GenerationShape::Matrix(9, 9);
let test_tensor3d = GenerationShape::Tensor3D(1, 9, 9);
let test_tensor_nd = GenerationShape::Tensor4D(1, 1, 9, 9);
assert_eq!(test_scalar.is_2d(), false);
assert_eq!(test_vector.is_2d(), false);
assert_eq!(test_tensor3d_2.is_2d(), false);
assert_eq!(test_tensor_nd_2.is_2d(), false);
assert_eq!(test_matrix.is_2d(), true);
assert_eq!(test_tensor3d.is_2d(), true);
assert_eq!(test_tensor_nd.is_2d(), true);Sourcepub fn is_3d(&self) -> bool
pub fn is_3d(&self) -> bool
Checks if the current GenerationShape can be conceptually treated as a 3D tensor.
This is true for GenerationShape variants with a dimensionality of at least 3, with
any additional dimensions having size 1.
§Returns
true if the shape can be treated as a 3D tensor, false otherwise.
§Examples
use qudit_expr::GenerationShape;
let test_scalar = GenerationShape::Scalar;
let test_vector = GenerationShape::Vector(1);
let test_matrix = GenerationShape::Matrix(1, 1);
let test_tensor_nd_2 = GenerationShape::Tensor4D(9, 1, 9, 9);
let test_tensor3d = GenerationShape::Tensor3D(9, 9, 9);
let test_tensor_nd = GenerationShape::Tensor4D(1, 9, 9, 9);
assert_eq!(test_scalar.is_3d(), false);
assert_eq!(test_vector.is_3d(), false);
assert_eq!(test_matrix.is_3d(), false);
assert_eq!(test_tensor_nd_2.is_3d(), false);
assert_eq!(test_tensor3d.is_3d(), true);
assert_eq!(test_tensor_nd.is_3d(), true);Sourcepub fn is_4d(&self) -> bool
pub fn is_4d(&self) -> bool
Checks if the current GenerationShape can be conceptually treated as a 4D tensor.
This is true for GenerationShape variants with a dimensionality of at least 4, with
any additional dimensions having size 1.
§Returns
true if the shape can be treated as a 4D tensor, false otherwise.
§Examples
use qudit_expr::GenerationShape;
let test_scalar = GenerationShape::Scalar;
let test_vector = GenerationShape::Vector(1);
let test_matrix = GenerationShape::Matrix(1, 1);
let test_tensor3d = GenerationShape::Tensor3D(1, 1, 1);
let test_tensor4d = GenerationShape::Tensor4D(9, 9, 9, 9);
let test_tensor_nd = GenerationShape::Tensor4D(1, 9, 9, 9);
assert_eq!(test_scalar.is_4d(), false);
assert_eq!(test_vector.is_4d(), false);
assert_eq!(test_matrix.is_4d(), false);
assert_eq!(test_tensor3d.is_4d(), false);
assert_eq!(test_tensor4d.is_4d(), true);
assert_eq!(test_tensor_nd.is_4d(), true);pub fn calculate_directions(&self, index_sizes: &[usize]) -> Vec<IndexDirection>
Trait Implementations§
Source§impl Add for GenerationShape
impl Add for GenerationShape
Source§impl Clone for GenerationShape
impl Clone for GenerationShape
Source§fn clone(&self) -> GenerationShape
fn clone(&self) -> GenerationShape
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for GenerationShape
impl Debug for GenerationShape
Source§impl<I: AsRef<[TensorIndex]>> From<I> for GenerationShape
impl<I: AsRef<[TensorIndex]>> From<I> for GenerationShape
Source§impl Hash for GenerationShape
impl Hash for GenerationShape
Source§impl Ord for GenerationShape
impl Ord for GenerationShape
Source§fn cmp(&self, other: &GenerationShape) -> Ordering
fn cmp(&self, other: &GenerationShape) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for GenerationShape
impl PartialEq for GenerationShape
Source§impl PartialOrd for GenerationShape
impl PartialOrd for GenerationShape
impl Copy for GenerationShape
impl Eq for GenerationShape
impl StructuralPartialEq for GenerationShape
Auto Trait Implementations§
impl Freeze for GenerationShape
impl RefUnwindSafe for GenerationShape
impl Send for GenerationShape
impl Sync for GenerationShape
impl Unpin for GenerationShape
impl UnwindSafe for GenerationShape
Blanket Implementations§
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,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
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