From bc414b6ed111718e6af7a10a3cdbb6f48818fc46 Mon Sep 17 00:00:00 2001 From: Noah Shutty Date: Wed, 24 Sep 2025 20:10:46 -0700 Subject: [PATCH] Fix correlated matching boundary component handling --- .../sparse_blossom/driver/user_graph.h | 8 ++++++-- tests/matching/decode_test.py | 20 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/pymatching/sparse_blossom/driver/user_graph.h b/src/pymatching/sparse_blossom/driver/user_graph.h index ab945cb4b..d1bf67a32 100644 --- a/src/pymatching/sparse_blossom/driver/user_graph.h +++ b/src/pymatching/sparse_blossom/driver/user_graph.h @@ -321,8 +321,12 @@ void iter_dem_instructions_include_correlations( "Encountered a decomposed error instruction with an undetectable component (0 detectors). " "This is not supported."); } else if (num_component_detectors > 0) { - // If the previous error in the decomposition had 1 or 2 detectors, we handle it - handle_dem_error(p, {component->node1, component->node2}, component->observable_indices); + // If the previous error in the decomposition had 1 or 2 detectors, we handle it. + if (component->node2 == SIZE_MAX) { + handle_dem_error(p, {component->node1}, component->observable_indices); + } else { + handle_dem_error(p, {component->node1, component->node2}, component->observable_indices); + } decomposed_err.components.push_back({}); component = &decomposed_err.components.back(); component->node1 = SIZE_MAX; diff --git a/tests/matching/decode_test.py b/tests/matching/decode_test.py index 6f37fe56c..3e2bcd513 100644 --- a/tests/matching/decode_test.py +++ b/tests/matching/decode_test.py @@ -400,6 +400,26 @@ def test_decode_to_edges_with_correlations(): assert np.array_equal(edges, expected_edges) +def test_correlated_matching_handles_single_detector_components(): + stim = pytest.importorskip("stim") + p = 0.1 + circuit = stim.Circuit.generated( + code_task="surface_code:rotated_memory_x", + distance=5, + rounds=5, + before_round_data_depolarization=p, + ) + circ_str = str(circuit).replace( + f"DEPOLARIZE1({p})", f"PAULI_CHANNEL_1(0, {p}, 0)" + ) + noisy_circuit = stim.Circuit(circ_str) + dem = noisy_circuit.detector_error_model( + decompose_errors=True, approximate_disjoint_errors=True + ) + m = Matching.from_detector_error_model(dem, enable_correlations=True) + assert m.num_detectors > 0 + + def test_load_from_circuit_with_correlations(): stim = pytest.importorskip("stim") circuit = stim.Circuit.generated(