Computing Library › Quantum Simulation
Quantum Simulation

A Worked Trotterization Example

A concrete numerical walk-through of first- and second-order Trotter error for a two-term Hamiltonian, with runnable code.

Setting up a test case

Take a single qubit with H = a X + b Z, two non-commuting Pauli terms. The exact propagator e^(-iHt) is easy to compute for validation, so this is an ideal sandbox to see Trotter error behave as the theory predicts: first order should scale as (t/r)^2 per step, second order as (t/r)^3.

python
import numpy as np
from scipy.linalg import expm

X = np.array([[0,1],[1,0]], dtype=complex)
Z = np.array([[1,0],[0,-1]], dtype=complex)

def exact(a, b, t):
    return expm(-1j * (a*X + b*Z) * t)

def trotter1(a, b, t, r):
    step = expm(-1j*a*X*t/r) @ expm(-1j*b*Z*t/r)
    return np.linalg.matrix_power(step, r)

def trotter2(a, b, t, r):
    step = expm(-1j*a*X*t/(2*r)) @ expm(-1j*b*Z*t/r) @ expm(-1j*a*X*t/(2*r))
    return np.linalg.matrix_power(step, r)
Kronos motion — materials first

Measuring the error

Comparing each approximation to the exact operator in the spectral norm confirms the scaling: doubling r roughly quarters the first-order error and reduces the second-order error eightfold. This is the O((t/r)^2) versus O((t/r)^3) per-step behavior accumulated over r steps.

python
def err(approx_fn, a, b, t, r):
    U = exact(a, b, t)
    V = approx_fn(a, b, t, r)
    return np.linalg.norm(U - V, 2)

for r in [1, 2, 4, 8, 16]:
    e1 = err(trotter1, 1.0, 0.7, 1.0, r)
    e2 = err(trotter2, 1.0, 0.7, 1.0, r)
    print(r, round(e1, 6), round(e2, 6))

Interpreting the numbers

Why the commutator sets the scale

The single-qubit case makes the error mechanism transparent. Because X and Z do not commute, [X, Z] = -2iY is nonzero, and this commutator is exactly the leading error term of the first-order formula. If instead b were zero, the two exponentials would commute and every Trotter approximation would be exact regardless of r. The whole cost of simulation, here and in the many-body case, traces back to non-commuting terms, so a Hamiltonian's commutator structure is what a practitioner should study before choosing a step count.

Scaling up

For a many-qubit local Hamiltonian the same principles hold, but each Trotter step becomes layers of two-qubit gates and the error prefactor depends on the sum of nested commutators rather than a single one. The number of gates per step grows with the number of Hamiltonian terms, while the number of steps r is set by the accuracy target and the total time. This tiny example captures the essential lesson: symmetrizing the product formula buys an extra order of accuracy for a modest increase in gates per step, which is why second-order Trotter is a common default in practice.