def test_valid_not_path(self): G = nx.cycle_graph(4) G.add_edge(0, 4) G.add_edge(1, 4) G.add_edge(5, 2) assert_true(nx.is_perfect_matching(G, {(1, 4), (0, 3), (5, 2)}))
def mwpm(G: nx.Graph, distance_G: nx.Graph, L: int, errors: List[Node]) -> List[List[Edge]]: ''' Args - G: nx.Graph, edges are qubits - distance_G: nx.Graph - errors: list of vertices (operators) in G where errors were observed Returns - correction_paths: list of paths, each path is a list of edges in G ''' mwpm_G = nx.Graph() mwpm_G.add_nodes_from(errors) # If an odd number of faulty nodes, add a non-faulty # node to complete the matching # Will this work? paths = {} for o1 in errors: for o2 in errors: if o1 == o2: continue nqubits, path = operator_distance(G, distance_G, L, o1, o2) mwpm_G.add_edge(o1, o2, weight=1/nqubits) paths[(o1, o2)] = path paths[(o2, o1)] = path[::-1] matching = nx.max_weight_matching(mwpm_G, maxcardinality=True) assert nx.is_perfect_matching(mwpm_G, matching) correction_paths = [paths[edge] for edge in matching] return correction_paths
def setUp(self) -> None: qpu = Grid2dQPU(5, 4) self.layer = Layer(qpu) self.graph = qpu.graph matching = nx.maximal_matching(self.graph) while not nx.is_perfect_matching(self.graph, matching): self.graph = nx.random_regular_graph(4, 20) matching = nx.maximal_matching(self.graph) self.matching_gates = set() for gate in matching: self.matching_gates.add(frozenset(gate))
def generate_P0(n,names,g): P0 = [] for u,v in it.product(range(n),range(n)): num2namepoint,Nu,Nv,dim = genMunkresBasis(names,n,g,u,v) # B,M = hungarianSolve(g,num2namepoint,Nu,Nv,dim) print(u,v," perfect matching: ",nx.is_perfect_matching(B,M)) linsum = extractweight(g,u,v) for m in M:linsum+=extractweight(g,m[0][1],m[1][1]) print(dim) P0.append((u,v,linsum)) return P0
def generate_P0(n,names,g): P0 = [] for u,v in it.product(range(n),range(n)): Nu,Nv,dim = buildCostMatrix1(n,g,u,v) # B,M = hungarianSolve(g,Nu,Nv,dim) print(u,v," perfect matching: ",nx.is_perfect_matching(B,M)) linsum = extractweight(g,u,v) for m in M:linsum+=extractweight(g,m[0][1],m[1][1]) print(dim) P0.append((u,v,linsum)) return P0
def RefineByMatching(_GA, _PA, _EFA, _kA): global counter PA = _PA print("PA before", PA.edges) kA = _kA GA = _GA EFA = _EFA #global tbl # next time try #global edgeUse #counter+=1 #tbl(counter,datetime.now(),kA,len(PA.edges),len(GA.edges)) _edgeUse = {(e[0], e[1]): 0 for e in list(GA.edges)} print(list(GA.edges)) for edge in list(PA.edges)[:]: e = tuple(sorted(edge)) i = e[0] j = e[1] CostMatrix = buildCostMatrix4(i, j, GA, PA, EFA, kA) cost, deleteEdges = HungarianSolve(CostMatrix) assert (nx.is_perfect_matching(CostMatrix, deleteEdges)) deletions = [(u.name, u.parent, v.name, v.parent) for u, v in deleteEdges] for d in deletions: u = d[0] v = d[2] if cost > 2 * kA: assert (tuple(u, v) in PA.edges) PA.remove_edge(u, v) else: _d1 = tuple(sorted((d[0], d[1]))) _d2 = tuple(sorted((d[2], d[3]))) print("DELETIONS", d) if _d1 in GA.edges: print(_d1) _edgeUse[_d1] += 1 if _d2 in GA.edges: print(_d2) _edgeUse[_d2] += 1 #s = list(sorted(edgeUse.items(), key=itemgetter(1), reverse=False)) #print("Maximum:",s[0]) #print("PRINTING EDGEUSE ARRAY") #for edge in s[:25]:print(edge[1],end=", ") #print("Done!") print("PA after:", PA.edges) return _edgeUse, PA
def hungarianSolve(g, num2namepoint, Nu, Nv, dim): LeftBp, RightBp = buildCostMatrix(num2namepoint, Nu, Nv, dim) weighted_edges = [(e[0], e[1], assignweight(g, e[0][1], e[1][1])) for e in it.product(LeftBp, RightBp)] #O(n^2) B = nx.Graph() B.add_nodes_from(LeftBp, bipartite=0) B.add_nodes_from(RightBp, bipartite=1) B.add_weighted_edges_from(weighted_edges) print(nx.is_connected(B)) print(B.edges(data=True)) left, right = nx.bipartite.sets(B) pos = {} # Update position for node from each group pos.update((node, (1, index)) for index, node in enumerate(left)) pos.update((node, (2, index)) for index, node in enumerate(right)) nx.draw(B, pos=pos) plt.show() M = nx.max_weight_matching(B, maxcardinality=True) print(nx.is_perfect_matching(B, M)) return B, M
def getSubGraphs(self): """ Decompose the base graph into matchings """ G = self.base_graph subgraphs = list() # first try to get as many maximal matchings as possible for i in range(self.size - 1): M1 = nx.max_weight_matching(G) if nx.is_perfect_matching(G, M1): G.remove_edges_from(list(M1)) subgraphs.append(list(M1)) else: edge_list = list(G.edges) random.shuffle(edge_list) G.remove_edges_from(edge_list) G.add_edges_from(edge_list) # use greedy algorithm to decompose the remaining part rpart = self.decomposition(list(G.edges)) for sgraph in rpart: subgraphs.append(sgraph) return subgraphs
def test_not_matching(self): G = nx.path_graph(4) assert_false(nx.is_perfect_matching(G, {(0, 1), (1, 2), (2, 3)}))
def test_maximal_but_not_perfect(self): G = nx.cycle_graph(4) G.add_edge(0, 4) G.add_edge(1, 4) assert_false(nx.is_perfect_matching(G, {(1, 4), (0, 3)}))
def test_dict(self): G = nx.path_graph(4) assert_true(nx.is_perfect_matching(G, {0: 1, 1: 0, 2: 3, 3: 2}))
def test_valid(self): G = nx.path_graph(4) assert_true(nx.is_perfect_matching(G, {(0, 1), (2, 3)}))