Skip to content

Commit 05b5d5a

Browse files
remove require_dem_text helper function
Signed-off-by: vedika-saravanan <vsaravanan@nvidia.com>
1 parent b0eb738 commit 05b5d5a

8 files changed

Lines changed: 37 additions & 49 deletions

File tree

libs/qec/include/cudaq/qec/decoder.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
#include "cuda-qx/core/tensor.h"
1414
#include "sparse_binary_matrix.h"
1515
#include "cudaq/qec/detector_error_model.h"
16+
#include <algorithm>
1617
#include <functional>
1718
#include <future>
1819
#include <memory>
1920
#include <optional>
2021
#include <string>
2122
#include <string_view>
23+
#include <tuple>
2224
#include <variant>
2325
#include <vector>
2426

@@ -31,8 +33,7 @@ using float_t = double;
3133
#endif
3234

3335
/// Decoder construction input: either a parity-check matrix or raw Stim DEM
34-
/// text. PCM-based decoders can accept both; DEM-native decoders can require
35-
/// the string alternative via `require_dem_text`.
36+
/// text.
3637
using decoder_init = std::variant<sparse_binary_matrix, std::string>;
3738

3839
/// @brief Validates that all keys in a heterogeneous map are found in a list of
@@ -516,6 +517,7 @@ get_decoder(const std::string &name, std::string_view stim_dem_text,
516517
return get_decoder(name, decoder_init{std::string{stim_dem_text}}, options);
517518
}
518519

520+
namespace details {
519521
/// DEM-derived defaults; pointers alias into the source `dem`.
520522
struct dem_default_values {
521523
const cudaqx::tensor<uint8_t> *O = nullptr;
@@ -526,9 +528,7 @@ struct dem_default_values {
526528
dem_default_values dem_defaults_for_missing_keys(
527529
const std::function<bool(const std::string &)> &contains_user_key,
528530
const detector_error_model &dem);
529-
530-
/// Extract raw Stim DEM text for DEM-native decoders.
531-
std::string_view require_dem_text(const decoder_init &init);
531+
} // namespace details
532532

533533
/// Build a PCM-based decoder. If `init` holds DEM text, it is parsed into a PCM
534534
/// and DEM-derived `"O"` / `"error_rate_vec"` defaults are added when absent.
@@ -541,7 +541,7 @@ make_pcm_decoder(const decoder_init &init,
541541

542542
const auto dem = dem_from_stim_text(std::get<std::string>(init));
543543
cudaqx::heterogeneous_map merged = params;
544-
const auto defaults = dem_defaults_for_missing_keys(
544+
const auto defaults = details::dem_defaults_for_missing_keys(
545545
[&](const std::string &key) { return merged.contains(key); }, dem);
546546
if (defaults.O)
547547
merged.insert("O", *defaults.O);

libs/qec/include/cudaq/qec/detector_error_model.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
#pragma once
99

1010
#include "cuda-qx/core/tensor.h"
11+
#include <cstddef>
12+
#include <cstdint>
1113
#include <optional>
1214
#include <string>
15+
#include <vector>
1316

1417
namespace cudaq::qec {
1518

libs/qec/lib/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ endif()
4141
set(QEC_SOURCES
4242
code.cpp
4343
decoder.cpp
44-
decoder_stim_dem.cpp
4544
detector_error_model.cpp
4645
experiments.cpp
4746
pcm_utils.cpp

libs/qec/lib/decoder.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,21 @@ decoder::get(const std::string &name, const decoder_init &init,
141141
return iter->second(init, param_map);
142142
}
143143

144+
namespace details {
145+
146+
dem_default_values dem_defaults_for_missing_keys(
147+
const std::function<bool(const std::string &)> &contains_user_key,
148+
const detector_error_model &dem) {
149+
dem_default_values out;
150+
if (!contains_user_key("O") && dem.num_observables() > 0)
151+
out.O = &dem.observables_flips_matrix;
152+
if (!contains_user_key("error_rate_vec"))
153+
out.error_rate_vec = &dem.error_rates;
154+
return out;
155+
}
156+
157+
} // namespace details
158+
144159
static uint32_t calculate_num_msyn_per_decode(
145160
const std::vector<std::vector<uint32_t>> &D_sparse) {
146161
uint32_t max_col = 0;

libs/qec/lib/decoder_stim_dem.cpp

Lines changed: 0 additions & 36 deletions
This file was deleted.

libs/qec/lib/detector_error_model.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212

1313
#include "stim.h"
1414

15+
#include <exception>
16+
#include <stdexcept>
17+
#include <string>
18+
#include <utility>
19+
#include <vector>
20+
1521
namespace cudaq::qec {
1622

1723
detector_error_model dem_from_stim_text(const std::string &dem_text) {

libs/qec/python/bindings/py_decoder.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ void bindDecoder(nb::module_ &mod) {
788788
if (PyDecoderRegistry::contains(name)) {
789789
auto dem = dem_from_stim_text(dem_text);
790790

791-
auto defaults = dem_defaults_for_missing_keys(
791+
auto defaults = details::dem_defaults_for_missing_keys(
792792
[&](const std::string &key) { return options.contains(key); }, dem);
793793
if (defaults.O)
794794
options["O"] = copyToPyArray(*defaults.O);
@@ -849,10 +849,11 @@ void bindDecoder(nb::module_ &mod) {
849849
raw DEM text via ``decoder_init``; Python-registered decoders receive
850850
the DEM-derived PCM plus ``O`` and ``error_rate_vec`` defaults.
851851
852-
For Python-registered decoders (``cudaq.qec.decoder`` decorator), ``H``
853-
is passed through to ``__init__`` unchanged (NumPy array or sparse dict).
854-
Call ``Decoder.__init__(self, H)`` so nanobind can store the PCM in CSC
855-
form when ``H`` is a dict without building a dense ``rows x cols``
852+
For Python-registered decoders (``cudaq.qec.decoder`` decorator),
853+
NumPy array and sparse dict inputs are passed through to ``__init__``
854+
unchanged. DEM string inputs are parsed first as described above. Call
855+
``Decoder.__init__(self, H)`` so nanobind can store the PCM in CSC form
856+
when ``H`` is a dict without building a dense ``rows x cols``
856857
allocation.
857858
)pbdoc");
858859

libs/qec/unittests/test_decoders.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ error(0.1) L0 L0
820820

821821
TEST(StimDemGetDecoder, DemWithoutObservablesDoesNotAddODefault) {
822822
auto dem = cudaq::qec::dem_from_stim_text("error(0.1) D0\n");
823-
auto defaults = cudaq::qec::dem_defaults_for_missing_keys(
823+
auto defaults = cudaq::qec::details::dem_defaults_for_missing_keys(
824824
[](const std::string &) { return false; }, dem);
825825

826826
EXPECT_EQ(defaults.O, nullptr);

0 commit comments

Comments
 (0)