Skip to main content

Tensor

Struct Tensor 

Source
pub struct Tensor {
    pub legs: Vec<Index>,
    pub data: Vec<Complex<f32>>,
}
Expand description

A dense complex tensor with named legs.

legs[d] is the Index of dimension d; every leg has extent 2 (a qubit wire). Data is stored row-major in leg order: the flat offset of a multi-index (i_0, …, i_{r-1}) is Σ_d i_d · 2^{r-1-d}.

Fields§

§legs: Vec<Index>

Names of the legs, in storage order.

§data: Vec<Complex<f32>>

Dense amplitudes, length 2^legs.len().

Implementations§

Source§

impl Tensor

Source

pub fn new(legs: Vec<Index>, data: Vec<Complex<f32>>) -> QuantumResult<Self>

Build a tensor from explicit legs and data, validating the length.

§Errors

Returns QuantumError::DimensionMismatch if data.len() != 2^legs.len().

Source

pub fn scalar(value: Complex<f32>) -> Self

Rank-0 scalar tensor.

Source

pub fn ket_zero(wire: Index) -> Self

The single-qubit |0⟩ state vector as a rank-1 tensor on leg wire.

Source

pub fn bra_basis(wire: Index, bit: bool) -> Self

The single-qubit basis bra ⟨b| as a rank-1 tensor on leg wire.

Source

pub fn gate_1q( out_wire: Index, in_wire: Index, m: &[[Complex<f32>; 2]; 2], ) -> Self

A single-qubit gate tensor out·in with legs [out_wire, in_wire].

m[r][c] is the matrix entry ⟨r|G|c⟩; the out_wire leg carries the row index, the in_wire leg the column index.

Source

pub fn gate_2q( out0: Index, out1: Index, in0: Index, in1: Index, m: &[[Complex<f32>; 4]; 4], ) -> Self

A two-qubit gate tensor with legs [out0, out1, in0, in1].

m is the 4 × 4 matrix in row-major form using the convention that the composite index is 2*q0 + q1 (qubit 0 most significant), i.e. the same ordering used by the dense two-qubit application path.

Source

pub fn rank(&self) -> usize

Rank of the tensor (number of legs).

Source

pub fn as_scalar(&self) -> QuantumResult<Complex<f32>>

Extract the scalar value of a rank-0 tensor.

§Errors

Returns QuantumError::Internal if the tensor is not rank-0.

Source

pub fn contract(&self, other: &Self) -> QuantumResult<Self>

Contract self with other over all shared legs (a tensordot).

The resulting tensor’s legs are self’s remaining legs followed by other’s remaining legs, both in their original order. Shared legs are summed over. With no shared legs this reduces to an outer product.

§Errors

Currently infallible for well-formed inputs; returns a Result for interface uniformity and future fallibility.

Trait Implementations§

Source§

impl Clone for Tensor

Source§

fn clone(&self) -> Tensor

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · 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

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