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

Modify some numpy types to be compatible with numpy>=1.20 #5668

Merged
merged 19 commits into from
Jul 7, 2022

Conversation

vtomole
Copy link
Collaborator

@vtomole vtomole commented Jul 6, 2022

Part of #3767

@CirqBot CirqBot added the size: S 10< lines changed <50 label Jul 6, 2022
Copy link
Collaborator

@dstrain115 dstrain115 left a comment

Choose a reason for hiding this comment

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

One comment, otherwise seems fine to me.

@@ -583,6 +583,6 @@ def apply_global_phase(self, coefficient: linear_dict.Scalar):
pass

def measure(
self, axes: Sequence[int], seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None
self, axes: Sequence[int], seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are we sure that removing the default won't break anything? Is it possible anyone is calling measure() without specifying a seed? (Same for 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.

I've put the default back but set it to np.random instead of None because it's needed by _measure.

@vtomole vtomole marked this pull request as ready for review July 6, 2022 23:10
@vtomole vtomole requested review from mrwojtek, a team and cduck as code owners July 6, 2022 23:10
@@ -2588,7 +2589,7 @@ def _apply_unitary_circuit(
state: np.ndarray,
qubits: Tuple['cirq.Qid', ...],
dtype: Type[np.complexfloating],
) -> np.ndarray:
) -> Optional[np.ndarray]:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please revert, the return value on line 2627 can be only None if protocols.apply_unitaries is called with default=None; without search argument it raises TypeError. #5669

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
Collaborator

@dabacon dabacon left a comment

Choose a reason for hiding this comment

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

Few minor comments. You may have conflicts with #5669

@@ -15,14 +15,14 @@
"""Utility methods for combining matrices."""

import functools
from typing import Union, TYPE_CHECKING
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: abc imports (also 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.

Not needed anymore. Reverted.

@@ -39,7 +39,9 @@ def kron(*factors: Union[np.ndarray, complex, float], shape_len: int = 2) -> np.
Returns:
The kronecker product of all the inputs.
"""
product = np.ones(shape=(1,) * shape_len)
product: np.ndarray[
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this can just be np.ndarray (worked locally for me). The extra typing likely is just more confusing than helpful.

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.

@@ -1054,6 +1054,7 @@ def unitary(
state = qis.eye_tensor(qid_shape, dtype=dtype)

result = _apply_unitary_circuit(self, state, qs, dtype)
assert result is not None
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 is fixed in #5669

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.

@dabacon
Copy link
Collaborator

dabacon commented Jul 7, 2022

Thanks for tackling these @vtomole . We cleared out a lot of the easy ones, the more fun ones are all that are left!

@vtomole vtomole requested review from dabacon and pavoljuhas July 7, 2022 01:12
@@ -38,7 +38,7 @@ def copy(self: TSelf, deep_copy_buffers: bool = True) -> TSelf:

@abc.abstractmethod
def measure(
self, axes: Sequence[int], seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None
Copy link
Collaborator

Choose a reason for hiding this comment

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

slightly strange that this one is None, but. one below for sample is None. Is this coming from a subclass?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Wanted to be consistent with clifford_tableau's measure but given that it's actually not needed, I've reverted this change.

@vtomole vtomole requested a review from dabacon July 7, 2022 01:20
Copy link
Collaborator

@dabacon dabacon left a comment

Choose a reason for hiding this comment

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

One more comment and LGTM

@@ -583,6 +583,6 @@ def apply_global_phase(self, coefficient: linear_dict.Scalar):
pass

def measure(
self, axes: Sequence[int], seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None
self, axes: Sequence[int], seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = np.random
) -> List[int]:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think I would keep this None and then do a seed or np.random in the parameter in self._measure. That way we don't have to change the default here (though I don't think it will matter much). np.random might be considered mutable(?) since it changes when different people use it, so best to not put mutables as defaults.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Do we want it as a parameter or like:

    def _measure(self, q, prng: Optional[np.random.RandomState]) -> int:
        if prng is None:
            prng = np.random

Copy link
Collaborator

Choose a reason for hiding this comment

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

that could also work, though this also does something I try to avoid which is re-assign a parameter. Mostly this doesn't ever hurt you, but there are cases where it does (mostly having to do with pytest, btw). It also sort of makes sense that the private method has a random state, but the public one has the default of None.

Copy link
Collaborator

Choose a reason for hiding this comment

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

whichever way you want 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah, makes sense. Switched to param.

@vtomole vtomole added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Jul 7, 2022
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Jul 7, 2022
@CirqBot CirqBot merged commit 27a7f58 into quantumlib:master Jul 7, 2022
@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 Jul 7, 2022
@vtomole vtomole deleted the numpy branch July 7, 2022 02:59
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: S 10< lines changed <50
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants