Realtime Decoding with the Playback Emulator

The Playback Emulator sends measurements and correction requests on a timed schedule, without a QPU or FPGA. These examples show two ways to use it:

Neither example measures decoding accuracy over a noise distribution or establishes a hardware feedback-latency guarantee.

In-Process Repetition-Code Playback

The example below configures a lookup-table decoder for a distance-3 repetition code, then plays back ten fixed shots against it, alternating between no error and a flipped data qubit. It prints each shot’s correction (0, then 1) and decoding latency.

Prerequisites

Use a realtime-enabled CUDA-Q QEC Python installation. The combined CUDA-Q/QEC Docker container includes this interface; the 0.9.0 pip wheels do not. This example uses the built-in lookup-table decoder and needs no GPU or running decoding server. Before running it, check that Python playback is available:

python3 -c 'import cudaq_qec as qec; assert hasattr(qec, "playback"), "A realtime-enabled CUDA-Q QEC Python installation is required"'

Run the Example

Download playback_emulator.py into your Python environment, or use docs/sphinx/examples/qec/python/playback_emulator.py from a matching source checkout. Run it from the directory containing the file:

python3 playback_emulator.py

The program builds a detector error model and measurement mapping for the repetition-code memory circuit. It then replays fixed measurements, resetting the decoder before each shot and sending the final data-qubit readout before requesting a correction:

import cudaq
import cudaq_qec as qec

code, rounds = qec.get_code("repetition", distance=3), 3
noise = cudaq.NoiseModel()
noise.add_all_qubit_channel("x", cudaq.Depolarization2(0.01), 1)
# Configure one lookup-table decoder (id 0) for this memory experiment.
ctx = qec.decoder_context_from_memory_circuit(code, qec.operation.prep0, rounds,
                                              noise)
dem, m2d, _ = ctx.full_component()
config = qec.decoder_config()
config.id, config.type = 0, "multi_error_lut"
config.syndrome_size, config.block_size = dem.detector_error_matrix.shape
config.H_sparse = qec.pcm_to_sparse_vec(dem.detector_error_matrix)
config.O_sparse = qec.pcm_to_sparse_vec(dem.observables_flips_matrix)
config.D_sparse = qec.d_sparse(m2d)
decoders = qec.multi_decoder_config()
decoders.decoders = [config]

# One shot: reset, 3 stabilizer rounds, data readout, then decode. Each shot
# starts 100 ticks (100 us) after the previous one finished sending.
shot = """\
+100 reset
-    stream source=0 rounds=3
-    enqueue_data source=0
-    get_corrections return_size=1
"""
schedule = shot * 10
# Raw measurements for one shot: 3 rounds of 2 ancilla bits, then 3 data bits.
# Shots alternate between no error and a flip of data qubit 0.
clean = [[0, 0]] * 3 + [[0, 0, 0]]
flipped = [[1, 0]] * 3 + [[1, 0, 0]]
source = dict(type="static", rounds=(clean + flipped) * 5)
result = qec.playback.run(schedule,
                          tick_ns=1000,
                          sources={0: source},
                          decoders=decoders)
for r in result.records[3::4]:  # The get_corrections records
    print(f"status={r['status']} correction={r['correction_bits']} "
          f"latency={(r['return_ns'] - r['call_ns'])/1000:4.1f} us")

Check the Result

Expect ten lines with status=0 and correction bits alternating 0, 1, repeated five times. These are logical correction bits: the flipped shots require a correction of 1. The reported latency is the elapsed time from the correction request to its reply; it varies between runs and does not include the preceding measurement stream.

A message saying the example was skipped is not a successful check; use an installation that passes the prerequisite command above.

Use the Same Configuration with a Server

To play the same schedule against a decoding server instead, save the decoders with decoders.to_yaml_str() to config.yaml, start the server, and pass udp_endpoints={0: "127.0.0.1:50000"} to run() in place of decoders=:

decoding_server --config=config.yaml --transport=udp --port=50000

This alternative requires the matching decoding_server executable and access to the saved YAML. The next example supplies the complete server setup and command-line playback sequence for a surface-code experiment.

Playback Against a Decoding Server

The software playback emulator acts as the client of a standalone decoding server, replaying measurement and correction requests on a timed schedule. This exercises the realtime decoding path without running a quantum circuit or using a QPU or FPGA. You will generate a surface-code memory-experiment configuration, replay one synthetic all-zero shot, and check the returned correction. The result verifies configuration and communication; it does not measure decoding accuracy or establish a hardware feedback-latency guarantee.

Prerequisites

Follow Set Up the Prebuilt Container to obtain the prebuilt image, create QEC_RUN_DIR and its memory-experiment subdirectory, and open the reusable qec-decoding container. These commands use CPU PyMatching over UDP; no GPU or host CUDA-Q SDK is required. Start in the container’s shared run directory, $QEC_RUN_DIR.

Generate the Configuration

In the container’s shared run directory, enter the memory-experiment subdirectory created on the host. Generate a distance-3, Z-basis experiment with 26 extraction rounds and PyMatching:

cd memory-experiment
memory-experiment-generator \
  --distance=3 --rounds=26 --basis=Z --error-rate=0.001 \
  --decoder=pymatching --output=memory.yaml

Wait for Generated server configuration: ... and exit status zero before using the file. The output directory must exist; an existing file or symlink is never overwritten. Choose another filename when changing an experiment.

For the supported geometries, decoder profiles, and argument meanings, see Configure a Surface-Code Memory Experiment.

Run Playback

This example uses the software playback emulator as the client, so no QPU is required. Real QPU and CUDA-Q clients send measurements and correction requests directly; they do not need a playback schedule. See Send the Experiment’s Measurements for that workflow. For schedule syntax, CLI and Python entry points, and source and session configuration, see Playback Emulator.

In this container terminal, start the CPU PyMatching server using the generated file. Leave it running in the foreground:

decoding_server \
  --config=memory.yaml --transport=udp --port=50000 --timeout=3600

Wait for QEC_DECODING_SERVER_READY before sending measurements. If port 50000 is unavailable, choose another free port and use it in both commands.

The schedule below is read by the playback emulator, not the decoding server. On the host, save it as $QEC_RUN_DIR/memory-experiment/memory_schedule.txt. That directory is shared with the container. The schedule resets the decoder, sends 26 eight-bit stabilizer rounds and a nine-bit final readout, then requests the correction. These synthetic, all-zero measurements match the distance-3 configuration above. They test the configuration-to-server handoff, not accuracy or performance:

memory_schedule.txt
# Synthetic distance-3 Z-memory smoke test: 26 extraction rounds plus readout.
# Not sampled circuit data or an accuracy/performance benchmark.
0 reset
1 stream source=0b00000000 rounds=26 every=1
- enqueue_data source=0b000000000
- get_corrections 0 return_size=1

Open a second container shell as described in Open the Decoding-Server Container. It starts in the shared run directory. Run:

cd memory-experiment
playback-emulator \
  --config=memory.yaml --schedule=memory_schedule.txt \
  --backend=udp --udp-endpoint=0:127.0.0.1:50000 --tick=1ms \
  --out=memory-result.csv

Check the Result

In get_corrections 0 return_size=1, 0 is the expected correction bit; return_size=1 requests one bit. Pass --config to playback too: its enqueue_data operation uses the YAML’s FINAL mapping. Check that the correction row has status=0, read_completed=1 and correction_bits=0, matching the expected zero correction. Stop the server with Ctrl+C in its original terminal. The shell remains available to generate another configuration and start the server with a different --config path. Use new filenames for each experiment’s YAML and results.

The YAML, schedule and CSV remain on the host under $QEC_RUN_DIR/memory-experiment. These are functional checks, not an end-to-end latency or throughput benchmark.

Next Steps