Decoding with a Dynamic DEM
This example constructs a dynamic detector error model (DEM) for a distance-5 repetition-code memory experiment, then uses PyMatching to decode a complete six-round shot of synthetic measurements containing a known data-qubit error. It shows the same phase description in YAML, Python, and C++. For phases, seams, measurement mappings, and when to use them, start with Dynamic Detector Error Models.
Use a CUDA-Q QEC installation with the PyMatching decoder available. The C++
example also requires the nvq++ compiler and CUDA-Q QEC development
libraries; see the Installation Guide.
Describe the Repetition-Code Experiment
The experiment has five data qubits and four stabilizer checks. Its DEM includes data-qubit flips and measurement errors, with a probability of 0.02 for each fault. The final round reads the five data qubits:
dem_chunks:
seam: {from: next_round, to: prev_round}
measurements_per_round: 4 # one per check
connections:
- {from: init, to: bulk}
- {from: bulk, to: bulk} # bulk repeats
- {from: bulk, to: final}
phases:
# Faults 0-4 flip data qubits 0-4; faults 5-8 are errors in measuring
# checks 0-3. Check i reads data qubits i and i + 1. Observable 0 reads
# data qubit 0.
- name: init # first round of checks
spec:
num_faults: 9
seam_specs:
- name: prev_round # detectors of this round
spec: {H_sparse: [0, 1, 5, -1, 1, 2, 6, -1, 2, 3, 7, -1,
3, 4, 8, -1]}
- name: next_round # detectors of the next round
spec: {H_sparse: [5, -1, 6, -1, 7, -1, 8, -1]}
D_sparse: [0, -1, 1, -1, 2, -1, 3, -1]
O_sparse: [0, -1]
error_rates: [0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02]
- name: bulk
spec:
num_faults: 9
seam_specs:
- name: prev_round
spec: {H_sparse: [0, 1, 5, -1, 1, 2, 6, -1, 2, 3, 7, -1,
3, 4, 8, -1]}
- name: next_round
spec: {H_sparse: [5, -1, 6, -1, 7, -1, 8, -1]}
# Detector i: check i in the previous round (index i) and in
# this one (index 4 + i).
D_sparse: [0, 4, -1, 1, 5, -1, 2, 6, -1, 3, 7, -1]
O_sparse: [0, -1]
error_rates: [0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02]
- name: final # data-qubit readout
spec:
num_faults: 5
num_measurements: 5 # data qubits 0-4
seam_specs:
- name: prev_round
spec: {H_sparse: [0, 1, -1, 1, 2, -1, 2, 3, -1, 3, 4, -1]}
# Detector i: check i in the previous round and data qubits i
# and i + 1 in this one.
D_sparse: [0, 4, 5, -1, 1, 5, 6, -1, 2, 6, 7, -1, 3, 7, 8, -1]
m2o_sparse: [0, -1] # observable 0 reads data qubit 0
O_sparse: [0, -1]
error_rates: [0.02, 0.02, 0.02, 0.02, 0.02]
initandbulkeach contain nine faults: five data-qubit flips and four check-measurement errors.finalcontains five data-qubit flips.initstarts the shot,bulkrepeats, andfinalends it. The YAML leavesnum_roundsunset for streaming with the Parallel Window example.The Python and C++ programs below set
num_rounds = 6: oneinitround, fourbulkrounds, and onefinalround.
Check the Round Boundaries
Faults 5 through 8 are check-measurement errors. They affect detectors in
both their own round and the next, so they appear in both prev_round
and next_round seam rows. Faults 0 through 4 are data-qubit flips and
appear only in the current round’s rows in this DEM. The final phase has no
outgoing next_round seam.
See Dynamic Detector Error Models for how seams join these effects across round boundaries.
Follow the Measurements
The DEM’s D_sparse rows form each detector from the measurements:
initcompares each check measurement with its expected value of zero.bulkcompares each check with the same check in the previous round.finalcompares each previous check with the parity of its two adjacent data-qubit readouts. Its five measurements replace the four check measurements of a bulk round.
The final phase’s m2o_sparse: [0, -1] reads data qubit 0 as the raw
logical observable. Its O_sparse: [0, -1] identifies fault 0 as a flip
of that observable. The matching indices refer to different things:
m2o_sparse indexes measurements, while O_sparse indexes faults.
See Measurements and Logical Readout for
the field definitions.
Build the DEM and Decode a Shot
These programs construct the same detector error model and use PyMatching to decode a fixed-length shot. The shot contains a flip of data qubit 0 in round 2, with rounds counted from zero:
def phase(name,
num_faults,
seams,
D_sparse,
num_measurements=None,
m2o_sparse=None):
"""A phase whose faults flip the rows each seam lists, and whose detectors
read the measurements D_sparse lists."""
spec = qec.DemChunkSpec()
spec.num_faults = num_faults
entries = []
for seam, rows in seams:
entry = qec.SeamSpecEntry()
entry.id = seam
entry.spec.H_sparse = rows
entries.append(entry)
spec.seam_specs = entries # assign the list; appending edits a copy
spec.D_sparse = D_sparse
if num_measurements is not None:
spec.num_measurements = num_measurements
if m2o_sparse is not None:
spec.m2o_sparse = m2o_sparse
spec.O_sparse = [0, -1] # observable 0 reads data qubit 0
spec.error_rates = [0.02] * num_faults
entry = qec.PhaseSpecEntry()
entry.id = qec.SeamId(name)
entry.spec = spec
return entry
def connection(from_phase, to_phase):
c = qec.PhaseConnection()
c.from_phase = qec.SeamId(from_phase)
c.to_phase = qec.SeamId(to_phase)
return c
# Faults 0-4 flip data qubits 0-4; faults 5-8 are errors in measuring checks
# 0-3. Check i reads data qubits i and i + 1. The final round reads the data
# qubits.
prev_round, next_round = qec.seam_name.prev_round, qec.seam_name.next_round
checks_seams = [
(prev_round, [0, 1, 5, -1, 1, 2, 6, -1, 2, 3, 7, -1, 3, 4, 8, -1]),
(next_round, [5, -1, 6, -1, 7, -1, 8, -1]),
]
seam = qec.SeamConnection()
seam.from_seam = next_round # this round's next_round rows...
seam.to_seam = prev_round # ...are the next round's prev_round rows
model = qec.DemChunksSpec()
model.seam = seam
model.measurements_per_round = 4 # one per check
model.phases = [
phase("init", 9, checks_seams, [0, -1, 1, -1, 2, -1, 3, -1]),
phase("bulk", 9, checks_seams, [0, 4, -1, 1, 5, -1, 2, 6, -1, 3, 7, -1]),
phase("final",
5, [(prev_round, [0, 1, -1, 1, 2, -1, 2, 3, -1, 3, 4, -1])],
[0, 4, 5, -1, 1, 5, 6, -1, 2, 6, 7, -1, 3, 7, 8, -1],
num_measurements=5,
m2o_sparse=[0, -1]),
]
model.connections = [
connection("init", "bulk"),
connection("bulk", "bulk"),
connection("bulk", "final"),
]
# Fix the shot length to decode complete shots: 5 rounds of checks and the
# final readout.
model.num_rounds = 6
graph = qec.DecodingTaskGraph.from_spec(model, "pymatching")
# Data qubit 0 flips in round 2: from then on, check 0 reads 1, and the final
# readout reads it as 1.
bits = [int(r >= 2 and i == 0) for r in range(5) for i in range(4)]
bits += [1, 0, 0, 0, 0]
shot = qec.MeasurementResults()
shot.bits = bits
corrections = list(graph.run(shot).bits)
print(f"corrections: {corrections}")
// A phase whose faults flip the rows each seam lists, and whose detectors read
// the measurements D_sparse lists; observable 0 reads data qubit 0.
static phase_spec_entry
phase(const char *name, std::uint64_t num_faults,
std::vector<std::pair<seam_id, std::vector<std::int64_t>>> seams,
std::vector<std::int64_t> D_sparse) {
phase_spec_entry entry;
dem_chunk_spec spec;
spec.num_faults = num_faults;
for (auto &[seam, rows] : seams) {
seam_spec_entry seam_entry;
seam_entry.id = seam;
seam_entry.spec.H_sparse = std::move(rows);
spec.seam_specs.push_back(std::move(seam_entry));
}
spec.D_sparse = std::move(D_sparse);
spec.O_sparse = {0, -1};
spec.error_rates.assign(num_faults, 0.02);
entry.id = seam_id{name};
entry.spec = std::move(spec);
return entry;
}
int main() {
// Faults 0-4 flip data qubits 0-4; faults 5-8 are errors in measuring checks
// 0-3. Check i reads data qubits i and i + 1. The final round reads the
// data qubits.
const std::vector<std::pair<seam_id, std::vector<std::int64_t>>> checks{
{seam_name::prev_round,
{0, 1, 5, -1, 1, 2, 6, -1, 2, 3, 7, -1, 3, 4, 8, -1}},
{seam_name::next_round, {5, -1, 6, -1, 7, -1, 8, -1}}};
dem_chunks_spec model;
model.seam = {seam_name::next_round, seam_name::prev_round};
model.measurements_per_round = 4; // one per check
model.phases = {
phase("init", 9, checks, {0, -1, 1, -1, 2, -1, 3, -1}),
phase("bulk", 9, checks, {0, 4, -1, 1, 5, -1, 2, 6, -1, 3, 7, -1}),
phase("final", 5,
{{seam_name::prev_round, {0, 1, -1, 1, 2, -1, 2, 3, -1, 3, 4, -1}}},
{0, 4, 5, -1, 1, 5, 6, -1, 2, 6, 7, -1, 3, 7, 8, -1}),
};
model.phases[2].spec.num_measurements = 5; // data qubits 0-4
model.phases[2].spec.m2o_sparse = {0, -1}; // observable 0 reads qubit 0
model.connections = {{seam_id{"init"}, seam_id{"bulk"}},
{seam_id{"bulk"}, seam_id{"bulk"}},
{seam_id{"bulk"}, seam_id{"final"}}};
// Fix the shot length to decode complete shots: 5 rounds of checks and the
// final readout.
model.num_rounds = 6;
auto graph = decoding_task_graph::from_spec(model, "pymatching");
// Data qubit 0 flips in round 2: from then on, check 0 reads 1, and the
// final readout reads it as 1.
measurement_results shot;
for (std::size_t r = 0; r < 5; ++r)
for (std::size_t i = 0; i < 4; ++i)
shot.bits.push_back(r >= 2 && i == 0);
shot.bits.insert(shot.bits.end(), {1, 0, 0, 0, 0});
const auto corrections = graph->run(shot).bits;
std::cout << "corrections: [" << int(corrections.at(0)) << "]\n";
Compile and run with
nvq++ -lcudaq-qec -lcudaq-qec-decoders dyn_dem.cpp
./a.out
corrections: [1]
The result says that the logical observable needs a flip, as expected from
the injected error. run() returns the correction; the
Parallel Window example also configures
an output for the corrected logical measurement.
In Python, assign whole lists, such as spec.seam_specs = [...]: appending
to a field changes a copy.
See Also
Dynamic Detector Error Models: concepts and construction choices.
Dynamic DEM and Dynamic DEM: the DEM specification types.
Parallel Window: decoding streams of any length.