Skip to content
Merged
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
7 changes: 5 additions & 2 deletions src/simplex.pybind.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef _SIMPLEX_PYBIND_H
#define _SIMPLEX_PYBIND_H

#include <pybind11/iostream.h>
#include <pybind11/operators.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
Expand Down Expand Up @@ -63,7 +64,8 @@ void add_simplex_module(py::module& root) {
.def_readwrite("end_time_to_errors", &SimplexDecoder::end_time_to_errors)
.def_readonly("low_confidence_flag", &SimplexDecoder::low_confidence_flag)
.def("init_ilp", &SimplexDecoder::init_ilp)
.def("decode_to_errors", &SimplexDecoder::decode_to_errors, py::arg("detections"))
.def("decode_to_errors", &SimplexDecoder::decode_to_errors, py::arg("detections"),
py::call_guard<py::scoped_ostream_redirect, py::scoped_estream_redirect>())
.def(
"get_observables_from_errors",
[](SimplexDecoder& self, const std::vector<size_t>& predicted_errors) {
Expand All @@ -88,6 +90,7 @@ void add_simplex_module(py::module& root) {
}
return result;
},
py::arg("detections"));
py::arg("detections"),
py::call_guard<py::scoped_ostream_redirect, py::scoped_estream_redirect>());
}
#endif
11 changes: 11 additions & 0 deletions src/tesseract.pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "tesseract.pybind.h"

#include <pybind11/iostream.h>
#include <pybind11/pybind11.h>

#include "common.pybind.h"
Expand All @@ -23,8 +24,18 @@

PYBIND11_MODULE(tesseract_decoder, tesseract) {
py::module::import("stim");

add_common_module(tesseract);
add_utils_module(tesseract);
add_simplex_module(tesseract);
add_tesseract_module(tesseract);

// Adds a context manager to the python library that can be used to redirect C++'s stdout/stderr
// to python's stdout/stderr at run time like
// with tesseract_decoder.ostream_redirect(stdout=..., stderr=...):
// do_work()
// This is only needed if the C++ function's stdout/stderr is not redirected to python's
// stdout/stderr using the py::call_guard<py::scoped_ostream_redirect,
// py::scoped_estream_redirect>() statement.
py::add_ostream_redirect(tesseract, "ostream_redirect");
}
10 changes: 7 additions & 3 deletions src/tesseract.pybind.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef _TESSERACT_PYBIND_H
#define _TESSERACT_PYBIND_H

#include <pybind11/iostream.h>
#include <pybind11/operators.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
Expand Down Expand Up @@ -71,11 +72,13 @@ void add_tesseract_module(py::module& root) {
.def(py::init<TesseractConfig>(), py::arg("config"))
.def("decode_to_errors",
py::overload_cast<const std::vector<uint64_t>&>(&TesseractDecoder::decode_to_errors),
py::arg("detections"))
py::arg("detections"),
py::call_guard<py::scoped_ostream_redirect, py::scoped_estream_redirect>())
.def("decode_to_errors",
py::overload_cast<const std::vector<uint64_t>&, size_t, size_t>(
&TesseractDecoder::decode_to_errors),
py::arg("detections"), py::arg("det_order"), py::arg("det_beam"))
py::arg("detections"), py::arg("det_order"), py::arg("det_beam"),
py::call_guard<py::scoped_ostream_redirect, py::scoped_estream_redirect>())
.def(
"get_observables_from_errors",
[](TesseractDecoder& self, const std::vector<size_t>& predicted_errors) {
Expand All @@ -100,7 +103,8 @@ void add_tesseract_module(py::module& root) {
}
return result;
},
py::arg("detections"))
py::arg("detections"),
py::call_guard<py::scoped_ostream_redirect, py::scoped_estream_redirect>())
.def_readwrite("low_confidence_flag", &TesseractDecoder::low_confidence_flag)
.def_readwrite("predicted_errors_buffer", &TesseractDecoder::predicted_errors_buffer)
.def_readwrite("errors", &TesseractDecoder::errors);
Expand Down
Loading