-
Notifications
You must be signed in to change notification settings - Fork 10
link tesseract with stim and add tests for the python wrapper of common.h #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c0a000f
Move the bazel rules for the python wrapper to MODULE.bazel
NoureldinYosri 7048319
fix module name
NoureldinYosri 9458f66
fix pybind11 build
NoureldinYosri 7acceb9
link tesseract with stim and add tests for the python wrapper of comm…
NoureldinYosri 8b67325
nit
NoureldinYosri 588e00b
nit
NoureldinYosri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
load("@pybind11_bazel//:build_defs.bzl", "pybind_library") | ||
load("@pybind11_bazel//:build_defs.bzl", "pybind_extension") | ||
|
||
SOURCE_FILES_NO_MAIN = glob( | ||
[ | ||
"src/**/*.cc", | ||
"src/**/*.h", | ||
"src/**/*.inl", | ||
], | ||
exclude = glob([ | ||
"src/**/*.test.cc", | ||
"src/**/*.test.h", | ||
"src/**/*.perf.cc", | ||
"src/**/*.perf.h", | ||
"src/**/*.pybind.cc", | ||
"src/**/*.pybind.h", | ||
"src/**/main.cc", | ||
]), | ||
) | ||
|
||
PYBIND_MODULES = [ | ||
"src/stim/py/march.pybind.cc", | ||
"src/stim/py/stim.pybind.cc", | ||
] | ||
|
||
PYBIND_FILES_WITHOUT_MODULES = glob( | ||
[ | ||
"src/**/*.pybind.cc", | ||
"src/**/*.pybind.h", | ||
], | ||
exclude=PYBIND_MODULES, | ||
) | ||
|
||
|
||
|
||
pybind_library( | ||
name = "stim_pybind_lib", | ||
srcs = SOURCE_FILES_NO_MAIN + PYBIND_FILES_WITHOUT_MODULES, | ||
copts = [ | ||
"-O3", | ||
"-std=c++20", | ||
"-fvisibility=hidden", | ||
"-march=native", | ||
"-DVERSION_INFO=0.0.dev0", | ||
], | ||
includes = ["src/"], | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
pybind_extension( | ||
name = "stim", | ||
srcs = PYBIND_MODULES, | ||
copts = [ | ||
"-O3", | ||
"-std=c++20", | ||
"-fvisibility=hidden", | ||
"-march=native", | ||
"-DSTIM_PYBIND11_MODULE_NAME=stim", | ||
"-DVERSION_INFO=0.0.dev0", | ||
], | ||
deps=[":stim_pybind_lib"], | ||
includes = ["src/"], | ||
visibility = ["//visibility:public"], | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,54 @@ | ||
#ifndef TESSERACT_COMMON_PY_H | ||
#define TESSERACT_COMMON_PY_H | ||
|
||
#include <vector> | ||
|
||
#include <pybind11/operators.h> | ||
#include <pybind11/pybind11.h> | ||
#include <pybind11/stl.h> | ||
|
||
#include <vector> | ||
#include "src/stim/dem/dem_instruction.pybind.h" | ||
#include "stim/dem/detector_error_model_target.pybind.h" | ||
|
||
#include "common.h" | ||
|
||
namespace py = pybind11; | ||
|
||
void add_common_module(py::module &root) { | ||
auto m = root.def_submodule("common", "classes commonly used by the decoder"); | ||
|
||
// TODO: add as_dem_instruction_targets | ||
py::class_<common::Symptom>(m, "Symptom") | ||
.def(py::init<std::vector<int>, common::ObservablesMask>(), | ||
py::arg("detectors") = std::vector<int>(), | ||
py::arg("observables") = 0) | ||
.def_readwrite("detectors", &common::Symptom::detectors) | ||
.def_readwrite("observables", &common::Symptom::observables) | ||
.def("__str__", &common::Symptom::str) | ||
.def(py::self == py::self) | ||
.def(py::self != py::self); | ||
|
||
// TODO: add constructor with stim::DemInstruction. | ||
py::class_<common::Error>(m, "Error") | ||
.def_readwrite("likelihood_cost", &common::Error::likelihood_cost) | ||
.def_readwrite("probability", &common::Error::probability) | ||
.def_readwrite("symptom", &common::Error::symptom) | ||
.def("__str__", &common::Error::str) | ||
.def(py::init<>()) | ||
.def(py::init<double, std::vector<int> &, common::ObservablesMask, | ||
std::vector<bool> &>()) | ||
.def(py::init<double, double, std::vector<int> &, common::ObservablesMask, | ||
std::vector<bool> &>()); | ||
void add_common_module(py::module &root) | ||
{ | ||
auto m = root.def_submodule("common", "classes commonly used by the decoder"); | ||
|
||
py::class_<common::Symptom>(m, "Symptom") | ||
.def(py::init<std::vector<int>, common::ObservablesMask>(), | ||
py::arg("detectors") = std::vector<int>(), | ||
py::arg("observables") = 0) | ||
.def_readwrite("detectors", &common::Symptom::detectors) | ||
.def_readwrite("observables", &common::Symptom::observables) | ||
.def("__str__", &common::Symptom::str) | ||
.def(py::self == py::self) | ||
.def(py::self != py::self) | ||
.def("as_dem_instruction_targets", [](common::Symptom s) | ||
{ | ||
std::vector<stim_pybind::ExposedDemTarget> ret; | ||
for(auto & t : s.as_dem_instruction_targets()) ret.emplace_back(t); | ||
return ret; }); | ||
|
||
py::class_<common::Error>(m, "Error") | ||
.def_readwrite("likelihood_cost", &common::Error::likelihood_cost) | ||
.def_readwrite("probability", &common::Error::probability) | ||
.def_readwrite("symptom", &common::Error::symptom) | ||
.def("__str__", &common::Error::str) | ||
.def(py::init<>()) | ||
.def(py::init<double, std::vector<int> &, common::ObservablesMask, | ||
std::vector<bool> &>()) | ||
.def(py::init<double, double, std::vector<int> &, common::ObservablesMask, | ||
std::vector<bool> &>()) | ||
.def(py::init([](stim_pybind::ExposedDemInstruction edi) | ||
{ return new common::Error(edi.as_dem_instruction()); })); | ||
|
||
m.def("merge_identical_errors", &common::merge_identical_errors); | ||
m.def("remove_zero_probability_errors", &common::remove_zero_probability_errors); | ||
m.def("dem_from_counts", &common::dem_from_counts); | ||
} | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import pytest | ||
import stim | ||
|
||
from src import tesseract_decoder | ||
|
||
|
||
def test_as_dem_instruction_targets(): | ||
s = tesseract_decoder.common.Symptom([1, 2], 4324) | ||
dits = s.as_dem_instruction_targets() | ||
assert dits == [ | ||
stim.DemTarget("D1"), | ||
stim.DemTarget("D2"), | ||
stim.DemTarget("L2"), | ||
stim.DemTarget("L5"), | ||
stim.DemTarget("L6"), | ||
stim.DemTarget("L7"), | ||
stim.DemTarget("L12"), | ||
] | ||
|
||
|
||
def test_error_from_dem_instruction(): | ||
di = stim.DemInstruction("error", [0.125], [stim.target_logical_observable_id(3)]) | ||
error = tesseract_decoder.common.Error(di) | ||
|
||
assert str(error) == "Error{cost=1.945910, symptom=Symptom{}}" | ||
|
||
|
||
def test_merge_identical_errors(): | ||
dem = stim.DetectorErrorModel() | ||
assert isinstance( | ||
tesseract_decoder.common.merge_identical_errors(dem), stim.DetectorErrorModel | ||
) | ||
|
||
|
||
def test_remove_zero_probability_errors(): | ||
dem = stim.DetectorErrorModel() | ||
assert isinstance( | ||
tesseract_decoder.common.remove_zero_probability_errors(dem), | ||
stim.DetectorErrorModel, | ||
) | ||
|
||
|
||
def test_dem_from_counts(): | ||
dem = stim.DetectorErrorModel() | ||
assert isinstance( | ||
tesseract_decoder.common.dem_from_counts(dem, [], 3), stim.DetectorErrorModel | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
raise SystemExit(pytest.main([__file__])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
stim | ||
pytest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a general question Nour!
do you think there might be a Stim version mismatch between this line that requires Stim 15 and the one in lines 21–26 of the same WORKSPACE file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
technically yes, but the difference between these two versions is two days ... for now this is fine since this is a workaround ... I think I should add the bazel rules in external/stim_py to stim itself to avoid having two dependencies on stim