Skip to content

feat: add pure-Python results post-processing helpers#69

Open
scarwizz wants to merge 2 commits into
ionq:mainfrom
scarwizz:feature/results-helpers
Open

feat: add pure-Python results post-processing helpers#69
scarwizz wants to merge 2 commits into
ionq:mainfrom
scarwizz:feature/results-helpers

Conversation

@scarwizz

@scarwizz scarwizz commented Jun 9, 2026

Copy link
Copy Markdown

Summary

Closes #57

This PR adds pure-Python, NumPy-free results post-processing helpers (probabilities_to_counts, relabel_to_bitstrings, marginal, and expectation_z) to ionq_core/results.py. These functions operate directly on standard Mapping[str, float] dictionaries to support downstream SDKs without adding heavyweight dependencies.

Key Features & Implementation Details

  • NumPy-free Logic: All implementations use pure Python built-ins.
  • probabilities_to_counts: Implements the largest-remainder method (Hare quota) to guarantee counts sum exactly to the requested shots, avoiding floating-point drift. It includes a deterministic tie-breaker (sorting by state integer ascending) for edge cases where remainders are identical. Gracefully handles shots=0 and 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 Python int.bit_count().
  • Strict Validation: The _validate_probabilities helper rigorously checks for and rejects invalid probabilities (NaN, ±Inf, negative values), out-of-bound state integers, and invalid qubit indices.
  • Repository Integration: Helpers are properly re-exported in ionq_core/__init__.py. The Jinja template was updated to prevent CI failures, NumPy-free conventions were added to AGENTS.md, and a CHANGELOG.md entry 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:

  • 39 comprehensive tests in tests/test_results.py.
  • Real simulator response fixtures (two-qubit Bell state: {"0": 0.5, "3": 0.5}, three-qubit GHZ state).
  • Deterministic rounding verification for complex fractional splits.
  • Full edge-case coverage for invalid inputs, missing fields, and out-of-bounds parameters.

Local checks passed successfully:

  • uv run pytest (100% coverage)
  • uv run ruff check (0 errors)
  • uv run ruff format --check
  • uv run ty check ionq_core/

unitaryHACK AI Disclosure:
I used Gemini to help draft the pure-Python logic, implement algorithms, format code to repository standards, and write comprehensive edge-case unit tests. I then manually ran the code, debugged local execution environment errors, and verified the logic locally using the uv run pytest and ruff toolchains to ensure 100% branch coverage and deterministic execution.


Important

Most code in ionq_core/ is auto-generated and overwritten on regeneration.
See CONTRIBUTING.md for which files are safe to edit.

@scarwizz scarwizz requested a review from a team as a code owner June 9, 2026 18:28
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.

Add pure-Python results post-processing helpers (counts, bitstrings, marginals, expectation values)

1 participant