Part B · Lesson 2
Timed queues and the lead
The firmware of every core works about 96 batches in the future. Its writes reach the channels long before they are needed, and timed queues release each parameter exactly when it must leave. This lesson shows where the numbers come from, what Lean proves about them, and what happens when a write is late.
You should know the channel registers of B1 and the path of one pulse in A3. The background chapter on deterministic timing goes deeper into why control systems schedule ahead.
1Different parameters, different delays
If every parameter of a pulse is written at the same moment, why do they not all arrive together?
A pulse is played by a pulse generator next to the DAC. Its parameters travel different paths through it. The phase
goes through the carrier generator and a complex multiplier; the envelope address goes to a memory and then the
same multiplier; the duration only gates the output. Each path has a fixed number of pipeline stages, listed in
RISC-Q's docs/dsp/PulseGenerator.md and copied into the structure Datapath of
QControl/RiscQ/Timing.lean. The lead of a parameter is how many batches before the pulse starts
it must leave its queue:
| Parameter | Path (batches) | Lead |
|---|---|---|
| Amplitude | carrier amplitude 27 + complex multiply 6 + gate 1 | 34 |
| Phase | carrier phase 28 + 6 + 1 | 35 |
| Carrier frequency | carrier frequency 29 + 6 + 1 | 36 |
| Phasors after a frequency change | 16 lanes + CORDIC 19 + 2, phasor 8, + 6 + 1 | 52 |
| Envelope address | envelope reader 4 + 6 + 1 | 11 |
| Duration | gate 1 + 1 | 2 |
The theorem default_leads checks by computation that these sums equal 34, 35, 36, 52, 11 and 2, the
constants RISC-Q reports for a 16-lane DAC channel. phase_is_slowest_fired_lead states that of the four
parameters a fire sends, the phase has the largest lead.
Of the parameters a fire sends (amplitude, phase, envelope address, duration), which must leave its queue earliest?
- The amplitude, 34 batches before the start.
- The phase, 35 batches before the start.
- The envelope address, 11 batches before the start.
- The duration, 2 batches before the start.
freq, not with a fire.2Timed queues
In this section: the rule a queue follows, and the three theorems about when a parameter lands.
Each parameter of a channel has its own timed queue
(TimedQueue.scala). A fire pushes the entry's fields, together with the start time, into the queues 2
batches after the write arrives (PulseParamBuffer.md). Each queue then tests, every batch,
time ≥ startTime − lead
and pops its head when the test holds. Two more rules come from the register implementation: an entry can pop at the
earliest 3 batches after its push (pushMargin), and two pops of one queue are at least 2 batches apart
(popInterval). The Lean file proves them as popCycle_ge_push and
popCycle_spacing.
A popped parameter reaches the converter lead batches later. The main theorem says when that works:
land_exact. If an entry is ready (pushed, plus the margin, plus the spacing
after the previous pop) no later than lead batches before its start time, and the start time is less
than 2³¹ batches away, the parameter lands exactly when the batch-time register reads the start time.
The arrival time of the write does not appear in the conclusion. land_independent_of_arrival spells
that out: two writes that both meet the margin land on the same batch, whatever link latency separated them. This is
why the posted link can be pipelined freely. The third theorem covers the other case:
land_late says that an entry that misses the margin pops as soon as it is ready and lands late by
exactly the amount it missed.
Timing.lean, proved by Lean from definitions taken from RISC-Q's Scala source and documents. They are not
proofs about the RTL. Conformance tests (tests/riscq-firmware.test.mjs) check that three RISC-Q firmware
binaries reproduce what RISC-Q's own RTL simulations assert, for example that pulse_sched.elf opens its
pulse exactly at its start time.3One pulse, batch by batch
Here is the first x90 of qubit 0 again (lesson A3), now with the queue rules.
Its start time is 320.
| Batch | Event | Where you see it |
|---|---|---|
| 217 | The core's wait for its issue time ends | frame q0:129.resume |
| 227 | The fire store leaves the core, after the phase offset (220) and start time (224) | q0:137 put |
| 230 | The fire arrives at q0.gate | e162 fire |
| 232 | The entry is pushed into the queues (fire + 2) | not a frame |
| 235 | The entry is ready (push + 3) | not a frame |
| 285 | Phase pops (320 − 35); amplitude at 286, envelope address at 309, duration at 318 | not frames |
| 320 | All parameters land; the window opens | e164 pulse: phaseLands = ampLands = envLands = 320 |
The entry was ready at 235 and needed to be ready by 320 − 35 = 285. It had 50 batches to spare. With that margin
the hypothesis of land_exact holds and the window opens on batch 320 whatever happened on the way.
e162. Its state shows startTime 320 and
lands 320. Press Next pulse to jump to the window opening.How many batches before its start time does the fire of h2 reach q0.gate?
By how many batches does the h2 entry beat the deadline of its slowest parameter? Compute
start − (fire + 2 + 3) − 35.
program.model.fireToPush; the push margin 3 and the phase lead 35 are proved constants.4Why 96
The firmware does not compute a lead per parameter. It issues every timed write a fixed
lead of 96 batches before its start time: LEAD = 96 in
riscq/soc.mjs, the value of LEAD in RISC-Q's map.py, which covers the link,
the slowest fired queue lead and the jitter of the core. On the ring that is 192 ns.
The firmware waits for its issue time with two control registers: it stores the issue time to
timeCmp (0x4000) and reads waitTimeCmp (0x4008). The read stalls
the core until its time register has nearly reached the value (time + 3 ≥ timeCmp,
ControlMemMaps.scala). In the trace this is a wait frame followed by a
resume frame. Note that the core's time lags the converter's batch counter by the link
depth. RISC-Q says the lag is constant without giving a number; the model assumes it equals the link depth. At the
first read of the run, at batch 4, core q0 reads 2.
5One write at a time: issue order and preroll
In this section: how the compiler orders the writes of a core, and why a segment may start later than planned.
A core issues one write at a time, and each costs it time. The compiler (issueOrder in
riscq/firmware.mjs) budgets an inline operation at 20 batches plus 5 for each field or phase offset it
sets plus 4 if it is conditioned. It sorts the operations by issue time and walks backward: each issue moves early
enough that the next one can still leave on time. A segment that only fires and changes carriers is issued by a loop
over a four-word table (issue time, start time, address, data) that costs 32 batches per write
(TABLE_COST).
Qubit 0 shows the effect. Its x90 starts at 0 and its CZ tone 18 batches later. The CZ must be issued at
18 − 96 = −78 relative to the time base. The x90 costs 25 batches (it sets the phase offset), so it
must be issued by −78 − 25 = −103, seven batches earlier than the lead allows. The compiler cannot move the first
write before the segment's time base minus 96, so it moves the whole segment 7 batches later. That shift is the
preroll. The run lists it per segment in preroll, and the
Program section of the detailed debugger says "delayed 7 batches (14 ns) so that its widest burst of writes leaves
on time". It is also why the first x90 opens at 320 = 313 + 7, where 313 is the time base after the
barrier.
By how many batches is segment 0 of the feed-forward run delayed?
x90 that must leave 25 batches before the CZ of the same core. Segment 1
holds one conditioned pulse and needs no preroll.
The same mechanism limits how many pulses one core can start on the same batch. The limit sweeps in
riscq/limits.mjs build a one-core machine at 100 MHz and fire n channels on one batch with the
table loop. The tests (tests/riscq-limits.test.mjs) find that all n pulses open on the same batch,
on time, after a preroll of (n − 1) · 32 batches: 0, 32, 96, 224 and 480 for 1, 2, 4, 8 and 16 pulses.
Queues have a depth too. A channel's queues hold one entry more than the build's queue depth (5 on the default build
of depth 4, 33 on x6y3), and the compiler refuses a write that would need a sixth entry. For a long
back-to-back train of equal pulses on one channel, the tests find the shortest pulse that still fits at 48, 31, 29
and 16 batches for queue depths 1, 4, 8 and 32.
The table loop issues one timed write every 32 batches. How much preroll do 16 pulses need that must all open on the same batch from one core?
- 32 batches.
- 96 batches, one lead.
- 480 batches.
- 512 batches.
6When a write is late
The lead hides the link, but only up to a point. Every stage of the link delays the write and, under the model's assumption, also delays the core's copy of the time by one batch. On the one-core 100 MHz build of the limit sweeps, the tests find that a pulse issued 96 batches ahead opens on time up to 13 link stages; at 14 the phase lands late while the window still opens. That limit depends on the assumed lag.
You can force the same failure on the ring. The ring is also available as a hardware design document
(lesson D1), whose board has a field linkStages, 2 by default. The exercises
below run the feed-forward program on that design with linkStages set to 30. From the command line:
node riscq/cli.mjs --ring-design > machine.json, change "linkStages": 2 to 30, then
node riscq/cli.mjs --design machine.json --qasm program.qasm --outcome c=1.
With 30 stages the fire of the first x90 reaches its channel only 22 batches before its start time of
348. The entry is ready at 331, but the phase had to be ready by 348 − 35 = 313. It misses by 18 batches, and as
land_late says, it lands at 331 + 35 = 366, exactly 18 late. The envelope address and the duration,
with leads of 11 and 2, still land at 348, so the window does open on time. But its parameters did not land on the
same batch, and the model reports a fault: "pulse parameters did not land on the same batch". The window lasts 18
batches, 348 to 366, so its new amplitude and phase arrive only as it closes. The model records the landing batches;
it does not compute what the DAC would have played meanwhile.
Run the ring design with linkStages = 30 (the answer check does it for you). At which batch does the
phase of the first x90 on qubit 0 land?
phaseLands = 366 for a start time of 348: ready at 331, plus
the phase lead of 35. The amplitude lands at 365, one batch earlier, because its lead is one less.How many faults does that run report in all?
x is worse:
its window also opens a batch after its start time, which is a second fault. The detailed debugger draws all of
them in red on the Channel timeline, and Next fault in the workspace steps through them.Which theorem of Timing.lean says that a parameter that misses its margin lands late by exactly the
amount it missed? Give its name.
land_late. With land_exact it covers both cases, so a late write never
lands at an unpredictable time: it lands as soon as it is ready plus its lead.What to remember
- Each parameter has a lead, derived from the pulse generator's pipeline: 34, 35, 36, 52, 11 and 2 batches.
- A timed queue pops a parameter its lead before the start time;
land_exactproves it lands on the start batch if it was ready in time. - The firmware issues every timed write 96 batches ahead, which covers the link, the slowest lead and the core's jitter.
- A core issues one write at a time; when a burst cannot fit, the segment starts later by its preroll.
- A late write lands late by exactly what it missed (
land_late), and the model reports the fault.