Skip to content

Use a single type alias for indexmap and indexset to enforce hasher#16130

Merged
raynelfss merged 2 commits into
Qiskit:mainfrom
mtreinish:common-indexmap
May 29, 2026
Merged

Use a single type alias for indexmap and indexset to enforce hasher#16130
raynelfss merged 2 commits into
Qiskit:mainfrom
mtreinish:common-indexmap

Conversation

@mtreinish
Copy link
Copy Markdown
Member

@mtreinish mtreinish commented May 4, 2026

Building off of #15920 which moved our hasher for IndexMap usage to be
foldhash this moves to having a common type alias for IndexMap and
IndexSet that has the hasher set. All the internal usage of IndexMap and
IndexSet are updated to use this new alias instead which removes the
boiler plate around dealing with initializing the hasher for the most
part, the only exception is when using with_capacity_and_hasher()
there is no method to create an empty object with a capacity
preallocated and the hasher is initialized from the generic type. To
enforce that we're using the correct hasher for all indexmap usage the
clippy rules are updated to disallow the indexmap::IndexMap and
indexmap::IndexSet types directly. This means that after this commit all
indexmap usage with be forced by clippy to leverage our standard pattern
using foldhash. The advantage is if we ever need to change the default
hasher again in the future we only need to update one central place
(and the places using with_capacity_and_hasher).

This PR is based on top of #15920 and will need to be rebased after that is merged. In the meantime you can view the contents of just this PR by looking at the PR branch's HEAD commit: 12f7ed5 This has been rebased.

AI/LLM disclosure

  • I didn't use LLM tooling, or only used it privately.
  • I used the following tool to help write this PR description:
  • I used the following tool to generate or modify code:

@mtreinish mtreinish added on hold Can not fix yet Changelog: None Do not include in the GitHub Release changelog. Rust This PR or issue is related to Rust code in the repository labels May 4, 2026
@qiskit-bot
Copy link
Copy Markdown
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core
  • @mtreinish

@mtreinish mtreinish requested a review from a team as a code owner May 4, 2026 16:06
@mtreinish mtreinish requested a review from alexanderivrii May 4, 2026 16:06
@coveralls
Copy link
Copy Markdown

coveralls commented May 4, 2026

Coverage Report for CI Build 26658117959

Coverage decreased (-0.003%) to 87.556%

Details

  • Coverage decreased (-0.003%) from the base build.
  • Patch coverage: 9 uncovered changes across 6 files (152 of 161 lines covered, 94.41%).
  • 9 coverage regressions across 2 files.

Uncovered Changes

File Changed Covered %
crates/bindgen-cli/src/lint.rs 3 0 0.0%
crates/transpiler/src/equivalence.rs 15 13 86.67%
crates/bindgen-cli/src/abi.rs 1 0 0.0%
crates/synthesis/src/evolution/suzuki_trotter.rs 1 0 0.0%
crates/transpiler/src/passes/basis_translator/compose_transforms.rs 15 14 93.33%
crates/transpiler/src/passes/commutation_analysis.rs 48 47 97.92%
Total (29 files) 161 152 94.41%

Coverage Regressions

9 previously-covered lines in 2 files lost coverage.

File Lines Losing Coverage Coverage
crates/qasm2/src/parse.rs 6 96.68%
crates/qasm2/src/lex.rs 3 92.03%

Coverage Stats

Coverage Status
Relevant Lines: 123920
Covered Lines: 108500
Line Coverage: 87.56%
Coverage Strength: 963063.52 hits per line

💛 - Coveralls

Building off of Qiskit#15920 which moved our hasher for IndexMap usage to be
foldhash this moves to having a common type alias for IndexMap and
IndexSet that has the hasher set. All the internal usage of IndexMap and
IndexSet are updated to use this new alias instead which removes the
boiler plate around dealing with initializing the hasher for the most
part, the only exception is when using `with_capacity_and_hasher()`
there is no method to create an empty object with a capacity
preallocated and the hasher is initialized from the generic type. To
enforce that we're using the correct hasher for all indexmap usage the
clippy rules are updated to disallow the indexmap::IndexMap and
indexmap::IndexSet types directly. This means that after this commit all
indexmap usage with be forced by clippy to leverage our standard pattern
using foldhash. The advantage is if we ever need to change the default
hasher again in the future we only need to update one central place
(and the places using with_capacity_and_hasher).
@mtreinish mtreinish removed the on hold Can not fix yet label May 21, 2026
@mtreinish
Copy link
Copy Markdown
Member Author

This has been rebased on main now that #15920 has merged and is ready for review.

@mtreinish mtreinish requested a review from raynelfss May 21, 2026 21:03
@mtreinish mtreinish added this to the 2.5.0 milestone May 26, 2026
Copy link
Copy Markdown
Contributor

@raynelfss raynelfss left a comment

Choose a reason for hiding this comment

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

LGTM. I can confirm there's no usage of Index{Map, Set}::new() (in favor of Index{Map, Set}::default()), no imports of indexmap::{IndexMap, IndexSet}, and no usage of them as they are now marked as disallowed.

I will have to keep in mind to always use qiskit_utils::{IndexMap, IndexSet} from now on but it at least guarantees that in the future, if we decide to use a different hasher, the changes are not as intrusive. 🚀

@raynelfss raynelfss enabled auto-merge May 29, 2026 20:13
@raynelfss raynelfss added this pull request to the merge queue May 29, 2026
Merged via the queue into Qiskit:main with commit 6da9a76 May 29, 2026
27 checks passed
@github-project-automation github-project-automation Bot moved this from Ready to Done in Qiskit 2.5 May 29, 2026
@mtreinish mtreinish deleted the common-indexmap branch May 30, 2026 00:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changelog: None Do not include in the GitHub Release changelog. Rust This PR or issue is related to Rust code in the repository

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants