-
Notifications
You must be signed in to change notification settings - Fork 575
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
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1283 +/- ##
=======================================
Coverage 98.14% 98.14%
=======================================
Files 147 147
Lines 11333 11344 +11
=======================================
+ Hits 11123 11134 +11
Misses 210 210
Continue to review full report at Codecov.
|
@@ -19,6 +19,7 @@ | |||
import networkx as nx | |||
import numpy as np | |||
import pennylane as qml | |||
from pennylane.vqe import Hamiltonian |
There was a problem hiding this comment.
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.
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 |
There was a problem hiding this comment.
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 🤔
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @anthayes92!
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.