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

Net flow constraint #1214

Merged
merged 36 commits into from
May 6, 2021
Merged

Net flow constraint #1214

merged 36 commits into from
May 6, 2021

Conversation

anthayes92
Copy link
Contributor

For the max weighted cycle QAOA problem, the unconstrained approach requires constraints to be included in the cost Hamiltonian. A net flow constraint is introduced to the cost Hamiltonian to help ensure it's minimised by selecting edges such that each node has a net flow of zero edges entering and leaving.

Add a net flow constraint function
Add a private function to calculate quadratic terms of the constraint.

Allows a user to easily introduce constraints to cost Hamiltonian.

It relies on private functions that may be redundant when changes to qml.Hamiltonian are introduced

@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.

@anthayes92 anthayes92 changed the base branch from master to qaoa_cycles_module April 14, 2021 18:47
@codecov
Copy link

codecov bot commented Apr 14, 2021

Codecov Report

Merging #1214 (705d4d2) into master (a839fcf) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1214   +/-   ##
=======================================
  Coverage   98.13%   98.14%           
=======================================
  Files         147      147           
  Lines       11279    11306   +27     
=======================================
+ Hits        11069    11096   +27     
  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 a839fcf...705d4d2. Read the comment docs.

@anthayes92 anthayes92 marked this pull request as ready for review April 15, 2021 14:15
@anthayes92 anthayes92 changed the base branch from qaoa_cycles_module to square_and_collect_private_helper_functions April 15, 2021 14:16
@anthayes92 anthayes92 marked this pull request as draft April 15, 2021 14:35
@anthayes92 anthayes92 marked this pull request as ready for review April 15, 2021 15:00
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.

Thanks @anthayes92! Looks great, just left some requested changes for docs + tests.

pennylane/qaoa/cycle.py Outdated Show resolved Hide resolved
pennylane/qaoa/cycle.py Outdated Show resolved Hide resolved
pennylane/qaoa/cycle.py Outdated Show resolved Hide resolved
pennylane/qaoa/cycle.py Outdated Show resolved Hide resolved
pennylane/qaoa/cycle.py Outdated Show resolved Hide resolved
pennylane/qaoa/cycle.py Outdated Show resolved Hide resolved
pennylane/qaoa/cycle.py Outdated Show resolved Hide resolved
tests/test_qaoa.py Outdated Show resolved Hide resolved
Comment on lines 787 to 788
assert expected_coeffs == h.coeffs
assert all([op.wires == exp.wires for op, exp in zip(h.ops, expected_ops)])
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we need another check to compare the ops.

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
assert expected_coeffs == h.coeffs
assert all([op.wires == exp.wires for op, exp in zip(h.ops, expected_ops)])
assert expected_coeffs == h.coeffs
assert all([op.wires == exp.wires for op, exp in zip(h.ops, expected_ops)])
assert h.ops[0].name == "Identity"
assert all("-".join(op1.name) == "-".join(op2.name) for op1, op2 in zip(h.ops[1:], expected_ops[1:]))

As an example - maybe there's a better way

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed here: f4456ac

tests/test_qaoa.py Show resolved Hide resolved
Base automatically changed from square_and_collect_private_helper_functions to master May 6, 2021 12:50
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.

Thanks @anthayes92! Approved, subject to the suggestions and updating the changelog.
🚀

pennylane/qaoa/cycle.py Outdated Show resolved Hide resolved
pennylane/qaoa/cycle.py Outdated Show resolved Hide resolved
pennylane/qaoa/cycle.py Outdated Show resolved Hide resolved
tests/test_qaoa.py Outdated Show resolved Hide resolved
@anthayes92 anthayes92 merged commit ea74076 into master May 6, 2021
@anthayes92 anthayes92 deleted the net_flow_constraint branch May 6, 2021 14:57
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