Skip to content

Fix marginal counts for mixed-width hex Counts#16200

Open
snoopuppy582 wants to merge 1 commit into
Qiskit:mainfrom
snoopuppy582:fix/marginal-counts-mixed-width
Open

Fix marginal counts for mixed-width hex Counts#16200
snoopuppy582 wants to merge 1 commit into
Qiskit:mainfrom
snoopuppy582:fix/marginal-counts-mixed-width

Conversation

@snoopuppy582
Copy link
Copy Markdown

@snoopuppy582 snoopuppy582 commented May 17, 2026

Fixes #16190.

Summary

This updates count marginalization to infer the classical-bit width from the longest cleaned count key, then treats shorter keys as left-zero-padded while marginalizing. This fixes Counts objects created from hexadecimal input without memory_slots, where the stored bitstrings can have mixed widths such as "0" and "11".

The same width handling is applied to the accelerated marginal-distribution path so higher-bit indices do not panic on mixed-width keys.

Details and comments

  • marginal_counts(Counts({"0x0": 50, "0x3": 30}), [0]) now returns {"0": 50, "1": 30}.
  • marginal_distribution(..., [1]) now handles the same input instead of panicking in the Rust helper.
  • The no-indices path still only strips spaces/underscores and does not pad keys.

Testing

  • python -X utf8 -m unittest test.python.result.test_counts.TestCounts.test_marginal_counts_mixed_width_hex_without_memory_slots test.python.result.test_counts.TestCounts.test_marginal_distribution_mixed_width_hex_without_memory_slots
  • python -X utf8 -m unittest test.python.result.test_counts
  • python -X utf8 -m unittest test.python.result.test_result.TestResultOperations.test_marginal_counts test.python.result.test_result.TestResultOperations.test_marginal_distribution test.python.result.test_result.TestResultOperations.test_marginal_counts_result test.python.result.test_result.TestResultOperations.test_marginal_counts_with_dict test.python.result.test_result.TestResultOperationsFailed.test_marginal_counts_no_cregs
  • python -X utf8 -m unittest discover -s test/python/result -p 'test_*.py'
  • cargo test -p qiskit-accelerate --lib
  • cargo fmt --check
  • black --check qiskit/result/utils.py test/python/result/test_counts.py
  • ruff check qiskit/result/utils.py test/python/result/test_counts.py
  • reno lint

AI/LLM disclosure

  • I didn't use LLM tooling, or only used it privately.
  • I used OpenAI Codex (GPT-5) to help write this PR description.
  • I used OpenAI Codex (GPT-5) to help investigate the issue and draft code/test changes; I reviewed and verified the final diff locally.

@snoopuppy582 snoopuppy582 requested a review from a team as a code owner May 17, 2026 05:44
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 17, 2026

CLA assistant check
All committers have signed the CLA.

@CLAassistant
Copy link
Copy Markdown

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.

@snoopuppy582
Copy link
Copy Markdown
Author

sign

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.

marginal_counts silently fails for Counts constructed from hex keys when optional memory_slots is omitted

2 participants