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

Fix Lightning fallbacks to support PL 0.29 changes #416

Merged
merged 19 commits into from
Feb 21, 2023
Merged

Conversation

mlxd
Copy link
Member

@mlxd mlxd commented Feb 15, 2023

Before submitting

Please complete the following checklist when submitting a PR:

  • All new features must include a unit test.
    If you've fixed a bug or added code that should be tested, add a test to the
    tests directory!

  • All new functions and code must be clearly commented and documented.
    If you do make documentation changes, make sure that the docs build and
    render correctly by running make docs.

  • Ensure that the test suite passes, by running make test.

  • Add a new entry to the .github/CHANGELOG.md file, summarizing the
    change, and including a link back to the PR.

  • Ensure that code is properly formatted by running make format.

When all the above are checked, delete everything above the dashed
line and fill in the pull request template.


Context: PR PennyLaneAI/pennylane#3585 updated how wires were handled for SparseHamiltonian types, where situations of device wires being different to operator wires required a reordering. The assumptions made in Lightning were that operator and device wires were always the same and in the same specific order --- this change broke that assumption. As such, when the two diverge Lightning does not apply the correct values to the appropriate locations.

In addition, due to the changes, fallback functionality in Lightning for non-CPP supported observables werre offloaded to _qubit_device with different assumptions made about the bases of said observables. In this case, where _qubit_device required the application of diagonalizing gate, Lightning by default did not, and so the results were inconsistent for said observables. This PR ensure all observable variants for prob, expval and var are offloaded to C++ functions where applicable, and raises an error where functionality support does not currently exist, rather than allowing the return of incorrect results.

Description of the Change: Update wire ordering of sparse matrix defined from Hamiltonian to be reflective of matching device and operator order. Ensure appropriate fallbacks are avoided, instead favouring C++ operations. Ensure error raised when attempting out of order wires for probs.

Benefits: Fixes edge case where the sparse matrix applied is not correct.

Possible Drawbacks:

Related GitHub Issues:

@AmintorDusko AmintorDusko mentioned this pull request Feb 15, 2023
@mlxd mlxd changed the title Fix wire ordering for sparseH Fix Lightning fallbacks to support PL 0.29 changes Feb 17, 2023
@codecov
Copy link

codecov bot commented Feb 17, 2023

Codecov Report

Merging #416 (675e35e) into master (d6eb549) will decrease coverage by 0.07%.
The diff coverage is 84.21%.

@@            Coverage Diff             @@
##           master     #416      +/-   ##
==========================================
- Coverage   99.88%   99.82%   -0.07%     
==========================================
  Files          49       49              
  Lines        4505     4517      +12     
==========================================
+ Hits         4500     4509       +9     
- Misses          5        8       +3     
Impacted Files Coverage Δ
pennylane_lightning/lightning_qubit.py 99.07% <76.92%> (-0.93%) ⬇️
pennylane_lightning/_serialize.py 100.00% <100.00%> (ø)
pennylane_lightning/_version.py 100.00% <100.00%> (ø)
pennylane_lightning/src/simulator/Measures.hpp 99.37% <100.00%> (+<0.01%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@mlxd mlxd marked this pull request as ready for review February 21, 2023 18:13
Copy link
Contributor

@AmintorDusko AmintorDusko left a comment

Choose a reason for hiding this comment

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

Great work, thanks!
I just made some suggestions, but I'm happy to approve.

pennylane_lightning/lightning_qubit.py Outdated Show resolved Hide resolved
pennylane_lightning/lightning_qubit.py Outdated Show resolved Hide resolved
tests/test_measures.py Outdated Show resolved Hide resolved
mlxd and others added 3 commits February 21, 2023 13:44
Co-authored-by: Amintor Dusko <87949283+AmintorDusko@users.noreply.github.com>
Co-authored-by: Amintor Dusko <87949283+AmintorDusko@users.noreply.github.com>
Co-authored-by: Amintor Dusko <87949283+AmintorDusko@users.noreply.github.com>
@mlxd mlxd requested a review from timmysilv February 21, 2023 20:28
Copy link
Contributor

@timmysilv timmysilv left a comment

Choose a reason for hiding this comment

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

I opened #419 (against this branch) to add more test cases to verify my latest comment. I think they wouldn't hurt to add, but this looks good to me either way. In other words, the tests are there should you want them - take em or leave em.

Copy link
Contributor

@vincentmr vincentmr left a comment

Choose a reason for hiding this comment

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

I'm not sure I understand properly what is being compared, so I left a comment about this line

(not np.all(list(wires)[:-1] <= list(wires)[1:]))

Otherwise, looks good! Thanks.

pennylane_lightning/lightning_qubit.py Outdated Show resolved Hide resolved
pennylane_lightning/lightning_qubit.py Outdated Show resolved Hide resolved
@mlxd mlxd merged commit b037f5e into master Feb 21, 2023
@mlxd mlxd deleted the fix_sparse_wire_order branch February 21, 2023 22:34
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