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
impl Tensor
Sourcepub fn new(legs: Vec<Index>, data: Vec<Complex<f32>>) -> QuantumResult<Self>
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().
Sourcepub fn ket_zero(wire: Index) -> Self
pub fn ket_zero(wire: Index) -> Self
The single-qubit |0⟩ state vector as a rank-1 tensor on leg wire.
Sourcepub fn bra_basis(wire: Index, bit: bool) -> Self
pub fn bra_basis(wire: Index, bit: bool) -> Self
The single-qubit basis bra ⟨b| as a rank-1 tensor on leg wire.
Sourcepub fn gate_1q(
out_wire: Index,
in_wire: Index,
m: &[[Complex<f32>; 2]; 2],
) -> Self
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.
Sourcepub fn gate_2q(
out0: Index,
out1: Index,
in0: Index,
in1: Index,
m: &[[Complex<f32>; 4]; 4],
) -> Self
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.
Sourcepub fn as_scalar(&self) -> QuantumResult<Complex<f32>>
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.
Sourcepub fn contract(&self, other: &Self) -> QuantumResult<Self>
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.