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 errors in cirq/qis and friends #4002

Merged
merged 4 commits into from
Apr 9, 2021

Conversation

mpharrigan
Copy link
Collaborator

Part of #3767

@mpharrigan mpharrigan requested review from cduck, vtomole and a team as code owners April 7, 2021 18:46
@google-cla google-cla bot added the cla: yes Makes googlebot stop complaining. label Apr 7, 2021
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, thanks!

@@ -13,7 +13,7 @@
# limitations under the License.
"""Measures on and between quantum states and operations."""

from typing import Optional, TYPE_CHECKING, Tuple
from typing import Optional, TYPE_CHECKING, Tuple, cast
Copy link
Collaborator

Choose a reason for hiding this comment

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

Optional: alphabetic ordering?

state2_arr = (
state2.density_matrix()
if state2._is_density_matrix()
else cast(np.ndarray, state2.state_vector())
Copy link
Collaborator

Choose a reason for hiding this comment

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

Optional: IIUC this is just to convert from Optional[np.ndarray] to np.ndarray. It'd be nice if you could make it more apparent to the reader. Perhaps an assert is understood by both mypy and humans?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

assert would work but not in this ternary operation

I found this thread when going through these: python/typing#645

honestly, I think these code patterns indicate that there's a missing method. Either "state_vector_for_sure_because_i_already_checked" or array_form_irrespective_of_whether_its_density_or_state. I'll investigate

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Otherwise the cast is safe since the only time state_vector returns none is when _is_density_matrix is true. Obviously mypy isn't smart enough to deduce that full logic

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree with you that there is a missing function to aid this pattern. Perhaps something like Rust's unwrap? This would have the benefit of being useful for all Optional types, not just state vectors. Ideally, we'd use it similar to how you're using cast here:

state2_arr = (
        state2.density_matrix()
        if state2._is_density_matrix()
        else unwrap(state2.state_vector())

WDYT?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Please check out the latest change, namely 45ed207 which fixes these the right way ™️

Copy link
Collaborator

Choose a reason for hiding this comment

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

I like it.

nit: maybe state_or_density_array -> state_vector_or_density_matrix. The latter is clearer (especially since state_tensor is also a thing so state alone in this context is ambiguous) and more consistent with other method names.

Copy link
Collaborator Author

@mpharrigan mpharrigan Apr 8, 2021

Choose a reason for hiding this comment

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

sure. I was trying to think of names that weren't too long but I guess this is a somewhat niche function so it can handle a long name.

edit: esp. since there are state vectors and tensors in this class

@@ -135,7 +136,7 @@ def density_matrix(self) -> np.ndarray:
(a two-dimensional array).
"""
if not self._is_density_matrix():
state_vector = self.state_vector()
state_vector = cast(np.ndarray, self.state_vector())
Copy link
Collaborator

Choose a reason for hiding this comment

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

ditto (optional as above, please address neither or both for consistency)

@@ -159,7 +160,10 @@ def validate(
is_state_tensor = self.data.shape == self.qid_shape
if is_state_vector or is_state_tensor:
validate_normalized_state_vector(
self.state_vector(), qid_shape=self.qid_shape, dtype=dtype, atol=atol
cast(np.ndarray, self.state_vector()),
Copy link
Collaborator

Choose a reason for hiding this comment

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

ditto (...)

@@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import warnings
from typing import Any, List, Sequence
from typing import Any, List, Sequence, Optional
Copy link
Collaborator

Choose a reason for hiding this comment

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

ordering

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

on the basis that none of the imports are ordered in cirq and we don't have tooling to check for or support these, I'm going to not do any alphabetizing of imports :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Fair enough in general.

Maybe at least keep the alphabetic order where it pre-exists, e.g. here, and do whatever elsewhere?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

discussed elsewhere, I'm going to hold out for tooling to do this for me

@mpharrigan mpharrigan added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Apr 9, 2021
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Apr 9, 2021
@CirqBot CirqBot merged commit 845836a into quantumlib:master Apr 9, 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 9, 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