Skip to content

fix(openqasm): reject invalid random seeds - #4219

Open
alexcastgonz (pcbeingused333) wants to merge 1 commit into
microsoft:mainfrom
pcbeingused333:fix/c2145-openqasm-seed
Open

alexcastgonz (pcbeingused333) wants to merge 1 commit into
microsoft:mainfrom
pcbeingused333:fix/c2145-openqasm-seed

Conversation

@pcbeingused333

Copy link
Copy Markdown

Fixes #4162

The source-string execution path silently drops Python extraction errors for seed, causing floats, negative integers, and integers outside the unsigned 64-bit range to run without a seed.

Return PyResult<Option<u64>> from get_seed and propagate conversion errors to the caller. Missing seeds and explicit None retain their existing behavior. Regression tests cover invalid inputs, deterministic execution with valid seeds (including zero and the maximum unsigned 64-bit value), and optional seeds.

A valid seed close to 2^64 used with several shots still overflows in the per-shot increment (s + i), which the issue also mentions; that is unchanged here, so this PR only covers the seeds that get dropped.

Testing: with the native extension built from this branch, tests/test_openqasm_seed.py passes (7 passed); with the original interop.rs the three invalid-seed cases fail and the four controls pass. The issue's repro now raises TypeError for seed=1.5 and OverflowError for seed=-1. Qiskit interop tests covering seeds, cargo clippy -p qdk and cargo fmt --check pass.

AI assistance was used to prepare this change and its tests.

This branch has not been deployed

No deployments
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.

[Bug] openqasm.run(<source>) silently ignores float or negative seeds

1 participant