Skip to content

Commit a88d341

Browse files
committed
Merge branch 'main' into gari-pr269-B-cpp
2 parents 9488efd + 6a260b0 commit a88d341

31 files changed

Lines changed: 158 additions & 19 deletions

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ pybind11_add_module(tesseract_decoder MODULE ${TESSERACT_SRC_DIR}/tesseract.pybi
132132
target_compile_options(tesseract_decoder PRIVATE ${OPT_COPTS})
133133
target_include_directories(tesseract_decoder PRIVATE ${TESSERACT_SRC_DIR})
134134
target_link_libraries(tesseract_decoder PRIVATE common utils simplex tesseract_lib)
135+
# Keep statically linked HiGHS symbols private to this extension on every
136+
# supported Unix platform. Mach-O uses an explicit export list because it has
137+
# no ELF-style --exclude-libs option; otherwise highspy can resolve symbols to
138+
# Tesseract's private HiGHS copy when both extensions share a process.
139+
if(APPLE)
140+
target_link_options(tesseract_decoder PRIVATE
141+
"-Wl,-exported_symbols_list,${TESSERACT_SRC_DIR}/tesseract_decoder.exports")
142+
elseif(UNIX)
143+
target_link_options(tesseract_decoder PRIVATE "-Wl,--exclude-libs,ALL")
144+
endif()
135145
set_target_properties(tesseract_decoder PROPERTIES
136146
LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/src
137147
LIBRARY_OUTPUT_DIRECTORY_DEBUG ${PROJECT_SOURCE_DIR}/src

src/BUILD

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,19 @@ pybind_extension(
105105
"tesseract.pybind.cc",
106106
],
107107
copts = OPT_COPTS,
108+
# Keep static-library symbols (notably HiGHS) private to this extension.
109+
# Mach-O has no equivalent to ELF's --exclude-libs, so it needs an explicit
110+
# export list; without it, highspy can see the linked HiGHS symbols.
111+
additional_linker_inputs = ["tesseract_decoder.exports"],
112+
linkopts = select({
113+
"@platforms//os:osx": [
114+
"-Wl,-exported_symbols_list,$(location :tesseract_decoder.exports)",
115+
],
116+
# The ELF equivalent also hides symbols pulled from static archives.
117+
"@platforms//os:linux": ["-Wl,--exclude-libs,ALL"],
118+
# The default also covers Windows and non-ELF toolchains.
119+
"//conditions:default": [],
120+
}),
108121
deps = [
109122
":tesseract_decoder_pybind",
110123
],

src/common.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <string>
2121
#include <vector>
2222

23+
namespace {
24+
2325
std::string vector_to_string(const std::vector<int>& vec) {
2426
std::stringstream ss;
2527
ss << "[";
@@ -34,6 +36,10 @@ std::string vector_to_string(const std::vector<int>& vec) {
3436
return ss.str();
3537
}
3638

39+
} // namespace
40+
41+
namespace tesseract_decoder {
42+
3743
std::string common::Symptom::str() const {
3844
std::string s = "Symptom{detectors=";
3945
s += vector_to_string(detectors);
@@ -255,3 +261,5 @@ stim::DetectorErrorModel common::dem_from_counts(const stim::DetectorErrorModel&
255261
}
256262
return out_dem;
257263
}
264+
265+
} // namespace tesseract_decoder

src/common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "stim.h"
2020

21+
namespace tesseract_decoder {
2122
namespace common {
2223

2324
// Represents the effect of an error
@@ -113,5 +114,6 @@ stim::DetectorErrorModel dem_from_counts(const stim::DetectorErrorModel& orig_de
113114
double merge_weights(double a, double b);
114115

115116
} // namespace common
117+
} // namespace tesseract_decoder
116118

117119
#endif

src/common.pybind.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
namespace py = pybind11;
2828

29+
namespace tesseract_decoder {
30+
2931
void add_common_module(py::module& root) {
3032
auto m = root.def_submodule("common", "classes commonly used by the decoder");
3133

@@ -217,4 +219,6 @@ void add_common_module(py::module& root) {
217219
)pbdoc");
218220
}
219221

222+
} // namespace tesseract_decoder
223+
220224
#endif

src/common.test.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#include "gtest/gtest.h"
1818
#include "stim.h"
1919

20+
namespace tesseract_decoder {
21+
namespace {
22+
2023
TEST(common, ErrorsStructFromDemInstruction) {
2124
// Test a pathological DEM error instruction
2225
stim::DetectorErrorModel dem("error(0.1) D0 ^ D0 D1 L0 L1 L1");
@@ -194,3 +197,6 @@ TEST(CommonTest, merge_indistinguishable_errors_two_errors) {
194197
auto merged_dem4 = common::merge_indistinguishable_errors(dem4, error_index_map);
195198
ASSERT_NEAR(get_merged_probability(merged_dem4), expected_merged_p, 1e-9);
196199
}
200+
201+
} // namespace
202+
} // namespace tesseract_decoder

src/simplex.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "io/HMPSIO.h"
2121
#include "utils.h"
2222

23+
namespace tesseract_decoder {
24+
2325
constexpr size_t T_COORD = 2;
2426

2527
std::string SimplexConfig::str() {
@@ -401,3 +403,5 @@ void SimplexDecoder::decode_shots(std::vector<stim::SparseShot>& shots,
401403
}
402404

403405
SimplexDecoder::~SimplexDecoder() {}
406+
407+
} // namespace tesseract_decoder

src/simplex.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ struct HighsModel;
2424
struct Highs;
2525
enum class HighsStatus;
2626

27+
namespace tesseract_decoder {
28+
2729
struct SimplexConfig {
2830
stim::DetectorErrorModel dem;
2931
bool parallelize = false;
@@ -81,4 +83,6 @@ struct SimplexDecoder {
8183
void init_ilp();
8284
};
8385

86+
} // namespace tesseract_decoder
87+
8488
#endif // SIMPLEX_HPP

src/simplex.pybind.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727

2828
namespace py = pybind11;
2929

30+
namespace tesseract_decoder {
3031
namespace {
32+
3133
// Helper function to compile the decoder.
3234
std::unique_ptr<SimplexDecoder> _compile_simplex_decoder_helper(const SimplexConfig& self) {
3335
return std::make_unique<SimplexDecoder>(self);
@@ -41,19 +43,27 @@ SimplexConfig simplex_config_maker(py::object dem, bool parallelize = false,
4143
{input_dem, parallelize, window_length, window_slide_length, verbose, merge_errors});
4244
}
4345

44-
}; // namespace
46+
} // namespace
4547

4648
void add_simplex_module(py::module& root) {
4749
auto m =
4850
root.def_submodule("simplex", "Module containing the SimplexDecoder and related methods");
4951

50-
py::class_<SimplexConfig>(m, "SimplexConfig", R"pbdoc(
52+
auto py_simplex_config = py::class_<SimplexConfig>(m, "SimplexConfig", R"pbdoc(
5153
Configuration object for the `SimplexDecoder`.
5254
5355
This class holds all the parameters needed to initialize and configure a
5456
Simplex decoder instance, including the detector error model and
5557
decoding options.
56-
)pbdoc")
58+
)pbdoc");
59+
auto py_simplex_decoder = py::class_<SimplexDecoder>(m, "SimplexDecoder", R"pbdoc(
60+
A class that implements the Simplex decoding algorithm.
61+
62+
It can decode syndromes from a `stim.DetectorErrorModel` to predict
63+
which observables have been flipped.
64+
)pbdoc");
65+
66+
py_simplex_config
5767
.def(py::init(&simplex_config_maker), py::arg("dem"), py::arg("parallelize") = false,
5868
py::arg("window_length") = 0, py::arg("window_slide_length") = 0,
5969
py::arg("verbose") = false, py::arg("merge_errors") = true, R"pbdoc(
@@ -101,12 +111,7 @@ void add_simplex_module(py::module& root) {
101111
settings.
102112
)pbdoc");
103113

104-
py::class_<SimplexDecoder>(m, "SimplexDecoder", R"pbdoc(
105-
A class that implements the Simplex decoding algorithm.
106-
107-
It can decode syndromes from a `stim.DetectorErrorModel` to predict
108-
which observables have been flipped.
109-
)pbdoc")
114+
py_simplex_decoder
110115
.def(py::init<SimplexConfig>(), py::arg("config"), R"pbdoc(
111116
The constructor for the `SimplexDecoder` class.
112117
@@ -351,4 +356,7 @@ void add_simplex_module(py::module& root) {
351356
(num_shots, num_observables).
352357
)pbdoc");
353358
}
359+
360+
} // namespace tesseract_decoder
361+
354362
#endif

src/simplex_main.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "stim.h"
2525
#include "utils.h"
2626

27+
using namespace tesseract_decoder;
28+
2729
struct Args {
2830
std::string circuit_path;
2931
std::string dem_path;

0 commit comments

Comments
 (0)