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

Prevent Hamiltonians that share wires from being multiplied together #1273

Merged
merged 7 commits into from
May 6, 2021

Conversation

trbromley
Copy link
Contributor

Raises a ValueError if Hamiltonians sharing wires are multiplied together:

h1 = qml.Hamiltonian([1, 1], [qml.PauliZ(0), qml.PauliZ(1)])
h1 @ h1

This will raise a ValueError.

Fixes #1272 although this is just to help prevent users getting caught-out, we still need a better solution for manipulation of Hamiltonians.

@github-actions
Copy link
Contributor

github-actions bot commented May 4, 2021

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 May 4, 2021

Codecov Report

Merging #1273 (0bc2bc4) into master (8cf8475) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1273   +/-   ##
=======================================
  Coverage   98.13%   98.13%           
=======================================
  Files         147      147           
  Lines       11256    11260    +4     
=======================================
+ Hits        11046    11050    +4     
  Misses        210      210           
Impacted Files Coverage Δ
pennylane/vqe/vqe.py 92.97% <100.00%> (+0.15%) ⬆️

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 8cf8475...0bc2bc4. Read the comment docs.

Copy link
Contributor

@glassnotes glassnotes left a comment

Choose a reason for hiding this comment

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

Looks good! 👍 In the long term, maybe @ can call grouping.pauli_mult for cases like this (though I think there would be some things to iron out in terms of wire maps -- perhaps @josh146 can speak further to this, he pointed it out while reviewing #1181).

@trbromley trbromley requested a review from josh146 May 5, 2021 19:25
@josh146
Copy link
Member

josh146 commented May 6, 2021

Looks good! 👍 In the long term, maybe @ can call grouping.pauli_mult for cases like this (though I think there would be some things to iron out in terms of wire maps -- perhaps @josh146 can speak further to this, he pointed it out while reviewing #1181).

Alternatively, is there anything stopping us from having __matmul__ call grouping.pauli_mult in the short term? 🙂 It might solve a lot of open issues we have on operator multiplication

@josh146 josh146 removed their request for review May 6, 2021 06:47
@anthayes92
Copy link
Contributor

Nice short term solution! A long term fix for this would render 1213 obselete.

@trbromley trbromley merged commit 2c6de3c into master May 6, 2021
@trbromley trbromley deleted the fix_multiplying_hamiltonians branch May 6, 2021 11:51
@glassnotes
Copy link
Contributor

Looks good! +1 In the long term, maybe @ can call grouping.pauli_mult for cases like this (though I think there would be some things to iron out in terms of wire maps -- perhaps @josh146 can speak further to this, he pointed it out while reviewing #1181).

Alternatively, is there anything stopping us from having __matmul__ call grouping.pauli_mult in the short term? slightly_smiling_face It might solve a lot of open issues we have on operator multiplication

Which __matmul__, the one in the Hamiltonian class? I think as long as a consistent wire map is chosen it should be fine (and we could make a like a cumulative wire map using the shared wires of all the terms, or even make it an argument to the Hamiltonian itself.).

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.

Incorrect multiplication of Hamiltonians
4 participants