Tesseract is a Most Likely Error decoder designed for Low Density Parity Check (LDPC) quantum
error-correcting codes. It applies pruning heuristics and manifold orientation techniques during a
search over the error subsets to identify the most likely error configuration consistent with the
observed syndrome. Tesseract achieves significant speed improvements over traditional integer
programming-based decoders while maintaining comparable accuracy at moderate physical error rates.
We tested the Tesseract decoder for:
- Surface codes
- Color codes
- Bivariate-bicycle codes
- Transversal CNOT protocols for surface codes
Features
- A* search: deploys A* search while
running a Dijkstra algorithm with early
stop for high performance.
- Stim and DEM Support: processes Stim circuit files and
Detector Error Model
(DEM)
files with arbitrary error models. Zero-probability error instructions are
automatically removed when a DEM is loaded.
- Parallel Decoding: uses multithreading to accelerate the decoding process, making it
suitable for large-scale simulations.
- Efficient Beam Search: implements a beam search
algorithm to minimize decoding cost and enhance efficiency.
- Sampling and Shot Range Processing: supports sampling shots from circuits. When a detection
error model is provided without an accompanying circuit, Tesseract requires detection events from
files using
--in. The decoder can also process specific shot ranges for flexible experiment
setups.
- Detailed Statistics: provides comprehensive statistics output, including shot counts, error
counts, and processing times.
- Heuristics: includes flexible heuristic options:
--beam, --det-penalty,
--beam-climbing, --no-revisit-dets, and --pqlimit to
improve performance while maintaining a low logical error rate. To learn more about these
options, use ./bazel-bin/src/tesseract --help.
- Visualization tool: open the viz directory in your browser to view decoding results. See viz/README.md for instructions on generating the visualization JSON.
Installation
Tesseract relies on the following external libraries:
- argparse: For command-line argument parsing.
- nlohmann/json: For JSON handling (used for statistics output).
- Stim: For quantum circuit simulation and error model
handling.
Build Instructions
Tesseract uses Bazel as its build system. To build the decoder:
bazel build src:all
Running Tests
Unit tests are executed with Bazel. Run the quick test suite using:
bazel test //src:all
By default the tests use reduced parameters and finish in under 30 seconds.
To run a more exhaustive suite with additional shots and larger distances, set:
TESSERACT_LONG_TESTS=1 bazel test //src:all
Usage
The file tesseract_main.cc provides the main entry point for Tesseract Decoder. It can decode
error events from Stim circuits, DEM files, and pre-existing detection event files.
Basic Usage:
./tesseract --circuit CIRCUIT_FILE.stim --sample-num-shots N --print-stats
To decode pre-generated detection events, provide the input file using
--in SHOTS_FILE --in-format FORMAT.
Example with Advanced Options:
./tesseract \
--pqlimit 1000000 \
--no-revisit-dets \
--det-order-seed 232852747 \
--num-det-orders 24 \
--circuit circuit_file.stim \
--sample-seed 232856747 \
--sample-num-shots 10000 \
--threads 32 \
--print-stats \
--beam 23 --beam-climbing \
--shot-range-begin 582 \
--shot-range-end 583
Example Usage
Sampling Shots from a Circuit:
./tesseract --circuit surface_code.stim --sample-num-shots 1000 \
--out predictions.01 --out-format 01
Using a Detection Event File:
./tesseract --in events.01 --in-format 01 --dem surface_code.dem \
--out decoded.txt --out-format 01
Using a Detection Event File and Observable Flips:
./tesseract --in events.01 --in-format 01 --dem surface_code.dem \
--obs_in obs.01 --obs-in-format 01 \
--out decoded.txt --out-format 01
Tesseract can combine generated detector-order methods with explicit detector
traversal orders from one or more JSON files. Sources are concatenated in
command-line order. For example, this uses three BFS orders, every order in the
file, and then three coordinate orders:
./tesseract \
--num-det-orders 3 \
--det-order-bfs \
--detector-orders orders.json \
--det-order-coordinate \
...
Each selected generated method contributes --num-det-orders orders and uses
--det-order-seed independently as its base seed. Within a method, order k
uses seed + k with an independent random-number stream; this differs from the
older shared-stream ensemble generator. --detector-orders FILE may be
repeated, and each occurrence contributes every order in that file. The JSON
format is the same list-of-lists form as Python's
TesseractConfig.det_orders:
[[0, 2, 1, 3], [3, 1, 2, 0]]
Every inner list must be a complete permutation of the DEM detector IDs. With
no detector-order source option, Tesseract uses the existing generated Index
source. A file-only invocation remains file-only; use --det-order-index
explicitly to combine file orders with generated Index orders.
Tesseract supports reading and writing from all of Stim's standard output
formats.
Decoding with GARI
Generate a GARI matrix DEM from a source circuit:
python src/py/_tesseract_py_util/gari.py \
--circuit circuit_file.stim \
--prior xor \
--out-dir gari_output
This writes gari_output/circuit_file_gari_xor.dem. Its physical detector rows preserve the
source detector IDs, and its added virtual rows form a suffix. GARI-aware detector orders can be
written in the generic CLI format when reordering is wanted:
import json
import stim
from tesseract_decoder import demutil
circuit = stim.Circuit.from_file("circuit_file.stim")
gari_dem = stim.DetectorErrorModel.from_file("gari_output/circuit_file_gari_xor.dem")
orders = demutil.gari.build_detector_orders(circuit, gari_dem, num_det_orders=5)
with open("gari_orders.json", "w") as f:
json.dump(orders, f)
Sample from the source circuit and decode with the generated DEM:
./bazel-bin/src/tesseract \
--circuit circuit_file.stim \
--dem gari_output/circuit_file_gari_xor.dem \
--detector-orders gari_orders.json \
--sample-num-shots 100 \
--sample-seed 1234 \
--threads 1 \
--pqlimit 1000000 \
--beam 5 \
--beam-climbing \
--no-revisit-dets \
--print-stats \
--stats-out gari-stats.json
The GARI matrix DEM is a decoding representation and must not be sampled. When a circuit and a
larger DEM are supplied together, both command-line decoders read or sample the circuit's detector
prefix and leave the DEM's virtual suffix zero. Their observable counts must agree. When reading a
source-width event file, pass both --circuit and --dem; with --dem alone the input width is the
full DEM width.
For matrix analysis, gari.py --row-order block writes a _block.dem file in the internal
physical-X, physical-Z, virtual-Z, virtual-X row order. This research form does not accept source
syndromes as a direct prefix. See
GARI transformed matrices for supported circuit
conventions and Python API details.
Performance Optimization
Here are some tips for improving performance:
- Parallelism over shots: increase
--threads to leverage multicore processors for faster
decoding.
- Beam Search: use
--beam to control the trade-off between accuracy and speed. Smaller beam sizes
result in faster decoding but potentially lower accuracy.
- Beam Climbing: enable
--beam-climbing for enhanced cost-based decoding.
- Priority Queue limit: use
--pqlimit to limit the size of the priority queue.
- Error sparsification: enable
--sparsify-errors to always keep low-degree errors while
selectively reactivating high-degree errors per shot. This can improve runtime on DEMs with
many high-degree errors, at the cost of a tunable accuracy/speed tradeoff.
Example with error sparsification:
./tesseract \
--circuit circuit_file.stim \
--sample-num-shots 10000 \
--beam 20 \
--beam-climbing \
--sparsify-errors \
--sparsify-base-degree 3 \
--print-stats
--sparsify-base-degree K is required when --sparsify-errors is enabled. Errors touching at most
K detectors are always active. Errors above K are optional and are ranked per shot by overlap
with the fired detectors. In the surface code (or other 'mostly graphlike' codes) try K = 2. In the
color code or bivariate bicycle codes, try K = 3. In general, it is recommended to set K to the number
of activated detectors created by a single data qubit error in the bulk, restricting to X or Z errors
only for CSS codes.
--sparsify-reactivate-limit M caps the number of optional high-degree errors reactivated per
shot. If omitted, Tesseract uses round((4.5^(K - 2) / 3) * num_detectors).
--sparsify-max-degree D optionally excludes optional errors above degree D. If omitted, optional
errors are not capped by degree.
Output Formats
- Observable flips output: predictions of logical errors.
- DEM usage frequency output: if
--dem-out is specified, outputs estimated error frequencies.
- Statistics output: includes number of shots, errors, low confidence shots, and processing time.
Multi-Pass Graph Shattering
Multi-pass graph shattering partitions a correlated detector error model into two detector
components and decodes the smaller component models separately. With two passes, predictions from
the first pass update error priors used during the final pass. The current implementation requires
exactly two components and accepts one or two passes.
Two-pass reweighting is a correlated-matching-style heuristic. A component symptom's XOR marginal
includes every mechanism that produces it, including one-sided mechanisms, while paired evidence
includes only mechanisms shared with the other component symptom. Their ratio is therefore not, in
general, an exact joint or conditional probability. Reweighted probabilities are capped at 0.499
so every retained error continues to have positive decoding cost. Because reweighting is defined on
aggregate component symptoms, two-pass decoding requires merge_errors=true. Unmerged mechanisms
remain supported with one-pass decoding.
Detector classification
The standalone CLI deliberately accepts one canonical convention only: every detector instruction
must have a JSON tag with a top-level "measure_basis" whose value is exactly "X" or "Z":
detector[{"measure_basis":"X"}](0, 0, 0) D0
detector[{"measure_basis":"Z"}](1, 0, 0) D1
The CLI does not infer bases from legacy metadata or coordinates. Tagged DETECTOR instructions in
a .stim circuit retain their tags during circuit-to-DEM conversion, so a canonically tagged
circuit can be passed directly. Otherwise, normalize its DEM in Python first. Automatic
normalization covers only the named automatic metadata and Chromobius-coordinate conventions. For
a Stim-generated surface-code circuit, select the explicit parity adapter as shown below. The helper
preserves coordinates, instruction order, repeats, shifts, errors, tags, and unrelated JSON
metadata:
from pathlib import Path
import stim
import tesseract_decoder
circuit = stim.Circuit(Path("circuit.stim").read_text())
dem = circuit.detector_error_model()
canonical_dem = tesseract_decoder.demutil.annotate_detector_bases(
dem,
detector_basis_classifier=(
tesseract_decoder.demutil.stim_surface_code_detector_basis_classifier
),
)
Path("canonical.dem").write_text(str(canonical_dem))
annotate_detector_bases(dem) writes top-level measure_basis, the authoritative field in both
Python and the CLI. It rejects an invalid or conflicting existing top-level measure_basis, but
preserves lower-priority fields such as basis and md