Guide

  1. Circuits as tensor networks
  2. Penrose notation used here
  3. Boundary conditions
  4. Contraction cost and memory
  5. Contraction strategies
  6. Controls and shortcuts
  7. Supported OpenQASM
  8. Implementation notes

1. Circuits as tensor networks

A tensor is a multi-dimensional array, drawn as a shape with one leg per index. A k-qubit gate is a unitary 2k×2k matrix. Reshaped, it becomes a tensor with k input and k output legs, each of dimension 2. Connecting two legs means summing over that shared index. A whole circuit acting on |0…0⟩ is therefore a network of such tensors joined along the qubit wires:

ψo1…on = Σinternal indices ∏tensors T(its legs)

Contracting the network means evaluating this sum. It is done one pair of tensors at a time: two tensors A and B are replaced by C = Σshared A·B, whose legs are the union of the non-shared legs of A and B. The final result does not depend on the order of the pairs, but the cost depends on it dramatically. That dependence is what the applet lets you explore.

2. Penrose notation used here

With Penrose split of controlled gates enabled (the default), a controlled-U gate is drawn through its operator-Schmidt decomposition CU = Σk∈{0,1} |k⟩⟨k| ⊗ Uk. The control becomes a COPY tensor δo,i,k, joined by a purple bond index k to a rank-3 target To,i,k = (Uk)o,i. For CX the target is the XOR tensor ⊕. A SWAP is simply a crossing of wires and has no tensor. With the option off, every gate is a single box of rank 2k.

Bell circuit, split (COPY–XOR)
Same circuit, gates as boxes

3. Boundary conditions

ModeNetworkResult
Output state|0⟩ on each input, output legs left openrank-n tensor = state vector |ψ⟩ (2n amplitudes)
Amplitudeoutputs capped by ⟨bq| (bitstring b, q[0] first)scalar ⟨b|U|0…0⟩
ExpectationU, then a layer of Paulis P, then U† (reversed, adjoint gates), capped by ⟨0|scalar ⟨ψ|P|ψ⟩

Pauli strings can be dense (ZZIX) or sparse (Z0 Z1 X3). Closing the network turns the result into a scalar, and the cheapest order is then often much cheaper than producing the full state vector.

Expectation network ⟨ψ|Z⊗Z|ψ⟩ for the Bell circuit

4. Contraction cost and memory

Contracting A (legs a∪s) with B (legs s∪b) over the shared legs s is a matrix product of size (|a|×|s|)·(|s|×|b|):

cost = ∏e ∈ a∪s∪b dim(e) = 2|a|+|s|+|b| complex multiply-adds   (≈ 8× real FLOPs)
memory(C) = 2|a|+|b| × 16 bytes

The statistics panel accumulates the total cost and tracks the largest intermediate, which is the quantity that limits simulations in practice. Its log2 (the rank of the largest tensor) is the contraction width of the chosen path. In the cost-profile chart the bars show log2 of each step's result size and the line shows the cumulative cost on a log scale. Contracting two tensors with no shared index (an outer product) is allowed, but usually expensive.

5. Contraction strategies

StrategyRuleTypical behaviour
Greedy (min. size growth)among all connected pairs pick the one minimising size(C) − size(A) − size(B), the same heuristic as opt_einsum's greedygood general default
Greedy (min. step cost)pick the pair with the cheapest contraction right nowvery cheap early steps, can paint itself into a corner
Time sweep (Schrödinger)absorb tensors left to right into one growing tensorthe growing tensor is the state vector, so memory is about 2n
Qubit-by-qubit sweepabsorb tensors row by row (qubit 0 first)bad for deep circuits, where width grows with depth (Feynman-like)
Randomrandom connected paira baseline for comparison

Enable suggest next pair to see (green dashed rings) which pair the selected strategy would contract next, then decide yourself.

6. Controls and shortcuts

ActionHow
Contract two tensorsclick one (it gets a blue ring), then click the other. Hover the second one first to preview the cost
Contract along an indexclick the edge
Inspecthover any tensor or edge
Step / play / allSpace / P / A
Undo / resetZ or Backspace / R, or click a step in the path list to rewind
Zoom / pan / fitmouse wheel or +- / drag the background / F
Rebuild from editorCtrl+Enter in the QASM editor
Load a file“Load .qasm” or drag & drop onto the canvas
ExportSVG (the diagram as currently shown) and Path JSON (tensors, contraction sequence, costs)

URL parameters preload a configuration, for example app.html?example=pauli&n=6&terms=10&mode=expectation&pauli=Z0 Z1. Other keys are bits, decompose=0, strategy, plus any example parameter (steps, J, h, dt, seed, …).

7. Supported OpenQASM

8. Implementation notes