-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Conversation
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.
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' |
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.
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).
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've put the default back but set it to np.random
instead of None
because it's needed by _measure
.
cirq-core/cirq/circuits/circuit.py
Outdated
@@ -2588,7 +2589,7 @@ def _apply_unitary_circuit( | |||
state: np.ndarray, | |||
qubits: Tuple['cirq.Qid', ...], | |||
dtype: Type[np.complexfloating], | |||
) -> np.ndarray: | |||
) -> Optional[np.ndarray]: |
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.
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
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.
Few minor comments. You may have conflicts with #5669
cirq-core/cirq/linalg/combinators.py
Outdated
@@ -15,14 +15,14 @@ | |||
"""Utility methods for combining matrices.""" | |||
|
|||
import functools | |||
from typing import Union, TYPE_CHECKING |
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: abc imports (also 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.
Not needed anymore. Reverted.
cirq-core/cirq/linalg/combinators.py
Outdated
@@ -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[ |
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 this can just be np.ndarray (worked locally for me). The extra typing likely is just more confusing than helpful.
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/circuits/circuit.py
Outdated
@@ -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 |
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 is fixed in #5669
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.
Thanks for tackling these @vtomole . We cleared out a lot of the easy ones, the more fun ones are all that are left! |
@@ -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 |
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.
slightly strange that this one is None, but. one below for sample is None. Is this coming from a subclass?
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.
Wanted to be consistent with clifford_tableau
's measure
but given that it's actually not needed, I've reverted this change.
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.
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]: |
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 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.
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.
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
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.
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.
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.
whichever way you want 👍
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.
Ah, makes sense. Switched to param.
Part of #3767