Skip to content

Commit b95b0d8

Browse files
committed
Renamed decoder_context structs
Signed-off-by: Eliot Heinrich <eheinrich@nvidia.com>
1 parent 8b7eecb commit b95b0d8

6 files changed

Lines changed: 39 additions & 36 deletions

File tree

docs/sphinx/examples/qec/cpp/real_time_complete.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828

2929
// [Begin Save DEM]
3030
// Save decoder configuration to YAML file
31-
void save_dem(const cudaq::qec::decoder_context &ctx,
31+
void save_dem(const cudaq::qec::decoder_inputs &inputs,
3232
const std::string &filename) {
33-
const auto &dem = ctx.dem;
33+
const auto &dem = inputs.dem;
3434
// Create decoder config
3535
cudaq::qec::decoding::config::decoder_config config;
3636
config.id = 0;
@@ -39,7 +39,7 @@ void save_dem(const cudaq::qec::decoder_context &ctx,
3939
config.syndrome_size = dem.num_detectors();
4040
config.H_sparse = cudaq::qec::pcm_to_sparse_vec(dem.detector_error_matrix);
4141
config.O_sparse = cudaq::qec::pcm_to_sparse_vec(dem.observables_flips_matrix);
42-
config.D_sparse = cudaq::qec::d_sparse(ctx.m2d);
42+
config.D_sparse = cudaq::qec::d_sparse(inputs.m2d);
4343

4444
// Decoder parameters are a plain heterogeneous_map; keys are governed by
4545
// the parameter schema the decoder registered.

libs/qec/include/cudaq/qec/experiments.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ sample_memory_circuit(const code &code, std::size_t numShots,
161161
std::size_t numRounds, cudaq::noise_model &noise);
162162

163163
/// @brief Finalized decoder inputs: a canonicalized DEM and measurement maps.
164-
struct decoder_context {
164+
struct decoder_inputs {
165165
detector_error_model dem;
166166
cudaq::M2DSparseMatrix m2d;
167167
cudaq::M2OSparseMatrix m2o;
@@ -171,23 +171,23 @@ struct decoder_context {
171171
///
172172
/// Stores the raw (uncanonicalized) circuit analysis. Call a component method
173173
/// to canonicalize exactly the stabilizer type needed and obtain a
174-
/// `decoder_context`:
174+
/// `decoder_inputs`:
175175
/// - `x_component()` — X-stabilizer detectors only
176176
/// - `z_component()` — Z-stabilizer detectors only
177177
/// - `full_component()` — both stabilizer types, boundary-aware
178-
struct decoder_context_handle {
178+
struct decoder_context {
179179
/// @brief Total number of measurements per shot (column count of m2d/m2o).
180180
std::size_t num_measurements() const;
181181

182-
/// @brief Canonicalize X-stabilizer detectors; return decoder_context.
183-
decoder_context x_component() const;
182+
/// @brief Canonicalize X-stabilizer detectors; return decoder_inputs.
183+
decoder_inputs x_component() const;
184184

185-
/// @brief Canonicalize Z-stabilizer detectors; return decoder_context.
186-
decoder_context z_component() const;
185+
/// @brief Canonicalize Z-stabilizer detectors; return decoder_inputs.
186+
decoder_inputs z_component() const;
187187

188188
/// @brief Canonicalize both stabilizer types with boundary awareness;
189-
/// return decoder_context.
190-
decoder_context full_component() const;
189+
/// return decoder_inputs.
190+
decoder_inputs full_component() const;
191191

192192
private:
193193
cudaq::M2DSparseMatrix m2d_;
@@ -198,7 +198,7 @@ struct decoder_context_handle {
198198
std::size_t num_z_stabilizers_ = 0;
199199
bool fixed_basis_is_z_ = false;
200200

201-
friend decoder_context_handle
201+
friend decoder_context
202202
decoder_context_from_memory_circuit(const code &, operation, std::size_t,
203203
cudaq::noise_model &, bool);
204204
};
@@ -254,7 +254,7 @@ detector_error_model z_dem_from_memory_circuit(const code &code,
254254
/// Executes `dem_from_kernel` once and stores the raw result. Call
255255
/// `x_component()`, `z_component()`, or `full_component()` on the returned
256256
/// handle to canonicalize exactly the stabilizer type needed.
257-
decoder_context_handle decoder_context_from_memory_circuit(
257+
decoder_context decoder_context_from_memory_circuit(
258258
const code &code, operation statePrep, std::size_t numRounds,
259259
cudaq::noise_model &noise, bool decompose_errors = false);
260260
} // namespace cudaq::qec

libs/qec/lib/experiments.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ namespace details {
440440
/// (dem, m2d, m2o). For the full (both-basis) case the boundary-aware
441441
/// canonicalization is used; for a single-type selection the layout is uniform
442442
/// so the simpler overload suffices.
443-
static decoder_context
443+
static decoder_inputs
444444
make_component(detector_error_model dem, cudaq::M2DSparseMatrix m2d,
445445
cudaq::M2OSparseMatrix m2o, std::size_t num_rounds,
446446
std::size_t num_x_stabilizers, std::size_t num_z_stabilizers,
@@ -513,9 +513,11 @@ std::vector<std::int64_t> d_sparse(const cudaq::M2DSparseMatrix &m2d) {
513513
return out;
514514
}
515515

516-
decoder_context_handle decoder_context_from_memory_circuit(
517-
const code &code, operation statePrep, std::size_t numRounds,
518-
cudaq::noise_model &noise, bool decompose_errors) {
516+
decoder_context decoder_context_from_memory_circuit(const code &code,
517+
operation statePrep,
518+
std::size_t numRounds,
519+
cudaq::noise_model &noise,
520+
bool decompose_errors) {
519521
if (numRounds == 0)
520522
throw std::runtime_error(
521523
"dem_from_memory_circuit error - numRounds must be >= 1. The memory "
@@ -556,7 +558,7 @@ decoder_context_handle decoder_context_from_memory_circuit(
556558

557559
cudaq::dem_options dem_opts;
558560
dem_opts.decompose_errors = decompose_errors;
559-
decoder_context_handle result;
561+
decoder_context result;
560562
auto dem_text = cudaq::dem_from_kernel(
561563
memory_circuit, &noise, dem_opts, result.m2d_, result.m2o_, stabRound,
562564
prep, numData, numAncx, numAncz, numRounds, xVec, zVec, obs_flat, num_obs,
@@ -569,25 +571,25 @@ decoder_context_handle decoder_context_from_memory_circuit(
569571
return result;
570572
}
571573

572-
std::size_t decoder_context_handle::num_measurements() const {
574+
std::size_t decoder_context::num_measurements() const {
573575
return m2d_.num_measurements;
574576
}
575577

576-
decoder_context decoder_context_handle::x_component() const {
578+
decoder_inputs decoder_context::x_component() const {
577579
return details::make_component(dem_, m2d_, m2o_, num_rounds_,
578580
num_x_stabilizers_, num_z_stabilizers_,
579581
fixed_basis_is_z_, /*keep_x=*/true,
580582
/*keep_z=*/false);
581583
}
582584

583-
decoder_context decoder_context_handle::z_component() const {
585+
decoder_inputs decoder_context::z_component() const {
584586
return details::make_component(dem_, m2d_, m2o_, num_rounds_,
585587
num_x_stabilizers_, num_z_stabilizers_,
586588
fixed_basis_is_z_, /*keep_x=*/false,
587589
/*keep_z=*/true);
588590
}
589591

590-
decoder_context decoder_context_handle::full_component() const {
592+
decoder_inputs decoder_context::full_component() const {
591593
return details::make_component(dem_, m2d_, m2o_, num_rounds_,
592594
num_x_stabilizers_, num_z_stabilizers_,
593595
fixed_basis_is_z_, /*keep_x=*/true,

libs/qec/python/bindings/py_code.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -697,20 +697,19 @@ void bindCode(nb::module_ &mod) {
697697
nb::arg("code"), nb::arg("op"), nb::arg("numShots"), nb::arg("numRounds"),
698698
nb::arg("noise") = nb::none());
699699

700-
nb::class_<decoder_context_handle>(qecmod, "DecoderContext",
701-
R"pbdoc(
700+
nb::class_<decoder_context>(qecmod, "DecoderContext",
701+
R"pbdoc(
702702
Lazy handle returned by ``decoder_context_from_memory_circuit``.
703703
704704
Stores the raw circuit analysis. Each *_component method canonicalizes
705705
exactly the requested stabilizer type and returns a ``(dem, m2d, m2o)``
706706
tuple.
707707
)pbdoc")
708-
.def_prop_ro("num_measurements",
709-
&decoder_context_handle::num_measurements,
708+
.def_prop_ro("num_measurements", &decoder_context::num_measurements,
710709
"Total number of measurements per shot.")
711710
.def(
712711
"x_component",
713-
[](const decoder_context_handle &h) {
712+
[](const decoder_context &h) {
714713
auto ctx = h.x_component();
715714
return nb::make_tuple(ctx.dem, ctx.m2d.rows, ctx.m2o.rows);
716715
},
@@ -722,7 +721,7 @@ void bindCode(nb::module_ &mod) {
722721
)pbdoc")
723722
.def(
724723
"z_component",
725-
[](const decoder_context_handle &h) {
724+
[](const decoder_context &h) {
726725
auto ctx = h.z_component();
727726
return nb::make_tuple(ctx.dem, ctx.m2d.rows, ctx.m2o.rows);
728727
},
@@ -734,7 +733,7 @@ void bindCode(nb::module_ &mod) {
734733
)pbdoc")
735734
.def(
736735
"full_component",
737-
[](const decoder_context_handle &h) {
736+
[](const decoder_context &h) {
738737
auto ctx = h.full_component();
739738
return nb::make_tuple(ctx.dem, ctx.m2d.rows, ctx.m2o.rows);
740739
},
@@ -761,8 +760,10 @@ void bindCode(nb::module_ &mod) {
761760
whose XOR forms detector ``d``. Each detector's indices are emitted in
762761
order, followed by -1.
763762
764-
This is a standalone helper for ``dem_from_kernel`` consumers who hold
765-
an m2d map but not a full ``DecoderContext``.
763+
This is a standalone helper for consumers who hold an m2d map (e.g.,
764+
extracted from a ``decoder_inputs`` returned by a component method)
765+
and want to build the D_sparse vector without going through a
766+
``DecoderContext``.
766767
)pbdoc",
767768
nb::arg("m2d"));
768769

libs/qec/python/tests/test_dem.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ def test_decoder_context_from_memory_circuit_requires_noise_model():
756756

757757

758758
def test_decoder_context_and_components():
759-
# decoder_context_from_memory_circuit returns a memory_circuit_handle;
759+
# decoder_context_from_memory_circuit returns a decoder_context;
760760
# canonicalization is deferred until a component method is called.
761761
# full_component() matches dem_from_memory_circuit;
762762
# x_component() / z_component() reproduce x_/z_dem_from_memory_circuit.

libs/qec/unittests/backend-specific/stim/test_qec_stim.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ void check_matrix_bits(const std::string &name,
593593
}
594594
}
595595

596-
// End-to-end realtime decode driven by the memory_circuit_handle API,
596+
// End-to-end realtime decode driven by the decoder_context API,
597597
// built from a full-basis CSS memory circuit. Steane's boundary is
598598
// inhomogeneous: only the fixed (Z) stabilizers carry boundary detectors while
599599
// interior rounds carry both types. The measurement
@@ -627,7 +627,7 @@ TEST(QECCodeTester, checkRealtimeDecodeFromMemoryCircuit) {
627627
}
628628
EXPECT_LT(minRow, maxRow);
629629

630-
// Configure the realtime decoder entirely from the decoder context.
630+
// Configure the realtime decoder from the decoder_inputs returned by full_component().
631631
auto decoder =
632632
cudaq::qec::get_decoder("single_error_lut", dem.detector_error_matrix);
633633
decoder->set_O_sparse(
@@ -660,7 +660,7 @@ bool tensors_equal(const cudaqx::tensor<uint8_t> &a,
660660
}
661661
} // namespace
662662

663-
// decoder_context_from_memory_circuit returns a memory_circuit_handle;
663+
// decoder_context_from_memory_circuit returns a decoder_context;
664664
// canonicalization is deferred until a component method is called. Verify that
665665
// full_component() matches dem_from_memory_circuit and that x_component() /
666666
// z_component() reproduce x_/z_dem_from_memory_circuit and partition the

0 commit comments

Comments
 (0)