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

Replace pure python loops with numpy where possible in channels.py. #5839

Merged
merged 3 commits into from Sep 12, 2022

Conversation

MichaelBroughton
Copy link
Collaborator

Boosts speed.

@CirqBot CirqBot added the size: S 10< lines changed <50 label Aug 26, 2022
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.

I have some minor suggestions, but this is very nice!

c += np.outer(v, v.conj())
return c
k = np.asarray(kraus_operators)
flat_ops = k.reshape((-1, d))
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 you can achieve the effect of asarray followed by reshape with reshape alone

k = np.reshape(kraus_operators, (-1, d))
return np.einsum(..., k, k.conj())

c += np.outer(v, v.conj())
return c
k = np.asarray(kraus_operators)
flat_ops = k.reshape((-1, d))
Copy link
Collaborator

Choose a reason for hiding this comment

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

As a matter of principle, I think we should prefer errors to be raised as soon as they occur and not propagate far away from the cause since if that happens the error is harder to diagnose and fix.

For this reason, I'd rather we be explicit about the expected array shape and say reshape((len(kraus_operators), d)) rather than reshape((-1, d)).


BTW: This -1 hides a concept that has a name: it is the Choi rank (assuming we were indeed pasted in a Kraus, i.e. minimal representation)! You can use this to make the code read well and (integrating the other comment) say:

choi_rank = len(kraus_operators)
k = np.reshape(kraus_operators, (choi_rank, d))
return np.einsum('bi,bj->ij',, k, k.conj())

This makes it clear that b index in the einsum ranges over the Kraus operators.

@MichaelBroughton MichaelBroughton added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Sep 12, 2022
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Sep 12, 2022
@CirqBot CirqBot merged commit 656ce9d into quantumlib:master Sep 12, 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 Sep 12, 2022
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
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.

None yet

3 participants