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

Store grouping in Hamiltonian #1515

Merged
merged 156 commits into from
Aug 16, 2021
Merged

Conversation

mariaschuld
Copy link
Contributor

@mariaschuld mariaschuld commented Aug 11, 2021

Context:

With the new VQE workflow, we need a way for the user to define whether PennyLane and devices are supposed to use grouping. Grouping observables can take a long time and should therefore only be computed once. A way to meet both requirements is to allow users to store grouping information in the Hamiltonian, which - if found - is used by PennyLane:

obs = [qml.PauliX(0), qml.PauliX(1), qml.PauliZ(0)]
coeffs = np.array([1., 2., 3.])
H = qml.Hamiltonian(coeffs, obs, compute_groupings=True)

Initialization with compute_groupings=True stores the indices required to make groups of
commuting observables and their coefficients. These are used
(or, if not found, computed) by the get_groupings() method, which returns the
actual groupings.

>>> H.grouping_indices
[[0, 1], [2]]
grouped_coeffs, grouped_obs = H.get_groupings()
>>> grouped_coeffs
[np.array([1., 2.]), np.array(3.)]
>>> grouped_obs
[[qml.PauliX(0), qml.PauliX(1)], [qml.PauliZ(0)]]

PennyLane's qnode, which currently splits a tape with expval(H) into tapes with a single Pauli measurement each, looks for the grouping_indices attribute, and if found, uses groups of Pauli measurements per tape. Also devices that support Hamiltonians can use this strategy if desired.

Description of the Change:

Added grouping functionality to the Hamiltonian class, and uses it in hamiltonian_expand, which in turn is used by the QNode to split expval(H) into Pauli expectations.

Benefits:

One can now control the use of grouping behaviour in the observable.

Possible Drawbacks:

I am quite unsure about the design details. Feedback welcome!

@mariaschuld
Copy link
Contributor Author

Locally, codecov has 100% coverage for all files I changed, except from one line that I never changed (and which is a NotImplementedError). Also the measurement_grouping file it mentions has 100% coverage. I really don't understand why it complains.

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, mostly minor suggestions!

pennylane/transforms/hamiltonian_expand.py Show resolved Hide resolved
pennylane/transforms/hamiltonian_expand.py Outdated Show resolved Hide resolved
pennylane/transforms/hamiltonian_expand.py Show resolved Hide resolved
pennylane/transforms/hamiltonian_expand.py Show resolved Hide resolved
pennylane/transforms/hamiltonian_expand.py Show resolved Hide resolved
pennylane/vqe/vqe.py Outdated Show resolved Hide resolved
pennylane/vqe/vqe.py Outdated Show resolved Hide resolved
pennylane/vqe/vqe.py Outdated Show resolved Hide resolved
pennylane/vqe/vqe.py Show resolved Hide resolved
tests/transforms/test_hamiltonian_expand.py Outdated Show resolved Hide resolved
mariaschuld and others added 13 commits August 13, 2021 11:57
Co-authored-by: Olivia Di Matteo <2068515+glassnotes@users.noreply.github.com>
Co-authored-by: Olivia Di Matteo <2068515+glassnotes@users.noreply.github.com>
Co-authored-by: Olivia Di Matteo <2068515+glassnotes@users.noreply.github.com>
Co-authored-by: Olivia Di Matteo <2068515+glassnotes@users.noreply.github.com>
Co-authored-by: Olivia Di Matteo <2068515+glassnotes@users.noreply.github.com>
Co-authored-by: Olivia Di Matteo <2068515+glassnotes@users.noreply.github.com>
Co-authored-by: Olivia Di Matteo <2068515+glassnotes@users.noreply.github.com>
Co-authored-by: Olivia Di Matteo <2068515+glassnotes@users.noreply.github.com>
Co-authored-by: Olivia Di Matteo <2068515+glassnotes@users.noreply.github.com>
@mariaschuld
Copy link
Contributor Author

Thanks @glassnotes I think I implemented all your suggestions and also checked the docs once more...

Comment on lines 73 to 77
compute_grouping (bool): If True, compute and store information on how to group commuting
observables upon initialization. This information may be accessed when QNodes containing this
Hamiltonian are executed on devices.
grouping_type (str): The type of binary relation between Pauli words.
Can be ``'qwc'``, ``'commuting'``, or ``'anticommuting'``. Ignored if ``compute_grouping`` is False.
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if having both of these args is redundant?

Copy link
Member

Choose a reason for hiding this comment

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

Good point. Are you thinking

qml.Hamiltonian(obs, coeffs, grouping=None)
qml.Hamiltonian(obs, coeffs, grouping="qwc")

etc.

Copy link
Member

Choose a reason for hiding this comment

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

yes

Copy link
Contributor Author

@mariaschuld mariaschuld Aug 16, 2021

Choose a reason for hiding this comment

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

I would find this a barrier for a user - they have to know something about the grouping to use it. My personal main use case would just be compute_grouping=True, I don't want to look up strings of strategies...

But if you all feel it's better I'll change it!

Copy link
Member

Choose a reason for hiding this comment

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

I admit I like it because:

  • it is explicit
  • it avoids the value of one argument affecting the behaviour of another (which I think is always confusing!)
  • it matches other libraries, e.g., scipy.optimize.minimize()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool, will change it then. method will still be influenced by grouping_type, but that's ok I hope...

@mariaschuld mariaschuld merged commit 9ed7ca4 into master Aug 16, 2021
@mariaschuld mariaschuld deleted the store_grouping_in_hamiltonian branch August 16, 2021 12:24
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