Skip to content

Commit

Permalink
add test for weighted search with p > 0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
noajshu committed Feb 28, 2024
1 parent e55ae98 commit b6fc313
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 40 deletions.
45 changes: 34 additions & 11 deletions doc/python_api_reference_vDev.md
Original file line number Diff line number Diff line change
Expand Up @@ -2452,6 +2452,15 @@ def shortest_error_problem_as_wcnf_file(
"""Generates a maxSAT problem instance in WDIMACS format whose optimal value is
the distance of the protocol, i.e. the minimum weight of any set of errors
that forms an undetectable logical error.
FYI: here is how you could fetch a solver from the
[2023 maxSAT competition](https://maxsat-evaluations.github.io/2023/) and
run the solver on a file produced with this method:
```
# first download nzip CASHWMaxSAT-CorePlus.zip from
# https://maxsat-evaluations.github.io/2023
unzip CASHWMaxSAT-CorePlus.zip
time ./CASHWMaxSAT-CorePlus/bin/cashwmaxsatcoreplus -bm -m problem.wcnf
```
Args:
weighted: Defaults to False (unweighted), so that the problem is to find
Expand All @@ -2473,17 +2482,31 @@ def shortest_error_problem_as_wcnf_file(
Examples:
>>> import stim
>>> circuit = stim.Circuit.generated(
... "surface_code:rotated_memory_x",
... rounds=5,
... distance=5,
... after_clifford_depolarization=0.001)
>>> print(circuit.shortest_undetectable_logical_error_wcnf(
num_distinct_weights=1))
....
>>> print(circuit.shortest_undetectable_logical_error_wcnf(
num_distinct_weights=10))
....
>>> 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())
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,
... num_distinct_weights=10
... ))
p wcnf 2 4 41
2 -1 0
10 -2 0
41 -1 0
41 2 0
"""
```

Expand Down
45 changes: 34 additions & 11 deletions doc/stim.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,15 @@ class Circuit:
"""Generates a maxSAT problem instance in WDIMACS format whose optimal value is
the distance of the protocol, i.e. the minimum weight of any set of errors
that forms an undetectable logical error.
FYI: here is how you could fetch a solver from the
[2023 maxSAT competition](https://maxsat-evaluations.github.io/2023/) and
run the solver on a file produced with this method:
```
# first download nzip CASHWMaxSAT-CorePlus.zip from
# https://maxsat-evaluations.github.io/2023
unzip CASHWMaxSAT-CorePlus.zip
time ./CASHWMaxSAT-CorePlus/bin/cashwmaxsatcoreplus -bm -m problem.wcnf
```
Args:
weighted: Defaults to False (unweighted), so that the problem is to find
Expand All @@ -1804,17 +1813,31 @@ class Circuit:
Examples:
>>> import stim
>>> circuit = stim.Circuit.generated(
... "surface_code:rotated_memory_x",
... rounds=5,
... distance=5,
... after_clifford_depolarization=0.001)
>>> print(circuit.shortest_undetectable_logical_error_wcnf(
num_distinct_weights=1))
....
>>> print(circuit.shortest_undetectable_logical_error_wcnf(
num_distinct_weights=10))
....
>>> 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())
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,
... num_distinct_weights=10
... ))
p wcnf 2 4 41
2 -1 0
10 -2 0
41 -1 0
41 2 0
"""
def shortest_graphlike_error(
self,
Expand Down
45 changes: 34 additions & 11 deletions glue/python/src/stim/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,15 @@ class Circuit:
"""Generates a maxSAT problem instance in WDIMACS format whose optimal value is
the distance of the protocol, i.e. the minimum weight of any set of errors
that forms an undetectable logical error.
FYI: here is how you could fetch a solver from the
[2023 maxSAT competition](https://maxsat-evaluations.github.io/2023/) and
run the solver on a file produced with this method:
```
# first download nzip CASHWMaxSAT-CorePlus.zip from
# https://maxsat-evaluations.github.io/2023
unzip CASHWMaxSAT-CorePlus.zip
time ./CASHWMaxSAT-CorePlus/bin/cashwmaxsatcoreplus -bm -m problem.wcnf
```
Args:
weighted: Defaults to False (unweighted), so that the problem is to find
Expand All @@ -1804,17 +1813,31 @@ class Circuit:
Examples:
>>> import stim
>>> circuit = stim.Circuit.generated(
... "surface_code:rotated_memory_x",
... rounds=5,
... distance=5,
... after_clifford_depolarization=0.001)
>>> print(circuit.shortest_undetectable_logical_error_wcnf(
num_distinct_weights=1))
....
>>> print(circuit.shortest_undetectable_logical_error_wcnf(
num_distinct_weights=10))
....
>>> 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())
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,
... num_distinct_weights=10
... ))
p wcnf 2 4 41
2 -1 0
10 -2 0
41 -1 0
41 2 0
"""
def shortest_graphlike_error(
self,
Expand Down
3 changes: 2 additions & 1 deletion src/stim/circuit/circuit.pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1986,7 +1986,8 @@ void stim_pybind::pybind_circuit_methods(pybind11::module &, pybind11::class_<Ci
[2023 maxSAT competition](https://maxsat-evaluations.github.io/2023/) and
run the solver on a file produced with this method:
```
wget https://maxsat-evaluations.github.io/2023/mse23-solver-src/exact/CASHWMaxSAT-CorePlus.zip
# first download nzip CASHWMaxSAT-CorePlus.zip from
# https://maxsat-evaluations.github.io/2023
unzip CASHWMaxSAT-CorePlus.zip
time ./CASHWMaxSAT-CorePlus/bin/cashwmaxsatcoreplus -bm -m problem.wcnf
```
Expand Down
14 changes: 11 additions & 3 deletions src/stim/circuit/circuit_pybind_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,11 +837,19 @@ def test_shortest_error_problem_as_wcnf_file():
DETECTOR rec[-1] rec[-2]
""")
wcnf_str = c.shortest_error_problem_as_wcnf_file()
print(wcnf_str)
assert wcnf_str == 'p wcnf 2 4 5\n1 -1 0\n1 -2 0\n5 -1 0\n5 2 0\n'
wcnf_str = c.shortest_error_problem_as_wcnf_file(num_distinct_weights=2)
print(wcnf_str)
wcnf_str = c.shortest_error_problem_as_wcnf_file(weighted=True, num_distinct_weights=2)
assert wcnf_str == 'p wcnf 2 4 9\n1 -1 0\n2 -2 0\n9 -1 0\n9 2 0\n'
c = stim.Circuit("""
X_ERROR(0.1) 0
M 0
OBSERVABLE_INCLUDE(0) rec[-1]
X_ERROR(0.7) 0
M 0
DETECTOR rec[-1] rec[-2]
""")
wcnf_str = c.shortest_error_problem_as_wcnf_file(weighted=True)
assert wcnf_str == 'p wcnf 2 4 5\n1 1 0\n1 -2 0\n5 -1 0\n5 2 0\n'

def test_shortest_graphlike_error_ignore():
c = stim.Circuit("""
Expand Down
17 changes: 14 additions & 3 deletions src/stim/search/sat/wcnf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,21 @@ std::string stim::shortest_error_problem_as_wcnf_file(
observables_flipped[t.val()] = instance.Xor(observables_flipped[t.val()], err_x);
}
}
// Add a soft clause for this error to be inactive
// Add a soft clause for this error
Clause clause;
clause.add_var(~err_x);
clause.weight = -std::log(e.arg_data[0] / (1 - e.arg_data[0]));
double p = e.arg_data[0];
if (!weighted or p <= 0.5) {
// For unweighted search or when the error has probability <= 0.5, the
// soft clause should be that the error is inactive.
clause.add_var(~err_x);
clause.weight = -std::log(p / (1 - p));
} else {
// Invert for weighted search when the probability is > 0.5 and invert
// the weight so that the clause decreases the overall cost when the
// error is active.
clause.add_var(err_x);
clause.weight = -std::log((1 - p) / p);
}
instance.add_clause(clause);
}
++error_index;
Expand Down

0 comments on commit b6fc313

Please sign in to comment.