-
Notifications
You must be signed in to change notification settings - Fork 983
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
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.
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? |
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. |
Oh lol, the test imports scipy.stats. |
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. |
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.
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)) |
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: can use np.log2
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)) |
This speeds up import on my box by around 13%. Before: ![image](https://user-images.githubusercontent.com/87974/115590844-4876e180-a286-11eb-9bd0-cfd7e6f3b932.png) After: ![image](https://user-images.githubusercontent.com/87974/115590899-5c224800-a286-11eb-9061-92be44897bc3.png)
This speeds up import on my box by around 13%.
Before:
After: