Skip to content

Commit

Permalink
Added bipartite to chimera mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
franklee26 committed May 23, 2019
1 parent a9a18c1 commit 6ae94ac
Showing 1 changed file with 106 additions and 5 deletions.
111 changes: 106 additions & 5 deletions Embed.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import matplotlib.pyplot as plt
import networkx as nx
import dwave_networkx as dnx
import math
import random

# Custom exceptions
Expand Down Expand Up @@ -118,16 +120,46 @@ def greedyIndSet(self, *, graph = None):
discards += N
return answer

def greedyBipartite(self):
def greedyBipartiteSets(self, *, getOCT = None):
assert(getOCT == False or getOCT == True or getOCT == None), "getOCT must be a boolean value."
L = self.greedyIndSet(graph = self.theGraph)
stahp = []
for x,y in self.theGraph:
if (x not in L) and (y not in L):
stahp.append((x,y))
R = self.greedyIndSet(graph = stahp)
if getOCT == True:
OCT = []
G = nx.Graph()
G.add_nodes_from([x for x,y in self.theGraph] + [y for x,y in self.theGraph])
G.add_edges_from(self.theGraph)
for nodes in G.nodes():
if nodes not in L and nodes not in R:
OCT.append(nodes)
return L,R,OCT
return L,R

def greedyBipartiteGraph(self):
L = self.greedyIndSet(graph = self.theGraph)
stahp = []
for x,y in self.theGraph:
if (x not in L) and (y not in L):
stahp.append((x,y))
R = self.greedyIndSet(graph = stahp)
answer = []
# build a graph for nodes
for x,y in self.theGraph:
if ((x not in L) and (x not in R)) or ((y not in L) and (y not in R)):
# OCT
pass
else:
answer.append((x,y))
return answer

def plotOCTDivision(self, *, left = None, right = None, removeOCT = None):
assert(left != None and right != None), "Must provide left and right sets."
assert(removeOCT == False or removeOCT == True or removeOCT == None), "removeOCT must be a boolean value."

if len(self.theGraph) == 0:
raise emptyGraphException()

Expand Down Expand Up @@ -155,13 +187,82 @@ def plotOCTDivision(self, *, left = None, right = None, removeOCT = None):
nx.draw(G, with_labels = True, font_weight = "bold", node_color = colorMap)
plt.show()

def plotChimera(self, l, m, n, *, biPartite = None):
assert(biPartite != None and len(biPartite) >= 2), "A bipartite graph w/ >= 2 nodes must be provided."
if len(self.theGraph) == 0:
raise emptyGraphException()

H = nx.Graph()

H.add_nodes_from([x for x,y in biPartite] + [y for x,y in biPartite])
H.add_edges_from(biPartite)

# need to find correct dimensions
# first need L and R sizes
# assuming this bipartite set is the one provided in the args
LSet, RSet, OCTSet = self.greedyBipartiteSets(getOCT = True)
left, right, OCT = len(LSet), len(RSet), len(OCTSet)

G = dnx.chimera_graph(l,m,n)
dnx.draw_chimera(G)

# abstracting built in exceptions
try:
dnx.draw_chimera(H, node_color='r', style='dashed', edge_color='r', width=3, with_labels = True, font_weight = "bold")
except:
print("QEmbed Error: provided graph is not bi-partite. Exiting...")
return

plt.show()

# given a bipartite graph, plot the chimera graph
def plotChimeraFromBipartite(self, showMappings = False, L = 2, M = 2, N = 2, *, left = [], right = []):
assert(left != None and right != None), "Must provide non-empty left and right sets."
assert(L >= 1 and M >= 1 and N >= 1), "Chimera L,M,N topologies must be at least 1."
answer = []
labelDict = {}
# Mapping bipartite sets. First left set:
for i in range(0, len(left)):
for j in range(0,M):
t = (j, math.ceil(i/L), 0, (i%L)-1) if (i%L)-1 >= 0 else (j, math.ceil(i/L), 0, 0)
answer.append(t)
if (showMappings == True):
print("Mapping v{} to {}.".format(i+1, t))
labelDict[t] = "v{}".format(i+1)
# then right set
for i in range(0, len(right)):
for j in range(0,N):
t = (math.ceil(i/L), j, 1, (i%L)-1) if (i%L)-1 >= 0 else (math.ceil(i/L), j, 1, 0)
answer.append(t)
if (showMappings == True):
print("Mapping h{} to {}.".format(i+1, t))
labelDict[t] = "h{}".format(i+1)

# L,M,N = 2,2,2
# for keys in labelDict:
# if keys[0] + 1 > L: L = keys[0] + 1
# if keys[1] + 1 > M: M = keys[1] + 1
# if keys[3] + 1 > N: N = keys[3] + 1

G = dnx.chimera_graph(L,M,N)
dnx.draw_chimera(G)

x = dnx.generators.chimera_graph(N,L,M, node_list = answer, coordinates = True)
dnx.draw_chimera(x, node_color = "red", labels = labelDict, with_labels = True, edge_color = "red", font_weight = "bold", font_size = "medium")
plt.show()

def clearGraph(self):
self.theGraph = []

################################################################################################################################

if __name__ == "__main__":
e = Embedding(graph = [(1,2),(1,5),(1,3),(2,4),(2,5),(3,5),(3,4)])
#e.generateRandomGraph(10, 25)
L,R = e.greedyBipartite()
e.plotOCTDivision(left = L, right = R, removeOCT = True)
e = Embedding(graph = [(1,2),(1,5),(1,3),(1,4),(2,4),(2,5),(3,5),(3,4)])

#e = Embedding()
#e.generateRandomGraph(20, 70)
L,R,OCT = e.greedyBipartiteSets(getOCT = True)
biG = e.greedyBipartiteGraph()
e.plotChimeraFromBipartite(left= L, right = R)
#e.plotChimera(2,2,2,biPartite = biG);
#e.plotOCTDivision(left = L, right = R, removeOCT = True)

0 comments on commit 6ae94ac

Please sign in to comment.