Tensor networks and contraction complexity
Count dense contraction operations and intermediate storage, showing why a compact network still needs a carefully chosen evaluation order.
On this page
The question for today
What you’ll understand
- Translate a shared-index contraction into a matrix multiplication cost.
- Compare two exact contraction orders with explicit arithmetic and intermediate sizes.
- Explain how boundary width, bond dimensions, and requested outputs govern computation.
A tensor network is a formula of local arrays connected by summed indices. Writing the formula compactly does not specify an efficient way to evaluate it. The same exact contraction can create either a tiny intermediate or a large dense array depending on the order of operations. We will count classical scalar multiplications for a concrete chain, distinguish them from memory and elapsed time, and then connect the example to the open-index boundaries that govern larger tensor networks.
A contraction has input, summation, and output dimensions
Suppose two real tensors share indices of combined size k, while their remaining indices have combined sizes m and n. After consistent reshaping, contracting all the shared indices is multiplication of an m-by-k matrix by a k-by-n matrix. The elementary dense algorithm uses mkn scalar multiplications and mn(k − 1) additions. When k is large, a common leading floating-point count is approximately 2mkn, but that convention counts both operation types.
The output contains mn entries, independently of how many arithmetic operations were required. Its storage is only one part of memory use: existing inputs, workspace, and other live intermediates also matter. These counts assume dense arrays and the classical multiplication algorithm; sparsity, symmetry, low-rank blocks, or faster matrix multiplication may change them. Comparing plans is meaningful only under a shared cost model.
Contraction order changes intermediate boundaries
Consider a network region already contracted into one intermediate tensor. Its remaining indices are exactly the edges crossing the region’s boundary, together with requested physical output indices. If every such index has dimension χ and there are w of them, the dense intermediate has χʷ entries. For unequal dimensions, replace that power by the product of boundary dimensions. The sequence of boundaries induced by an ordering controls its memory growth.
This explains why tree-like or narrow-width structures can be tractable when bond dimensions stay bounded, whereas a poor ordering can expose a large boundary. Graph-width methods formalize the relation between network geometry and contraction complexity, with precise graph and dimension conventions. We use the boundary-product count directly rather than asserting that the number of local tensors alone determines the cost. Even a short matrix chain already exhibits a substantial ordering effect.
Representation, evaluation, and approximation are separate costs
A tensor train with bounded ranks can evaluate one entry by multiplying its selected core slices without forming the full tensor. Contracting an inner product of two trains also benefits from a structured sequence of small environments. But requesting every dense entry still has a product-of-mode-sizes output cost. A compact representation removes unnecessary materialization only when the requested computation can exploit that representation.
Replacing an intermediate with a low-rank approximation can further reduce computation, but it changes the result and needs an error budget. Exact reordering changes only evaluation order, apart from floating-point rounding; truncation introduces approximation error as well. Finally, fewer arithmetic operations need not translate proportionally into wall-clock speed because memory traffic, parallelism, and kernel sizes matter. Operation counts and roofline ceilings inform different parts of that performance assessment.
Work it through
Two orders for one matrix-chain network
Let A have size 100×2, B size 2×100, and C size 100×2. Compare the scalar multiplication counts and the newly created internal intermediate sizes for (AB)C and A(BC). Exclude the shared final output and original inputs from the intermediate comparison.
For (AB)C, first form AB of size 100×100. This uses 100×2×100 = 20,000 multiplications and creates 10,000 internal entries.
Multiplying that result by C uses another 100×100×2 = 20,000 multiplications, totaling 40,000. The final output has size 100×2 in either ordering.
For A(BC), first form BC of size 2×2 with 2×100×2 = 400 multiplications, then multiply A by it using 100×2×2 = 400. The total is 800 and the sole internal intermediate has four entries.
The second ordering uses 50 times fewer classical multiplications and 2,500 times fewer entries in its internal intermediate. This does not claim a 50-fold measured runtime speedup.
The living notebook
Explore the model
Interactive lab · roofline
Arithmetic counts and bandwidth ceilings answer different questions
For each run Pause and Reset, then set both controls. Choose operational intensity I = 2 FLOP/byte and bandwidth B = 50 GB/s; predict a 100 GFLOP/s ceiling, below the fixed 1000 GFLOP/s compute roof. In a fresh run use I = 20 and B = 50; the ceiling reaches 1000 at the ridge intensity 20. These are selected hypothetical kernel intensities, not measured values for the worked contraction orders.
- Attainable ceiling
- 640 GFLOP/s
- Ridge intensity
- 12.5 FLOP/byte
- Ceiling regime
- Memory-bound ceiling
Synthetic roofline ceiling min(1000, B·I), in GFLOP/s, with B in decimal GB/s and I in FLOP/byte. Playback traces the roof without changing the selected kernel. This upper-bound model is not a measured throughput prediction, hardware comparison or price model. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forA contraction plan’s arithmetic count and its memory behavior must both be considered before predicting execution time.
Pause & reason
Check your understanding
Your turn
Try it yourself
Let A be 10×100, B be 100×5, and C be 5×50. Compare classical multiplication counts for both orders and count each internal intermediate, excluding original inputs and the final output.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Use mkn at each multiplication and distinguish the first intermediate’s shape from the final 10×50 result.
Compare with the solution
For (AB)C, AB has size 10×5 and costs 10×100×5 = 5000 multiplications. The second multiplication costs 10×5×50 = 2500, totaling 7500, with fifty internal entries. For A(BC), BC has size 100×50 and costs 100×5×50 = 25,000; the second multiplication costs 10×100×50 = 50,000, totaling 75,000, with 5000 internal entries. Thus the first order uses ten times fewer multiplications and a hundred times fewer internal entries. Both have the same five-hundred-entry final output; neither internal count alone is the total live-memory peak.
Keep the idea
Take it with you
- Dense contraction cost depends on the shared and external index products.
- Evaluation order controls intermediate boundaries and can dominate arithmetic and storage.
- Compact representation, exact contraction, approximation error, and hardware performance are distinct parts of the computation.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.