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 max_weight_cycle helper function #1283

Merged
merged 25 commits into from
May 10, 2021
Merged

Conversation

anthayes92
Copy link
Contributor

Helper functions for the UI exist for each QAOA solution. This adds a helper function for the maximum weighted cycle functionality.

The penealty term size in the uncontrained approach is fixed. A user would have to access the penalty terms through qaoa.cycle to control this.

pennylane/qaoa/cost.py Outdated Show resolved Hide resolved
pennylane/qaoa/cost.py Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented May 7, 2021

Codecov Report

Merging #1283 (98d4aa6) into master (fea7095) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1283   +/-   ##
=======================================
  Coverage   98.14%   98.14%           
=======================================
  Files         147      147           
  Lines       11333    11344   +11     
=======================================
+ Hits        11123    11134   +11     
  Misses        210      210           
Impacted Files Coverage Δ
pennylane/qaoa/__init__.py 100.00% <100.00%> (ø)
pennylane/qaoa/cost.py 100.00% <100.00%> (ø)
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 fea7095...98d4aa6. Read the comment docs.

tests/test_qaoa.py Outdated Show resolved Hide resolved
@anthayes92 anthayes92 requested a review from trbromley May 10, 2021 12:48
@anthayes92 anthayes92 marked this pull request as ready for review May 10, 2021 16:00
@@ -19,6 +19,7 @@
import networkx as nx
import numpy as np
import pennylane as qml
from pennylane.vqe import Hamiltonian
Copy link
Contributor

Choose a reason for hiding this comment

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

Doing this prevents problems with importing into PL.

pennylane/qaoa/cost.py Outdated Show resolved Hide resolved
Comment on lines +1442 to +1465
def find_simple_cycle(list_of_edges):
"""Returns True if list_of_edges contains a permutation corresponding to a simple cycle"""
permutations = list(itertools.permutations(list_of_edges))

for edges in permutations:
if edges[0][0] != edges[-1][-1]: # check first node is equal to last node
continue
all_nodes = []
for edge in edges:
for n in edge:
all_nodes.append(n)
inner_nodes = all_nodes[
1:-1
] # find all nodes in all edges excluding the first and last nodes
nodes_out = [
inner_nodes[i] for i in range(len(inner_nodes)) if i % 2 == 0
] # find the nodes each edge is leaving
node_in = [
inner_nodes[i] for i in range(len(inner_nodes)) if i % 2 != 0
] # find the nodes each edge is entering
if nodes_out == node_in and (
len([all_nodes[0]] + nodes_out) == len(set([all_nodes[0]] + nodes_out))
): # check that each edge connect to the next via a common node and that no node is crossed more than once
return True
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this function allow for the (valid) case where two disconnected cycles are contained? I guess this case isn't possible anyway with 3 nodes 🤔

Copy link
Contributor Author

@anthayes92 anthayes92 May 10, 2021

Choose a reason for hiding this comment

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

I don't think it will since it checks the 1st and last nodes are equal, for example this disconnected pair of 2 cycles would not be classed as a valid case: disconnected = ((2,1), (1,2), (3,4), (4,3)) since
disconnected[0][0]!=disconnected[-1][-1]
But as you've pointed out, it will pick up all valid cycles for the purposes of this test.

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!

@trbromley trbromley merged commit 9abc4b2 into master May 10, 2021
@trbromley trbromley deleted the max_weight_cycle_helper_function branch May 10, 2021 20:04
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