Skip to content

Return neighbors in a deterministic order#1638

Merged
IvanIsCoding merged 2 commits into
Qiskit:mainfrom
juan52878911:fix/neighbors-deterministic-order-1501
Jul 15, 2026
Merged

Return neighbors in a deterministic order#1638
IvanIsCoding merged 2 commits into
Qiskit:mainfrom
juan52878911:fix/neighbors-deterministic-order-1501

Conversation

@juan52878911

@juan52878911 juan52878911 commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Summary

PyGraph.neighbors, PyDiGraph.neighbors and PyDiGraph.neighbors_undirected returned neighbor node indices in a non-deterministic order that could change between runs. This makes them return a stable, deterministic order while keeping their multigraph de-duplication behaviour.

Fixes #1501.

Root cause

All three methods deduplicated neighbors by collecting into a HashSet<usize> and then draining it:

.map(|node| node.index())
.collect::<HashSet<usize>>()
.drain()
.collect(),

HashSet's iteration order is randomized, so the order of the returned indices was arbitrary and unstable — it could differ between runs and even between successive calls on the same graph.

Fix

We swapped HashSet with IndexSet

Tests

Added regression tests in tests/graph/test_neighbors.py and tests/digraph/test_neighbors.py that assert a stable order across repeated calls, the exact expected order, and correct de-duplication of parallel edges.

Verified locally against a fresh build (rustworkx main): the new tests pass, the full tests/graph (820) and tests/digraph (918) suites pass, and cargo fmt/cargo clippy -- -D warnings are clean.

PyGraph.neighbors, PyDiGraph.neighbors and PyDiGraph.neighbors_undirected
deduplicated their results through a HashSet and returned them via
`drain()`. The HashSet iteration order is randomized, so the order of the
returned node indices could change between runs and even between calls.

Deduplicate while preserving the graph's edge iteration order by filtering
through a `seen` set instead of draining a HashSet. This keeps the
multigraph deduplication behaviour but makes the ordering stable.

Also update the now-deterministic neighbors_undirected docstring example
and add regression tests asserting a stable order and correct dedup.

Fixes Qiskit#1501
@CLAassistant

CLAassistant commented Jul 15, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@IvanIsCoding IvanIsCoding enabled auto-merge July 15, 2026 16:26
@IvanIsCoding IvanIsCoding added this pull request to the merge queue Jul 15, 2026
@mergify

mergify Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

Merged via the queue into Qiskit:main with commit 5ff5a9f Jul 15, 2026
49 of 139 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.

Randomized iteration order for PyGraph.neighbors

3 participants