Skip to content

Commit

Permalink
remove old method, add better docstrings, add more tests and fix shor…
Browse files Browse the repository at this point in the history
…test error case for probability 0 errors
  • Loading branch information
noajshu committed Mar 15, 2024
1 parent a314b62 commit 0a4fc3e
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 172 deletions.
26 changes: 10 additions & 16 deletions doc/python_api_reference_vDev.md
Original file line number Diff line number Diff line change
Expand Up @@ -2145,13 +2145,13 @@ def likeliest_error_sat_problem(
quantization: int = 100,
format: str = 'WDIMACS',
) -> str:
"""Makes a maxSAT problem of the circuit's distance, that other tools can solve.
"""Makes a maxSAT problem of the circuit's most likely undetectable logical
error, that other tools can solve.
The output is a string describing the maxSAT problem in WDIMACS format
(see https://maxhs.org/docs/wdimacs.html). The optimal solution to the
problem is the fault distance of the circuit (the minimum number of error
mechanisms that combine to flip any logical observable while producing no
detection events).
problem is the highest likelihood set of error mechanisms that combine to
flip any logical observable while producing no detection events).
There are many tools that can solve maxSAT problems in WDIMACS format.
For example, you can download one of the entries in the 2023 maxSAT
Expand All @@ -2165,12 +2165,12 @@ def likeliest_error_sat_problem(
Args:
format: Defaults to "WDIMACS", corresponding to WDIMACS format which is
described here: http://www.maxhs.org/docs/wdimacs.html
weight_bins: Defaults to 1 (unweighted errors). Error probabilities are
converted to log-odds and scaled/rounded to be positive integers at
most this large. Setting this argument to a larger number results in
the solution to the problem being the most likely logical error
weighted by probability of its parts, instead of just the one with
the fewest parts.
quantization: Defaults to 10. Error probabilities are converted to log-odds
and scaled/rounded to be positive integers at most this large. Setting
this argument to a larger number results in more accurate quantization
such that the returned error set should have a likelihood closer to the
true most likely solution. This comes at the cost of making some maxSAT
solvers slower.
Returns:
A string corresponding to the contents of a WCNF file in the requested
Expand Down Expand Up @@ -2541,12 +2541,6 @@ def shortest_error_sat_problem(
Args:
format: Defaults to "WDIMACS", corresponding to WDIMACS format which is
described here: http://www.maxhs.org/docs/wdimacs.html
weight_bins: Defaults to 1 (unweighted errors). Error probabilities are
converted to log-odds and scaled/rounded to be positive integers at
most this large. Setting this argument to a larger number results in
the solution to the problem being the most likely logical error
weighted by probability of its parts, instead of just the one with
the fewest parts.
Returns:
A string corresponding to the contents of a WCNF file in the requested
Expand Down
26 changes: 10 additions & 16 deletions doc/stim.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1531,13 +1531,13 @@ class Circuit:
quantization: int = 100,
format: str = 'WDIMACS',
) -> str:
"""Makes a maxSAT problem of the circuit's distance, that other tools can solve.
"""Makes a maxSAT problem of the circuit's most likely undetectable logical
error, that other tools can solve.
The output is a string describing the maxSAT problem in WDIMACS format
(see https://maxhs.org/docs/wdimacs.html). The optimal solution to the
problem is the fault distance of the circuit (the minimum number of error
mechanisms that combine to flip any logical observable while producing no
detection events).
problem is the highest likelihood set of error mechanisms that combine to
flip any logical observable while producing no detection events).
There are many tools that can solve maxSAT problems in WDIMACS format.
For example, you can download one of the entries in the 2023 maxSAT
Expand All @@ -1551,12 +1551,12 @@ class Circuit:
Args:
format: Defaults to "WDIMACS", corresponding to WDIMACS format which is
described here: http://www.maxhs.org/docs/wdimacs.html
weight_bins: Defaults to 1 (unweighted errors). Error probabilities are
converted to log-odds and scaled/rounded to be positive integers at
most this large. Setting this argument to a larger number results in
the solution to the problem being the most likely logical error
weighted by probability of its parts, instead of just the one with
the fewest parts.
quantization: Defaults to 10. Error probabilities are converted to log-odds
and scaled/rounded to be positive integers at most this large. Setting
this argument to a larger number results in more accurate quantization
such that the returned error set should have a likelihood closer to the
true most likely solution. This comes at the cost of making some maxSAT
solvers slower.
Returns:
A string corresponding to the contents of a WCNF file in the requested
Expand Down Expand Up @@ -1864,12 +1864,6 @@ class Circuit:
Args:
format: Defaults to "WDIMACS", corresponding to WDIMACS format which is
described here: http://www.maxhs.org/docs/wdimacs.html
weight_bins: Defaults to 1 (unweighted errors). Error probabilities are
converted to log-odds and scaled/rounded to be positive integers at
most this large. Setting this argument to a larger number results in
the solution to the problem being the most likely logical error
weighted by probability of its parts, instead of just the one with
the fewest parts.
Returns:
A string corresponding to the contents of a WCNF file in the requested
Expand Down
26 changes: 10 additions & 16 deletions glue/python/src/stim/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1531,13 +1531,13 @@ class Circuit:
quantization: int = 100,
format: str = 'WDIMACS',
) -> str:
"""Makes a maxSAT problem of the circuit's distance, that other tools can solve.
"""Makes a maxSAT problem of the circuit's most likely undetectable logical
error, that other tools can solve.
The output is a string describing the maxSAT problem in WDIMACS format
(see https://maxhs.org/docs/wdimacs.html). The optimal solution to the
problem is the fault distance of the circuit (the minimum number of error
mechanisms that combine to flip any logical observable while producing no
detection events).
problem is the highest likelihood set of error mechanisms that combine to
flip any logical observable while producing no detection events).
There are many tools that can solve maxSAT problems in WDIMACS format.
For example, you can download one of the entries in the 2023 maxSAT
Expand All @@ -1551,12 +1551,12 @@ class Circuit:
Args:
format: Defaults to "WDIMACS", corresponding to WDIMACS format which is
described here: http://www.maxhs.org/docs/wdimacs.html
weight_bins: Defaults to 1 (unweighted errors). Error probabilities are
converted to log-odds and scaled/rounded to be positive integers at
most this large. Setting this argument to a larger number results in
the solution to the problem being the most likely logical error
weighted by probability of its parts, instead of just the one with
the fewest parts.
quantization: Defaults to 10. Error probabilities are converted to log-odds
and scaled/rounded to be positive integers at most this large. Setting
this argument to a larger number results in more accurate quantization
such that the returned error set should have a likelihood closer to the
true most likely solution. This comes at the cost of making some maxSAT
solvers slower.
Returns:
A string corresponding to the contents of a WCNF file in the requested
Expand Down Expand Up @@ -1864,12 +1864,6 @@ class Circuit:
Args:
format: Defaults to "WDIMACS", corresponding to WDIMACS format which is
described here: http://www.maxhs.org/docs/wdimacs.html
weight_bins: Defaults to 1 (unweighted errors). Error probabilities are
converted to log-odds and scaled/rounded to be positive integers at
most this large. Setting this argument to a larger number results in
the solution to the problem being the most likely logical error
weighted by probability of its parts, instead of just the one with
the fewest parts.
Returns:
A string corresponding to the contents of a WCNF file in the requested
Expand Down
92 changes: 10 additions & 82 deletions src/stim/circuit/circuit.pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2003,12 +2003,6 @@ void stim_pybind::pybind_circuit_methods(pybind11::module &, pybind11::class_<Ci
Args:
format: Defaults to "WDIMACS", corresponding to WDIMACS format which is
described here: http://www.maxhs.org/docs/wdimacs.html
weight_bins: Defaults to 1 (unweighted errors). Error probabilities are
converted to log-odds and scaled/rounded to be positive integers at
most this large. Setting this argument to a larger number results in
the solution to the problem being the most likely logical error
weighted by probability of its parts, instead of just the one with
the fewest parts.
Returns:
A string corresponding to the contents of a WCNF file in the requested
Expand Down Expand Up @@ -2051,13 +2045,13 @@ void stim_pybind::pybind_circuit_methods(pybind11::module &, pybind11::class_<Ci
pybind11::arg("quantization") = 100,
pybind11::arg("format") = "WDIMACS",
clean_doc_string(R"DOC(
Makes a maxSAT problem of the circuit's distance, that other tools can solve.
Makes a maxSAT problem of the circuit's most likely undetectable logical
error, that other tools can solve.
The output is a string describing the maxSAT problem in WDIMACS format
(see https://maxhs.org/docs/wdimacs.html). The optimal solution to the
problem is the fault distance of the circuit (the minimum number of error
mechanisms that combine to flip any logical observable while producing no
detection events).
problem is the highest likelihood set of error mechanisms that combine to
flip any logical observable while producing no detection events).
There are many tools that can solve maxSAT problems in WDIMACS format.
For example, you can download one of the entries in the 2023 maxSAT
Expand All @@ -2071,12 +2065,12 @@ void stim_pybind::pybind_circuit_methods(pybind11::module &, pybind11::class_<Ci
Args:
format: Defaults to "WDIMACS", corresponding to WDIMACS format which is
described here: http://www.maxhs.org/docs/wdimacs.html
weight_bins: Defaults to 1 (unweighted errors). Error probabilities are
converted to log-odds and scaled/rounded to be positive integers at
most this large. Setting this argument to a larger number results in
the solution to the problem being the most likely logical error
weighted by probability of its parts, instead of just the one with
the fewest parts.
quantization: Defaults to 10. Error probabilities are converted to log-odds
and scaled/rounded to be positive integers at most this large. Setting
this argument to a larger number results in more accurate quantization
such that the returned error set should have a likelihood closer to the
true most likely solution. This comes at the cost of making some maxSAT
solvers slower.
Returns:
A string corresponding to the contents of a WCNF file in the requested
Expand Down Expand Up @@ -2112,72 +2106,6 @@ void stim_pybind::pybind_circuit_methods(pybind11::module &, pybind11::class_<Ci
)DOC")
.data());

// c.def(
// "shortest_error_problem_as_wcnf_file",
// &py_shortest_error_problem_as_wcnf_file,
// pybind11::kw_only(),
// pybind11::arg("weighted") = false,
// pybind11::arg("weight_scale_factor") = 0,
// clean_doc_string(R"DOC(
// Makes a maxSAT problem of the circuit's distance, that other tools can solve.

// The output is a string describing the maxSAT problem in WDIMACS format
// (see https://maxhs.org/docs/wdimacs.html). The optimal solution to the
// problem is the fault distance of the circuit (the minimum number of error
// mechanisms that combine to flip any logical observable while producing no
// detection events).

// There are many tools that can solve maxSAT problems in WDIMACS format.
// For example, you can download one of the entries in the 2023 maxSAT
// competition (see https://maxsat-evaluations.github.io/2023), for example
// CASHWMaxSAT-CorePlus.zip and run it on your problem by running these
// BASH terminal commands:

// unzip CASHWMaxSAT-CorePlus.zip
// ./CASHWMaxSAT-CorePlus/bin/cashwmaxsatcoreplus -bm -m your_problem.wcnf

// Args:
// format: Defaults to "WDIMACS", corresponding to WDIMACS format which is
// described here: http://www.maxhs.org/docs/wdimacs.html
// weight_bins: Defaults to 1 (unweighted errors). Error probabilities are
// converted to log-odds and scaled/rounded to be positive integers at
// most this large. Setting this argument to a larger number results in
// the solution to the problem being the most likely logical error
// weighted by probability of its parts, instead of just the one with
// the fewest parts.

// Returns:
// A string corresponding to the contents of a WCNF file in the requested
// format.

// Examples:
// >>> import stim
// >>> circuit = stim.Circuit("""
// ... X_ERROR(0.1) 0
// ... M 0
// ... OBSERVABLE_INCLUDE(0) rec[-1]
// ... X_ERROR(0.4) 0
// ... M 0
// ... DETECTOR rec[-1] rec[-2]
// ... """)
// >>> print(circuit.shortest_error_problem_as_wcnf_file(), end='')
// p wcnf 2 4 5
// 1 -1 0
// 1 -2 0
// 5 -1 0
// 5 2 0
// >>> print(circuit.shortest_error_problem_as_wcnf_file(
// ... weighted=True,
// ... weight_scale_factor=1000
// ... ), end='')
// p wcnf 2 4 4001
// 185 -1 0
// 1000 -2 0
// 4001 -1 0
// 4001 2 0
// )DOC")
// .data());

c.def(
"explain_detector_error_model_errors",
[](const Circuit &self,
Expand Down
4 changes: 2 additions & 2 deletions src/stim/search/sat/wcnf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ std::string sat_problem_as_wcnf_string(const DetectorErrorModel& model, bool wei

size_t error_index = 0;
model.iter_flatten_error_instructions([&](const DemInstruction& e) {
if (e.arg_data[0] != 0) {
if (!weighted or e.arg_data[0] != 0) {
BoolRef err_x = errors_activated[error_index];
// Add parity contribution to the detectors and observables
for (const auto& t : e.target_data) {
Expand Down Expand Up @@ -232,7 +232,7 @@ std::string sat_problem_as_wcnf_string(const DetectorErrorModel& model, bool wei
// For unweighted search the error should be soft clause should be that
// the error is inactive.
clause.add_var(~err_x);
clause.weight = -std::log(p / (1 - p));
clause.weight = 1.0;
instance.add_clause(clause);
}
}
Expand Down
Loading

0 comments on commit 0a4fc3e

Please sign in to comment.