feat: add pure-Python results post-processing helpers#69
Open
scarwizz wants to merge 2 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #57
This PR adds pure-Python, NumPy-free results post-processing helpers (
probabilities_to_counts,relabel_to_bitstrings,marginal, andexpectation_z) toionq_core/results.py. These functions operate directly on standardMapping[str, float]dictionaries to support downstream SDKs without adding heavyweight dependencies.Key Features & Implementation Details
probabilities_to_counts: Implements the largest-remainder method (Hare quota) to guarantee counts sum exactly to the requestedshots, avoiding floating-point drift. It includes a deterministic tie-breaker (sorting by state integer ascending) for edge cases where remainders are identical. Gracefully handlesshots=0and empty mappings.relabel_to_bitstrings: Converts integer keys to zero-padded bitstrings using big-endian ordering (qubit 0 is the most significant bit), matching IonQ's Cloud API conventions.marginal: Safely computes marginal distributions while maintaining the requested ordering of the subset qubits. Safely handles duplicate qubits and negative indices.expectation_z: Calculates Z-parity expectation using highly efficient, native Pythonint.bit_count()._validate_probabilitieshelper rigorously checks for and rejects invalid probabilities (NaN,±Inf, negative values), out-of-bound state integers, and invalid qubit indices.ionq_core/__init__.py. The Jinja template was updated to prevent CI failures, NumPy-free conventions were added toAGENTS.md, and aCHANGELOG.mdentry is included. Proper SPDX headers are present in all new files.Test Plan
Achieved 100% branch coverage on
ionq_core/results.py, satisfying the repository's strict CI requirements.Tests include:
tests/test_results.py.{"0": 0.5, "3": 0.5}, three-qubit GHZ state).Local checks passed successfully:
uv run pytest(100% coverage)uv run ruff check(0 errors)uv run ruff format --checkuv run ty check ionq_core/Important
Most code in
ionq_core/is auto-generated and overwritten on regeneration.See CONTRIBUTING.md for which files are safe to edit.