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

Micro-optimize basis state conversion #1088

Merged
merged 4 commits into from
Feb 17, 2021
Merged

Conversation

josh146
Copy link
Member

@josh146 josh146 commented Feb 17, 2021

Context:

Description of the Change:

  • Modifies QubitDevice, replacing the current conversion logic with one that works for > 32 qubits.

Benefits:

  • Samples can now be processed if the devices has more than 32 qubits

  • The new logic is easier to read and understand than np.ravel_multi_index

  • The new logic is ever so slightly faster:

    image

Possible Drawbacks:

There are two other places where np.ravel_multi_index is used:

  • default.qubit, when applying a statevector to a subset of wires
  • default.mixed, same as above.

It might be worth exploring if a better implementation can be used here as well.

Related GitHub Issues: n/a

@josh146 josh146 added the review-ready 👌 PRs which are ready for review by someone from the core team. label Feb 17, 2021
@codecov
Copy link

codecov bot commented Feb 17, 2021

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.73%. Comparing base (a4060d6) to head (3d46779).
Report is 2634 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1088   +/-   ##
=======================================
  Coverage   97.73%   97.73%           
=======================================
  Files         154      154           
  Lines       11679    11681    +2     
=======================================
+ Hits        11414    11416    +2     
  Misses        265      265           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@chaserileyroberts chaserileyroberts left a comment

Choose a reason for hiding this comment

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

Let me grab a before/after benchmark from asv

pennylane/_qubit_device.py Outdated Show resolved Hide resolved
unraveled_indices = [2] * len(device_wires)
indices = np.ravel_multi_index(samples.T, unraveled_indices)
powers_of_two = 2 ** np.arange(len(device_wires))[::-1]
indices = samples @ powers_of_two
Copy link
Contributor

Choose a reason for hiding this comment

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

This is very clever!

pennylane/_qubit_device.py Show resolved Hide resolved
Co-authored-by: Chase Roberts <chase@xanadu.ai>
@chaserileyroberts chaserileyroberts merged commit b984f42 into master Feb 17, 2021
@chaserileyroberts chaserileyroberts deleted the optimize-samples branch February 17, 2021 18:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
review-ready 👌 PRs which are ready for review by someone from the core team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants