GenerationShape

Enum GenerationShape 

Source
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

Source

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.

Source

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.
Source

pub fn hessian_shape(&self, num_params: usize) -> Self

Determine the hessian shape of a tensor with this shape that has num_params parameters.

§Arguments
  • num_params - The number of parameters in the hessian.
§Returns

A new GenerationShape representing the shape of the hessian.

§See Also
  • [gradient_shape] For the shape of a gradient tensor.
Source

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

pub fn is_scalar(&self) -> bool

Checks if the current GenerationShape is strictly a scalar variant.

Source

pub fn is_vector(&self) -> bool

Checks if the current GenerationShape is strictly a vector variant.

Source

pub fn is_matrix(&self) -> bool

Checks if the current GenerationShape is strictly a matrix variant.

Source

pub fn is_tensor3d(&self) -> bool

Checks if the current GenerationShape is strictly a tensor3D variant.

Source

pub fn is_tensor4d(&self) -> bool

Checks if the current GenerationShape is strictly a tensor4D variant.

Source

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

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

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

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

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

pub fn ncols(&self) -> usize

Returns the number of columns for the current shape.

§Returns

The number of columns.

§Examples
use qudit_expr::GenerationShape;
let matrix_shape = GenerationShape::Matrix(2, 3);
assert_eq!(matrix_shape.ncols(), 3);
Source

pub fn nrows(&self) -> usize

Returns the number of rows for the current shape.

§Returns

The number of rows.

§Examples
use qudit_expr::GenerationShape;
let matrix_shape = GenerationShape::Matrix(2, 3);
assert_eq!(matrix_shape.nrows(), 2);
Source

pub fn nmats(&self) -> usize

Returns the number of matrices for the current shape.

§Returns

The number of matrices.

§Examples
use qudit_expr::GenerationShape;
let tensor3d_shape = GenerationShape::Tensor3D(5, 2, 3);
assert_eq!(tensor3d_shape.nmats(), 5);
Source

pub fn ntens(&self) -> usize

Returns the number of tensors (in the first dimension) for the current shape.

§Returns

The number of tensors.

§Examples
use qudit_expr::GenerationShape;
let tensor4d_shape = GenerationShape::Tensor4D(7, 5, 2, 3);
assert_eq!(tensor4d_shape.ntens(), 7);
Source

pub fn calculate_directions(&self, index_sizes: &[usize]) -> Vec<IndexDirection>

Trait Implementations§

Source§

impl Add for GenerationShape

Source§

type Output = GenerationShape

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl Clone for GenerationShape

Source§

fn clone(&self) -> GenerationShape

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 GenerationShape

Source§

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

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

impl<I: AsRef<[TensorIndex]>> From<I> for GenerationShape

Source§

fn from(indices: I) -> Self

Converts to this type from the input type.
Source§

impl Hash for GenerationShape

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 Ord for GenerationShape

Source§

fn cmp(&self, other: &GenerationShape) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for GenerationShape

Source§

fn eq(&self, other: &GenerationShape) -> 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 PartialOrd for GenerationShape

Source§

fn partial_cmp(&self, other: &GenerationShape) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Copy for GenerationShape

Source§

impl Eq for GenerationShape

Source§

impl StructuralPartialEq for GenerationShape

Auto Trait Implementations§

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> ByRef<T> for T

Source§

fn by_ref(&self) -> &T

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> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

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

Source§

fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> T
where Self: Distribution<T>,

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<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<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> 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> Boilerplate for T
where T: Copy + Send + Sync + Debug + PartialEq + 'static,