Skip to content

Fix histogram() for qudits by adding fold_base parameter#8138

Open
Su760 wants to merge 5 commits into
quantumlib:mainfrom
Su760:fix-issue-5926
Open

Fix histogram() for qudits by adding fold_base parameter#8138
Su760 wants to merge 5 commits into
quantumlib:mainfrom
Su760:fix-issue-5926

Conversation

@Su760

@Su760 Su760 commented Jun 14, 2026

Copy link
Copy Markdown

The histogram function uses base-2 by default, which gives incorrect results for qudits.
For example, three qutrits, each measuring 2, give 7 instead of 26.
I added an optional fold_base parameter to specify the qudit dimension. Raises ValueError if both fold_func and fold_base are passed.

Fixes #5926

@Su760 Su760 requested a review from a team as a code owner June 14, 2026 18:12
@Su760 Su760 requested a review from dabacon June 14, 2026 18:12
@github-actions github-actions Bot added the size: S 10< lines changed <50 label Jun 14, 2026

@pavoljuhas pavoljuhas left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please rewrite the PR description in style of commit messages of the main branch using your own words and thinking. The description should be brief, to the point and without redundant AI checklists.

Comment thread cirq-core/cirq/study/result.py Outdated
key: TMeasurementKey,
fold_func: Callable[[tuple], T] = cast(Callable[[tuple], T], value.big_endian_bits_to_int),
fold_func: Callable[[tuple], T] | None = None,
qid_shape: tuple[int, ...] | None = None,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I suggest to rename the new argument to fold_base with the same possible values as in big_endian_digits_to_int. This would make it more convenient to handle same-dimension qudits as their dimension would not need to be repeated.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Renamed to fold_base. Fold_base=3 is much cleaner than repeating the shape.

Comment thread cirq-core/cirq/study/result.py Outdated
Comment on lines +254 to +257
qid_shape: The qudit dimensions for the measured qubits. If
specified, the default fold_func will use this to correctly
interpret measurement results for qudits with dimension > 2.
If not specified, defaults to base-2 (qubit) interpretation.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The second line needs to be indented as in the fold_func doc above.

I suggest to raise ValueError when both the fold_func and the qid_shape / fold_base are specified. The docstring should make it clear that the new argument is a convenience way of providing fold_func for qudits.

Comment thread cirq-core/cirq/study/result_test.py Outdated
)
assert result.histogram(key='q', qid_shape=(3, 3, 3)) == collections.Counter({26: 1})
# Verify backward compatibility: without qid_shape, base-2 is used
assert result.histogram(key='q') == collections.Counter({7: 1})

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is incorrect, the default fold_func for qubits should raise an error if there is anything else than 0 or 1 in the measured array.

If you can do the work without outsourcing to an LLM, please update big_endian_bits_to_int to raise an error for the above. If this would be just passed on to an AI tool, please delete the assertion and let it be.

@codecov

codecov Bot commented Jun 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.60%. Comparing base (6baa941) to head (c218685).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8138      +/-   ##
==========================================
- Coverage   99.60%   99.60%   -0.01%     
==========================================
  Files        1118     1118              
  Lines      101043   101054      +11     
==========================================
+ Hits       100643   100653      +10     
- Misses        400      401       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Su760 Su760 changed the title Fix histogram() to support qudits via qid_shape parameter Fix histogram() for qudits by adding fold_base parameter Jun 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: S 10< lines changed <50

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Histogram function doesn't fail for non-bool values.

2 participants