Skip to content

Commit b4c9d08

Browse files
committed
Merge remote-tracking branch 'quantum/main'
2 parents 106ce02 + fd23944 commit b4c9d08

File tree

8 files changed

+55
-21
lines changed

8 files changed

+55
-21
lines changed

src/common.cc

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,31 @@
1414

1515
#include "common.h"
1616

17-
std::string common::Symptom::str() const {
18-
std::string s = "Symptom{";
19-
for (size_t d : detectors) {
20-
s += "D" + std::to_string(d);
21-
s += " ";
17+
#include <iomanip>
18+
#include <iostream>
19+
#include <sstream>
20+
#include <string>
21+
#include <vector>
22+
23+
std::string vector_to_string(const std::vector<int>& vec) {
24+
std::stringstream ss;
25+
ss << "[";
26+
for (size_t i = 0; i < vec.size(); ++i) {
27+
ss << vec[i];
28+
if (i < vec.size() - 1) {
29+
ss << " ";
30+
}
2231
}
32+
33+
ss << "]";
34+
return ss.str();
35+
}
36+
37+
std::string common::Symptom::str() const {
38+
std::string s = "Symptom{detectors=";
39+
s += vector_to_string(detectors);
40+
s += ", observables=";
41+
s += vector_to_string(observables);
2342
s += "}";
2443
return s;
2544
}
@@ -63,7 +82,9 @@ common::Error::Error(const stim::DemInstruction& error) {
6382
}
6483

6584
std::string common::Error::str() const {
66-
return "Error{cost=" + std::to_string(likelihood_cost) + ", symptom=" + symptom.str() + "}";
85+
std::stringstream ss;
86+
ss << std::fixed << std::setprecision(6) << likelihood_cost;
87+
return "Error{cost=" + ss.str() + ", symptom=" + symptom.str() + "}";
6788
}
6889

6990
double common::Error::get_probability() const {

src/common.pybind.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
namespace py = pybind11;
2828

29-
void add_common_module(py::module &root) {
29+
void add_common_module(py::module& root) {
3030
auto m = root.def_submodule("common", "classes commonly used by the decoder");
3131

3232
py::class_<common::Symptom>(m, "Symptom", R"pbdoc(
@@ -58,7 +58,7 @@ void add_common_module(py::module &root) {
5858
"as_dem_instruction_targets",
5959
[](common::Symptom s) {
6060
std::vector<py::object> ret;
61-
for (auto &t : s.as_dem_instruction_targets())
61+
for (auto& t : s.as_dem_instruction_targets())
6262
ret.push_back(make_py_object(t, "DemTarget"));
6363
return ret;
6464
},
@@ -84,7 +84,7 @@ void add_common_module(py::module &root) {
8484
.def(py::init<>(), R"pbdoc(
8585
Default constructor for the `Error` class.
8686
)pbdoc")
87-
.def(py::init<double, std::vector<int> &, std::vector<int>>(), py::arg("likelihood_cost"),
87+
.def(py::init<double, std::vector<int>&, std::vector<int>>(), py::arg("likelihood_cost"),
8888
py::arg("detectors"), py::arg("observables"), R"pbdoc(
8989
Constructor for the `Error` class.
9090

src/py/common_test.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ def test_error_from_direct_constructor():
4949
assert error.symptom.observables == observables
5050

5151

52+
def test_error_str():
53+
# Test the new constructor with likelihood_cost, detectors, and observables
54+
likelihood_cost = 5.5
55+
detectors = [1, 2]
56+
observables = [5, 10]
57+
error = tesseract_decoder.common.Error(likelihood_cost, detectors, observables)
58+
59+
assert str(error) == "Error{cost=5.500000, symptom=Symptom{detectors=[1 2], observables=[5 10]}}"
60+
61+
5262
def test_as_dem_instruction_targets():
5363
s = tesseract_decoder.common.Symptom([1, 2], get_set_bits(4324))
5464
dits = s.as_dem_instruction_targets()
@@ -66,7 +76,7 @@ def test_as_dem_instruction_targets():
6676
def test_error_from_dem_instruction():
6777
di = stim.DemInstruction("error", [0.125], [stim.target_logical_observable_id(3)])
6878
error = tesseract_decoder.common.Error(di)
69-
assert str(error) == "Error{cost=1.945910, symptom=Symptom{}}"
79+
assert str(error) == "Error{cost=1.945910, symptom=Symptom{detectors=[], observables=[3]}}"
7080

7181
def test_error_get_set_probability():
7282
error = tesseract_decoder.common.Error()
@@ -121,5 +131,8 @@ def test_dem_from_counts():
121131
)
122132

123133

134+
135+
136+
124137
if __name__ == "__main__":
125138
raise SystemExit(pytest.main([__file__]))

src/py/utils_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def test_build_det_orders_no_bfs():
5757

5858

5959
def test_get_errors_from_dem():
60-
expected = "Error{cost=1.945910, symptom=Symptom{D0 }}, Error{cost=0.510826, symptom=Symptom{D0 D1 }}, Error{cost=1.098612, symptom=Symptom{D1 }}"
60+
expected = "Error{cost=1.945910, symptom=Symptom{detectors=[0], observables=[]}}, Error{cost=0.510826, symptom=Symptom{detectors=[0 1], observables=[]}}, Error{cost=1.098612, symptom=Symptom{detectors=[1], observables=[]}}"
6161
assert (
6262
", ".join(
6363
map(str, tesseract_decoder.utils.get_errors_from_dem(_DETECTOR_ERROR_MODEL))

src/simplex.pybind.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ void add_simplex_module(py::module& root) {
227227
.def(
228228
"decode",
229229
[](SimplexDecoder& self, const py::array_t<bool>& syndrome) {
230-
if (syndrome.size() != self.num_detectors) {
230+
if ((size_t)syndrome.size() != self.num_detectors) {
231231
std::ostringstream msg;
232232
msg << "Syndrome array size (" << syndrome.size()
233233
<< ") does not match the number of detectors in the decoder ("
@@ -237,7 +237,7 @@ void add_simplex_module(py::module& root) {
237237

238238
std::vector<uint64_t> detections;
239239
auto syndrome_unchecked = syndrome.unchecked<1>();
240-
for (size_t i = 0; i < syndrome_unchecked.size(); i++) {
240+
for (size_t i = 0; i < (size_t)syndrome_unchecked.size(); i++) {
241241
if (syndrome_unchecked(i)) {
242242
detections.push_back(i);
243243
}

src/tesseract.pybind.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ void add_tesseract_module(py::module& root) {
281281
.def(
282282
"decode",
283283
[](TesseractDecoder& self, const py::array_t<bool>& syndrome) {
284-
if (syndrome.size() != self.num_detectors) {
284+
if ((size_t)syndrome.size() != self.num_detectors) {
285285
std::ostringstream msg;
286286
msg << "Syndrome array size (" << syndrome.size()
287287
<< ") does not match the number of detectors in the decoder ("
@@ -291,7 +291,7 @@ void add_tesseract_module(py::module& root) {
291291

292292
std::vector<uint64_t> detections;
293293
auto syndrome_unchecked = syndrome.unchecked<1>();
294-
for (size_t i = 0; i < syndrome_unchecked.size(); ++i) {
294+
for (size_t i = 0; i < (size_t)syndrome_unchecked.size(); ++i) {
295295
if (syndrome_unchecked(i)) {
296296
detections.push_back(i);
297297
}

src/utils.pybind.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
namespace py = pybind11;
2525

26-
void add_utils_module(py::module &root) {
26+
void add_utils_module(py::module& root) {
2727
auto m = root.def_submodule("utils", "utility methods");
2828

2929
m.attr("EPSILON") = EPSILON;

src/visualization.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
#include "common.h"
99

1010
struct Visualizer {
11-
void add_detector_coords(const std::vector<std::vector<double>> &);
12-
void add_errors(const std::vector<common::Error> &);
13-
void add_activated_errors(const std::vector<size_t> &);
14-
void add_activated_detectors(const boost::dynamic_bitset<> &, size_t);
11+
void add_detector_coords(const std::vector<std::vector<double>>&);
12+
void add_errors(const std::vector<common::Error>&);
13+
void add_activated_errors(const std::vector<size_t>&);
14+
void add_activated_detectors(const boost::dynamic_bitset<>&, size_t);
1515

16-
void write(const char *fpath);
16+
void write(const char* fpath);
1717

1818
private:
1919
std::list<std::string> lines;

0 commit comments

Comments
 (0)