Function Repository Resource:

 TensorTrainContract

Source Notebook

Contract a tensor train (TT) of low-rank cores back into the dense multidimensional array it represents

Contributed by: Ruben Ranval

ResourceFunction["TensorTrainContract"][{A1,A2…,An}]

contracts the tensor train formed by the rank-3 cores list {A1,A2…,An} back into the dense array it represents.

Details

ResourceFunction["TensorTrainContract"] reconstructs a full dense array from its Tensor Train (TT) representation (also known as the Matrix Product State representation). It is the inverse of ResourceFunction["TensorTrainDecomposition"].
The input is a nonempty list of rank-3 arrays {A1,A2…,An}, where n is the number of dimensions of the resulting array.
The bond dimension refers to the size of the internal contracted indices connecting adjacent tensor cores. It effectively dictates the rank and compression level of the Tensor Train.
Each core Ak has dimensions {χk-1,dk,χk} where dk is the size of the kth dimension of the resulting array, and χk is the bond dimension between cores k andk+1.
The first core has χ0=1 and the last core has χn=1.
Contracting the cores returned by ResourceFunction[“TensorTrainDecomposition”] reconstructs the original array, exactly up to floating-point error when the decomposition was exact, or up to the truncation error when a "MaxBondDimension" cap or nonzero Tolerance was applied.
For more background on the representation used for Tensor Trains, see ResourceFunction["TensorTrainDecomposition"].

Examples

Basic Examples (2) 

Decompose a tensor and contract it back to recover the original array:

In[1]:=
tensor = RandomReal[1, {2, 3, 4}];
tensor // MatrixForm
Out[2]=
In[3]:=
tensorTrain = ResourceFunction["TensorTrainDecomposition"][tensor];
In[4]:=
recon = ResourceFunction[
   "TensorTrainContract", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"]@tensorTrain;
recon // MatrixForm
Out[5]=

The original and the reconstructed tensors are exactly equal up to floating-point error:

In[6]:=
Max[Abs[tensor - recon]]
Out[6]=

Applications (1) 

Because contraction works on any valid train, you can decompose with a capped bond dimension and contract the result to obtain a low-rank approximation of the original tensor. The difference is the truncation error:

In[7]:=
largeTensor = RandomReal[{-1, 1}, {4, 4, 4, 4}];
compressed = ResourceFunction["TensorTrainDecomposition"][largeTensor, "MaxBondDimension" -> 2];
approx = ResourceFunction[
   "TensorTrainContract", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][compressed];
Max[Abs[largeTensor - approx]]
Out[4]=

Possible Issues (2) 

Cores whose adjacent bond dimensions do not match cannot be contracted (here 3 ≠ 4):

In[8]:=
ResourceFunction[
 "TensorTrainContract", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][{RandomReal[1, {1, 2, 3}], RandomReal[1, {4, 2, 1}]}]
Out[8]=

The first and last cores must have boundary bond dimension 1:

In[9]:=
ResourceFunction[
 "TensorTrainContract", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][{RandomReal[1, {2, 3, 1}]}]
Out[9]=

Neat Examples (1) 

Reconstruct a non-separable pattern from tensor trains of increasing bond dimension to watch a low-rank approximation sharpen into the exact array:

In[10]:=
n = 120;
c = (n + 1)/2;
pattern = Table[Sin[Sqrt[(i - c)^2 + (j - c)^2]/4.], {i, n}, {j, n}];

ranks = {1, 3, 5, 20, 30};
recons = Table[ResourceFunction[
    "TensorTrainContract", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][
    ResourceFunction["TensorTrainDecomposition"][pattern, "MaxBondDimension" -> r]], {r, ranks}];

Row[MapThread[
  ArrayPlot[#1, ColorFunction -> "DeepSeaColors", Frame -> False, ImageSize -> 150, PlotLabel -> #2] &, {Append[recons, pattern], Append[("\[Chi] = " <> ToString[#] &) /@ ranks, "exact"]}]]
Out[6]=

Publisher

Ruben Ranval

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.1 – 01 July 2026
  • 1.0.0 – 18 June 2026

Source Metadata

Related Resources

License Information