-
Notifications
You must be signed in to change notification settings - Fork 983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix sympy 1.10 related mypy typing issues #5263
Conversation
- Fixes most of the typing issues related to sympy 1.10 - Most were caused by either: - Use of sympy.Basic which is too general (we probably want sympy.Expr) that did not have the right duck-typing - Not handling of sympy.Expr in various places that could theoretically have sympy expressions returned to them. - Fixed others with casts or other checks. - About ~10 out of 450 errors I just put type: ignore after struggling for a while.
- Apparently boolean expressions in sympy are not sympy.Expr and are sympy.Basic. Sigh...
|
||
if repetitions <= 0: | ||
raise ValueError('repetitions <= 0') | ||
if not isinstance(min_delay_nanos, float) or not isinstance(max_delay_nanos, float): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
think this needs to be (float,int)
and not just float
. isinstance(1, float)
returns False
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also we probably want to allow things like np.int64
, np.float64
, etc. It might be nice to define a tuple of allowed "numeric" types in one place and use it throughout cirq, though that could be a future PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While, in general, this is true, I don't think it is true here, since we control cirq.Duration.total_nanos().
I went ahead and modified cirq.Duration to guarantee returning a float, even when passed a numpy.int64 type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I take that back. It looks like Duration is not typed correctly and can return an int. I have made the typing more clear for Duration as well.
|
||
# Input validation | ||
if repetitions <= 0: | ||
raise ValueError('repetitions <= 0') | ||
if not isinstance(min_delay_nanos, float) or not isinstance(max_delay_nanos, float): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
float, int
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above.
cirq-core/cirq/study/resolver.py
Outdated
@@ -72,7 +72,7 @@ def __init__(self, param_dict: 'cirq.ParamResolverOrSimilarType' = None) -> None | |||
|
|||
def value_of( | |||
self, value: Union['cirq.TParamKey', float], recursive: bool = True | |||
) -> 'cirq.TParamVal': | |||
) -> Union['cirq.TParamVal']: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't understand this one. Union with what?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working through all these issues!
A few general comments:
- When adding type ignore comments, consider adding the specific error code indicated by mypy, e.g.
type: ignore[dict-item]
. - It would be nice to try to use common type aliases we have already defined like
cirq.TParamKey
,cirq.TParamVal
, andcirq.ParamDictType
where possible. - Similarly, it would be nice to define common type tuples to be used in
isinstance
checks, but this could be something for the future (it's unfortunate that we need to define type unions and tuples separately, though this will be fixed in python 3.10).
|
||
if repetitions <= 0: | ||
raise ValueError('repetitions <= 0') | ||
if not isinstance(min_delay_nanos, float) or not isinstance(max_delay_nanos, float): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also we probably want to allow things like np.int64
, np.float64
, etc. It might be nice to define a tuple of allowed "numeric" types in one place and use it throughout cirq, though that could be a future PR.
Co-authored-by: Matthew Neeley <mneeley@gmail.com>
Co-authored-by: Matthew Neeley <mneeley@gmail.com>
…into sympy_v10_part1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly looks good. The only thing I'm not sure about are the circuit_serializer.py
tests, which look like a functional change not just a typechecking fix.
min_delay_nanos = min_delay_dur.total_nanos() | ||
max_delay_nanos = max_delay_dur.total_nanos() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might consider adding a function on Duration
itself that would get the total nanos as a number and fail if it is symbolic. Then users could call that function instead of doing their own type checks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possibly, but that's an issue for another PR.
@@ -38,7 +38,11 @@ def assert_optimizes( | |||
if compare_unitaries: | |||
if cirq.is_parameterized(circuit): | |||
for a in (0, 0.1, 0.5, -1.0, np.pi, np.pi / 2): | |||
params = {'x': a, 'y': a / 2, 'z': -2 * a} | |||
params: Dict[Union[str, sympy.Expr], Union[float, sympy.Expr]] = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Use cirq.ParamDictType
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
cirq-core/cirq/work/sampler.py
Outdated
@@ -29,6 +29,7 @@ | |||
|
|||
if TYPE_CHECKING: | |||
import cirq | |||
import sympy |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is sympy still needed? Looks like it's not used below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
@@ -434,54 +434,72 @@ def _deserialize_gate_op( | |||
exponent=arg_func_langs.float_arg_from_proto( | |||
operation_proto.xpowgate.exponent, | |||
arg_function_language=arg_function_language, | |||
required_arg_name=None, | |||
required_arg_name='exponent', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the point of this change? Seems like this will change functionality by requiring exponent
to be set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I did this for better error messages, but I just reverted it since it may have unintended effects.
@@ -57,7 +61,11 @@ def _execute_and_read_result( | |||
memory_map = {} | |||
|
|||
for region_name, values in memory_map.items(): | |||
executable.write_memory(region_name=region_name, value=values) | |||
print(region_name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove print statement
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Co-authored-by: Matthew Neeley <mneeley@gmail.com>
Co-authored-by: Matthew Neeley <mneeley@gmail.com>
…into sympy_v10_part1
Ok, everyone, please review! Your prompt attention would be appreciated, since this is quite challenging to keep up to date with merge conflicts. |
I'm fine with the changes under |
…into sympy_v10_part1
* Fix sympy 1.10 related mypy typing issues. - Fixes most of the typing issues related to sympy 1.10 - Most were caused by either: - Use of sympy.Basic which is too general (we probably want sympy.Expr) that did not have the right duck-typing - Not handling of sympy.Expr in various places that could theoretically have sympy expressions returned to them. - Fixed others with casts or other checks. - About ~10 out of 450 errors I just put type: ignore after struggling for a while. Co-authored-by: Matthew Neeley <mneeley@gmail.com>
sympy.Expr) that did not have the right duck-typing
theoretically have sympy expressions returned to them.
for a while.