Add NumPy number types to TParamVal - #8236
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
mhucka
left a comment
There was a problem hiding this comment.
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?
|
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 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. |
987c1a3 to
b54346b
Compare
|
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>
b54346b to
d4780eb
Compare
|
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>
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>
|
Extended the existing 546 passed |
Fixes #5758.
Summary
TParamValwas defined asfloat | sympy.Expr, while parameter values such asEigenGateexponents already accept NumPy scalars (e.g.np.double,np.short) at runtime. This change addsnp.numberso the type alias matches that behavior, consistent withTParamValComplexwhich already includesnp.number.Notes
TParamValInput/ conversion layer).Scalarfromlinear_dict.py(orthogonal follow-up if desired).# TODO(#5758)marker next to existing serializer tests that already covernp.double/np.shortexponents.np.numberTypeVar overload oncanonicalize_half_turns.canonicalize_half_turnsand 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.pycirq-googleserializer cases already exercise NumPy exponents end-to-endAI/LLM disclosure