Skip to content

Conversation

@jberg5
Copy link
Contributor

@jberg5 jberg5 commented Jan 31, 2026

Addresses #1545.

max_weight_matching was nondeterministic for some graphs with multiple valid solutions. See the tests I added, or the linked issue, for some examples of this.

I evaluated two choices:

  • calling sort_unstable() on best_edge_to.values()
  • using a BTreeMap instead of a HashMap

I did some benchmarking on various problem sizes and found that sorting the values of a HashMap caused a roughly 19% slowdown overall, especially for smaller problems. BTreeMap performance though looks pretty much identical modulo some noise:

image

I think determinism is a really nice property to have for a lot of different applications, and if we can achieve it without a performance penalty, it's worth it.

AI disclaimer:
I had claude come up with a script to find small examples of nondeterministic graphs; the examples in the tests come from there. (The example that I originally detected nondeterminism with had 60 nodes and thousands of edges and would have made a poor test case!)

I also had claude write the benchmarking script I used. I'd be happy to share it if there's interest. Nothing fancy; it just came up with some random graphs with different numbers of nodes and edges, and used matplotlib to generate a visualization.

@CLAassistant
Copy link

CLAassistant commented Jan 31, 2026

CLA assistant check
All committers have signed the CLA.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link
Collaborator

@IvanIsCoding IvanIsCoding left a comment

Choose a reason for hiding this comment

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

Overall I think determinism is welcome. But I don't think BTreeMap is the solution I want merged.

Another exercise before merging is revisiting other points of the algorithm to check if they are deterministic. Matthew wrote this ages ago. At the time we hadn't gotten the feedback from users about determinism. From time to time we still get that feedback (#1501).

@jberg5
Copy link
Contributor Author

jberg5 commented Jan 31, 2026

Thanks for the feedback and suggestions @IvanIsCoding ! Nothing else stands out to me as a source of additional nondeterminism. Just to be sure, I've been running the script I used to come up with the nondeterministic example graphs I added in the tests, and ever since switching to IndexMap it has run through 15 million random graphs so far without finding any further examples.

@coveralls
Copy link

coveralls commented Jan 31, 2026

Pull Request Test Coverage Report for Build 21552760185

Details

  • 1 of 1 (100.0%) changed or added relevant line in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 94.167%

Totals Coverage Status
Change from base Build 21503381725: 0.0%
Covered Lines: 18388
Relevant Lines: 19527

💛 - Coveralls

@IvanIsCoding IvanIsCoding added this pull request to the merge queue Feb 1, 2026
Merged via the queue into Qiskit:main with commit 1227db6 Feb 1, 2026
37 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.

4 participants