Part E · Lesson 3

A real decoder on a control core

A lookup table decodes a repetition code. A surface code needs an algorithm. This lesson takes the C program of an FPGA decoder, compiles it for a RISC-Q control core with RISC-Q's own runtime, checks every answer against the original, and measures what decoding in control-core software costs.

Read lesson E2 first. The background chapter on measurement, feedback and decoding explains surface-code syndromes and decoding graphs in more depth.

1Why a table is not enough

What does the decoder of a surface code have to compute?

The decoder of lesson E2 had one table word for each of the 2n possible syndromes. For two syndrome bits that is four words. A distance-3 surface code measured over three rounds produces 12 syndrome bits of one error type, 4,096 table words. At distance 5 over five rounds it is 60 bits, and no memory holds 260 words. A decoder for a real code computes its answer.

Union-find is one such algorithm (Delfosse and Nickerson, "Almost-linear time decoding algorithm for topological codes", Quantum 5, 595, 2021). It works on a decoding graph: one node per check per round, and one edge per possible error, which flips the checks at its two ends. A node whose check changed value is a defect. Union-find starts with every node as its own cluster, grows every cluster that holds an odd number of defects by half an edge in every direction, merges clusters that touch, and stops when every cluster is even or touches the boundary of the code. A correction inside each cluster then follows.

2Helios and its C program

In this section: where the decoder comes from, what its program computes, and what it reports.

Helios is a union-find decoder built in FPGA logic (Liyanage, Wu, Tagare and Zhong, "FPGA-based Distributed Union-Find Decoder for Surface Codes", IEEE Transactions on Quantum Engineering, 2024; source, BSD-3-Clause). It is the decoder of RISC-Q's own error-correction system: Liu et al. integrate it there and run the whole algorithm in the FPGA fabric (arXiv:2603.16203).

Its repository also carries a C program, software_code/union_find.c, that computes what the hardware computes. The hardware test benches are checked against it. That file, at commit 622d85d, is the decoder of this lesson. It is used unmodified except for one line, #define D 13, which the build sets to the distance as the upstream README instructs. A test checks the vendored file's SHA-256 and that this one line is the only difference.

The decoding graph

The program decodes one error type of a rotated surface code of distance D over D measurement rounds. A rotated patch has D2 − 1 checks, half of each type, so one round has (D + 1)(D − 1)/2 nodes, laid out as D + 1 rows of (D − 1)/2 columns. Over D rounds:

nodes(D) = D · (D + 1) · (D − 1) / 2 nodes(3) = 12, nodes(5) = 60, nodes(7) = 168

The program's main loop is plain. Each pass grows every edge of every odd cluster, then merges across every fully grown edge, and the loop repeats while anything changed. The passes visit every edge, whether or not a cluster is nearby. That makes the C program an exact reference for the hardware, which grows all edges in parallel, and a slow program on a sequential core.

What it reports: roots, not corrections

For each syndrome the program writes the syndrome's id, then one line per node with the root of that node's cluster, encoded as the word 0x00KKIIJJ: round KK, row II, column JJ. A node that is alone in its cluster is its own root. Nodes that share a root were merged. The program stops there. It does not turn a cluster into a set of edges to flip, so its output is not yet a correction.

round 0round 1round 2 row 0row 1 row 2row 3 0 1 2 3 4 5 6 7 8 9 10 11 the defect: node 6 nodes merged into its cluster the cluster's root, node 2: word 0x00000200 Every other node is its own root: node 9 reports 0x00020100 (round 2, row 1). 8 clusters in all.
The distance-3 decoding graph and one decode. The 12 nodes, one per check of one type per round, in the order the program reads them. Colored: the second syndrome of the decoder-helios-d3 run, with its defect at node 6, and the roots the firmware reported for it. Edges are not drawn. Schematic drawing; the roots are read from the run.

Run the example decoder-helios-d5. How many roots does the decoder report for one syndrome?

One root per node: 5 · 6 · 4 / 2 = 60. The answer function counts the roots of the first decode in derived.decodes.

3From C to a control core

In this section: the build, the file layer that replaces stdio, and the core it runs on.

The build follows RISC-Q's own C flow (build.py compile_c), implemented in riscq/toolchain.mjs: RISC-Q's start.S, muldiv.c and riscq.h, vendored unmodified, a generated riscq_map.h with the core's addresses, and a generated linker script with one RAM region and a 1 KB stack reserve. The compiler is clang 20.1.4:

clang --target=riscv32-unknown-elf -march=rv32i_zmmul -mabi=ilp32 -O2 -fwrapv
      -nostdlib -ffreestanding -mno-relax -fuse-ld=lld -T link.ld
      start.S muldiv.c rq_input.c rq_libc.c union_find.c

rv32i_zmmul is the base integer instruction set plus multiply without divide, which RISC-Q's cores implement; muldiv.c supplies division in software. The core is a single RISC-Q core at the 500 MHz of the x6y3 build with 64 KiB of RAM. The distance-3 image is 14,328 bytes, 2,573 words of RAM image.

The upstream program reads its syndromes with fscanf from a file and writes roots with fprintf. A control core has no files, so a small file layer, riscq/decoders/libc/rq_libc.c, stands in for them. fscanf returns the next word of an array rq_input that the build places in RAM, and after the last word of each syndrome it reads the core's batch-time register, so the trace marks the moment a syndrome is fully loaded. fprintf assembles one 32-bit word from the hexadecimal digits of each line and stores it to the next slot of the host window. The words in the host window are the lines of the output file.

The Lean model executes the image instruction by instruction. The trace records every read of the time register and every store to the host window, and the bundle's derived.decodes lists each syndrome with three batch numbers: input loaded, first output word, last output word. The decode time is the first output minus the input mark. Each instruction is attributed to its C function, grouped into three phases: load syndrome, union-find and report roots.

Step 16 of the distance-3 run: fprintf in print_output stores the id of the second syndrome, the first with a defect, to the host window. The next twelve steps store its twelve roots.

4Is it the same decoder?

In this section: what the firmware's answers are compared with.

A decoder that runs is not yet a decoder that is right. The tests in tests/riscq-helios.test.mjs compare every root the firmware stores with an independent reference:

DistanceNodesSyndromesReference
3121411 sampled syndromes against the upstream program compiled natively with gcc, and 3 of Helios's own 1,000 test vectors against Helios's published outputs
5609The natively compiled upstream program
71687The natively compiled upstream program (a slow test, about 40 s)

The sampled syndromes flip each edge of the decoding graph with probability 0.001 or 0.01. Every root of every case matches. The committed images carry their SHA-256 and the exact compiler command, and with the same clang the distance-3 image rebuilds to the same bytes. This is a conformance test, not a proof: it shows that the compiled firmware, executed by the model, computes what the C program computes on these inputs.

The figure showed the second syndrome. Now take the fifth syndrome of the distance-3 run (record id 5): one defect, at node 10 (round 2, row 2). Read node 10's root word from derived.decodes and give the index n = round · 4 + row of its root (at distance 3 there is one column).

The root word of node 10 is 0x00010200: round 1, row 2, column 0, so n = 6. Nodes 6, 9, 10 and 11 share that root: the single defect's cluster grew until it touched a boundary of the graph, which counts as even, and the nodes it swept were merged into one cluster. The answer function decodes the word from the bundle.

5What it costs

In this section: decode times in the model, why they vary, and how they compare with the same algorithm in FPGA logic.

DistanceNo defectWith defects
317 µs34 µs to 58 µs
594 µsup to 285 µs
7274 µsup to 813 µs

The times come in steps. With no defect, the loop runs once: nothing is odd, nothing grows, and the pass that finds this visits every edge. Two adjacent defects merge into one even cluster after one growth, and the program needs about twice the time. A single defect has no partner; its cluster stays odd until it grows into the boundary, and the program needs about three times the time. The same pattern holds at distance 7: 274 µs with no defect, 774 µs for one defect. Each pass costs roughly the same because it visits every edge regardless of where the defects are.

In the distance-3 run, how long does the first syndrome, the one with no defect, take to decode?

8,579 batches of 2 ns, about 17.2 µs: one pass of the loop over every edge, plus setting up the graph.

Divide the decode time of the second syndrome, the single defect of the figure, by the no-defect time, and round to a whole number. That estimates how many passes of the grow-and-merge loop it needed.

About 55.5 µs over 17.2 µs: 3. The odd cluster grows until it touches the boundary, and a last pass finds that nothing changes. The estimate assumes each pass costs about the same, which is reasonable because every pass visits every edge; the first decode also includes setting up the graph, so the count is approximate.

Divide the no-defect decode time at distance 5 by the one at distance 3. The graph has 5 times as many nodes. What ratio do you get?

94.1 µs over 17.2 µs is about 5.5, close to the ratio of nodes: one pass costs roughly in proportion to the size of the graph. With defects, the number of passes grows too, which is why the slowest distance-5 syndrome takes 285 µs.

Against Helios in logic

Liu et al. report that Helios, in the FPGA fabric of their system, completes a distance-3 decode in 56 ns for the worst-case syndrome pattern of their end-to-end test, and decodes in 65 ns and 90 ns on average at distances 5 and 7, for pre-sampled syndromes at a physical error rate of 0.001 (arXiv:2603.16203). The same algorithm as firmware on one control core takes 17 µs to 58 µs at distance 3 in the model: about 300 to 1,000 times longer.

What this comparison shows. It shows what decoding in control-core software costs, not what Helios costs. Three things separate the numbers. The C program is a reference model that visits every edge on every pass, while Helios grows all edges at once in parallel logic. The core runs one instruction at a time. And its speed in the model is an assumption: one batch per instruction plus penalties for loads, stores and taken branches. Pulse timing does not depend on that assumption, because the lead absorbs it; decode times do.

Which statement about the 17 µs to 58 µs at distance 3 is right?

  1. It is the measured decode time of Helios on a RISC-Q board.
  2. It proves a soft core can never decode fast enough for error correction.
  3. It is the time to compute a correction for the surface code.
  4. It is the model's time for the reference C program on one core with an assumed instruction cost, ending with cluster roots.
The time is a property of this model and this program: sequential code, an assumed cost per instruction, and an output of roots. A different program (one that visits only active clusters) or a different core would give other numbers. Nothing here was measured on a board.

6Why it is not yet a system call

On the ring of lesson E2, decode_syndrome is a lookup. The union-find firmware is not wired to it, for two reasons. It reports roots, and a correction needs one more step: inside each cluster, choose the edges to flip. And the eight-transmon ring is too small for a surface code: a distance-3 rotated patch needs 9 data qubits and 8 check qubits. What the run already gives the virtual machine is a measured decode latency as a function of distance and defect count, the shape a decoder contract needs (the FTQVM allows a size-dependent decoder latency λd(n)).

Not simulated. No qubit is measured here. The syndromes are inputs placed in RAM, sampled from the decoding graph or taken from Helios's test vectors.

What to remember

  1. Helios's C union-find, unchanged except for its distance, runs as RV32I firmware built with RISC-Q's own runtime.
  2. Its decoding graph has D · (D + 1) · (D − 1)/2 nodes; it reports a root word 0x00KKIIJJ per node, not a correction.
  3. Every root matches Helios's test vectors at distance 3 and the natively compiled program at distances 3, 5 and 7.
  4. In the model a decode takes 17 µs to 58 µs at distance 3 and up to 285 µs at distance 5; Helios in logic takes tens of ns.
  5. Those model times rest on an assumed cost per instruction and a program that visits every edge on every pass.