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

Speed up cirq import by removing scipy.stats import #4041

Merged
merged 5 commits into from
Apr 22, 2021

Conversation

dabacon
Copy link
Collaborator

@dabacon dabacon commented Apr 21, 2021

This speeds up import on my box by around 13%.

Before:
image
After:
image

@dabacon dabacon requested review from cduck, vtomole and a team as code owners April 21, 2021 16:46
@dabacon dabacon requested a review from maffoo April 21, 2021 16:46
@google-cla google-cla bot added the cla: yes Makes googlebot stop complaining. label Apr 21, 2021
Copy link
Contributor

@maffoo maffoo left a comment

Choose a reason for hiding this comment

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

Looks like this is used in von_neumann_entropy below, so not sure that we can remove the import: https://github.com/quantumlib/Cirq/blob/master/cirq-core/cirq/qis/measures.py#L275. Tests pass, but I don't know if we have coverage of that particular case in von_neumann_entropy.

@balopat
Copy link
Contributor

balopat commented Apr 21, 2021

Looks like this is used in von_neumann_entropy below, so not sure that we can remove the import: https://github.com/quantumlib/Cirq/blob/master/cirq-core/cirq/qis/measures.py#L275. Tests pass, but I don't know if we have coverage of that particular case in von_neumann_entropy.

Nice find! I guess we could still move the import into the von_neumann_entropy function and add a comment that this is a local import for import performance reasons?

@dabacon
Copy link
Collaborator Author

dabacon commented Apr 22, 2021

Looks like this is used in von_neumann_entropy below, so not sure that we can remove the import: https://github.com/quantumlib/Cirq/blob/master/cirq-core/cirq/qis/measures.py#L275. Tests pass, but I don't know if we have coverage of that particular case in von_neumann_entropy.

We do have coverage, at least I checked by printing debug right before the statement then failing a test and seeing that the print showed up. So yeah strange let me investigate.

@dabacon
Copy link
Collaborator Author

dabacon commented Apr 22, 2021

Oh lol, the test imports scipy.stats.

@dabacon
Copy link
Collaborator Author

dabacon commented Apr 22, 2021

OK, made the import delayed in the function. Alternatively we could just calculate the entropy using numpy, the only case you need to be careful about are the 0 * log(0) terms.

I also updated the test to not use scipy.stats and just calculate the entropy directly.

@dabacon dabacon requested a review from maffoo April 22, 2021 15:16
Copy link
Contributor

@maffoo maffoo left a comment

Choose a reason for hiding this comment

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

Minor comment, then LGTM

np.testing.assert_allclose(cirq.von_neumann_entropy(mat), scipy.stats.entropy(probs, base=2))

np.testing.assert_allclose(
cirq.von_neumann_entropy(mat), -np.sum(probs * np.log(probs) / np.log(2))
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: can use np.log2

Suggested change
cirq.von_neumann_entropy(mat), -np.sum(probs * np.log(probs) / np.log(2))
cirq.von_neumann_entropy(mat), -np.sum(probs * np.log2(probs))

@maffoo maffoo added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Apr 22, 2021
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Apr 22, 2021
@CirqBot CirqBot merged commit 81de725 into quantumlib:master Apr 22, 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 22, 2021
@dabacon dabacon deleted the faster branch April 16, 2022 15:42
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants