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 a modified simplify function for building Hamiltonians #2103

Merged
merged 13 commits into from
Jan 28, 2022

Conversation

soranjh
Copy link
Contributor

@soranjh soranjh commented Jan 18, 2022

Context:
Add a modified version of the simplify function to the hf module. This functions combines redundant terms in a Hamiltonian and eliminates terms with zero coefficients.

Description of the Change:
Function _simplify is added to qml.hf.hamiltonian.

Benefits:
The new function makes construction of molecular Hamiltonians more efficient. For LiH, as an example, the time to construct the Hamiltonian is reduced from ~200 s to ~10 s.

Possible Drawbacks:

Related GitHub Issues:

@soranjh soranjh added WIP 🚧 Work-in-progress qchem ⚛️ Related to the QChem package labels Jan 18, 2022
@github-actions
Copy link
Contributor

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.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 Jan 18, 2022

Codecov Report

Merging #2103 (ea4289e) into master (f9bffe4) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2103   +/-   ##
=======================================
  Coverage   99.20%   99.20%           
=======================================
  Files         230      230           
  Lines       17717    17718    +1     
=======================================
+ Hits        17576    17577    +1     
  Misses        141      141           
Impacted Files Coverage Δ
pennylane/hf/hamiltonian.py 100.00% <100.00%> (ø)
pennylane/hf/tapering.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 f9bffe4...ea4289e. Read the comment docs.

@soranjh soranjh changed the title [WIP] add modified simplify function Add modified simplify function Jan 18, 2022
@soranjh soranjh changed the title Add modified simplify function Add a modified simplify function for building Hamiltonians Jan 18, 2022
@soranjh soranjh removed the WIP 🚧 Work-in-progress label Jan 18, 2022
@soranjh soranjh marked this pull request as ready for review January 18, 2022 17:56

return qml.Hamiltonian(coeffs, ops)


Copy link
Contributor

Choose a reason for hiding this comment

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

It would be great to reproduce this pattern in the Hamiltonian class. At the moment, H.simplify() is a method that manipulates the internal data of H, which is dangerous. We'd like it to return a new operator. Another option is qml.simplify(H), which could be cool because other ops may be simplified in some way too.

I don't think this relates to this PR though, which is just about building Hamiltonians in a special way, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

This PR is literally adding a function that simplifies any input Hamiltonian. As far as I can tell, it should be possible to reproduce in the Hamiltonian class, or as you suggest, having qml.simplify(H). I really like this latter suggestion actually

Copy link
Contributor Author

@soranjh soranjh Jan 25, 2022

Choose a reason for hiding this comment

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

Hi @mariaschuld, @ixfoduap and @josh146. Here are my thoughts on the simplify function.

This is a general functions that does not necessary belong to qml.hf. However, the way it is written currently is based on one important point: convert Pauli words to strings and manipulate the strings with python. For example, instead of looping over the words and using compare, one can simply use
if o in ['XYZ', ...]
However, I am not sure we want to manipulate Hamiltonians/operators by turning them to strings. Another philosophy could be generating the binary representation of the words and Hamiltonians and manipulate those objects numerically to, for example, compare or multiply them. I think we need to decide first what approach we want to follow for operator manipulation/arithmetic in general. This is the only reason that I hided _simplify inside hf.hamiltonian for now.

So, I want your thoughts on these options:

  1. Convert hf.hamiltonian._simplify to qml.simplify(H).
  2. Wait until we have an ADR on how we really want to efficiently work with operators/Hamiltonian in PL and then create qml.simplify(H) accordingly.
  3. Change hf.hamiltonian._simplify to hf.hamiltonian.simplify.

Copy link
Member

Choose a reason for hiding this comment

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

@soranjh I think these are very valid points, still a lot needs to be figured out in terms of how to best implement comparison/simplification long-term.

Having said that, it's not quite clear when this research will be done/implemented. Since your version already is an improvement over what is already in PL, I am very much in favour of making it more general now, and continuing to improve it iteratively down the line.

In other words: since this is already a net benefit over what PL users have access to, I don't think there is much disadvantage to making this generally available in the short term?

Copy link
Contributor

@mariaschuld mariaschuld Jan 26, 2022

Choose a reason for hiding this comment

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

@soranjh @josh146 I would suggest to go for 2 in this PR - this is the least work for you now, and it incurs only 100% non-user-facing changes in the future!

Then, echoing Josh, I would suggest to soon make a PR to call the private function (hacky for now) in Hamiltonian.simplify if possible, or to otherwise improve that method with the insights gained here. We could combine it with making this Hamiltonian method return a new operator, but this breaks a few user-facing things so it is not trivial.

Lastly, we win more time for the ADR of how to do things properly.

Copy link
Member

Choose a reason for hiding this comment

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

@mariaschuld makes the following proposal, which I think makes a lot of sense:

  1. Continuing working on this PR

  2. Once this PR is merged, improve the existing Hamiltonian.simplify() method with this code

  3. Finally, we can use what we learnt to (a) inform the ADR, and (b) help us decide any final UI/design decisions.

Copy link
Contributor

@ixfoduap ixfoduap left a comment

Choose a reason for hiding this comment

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

The code looks great, no real suggestions.

But I wonder if following Maria's comment we should turn this into a more general qml.simplify() function, which I think is a great idea

doc/releases/changelog-dev.md Outdated Show resolved Hide resolved
pennylane/hf/hamiltonian.py Outdated Show resolved Hide resolved
pennylane/hf/hamiltonian.py Show resolved Hide resolved
pennylane/hf/hamiltonian.py Show resolved Hide resolved
pennylane/hf/hamiltonian.py Show resolved Hide resolved

return qml.Hamiltonian(coeffs, ops)


Copy link
Contributor

Choose a reason for hiding this comment

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

This PR is literally adding a function that simplifies any input Hamiltonian. As far as I can tell, it should be possible to reproduce in the Hamiltonian class, or as you suggest, having qml.simplify(H). I really like this latter suggestion actually

@soranjh soranjh requested a review from ixfoduap January 26, 2022 17:11
Copy link
Contributor

@obliviateandsurrender obliviateandsurrender left a comment

Choose a reason for hiding this comment

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

Just leaving a couple of suggestions and clarifications. Everything else looks good to me!

doc/releases/changelog-dev.md Outdated Show resolved Hide resolved
doc/releases/changelog-dev.md Outdated Show resolved Hide resolved
pennylane/hf/hamiltonian.py Show resolved Hide resolved
pennylane/hf/hamiltonian.py Show resolved Hide resolved
doc/releases/changelog-dev.md Outdated Show resolved Hide resolved
@soranjh soranjh merged commit b820a14 into master Jan 28, 2022
@soranjh soranjh deleted the hf_simplify_hamiltonian branch January 28, 2022 16:02
@josh146 josh146 mentioned this pull request Feb 4, 2022
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
qchem ⚛️ Related to the QChem package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants