diff --git a/src/common.cc b/src/common.cc index bfb34ab..8435652 100644 --- a/src/common.cc +++ b/src/common.cc @@ -20,6 +20,8 @@ #include #include +namespace { + std::string vector_to_string(const std::vector& vec) { std::stringstream ss; ss << "["; @@ -34,6 +36,10 @@ std::string vector_to_string(const std::vector& vec) { return ss.str(); } +} // namespace + +namespace tesseract_decoder { + std::string common::Symptom::str() const { std::string s = "Symptom{detectors="; s += vector_to_string(detectors); @@ -241,3 +247,5 @@ stim::DetectorErrorModel common::dem_from_counts(const stim::DetectorErrorModel& } return out_dem; } + +} // namespace tesseract_decoder diff --git a/src/common.h b/src/common.h index 0e048f9..ce694d8 100644 --- a/src/common.h +++ b/src/common.h @@ -18,6 +18,7 @@ #include "stim.h" +namespace tesseract_decoder { namespace common { // Represents the effect of an error @@ -107,5 +108,6 @@ stim::DetectorErrorModel dem_from_counts(const stim::DetectorErrorModel& orig_de double merge_weights(double a, double b); } // namespace common +} // namespace tesseract_decoder #endif diff --git a/src/common.pybind.h b/src/common.pybind.h index 693aae8..d3d5fa9 100644 --- a/src/common.pybind.h +++ b/src/common.pybind.h @@ -26,6 +26,8 @@ namespace py = pybind11; +namespace tesseract_decoder { + void add_common_module(py::module& root) { auto m = root.def_submodule("common", "classes commonly used by the decoder"); @@ -217,4 +219,6 @@ void add_common_module(py::module& root) { )pbdoc"); } +} // namespace tesseract_decoder + #endif diff --git a/src/common.test.cc b/src/common.test.cc index 350853c..70eebd9 100644 --- a/src/common.test.cc +++ b/src/common.test.cc @@ -17,6 +17,9 @@ #include "gtest/gtest.h" #include "stim.h" +namespace tesseract_decoder { +namespace { + TEST(common, ErrorsStructFromDemInstruction) { // Test a pathological DEM error instruction stim::DetectorErrorModel dem("error(0.1) D0 ^ D0 D1 L0 L1 L1"); @@ -194,3 +197,6 @@ TEST(CommonTest, merge_indistinguishable_errors_two_errors) { auto merged_dem4 = common::merge_indistinguishable_errors(dem4, error_index_map); ASSERT_NEAR(get_merged_probability(merged_dem4), expected_merged_p, 1e-9); } + +} // namespace +} // namespace tesseract_decoder diff --git a/src/simplex.cc b/src/simplex.cc index fc47b86..1540334 100644 --- a/src/simplex.cc +++ b/src/simplex.cc @@ -20,6 +20,8 @@ #include "io/HMPSIO.h" #include "utils.h" +namespace tesseract_decoder { + constexpr size_t T_COORD = 2; std::string SimplexConfig::str() { @@ -399,3 +401,5 @@ void SimplexDecoder::decode_shots(std::vector& shots, } SimplexDecoder::~SimplexDecoder() {} + +} // namespace tesseract_decoder diff --git a/src/simplex.h b/src/simplex.h index 4288f8e..5929feb 100644 --- a/src/simplex.h +++ b/src/simplex.h @@ -24,6 +24,8 @@ struct HighsModel; struct Highs; enum class HighsStatus; +namespace tesseract_decoder { + struct SimplexConfig { stim::DetectorErrorModel dem; bool parallelize = false; @@ -81,4 +83,6 @@ struct SimplexDecoder { void init_ilp(); }; +} // namespace tesseract_decoder + #endif // SIMPLEX_HPP diff --git a/src/simplex.pybind.h b/src/simplex.pybind.h index 27439b2..448adc6 100644 --- a/src/simplex.pybind.h +++ b/src/simplex.pybind.h @@ -27,7 +27,9 @@ namespace py = pybind11; +namespace tesseract_decoder { namespace { + // Helper function to compile the decoder. std::unique_ptr _compile_simplex_decoder_helper(const SimplexConfig& self) { return std::make_unique(self); @@ -41,9 +43,11 @@ SimplexConfig simplex_config_maker(py::object dem, bool parallelize = false, {input_dem, parallelize, window_length, window_slide_length, verbose, merge_errors}); } -}; // namespace +} // namespace +} // namespace tesseract_decoder void add_simplex_module(py::module& root) { + using namespace tesseract_decoder; auto m = root.def_submodule("simplex", "Module containing the SimplexDecoder and related methods"); diff --git a/src/simplex_main.cc b/src/simplex_main.cc index e8da07d..ee0d484 100644 --- a/src/simplex_main.cc +++ b/src/simplex_main.cc @@ -23,6 +23,8 @@ #include "stim.h" #include "utils.h" +using namespace tesseract_decoder; + struct Args { std::string circuit_path; std::string dem_path; diff --git a/src/stim_utils.pybind.h b/src/stim_utils.pybind.h index ff638a7..319e689 100644 --- a/src/stim_utils.pybind.h +++ b/src/stim_utils.pybind.h @@ -7,6 +7,8 @@ #include "stim.h" +namespace tesseract_decoder { + namespace { namespace py = pybind11; } @@ -23,7 +25,7 @@ T parse_py_object(py::object py_obj) { return T(obj_str); } -stim::DemInstructionType parse_dit(std::string dit_str) { +inline stim::DemInstructionType parse_dit(std::string dit_str) { if (dit_str == "error") return stim::DemInstructionType::DEM_ERROR; if (dit_str == "detector") return stim::DemInstructionType::DEM_DETECTOR; if (dit_str == "logical_observable") return stim::DemInstructionType::DEM_LOGICAL_OBSERVABLE; @@ -33,12 +35,12 @@ stim::DemInstructionType parse_dit(std::string dit_str) { return stim::DemInstructionType::DEM_DETECTOR; } -stim::DemTarget parse_py_dem_target(py::object py_obj) { +inline stim::DemTarget parse_py_dem_target(py::object py_obj) { return stim::DemTarget::from_text(py::cast(py_obj.attr("__str__")())); } -stim::DemInstruction parse_py_dem_instruction(py::object py_obj, std::vector& args, - std::vector& targets) { +inline stim::DemInstruction parse_py_dem_instruction(py::object py_obj, std::vector& args, + std::vector& targets) { for (auto t : py_obj.attr("args_copy")()) args.push_back(t.cast()); stim::SpanRef args_ref(args); @@ -66,4 +68,6 @@ void dem_setter(T& config, py::object dem) { config.dem = parse_py_object(dem); } +} // namespace tesseract_decoder + #endif diff --git a/src/tesseract.cc b/src/tesseract.cc index f097617..dadfeed 100644 --- a/src/tesseract.cc +++ b/src/tesseract.cc @@ -53,6 +53,8 @@ struct hash> { }; } // namespace std +namespace tesseract_decoder { + std::string TesseractConfig::str() { auto& config = *this; std::stringstream ss; @@ -567,3 +569,5 @@ void TesseractDecoder::decode_shots(std::vector& shots, obs_predicted[i] = decode(shots[i].hits); } } + +} // namespace tesseract_decoder diff --git a/src/tesseract.h b/src/tesseract.h index 528c43b..2c406fd 100644 --- a/src/tesseract.h +++ b/src/tesseract.h @@ -27,6 +27,8 @@ #include "utils.h" #include "visualization.h" +namespace tesseract_decoder { + constexpr size_t INF_DET_BEAM = std::numeric_limits::max(); constexpr int DEFAULT_DET_BEAM = 5; constexpr size_t DEFAULT_PQLIMIT = 200000; @@ -123,4 +125,6 @@ struct TesseractDecoder { std::vector& detector_cost_tuples) const; }; +} // namespace tesseract_decoder + #endif // TESSERACT_DECODER_H diff --git a/src/tesseract.perf.cc b/src/tesseract.perf.cc index 655d357..b004d33 100644 --- a/src/tesseract.perf.cc +++ b/src/tesseract.perf.cc @@ -20,6 +20,8 @@ #include "stim.h" #include "utils.h" +using namespace tesseract_decoder; + constexpr uint64_t test_data_seed = 752024; template diff --git a/src/tesseract.pybind.cc b/src/tesseract.pybind.cc index 9f2808f..f81486d 100644 --- a/src/tesseract.pybind.cc +++ b/src/tesseract.pybind.cc @@ -25,6 +25,7 @@ #include "visualization.pybind.h" PYBIND11_MODULE(tesseract_decoder, tesseract) { + using namespace tesseract_decoder; py::module::import("stim"); add_common_module(tesseract); diff --git a/src/tesseract.pybind.h b/src/tesseract.pybind.h index 3bdf477..384a02f 100644 --- a/src/tesseract.pybind.h +++ b/src/tesseract.pybind.h @@ -27,7 +27,9 @@ namespace py = pybind11; +namespace tesseract_decoder { namespace { + // Helper function to compile the decoder. std::unique_ptr _compile_tesseract_decoder_helper(const TesseractConfig& self) { return std::make_unique(self); @@ -68,12 +70,20 @@ void add_tesseract_module(py::module& root) { m.attr("INF_DET_BEAM") = INF_DET_BEAM; m.doc() = "A sentinel value indicating an infinite beam size for the decoder."; - py::class_(m, "TesseractConfig", R"pbdoc( + auto py_tesseract_config = py::class_(m, "TesseractConfig", R"pbdoc( Configuration object for the `TesseractDecoder`. This class holds all the parameters needed to initialize and configure a Tesseract decoder instance. - )pbdoc") + )pbdoc"); + auto py_tesseract_decoder = py::class_(m, "TesseractDecoder", R"pbdoc( + A class that implements the Tesseract decoding algorithm. + + It can decode syndromes from a `stim.DetectorErrorModel` to predict + which observables have been flipped. + )pbdoc"); + + py_tesseract_config .def(py::init<>(), R"pbdoc( Default constructor for TesseractConfig. Creates a new instance with default parameter values. @@ -199,12 +209,7 @@ void add_tesseract_module(py::module& root) { `TesseractConfig` object. )pbdoc"); - py::class_(m, "TesseractDecoder", R"pbdoc( - A class that implements the Tesseract decoding algorithm. - - It can decode syndromes from a `stim.DetectorErrorModel` to predict - which observables have been flipped. - )pbdoc") + py_tesseract_decoder .def(py::init(), py::arg("config"), R"pbdoc( The constructor for the `TesseractDecoder` class. @@ -482,4 +487,6 @@ void add_tesseract_module(py::module& root) { "visualization of the algorithm"); } +} // namespace tesseract_decoder + #endif diff --git a/src/tesseract.test.cc b/src/tesseract.test.cc index 1ac9f00..17f1de5 100644 --- a/src/tesseract.test.cc +++ b/src/tesseract.test.cc @@ -23,6 +23,9 @@ #include "stim.h" #include "utils.h" +namespace tesseract_decoder { +namespace { + constexpr uint64_t test_data_seed = 752024; bool simplex_test_compare(stim::DetectorErrorModel& dem, std::vector& shots) { @@ -395,3 +398,6 @@ TEST(tesseract, DecodeToErrorsThrowsOnInvalidSymptom) { err.what()); } } + +} // namespace +} // namespace tesseract_decoder diff --git a/src/tesseract_main.cc b/src/tesseract_main.cc index 65fb4e2..84ee9af 100644 --- a/src/tesseract_main.cc +++ b/src/tesseract_main.cc @@ -26,6 +26,8 @@ #include "tesseract.h" #include "utils.h" +using namespace tesseract_decoder; + struct Args { std::string circuit_path; std::string dem_path; diff --git a/src/tesseract_sinter_compat.pybind.h b/src/tesseract_sinter_compat.pybind.h index c889423..6ddd728 100644 --- a/src/tesseract_sinter_compat.pybind.h +++ b/src/tesseract_sinter_compat.pybind.h @@ -25,6 +25,8 @@ namespace py = pybind11; +namespace tesseract_decoder { + // These are the classes that will be exposed to Python. struct TesseractSinterCompiledDecoder; struct TesseractSinterDecoder; @@ -398,3 +400,5 @@ void pybind_sinter_compat(py::module& root) { root.attr("TesseractSinterDecoder") = m.attr("TesseractSinterDecoder"); root.attr("make_tesseract_sinter_decoders_dict") = m.attr("make_tesseract_sinter_decoders_dict"); } + +} // namespace tesseract_decoder diff --git a/src/test_data.h b/src/test_data.h index 137f57e..afe22bb 100644 --- a/src/test_data.h +++ b/src/test_data.h @@ -19,6 +19,8 @@ #include "stim.h" +namespace tesseract_decoder { + std::vector get_small_test_circuits() { return {}; } @@ -27,4 +29,6 @@ std::vector get_large_test_circuits() { return {}; } +} // namespace tesseract_decoder + #endif // TESSERACT_TEST_DATA_H diff --git a/src/utils.cc b/src/utils.cc index 56115d4..e0d2968 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -27,6 +27,8 @@ #include "common.h" #include "stim.h" +namespace tesseract_decoder { + std::vector> get_detector_coords(const stim::DetectorErrorModel& dem) { std::vector> detector_coords; for (const stim::DemInstruction& instruction : dem.flattened().instructions) { @@ -279,3 +281,5 @@ uint64_t vector_to_u64_mask(const std::vector& v) { } return mask; } + +} // namespace tesseract_decoder diff --git a/src/utils.h b/src/utils.h index 73d7817..d2e9013 100644 --- a/src/utils.h +++ b/src/utils.h @@ -26,6 +26,8 @@ #include "common.h" #include "stim.h" +namespace tesseract_decoder { + constexpr const double EPSILON = 1e-7; std::vector> get_detector_coords(const stim::DetectorErrorModel& dem); @@ -54,4 +56,7 @@ std::vector get_errors_from_dem(const stim::DetectorErrorModel& d std::vector get_files_recursive(const std::string& directory_path); uint64_t vector_to_u64_mask(const std::vector& v); + +} // namespace tesseract_decoder + #endif // __TESSERACT_UTILS_H__ diff --git a/src/utils.pybind.h b/src/utils.pybind.h index 92ba668..71b3af7 100644 --- a/src/utils.pybind.h +++ b/src/utils.pybind.h @@ -24,6 +24,7 @@ namespace py = pybind11; void add_utils_module(py::module& root) { + using namespace tesseract_decoder; auto m = root.def_submodule("utils", "utility methods"); m.attr("EPSILON") = EPSILON; diff --git a/src/visualization.cc b/src/visualization.cc index ecd78fa..c9c6f9b 100644 --- a/src/visualization.cc +++ b/src/visualization.cc @@ -1,6 +1,8 @@ #include "visualization.h" +namespace tesseract_decoder { + void Visualizer::add_errors(const std::vector& errors) { for (auto& error : errors) { lines.push_back(error.str()); @@ -55,3 +57,5 @@ void Visualizer::write(const char* fpath) { fclose(fout); } + +} // namespace tesseract_decoder diff --git a/src/visualization.h b/src/visualization.h index 64d9ad5..d3af2e9 100644 --- a/src/visualization.h +++ b/src/visualization.h @@ -7,6 +7,8 @@ #include "common.h" +namespace tesseract_decoder { + struct Visualizer { void add_detector_coords(const std::vector>&); void add_errors(const std::vector&); @@ -19,4 +21,6 @@ struct Visualizer { std::list lines; }; +} // namespace tesseract_decoder + #endif diff --git a/src/visualization.pybind.h b/src/visualization.pybind.h index 820bf86..330f08e 100644 --- a/src/visualization.pybind.h +++ b/src/visualization.pybind.h @@ -10,7 +10,7 @@ namespace py = pybind11; void add_visualization_module(py::module& root) { auto m = root.def_submodule("viz", "Module containing the visualization tools"); - py::class_(m, "Visualizer") + py::class_(m, "Visualizer") .def(py::init<>()) - .def("write", &Visualizer::write, py::arg("fpath")); + .def("write", &tesseract_decoder::Visualizer::write, py::arg("fpath")); }