Real-Time Decoding with a Deadline#
Quantum error correction measures syndromes, decodes them into corrections, and applies them, round after round. In real time, the decoder must answer while the qubits wait, and the program needs a plan for when it doesn’t.
This tutorial runs one round of a distance-3 repetition code: pulses extract the syndrome, a decoder is asked for the corrections, and a table on the controller takes over when the decoder is late.
What you’ll learn:
how to extract a syndrome with pulses on coupled qubits;
how to call a decoder from a kernel with a deadline;
how to fall back to a table on the controller;
how to check both arms in simulation;
how the same round calls a CUDA-Q QEC decoder.
All the code on this page is also one Python script,
realtime_decoding.py.
The code and its syndrome#
Three data qubits hold one logical bit, and two ancillas hold the syndrome: the parity of data qubits 0 and 1, and of 1 and 2.
A single bit flip shows up as a unique syndrome, which says which qubit to flip back:
Syndrome (s0, s1) |
Flipped data qubit |
|---|---|
(0, 0) |
none |
(1, 0) |
0 |
(1, 1) |
1 |
(0, 1) |
2 |
The decoder below answers with that table:
import cudaq_pulse as pulse
def lookup(syndrome: pulse.Bits) -> pulse.Bits:
s0, s1 = syndrome[0], syndrome[1]
# One correction bit per data qubit.
return [s0 & (1 - s1), s0 & s1, s1 & (1 - s0)]
What lookup corrects for each syndrome. Circles are data qubits,
squares the syndrome bits between them.#
Four small diagrams of the three data qubits with the two syndrome bits between them, one per syndrome, with what the page's lookup corrects: syndrome (0, 0), no flip; syndrome (1, 0), flip data qubit 0; syndrome (1, 1), flip data qubit 1; syndrome (0, 1), flip data qubit 2.
What to notice: a lit syndrome bit points at the data qubit next to it. Two lit bits point at the one they share.
A real decoder, such as the one in the last section, does the same job for codes whose table doesn’t fit in the controller’s memory.
Declare it as a classical function. Corrections come back in about 62 µs, and 75 µs at worst:
decode = pulse.classical(
lookup,
link="gpu",
nominal_latency_ns=62_000,
max_latency_ns=75_000,
)
pulse.classical declares a function you already have, as well as
decorating a new one. That makes it easy to declare it again with another
budget, as the simulation below does.
Note
The latencies are round trips from a host process to a decoding server
on a GPU in the same workstation. On NVQLink, requests go from the
controller to the GPU without the host, in a few microseconds.
benchmarks/classical/decoder_round_trip.py measures a host’s round
trip on your own machine.
Five transmons in a row#
The round runs on five coupled transmons, data qubits and ancillas alternating along a chain: data 0, ancilla 0, data 1, ancilla 1, data 2. Neighbours sit 50 MHz apart and exchange at 4 MHz. Describe them as Describe Your Own Quantum System does, with the data qubits as qudits 0 to 2 and the ancillas as 3 and 4:
import itertools
from cudaq_pulse.quantum_systems import (
Coupling,
DragPulse,
PhysicalQudit,
)
# Qudit indices along the chain.
CHAIN = [0, 3, 1, 4, 2]
x_pulse = {pulse.gates.X: DragPulse(duration_ns=40.0, sigma_ns=10.0)}
qudits = {}
for position, index in enumerate(CHAIN):
qudits[index] = PhysicalQudit(
index=index,
frequency_hz=5.0e9 + position * 50e6,
t1_us=60.0,
t2_star_us=40.0,
anharmonicity_hz=-330e6,
calibrations=x_pulse,
)
couplings = []
for pair in itertools.pairwise(CHAIN):
couplings.append(Coupling(pair, strength_hz=4.0e6))
system = pulse.quantum_system(
name="repetition_chain",
qudits=qudits,
couplings=couplings,
)
data = system.qudits[0:3]
ancillas = system.qudits[3:5]
Extract the syndrome with pulses#
An ancilla holds the parity of its two neighbours once each of them has turned it. The turn is a cross-resonance pulse: a data qubit driven at its neighbouring ancilla’s frequency. Through their coupling, that drive rotates the ancilla one way when the data qubit is in 0 and the other way when it is in 1.
Each pulse here is a quarter turn. On this chain an ancilla’s two neighbours turn it in opposite senses, one sitting below it in frequency and one above. So two equal neighbours cancel and leave the ancilla in 0, and two different ones add up to a half turn and leave it in 1:
@pulse.kernel
def extract(data, ancillas):
cross = gaussian_square(
190 * pulse.ns,
0.12,
10 * pulse.ns,
150 * pulse.ns,
)
for step in range(2):
# Step 0: data 0 turns ancilla 0, data 1 turns ancilla 1.
# Step 1: data 1 turns ancilla 0, data 2 turns ancilla 1.
for check in range(2):
line = get_drive_line(data[check + step])
ancilla_tone = get_drive_line(ancillas[check]).tone
drive(line, cross, tone=ancilla_tone)
sync(
get_drive_line(data[0]),
get_drive_line(data[1]),
get_drive_line(data[2]),
)
What to notice: tone= plays the pulse on the data qubit’s drive line at
the ancilla’s frequency. The pulse’s amplitude and its 150 ns flat top set
the quarter turn.
The simulation further down reads the syndrome these pulses leave for every flip.
One round, with a deadline#
The round flips a data qubit or none with a π pulse, extracts the syndrome, reads both ancillas and asks the decoder. The qubit to flip and the deadline are kernel arguments. Then each data qubit takes the decoder’s answer, or the table’s if the answer is late:
NO_ERROR = -1
@pulse.kernel
def qec_round(data, ancillas, error: int, deadline_ns: int):
measurement = square(400 * pulse.ns, 0.05)
# The error: a π pulse on one data qubit, or none.
if error != NO_ERROR:
line = get_drive_line(data[error])
drive(line, line.get_calibration(pulse.gates.X))
extract(data, ancillas)
sync(
get_drive_line(data[0]),
get_readout_line(ancillas[0]),
get_readout_line(ancillas[1]),
)
s0 = readout(get_readout_line(ancillas[0]), measurement)
s1 = readout(get_readout_line(ancillas[1]), measurement)
request = decode.submit([s0, s1])
in_time, fix = request.result(timeout=deadline_ns * pulse.ns)
# The fallback, computed on the controller.
table = [s0 and not s1, s0 and s1, s1 and not s0]
for i in range(3):
flip = fix[i] if in_time else table[i]
if flip:
line = get_drive_line(data[i])
drive(line, line.get_calibration(pulse.gates.X))
What to notice:
the syndrome the decoder gets is what the ancillas’ readouts return;
the correction is each data qubit’s calibrated π pulse;
the deadline is an argument, so you can trade waiting against falling back without editing the program.
Check the timing#
Compiling checks the timing before anything runs. Compile the round for the chain, with a link to the GPU:
gpu_link = pulse.Link(feedback_ns=50)
control_system = pulse.ControlSystem(links={"gpu": gpu_link})
compiled = pulse.compile(
qec_round,
quantum_system=system,
control_system=control_system,
)
With no error and an 80 µs deadline, find when the answer is due:
scheduled = compiled.lower(
data,
ancillas,
NO_ERROR,
deadline_ns=80_000,
)
call = scheduled.classical_report()[0]
sent_ns = call.submit_cycles / system.clock_ghz
send_ns = call.submit_duration_cycles / system.clock_ghz
due_ns = call.due_cycles / system.clock_ghz
print(f"syndrome sent at {sent_ns} ns, for {send_ns} ns")
print(f"answer due at {due_ns} ns")
Output
syndrome sent at 780.0 ns, for 0.5 ns
answer due at 80830.5 ns
The syndrome is sent when the readouts end: after the 380 ns of extraction
and their 400 ns. Sending it holds the link for 0.5 ns: this link is
declared with no sending time of its own (no fixed_ns), and a request
holds its link for at least one clock cycle, which is 0.5 ns at 2 GHz.
Then come the 80 µs wait and the link’s 50 ns of feedback. That makes
780 + 0.5 + 80,000 + 50 = 80,830.5 ns. The corrections are scheduled after
it.
Simulate both arms#
Now check what the round reads and how often it restores the data qubits.
simulate_round simulates the round for one flip, branching on the
readouts and on whether the answer arrives in time. Three helpers read a
result: the syndrome the ancillas’ readouts gave most often, the share of
shots that used the decoder’s answer, and the share whose data qubits end
back in 000:
def simulate_round(compiled, error, deadline_ns):
program = compiled.lower(
data,
ancillas,
error,
deadline_ns=deadline_ns,
)
return pulse.simulate(
program,
levels=2,
latency_branching=True,
dt_ns=1.0,
)
def syndrome_read(result):
shares = {}
for branch in result.branches:
outcomes = tuple(branch.levels)
shares[outcomes] = shares.get(outcomes, 0.0) + branch.probability
likeliest = max(shares, key=lambda outcomes: shares[outcomes])
return likeliest, shares[likeliest]
def decoder_used(result):
used = 0.0
for branch in result.branches:
if branch.calls and branch.calls[-1].in_time:
used += branch.probability
return used
def data_restored(result):
restored = 0.0
for index in range(32):
# The data qubits are the three lowest bits.
if index & 0b111 == 0:
restored += result.density_matrix[index, index].real
return restored
levels=2 simulates two levels per transmon. Leakage to the third
level, which a cross-resonance pulse on hardware has to keep small, isn’t
in this simulation.
Run the round with a 1 µs deadline, for no error and for a flip of each data qubit:
ERRORS = (NO_ERROR, 0, 1, 2)
results = {}
print("flipped syndrome share of shots data back in 000")
for error in ERRORS:
result = simulate_round(compiled, error, deadline_ns=1_000)
results[error] = result
(s0, s1), share = syndrome_read(result)
flipped = "none" if error == NO_ERROR else f"data {error}"
back = data_restored(result)
print(f"{flipped:7} ({s0}, {s1}) {share:.3f} {back:.3f}")
Output
flipped syndrome share of shots data back in 000
none (0, 0) 0.995 0.995
data 0 (1, 0) 0.980 0.927
data 1 (1, 1) 0.960 0.920
data 2 (0, 1) 0.978 0.927
What to notice:
the readouts give the syndrome of the table at the top of the page for every flip, on 96% to 98% of shots, with no syndrome put in by hand;
the shots that read another syndrome are the extraction’s own error: the qubits decay and dephase during its 380 ns, and the quarter turns aren’t exact;
part of that error is the coupling itself. It is always on, so a flipped data qubit alone is not an eigenstate of the chain: it shares its excitation with each neighbouring ancilla by about the square of the coupling over the detuning, (4 MHz / 50 MHz)2 = 0.6%. So an ancilla next to a flipped data qubit can read 1 with no extraction pulse at all.
summary averages results, each an equal share of the shots:
def summary(results):
results = list(results)
used = sum(decoder_used(r) for r in results) / len(results)
restored = sum(data_restored(r) for r in results) / len(results)
return round(float(used), 3), round(float(restored), 3)
used, restored = summary(results.values())
print(f"decoder's answer used: {used}")
print(f"data back in 000: {restored}")
Output
decoder's answer used: 0.0
data back in 000: 0.942
The decoder’s 62 µs answer is late on nearly every shot: the share that uses it rounds to 0.0. So the table makes nearly every correction.
Where the other 0.058 goes: with no error the data qubits stay in 000 on 0.995 of shots, and with a flip they are restored on 0.92 to 0.93. Two things take most of that. The extraction reads another syndrome on 2% to 4% of the shots with a flip, the always-on coupling’s share included, and then the wrong qubit is corrected or none. And the flipped qubit waits 1.9 µs for its correction, from its flip to the end of the deadline, and decays on about 3% of shots at its T1 of 60 µs; the correction then excites it again.
The round with a flip of data 1 and the 1 µs deadline. “at a0” marks a pulse played at ancilla 0’s tone. Dashed corrections play only when flagged.#
Timeline of one round of the distance-3 repetition code, with a lane for each data qubit's drive line, each ancilla's readout line and the GPU link. A π pulse flips data 1, the error. Then the extraction takes 380 ns in two steps of cross-resonance pulses, each on a data qubit's drive line at an ancilla's tone: data 0 at ancilla 0's and data 1 at ancilla 1's, then data 1 at ancilla 0's and data 2 at ancilla 1's. Both ancillas are read out for 400 ns, and their outcomes go to the decoder over the link. The answer is due at 1870.5 ns, and a correction pulse on each data qubit, which plays only if the decoder's answer or the fallback table says so, starts at that moment.
What to notice: the extraction’s four pulses come in two steps, and after the readouts the round is mostly the wait between the syndrome and the corrections.
Up close, the end of that wait:
The last 110 ns of the wait, and the corrections.#
The last 110 ns of the round's wait. The kernel waits for the answer with a 1 microsecond timeout, plus 50 ns of feedback on the link, so the answer is due at 1870.5 ns. The dashed correction pulses on the data qubits start at that moment, whichever arm decides them.
What to notice: every correction waits for the deadline, whichever arm decides it.
Give the decoder a budget that fits#
Declare the decoder again with a 500 ns budget, compile again, and run the round for a flip of data 1:
decode = pulse.classical(lookup, link="gpu", max_latency_ns=500)
fast = pulse.compile(
qec_round,
quantum_system=system,
control_system=control_system,
)
in_time = simulate_round(fast, 1, deadline_ns=1_000)
used, restored = summary([in_time])
print(f"decoder's answer used: {used}")
print(f"data back in 000: {restored}")
Output
decoder's answer used: 1.0
data back in 000: 0.92
A kernel reads the functions it calls when it compiles, so the new declaration is picked up.
The two runs with a flip of data 1 and a 1 µs deadline, side by side.#
The round with a flip of data 1 and a 1 microsecond deadline, for the decoder as first declared and with a 500 ns budget. The share of shots that use the decoder's answer is 0 and 1; the share whose data qubits end back in 000 is 0.92 and 0.92.
What to notice:
every shot now uses the decoder’s answer;
the data qubits are restored as often as in the row for data 1 above, where the table corrected, because the two agree on every syndrome;
that agreement is what makes the fallback safe, and checking it before the program runs on hardware is the point of the exercise.
The trade-off#
Between the two budgets lies the real choice. With the decoder as first declared, each deadline trades waiting for the answer against the decay of a flipped qubit that waits for its correction:
The first compiled round, averaged over the four cases of ERRORS,
for deadlines from 1 to 75 µs.#
Two curves against the decoding deadline, from 1 to 75 microseconds, averaged over no error and a flip of each data qubit, simulated with the decoder's 62 microsecond nominal and 75 microsecond maximum latency. The share of shots that use the decoder's answer rises from 0 at 1 microsecond through 0.54 at 50 to 1 at the maximum. The share whose data qubits end back in 000 falls from 0.942 at 1 microsecond to 0.461 at 75, because a flipped qubit decays while it waits for its correction.
What to notice: waiting lets more shots use the decoder, and lets a flipped data qubit decay before it is corrected. This round’s table is as good as the decoder, so here the shortest deadline wins.
With a CUDA-Q QEC decoder#
CUDA-Q QEC provides GPU decoders
and their real-time functions. pulse.classical takes them as they are,
with a link and a latency budget.
This section needs:
Linux, an NVIDIA GPU, and Python 3.12 or 3.13;
pip install cudaq-qec.
The test suite runs it only there. The code comes from examples/27_realtime_decoding.py.
Import CUDA-Q QEC:
import cudaq_qec
Configure decoder 0, BP+OSD for the distance-3 repetition code:
# Decoder 0: BP+OSD for a distance-3 repetition code.
cudaq_qec.configure_decoders_from_str("""
decoders:
- id: 0
type: nv-qldpc-decoder
block_size: 3
syndrome_size: 2
H_sparse: [0, 1, -1, 1, 2, -1]
O_sparse: [0, -1, 1, -1, 2, -1]
D_sparse: [0, -1, 1, -1]
decoder_custom_args:
error_rate_vec: [0.1, 0.1, 0.1]
use_sparsity: true
max_iterations: 50
use_osd: true
osd_method: 3
osd_order: 7
""")
Declare its real-time functions, with the same link and budget as
decode:
# Its real-time calls, sent to the decoding server over the GPU link.
enqueue_syndromes = pulse.classical(
cudaq_qec.qec.enqueue_syndromes,
link="gpu",
)
get_corrections = pulse.classical(
cudaq_qec.qec.get_corrections,
link="gpu",
nominal_latency_ns=62_000,
max_latency_ns=75_000,
)
reset_decoder = pulse.classical(
cudaq_qec.qec.reset_decoder,
link="gpu",
)
The round sends the syndrome with one call and takes the corrections with another:
@pulse.kernel
def qec_round(data, ancillas, error: int, deadline_ns: int):
measurement = square(400 * pulse.ns, 0.05)
# The error: a pi pulse on one data qubit, or none.
if error != NO_ERROR:
line = get_drive_line(data[error])
drive(line, line.get_calibration(pulse.gates.X))
# Syndrome extraction, then the ancillas' readouts.
extract(data, ancillas)
sync(
get_drive_line(data[0]),
get_readout_line(ancillas[0]),
get_readout_line(ancillas[1]),
)
s0 = readout(get_readout_line(ancillas[0]), measurement)
s1 = readout(get_readout_line(ancillas[1]), measurement)
# Decoder 0, round 0.
enqueue_syndromes.submit(0, [s0, s1], 0)
request = get_corrections.submit(0, 3, False)
in_time, fix = request.result(timeout=deadline_ns * pulse.ns)
The corrections use the same deadline and fallback as before:
# The decoder's answer, or the local table if it is late.
table = [s0 and not s1, s0 and s1, s1 and not s0]
for i in range(3):
flip = fix[i] if in_time else table[i]
if flip:
line = get_drive_line(data[i])
drive(line, line.get_calibration(pulse.gates.X))
Compile it for the same chain and simulate it as before, for the four
cases of ERRORS:
decoded = pulse.compile(
qec_round,
quantum_system=system,
control_system=control_system,
)
late = [simulate_round(decoded, e, deadline_ns=1_000) for e in ERRORS]
used, restored = summary(late)
print(f"decoder's answer used: {used}")
print(f"data back in 000: {restored}")
Output
decoder's answer used: 0.0
data back in 000: 0.943
As with decode, the 62 µs answer is late and the table corrects. Give
this decoder the 500 ns budget too, and every correction is its answer:
get_corrections = pulse.classical(
cudaq_qec.qec.get_corrections,
link="gpu",
max_latency_ns=500,
)
decoded_fast = pulse.compile(
qec_round,
quantum_system=system,
control_system=control_system,
)
in_time = []
for error in ERRORS:
result = simulate_round(decoded_fast, error, deadline_ns=1_000)
in_time.append(result)
used, restored = summary(in_time)
print(f"decoder's answer used: {used}")
print(f"data back in 000: {restored}")
cudaq_qec.finalize_decoders()
Output
decoder's answer used: 1.0
data back in 000: 0.943
What to notice: BP+OSD on the GPU and the three-line table restore the
data qubits equally often, from syndromes the pulses extracted. The round
with CUDA-Q QEC sends two requests where decode sends one, which moves
its corrections by one clock cycle and the last digit of the result.
On hardware, these requests are the messages of CUDA-Q QEC’s decoding server. In simulation, the configured decoder answers them in the same process.
Where to go next#
examples/27_realtime_decoding.py runs this round end to end and compares both arms with no correction at all.
examples/19_classical_calls.py covers decoding with feed-forward, call slack and hidden latency.
examples/18_per_kernel_schedules.py schedules a surface-code-style stabilizer cycle with dynamical decoupling.
examples/transmon/transmon_surface17_surface_code_cycle.py lays out one error-correction cycle of the 17-qubit surface code.
Call Classical Code During a Shot describes the decoder messages, emulation in virtual time, and how a late answer is reported.