Background · Chapter 6
How this simulator works
Every picture in the workspace, every number in an exercise and every plot in the debugger is read from one list: the trace of a firmware execution in a Lean model of the controller. This chapter explains how a program becomes that trace, what one entry of it contains, how you can replay and check it yourself, and exactly which parts of the result are proved, tested, assumed or not modeled.
This chapter uses the vocabulary of chapters 3 and 4. The source files are named as paths in the repository; the Lean model is readable in the browser at /model/RiscQ/Soc.lean, /model/RiscQ/Timing.lean and /model/RiscQ/Rv32.lean. The full reference is docs/riscq-controller.md.
1From a program to a trace
What exactly is simulated, and by what?
In this section: the five representations of a run and the program that produces each.
Each platform has three levels below its source program (docs/riscq-controller.md). A
hardware instruction is one physical operation with a start time and a
duration. A channel action is one timed register write on one control core. The
firmware is the RV32I program that issues those writes. The Lean model runs the
firmware and emits the trace. Nothing in the views is computed from the source program directly; a pulse is shown because a trace
frame says it opened.
2Lowering, per platform
| Platform | Source | Hardware instructions | What a control core drives |
|---|---|---|---|
| Superconducting | OpenQASM 3 | x90, x, frame (virtual Z), cz, measure, scheduled as soon as their qubits are free | One core per qubit: a drive channel, one more drive channel per CZ partner, a readout tone and a demodulation window |
| Trapped-ion QCCD | OpenQASM compiled by QCCD Studio, or a Studio TSIR program or design | The cycles of Studio's own replay: transport, rotate, ms, measure, cool, frame | Transport cores with one DAC channel per Studio electrode control channel; one core per gate zone with three laser modulators and a photon counter; one core for broadcast light |
| Neutral atoms | A compiled ZAC program | pickup, move, drop, raman, rydberg, at ZAC's times | One core per AOD with one RF tone channel per row and column; one laser core |
From docs/riscq-controller.md; the code is riscq/platforms/superconducting.mjs, qccd.mjs and neutral.mjs.
Superconducting. Standard gates decompose into x90, x and frame updates; durations, amplitudes,
frequencies and phases come from RISC-Q's x6y3 calibration. A measurement whose bit conditions a later gate ends a segment.
Trapped ions. QCCD Studio already states, for every cycle, which ions move over which edges and which electrode control
channels act; the lowering plays one waveform per engaged control channel, sets angle and phase of each gate pulse from the TSIR
parameters, and fires one detection pulse per gate zone. Electrode voltages, ion motion and heating stay Studio's models. When Studio
reports unit times, each cycle takes the standard duration of its kind (chapter 7).
Neutral atoms. Each active AOD tone ramps its amplitude up at pickup, holds it during the move while its frequency steps every
2 µs along an assumed constant-acceleration path, and ramps down at drop. Lessons C1, C2
and C3 follow one program on each.
3Firmware generation
How does a list of timed writes become a program for a processor?
In this section: plans per core, segments and barriers, the table loop, and inline issue.
The shared back end (riscq/compile.mjs, plans) groups a platform's channel actions into one plan per core. A
core with no action parks at once and joins no barrier: in the feed-forward run, cores q3 to q7 each run a six-instruction program.
Every classical bit has exactly one producer. A bit that conditions an action on another core is given a slot in one of the hub's
four 32-bit group words (128 shared bits at most), and a decoder's inputs take consecutive slots so it can read them as one field.
compileCore in riscq/firmware.mjs then writes each core's program. It loads base registers (s1
points at the control block at 0x4000, s2 at the bit table near the top of RAM), programs every pulse table, and for each
segment:
- Synchronizes. All active cores arrive at a counted barrier; each sets its time base
s0to the release time plusSYNC_LEAD(128 batches), plus the segment's preroll. - Fetches the shared bits this segment conditions on, from the core's
boardregisters, into its bit table. - Issues. A segment of plain timed writes is issued by a loop over a four-word table: issue time, start time, register address,
data. Each pass waits until the issue time with
waitTimeCmp, writesstartTimeand then the data word. Any other segment, one with conditions, phase offsets, result reads, photon counts, decodes or frame updates, is issued inline, one operation after another, each preceded by a wait until its issue time.
The issue order comes from issueOrder: every write one lead (96 batches) before its start, moved earlier where a result
read would hold the core, and a backward pass that spaces writes by their issue cost. If the earliest write then falls before the time
base, the segment's start moves later by the preroll. A schedule that would overflow a channel's queues is rejected with an error
that names the channel and both batches. The program is assembled by riscq/rv32.mjs, and every word carries a source-map
entry naming the hardware instruction it serves, so the workspace can light the firmware row of any event.
4The Lean model as a discrete-event system
Soc.lean is the executable model of a RISC-Q system-on-chip, written in
Lean so that it both runs and can carry proofs. Its header states the design: "Execution is
a discrete-event simulation with exact cycle arithmetic." The world holds every core's registers and RAM, every channel's register
file and queues, every sink, the hub and a schedule of pending events. Time advances to the next batch at which anything happens. At
equal batches, converter-edge and link events apply before a core retires an instruction, and cores retire in index order. All times
are whole batches; atPs = atBatch × dspPeriodPs.
A core executes the RV32I semantics of Rv32.lean: decode a 32-bit word, compute
the register update and the single bus access it requests. Soc.lean resolves that access against RAM, the control
block, the sinks or the put window. A put becomes a scheduled link event; when it reaches its channel it is decoded by
decodeCmd of Timing.lean and applied by Buffer.apply; a
fire pushes queue entries whose pop and landing cycles are exactly the popCycle and landCycle the timing
theorems are about. So the theorems are about the same definitions that produce the trace.
5Frames
What does one step of the trace contain?
In this section: the fields of a frame, the kinds of events, and the two trace levels.
Every event is one frame of the trace. This is the
frame in which the first x90 of the feed-forward run opens its window, as the run bundle stores it (state shortened):
{ "id": "e164", "op": "pulse", "part": "q0.gate", "targets": ["dac0"],
"atBatch": 320, "atPs": 640000, "parents": ["e162"],
"pc": 2147484196, "source": "h2",
"state": { "opens": 320, "closes": 338, "atConverter": 326, "dur": 18,
"amp": 2101, "phase": 49094, "freq": 45047,
"ampLands": 320, "phaseLands": 320, "envLands": 320,
"onTime": true, "aligned": true, "overlap": false, … } }
idis stable: the same program produces the same IDs, so a link to a step names the same event tomorrow.partis the scene part the event acts on, andtargetsthe parts it acts toward. Every part has a 3D component in the workspace.parentsare its causal predecessors, here the firee162on the same channel.pcandsourcename the firmware address and the hardware instruction that caused it, hereh2, thex90on qubit 0.stateis the committed state of the part after the event. For a pulse it includes when each parameter landed;onTimeandalignedsay whether all of them landed on the start time.
The event kinds in a run of the feed-forward program are: core events read, write, put,
wait, resume, host, done; channel events write, freq,
fire, pulse; hub events wait, arrive, release, publish;
inbox events, where hub deliveries and reports reach a core, released, board, result and
clear; and effect frames when a pulse reaches a qubit or resonator, with the arrival time after the wire.
io and full traces
By default a run records the io trace: only instructions that touch a control register appear, together with every channel,
hub and apparatus event. With --trace full every retired instruction is a frame too, of kind exec. Because a core
frame carries the register it wrote, the register file is rebuilt by replaying frames; with the io trace the debugger marks
registers that no recorded frame wrote.
Run the feed-forward program with the measured bit set to 1 and the default io trace. How many frames does the trace
hold?
frames in the run bundle, or read the last step number in the workspace's trace dock. Every one
of them is an event on some part of the scene.Run the same program with trace: "full". How many more frames does it record than the io trace?
exec frames: instructions that touched no control register, such as
address arithmetic and branches. The events that act on the machine are the same in both traces.6Causal chains
Following parents back from any frame gives its causal chain, which ends at
the core instruction that started it (causalChain in studio/riscq-model.js). The conditioned x on
qubit 2 in the feed-forward run has a short one:
| Frame | Kind | Part | Batch | What happened |
|---|---|---|---|---|
q2:152 | put | q2 | 1,157 | sw x6, 0(x5): the core writes slot 1 to the fire register of channel 0 |
e213 | fire | q2.gate | 1,160 | the write arrives after 1 + L = 3 batches; entry 1 is fired for start time 1,239 |
e214 | pulse | q2.gate | 1,239 | the window opens, all parameters landed on time |
e215 | effect | qubit2 | 1,245 | the pulse is at the DAC port, 6 batches later, and arrives at the qubit at 2,495,003 ps |
Read from the run of superconducting-feedforward with c = 1, for this chapter.
The chain stops at the first core instruction, the store. The barrier and the published bit are not in it: they decided
whether and when the core reached that store. The core's own frames are linked as well, each to the one before it
(q2:152 has the parent q2:149), so you can keep walking back through the firmware in the debugger. The tests
require every chain to end at a core instruction (tests/riscq-debugger.test.mjs).
How many frames long is the causal chain of the last effect frame in that run, counting the frame itself?
causalChain of studio/riscq-model.js, the same function the debugger
uses to light the causal path back to the core instruction.7Replay, reconstruction and --verify
A run saved with node riscq/cli.mjs --example <id> --out run.json contains the source, the hardware instructions, the
firmware statistics, the Lean program document, every frame and the summary. Three things can be done with it without a browser:
- Verify.
node riscq/cli.mjs --verify run.jsonre-executes the saved program in Lean and names the first frame that differs. For the feed-forward run it prints{"events":451,"verified":true}. - Reconstruct.
stateAt(bundle, index)instudio/riscq-model.jsrebuilds pulse tables, start times, carriers, queues, sinks, hub groups and registers at any step from the frames up to that step and from nothing else. The tests require the state rebuilt at the last frame to equal the summary Lean computes from its own state, and a state asked for out of order to equal a replay from reset. - Inspect. The functions of
studio/riscq-layers.jsanswer what the workspace shows:focusAtgives the source line, hardware instruction, core and firmware row of a step;ionPositionsandatomPositionsgive what the chip view draws (docs/riscq-workspace.md, "Headless use").
A person in the workspace and a script reading the bundle therefore see the same result. The exercises in this course use the same route: their answer functions run the program on the server and read the bundle.
8What is proved, tested, assumed and not modeled
In this section: the four kinds of claim, kept apart, as docs/riscq-controller.md lists them.
Proved in Lean, about the model
| Theorem | Statement | File |
|---|---|---|
land_exact | A write ready no later than its lead before the start time lands exactly when the batch-time register reads the start time | Timing.lean |
land_independent_of_arrival | Two writes that both meet the margin land on the same batch, whatever link latency separates them | Timing.lean |
land_late | A write that misses the margin lands late by exactly the amount it missed | Timing.lean |
popCycle_ge_push, popCycle_spacing | A queue entry pops at least 3 batches after its push and at least 2 after the previous pop | Timing.lean |
default_leads | The leads derived from the block latencies equal 34, 35, 36, 52, 11 and 2 | Timing.lean |
fire_fire_contiguous, explicit_start_wins | A second fire is scheduled one duration after the first; writing startTime after a fire discards the advance | Timing.lean |
fire_preserves_table, decode_layout | A fire changes no table entry; the register addresses firmware writes decode to the intended fields | Timing.lean |
readRam_writeRam_word | A word read returns what a word write stored | Soc.lean |
nextSource_waiting, nextSource_isSome | The hub arbiter grants only a core with a message waiting, and grants one in every batch in which any core has one | Soc.lean |
Cpu.setReg_zero, exec_selfJump_pc | Register x0 is never written; a jump to itself is a fixed point | Rv32.lean |
Conformance tests
Against RISC-Q artifacts. Three firmware binaries from the RISC-Q repository run unmodified and reproduce what RISC-Q's RTL
simulations assert: pulse_sched.elf, xcore.elf and host_window.elf. A publish reaches every board
after 23 batches, as RISC-Q documents. The hub arbiter follows the statements of PutHub.md, not an RTL trace.
Against independent references. Arithmetic, shifts, comparisons, multiplies, loads, stores, branches and links are checked against
values computed outside the model; every word the assembler emits is decoded by Lean as the same instruction; gate decompositions are
checked as 2 × 2 matrices up to a global phase. Tests: tests/riscq-firmware.test.mjs, riscq-rv32.test.mjs,
riscq-superconducting.test.mjs, riscq-qccd.test.mjs, riscq-neutral.test.mjs,
riscq-limits.test.mjs.
Assumed or schematic
- Three latencies: the core's time lag (link depth), the report path (3 + 2 + L + 1) and the branch and bus penalties (+5, +1).
- The hub arbiter starts its round at core 0 and is modeled at the dispatch stage.
- The core's cost model: one batch per instruction plus the two penalties. Pulse timing does not depend on it while the lead margin holds; decode times do.
- The controller floorplan positions: a schematic two-region layout in tenths of a clock region, not physical dimensions.
- The mapping of one QCCD Studio time unit to one shuttle; the path inside a neutral-atom move; the positions of the ring written as a hardware design.
Not modeled
- Carrier synthesis is represented by its latency; a pulse frame reports the scheduled codes, and a waveform is drawn from them for display.
- Electrode voltages, ion motion and heating remain QCCD Studio's models.
- A frequency step is not phase continuous on the RISC-Q generator.
- The two ring pairs whose CZ needs a pulse on the second excited level are not offered.
- Multi-board operation over White Rabbit, interrupts and CSRs are absent.
- Quantum states, gate fidelities, atom loss and imaging.
9Hardware design documents
A pulse-controlled machine can also be given as a document instead of code (riscq/design.mjs): a control board, cores, DACs,
ADCs, qubits, resonators and passive stages with positions in millimetres, the channels of each core, wires with a medium and a routed
path, and the calibrated pulses of each qubit and coupling. The simulator checks it against rules and derives the build, the pulse tables
and every propagation delay from it. The editor at /riscq-design edits such a document, and
node riscq/cli.mjs --ring-design prints the built-in ring as one.
- Rules. One board. 1 to 64 cores, 1 to 16 channels each. A pulse channel names a DAC and a demodulation channel an ADC. Every wire starts and ends at its parts. Every calibrated pulse has a wired path from its converter to the part it acts on, and every resonator a path back to its ADC. A readout whose tone returns after the acquisition window opens is a warning.
- Timing. A wire's delay is its routed length over the signal speed of its medium (chapter 2).
- Checked. The ring written as a design plays exactly the pulses of the built-in ring. Moving the chip 2 m farther delays every arrival by 10 ns and changes nothing at the converters. Driving three qubits from one core runs the feed-forward program without any message through the hub.
- Boundary. Only machines whose gates are pulses on fixed qubits. The ion and atom machines still take their control hardware from code. Loss, dispersion, crosstalk and placement inside the FPGA are not modeled; the posted-link depth is one number for the board.
Lessons D1 to D4 work with these documents.
What to remember
- Source → hardware instructions → channel actions → firmware → Lean execution → trace. Every view reads the trace.
- Firmware is generated per core: tables, a barrier per segment, a table loop or inline issue one lead ahead.
- The Lean model is a discrete-event system with exact batch arithmetic and a fixed order at equal batches.
- A frame has a stable ID, a part, targets, parents, a firmware address, a source instruction and the committed state.
- The
iotrace omits only instructions that touch no control register;fullrecords them asexec. --verifyre-executes a saved run;stateAtrebuilds the machine at any step from the frames alone.- Keep four kinds of claim apart: proved about the model, conformance-tested, assumed, and not modeled.