Skip to content
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

Merged
merged 41 commits into from
Apr 27, 2022

Conversation

dstrain115
Copy link
Collaborator

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

- 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.
@CirqBot CirqBot added the size: L 250< lines changed <1000 label Apr 15, 2022
- 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):
Copy link
Collaborator

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.

Copy link
Contributor

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.

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

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):
Copy link
Collaborator

Choose a reason for hiding this comment

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

float, int

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Same as above.

@@ -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']:
Copy link
Collaborator

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Reverted.

Copy link
Contributor

@maffoo maffoo left a 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, and cirq.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):
Copy link
Contributor

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.

cirq-core/cirq/experiments/xeb_fitting.py Outdated Show resolved Hide resolved
cirq-core/cirq/study/flatten_expressions.py Outdated Show resolved Hide resolved
@dstrain115 dstrain115 requested a review from dabacon April 18, 2022 17:26
@dstrain115 dstrain115 requested a review from maffoo April 18, 2022 17:59
Copy link
Contributor

@maffoo maffoo left a 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.

cirq-core/cirq/experiments/t1_decay_experiment.py Outdated Show resolved Hide resolved
Comment on lines +66 to +67
min_delay_nanos = min_delay_dur.total_nanos()
max_delay_nanos = max_delay_dur.total_nanos()
Copy link
Contributor

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.

Copy link
Collaborator Author

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.

cirq-core/cirq/experiments/t2_decay_experiment.py Outdated Show resolved Hide resolved
cirq-core/cirq/experiments/xeb_fitting.py Outdated Show resolved Hide resolved
cirq-core/cirq/experiments/xeb_fitting.py Outdated Show resolved Hide resolved
@@ -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]] = {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Use cirq.ParamDictType?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

Copy link
Collaborator Author

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/observable_settings.py Show resolved Hide resolved
@@ -29,6 +29,7 @@

if TYPE_CHECKING:
import cirq
import sympy
Copy link
Contributor

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed.

Copy link
Collaborator Author

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',
Copy link
Contributor

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.

Copy link
Collaborator Author

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)
Copy link
Contributor

Choose a reason for hiding this comment

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

remove print statement

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

@dstrain115 dstrain115 requested a review from maffoo April 27, 2022 01:09
@dstrain115
Copy link
Collaborator Author

Ok, everyone, please review! Your prompt attention would be appreciated, since this is quite challenging to keep up to date with merge conflicts.

@mrwojtek
Copy link
Collaborator

I'm fine with the changes under cirq_google.calibration

@dstrain115 dstrain115 merged commit e00767a into quantumlib:master Apr 27, 2022
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: L 250< lines changed <1000
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants