def test_random_graph_different_dtype_astype_no_copy(self): input_matrix = np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]], dtype=np.int64) graph = retworkx.PyGraph.from_adjacency_matrix( input_matrix.astype(np.float64, copy=False)) adj_matrix = retworkx.graph_adjacency_matrix(graph, lambda x: x) self.assertTrue(np.array_equal(adj_matrix, input_matrix))
def test_from_adjacency_matrix(self): input_array = np.array( [[0.0, 4.0, 0.0], [4.0, 0.0, 4.0], [0.0, 4.0, 0.0]], dtype=np.float64) graph = retworkx.PyGraph.from_adjacency_matrix(input_array) out_array = retworkx.graph_adjacency_matrix(graph, lambda x: x) self.assertTrue(np.array_equal(input_array, out_array))
def test_docplex_maxcut(self): """ Docplex maxcut test """ # Generating a graph of 4 nodes n = 4 graph = rx.PyGraph() graph.add_nodes_from(np.arange(0, n, 1).tolist()) elist = [(0, 1, 1.0), (0, 2, 1.0), (0, 3, 1.0), (1, 2, 1.0), (2, 3, 1.0)] graph.add_edges_from(elist) # Computing the weight matrix from the random graph w = rx.graph_adjacency_matrix(graph, lambda weight: weight) # Create an Ising Hamiltonian with docplex. mdl = Model(name='max_cut') mdl.node_vars = mdl.binary_var_list(list(range(4)), name='node') maxcut_func = mdl.sum(w[i, j] * mdl.node_vars[i] * (1 - mdl.node_vars[j]) for i in range(n) for j in range(n)) mdl.maximize(maxcut_func) qubit_op, offset = docplex.get_operator(mdl) e_e = NumPyMinimumEigensolver(qubit_op) result = e_e.run() ee_expected = NumPyMinimumEigensolver(QUBIT_OP_MAXCUT) expected_result = ee_expected.run() # Compare objective self.assertAlmostEqual(result.eigenvalue.real + offset, expected_result.eigenvalue.real + OFFSET_MAXCUT)
def test_float_cast_weight_func(self): graph = retworkx.PyGraph() node_a = graph.add_node("a") node_b = graph.add_node("b") graph.add_edge(node_a, node_b, 7.0) res = retworkx.graph_adjacency_matrix(graph, lambda x: float(x)) self.assertIsInstance(res, np.ndarray) self.assertTrue(np.array_equal(np.array([[0.0, 7.0], [7.0, 0.0]]), res))
def test_negative_weight(self): input_matrix = np.array( [[0, -1, 0], [-1, 0, -1], [0, -1, 0]], dtype=float ) graph = retworkx.PyGraph.from_adjacency_matrix(input_matrix) adj_matrix = retworkx.graph_adjacency_matrix(graph, lambda x: x) self.assertTrue(np.array_equal(adj_matrix, input_matrix)) self.assertEqual([(0, 1, -1), (1, 2, -1)], graph.weighted_edge_list())
def test_graph_with_index_holes(self): graph = retworkx.PyGraph() node_a = graph.add_node("a") node_b = graph.add_node("b") graph.add_edge(node_a, node_b, 1) node_c = graph.add_node("c") graph.add_edge(node_a, node_c, 1) graph.remove_node(node_b) res = retworkx.graph_adjacency_matrix(graph, lambda x: 1) self.assertIsInstance(res, np.ndarray) self.assertTrue(np.array_equal(np.array([[0, 1], [1, 0]]), res))
def test_multigraph_sum_cast_weight_func_non_zero_null(self): graph = retworkx.PyGraph() node_a = graph.add_node("a") node_b = graph.add_node("b") graph.add_edge(node_a, node_b, 7.0) graph.add_edge(node_a, node_b, 0.5) res = retworkx.graph_adjacency_matrix( graph, lambda x: float(x), null_value=np.inf ) self.assertIsInstance(res, np.ndarray) self.assertTrue( np.array_equal(np.array([[np.inf, 7.5], [7.5, np.inf]]), res) )
def test_single_neighbor(self): graph = retworkx.PyGraph() node_a = graph.add_node('a') node_b = graph.add_node('b') graph.add_edge(node_a, node_b, 'edge_a') node_c = graph.add_node('c') graph.add_edge(node_b, node_c, 'edge_b') res = retworkx.graph_adjacency_matrix(graph, lambda x: 1) self.assertIsInstance(res, np.ndarray) self.assertTrue( np.array_equal( np.array([[0.0, 1.0, 0.0], [1.0, 0.0, 1.0], [0.0, 1.0, 0.0]], dtype=np.float64), res))
def test_default_weight(self): graph = retworkx.PyGraph() node_a = graph.add_node('a') node_b = graph.add_node('b') graph.add_edge(node_a, node_b, 'edge_a') node_c = graph.add_node('c') graph.add_edge(node_b, node_c, 'edge_b') res = retworkx.graph_adjacency_matrix(graph, default_weight=4) self.assertIsInstance(res, np.ndarray) self.assertTrue( np.array_equal( np.array([[0.0, 4.0, 0.0], [4.0, 0.0, 4.0], [0.0, 4.0, 0.0]], dtype=np.float64), res))
def test_no_weight_fn(self): graph = retworkx.PyGraph() node_a = graph.add_node("a") node_b = graph.add_node("b") graph.add_edge(node_a, node_b, "edge_a") node_c = graph.add_node("c") graph.add_edge(node_b, node_c, "edge_b") res = retworkx.graph_adjacency_matrix(graph) self.assertIsInstance(res, np.ndarray) self.assertTrue( np.array_equal( np.array( [[0.0, 1.0, 0.0], [1.0, 0.0, 1.0], [0.0, 1.0, 0.0]], dtype=np.float64, ), res, ))
def test_random_graph_float_dtype(self): input_matrix = np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]], dtype=float) graph = retworkx.PyGraph.from_adjacency_matrix(input_matrix) adj_matrix = retworkx.graph_adjacency_matrix(graph, lambda x: x) self.assertTrue(np.array_equal(adj_matrix, input_matrix))
def test_random_graph_full_path(self): graph = retworkx.undirected_gnp_random_graph(100, 0.95, seed=42) adjacency_matrix = retworkx.graph_adjacency_matrix(graph) new_graph = retworkx.PyGraph.from_adjacency_matrix(adjacency_matrix) new_adjacency_matrix = retworkx.graph_adjacency_matrix(new_graph) self.assertTrue(np.array_equal(adjacency_matrix, new_adjacency_matrix))
def test_no_edge_graph_adjacency_matrix(self): graph = retworkx.PyGraph() for i in range(50): graph.add_node(i) res = retworkx.graph_adjacency_matrix(graph, lambda x: 1) self.assertTrue(np.array_equal(np.zeros([50, 50]), res))
width = initial_width * pow(2, i) for u, v in nx.bfs_beam_edges(a, s, v, width): if con(v): return v print(nx.NodeNotFound("no node")) def has_high_centrality(v: int) -> bool: return centrality[v] >= avg_centrality seed: int = 89 # conversion of retworkx graph to networkx couldn't find any func in retworkx to calculate centrality G: rx.PyGraph = rx.undirected_gnp_random_graph(100, 0.5, seed=seed) n1: numpy.ndarray = rx.graph_adjacency_matrix(G, None) G1: nx.classes.graph.Graph = nx.from_numpy_matrix(n1) centrality: dict = nx.eigenvector_centrality(G1) avg_centrality: float = sum(centrality.values()) / len(G1) source: int = 0 value = centrality.get print(type(value)) condition: Callable[[int], bool] = has_high_centrality found_node: int = progressive_widening_search(G1, source, value, condition) c: float = centrality[found_node] print(f"found node {found_node} with centrality {c}") # plotting pos: dict = nx.spring_layout(G1, seed=seed)
G: rx.PyGraph = rx.generators.grid_graph(5, 5, None) dot: Dot = pydot.graph_from_dot_data(G.to_dot())[0] # plotting retworkx not working finding an alternative method """ with tempfile.TemporaryDirectory() as tmpdirname: tmp_path = os.path.join(tmpdirname, 'dag.png') dot.write_png(tmp_path) image = Image.open(tmp_path) os.remove(tmp_path) image """ M: ndarray = rx.graph_adjacency_matrix(G, None) AdjList: defaultdict = convert(M) a: EdgeList = G.edge_list() with tempfile.NamedTemporaryFile('wt') as fd: path: str = fd.name for i in a: fd.write(str(i[0]) + ' ' + str(i[1]) + '\n') fd.flush() H: rx.PyGraph = rx.PyGraph() H.read_edge_list(path) dot = pydot.graph_from_dot_data(H.to_dot())[0] """ with tempfile.NamedTemporaryFile('wt') as fd: tmp_path = fd.name
def peakmem_adjacency_matrix(self, _, __): retworkx.graph_adjacency_matrix(self.graph, weight_fn=float)