Simple Algorithm
Deep Learning · Emergence · 30 min

What Transformers Can Compute

Circuit complexity, formal languages and the extra power of chain of thought.

A transformer with a fixed number of layers does a fixed amount of sequential work per output, however long its input. Circuit complexity turns that observation into theorems: depending on how attention and arithmetic are idealized, constant-depth transformers sit inside the classes AC0\mathsf{AC}^0 or TC0\mathsf{TC}^0, which separates problems they can solve in parallel from problems that seem to need serial steps. The same theory explains what depth and chain of thought buy, and how compactly transformers describe what they can recognize.

What you’ll learn

  • Define AC0\mathsf{AC}^0, TC0\mathsf{TC}^0 and NC1\mathsf{NC}^1, and place standard problems (parity, majority, sorting, permutation composition, formula evaluation) among them.
  • Sketch why unique hard attention stays in AC0\mathsf{AC}^0 while log-precision softmax attention needs threshold gates and lands in uniform TC0\mathsf{TC}^0.
  • Derive the consequences under standard conjectures, and explain how logarithmic depth and chain of thought add serial computation.
  • State the succinctness results for hard-attention transformers and what they imply for verification.
  • Use RASP to write transformer programs, and separate what transformers can express from what training finds.

Transformers as language recognizers

To ask what a transformer can compute, fix the model and vary the input length nn. A transformer recognizes a language L⊆Σ∗\mathcal L\subseteq\Sigma^* if, for every nn, it reads a string of length nn and accepts exactly the strings in L\mathcal L, for example by thresholding the output at one position. The weights are fixed; only nn grows. Two idealizations decide the answers:

  • Attention. Soft attention is the usual softmax. Unique hard attention (UHAT) puts all weight on one maximizing position; average hard attention (AHAT, also called saturated) averages uniformly over all maximizing positions.
  • Precision. Real hardware has fixed precision, but the theory usually allows O(log⁡n)\order{\log n} bits per number: enough to write down a position index, and enough for an average over nn positions not to underflow.

Results without these limits are of a different kind. With positional encodings, transformers approximate any continuous sequence-to-sequence function on a compact domain (Yun et al., 2020), and with hard attention, unbounded precision and unboundedly many decoding steps they are Turing complete (Pérez, Barceló & Marinković, 2021). Such results allow the size of the network or its precision to grow with the input. This lesson keeps the network fixed and asks what changes with nn.

VariantAttentionPrecisionUpper bound
UHATone maximizing positionO(log⁡n)\order{\log n}AC0\mathsf{AC}^0 (Hao, Angluin & Frank, 2022)
AHAT (saturated)uniform average over maximizersO(log⁡n)\order{\log n}TC0\mathsf{TC}^0 (Merrill, Sabharwal & Smith, 2022)
SoftmaxsoftmaxO(log⁡n)\order{\log n}L-uniform TC0\mathsf{TC}^0 (Merrill & Sabharwal, 2023)
SoftmaxsoftmaxconstantAC0\mathsf{AC}^0 (Li, Liu, Zhou & Ma, 2024)
Bounds for a constant number of layers, a constant width and a single forward pass (no chain of thought). The models differ in idealization, not in architecture.

Circuit classes, plainly

AC0 ⊊ TC0 ⊆ NC1 ⊆ L ⊆ P\mathsf{AC}^0\ \subsetneq\ \mathsf{TC}^0\ \subseteq\ \mathsf{NC}^1\ \subseteq\ \mathsf L\ \subseteq\ \mathsf P
Only the first inclusion is known to be strict: PARITY and MAJORITY are not in AC0\mathsf{AC}^0 (Furst, Saxe & Sipser, 1984; Ajtai, 1983; with exponential lower bounds by Håstad, 1986), but both are in TC0\mathsf{TC}^0. That TC0≠NC1\mathsf{TC}^0\ne\mathsf{NC}^1 is widely believed and unproven.
ClassTypical membersIntuition
AC0\mathsf{AC}^0pattern matching with a fixed number of quantifiers; adding two numbers; recognizing star-free regular languageslook for things in parallel, but never count exactly
TC0\mathsf{TC}^0PARITY, MAJORITY, counting, iterated addition, multiplication, division, sorting nn numbersparallel counting and arithmetic
NC1\mathsf{NC}^1-completecomposing nn permutations of five objects (the word problem of S5S_5; Barrington, 1989); evaluating Boolean formulasproblems that seem to need log⁡n\log n rounds of combining
P\mathsf P-completecircuit value; feasibility of linear equalities with sign constraints on the variables (a form of linear programming); membership in an arbitrary context-free grammar with empty productionsapparently inherently sequential
"Complete" means every problem in the class reduces to it with simple reductions, so placing a complete problem in a smaller class would collapse the two.

Hard attention in AC⁰, softmax in TC⁰

DerivationProof sketch: where the threshold gates come from
  1. f:{0,1}clog⁡n→{0,1}c′log⁡n⟹a lookup circuit of size poly⁡(n), depth 2f:\{0,1\}^{c\log n}\to\{0,1\}^{c'\log n}\quad\Longrightarrow\quad\text{a lookup circuit of size }\poly(n),\ \text{depth }2

    Local work is free. Embeddings, feed-forward layers, normalizations, exp⁡\exp and division act on a constant number of numbers of O(log⁡n)\order{\log n} bits each. Any function of clog⁡nc\log n input bits has a truth table of ncn^c rows, so a depth-2 circuit of polynomial size computes it.

  2. selj=⋀l[sj≥sl],z=⋁j(selj∧vj)\text{sel}_j=\bigwedge_{l}\big[s_j\ge s_l\big],\qquad\mathbf z=\bigvee_j\big(\text{sel}_j\wedge\mathbf v_j\big)

    Hard attention only compares. Unique hard attention selects the position jj with the largest score. "Is jj the selected position?" is an AND, over all ll, of comparisons between O(log⁡n)\order{\log n}-bit numbers (with ties broken by position), and each comparison is in AC0\mathsf{AC}^0. Reading the selected value is an OR over positions. Nothing is ever counted, so a constant number of layers stays in AC0\mathsf{AC}^0:

  3. ∑j=1nxj  (xj∈{0,1}O(log⁡n)) ∈ TC0∖AC0\sum_{j=1}^{n}x_j\ \ (x_j\in\{0,1\}^{\order{\log n}})\ \in\ \mathsf{TC}^0\setminus\mathsf{AC}^0

    Softmax sums over all positions. The output ∑jesjvj/∑jesj\sum_je^{s_j}\mathbf v_j/\sum_je^{s_j} needs the sum of nn numbers of O(log⁡n)\order{\log n} bits. Iterated addition is in TC0\mathsf{TC}^0 but not in AC0\mathsf{AC}^0, since it includes counting the 11s of a bit string, and counting decides PARITY and MAJORITY. Averaging hard attention needs the same sum. With constant precision only constantly many values are representable, and Li et al. (2024) show that the rounded sums can then be computed without threshold gates, which is why that variant falls back to AC0\mathsf{AC}^0:

  4. L layers ⟹ depth O(L)=O(1)L\ \text{layers}\ \Longrightarrow\ \text{depth}\ \order{L}=\order{1}

    Layers compose. Each layer is a constant-depth circuit of polynomial size, and a constant number of layers composes to one. The circuits are built by a log-space procedure from nn, which gives uniformity. A sharper statement: log-precision transformers can be expressed in first-order logic with majority quantifiers, FO(M)\mathsf{FO(M)}, a logical description of uniform TC0\mathsf{TC}^0 (Merrill & Sabharwal, 2023, NeurIPS).

Exact characterizations. For hard attention the upper bounds can be matched by logics. Unique-hard-attention encoders recognize every language definable in first-order logic with arbitrary unary numerical predicates, a rich fragment of AC0\mathsf{AC}^0 (Barceló, Kozachinskiy, Lin & Podolskii, 2024). With strict causal masking and no position embeddings, masked hard-attention transformers recognize exactly the star-free regular languages, those definable in linear temporal logic (Yang, Chiang & Angluin, 2024). So masked hard-attention transformers without position embeddings cannot recognize even (aa)∗(aa)^*, the strings of even length, which needs counting modulo 2 and is not star-free.

Depth, serial steps and chain of thought

Combine the upper bound with the table of classes. If TC0≠NC1\mathsf{TC}^0\ne\mathsf{NC}^1, no constant-depth log-precision transformer composes nn permutations of five objects or evaluates Boolean formulas for all nn. If L≠P\mathsf L\ne\mathsf P (L-uniform TC0\mathsf{TC}^0 lies inside L\mathsf L), none solves the P\mathsf P-complete problems in the table, such as sign-constrained linear equalities or membership in arbitrary context-free grammars with empty productions (Merrill & Sabharwal, 2023). They call this the parallelism trade-off: any architecture as parallelizable as the transformer inherits limits of this kind. State space models are no escape; Merrill, Petty and Sabharwal (2024) place common SSMs in TC0\mathsf{TC}^0 as well.

Depth as serial computation. If constant depth is not enough, how much is? Liu et al. (2023) showed that a transformer with O(log⁡T)\order{\log T} layers can simulate any finite automaton for TT steps, by composing state transitions in a balanced tree (a parallel prefix over the automaton's transformation semigroup), and that O(1)\order{1} layers suffice when the automaton's groups are solvable, by Krohn–Rhodes theory. Gradient training found such shortcuts on their synthetic tasks, but the shortcuts were brittle out of distribution. Sanford, Hsu and Telgarsky (2024) proved that a constant number of attention layers simulates, and is simulated by, a constant number of rounds of massively parallel computation; for the kk-hop induction head task, following a chain of kk in-context lookups, depth Θ(log⁡k)\Theta(\log k) is both sufficient and necessary under a standard conjecture, and in their experiments the largest learnable kk grew exponentially with depth.

Predict before you look

Each position stores a pointer to another position, and the task is to follow the chain k=1024k=1024 hops. In one layer, every position can read the pointer stored where its own pointer leads, composing its pointer with itself; after ℓ\ell layers it knows its 2ℓ2^\ell-th successor. How many layers are needed?

Show the answer

10 layers

2ℓ≥10242^\ell\ge1024 gives ℓ=10\ell=10, against 10241024 steps for a recurrent network that follows one hop per step. Pointer doubling is the reason Θ(log⁡k)\Theta(\log k) layers suffice, and the lower bound says a transformer cannot do much better.

Chain of thought. Each generated token feeds back as input, adding one round of serial computation. With t(n)t(n) intermediate steps, under their idealizations, decoders gain little from O(log⁡n)\order{\log n} steps, recognize all regular languages with O(n)\order{n} steps, and recognize exactly the languages in P\mathsf P with polynomially many (Merrill & Sabharwal, 2024). Li, Liu, Zhou and Ma (2024) give a circuit-size version: constant-depth, constant-precision transformers lie in AC0\mathsf{AC}^0 without chain of thought, but with TT steps they solve any problem that Boolean circuits of size TT solve. Feng et al. (2023) show the same split for arithmetic expressions, linear equations and dynamic programming. The Artificial Intelligence course's lesson Reasoning in Language Models builds the S5S_5 construction step by step.

Check your understanding

Assume TC0≠NC1\mathsf{TC}^0\ne\mathsf{NC}^1. Which task can a fixed-depth, log-precision transformer not solve for all input lengths in one forward pass, although a chain of thought of nn steps makes it easy?

  1. Deciding whether a bit string contains more 1s than 0s
  2. Sorting a list of nn numbers of O(log⁡n)\order{\log n} bits
  3. Composing nn given permutations of five objects
  4. Checking whether a bit string has an even number of 1s
Show the answer

C. Composing nn given permutations of five objects

The word problem of S5S_5 is NC1\mathsf{NC}^1-complete (Barrington, 1989), so it lies outside TC0\mathsf{TC}^0 unless the classes are equal. Writing the running product as tokens turns it into nn constant-size lookups. Majority, sorting and parity are all in TC0\mathsf{TC}^0.

Succinctness, programs and learnability

Succinctness. Expressivity asks which languages a model can recognize; succinctness asks how large a description must be. Bergsträßer, Cotterell and Lin (2026) studied masked unique-hard-attention transformers with fixed-precision numbers, which recognize only star-free languages. Yet within that class they are remarkably compact:

The constructions use languages built from a binary counter with 2n2^n cells (tilings of exponentially wide rows), whose shortest accepted strings have length at least 22Ω(n)2^{2^{\Omega(n)}}. Two lessons follow. Compactness is a second axis of power: attention can refer to far-away positions by content and index arithmetic, so it can describe in polynomial size what an automaton must spell out state by state. And compactness has a price: the verification of even simple properties of such transformers is provably intractable in the worst case.

Programs. RASP (Weiss, Goldberg & Yahav, 2021) is a programming language whose primitives compile to attention: select builds a selector, a matrix of which query positions may read which key positions; aggregate averages values over the selected positions; selector_width counts them.

text
# built-in sequences: tokens, indices, length
# histogram: how many tokens equal the token at each position?
same     = select(tokens, tokens, ==)          # query i may read key j if tokens[j] == tokens[i]
hist     = selector_width(same)                 # count of selected positions, per query
# reverse a sequence
opp      = length - indices - 1
flip     = select(indices, opp, ==)              # query i reads key j = length - i - 1
reverse  = aggregate(flip, tokens)               # average over one selected position: tokens[j]

Each select–aggregate pair is one attention head; elementwise operations are feed-forward layers. Weiss et al. found that transformers with as many layers and heads as a RASP program compiles to learned the task, while for most tasks smaller ones did markedly worse; the Tracr compiler (Lindner et al., 2023) turns RASP programs into actual transformer weights.

What training finds. Expressivity results are existence proofs. Zhou et al. (2024) proposed the RASP-Generalization Conjecture: decoder-only transformers trained on short inputs tend to generalize to longer ones when the task has a short program, valid for every length, in RASP-L, a restricted RASP that limits how positions may be used. Tasks with short RASP-L programs, such as counting, sorting and copying with unique tokens, length-generalized in their experiments; addition and parity did not, until the input and scratchpad formats were changed so that short programs existed. Hahn and Rofin (2024) proved a complementary obstacle: transformers representing highly sensitive functions such as PARITY sit in isolated, very sharp minima, and small perturbations turn them into much less sensitive functions, which biases training toward low sensitivity.

Key takeaways

  • AC0⊊TC0⊆NC1⊆L⊆P\mathsf{AC}^0\subsetneq\mathsf{TC}^0\subseteq\mathsf{NC}^1\subseteq\mathsf L\subseteq\mathsf P: PARITY and MAJORITY separate the first two; S5S_5 composition and formula evaluation are NC1\mathsf{NC}^1-complete; strictness beyond AC0\mathsf{AC}^0 is conjectured.
  • Unique hard attention only compares and selects, so it stays in AC0\mathsf{AC}^0; log-precision softmax needs the sum over nn positions, so constant-depth transformers land in uniform TC0\mathsf{TC}^0, and constant precision falls back to AC0\mathsf{AC}^0.
  • Under TC0≠NC1\mathsf{TC}^0\ne\mathsf{NC}^1 or L≠P\mathsf L\ne\mathsf P, fixed-depth transformers cannot solve inherently serial problems in one pass. Depth O(log⁡T)\order{\log T} simulates any automaton, Θ(log⁡k)\Theta(\log k) layers follow kk hops, and chain-of-thought steps add serial rounds up to all of P\mathsf P.
  • Masked hard-attention transformers are succinct: polynomial size can require exponentially larger LTL formulas or RNNs and doubly exponentially larger automata, and verifying them is EXPSPACE\mathsf{EXPSPACE}-complete.
  • RASP writes transformer programs; the RASP-Generalization Conjecture links short RASP-L programs to length generalization, and sensitivity results explain why expressible functions such as PARITY are hard to learn.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

Constant-depth softmax transformers whose numbers have O(log⁡n)\order{\log n} bits recognize only languages in which class?

  1. AC0\mathsf{AC}^0
  2. Uniform TC0\mathsf{TC}^0
  3. NC1\mathsf{NC}^1, and no smaller class is known
  4. All of P\mathsf P
Show the answer

B. Uniform TC0\mathsf{TC}^0

Every layer is a constant-depth threshold circuit: lookups for the local operations, and iterated addition for the softmax sums (Merrill & Sabharwal, 2023). Chain of thought, not a single pass, is what reaches P\mathsf P.

Question 2

Why does unique hard attention stay within AC0\mathsf{AC}^0 while softmax attention with O(log⁡n)\order{\log n} bits of precision needs threshold gates?

  1. Hard attention uses fewer parameters
  2. Softmax attention computes exponentials, which AC0\mathsf{AC}^0 cannot evaluate
  3. Hard attention only compares scores and selects one position, while softmax sums over all nn positions, which includes counting
  4. Softmax attention is causal and hard attention is not
Show the answer

C. Hard attention only compares scores and selects one position, while softmax sums over all nn positions, which includes counting

Selecting a maximum is an AND of comparisons, in AC0\mathsf{AC}^0. A sum of nn numbers can count the 11s of a bit string, which decides PARITY and MAJORITY, both outside AC0\mathsf{AC}^0.

Question 3

What do Liu et al. (2023) show about simulating a finite automaton for TT steps with a transformer?

  1. It needs TT layers, one per step
  2. O(log⁡T)\order{\log T} layers always suffice, and O(1)\order{1} suffice when the automaton's groups are solvable
  3. It is impossible for any automaton with more than two states
  4. One layer suffices for every automaton
Show the answer

B. O(log⁡T)\order{\log T} layers always suffice, and O(1)\order{1} suffice when the automaton's groups are solvable

Transitions compose associatively, so a balanced tree of compositions needs depth O(log⁡T)\order{\log T}. Krohn–Rhodes theory gives constant-depth shortcuts for solvable groups; for non-solvable ones such as S5S_5 constant depth would imply TC0=NC1\mathsf{TC}^0=\mathsf{NC}^1.

Question 4

What does the succinctness result for masked hard-attention transformers say?

  1. They recognize languages that no finite automaton recognizes
  2. They can be verified in polynomial time
  3. They are always smaller than equivalent recurrent networks
  4. Some languages recognized by polynomial-size transformers need exponentially larger LTL formulas or recurrent networks and doubly exponentially larger automata
Show the answer

D. Some languages recognized by polynomial-size transformers need exponentially larger LTL formulas or recurrent networks and doubly exponentially larger automata

These transformers recognize only star-free regular languages, so the gain is in description size, not in the class of languages. The flip side: non-emptiness and equivalence are EXPSPACE\mathsf{EXPSPACE}-complete.

Further reading

  • W. Merrill & A. Sabharwal, "The parallelism tradeoff: Limitations of log-precision transformers," TACL 11 (2023).
  • Y. Hao, D. Angluin & R. Frank, "Formal language recognition by hard attention transformers: Perspectives from circuit complexity," TACL 10 (2022).
  • B. Liu, J. T. Ash, S. Goel, A. Krishnamurthy & C. Zhang, "Transformers learn shortcuts to automata," ICLR (2023).
  • P. Bergsträßer, R. Cotterell & A. W. Lin, "Transformers are inherently succinct," ICLR (2026).
  • G. Weiss, Y. Goldberg & E. Yahav, "Thinking like transformers," ICML (2021).
  • L. Strobl, W. Merrill, G. Weiss, D. Chiang & D. Angluin, "What formal languages can transformers express? A survey," TACL 12 (2024).