Skip to content

Calculate implied edge weights from joint probabilities#129

Merged
smadhuk merged 59 commits into
correlated-matchingfrom
u/smadhuk/calculate-edge-weights
Aug 6, 2025
Merged

Calculate implied edge weights from joint probabilities#129
smadhuk merged 59 commits into
correlated-matchingfrom
u/smadhuk/calculate-edge-weights

Conversation

@smadhuk
Copy link
Copy Markdown
Collaborator

@smadhuk smadhuk commented Jul 10, 2025

Add in edge weight calculation when creating a user_graph with correlations enabled. Populating edges with their implied weight is a TODO.

-- Performance before this PR --

[.................*<<|....................] 530 us (vs 290 us) (2.8 Mdets/s) (9.5 Mlayers/s) (1.9 Mshots/s) Decode_surface_r5_d5_p1000
[................*<<<|....................]  23 ms (vs  10 ms) (920 kdets/s) ( 59 klayers/s) (5.3 kshots/s) Decode_surface_r11_d11_p100
[................*<<<|....................] 3.3 ms (vs 1.5 ms) (2.9 Mdets/s) (1.6 Mlayers/s) (150 kshots/s) Decode_surface_r11_d11_p1000
[..................*<|....................] 140 us (vs  83 us) (3.6 Mdets/s) ( 19 Mlayers/s) (1.7 Mshots/s) Decode_surface_r11_d11_p10000
[..................*<|....................]  57 us (vs  33 us) (3.3 Mdets/s) (190 Mlayers/s) ( 17 Mshots/s) Decode_surface_r11_d11_p100000
[.................*<<|....................]  16 ms (vs 7.5 ms) (630 kdets/s) ( 10 klayers/s) (490 shots/s) Decode_surface_r21_d21_p100
[................*<<<|....................]  20 ms (vs 7.8 ms) (490 kdets/s) (8.0 klayers/s) (380 shots/s) Decode_surface_r21_d21_p100_with_dijkstra
[................*<<<|....................]  22 ms (vs 8.2 ms) (460 kdets/s) (7.5 klayers/s) (360 shots/s) Decode_surface_r21_d21_p100_to_edges
[.................*<<|....................]  12 ms (vs 6.3 ms) (2.8 Mdets/s) (410 klayers/s) ( 19 kshots/s) Decode_surface_r21_d21_p1000
[...............*<<<<|....................]  23 ms (vs 7.7 ms) (1.5 Mdets/s) (220 klayers/s) ( 10 kshots/s) Decode_surface_r21_d21_p1000_with_dijkstra
[...............*<<<<|....................]  25 ms (vs 8.4 ms) (1.4 Mdets/s) (210 klayers/s) ( 10 kshots/s) Decode_surface_r21_d21_p1000_to_edges
[................*<<<|....................] 2.3 ms (vs 980 us) (3.3 Mdets/s) (4.6 Mlayers/s) (220 kshots/s) Decode_surface_r21_d21_p10000
[...............*<<<<|....................] 4.1 ms (vs 1.3 ms) (1.8 Mdets/s) (2.5 Mlayers/s) (120 kshots/s) Decode_surface_r21_d21_p10000_with_dijkstra
[...............*<<<<|....................] 4.5 ms (vs 1.4 ms) (1.6 Mdets/s) (2.3 Mlayers/s) (110 kshots/s) Decode_surface_r21_d21_p10000_to_edges
[.................*<<|....................] 180 us (vs  94 us) (4.0 Mdets/s) ( 59 Mlayers/s) (2.8 Mshots/s) Decode_surface_r21_d21_p100000
[...............*<<<<|....................] 360 us (vs 130 us) (1.9 Mdets/s) ( 29 Mlayers/s) (1.3 Mshots/s) Decode_surface_r21_d21_p100000_with_dijkstra
[...............*<<<<|....................] 400 us (vs 130 us) (1.7 Mdets/s) ( 26 Mlayers/s) (1.2 Mshots/s) Decode_surface_r21_d21_p100000_to_edges
[..................*<|....................]  59 ms (vs  35 ms) (160 loads/s) Load_dem_r11_d11_p100
[..................*<|....................] 430 ms (vs 280 ms) ( 23 loads/s) Load_dem_r21_d21_p100
[...................*|....................]  45 ms (vs  35 ms) (210 loads/s) Load_dem_r11_d11_p100_correlations
[...................*|....................] 370 ms (vs 280 ms) ( 26 loads/s) Load_dem_r21_d21_p100_correlations
[................*<<<|....................] 9.5 ms (vs 3.5 ms) (1.0 Gcalls/s) Varying32_get_distance_at_time
[..............*<<<<<|....................]  25 ms (vs 5.9 ms) (390 Mcalls/s) Varying64_get_distance_at_time
[....................*....................] 5.0 us (vs 4.9 us) (190 MEnqueueDequeues/s) bucket_queue_sort
[..................*<|....................] 140 us (vs  99 us) ( 67 MEnqueueDequeues/s) bucket_queue_stream

-- Performance after this PR --

[.................*<<|....................] 530 us (vs 290 us) (2.8 Mdets/s) (9.5 Mlayers/s) (1.9 Mshots/s) Decode_surface_r5_d5_p1000
[................*<<<|....................]  24 ms (vs  10 ms) (900 kdets/s) ( 58 klayers/s) (5.2 kshots/s) Decode_surface_r11_d11_p100
[................*<<<|....................] 3.5 ms (vs 1.5 ms) (2.8 Mdets/s) (1.5 Mlayers/s) (140 kshots/s) Decode_surface_r11_d11_p1000
[..................*<|....................] 140 us (vs  83 us) (3.6 Mdets/s) ( 19 Mlayers/s) (1.8 Mshots/s) Decode_surface_r11_d11_p10000
[..................*<|....................]  56 us (vs  33 us) (3.3 Mdets/s) (190 Mlayers/s) ( 18 Mshots/s) Decode_surface_r11_d11_p100000
[.................*<<|....................]  16 ms (vs 7.5 ms) (620 kdets/s) ( 10 klayers/s) (480 shots/s) Decode_surface_r21_d21_p100
[................*<<<|....................]  21 ms (vs 7.8 ms) (460 kdets/s) (7.6 klayers/s) (360 shots/s) Decode_surface_r21_d21_p100_with_dijkstra
[................*<<<|....................]  22 ms (vs 8.2 ms) (450 kdets/s) (7.4 klayers/s) (350 shots/s) Decode_surface_r21_d21_p100_to_edges
[.................*<<|....................]  13 ms (vs 6.3 ms) (2.8 Mdets/s) (410 klayers/s) ( 19 kshots/s) Decode_surface_r21_d21_p1000
[...............*<<<<|....................]  26 ms (vs 7.7 ms) (1.3 Mdets/s) (200 klayers/s) (9.7 kshots/s) Decode_surface_r21_d21_p1000_with_dijkstra
[................*<<<|....................]  23 ms (vs 8.4 ms) (1.5 Mdets/s) (230 klayers/s) ( 11 kshots/s) Decode_surface_r21_d21_p1000_to_edges
[................*<<<|....................] 2.2 ms (vs 980 us) (3.4 Mdets/s) (4.7 Mlayers/s) (220 kshots/s) Decode_surface_r21_d21_p10000
[...............*<<<<|....................] 4.1 ms (vs 1.3 ms) (1.8 Mdets/s) (2.5 Mlayers/s) (120 kshots/s) Decode_surface_r21_d21_p10000_with_dijkstra
[...............*<<<<|....................] 4.1 ms (vs 1.4 ms) (1.8 Mdets/s) (2.5 Mlayers/s) (120 kshots/s) Decode_surface_r21_d21_p10000_to_edges
[.................*<<|....................] 170 us (vs  94 us) (4.1 Mdets/s) ( 61 Mlayers/s) (2.9 Mshots/s) Decode_surface_r21_d21_p100000
[................*<<<|....................] 320 us (vs 130 us) (2.2 Mdets/s) ( 33 Mlayers/s) (1.5 Mshots/s) Decode_surface_r21_d21_p100000_with_dijkstra
[................*<<<|....................] 330 us (vs 130 us) (2.1 Mdets/s) ( 32 Mlayers/s) (1.5 Mshots/s) Decode_surface_r21_d21_p100000_to_edges
[..................*<|....................]  53 ms (vs  35 ms) (180 loads/s) Load_dem_r11_d11_p100
[..................*<|....................] 400 ms (vs 280 ms) ( 24 loads/s) Load_dem_r21_d21_p100
[....................|*...................]  25 ms (vs  35 ms) (390 loads/s) Load_dem_r11_d11_p100_correlations
[....................|>*..................] 180 ms (vs 280 ms) ( 54 loads/s) Load_dem_r21_d21_p100_correlations
[................*<<<|....................] 9.6 ms (vs 3.5 ms) (1.0 Gcalls/s) Varying32_get_distance_at_time
[..............*<<<<<|....................]  25 ms (vs 5.9 ms) (390 Mcalls/s) Varying64_get_distance_at_time
[....................*....................] 4.8 us (vs 4.9 us) (200 MEnqueueDequeues/s) bucket_queue_sort
[..................*<|....................] 150 us (vs  99 us) ( 63 MEnqueueDequeues/s) bucket_queue_stream

@smadhuk smadhuk changed the base branch from u/smadhuk/inject-correlations to correlated-matching July 10, 2025 00:28
@smadhuk smadhuk requested a review from oscarhiggott July 10, 2025 23:13
Copy link
Copy Markdown
Owner

@oscarhiggott oscarhiggott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly looks good, however the weights should be kept as floats while they are in the UserGraph and only converted to ints after they are rescaled during the conversion to a MatchingGraph

Comment on lines +442 to +444
pm::weight_int pm::convert_probability_to_weight(double p) {
return std::log((1 - p) / p);
}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The weights should just be stored as floats, not ints, at this stage. The weights need to be rescaled before being turned into integers, but this is only done when the UserGraph is converted to the MatchingGraph

Copy link
Copy Markdown
Collaborator Author

@smadhuk smadhuk Aug 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed (this code was actually removed) by changes from an upstream PR

Comment on lines +451 to +462

TEST(ConvertProbabilityToWeight, PositiveResultIsTruncated) {
const double p = 0.1;
const pm::weight_int expected_weight = 2; // 2.197... is truncated to 2
EXPECT_EQ(pm::convert_probability_to_weight(p), expected_weight);
}

TEST(ConvertProbabilityToWeight, SmallPositiveResultTruncatesToZero) {
const double p = 0.4;
const pm::weight_int expected_weight = 0;
EXPECT_EQ(pm::convert_probability_to_weight(p), expected_weight);
}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something we don't want to happen (should rescale before converting to int)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed (this code was actually removed) by changes from an upstream PR

struct ImpliedWeightUnconverted {
size_t node1;
size_t node2;
weight_int new_weight;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be:

double new_weight;

instead of

weight_int new_weight;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed by changes from an upstream PR

smadhuk and others added 7 commits August 4, 2025 10:33
Co-authored-by: oscarhiggott <29460323+oscarhiggott@users.noreply.github.com>
Co-authored-by: oscarhiggott <29460323+oscarhiggott@users.noreply.github.com>
Co-authored-by: oscarhiggott <29460323+oscarhiggott@users.noreply.github.com>
Co-authored-by: oscarhiggott <29460323+oscarhiggott@users.noreply.github.com>
Co-authored-by: oscarhiggott <29460323+oscarhiggott@users.noreply.github.com>
Co-authored-by: oscarhiggott <29460323+oscarhiggott@users.noreply.github.com>
smadhuk and others added 29 commits August 4, 2025 16:47
Co-authored-by: oscarhiggott <29460323+oscarhiggott@users.noreply.github.com>
Co-authored-by: oscarhiggott <29460323+oscarhiggott@users.noreply.github.com>
Co-authored-by: oscarhiggott <29460323+oscarhiggott@users.noreply.github.com>
…ings

Update pybindings to handle correlations
…logic

Fix search graph logic to not depend on the implied weights inside the graph flooder.
Pipe reweighting logic all the way to the Pymatching binary CLI
…ethod

Implement reweight logic for the search and matching graphs
…ghts

Finish ImpliedWeight conversion (with pointers to weights to be reweighted).
Co-authored-by: oscarhiggott <29460323+oscarhiggott@users.noreply.github.com>
Co-authored-by: oscarhiggott <29460323+oscarhiggott@users.noreply.github.com>
Co-authored-by: oscarhiggott <29460323+oscarhiggott@users.noreply.github.com>
…ts-to-matching-graph

Populate unconverted implied weights during matching graph creation
…edges

Populate implied edge weights in edges in the User Graph
…ts-in-matching-graph

Only discretize weights when converting to MatchingGraphs
…d_weight_validity

Check implied weight validity when computing normalizing constant
…ts-in-matching-graph

Merge validity check changes into reweighting fixes
Add validity checks and no more discretized edges into u/smadhuk/calculate-edge-weights
…mber

Make previously free edges to implied weights unconverted variable a class member
@smadhuk smadhuk merged commit c5f087b into correlated-matching Aug 6, 2025
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants