Skip to content

Commit b8f4393

Browse files
committed
Add multi-pass infrastructure libraries
Add foundational libraries for multi-pass decoding: - bern_utils: Bernoulli probability utilities (log-likelihood conversion, probability clamping) - tanner_graph: Union-Find-based connected component analysis of the detector-error Tanner graph - error_correlations: Correlation extraction pipeline computing marginal, joint, and conditional error probabilities from first-pass decoding results - dem_decomposition: DEM decomposition by detector class, error splitting across components, observable assignment, and DEM merging for multi-component decoding
1 parent 4b1c379 commit b8f4393

13 files changed

Lines changed: 1290 additions & 8 deletions

CMakeLists.txt

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ project(tesseract_decoder LANGUAGES CXX)
33

44
set(CMAKE_CXX_STANDARD 20)
55
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -include cstdint")
67

78
include(FetchContent)
89
find_package(Threads REQUIRED)
@@ -73,7 +74,7 @@ FetchContent_Declare(
7374
FetchContent_MakeAvailable(googletest)
7475

7576

76-
set(OPT_COPTS -Ofast -fno-fast-math -march=native)
77+
set(OPT_COPTS -Ofast -fno-fast-math -march=native -include cstdint)
7778

7879
set(TESSERACT_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
7980

@@ -93,6 +94,25 @@ target_include_directories(visualization PUBLIC ${TESSERACT_SRC_DIR})
9394
target_compile_options(visualization PRIVATE ${OPT_COPTS})
9495
target_link_libraries(visualization PUBLIC common boost_headers)
9596

97+
add_library(bern_utils ${TESSERACT_SRC_DIR}/bern_utils.cc ${TESSERACT_SRC_DIR}/bern_utils.h)
98+
target_include_directories(bern_utils PUBLIC ${TESSERACT_SRC_DIR})
99+
target_compile_options(bern_utils PRIVATE ${OPT_COPTS})
100+
101+
add_library(error_correlations ${TESSERACT_SRC_DIR}/error_correlations.cc ${TESSERACT_SRC_DIR}/error_correlations.h)
102+
target_include_directories(error_correlations PUBLIC ${TESSERACT_SRC_DIR})
103+
target_compile_options(error_correlations PRIVATE ${OPT_COPTS})
104+
target_link_libraries(error_correlations PUBLIC libstim)
105+
106+
add_library(tanner_graph ${TESSERACT_SRC_DIR}/tanner_graph.cc ${TESSERACT_SRC_DIR}/tanner_graph.h)
107+
target_include_directories(tanner_graph PUBLIC ${TESSERACT_SRC_DIR})
108+
target_compile_options(tanner_graph PRIVATE ${OPT_COPTS})
109+
target_link_libraries(tanner_graph PUBLIC libstim)
110+
111+
add_library(dem_decomposition ${TESSERACT_SRC_DIR}/dem_decomposition.cc ${TESSERACT_SRC_DIR}/dem_decomposition.h)
112+
target_include_directories(dem_decomposition PUBLIC ${TESSERACT_SRC_DIR})
113+
target_compile_options(dem_decomposition PRIVATE ${OPT_COPTS})
114+
target_link_libraries(dem_decomposition PUBLIC bern_utils libstim)
115+
96116
add_library(tesseract_lib ${TESSERACT_SRC_DIR}/tesseract.cc ${TESSERACT_SRC_DIR}/tesseract.h)
97117
target_include_directories(tesseract_lib PUBLIC ${TESSERACT_SRC_DIR})
98118
target_compile_options(tesseract_lib PRIVATE ${OPT_COPTS})
@@ -117,13 +137,12 @@ target_link_libraries(simplex_bin PRIVATE common simplex argparse::argparse nloh
117137
pybind11_add_module(_core MODULE ${TESSERACT_SRC_DIR}/tesseract.pybind.cc)
118138
target_compile_options(_core PRIVATE ${OPT_COPTS})
119139
target_include_directories(_core PRIVATE ${TESSERACT_SRC_DIR})
120-
target_link_libraries(_core PRIVATE common utils simplex tesseract_lib)
121140
set_target_properties(_core PROPERTIES
122-
LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/src
123-
LIBRARY_OUTPUT_DIRECTORY_DEBUG ${PROJECT_SOURCE_DIR}/src
124-
LIBRARY_OUTPUT_DIRECTORY_RELEASE ${PROJECT_SOURCE_DIR}/src
125-
LIBRARY_OUTPUT_DIRECTORY_MINSIZEREL ${PROJECT_SOURCE_DIR}/src
126-
LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO ${PROJECT_SOURCE_DIR}/src
141+
LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/tesseract_decoder
142+
LIBRARY_OUTPUT_DIRECTORY_DEBUG ${PROJECT_SOURCE_DIR}/tesseract_decoder
143+
LIBRARY_OUTPUT_DIRECTORY_RELEASE ${PROJECT_SOURCE_DIR}/tesseract_decoder
144+
LIBRARY_OUTPUT_DIRECTORY_MINSIZEREL ${PROJECT_SOURCE_DIR}/tesseract_decoder
145+
LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO ${PROJECT_SOURCE_DIR}/tesseract_decoder
127146
)
128147

129148
# === Tests ===
@@ -137,3 +156,15 @@ add_executable(tesseract_test ${TESSERACT_SRC_DIR}/tesseract.test.cc)
137156
target_link_libraries(tesseract_test PRIVATE tesseract_lib simplex GTest::gtest_main)
138157
add_test(NAME tesseract_test COMMAND tesseract_test)
139158

159+
add_executable(dem_decomposition_test ${TESSERACT_SRC_DIR}/dem_decomposition.test.cc)
160+
target_link_libraries(dem_decomposition_test PRIVATE dem_decomposition GTest::gtest_main libstim)
161+
add_test(NAME dem_decomposition_test COMMAND dem_decomposition_test)
162+
163+
add_executable(tanner_graph_test ${TESSERACT_SRC_DIR}/tanner_graph.test.cc)
164+
target_link_libraries(tanner_graph_test PRIVATE tanner_graph GTest::gtest_main libstim)
165+
add_test(NAME tanner_graph_test COMMAND tanner_graph_test)
166+
167+
add_executable(error_correlations_test ${TESSERACT_SRC_DIR}/error_correlations.test.cc)
168+
target_link_libraries(error_correlations_test PRIVATE error_correlations GTest::gtest_main libstim)
169+
add_test(NAME error_correlations_test COMMAND error_correlations_test)
170+

src/BUILD

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ pybind_extension(
9393
)
9494

9595

96-
9796
cc_library(
9897
name = "libutils",
9998
srcs = ["utils.cc"],
@@ -133,6 +132,87 @@ cc_library(
133132
],
134133
)
135134

135+
cc_library(
136+
name = "liberror_correlations",
137+
srcs = ["error_correlations.cc"],
138+
hdrs = ["error_correlations.h"],
139+
copts = OPT_COPTS,
140+
linkopts = OPT_LINKOPTS,
141+
deps = [
142+
"@stim//:stim_lib",
143+
],
144+
)
145+
146+
cc_test(
147+
name = "error_correlations_tests",
148+
srcs = ["error_correlations.test.cc"],
149+
copts = OPT_COPTS,
150+
linkopts = OPT_LINKOPTS,
151+
deps = [
152+
":liberror_correlations",
153+
"@gtest",
154+
"@gtest//:gtest_main",
155+
"@stim//:stim_lib",
156+
],
157+
)
158+
159+
cc_library(
160+
name = "libtanner_graph",
161+
srcs = ["tanner_graph.cc"],
162+
hdrs = ["tanner_graph.h"],
163+
copts = OPT_COPTS,
164+
linkopts = OPT_LINKOPTS,
165+
deps = [
166+
"@stim//:stim_lib",
167+
],
168+
)
169+
170+
cc_test(
171+
name = "tanner_graph_tests",
172+
srcs = ["tanner_graph.test.cc"],
173+
copts = OPT_COPTS,
174+
linkopts = OPT_LINKOPTS,
175+
deps = [
176+
":libtanner_graph",
177+
"@gtest",
178+
"@gtest//:gtest_main",
179+
"@stim//:stim_lib",
180+
],
181+
)
182+
183+
cc_library(
184+
name = "libbern_utils",
185+
srcs = ["bern_utils.cc"],
186+
hdrs = ["bern_utils.h"],
187+
copts = OPT_COPTS,
188+
linkopts = OPT_LINKOPTS,
189+
)
190+
191+
cc_library(
192+
name = "libdem_decomposition",
193+
srcs = ["dem_decomposition.cc"],
194+
hdrs = ["dem_decomposition.h"],
195+
copts = OPT_COPTS,
196+
linkopts = OPT_LINKOPTS,
197+
deps = [
198+
":libbern_utils",
199+
"@stim//:stim_lib",
200+
],
201+
)
202+
203+
cc_test(
204+
name = "dem_decomposition_tests",
205+
srcs = ["dem_decomposition.test.cc"],
206+
copts = OPT_COPTS,
207+
linkopts = OPT_LINKOPTS,
208+
deps = [
209+
":libdem_decomposition",
210+
"@gtest",
211+
"@gtest//:gtest_main",
212+
"@stim//:stim_lib",
213+
],
214+
)
215+
136216
cc_binary(
137217
name = "tesseract",
138218
srcs = ["tesseract_main.cc"],

src/bern_utils.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "bern_utils.h"
2+
#include <cmath>
3+
#include <limits>
4+
5+
namespace tesseract {
6+
7+
double bernoulli_xor(double p1, double p2) {
8+
return p1 * (1 - p2) + p2 * (1 - p1);
9+
}
10+
11+
double to_weight(double probability) {
12+
if (probability >= 1.0) {
13+
return -std::numeric_limits<double>::infinity();
14+
}
15+
if (probability <= 0) {
16+
return std::numeric_limits<double>::infinity();
17+
}
18+
return std::log((1 - probability) / probability);
19+
}
20+
21+
} // namespace two_pass_decoding

src/bern_utils.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef BERN_UTILS_H
2+
#define BERN_UTILS_H
3+
4+
namespace tesseract {
5+
6+
// Calculates the probability of an odd number of independent events with
7+
// probabilities p1 and p2 occurring: p1*(1-p2) + p2*(1-p1).
8+
double bernoulli_xor(double p1, double p2);
9+
10+
// Converts a probability to a log-likelihood ratio weight.
11+
// The weight is calculated as w = ln((1-p)/p).
12+
double to_weight(double probability);
13+
14+
} // namespace two_pass_decoding
15+
16+
#endif // BERN_UTILS_H
17+

0 commit comments

Comments
 (0)