forked from oscarhiggott/PyMatching
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request oscarhiggott#23 from oscarhiggott/negative-weights
Support negative edge weights
- Loading branch information
Showing
8 changed files
with
146 additions
and
35 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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 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 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 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,55 @@ | ||
import pytest | ||
import numpy as np | ||
import networkx as nx | ||
|
||
from pymatching import Matching | ||
|
||
|
||
@pytest.mark.parametrize("nn", (None, 30)) | ||
def test_negative_weight_repetition_code(nn): | ||
m = Matching() | ||
m.add_edge(0, 1, 0, -1) | ||
m.add_edge(1, 2, 1, -1) | ||
m.add_edge(2, 3, 2, -1) | ||
m.add_edge(3, 4, 3, -1) | ||
m.add_edge(4, 5, 4, -1) | ||
m.add_edge(5, 0, 5, -1) | ||
c, w = m.decode([0, 1, 1, 0, 0, 0], return_weight=True, num_neighbours=nn) | ||
assert np.array_equal(c, np.array([1, 0, 1, 1, 1, 1])) | ||
assert w == -5 | ||
|
||
|
||
@pytest.mark.parametrize("nn", (None, 30)) | ||
def test_isolated_negative_weight(nn): | ||
m = Matching() | ||
m.add_edge(0, 1, 0, 1) | ||
m.add_edge(1, 2, 1, -10) | ||
m.add_edge(2, 3, 2, 1) | ||
m.add_edge(3, 0, 3, 1) | ||
c, w = m.decode([0, 1, 1, 0], return_weight=True, num_neighbours=nn) | ||
assert np.array_equal(c, np.array([0, 1, 0, 0])) | ||
assert w == -10 | ||
|
||
|
||
@pytest.mark.parametrize("nn", (None, 30)) | ||
def test_negative_and_positive_in_matching(nn): | ||
g = nx.Graph() | ||
g.add_edge(0, 1, fault_ids=0, weight=1) | ||
g.add_edge(1, 2, fault_ids=1, weight=-10) | ||
g.add_edge(2, 3, fault_ids=2, weight=1) | ||
g.add_edge(3, 0, fault_ids=3, weight=1) | ||
m = Matching(g) | ||
c, w = m.decode([0, 1, 0, 1], return_weight=True, num_neighbours=nn) | ||
assert np.array_equal(c, np.array([0, 1, 1, 0])) | ||
assert w == -9 | ||
|
||
|
||
def test_negative_weight_edge_returned(): | ||
m = Matching() | ||
m.add_edge(0, 1, weight=0.5, error_probability=0.3) | ||
m.add_edge(1, 2, weight=0.5, error_probability=0.3, fault_ids=0) | ||
m.add_edge(2, 3, weight=-0.5, error_probability=0.7, fault_ids={1, 2}) | ||
expected = [(0, 1, {'fault_ids': set(), 'weight': 0.5, 'error_probability': 0.3}), | ||
(1, 2, {'fault_ids': {0}, 'weight': 0.5, 'error_probability': 0.3}), | ||
(2, 3, {'fault_ids': {1, 2}, 'weight': -0.5, 'error_probability': 0.7})] | ||
assert m.edges() == expected |