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

Add generate_samples() to lightning #247

Merged
merged 67 commits into from
Mar 18, 2022
Merged

Add generate_samples() to lightning #247

merged 67 commits into from
Mar 18, 2022

Conversation

trevor-vincent
Copy link
Contributor

@trevor-vincent trevor-vincent commented Mar 7, 2022

Context:

generate_samples() performs most of the heavy lifting for the sample() function. Currently there is no lightning version of generate_samples().

Description of the Change:

This pr adds a lightning C++ version of the generate_samples function using the alias sampling method (https://en.wikipedia.org/wiki/Alias_method). This C++ function currently single-threaded only, however a parallel version is straight-forward (e.g. insert a openmp pragma above the for loop that computes samples while being mindful that the parallel random number generation and caching is thread-safe).

Benefits:

Lightning now has it's own version of generate_samples(), getting us closer to a complete independent lightning statistics module. For low numbers of shots, the single-threaded version is faster than default.qubit (which uses numpy.random.choice) on my machine:

bench

This benchmark times the generate_samples() function for a randomly generated 10-qubit circuit (benchmarks were single-threaded for lightning, however numpy may be multi-threaded).

Possible Drawbacks:

At higher shots, we should likely switch to a parallel version.

Related GitHub Issues:

@github-actions
Copy link
Contributor

github-actions bot commented Mar 7, 2022

Hello. You may have forgotten to update the changelog!
Please edit .github/CHANGELOG.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

@codecov
Copy link

codecov bot commented Mar 7, 2022

Codecov Report

Merging #247 (c616693) into master (1c0a973) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##            master      #247   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            4         4           
  Lines          366       379   +13     
=========================================
+ Hits           366       379   +13     
Impacted Files Coverage Δ
pennylane_lightning/_version.py 100.00% <100.00%> (ø)
pennylane_lightning/lightning_qubit.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1c0a973...c616693. Read the comment docs.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 7, 2022

Test Report (C++) on Ubuntu

       1 files  ±  0         1 suites  ±0   0s ⏱️ ±0s
   561 tests +  6     561 ✔️ +  6  0 💤 ±0  0 ±0 
2 299 runs  +10  2 299 ✔️ +10  0 💤 ±0  0 ±0 

Results for commit c616693. ± Comparison against base commit 1c0a973.

This pull request removes 4 and adds 10 tests. Note that renamed tests count towards both.
runner.global ‑ Variances/Testing list of operators defined by a matrix:
runner.global ‑ Variances/Testing list of operators defined by its name:
runner.global ‑ Variances/Testing single operation defined by a matrix:
runner.global ‑ Variances/Testing single operation defined by its name:
runner.global ‑ Sample - double/No wires provided:
runner.global ‑ Sample - float/No wires provided:
runner.global ‑ Variances - double/Testing list of operators defined by a matrix:
runner.global ‑ Variances - double/Testing list of operators defined by its name:
runner.global ‑ Variances - double/Testing single operation defined by a matrix:
runner.global ‑ Variances - double/Testing single operation defined by its name:
runner.global ‑ Variances - float/Testing list of operators defined by a matrix:
runner.global ‑ Variances - float/Testing list of operators defined by its name:
runner.global ‑ Variances - float/Testing single operation defined by a matrix:
runner.global ‑ Variances - float/Testing single operation defined by its name:

♻️ This comment has been updated with latest results.

Copy link
Member

@mlxd mlxd left a comment

Choose a reason for hiding this comment

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

Thanks @trevor-vincent
I have a few comments and some suggestions

pennylane_lightning/src/bindings/Bindings.cpp Outdated Show resolved Hide resolved
pennylane_lightning/src/bindings/Bindings.cpp Outdated Show resolved Hide resolved
pennylane_lightning/src/bindings/Bindings.cpp Outdated Show resolved Hide resolved
pennylane_lightning/src/bindings/Bindings.cpp Outdated Show resolved Hide resolved
pennylane_lightning/src/bindings/Bindings.cpp Outdated Show resolved Hide resolved
pennylane_lightning/src/simulator/Measures.hpp Outdated Show resolved Hide resolved
pennylane_lightning/src/simulator/Measures.hpp Outdated Show resolved Hide resolved
pennylane_lightning/src/simulator/Measures.hpp Outdated Show resolved Hide resolved
pennylane_lightning/src/tests/Test_Measures.cpp Outdated Show resolved Hide resolved
tests/test_measures.py Show resolved Hide resolved
trevor-vincent and others added 4 commits March 9, 2022 16:42
Co-authored-by: Lee James O'Riordan <mlxd@users.noreply.github.com>
Co-authored-by: Lee James O'Riordan <mlxd@users.noreply.github.com>
Co-authored-by: Lee James O'Riordan <mlxd@users.noreply.github.com>
Co-authored-by: Lee James O'Riordan <mlxd@users.noreply.github.com>
Copy link
Member

@maliasadi maliasadi left a comment

Choose a reason for hiding this comment

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

Thanks @trevor-vincent. Happy to approve!

Copy link
Member

@mlxd mlxd left a comment

Choose a reason for hiding this comment

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

Great job @trevor-vincent
One suggestion below, but don't see any blocker otherwise. Happy to approve after this change!

pennylane_lightning/src/tests/Test_Measures.cpp Outdated Show resolved Hide resolved
trevor-vincent and others added 2 commits March 18, 2022 15:46
Co-authored-by: Lee James O'Riordan <mlxd@users.noreply.github.com>
@trevor-vincent trevor-vincent merged commit 35a7403 into master Mar 18, 2022
@trevor-vincent trevor-vincent deleted the add-sample branch March 18, 2022 20:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants