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 private helper functions for squaring Hamiltonian terms and colle… #1213

Merged
merged 13 commits into from
May 6, 2021

Conversation

anthayes92
Copy link
Contributor

@anthayes92 anthayes92 commented Apr 14, 2021

In order to evaluate flow constraints of the cost Hamiltonian in the max weighted cycle QAOA problem, we need to reliably expand quadratic terms and simplify the resulting expressions.

Add a private function to find the coefficients and operators of the term of the Hamiltonian to be squared.
Add a private function to collect duplicate terms of a Hamiltonian.

Example of current and desired behaviour:

Using the tensor product operation @ as currently implemented we find:

>>> coeffs = [0, -1, -1, 1, 1]
>>> ops = [qml.Identity(wires=[0]), qml.PauliZ(wires=[0]), qml.PauliZ(wires=[1]), qml.PauliZ(wires=[2]), qml.PauliZ(wires=[4])]
>>> H = qml.Hamiltonian(coeffs, ops)
>>> square_H = H @ H
>>> print(square_H)
  (0) [I0]
+ (1) [Z0 Z0]
+ (1) [Z1 Z1]
+ (1) [Z2 Z2]
+ (1) [Z4 Z4]
+ (-2) [Z0 Z2]
+ (-2) [Z0 Z4]
+ (-2) [Z1 Z2]
+ (-2) [Z1 Z4]
+ (2) [Z0 Z1]
+ (2) [Z2 Z4]

The desired output of the above example would be:

  (4) [I0]
+ (-2) [Z0 Z2]
+ (-2) [Z0 Z4]
+ (-2) [Z1 Z2]
+ (-2) [Z1 Z4]
+ (2) [Z0 Z1]
+ (2) [Z2 Z4]

This may no longer be needed when related issues on Hamiltonian simplification are closed

@github-actions
Copy link
Contributor

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 Apr 14, 2021

Codecov Report

Merging #1213 (9905194) into master (2c6de3c) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1213   +/-   ##
=======================================
  Coverage   98.13%   98.13%           
=======================================
  Files         147      147           
  Lines       11260    11277   +17     
=======================================
+ Hits        11050    11067   +17     
  Misses        210      210           
Impacted Files Coverage Δ
pennylane/qaoa/cycle.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 2c6de3c...9905194. Read the comment docs.

@anthayes92 anthayes92 marked this pull request as ready for review April 14, 2021 18:21
@trbromley
Copy link
Contributor

Thanks @anthayes92! Looks good, could we come up with two minimal working examples that motivate why we need these functions (to be sure it's worth adding them in)?

E.g., could we have a simple Hamiltonian H where squaring with H @ H doesn't work as intended? Likewise a Hamiltonian H where calling the simplify() method doesn't work as intended?

This will also help us understand the shortfalls of the current approach in qml.Hamiltonian. Also, it can help us decide whether we could just make a quick change to the qml.Hamiltonian.simplify() method instead of having these improvements just available in the cycle module.

Base automatically changed from qaoa_cycles_module to master April 21, 2021 10:17
Copy link
Contributor

@trbromley trbromley left a comment

Choose a reason for hiding this comment

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

Awesome!

pennylane/qaoa/cycle.py Outdated Show resolved Hide resolved
@trbromley trbromley merged commit a8a24da into master May 6, 2021
@trbromley trbromley deleted the square_and_collect_private_helper_functions branch May 6, 2021 12:50
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.

None yet

2 participants