|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
1 | 15 | #ifndef TESSERACT_COMMON_PY_H |
2 | 16 | #define TESSERACT_COMMON_PY_H |
3 | 17 |
|
4 | | -#include <vector> |
5 | | - |
6 | 18 | #include <pybind11/operators.h> |
7 | 19 | #include <pybind11/pybind11.h> |
8 | 20 | #include <pybind11/stl.h> |
9 | 21 |
|
10 | | -#include "src/stim/dem/dem_instruction.pybind.h" |
11 | | -#include "stim/dem/detector_error_model_target.pybind.h" |
| 22 | +#include <vector> |
12 | 23 |
|
13 | 24 | #include "common.h" |
| 25 | +#include "src/stim/dem/dem_instruction.pybind.h" |
| 26 | +#include "stim/dem/detector_error_model_target.pybind.h" |
14 | 27 |
|
15 | 28 | namespace py = pybind11; |
16 | 29 |
|
17 | | -void add_common_module(py::module &root) |
18 | | -{ |
19 | | - auto m = root.def_submodule("common", "classes commonly used by the decoder"); |
20 | | - |
21 | | - py::class_<common::Symptom>(m, "Symptom") |
22 | | - .def(py::init<std::vector<int>, common::ObservablesMask>(), |
23 | | - py::arg("detectors") = std::vector<int>(), |
24 | | - py::arg("observables") = 0) |
25 | | - .def_readwrite("detectors", &common::Symptom::detectors) |
26 | | - .def_readwrite("observables", &common::Symptom::observables) |
27 | | - .def("__str__", &common::Symptom::str) |
28 | | - .def(py::self == py::self) |
29 | | - .def(py::self != py::self) |
30 | | - .def("as_dem_instruction_targets", [](common::Symptom s) |
31 | | - { |
32 | | - std::vector<stim_pybind::ExposedDemTarget> ret; |
33 | | - for(auto & t : s.as_dem_instruction_targets()) ret.emplace_back(t); |
34 | | - return ret; }); |
35 | | - |
36 | | - py::class_<common::Error>(m, "Error") |
37 | | - .def_readwrite("likelihood_cost", &common::Error::likelihood_cost) |
38 | | - .def_readwrite("probability", &common::Error::probability) |
39 | | - .def_readwrite("symptom", &common::Error::symptom) |
40 | | - .def("__str__", &common::Error::str) |
41 | | - .def(py::init<>()) |
42 | | - .def(py::init<double, std::vector<int> &, common::ObservablesMask, |
43 | | - std::vector<bool> &>()) |
44 | | - .def(py::init<double, double, std::vector<int> &, common::ObservablesMask, |
45 | | - std::vector<bool> &>()) |
46 | | - .def(py::init([](stim_pybind::ExposedDemInstruction edi) |
47 | | - { return new common::Error(edi.as_dem_instruction()); })); |
48 | | - |
49 | | - m.def("merge_identical_errors", &common::merge_identical_errors); |
50 | | - m.def("remove_zero_probability_errors", &common::remove_zero_probability_errors); |
51 | | - m.def("dem_from_counts", &common::dem_from_counts); |
| 30 | +void add_common_module(py::module &root) { |
| 31 | + auto m = root.def_submodule("common", "classes commonly used by the decoder"); |
| 32 | + |
| 33 | + py::class_<common::Symptom>(m, "Symptom") |
| 34 | + .def(py::init<std::vector<int>, common::ObservablesMask>(), |
| 35 | + py::arg("detectors") = std::vector<int>(), |
| 36 | + py::arg("observables") = 0) |
| 37 | + .def_readwrite("detectors", &common::Symptom::detectors) |
| 38 | + .def_readwrite("observables", &common::Symptom::observables) |
| 39 | + .def("__str__", &common::Symptom::str) |
| 40 | + .def(py::self == py::self) |
| 41 | + .def(py::self != py::self) |
| 42 | + .def("as_dem_instruction_targets", [](common::Symptom s) { |
| 43 | + std::vector<stim_pybind::ExposedDemTarget> ret; |
| 44 | + for (auto &t : s.as_dem_instruction_targets()) ret.emplace_back(t); |
| 45 | + return ret; |
| 46 | + }); |
| 47 | + |
| 48 | + py::class_<common::Error>(m, "Error") |
| 49 | + .def_readwrite("likelihood_cost", &common::Error::likelihood_cost) |
| 50 | + .def_readwrite("probability", &common::Error::probability) |
| 51 | + .def_readwrite("symptom", &common::Error::symptom) |
| 52 | + .def("__str__", &common::Error::str) |
| 53 | + .def(py::init<>()) |
| 54 | + .def(py::init<double, std::vector<int> &, common::ObservablesMask, |
| 55 | + std::vector<bool> &>(), |
| 56 | + py::arg("likelihood_cost"), py::arg("detectors"), |
| 57 | + py::arg("observables"), py::arg("dets_array")) |
| 58 | + .def(py::init<double, double, std::vector<int> &, common::ObservablesMask, |
| 59 | + std::vector<bool> &>(), |
| 60 | + py::arg("likelihood_cost"), py::arg("probability"), |
| 61 | + py::arg("detectors"), py::arg("observables"), py::arg("dets_array")) |
| 62 | + .def(py::init([](stim_pybind::ExposedDemInstruction edi) { |
| 63 | + return new common::Error(edi.as_dem_instruction()); |
| 64 | + }), |
| 65 | + py::arg("error")); |
| 66 | + |
| 67 | + m.def("merge_identical_errors", &common::merge_identical_errors, |
| 68 | + py::arg("dem")); |
| 69 | + m.def("remove_zero_probability_errors", |
| 70 | + &common::remove_zero_probability_errors, py::arg("dem")); |
| 71 | + m.def("dem_from_counts", &common::dem_from_counts, py::arg("orig_dem"), |
| 72 | + py::arg("error_counts"), py::arg("num_shots")); |
52 | 73 | } |
53 | 74 |
|
54 | 75 | #endif |
0 commit comments