Skip to content

Add NumPy number types to TParamVal - #8236

Open
sankalpsthakur wants to merge 7 commits into
quantumlib:mainfrom
sankalpsthakur:fix/5758-tparamval-numpy
Open

Add NumPy number types to TParamVal#8236
sankalpsthakur wants to merge 7 commits into
quantumlib:mainfrom
sankalpsthakur:fix/5758-tparamval-numpy

Conversation

@sankalpsthakur

@sankalpsthakur sankalpsthakur commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Fixes #5758.

Summary

TParamVal was defined as float | sympy.Expr, while parameter values such as EigenGate exponents already accept NumPy scalars (e.g. np.double, np.short) at runtime. This change adds np.number so the type alias matches that behavior, consistent with TParamValComplex which already includes np.number.

TParamVal = float | np.number | sympy.Expr

Notes

  • Does not redesign the parameter system (no new TParamValInput / conversion layer).
  • Does not move Scalar from linear_dict.py (orthogonal follow-up if desired).
  • Removes the # TODO(#5758) marker next to existing serializer tests that already cover np.double / np.short exponents.
  • Adds an np.number TypeVar overload on canonicalize_half_turns.
  • Adds NumPy scalar cases to canonicalize_half_turns and ParamResolver tests.

Test plan

  • pytest cirq-core/cirq/value/angle_test.py cirq-core/cirq/protocols/resolve_parameters_test.py cirq-core/cirq/ops/eigen_gate_test.py cirq-core/cirq/study/resolver_test.py
  • Existing cirq-google serializer cases already exercise NumPy exponents end-to-end

AI/LLM disclosure

  • AI coding tools (including Grok and/or Codex agent-assisted editing) were used to help draft or modify code and this PR description.
  • I reviewed the complete change, understand the reasoning, and ran the reported local tests before submitting.
  • This submission is original work of authorship under the project CLA / contributor terms; AI output was not pasted unreviewed.

@github-actions github-actions Bot added the size: S 10< lines changed <50 label Aug 1, 2026
@codecov

codecov Bot commented Aug 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.59%. Comparing base (30eb1ad) to head (f22606b).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8236      +/-   ##
==========================================
- Coverage   99.59%   99.59%   -0.01%     
==========================================
  Files        1122     1122              
  Lines      102566   102843     +277     
==========================================
+ Hits       102153   102427     +274     
- Misses        413      416       +3     

☔ 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.

@mhucka mhucka left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you for this work.

To go along with these changes, an additional overload for canonicalize_half_turns for np.number (or TypeVar bounded by np.number) is needed in file cirq/value/angle.py.

The test cases also should be expanded to exercise both floating-point and integer NumPy scalar subtypes (e.g., np.float64, np.double, np.int64, np.short) to verify that arithmetic operations (e.g., modulo operations, and range wrapping into (-1, +1]) preserve type and value invariants as expected across NumPy numeric types.

Finally, Dax Fohl noted in the last comment on issuue 5758 that a change of this nature would have backward compatibility implications. Can you add test cases (e.g., for the case mentioned at the end of the first paragraph in that comment) to verify that backards compatibility is maintained?

@mhucka mhucka added triage/discuss Needs decision / discussion, bring these up during Cirq Cynque area/numpy labels Aug 18, 2026
@mhucka

mhucka commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Discussed during Cirq Cynq 2026-08-19: consensus is that this change may indeed work, but this definitely needs many more test cases than what is in the PR currently. In particular, we need to check that the parameter resolver works correctly. (See resolve_parameter in cirq/protocols/resolver_parameter.py). The param resolver needs to be able to correctly resolve a bunch of different gates.

A possible approach is to look at the tests already in param resolver, then extend the relevant tests with numpy values, and make sure everything still works.

@sankalpsthakur

Copy link
Copy Markdown
Contributor Author

Added an np.number TypeVar overload on canonicalize_half_turns, plus wrapping tests for np.float64/np.double/np.int64/np.short, ParamResolver coverage on X/Y/Z/H/CZ/CX, and the XPowGate(np.double(0.5)) isinstance(float) case from #5758. 987c1a3, please take another look.

@github-actions github-actions Bot added size: M 50< lines changed <250 and removed size: S 10< lines changed <50 labels Aug 19, 2026
@sankalpsthakur
sankalpsthakur force-pushed the fix/5758-tparamval-numpy branch from 987c1a3 to b54346b Compare August 19, 2026 20:42
@sankalpsthakur

Copy link
Copy Markdown
Contributor Author

Rebased onto main and fixed format in b54346b.

TParamVal was float | sympy.Expr, but parameter values (e.g. EigenGate
exponents) already accept NumPy scalars like np.double at runtime.
Mirror TParamValComplex by including np.number so static type checkers
agree with the established runtime behavior.

Fixes quantumlib#5758

Signed-off-by: Sankalp Thakur <sankalphimself@gmail.com>
Signed-off-by: Sankalp Thakur <sankalphimself@gmail.com>
Pytest Ubuntu (3.14) failed on stream_manager_test cancel race;
Mac/Win 3.14 and other Ubuntu versions passed. PR only touches TParamVal.
Add an np.number TypeVar overload for canonicalize_half_turns and cover
float/integer NumPy scalars, ParamResolver, and the quantumlib#5758 isinstance case.

Signed-off-by: Sankalp Thakur <sankalphimself@gmail.com>
Signed-off-by: Sankalp Thakur <sankalphimself@gmail.com>
@sankalpsthakur
sankalpsthakur force-pushed the fix/5758-tparamval-numpy branch from b54346b to d4780eb Compare August 20, 2026 01:02
@sankalpsthakur

Copy link
Copy Markdown
Contributor Author

Rebased onto main in d4780eb.

Cover np.float32/np.int32 through canonicalize_half_turns wrapping into
(-1, +1], TParamVal, ParamResolver, and the quantumlib#5758 np.double isinstance case.

Signed-off-by: Sankalp Thakur <sankalphimself@gmail.com>
@github-actions github-actions Bot added size: L 250< lines changed <1000 and removed size: M 50< lines changed <250 labels Aug 20, 2026
@sankalpsthakur

sankalpsthakur commented Aug 20, 2026

Copy link
Copy Markdown
Contributor Author

@mhucka added wrapping tests for np.float32, np.float64, np.int32, and np.int64 on canonicalize_half_turns (type preserved into (-1, +1]), plus ParamResolver coverage on more EigenGates. The #5758 np.double isinstance(float) case is still there. Overload is a TypeVar bound by np.number.

Add NumPy values to the existing resolve_parameters, is_parameterized,
parameter_names, and recursive resolve tests. Cover ParamResolver
formulas plus rx/ry/rz, FSim, PhasedXZ, GlobalPhase, Wait, CCZ/CCX,
and controlled XPow.

Signed-off-by: Sankalp Thakur <sankalphimself@gmail.com>
@sankalpsthakur

Copy link
Copy Markdown
Contributor Author

Extended the existing resolve_parameters / is_parameterized / parameter_names / recursive tests with NumPy scalars (np.float32, np.float64, np.double, np.int32, np.int64, np.short). ParamResolver formulas and gates: rx/ry/rz, FSim, PhasedXZ, GlobalPhase, Wait, CCZ/CCX, controlled XPow.

f22606b

PYTHONPATH=cirq-core pytest cirq-core/cirq/protocols/resolve_parameters_test.py cirq-core/cirq/study/resolver_test.py cirq-core/cirq/ops/eigen_gate_test.py cirq-core/cirq/value/angle_test.py

546 passed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/numpy size: L 250< lines changed <1000 triage/discuss Needs decision / discussion, bring these up during Cirq Cynque

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider adding numpy number types to TParamVal

2 participants