RubenRanval/ TensorTrainTools

Decompose, compress, and compute with high-dimensional arrays in the tensor train (also known as MPS) format

Contributed by: Ruben Ranval

TensorTrainTools provides a compact Tensor Train representation for high-rank tensors, together with the standard operations of the tensor-train toolkit: decomposition of dense arrays with controlled truncation, rank compression, orthogonalization, norms and inner products computed without ever forming the dense tensor, and exact tensor arithmetic through familiar operators. A tensor with millions or billions of entries whose content is structured is often representable by a few thousand parameters. This paclet lets you find that representation, verify its accuracy, and keep computing inside it.

Installation Instructions

To install this paclet in your Wolfram Language environment, evaluate this code:
PacletInstall["RubenRanval/TensorTrainTools"]


To load the code after installation, evaluate this code:
Needs["RubenRanval`TensorTrainTools`"]

Details

A tensor train represents a rank-d tensor as a chain of d rank-3 cores. Storage scales as O(d·n·χ²) in the bond dimension χ instead of O(nᵈ).
Standard operators work directly on tensor trains: t1+t2, ct (multiplication by a scalar c), t1-t2, and the elementwise product (also known as Hadamard product) t1⊙ t2.
Arithmetic is exact and grows bond dimensions (sums add ranks, Hadamard product multiply them). Using TensorTrainCompress allows the reduce bond dimension after each operation.
Applications span high-dimensional PDEs, fluid dynamics, quantum many-body physics (where tensor train are known as matrix product states, MPS), and machine learning for compressing neural network layers.

Paclet Guide

Examples

Basic Examples

Decompose a structured tensor t into a tensor train tt:

In[1]:=
t = N@Table[Sin[x + y + z], {x, 20}, {y, 20}, {z, 20}];
tt = TensorTrainDecomposition[t, Tolerance -> 10^-10]
Out[2]=

The compression factor (the ration between the number of stored elements and the number of elements in the dense tensor) can be accessed in the summary box or with the "CompressionRatio" property:

In[3]:=
%["CompressionRatio"]
Out[3]=

The representation is accurate to machine precision:

In[4]:=
Max@Abs[Normal[tt] - t]
Out[4]=

One can visualize the tensor train structure quickly with a tensor network diagram, using the "Diagram" property:

In[5]:=
RubenRanval`TensorTrainTools`TensorTrain[{CompressedData["
1:eJwBVQGq/iFib1JlAwAAAAEAAAAUAAAAAgAAAE2op/bFpdO/oTI/bm+Hmj/g
TNtQMuTHv1w9kmJrj9C/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qE6mO8ass/e/7w
KvKayb8EAvCj0vG1PxV2U/D+Gse/3DhWmdMev78ki5fKkmV0PzBmj/pnycu/
5m3UllLLxz8Len6Cu+68vzvbkBsjE8k/O/MAJhBPuD+NDFMK0GmaP65jipmz
mcs/+RadCciBxb+dDiK6sqvBPwfZPqLMisq/YEAjtb4Csb8eUwgp17KsvyeR
J6+T3Mq/iaC/uwrKwj+ka2L8fIXEv1ja7q52ess/6LNHfIi+oj+l/lFO
"], CompressedData["
1:eJwBVQGq/iFib1JlAwAAAAIAAAAUAAAAAQAAAOr7tpb+JixAQOXZ2dgdIUAI
SMiR5k8TwNhU2EkMjSvArukRGpsdJMD3fybMcEEHQKzNU0nvZSpAtgFTlku2
JkAxXLjfr6/tv12vSrKPtyjAE2k3i5zaKMB0lIyEPx/xvyViBq+KiiZAbXht
fJR/KkDUIZDzTV8IQEKSDVQG6iPAP7oTcMacK8DPc7J3DdkTwFlESYB44yBA
P/u5Gn0sLEBSJW2S1A3yP7HPDD4siibAgWVpEAGdKsCQIsir9t8IwM+XfAOx
5CNAEIyYQC23K0D0Wy1PxxwUQAXzyKNH2SDAEB+v4FZDLMDUwLDBhWIbwGkI
wmcT7xpAKhboxK8+LEB8Bray+Q0hQOmu+0CWoRPAFp9QxE+pK8Cq4nLzTRMk
wHH6oZQF3wdA2AuhPjSGKkBrzZA4xbEmQDXKaH0dAfC/bVCUxA==
"]}]["Diagram"]
Out[5]=

One can then perform arithmetic on tensor trains. Arithmetic stays in the compressed format:

In[6]:=
a = TensorTrainDecomposition[RandomReal[{-1, 1}, {2, 3, 4, 3, 2}]]
Out[6]=
In[7]:=
b = TensorTrainDecomposition[RandomReal[{-1, 1}, {2, 3, 4, 3, 2}]]
Out[7]=

As we can see, the bond dimensions add up:

In[8]:=
s = 2 a - b
Out[8]=
In[9]:=
s["BondDimensions"]
Out[9]=

A rank-40 tensor with 2⁴⁰ entries (about 8 TB dense) can be held in a few thousand parameters, with its norm computed directly in the compressed format. Here we are generating a random tensor train representing a tensor of dimensions 2 ⨯ 2 ⨯ … ⨯ 2 with max bond dimensions {2, 4, 4, …, 4, 2}:

In[10]:=
big = RandomTensorTrain[ConstantArray[2, 40], Join[{2}, ConstantArray[4, 37], {2}]]
Out[10]=

We can get the full element count of the corresponding dense tensor:

In[11]:=
big["FullElementCount"]
Out[11]=

We can also get the number of entries actually stored in the cores of the tensor train:

In[12]:=
big["ParameterCount"]
Out[12]=

And compute the norm directly on the tensor train, without ever generating the full dense tensor:

In[13]:=
TensorTrainNorm[big]
Out[13]=

Publisher

Ruben Ranval

Compatibility

Wolfram Language Version 14

Version History

  • 1.0.0 – 20 July 2026

License Information

MIT License

Paclet Source

Source Metadata