Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/pymatching/matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -1678,6 +1678,7 @@ def to_networkx(self) -> nx.Graph:
has_virtual_boundary = True
else:
graph.add_edge(u, v, **data)
graph.add_nodes_from(range(num_nodes))
boundary = self.boundary
for i in graph.nodes:
is_boundary = i in boundary
Expand Down
15 changes: 15 additions & 0 deletions tests/matching/output_graph_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,18 @@ def test_self_loop_to_networkx():
assert list(g.edges(data=True)) == [(0, 0, {'fault_ids': set(), 'weight': 3.0, 'error_probability': -1.0}),
(1, 1, {'fault_ids': set(), 'weight': 2.0, 'error_probability': -1.0}),
(1, 2, {'fault_ids': set(), 'weight': 1.0, 'error_probability': -1.0})]


def test_networkx_roundtrip_with_isolated_nodes():
graph = nx.Graph()
graph.add_nodes_from(range(4))
graph.add_edge(0, 1)
graph.nodes[3]['is_boundary'] = True
original = Matching(graph)
exported = original.to_networkx()
assert set(nx.isolates(exported)) == {2, 3}
assert exported.nodes[2]['is_boundary'] is False
assert exported.nodes[3]['is_boundary'] is True
restored = Matching(exported)
assert restored.num_detectors == original.num_detectors
assert restored.boundary == original.boundary