Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <string>
#include <vector>

namespace {

std::string vector_to_string(const std::vector<int>& vec) {
std::stringstream ss;
ss << "[";
Expand All @@ -34,6 +36,10 @@ std::string vector_to_string(const std::vector<int>& vec) {
return ss.str();
}

} // namespace

namespace tesseract_decoder {

std::string common::Symptom::str() const {
std::string s = "Symptom{detectors=";
s += vector_to_string(detectors);
Expand Down Expand Up @@ -241,3 +247,5 @@ stim::DetectorErrorModel common::dem_from_counts(const stim::DetectorErrorModel&
}
return out_dem;
}

} // namespace tesseract_decoder
2 changes: 2 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "stim.h"

namespace tesseract_decoder {
namespace common {

// Represents the effect of an error
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions src/common.pybind.h
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -217,4 +219,6 @@ void add_common_module(py::module& root) {
)pbdoc");
}

} // namespace tesseract_decoder

#endif
6 changes: 6 additions & 0 deletions src/common.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions src/simplex.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include "io/HMPSIO.h"
#include "utils.h"

namespace tesseract_decoder {

constexpr size_t T_COORD = 2;

std::string SimplexConfig::str() {
Expand Down Expand Up @@ -399,3 +401,5 @@ void SimplexDecoder::decode_shots(std::vector<stim::SparseShot>& shots,
}

SimplexDecoder::~SimplexDecoder() {}

} // namespace tesseract_decoder
4 changes: 4 additions & 0 deletions src/simplex.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ struct HighsModel;
struct Highs;
enum class HighsStatus;

namespace tesseract_decoder {

struct SimplexConfig {
stim::DetectorErrorModel dem;
bool parallelize = false;
Expand Down Expand Up @@ -81,4 +83,6 @@ struct SimplexDecoder {
void init_ilp();
};

} // namespace tesseract_decoder

#endif // SIMPLEX_HPP
6 changes: 5 additions & 1 deletion src/simplex.pybind.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@

namespace py = pybind11;

namespace tesseract_decoder {
namespace {

// Helper function to compile the decoder.
std::unique_ptr<SimplexDecoder> _compile_simplex_decoder_helper(const SimplexConfig& self) {
return std::make_unique<SimplexDecoder>(self);
Expand All @@ -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");

Expand Down
2 changes: 2 additions & 0 deletions src/simplex_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "stim.h"
#include "utils.h"

using namespace tesseract_decoder;

struct Args {
std::string circuit_path;
std::string dem_path;
Expand Down
12 changes: 8 additions & 4 deletions src/stim_utils.pybind.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include "stim.h"

namespace tesseract_decoder {

namespace {
namespace py = pybind11;
}
Expand All @@ -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;
Expand All @@ -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<std::string>(py_obj.attr("__str__")()));
}

stim::DemInstruction parse_py_dem_instruction(py::object py_obj, std::vector<double>& args,
std::vector<stim::DemTarget>& targets) {
inline stim::DemInstruction parse_py_dem_instruction(py::object py_obj, std::vector<double>& args,
std::vector<stim::DemTarget>& targets) {
for (auto t : py_obj.attr("args_copy")()) args.push_back(t.cast<double>());
stim::SpanRef args_ref(args);

Expand Down Expand Up @@ -66,4 +68,6 @@ void dem_setter(T& config, py::object dem) {
config.dem = parse_py_object<stim::DetectorErrorModel>(dem);
}

} // namespace tesseract_decoder

#endif
4 changes: 4 additions & 0 deletions src/tesseract.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ struct hash<boost::dynamic_bitset<>> {
};
} // namespace std

namespace tesseract_decoder {

std::string TesseractConfig::str() {
auto& config = *this;
std::stringstream ss;
Expand Down Expand Up @@ -567,3 +569,5 @@ void TesseractDecoder::decode_shots(std::vector<stim::SparseShot>& shots,
obs_predicted[i] = decode(shots[i].hits);
}
}

} // namespace tesseract_decoder
4 changes: 4 additions & 0 deletions src/tesseract.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "utils.h"
#include "visualization.h"

namespace tesseract_decoder {

constexpr size_t INF_DET_BEAM = std::numeric_limits<uint16_t>::max();
constexpr int DEFAULT_DET_BEAM = 5;
constexpr size_t DEFAULT_PQLIMIT = 200000;
Expand Down Expand Up @@ -123,4 +125,6 @@ struct TesseractDecoder {
std::vector<DetectorCostTuple>& detector_cost_tuples) const;
};

} // namespace tesseract_decoder

#endif // TESSERACT_DECODER_H
2 changes: 2 additions & 0 deletions src/tesseract.perf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include "stim.h"
#include "utils.h"

using namespace tesseract_decoder;

constexpr uint64_t test_data_seed = 752024;

template <typename Decoder>
Expand Down
1 change: 1 addition & 0 deletions src/tesseract.pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 15 additions & 8 deletions src/tesseract.pybind.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@

namespace py = pybind11;

namespace tesseract_decoder {
namespace {

// Helper function to compile the decoder.
std::unique_ptr<TesseractDecoder> _compile_tesseract_decoder_helper(const TesseractConfig& self) {
return std::make_unique<TesseractDecoder>(self);
Expand Down Expand Up @@ -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_<TesseractConfig>(m, "TesseractConfig", R"pbdoc(
auto py_tesseract_config = py::class_<TesseractConfig>(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_<TesseractDecoder>(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.
Expand Down Expand Up @@ -199,12 +209,7 @@ void add_tesseract_module(py::module& root) {
`TesseractConfig` object.
)pbdoc");

py::class_<TesseractDecoder>(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<TesseractConfig>(), py::arg("config"), R"pbdoc(
The constructor for the `TesseractDecoder` class.

Expand Down Expand Up @@ -482,4 +487,6 @@ void add_tesseract_module(py::module& root) {
"visualization of the algorithm");
}

} // namespace tesseract_decoder

#endif
6 changes: 6 additions & 0 deletions src/tesseract.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<stim::SparseShot>& shots) {
Expand Down Expand Up @@ -395,3 +398,6 @@ TEST(tesseract, DecodeToErrorsThrowsOnInvalidSymptom) {
err.what());
}
}

} // namespace
} // namespace tesseract_decoder
2 changes: 2 additions & 0 deletions src/tesseract_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "tesseract.h"
#include "utils.h"

using namespace tesseract_decoder;

struct Args {
std::string circuit_path;
std::string dem_path;
Expand Down
4 changes: 4 additions & 0 deletions src/tesseract_sinter_compat.pybind.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

namespace py = pybind11;

namespace tesseract_decoder {

// These are the classes that will be exposed to Python.
struct TesseractSinterCompiledDecoder;
struct TesseractSinterDecoder;
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions src/test_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include "stim.h"

namespace tesseract_decoder {

std::vector<std::string> get_small_test_circuits() {
return {};
}
Expand All @@ -27,4 +29,6 @@ std::vector<std::string> get_large_test_circuits() {
return {};
}

} // namespace tesseract_decoder

#endif // TESSERACT_TEST_DATA_H
4 changes: 4 additions & 0 deletions src/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "common.h"
#include "stim.h"

namespace tesseract_decoder {

std::vector<std::vector<double>> get_detector_coords(const stim::DetectorErrorModel& dem) {
std::vector<std::vector<double>> detector_coords;
for (const stim::DemInstruction& instruction : dem.flattened().instructions) {
Expand Down Expand Up @@ -279,3 +281,5 @@ uint64_t vector_to_u64_mask(const std::vector<int>& v) {
}
return mask;
}

} // namespace tesseract_decoder
5 changes: 5 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "common.h"
#include "stim.h"

namespace tesseract_decoder {

constexpr const double EPSILON = 1e-7;

std::vector<std::vector<double>> get_detector_coords(const stim::DetectorErrorModel& dem);
Expand Down Expand Up @@ -54,4 +56,7 @@ std::vector<common::Error> get_errors_from_dem(const stim::DetectorErrorModel& d
std::vector<std::string> get_files_recursive(const std::string& directory_path);

uint64_t vector_to_u64_mask(const std::vector<int>& v);

} // namespace tesseract_decoder

#endif // __TESSERACT_UTILS_H__
1 change: 1 addition & 0 deletions src/utils.pybind.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/visualization.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

#include "visualization.h"

namespace tesseract_decoder {

void Visualizer::add_errors(const std::vector<common::Error>& errors) {
for (auto& error : errors) {
lines.push_back(error.str());
Expand Down Expand Up @@ -55,3 +57,5 @@ void Visualizer::write(const char* fpath) {

fclose(fout);
}

} // namespace tesseract_decoder
Loading
Loading