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.

SourceOpenQASM, TSIR,ZAC, system calls, C Hardwareinstructionsplatforms/*.mjs Channel actionstimed register writesper core, per segment FirmwareRV32I words per corefirmware.mjs Lean modeldiscrete-event runSoc.lean → qcontrol Trace: frames with IDs, parents, stateplus the summary Lean computes from its own state Views, all derived from the trace workspace layers and 3D stage · debugger inspector machine state at any step · channel timeline apparatus: ions, atoms, qubits reached studio/riscq-model.js, riscq-layers.js node riscq/cli.mjs --verify run.jsonre-executes and compares every frame
The pipeline. JavaScript compilers produce hardware instructions, channel actions and firmware; the Lean executable runs the firmware and emits the trace; the pages and the headless functions only read the trace. Schematic drawing.

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.

No quantum state. The simulator follows classical signals to the qubit, ion, electrode or atom they reach and records when they arrive. It never computes amplitudes. Measurement results, photon counts and digital input edges are inputs to the run.

2Lowering, per platform

PlatformSourceHardware instructionsWhat a control core drives
SuperconductingOpenQASM 3x90, x, frame (virtual Z), cz, measure, scheduled as soon as their qubits are freeOne core per qubit: a drive channel, one more drive channel per CZ partner, a readout tone and a demodulation window
Trapped-ion QCCDOpenQASM compiled by QCCD Studio, or a Studio TSIR program or designThe cycles of Studio's own replay: transport, rotate, ms, measure, cool, frameTransport 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 atomsA compiled ZAC programpickup, move, drop, raman, rydberg, at ZAC's timesOne 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:

  1. Synchronizes. All active cores arrive at a counted barrier; each sets its time base s0 to the release time plus SYNC_LEAD (128 batches), plus the segment's preroll.
  2. Fetches the shared bits this segment conditions on, from the core's board registers, into its bit table.
  3. 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, writes startTime and 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, … } }

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?

Count 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?

The difference is exactly the 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:

FrameKindPartBatchWhat happened
q2:152putq21,157sw x6, 0(x5): the core writes slot 1 to the fire register of channel 0
e213fireq2.gate1,160the write arrives after 1 + L = 3 batches; entry 1 is fired for start time 1,239
e214pulseq2.gate1,239the window opens, all parameters landed on time
e215effectqubit21,245the 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?

The answer is computed by 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:

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

TheoremStatementFile
land_exactA write ready no later than its lead before the start time lands exactly when the batch-time register reads the start timeTiming.lean
land_independent_of_arrivalTwo writes that both meet the margin land on the same batch, whatever link latency separates themTiming.lean
land_lateA write that misses the margin lands late by exactly the amount it missedTiming.lean
popCycle_ge_push, popCycle_spacingA queue entry pops at least 3 batches after its push and at least 2 after the previous popTiming.lean
default_leadsThe leads derived from the block latencies equal 34, 35, 36, 52, 11 and 2Timing.lean
fire_fire_contiguous, explicit_start_winsA second fire is scheduled one duration after the first; writing startTime after a fire discards the advanceTiming.lean
fire_preserves_table, decode_layoutA fire changes no table entry; the register addresses firmware writes decode to the intended fieldsTiming.lean
readRam_writeRam_wordA word read returns what a word write storedSoc.lean
nextSource_waiting, nextSource_isSomeThe hub arbiter grants only a core with a message waiting, and grants one in every batch in which any core has oneSoc.lean
Cpu.setReg_zero, exec_selfJump_pcRegister x0 is never written; a jump to itself is a fixed pointRv32.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

Not modeled

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.

Lessons D1 to D4 work with these documents.

What to remember

  1. Source → hardware instructions → channel actions → firmware → Lean execution → trace. Every view reads the trace.
  2. Firmware is generated per core: tables, a barrier per segment, a table loop or inline issue one lead ahead.
  3. The Lean model is a discrete-event system with exact batch arithmetic and a fixed order at equal batches.
  4. A frame has a stable ID, a part, targets, parents, a firmware address, a source instruction and the committed state.
  5. The io trace omits only instructions that touch no control register; full records them as exec.
  6. --verify re-executes a saved run; stateAt rebuilds the machine at any step from the frames alone.
  7. Keep four kinds of claim apart: proved about the model, conformance-tested, assumed, and not modeled.