The Wolfram TensorNetworks paclet provides a general framework for Tensor Networks in the Wolfram Language. It integrates the Cotengra library (written in Rust) for high-performance contraction path optimization.
The paclet is available from the Wolfram Paclet Repository:
Wolfram/TensorNetworks
You can install the latest development version directly from the cloud:
PacletInstall["https://www.wolframcloud.com/obj/wolframquantumframework/TensorNetworks.paclet"]
To install from the source code locally:
- Clone the repository.
- Load the paclet directory:
PacletDirectoryLoad["paclet_path"]
Needs["Wolfram`TensorNetworks`"]
Contraction Path Optimization
(*generate a TN based on a graph with tensors being generated randomly*)
net = RandomTensorNetwork[StarGraph[5], Method -> "RandomComplex"];
(*contract TN with global indices in TensorContract*)
TensorNetworkContract[net]
(*find a contraction path using Cotengra*)
path = TensorNetworkFindContractionPath[net]
(*given different methods for contraction, perform path-dependent contraction*)
ActivateTensors /@ AssociationMap[
TensorNetworkContraction[net, path,
Method -> #] &, $TensorNetworkContractionMethods]
This section provides comprehensive documentation for all public functions.
Core Tensor Network Construction
Creates a tensor network object with a summary box display.
| Calling Form |
Description |
| TensorNetwork[{A₁,A₂,…}, {h₁,h₂,…}] |
creates a tensor network from tensors Aᵢ with hyperedge index lists hᵢ |
| TensorNetwork[{A₁,A₂,…}, {i₁,i₂,…} → out] |
creates a tensor network using Einstein summation notation |
| TensorNetwork[expr] |
constructs from TensorContract, Transpose, or TensorProduct expressions |
| TensorNetwork[graph] |
creates from a directed acyclic graph with tensor annotations |
| Calling Form |
Description |
| TensorNetworkQ[tn] |
yields True if tn is a valid TensorNetwork object, False otherwise |
Creates random tensor networks of various architectures.
| Calling Form |
Description |
| RandomTensorNetwork[{n,m}, maxDim] |
random network based on RandomGraph[{n,m}] with dimensions up to maxDim |
| RandomTensorNetwork[{n,m}, {dim}] |
uses fixed dimension dim for all indices |
| RandomTensorNetwork[graph, maxDim] |
creates from the specified graph |
| RandomTensorNetwork["MPS"[length, bondDim, physicalDim]] |
Matrix Product State |
| RandomTensorNetwork["TT"[length, bondDim]] |
Tensor Train (no physical indices) |
| RandomTensorNetwork["MPO"[length, bondDim, physicalDim]] |
Matrix Product Operator |
| RandomTensorNetwork["PEPS"[{rows,cols}, bondDim, physicalDim]] |
Projected Entangled Pair State |
| RandomTensorNetwork["TTN"[depth, bondDim, branching]] |
Tree Tensor Network |
| RandomTensorNetwork["MERA"[width, bondDim, layers]] |
Multi-scale Entanglement Renormalization Ansatz |
Options:
- Method → Automatic — "Complex" for complex-valued tensors
- "Boundary" → "Open" — "Periodic" for periodic boundary conditions
| Calling Form |
Description |
| TensorNetworkData[tn] |
returns an Association with vertices, tensors, indices, dimensions, and contractions |
| Calling Form |
Description |
| TensorNetworkSize[tn] |
returns the total number of elements across all tensors |
TensorNetworkContractions
| Calling Form |
Description |
| TensorNetworkContractions[tn] |
returns the list of index contractions in the network |
| Calling Form |
Description |
| TensorNetworkAdd[tn, tensor, indices] |
adds a tensor to the network with specified indices |
Indices matching existing network indices create new contractions.
| Calling Form |
Description |
| TensorNetworkDelete[tn, k] |
deletes the k-th tensor from the network |
| TensorNetworkDelete[tn, -1] |
deletes the last tensor |
TensorNetworkReplaceIndices
| Calling Form |
Description |
| TensorNetworkReplaceIndices[net, {i₁→j₁, i₂→j₂, …}] |
replaces indices according to rules |
| Calling Form |
Description |
| InitializeTensorNetwork[net, {t₁,t₂,…}] |
initializes a network graph with numerical tensors |
| Calling Form |
Description |
| ToTensorNetworkGraph[graph] |
constructs a tensor network graph from a directed acyclic graph |
| ToTensorNetworkGraph[tn] |
converts a TensorNetwork object to graph representation |
Options:
- Method → "Symbolic" — uses symbolic tensor representations
| Calling Form |
Description |
| TensorNetworkGraphQ[net] |
yields True if net is a valid tensor network graph |
| TensorNetworkGraphQ[net, True] |
prints diagnostic messages for invalid networks |
| Calling Form |
Description |
| TensorNetworkIndexGraph[net] |
returns a graph representing the index connectivity |
TensorNetworkRemoveCycles
| Calling Form |
Description |
| TensorNetworkRemoveCycles[net] |
inserts identity tensors to break cycles, making the graph acyclic |
| Calling Form |
Description |
| TensorNetworkToNetGraph[net] |
converts to a Wolfram Neural NetGraph object |
| Calling Form |
Description |
| TensorNetworkIndices[net] |
returns the list of index specifications for each tensor vertex |
| Calling Form |
Description |
| TensorNetworkTensors[net] |
returns the list of tensors stored in the network vertices |
| Calling Form |
Description |
| TensorNetworkGraphData[net] |
returns an Association with raw data (tensors, indices, dimensions, contractions) |
TensorNetworkIndexDimensions
| Calling Form |
Description |
| TensorNetworkIndexDimensions[net] |
returns an Association mapping each index to its dimension |
| TensorNetworkIndexDimensions[indices, tensors] |
computes dimensions from explicit lists |
| Calling Form |
Description |
| TensorNetworkFreeIndices[net] |
returns the list of uncontracted (free/open) indices |
Contraction & Path Optimization
Contracts the tensor network to a single tensor.
| Calling Form |
Description |
| TensorNetworkContract[tn] |
contracts using the default (optimal) path |
| TensorNetworkContract[tn, path] |
contracts using the specified contraction path |
| TensorNetworkContract[data, path] |
contracts using pre-computed network data |
Returns an evaluated numerical tensor (not symbolic).
TensorNetworkFindContractionPath
| Calling Form |
Description |
| TensorNetworkFindContractionPath[tn] |
computes an optimized contraction path |
| TensorNetworkFindContractionPath[tn, Method → "Greedy"] |
uses greedy optimization |
| TensorNetworkFindContractionPath[tn, Method → "Optimal"] |
finds the optimal path (slower for large networks) |
Returns a symbolic contraction expression.
| Calling Form |
Description |
| TensorNetworkContraction[tn, path] |
symbolic expression along the given path |
| TensorNetworkContraction[tn, treePath] |
uses a tree-structured contraction path |
| TensorNetworkContraction[tn, "Greedy"] |
automatically computes a greedy path |
| TensorNetworkContraction[tn, "Optimal"] |
automatically computes an optimal path |
Options:
- Method → "ArrayDot" — contraction method ("ArrayDotTranspose", "ArrayDot", "Dot", "TensorContract", "TableSum")
- "Inactive" → True — returns inactive expressions
| Calling Form |
Description |
| ContractionTree[tn] |
returns a Tree representing the contraction hierarchy |
| ContractionTree[tn, path] |
uses the specified contraction path |
$TensorNetworkContractionMethods
List of available contraction methods: "ArrayDotTranspose", "ArrayDot", "Dot", "TensorContract", "TableSum".
Matrix Product States (MPS)
Transforms an MPS into canonical form.
| Calling Form |
Description |
| MPSCanonicalForm[mps] |
left-canonical form (default) |
| MPSCanonicalForm[mps, "Left"] |
left-isometric form where A†·A = I |
| MPSCanonicalForm[mps, "Right"] |
right-isometric form where A·A† = I |
| MPSCanonicalForm[mps, {"Mixed", k}] |
mixed canonical form centered at site k |
| Calling Form |
Description |
| MPSCanonicalQ[mps, "Left"] |
checks left-canonical form |
| MPSCanonicalQ[mps, "Right"] |
checks right-canonical form |
| MPSCanonicalQ[mps, {"Mixed", k}] |
checks mixed-canonical form at site k |
Uses tolerance 10⁻¹⁰ for numerical comparison.
| Calling Form |
Description |
| MPSOverlap[mps₁, mps₂] |
computes the inner product ⟨Ψ₁|Ψ₂⟩ between two MPS |
The second MPS is conjugated. MPSOverlap[mps, mps] returns the squared norm.
| Calling Form |
Description |
| MPSNorm[mps] |
computes the norm ‖Ψ‖ of an MPS |
Equivalent to Sqrt[Abs[MPSOverlap[mps, mps]]].
| Calling Form |
Description |
| MPSNormalize[mps] |
returns an MPS normalized to unit norm |
The physical state is preserved; only the overall scale factor changes.
| Calling Form |
Description |
| MPSEntanglementEntropy[mps, site] |
von Neumann entropy S = -∑ p log(p) at bond between site and site+1 |
Returns 0 for product states, log(D) for maximally entangled states with bond dimension D. Uses natural logarithm.
| Calling Form |
Description |
| MPSSchmidtValues[mps, site] |
normalized Schmidt coefficients λᵢ at the bond between site and site+1 |
Values satisfy ∑ λᵢ² = 1. Number of values equals the bond dimension at that cut.
| Calling Form |
Description |
| MPSTruncate[mps, maxBond] |
compresses MPS by truncating all bonds to at most maxBond |
Result is in left-canonical form. Truncation error depends on discarded singular values.
Contracts tensors according to index specification.
| Calling Form |
Description |
| EinsteinSummation[{i₁,i₂,…} → out, {A₁,A₂,…}] |
contracts tensors according to index specification |
| EinsteinSummation[{i₁,i₂,…}, {A₁,A₂,…}] |
auto-determines output from indices appearing once |
| EinsteinSummation["ij,jk->ik", {A, B}] |
string notation with comma-separated indices |
| Calling Form |
Description |
| TensorJoin[{i₁,i₂,…}, {A₁,A₂,…}] |
joins tensors over shared indices, broadcasting along non-shared dimensions |
Returns {indices, tensor} where tensor is the joined result.
| Calling Form |
Description |
| ActivateTensors[expr] |
activates Inactive[TensorProduct], Inactive[TensorContract], and Inactive[Transpose] |
Converts symbolic tensor expressions to evaluated numerical tensors.
| Calling Form |
Description |
| GreedyPath[inputs, output, sizeDict, …] |
finds a contraction path using a greedy heuristic |
Arguments:
- inputs — list of index lists for each tensor
- output — list of indices for the final result
- sizeDict — Association mapping indices to dimensions
Optional: costMod, temperature, maxNeighbors, seed, simplify, useSSA
| Calling Form |
Description |
| OptimalPath[inputs, output, sizeDict, …] |
finds an optimal contraction path via dynamic programming |
Optional: minimize, costCap, searchOuter, simplify, useSSA
| Calling Form |
Description |
| ContractIndices[i, j] |
returns the indices that would be contracted between index sets i and j |
| Calling Form |
Description |
| TreePathToPath[treePath, indices] |
converts a tree-structured path to a linear (pairwise) path |
| Calling Form |
Description |
| PathToTreePath[path, indices] |
converts a linear path to a tree-structured path |
| Calling Form |
Description |
| CanonicalPath[path, indices] |
returns a canonical (normalized) representation of the path |
| Calling Form |
Description |
| PathIndexContractions[path, indices] |
returns the sequence of index sets contracted at each step |
Creating Specific Tensor Network Types
(* Matrix Product State: 10 sites, bond dimension 4, physical dimension 2 *)
mps = RandomTensorNetwork["MPS"[10, 4, 2]]
(* Tensor Train: 8 sites, bond dimension 3 *)
tt = RandomTensorNetwork["TT"[8, 3]]
(* Matrix Product Operator: 6 sites, bond dimension 3, physical dimension 2 *)
mpo = RandomTensorNetwork["MPO"[6, 3, 2]]
(* PEPS: 4×4 grid, bond dimension 3, physical dimension 2 *)
peps = RandomTensorNetwork["PEPS"[{4, 4}, 3, 2]]
(* Tree Tensor Network: depth 3, bond dimension 4, branching 2 *)
ttn = RandomTensorNetwork["TTN"[3, 4, 2]]
(* MERA: width 4, bond dimension 4, 2 layers *)
mera = RandomTensorNetwork["MERA"[4, 4, 2]]
(* Create and canonicalize an MPS *)
mps = RandomTensorNetwork["MPS"[10, 4, 2]]
canonical = MPSCanonicalForm[mps, "Left"]
(* Check canonicality *)
MPSCanonicalQ[canonical, "Left"] (* True *)
(* Compute norm and normalize *)
norm = MPSNorm[mps]
normalized = MPSNormalize[mps]
(* Compute entanglement entropy at site 5 *)
entropy = MPSEntanglementEntropy[normalized, 5]
(* Truncate to smaller bond dimension *)
truncated = MPSTruncate[mps, 2]
(* Matrix multiplication: C = A.B *)
A = RandomReal[1, {3, 4}];
B = RandomReal[1, {4, 5}];
C = EinsteinSummation[{{i, k}, {k, j}} -> {i, j}, {A, B}]
(* Trace: Tr[A] *)
M = RandomReal[1, {4, 4}];
trace = EinsteinSummation[{{i, i}}, {M}]
(* String notation *)
result = EinsteinSummation["ij,jk->ik", {A, B}]