Skip to content

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

Merged
CirqBot merged 19 commits into
quantumlib:masterfrom
vtomole:numpy
Jul 7, 2022
Merged

CirqBot merged 19 commits into
quantumlib:masterfrom
vtomole:numpy

Conversation

@vtomole

@vtomole vtomole commented Jul 6, 2022

Copy link
Copy Markdown
Contributor

Part of #3767

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

@dstrain115 dstrain115 left a comment

Copy link
Copy Markdown
Collaborator

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.

Comment thread cirq-core/cirq/qis/clifford_tableau.py Outdated

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
Copy Markdown
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
Copy Markdown
Contributor 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 a team, cduck and mrwojtek as code owners July 6, 2022 23:10
Comment thread cirq-core/cirq/circuits/circuit.py Outdated
qubits: Tuple['cirq.Qid', ...],
dtype: Type[np.complexfloating],
) -> np.ndarray:
) -> Optional[np.ndarray]:

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reverted.

@dabacon dabacon left a comment

Copy link
Copy Markdown
Collaborator

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

"""Utility methods for combining matrices."""

import functools
from typing import Union, TYPE_CHECKING

Copy link
Copy Markdown
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
Copy Markdown
Contributor 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.

Comment thread cirq-core/cirq/linalg/combinators.py Outdated
The kronecker product of all the inputs.
"""
product = np.ones(shape=(1,) * shape_len)
product: np.ndarray[

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread cirq-core/cirq/circuits/circuit.py Outdated
state = qis.eye_tensor(qid_shape, dtype=dtype)

result = _apply_unitary_circuit(self, state, qs, dtype)
assert result is not None

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

@dabacon

dabacon commented Jul 7, 2022

Copy link
Copy Markdown
Collaborator

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

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

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

@dabacon dabacon left a comment

Copy link
Copy Markdown
Collaborator

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

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
Copy Markdown
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
Copy Markdown
Contributor 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor 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