Accuracy and Tuning¶
What each setting does, when to change it, and how to tell whether a result is trustworthy. The short version: leave the defaults alone until a fidelity tells you not to.
The settings¶
All configuration is construction-only. A state carries the settings it was built with, so build a new simulator to change one.
from tensorkeeper import TkSimulator
sim = TkSimulator(
bonddim=256, # cap on the MPS bond dimension
sv_cutoff=1e-8, # singular values below this are discarded
disentangle_period=1, # N = every N MPS operations, "auto", 0 = off
)
All three trade accuracy against time and memory. Reproducibility is not a
simulator setting: pass seed= to execute (see
Randomness). Nothing about bond dimension or fidelity
depends on the seed: with no measurement in the circuit, the evolution is
deterministic.
bonddim¶
The maximum bond dimension the residual MPS may use. Reaching it forces truncation, which loses fidelity.
An \(n\)-qubit state cannot exceed \(\chi = 2^{\lfloor n/2 \rfloor}\), so for small registers the default 256 can never truncate. It starts to bind when the circuit's magic grows: bond dimension in \(|\varphi\rangle\) tracks the non-Clifford content, so what matters is the \(T\) count, not the qubit count.
Raise it when the fidelity drops below what you can accept. Cost grows roughly as \(\chi^3\), so doubling it is roughly eight times the work. Lower it to trade accuracy for speed and memory on a deliberately approximate run.
sv_cutoff¶
During compression, singular values below this fraction of the largest one are
discarded, whatever bonddim allows. The default 1e-8 is numerical hygiene
rather than approximation: far below any physically meaningful weight, but large
enough to prune the numerical noise that would otherwise make bonds creep upward
for no benefit.
Raise it (1e-4, 1e-3) to trade a controlled amount of fidelity for a smaller
bond dimension on a circuit that is too big at the cutoff you have. It is the
gentler of the two truncation knobs, because it drops weight that is genuinely
small instead of whatever does not fit. Leave it alone for exact work.
Do not set it to zero, or below about 1e-16. There you stop pruning noise and
start retaining bonds that carry nothing, which costs time and memory and buys no
accuracy.
disentangle_period¶
Controls the Clifford disentangler: a periodic sweep that looks for two-qubit Cliffords lowering the entanglement of \(|\varphi\rangle\), applies them to the MPS and compensates them in the frame. It moves entanglement from the MPS into the frame, where it is free. The re-split is exact, so enabling it can never change the answer on a run that was not truncating.
| Value | Behavior |
|---|---|
1 (default) |
sweep after every MPS-touching operation |
N |
sweep every N MPS-touching operations |
"auto" |
adaptive: sweep after an operation that grew the bond dimension; the period doubles whenever a sweep fails to reduce it, and resets as soon as one succeeds |
0 |
never sweep |
It is on by default because a lower bond dimension makes every later MPS
operation cheaper (their cost grows as \(\chi^3\)), so the sweeps usually pay for
themselves. On magic state cultivation workloads around 92% of the peak bond
dimension is recoverable this way: a shot of msc_d5_noisy runs 7× faster with
1 than with 0. When a run saturates bonddim, recovering even a factor
of two in the residual also converts directly into accuracy.
"auto" is eager while sweeps keep collapsing \(\chi\) and backs off once they
stop, so it suits a circuit the disentangler helps only now and then. Set 0
only for a circuit that never accumulates gauge entanglement.
The disentangler affects the fidelity claim
The sweep compresses at sv_cutoff, and the MPS engine does not report how
much weight a compression discarded. Once a sweep has run, the result can no
longer claim exactness, even if nothing was in fact lost.
Reading the fidelity¶
evolve returns (state, fidelity), and execute fills results.fidelities.
There are two answers:
ExactFidelity(): the bonddim cap never bound. Before each operation that
can grow a bond, every bond had room to grow by that operation's full factor
(twice for a rotation, a two-site gate or a disentangler gate, the number of terms
for a wide Pauli sum), so nothing the cap would cut was ever at risk. A pure
Clifford circuit always reports this, and so does a circuit with magic at a
generous bonddim.
TruncationLowerBound(v): the cap bound at least once, and the fidelity is
at least v, the weight that survived.
Weight dropped below sv_cutoff counts against neither: it is the tolerance you
asked for. At the default 1e-8 that is numerical noise, but a raised
sv_cutoff can lose real accuracy while the claim still reads ExactFidelity().
Validate a run with a raised cutoff against one at the default.
Why TruncationLowerBound(1.0) is not ExactFidelity()¶
The two are different claims:
ExactFidelity()says the cap never could have cut anything.TruncationLowerBound(1.0)says the cap was reached, and nothing is known to have been cut.
An operation that reached the cap and happened to discard nothing gives the
second, not the first. The distinction is about what could have been lost,
and weakening the claim is the honest thing to do when the cap was in play. You
will meet TruncationLowerBound(1.0) when bonddim is just large enough for the
state, the edge where raising it is cheap insurance.
Treat any TruncationLowerBound as a signal to check convergence, even at 1.0.
What the number is¶
The reported value is a product of per-step retained weights: the surviving norm of the state. That is exact for a single truncation, but across many steps it is an estimate rather than a guarantee, and it can read higher than the true overlap with the untruncated state.
A truncated state is left unnormalized, which gives you a second, independent reading of the same quantity. Probabilities over all basis states sum to the weight that actually survived rather than to 1:
from mimiqcircuits import BitString
total = sum(state.probability(BitString.fromint(n, i)) for i in range(2 ** n))
print(total) # 1.0 on an exact run; the surviving weight on a truncated one
On a mildly truncated run that sum matches the reported number. When it comes back well below the reported number, the report is optimistic and the run is badly truncated. Read a low fidelity as "do not trust this run", not as a percentage of correctness.
Give the cap headroom
Setting bonddim to exactly the bond dimension the state needs leaves no
room, and the run can come back with weight missing from the probabilities
while the reported bound still reads 0.99…. Leave the cap some headroom, and
confirm accuracy with the convergence sweep below rather than from the
reported number alone.
Checking convergence¶
The reliable method is a bond-dimension sweep. Run the same circuit at increasing
bonddim and watch the quantity you care about stop moving:
import mimiqcircuits as mc
from tensorkeeper import TkSimulator
# A deliberately magic-dense circuit: a T on every qubit in every layer, so
# truncation actually bites. This is the regime where tuning matters.
n = 12
c = mc.Circuit()
for _ in range(12):
for q in range(n):
c.push(mc.GateH(), q)
c.push(mc.GateT(), q)
for q in range(0, n - 1, 2):
c.push(mc.GateCX(), q, q + 1)
for q in range(1, n - 1, 2):
c.push(mc.GateCX(), q, q + 1)
for chi in (4, 8, 16, 32, 64, 128):
sim = TkSimulator(bonddim=chi)
state = sim.build_state(n)
state, fidelity = sim.evolve(state, sim.compile(c))
amp = state.amplitude(mc.BitString("0" * n))
print(f"chi={chi:4d} bond={state.bond_dim:4d} amp={amp.real:.8f} {fidelity}")
chi= 4 bond= 4 amp=0.00193885 TruncationLowerBound(value=0.0075457069804879375)
chi= 8 bond= 8 amp=0.00758821 TruncationLowerBound(value=0.03899335457021127)
chi= 16 bond= 16 amp=0.02081354 TruncationLowerBound(value=0.12923164205102064)
chi= 32 bond= 32 amp=0.02032901 TruncationLowerBound(value=0.4027227961193784)
chi= 64 bond= 64 amp=0.02394088 TruncationLowerBound(value=0.9999999999999301)
chi= 128 bond= 64 amp=0.02394088 TruncationLowerBound(value=0.9999999999999301)
Converged at \(\chi = 64\): the amplitude stopped moving, the fidelity reached 1 to numerical precision, and raising the cap to 128 left the bond dimension at 64, which means nothing was being truncated any more. The intermediate rows are the warning. At \(\chi = 16\) the amplitude looks plausible and is wrong by 13%, and the fidelity of 0.13 was telling you so.
Do not judge convergence from the fidelity alone: it bounds the truncation loss, not the error in any particular observable.
Diagnosing cost¶
If a circuit is slower or larger than you expected, bond_dim is the first thing
to look at.
- \(\chi = 1\) and it should not be. Two innocent explanations before you suspect a problem. The circuit may be genuinely all Clifford: a rotation at a Clifford angle, \(R_Z(\pi/2)\) for example, is absorbed by the frame. Or the magic is real but still free, which is normal for the first order-\(n\) non-Clifford gates on a wide register.
- \(\chi\) larger than the \(T\) count suggests. The dressed gates are landing on
wide supports. If you turned the disentangler off or slowed it down, set
disentangle_period=1back. - \(\chi\) pinned at
bonddim. You are truncating. Raise the cap, and expect aTruncationLowerBound. - \(\chi\) at the \(2^{\lfloor n/2 \rfloor}\) ceiling. The residual has become a generic state and the frame is buying you nothing. This is the magic-everywhere regime where TensorKeeper is the wrong tool; see When to use it.
Remember that this number describes \(|\varphi\rangle\), not \(|\psi\rangle\). See Concepts.
Next steps¶
- Examples:
08_tuningworks through the sweep above. - API Reference: every parameter in one place.