TL;DR
A tensor is a grid of numbers with one or more index slots: a vector has one slot, a matrix has two. Tensor contraction takes two slots of the same size, sets their indices equal, and adds up the results over all their values. The number of indices drops by two. This single rule covers matrix products, dot products and traces.
Builds on: Tensor Product, Matrix Multiplication · Leads to: Tensor Network
Where it's used
- Quantum computing. A gate acting on one Qubit of a joint state is a contraction over the qubit's index. Tensor-network simulators store a large quantum state as a chain of small arrays and answer questions by contracting them, and the cost of each contraction decides which circuits can be simulated.
- Deep learning. The score that a Transformer attention layer assigns to a pair of tokens is a sum of products of their query and key vectors over the feature index. Computing those scores for every pair of tokens and every attention head is the same kind of contraction, written once for a whole batch.
- Physics papers. General relativity and continuum mechanics write their laws with tensors. A common check is that every term in an equation has the same free indices, which is a contraction bookkeeping rule applied to the whole equation.
How it works
Take two matrices, each with two index slots. Matrix A has rows (1, 2) and (3, 4). Matrix B has rows (5, 6) and (7, 8).
- Name the indices. Write each entry with its position: A has entries , where i is the row and j is the column. B has entries . The letter j appears in both, so it is the shared index. Check: A and B each have two slots.
- Sum over the shared index. Set the shared j equal on both sides and add over its values j = 1 and j = 2: . The indices i and k appear once, so they stay free and label the result. Check: two free indices means the result is a matrix.
- Compute the entry . Row 1 of A is (1, 2) and column 1 of B is (5, 7). So . Check: this is the dot product of row 1 with column 1.
- Compute the other three entries. , , and . Check: each entry uses one row of A and one column of B.
- Read the result. C has rows (19, 22) and (43, 50). This is exactly the usual matrix product, so contracting over the shared index is matrix multiplication. Check: the entries are the dot products of rows of A with columns of B.
- Contract a matrix with itself. Set both slots of A equal and sum: . This is the trace. Both indices were removed and nothing is left, so the result is one number. Check: the number of indices went from 2 to 0.
- Contract two vectors. For u = (1, 2) and v = (3, 4), set the single index equal on both and sum: . This is the dot product of two vectors. Check: each vector had one index, and both were removed.
flowchart LR T[Tensor with index slots] --> P[Pick two slots of equal size] P --> S[Set their indices equal and sum over all values] S --> R[Result has two fewer indices]
The math
Let be an array with index slots of sizes , and let slots and have the same size . The contraction of over slots and is the array whose entries are
with the index appearing in both slots and summed, and every other index left free. The result has indices.
A contraction is linear in the tensor: contracting a sum of tensors gives the sum of their contractions, and contracting a scaled tensor scales the result. Each index is named by a letter, and a letter that appears in exactly two slots of a term is summed. This convention is the Einstein notation described in the History section.
Steps 1 and 2 in symbols: the matrix product
In the index j is summed (the summation notation at work), and i and k are free. Written out for two-by-two matrices, . This is the matrix product, so matrix multiplication is one example of contraction.
Steps 3 and 4 in symbols: the numbers
The other entries follow the same pattern with the index pair changed: , , .
Step 6 in symbols: the trace
Here the same letter i fills both slots of one matrix, so the contraction is over that matrix's own two slots.
Step 7 in symbols: the dot product and the tensor product
The dot product of two vectors is . The tensor product from Tensor Product has two slots. Contracting those slots gives , which is the trace of M. For u = (1, 2) and v = (3, 4), the entries of M are , , and . Its trace is , the same as the dot product.
Worked examples
Let u = (1, 2) and v = (3, 4), two vectors. Find , then check the answer with the tensor product.
- Write the sum with the index i in both slots: . Since i is in both, it is summed.
- Put in the numbers: .
- Multiply each pair: .
- Add: the answer is 11. It is a single number, because both indices were removed.
- Check with the tensor product. Form , which gives , , , . Its trace matches. The dot product of two vectors is the trace of their tensor product.
A has rows (1, 2) and (3, 4). B has rows (5, 6) and (7, 8). Compute , then compute to see whether the order matters.
- Entry : sum over j of , which is .
- Entry : .
- Entry : .
- Entry : . So C has rows (19, 22) and (43, 50).
- Swap the roles: . Entry is .
- Entry is . Entry is . Entry is .
- Compare the two results. BA has rows (23, 34) and (31, 46), which is not C. The summed index is the same kind in both products, but the free indices sit in different slots, so the answer changes.
A two-qubit state is . Store its amplitudes as a two-by-two array , where a is the first qubit and b is the second. The bit-flip gate X on the first qubit is the array with , , and . Write , about 0.707.
- Store the state: , , , .
- Write the gate as a contraction: . The index c is the second slot of X and the first slot of , so it is summed. The indices a and b stay free. Check: the result has the same two free indices as .
- Compute .
- Compute .
- Compute .
- Compute .
- Read the new state: . Check: the squared amplitudes add to , so it is still a valid state.
Try it: apply X to the second qubit instead, so . Find .
Answer: , which is , about 0.707.
Common mistakes and tradeoffs
- It is tempting to think a tensor product and a contraction are opposite kinds of "combining". In fact a tensor product keeps every index and adds the counts (two vectors with one index each give a tensor with two), while a contraction removes two indices (a matrix becomes a single number). Because the index count changes in opposite directions, check it at every step.
- It is tempting to think you can sum over an index that appears only once. In fact a summed index must appear in exactly two slots. An index that appears once is free and stays in the answer. An index that appears three times has no clear meaning without an extra rule, so avoid it.
- It is tempting to think the order of matrices in a contraction does not matter. In fact matrix products are not commutative, as Example 2 shows, because the free indices land in different slots. Keep track of which matrix carries i and which carries k.
- The contracted slots must have the same length. Contracting the column slot of a 2 by 3 matrix (length 3) with a slot of a 2 by 2 matrix (length 2) is undefined, because the two lengths differ.
When to use it: when several arrays share indices and you want the index pattern to say what is being computed, such as batched products, attention scores, and multi-index physics formulas. Many numerical libraries accept the pattern as text, which is convenient for these cases.
When not to use it: for a single matrix product or dot product, ordinary matrix notation is clearer. For long formulas, the index bookkeeping invites mistakes.
Cost: contracting many large arrays in a poor order can take far more work than the answer needs. Tensor-network methods spend effort choosing a cheap order. Alternatives are plain matrix notation, explicit loops (slow but easy to read), and an einsum-style function that can search for a cheaper order.
History
The tensor calculus that gave this operation its notation was built to describe curved spaces. Gregorio Ricci-Curbastro and Tullio Levi-Civita published their account of the absolute differential calculus around 1900, where manipulating indices was already the core technique. In 1916 Albert Einstein used a summation convention in his paper on general relativity: a letter repeated in a term means a sum over it. This is why the convention is called Einstein notation, and why many physics papers write equations without the summation signs. Later, numerical computing turned the same index patterns into function calls, which is how most programmers now meet contraction.
Recap
- A tensor has index slots: a vector has one, a matrix has two.
- A contraction picks two slots of equal size, sets their indices equal, and sums over all their values.
- The result has two fewer indices. Matrix products, traces and dot products are all contractions.
- Contracting the tensor product of two vectors gives their dot product, which ties contraction back to Tensor Product.
- Read next: Tensor Network, which stores quantum states as networks of small arrays and contracts them, and Trace of a Matrix for the trace in more depth.