Summa All packages GitHub

Summa

Fast Einstein summation

The syntax of numpy.einsum, indices repeated at will, optimal contraction orders, tensor-network diagrams, in the Wolfram Language

What it does

Summa contracts tensors with the syntax of numpy.einsum: you write a contraction as on paper, the indices of every factor, "ij,jk->ik", and Summa works out how to compute it — the cheapest order of the pairwise contractions and the kernel of each, BLAS or vectorised, with no transposes, reshapes or intermediate arrays by hand. An index may appear in any number of tensors — diagonals, traces, hyperedges, batch indices — and the network can be drawn as it is computed.

  • Written as on paper, computed for youThe string syntax of numpy, "ij,jk->ik", with implicit output, ellipsis and broadcasting, Unicode labels, or lists of labels of any kind; how to compute it is Summa's job.
  • Indices repeated at willDiagonals, traces and sums, and an index shared by three or more tensors (hyperedges, batch indices) in a single call.
  • The cheapest orderThe order of the pairwise contractions minimises the flops (an exact search up to eight tensors, greedy beyond); EinsumPath gives its cost and its speed-up over the naive sum.
  • Fast kernelsEach contraction runs on BLAS Dot, on batched kernels or on broadcasting, with as few transposes as possible; plans are cached, and EinsumFunction precompiles an einsum for loops.
  • The network, drawnEinsumDiagram draws the tensor network — bonds, open legs, traces, hyperedges, the order of the contractions — and EinsumTree the contraction tree with the cost of every step.
  • Any entriesMachine numbers, exact and symbolic entries, complex numbers, SparseArray.

Every result is checked against numpy.einsum, and the optimal order against an exhaustive search of all orders.

A first example

One session, from a trace to a tensor network:

Needs["Summa`"]

Indices as on paper. A repeated index is summed: the trace of a matrix, and its square.

m = {{a, b}, {c, d}};
{Einsum["ii", m], Einsum["ij,jk->ik", m, m]}

An index in any number of tensors. ResourceFunction["EinsteinSummation"] refuses an index that appears three times, and TensorContract sums one only after building the whole outer product. In Summa an index may appear in any number of tensors: here i is shared by three vectors, :

Einsum["i,i,i->", {a, b}, {c, d}, {e, f}]

and in "ai,bi,ci->abc" by three matrices, the reconstruction of a tensor from its canonical decomposition: a hyperedge, drawn by EinsumDiagram.

EinsumDiagram["ai,bi,ci->abc"]
The tensor network of ai,bi,ci->abc: three tensors A, B, C, each with an open leg a, b, c, joined by the hyperedge i, drawn as a purple dot

A Kubo formula, as it is written. The optical conductivity of a two-band chain, , in which , and each appear in three tensors: one einsum, with no loop and no intermediate array written by hand.

h[k_] := (1 + 0.6 Cos[k]) PauliMatrix[1] + 0.6 Sin[k] PauliMatrix[2] + 0.4 PauliMatrix[3];   (* the Rice-Mele chain *)
ks = Most[Subdivide[-Pi, Pi, 400]]; ws = Subdivide[0.5, 4., 350];
{e, u} = Transpose[Eigensystem /@ (h /@ ks)];
current = MapThread[Conjugate[#1] . (D[h[q], q] /. q -> #2) . Transpose[#1] &, {u, ks}];
f = UnitStep[-e];   (* half filling *)
weight = Table[If[n == m, 0, (f[[q, n]] - f[[q, m]])/(e[[q, m]] - e[[q, n]])/(w - e[[q, m]] + e[[q, n]] + 0.05 I)],
   {q, 400}, {n, 2}, {m, 2}, {w, ws}];
sigma = I/400 Einsum["knm,kmn,knmw->w", current, current, weight];
ListLinePlot[Transpose[{ws, Re[sigma]}], AxesLabel -> {"\[Omega]", "Re \[Sigma]"}]
The real part of the optical conductivity of the Rice-Mele chain against the frequency: two peaks at the edges of the interband transitions, near 1.2 and 3.3

A tensor network. The norm of a matrix-product state of four sites, the state above and its conjugate below:

mps = "pa,aqb,brc,cs,pA,AqB,BrC,Cs->";
EinsumDiagram[mps]
The tensor network of the norm of a matrix-product state: two rows of four tensors, joined along each row by the bonds a, b, c and A, B, C, and between the rows by the physical indices p, q, r, s

The order matters. With bonds of dimension 32, the order that Summa chooses costs flops, the naive sum :

dims = {{2, 32}, {32, 2, 32}, {32, 2, 32}, {32, 2}, {2, 32}, {32, 2, 32}, {32, 2, 32}, {32, 2}};
Lookup[EinsumPath[mps, dims], {"Flops", "NaiveFlops", "Speedup"}]
EinsumTree[mps, dims]
The contraction tree of the norm of the matrix-product state: each half closed site by site, 4096 and 512 flops per step, then the two halves joined into a scalar

Fast in loops. EinsumFunction plans once for fixed shapes: here a thousand products of 4×4 matrices, a batched kernel instead of a thousand calls to Dot.

SeedRandom[1]; {x, y} = RandomReal[1, {2, 1000, 4, 4}];
batch = EinsumFunction["bij,bjk->bik", {{1000, 4, 4}, {1000, 4, 4}}];
batch[x, y] == MapThread[Dot, {x, y}]
True

Next. The guided tour goes through the syntax, repeated indices, orders and kernels step by step; the wiki has the recipes; the showcase shows what the package can do and the manual describes every function. ?Summa`* lists the functions and ?Name explains each one.

Installation

Install the paclet of the latest release once:

PacletInstall["https://github.com/n4sd4q/Summa/releases/download/v1.0.0/Summa-1.0.0.paclet"]

and load it in every session:

Needs["Summa`"]

There are no dependencies; the package needs Mathematica 12.1 or later. The guided tour, a notebook to run cell by cell, comes with the paclet:

NotebookOpen[PacletObject["Summa"]["AssetLocation", "Examples"]]

In a notebook the package looks for a newer release on GitHub once a day; $SummaUpdateCheck = False before Needs turns this off (SECURITY.md says more). Other ways to install, and updating: the wiki's Installation page.

Checks

The tests (wolfram -script Tests/RunTests.wls, 735 tests) compare Einsum with numpy.einsum on 213 contractions computed in exact integer arithmetic, every one with every order and every kernel, and with an explicit sum in machine precision, real and complex; the optimal order must cost as little as an exhaustive search of all orders on 40 networks. sh Development/check_docs.sh runs the code of the showcase, the manual, the guided tour, the README and the wiki again, in fresh kernels, and compares it with what they print.

Related software

numpy.einsum defines the syntax, and opt_einsum the search of the contraction order; TensorOperations.jl (Julia) and cotengra (Python, large networks) do the same in other languages. In Mathematica, TensorContract and ResourceFunction["EinsteinSummation"] contract pairs of indices. Summa brings the syntax of numpy to the Wolfram Language with indices shared by any number of tensors, the optimal order, BLAS and batched kernels, and the drawing of the network.

How to cite

If you use Summa in published work, please cite it (CITATION.cff):

@software{africani_summa,
  author  = {Africani, Nazzareno},
  title   = {{Summa}: fast Einstein summation (einsum) for the Wolfram Language},
  version = {1.0.0},
  year    = {2026},
  url     = {https://github.com/n4sd4q/Summa}
}