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 most numpy type errors in cirq/linalg #4000

Merged
merged 13 commits into from
Apr 12, 2021

Conversation

mpharrigan
Copy link
Collaborator

@mpharrigan mpharrigan commented Apr 6, 2021

Using check/mypy --next | grep cirq/linalg this fixes all the
problems.

#3767

Using `check/mypy --next | grep cirq/linalg` this fixes all the
problems.
@mpharrigan mpharrigan requested review from cduck, vtomole and a team as code owners April 6, 2021 20:04
@google-cla google-cla bot added the cla: yes Makes googlebot stop complaining. label Apr 6, 2021
cirq/linalg/decompositions.py Outdated Show resolved Hide resolved
interaction_matrix(y_mat, y),
interaction_matrix(x_mat, x),
before,
return self.global_phase * cast(
Copy link
Collaborator

Choose a reason for hiding this comment

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

ditto

(Possibly could apply to other use of cast below, too.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

changed this per above. what other use of cast?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Well, there are six more left at this point. It'd be nice to get rid of as many as you can. They may hide bugs and make code harder to read.

cirq/linalg/tolerance.py Show resolved Hide resolved
cirq/linalg/transformations.py Show resolved Hide resolved
cirq/linalg/transformations.py Outdated Show resolved Hide resolved
cirq/linalg/transformations.py Outdated Show resolved Hide resolved
left = left_adjust.T.dot(base_left.T)
right = base_right.T.dot(right_adjust.T)
left = cast(np.ndarray, left_adjust.T.dot(base_left.T))
right = cast(np.ndarray, base_right.T.dot(right_adjust.T))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So only the cast on left is necessary and I'm trying to figure out why ......

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it seems like in the general case

x: np.ndarray
y: np.ndarray
x.dot(y) --> Union[number[Any], ndarray]

but

np.dot(x, y) --> ndarray

weird

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

the former is defined in the top-level __init__.pyi type stub file. The latter may be due to the docstring of the skeleton implementation in numpy/core/multiarray.py (that dispatches to c ufuncs). Maybe an oversight on numpy's part? I still don't know why right doesn't need a cast in the above.

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 the former is the correct signature but the latter is the easier signature. I've re-written this function to use cirq.dot and np.dot to keep everything as ndarrays

@@ -326,6 +324,14 @@ def partial_trace(tensor: np.ndarray, keep_indices: List[int]) -> np.ndarray:
return np.einsum(tensor, left_indices + right_indices)


class EntangledStateError(ValueError):
"""Raised by `sub_state_error` if the result of factoring is not a pure state
Copy link
Collaborator

Choose a reason for hiding this comment

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

"not a pure state" -> "not a product state"

Also, I'd remove "by sub_state_error" since the exception may be used in future by related functions.

Suggestion:

"""Raised when a product state is expected, but an entangled state is provided."""

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Changed to use your nice wording

Copy link
Collaborator

@viathor viathor left a comment

Choose a reason for hiding this comment

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

LGTM! Nice work finding a way to get rid of type casts!

raise ValueError('mat1 @ mat2.T must be symmetric.')
if not predicates.is_hermitian(mat1.T.dot(mat2), rtol=rtol, atol=atol):
if not predicates.is_hermitian(cast(np.ndarray, mat1.T.dot(mat2)), rtol=rtol, atol=atol):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Optional: These are the last two casts remaining. It'd be great if we could get rid of them, too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

got rid of 'em!

We'll have to keep an eye out if the numpy folks change the signature for np.dot vs. ndarray.dot. It strikes me as odd that they're different.

@mpharrigan mpharrigan added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Apr 12, 2021
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Apr 12, 2021
@CirqBot
Copy link
Collaborator

CirqBot commented Apr 12, 2021

Automerge cancelled: A status check is failing.

@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Apr 12, 2021
@mpharrigan
Copy link
Collaborator Author

test fails on CI and one of my python environments but not the other 🤷

Changing back to np.dot rather than @

@mpharrigan mpharrigan added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Apr 12, 2021
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Apr 12, 2021
@CirqBot CirqBot merged commit 9c710fb into quantumlib:master Apr 12, 2021
@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Apr 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Makes googlebot stop complaining. numpy
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants