def test_non_induced_subgraph_isomorphic(self): g_a = retworkx.PyDiGraph() g_b = retworkx.PyDiGraph() nodes = g_a.add_nodes_from(["a_1", "a_2", "a_3"]) g_a.add_edges_from([ (nodes[0], nodes[1], "a_1"), (nodes[1], nodes[2], "a_2"), (nodes[2], nodes[0], "a_3"), ]) nodes = g_b.add_nodes_from(["a_1", "a_2", "a_3"]) g_b.add_edges_from([(nodes[0], nodes[1], "a_1"), (nodes[1], nodes[2], "a_2")]) for id_order in [False, True]: with self.subTest(id_order=id_order, induced=True): self.assertFalse( retworkx.is_subgraph_isomorphic(g_a, g_b, id_order=id_order, induced=True)) with self.subTest(id_order=id_order, induced=False): self.assertTrue( retworkx.is_subgraph_isomorphic(g_a, g_b, id_order=id_order, induced=False))
def test_edge_map_and_node_map_funcs_digraph_compose(self): digraph = retworkx.PyDiGraph() original_input_nodes = digraph.add_nodes_from(["qr[0]", "qr[1]"]) original_op_nodes = digraph.add_nodes_from(["h"]) output_nodes = digraph.add_nodes_from(["qr[0]", "qr[1]"]) digraph.add_edge(original_input_nodes[0], original_op_nodes[0], "qr[0]") digraph.add_edge(original_op_nodes[0], output_nodes[0], "qr[0]") # Setup other graph other_digraph = retworkx.PyDiGraph() input_nodes = other_digraph.add_nodes_from(["qr[2]", "qr[3]"]) op_nodes = other_digraph.add_nodes_from(["cx"]) other_output_nodes = other_digraph.add_nodes_from(["qr[2]", "qr[3]"]) other_digraph.add_edges_from([ (input_nodes[0], op_nodes[0], "qr[2]"), (input_nodes[1], op_nodes[0], "qr[3]"), ]) other_digraph.add_edges_from([ (op_nodes[0], other_output_nodes[0], "qr[2]"), (op_nodes[0], other_output_nodes[1], "qr[3]"), ]) def map_fn(weight): if weight == "qr[2]": return "qr[0]" elif weight == "qr[3]": return "qr[1]" else: return weight digraph.remove_nodes_from(output_nodes) other_digraph.remove_nodes_from(input_nodes) node_map = { original_op_nodes[0]: (op_nodes[0], "qr[0]"), original_input_nodes[1]: (op_nodes[0], "qr[1]"), } res = digraph.compose(other_digraph, node_map, node_map_func=map_fn, edge_map_func=map_fn) self.assertEqual({2: 4, 3: 3, 4: 5}, res) self.assertEqual(digraph[res[other_output_nodes[0]]], "qr[0]") self.assertEqual(digraph[res[other_output_nodes[1]]], "qr[1]") # qr[0] -> h self.assertTrue(digraph.has_edge(0, 2)) self.assertTrue(digraph.get_all_edge_data(0, 2), ["qr[0]"]) # qr[1] -> cx self.assertTrue(digraph.has_edge(1, 4)) self.assertTrue(digraph.get_all_edge_data(1, 4), ["qr[1]"]) # h -> cx self.assertTrue(digraph.has_edge(2, 4)) self.assertTrue(digraph.get_all_edge_data(0, 2), ["qr[0]"]) # cx -> qr[2] self.assertTrue(digraph.has_edge(4, 3)) self.assertTrue(digraph.get_all_edge_data(0, 2), ["qr[0]"]) # cx -> qr[3] self.assertTrue(digraph.has_edge(4, 5)) self.assertTrue(digraph.get_all_edge_data(0, 2), ["qr[1]"])
def test_empty_subgraph_isomorphic(self): g_a = retworkx.PyDiGraph() g_b = retworkx.PyDiGraph() for id_order in [False, True]: with self.subTest(id_order=id_order): self.assertTrue( retworkx.is_subgraph_isomorphic(g_a, g_b, id_order=id_order))
def test_digraph_non_isomorphic_rule_ins_incoming(self): graph = retworkx.PyDiGraph() graph.add_nodes_from([0, 1, 2, 3]) graph.add_edges_from_no_data([(1, 0), (2, 0), (2, 1)]) second_graph = retworkx.PyDiGraph() second_graph.add_nodes_from([0, 1, 2, 3]) second_graph.add_edges_from_no_data([(1, 0), (2, 0), (3, 1)]) self.assertFalse( retworkx.is_isomorphic(graph, second_graph, id_order=True))
def test_empty_subgraph_isomorphic_compare_nodes(self): g_a = retworkx.PyDiGraph() g_b = retworkx.PyDiGraph() for id_order in [False, True]: with self.subTest(id_order=id_order): self.assertTrue( retworkx.is_subgraph_isomorphic(g_a, g_b, lambda x, y: x == y, id_order=id_order))
def test_empty_digraph_vf2_mapping(self): g_a = retworkx.PyDiGraph() g_b = retworkx.PyDiGraph() for id_order in [False, True]: with self.subTest(id_order=id_order): mapping = retworkx.digraph_vf2_mapping(g_a, g_b, id_order=id_order, subgraph=False) self.assertEqual({}, next(mapping))
def test_digraph_non_isomorphic_edge_mismatch_self_loop(self): graph = retworkx.PyDiGraph() graph.add_nodes_from([0]) graph.add_edges_from([(0, 0, "a")]) second_graph = retworkx.PyDiGraph() second_graph.add_nodes_from([0]) second_graph.add_edges_from([(0, 0, "b")]) self.assertFalse( retworkx.is_isomorphic(graph, second_graph, edge_matcher=lambda x, y: x == y))
def test_subgraph_isomorphic_edge_matcher(self): first = retworkx.PyDiGraph() first.extend_from_weighted_edge_list([(0, 1, "a"), (1, 2, "b"), (2, 0, "c")]) second = retworkx.PyDiGraph() second.extend_from_weighted_edge_list([(0, 1, "a"), (1, 2, "b")]) self.assertTrue( retworkx.is_subgraph_isomorphic(first, second, induced=False, edge_matcher=lambda x, y: x == y))
def test_subgraph_isomorphic_mismatch_edge_data_parallel_edges(self): first = retworkx.PyDiGraph() first.extend_from_weighted_edge_list([(0, 1, "a"), (0, 1, "f"), (1, 2, "b"), (2, 0, "c")]) second = retworkx.PyDiGraph() second.extend_from_weighted_edge_list([(0, 1, "a"), (0, 1, "a"), (1, 2, "b")]) self.assertFalse( retworkx.is_subgraph_isomorphic(first, second, id_order=True, edge_matcher=lambda x, y: x == y))
def test_union_mismatch_edge_weight(self): first = retworkx.PyDiGraph() nodes = first.add_nodes_from([0, 1]) first.add_edges_from([(nodes[0], nodes[1], "a")]) second = retworkx.PyDiGraph() nodes = second.add_nodes_from([0, 1]) second.add_edges_from([(nodes[0], nodes[1], "b")]) final = retworkx.digraph_union(first, second, merge_nodes=True, merge_edges=True) self.assertEqual(final.weighted_edge_list(), [(0, 1, "a"), (0, 1, "b")])
def test_empty_directed(self): N = 5 graph = retworkx.PyDiGraph() graph.add_nodes_from([i for i in range(N)]) expected_graph = retworkx.PyDiGraph() expected_graph.extend_from_edge_list([(i, j) for i in range(N) for j in range(N) if i != j]) complement_graph = retworkx.complement(graph) self.assertTrue( retworkx.is_isomorphic( expected_graph, complement_graph, ))
def test_union_merge_all(self): dag_a = retworkx.PyDiGraph() dag_b = retworkx.PyDiGraph() node_a = dag_a.add_node("a_1") dag_a.add_child(node_a, "a_2", "e_1") dag_a.add_child(node_a, "a_3", "e_2") node_b = dag_b.add_node("a_1") dag_b.add_child(node_b, "a_2", "e_1") dag_b.add_child(node_b, "a_3", "e_2") dag_c = retworkx.digraph_union(dag_a, dag_b, True, True) self.assertTrue(retworkx.is_isomorphic(dag_a, dag_c))
def test_subgraph_isomorphic_node_count_not_ge(self): g_a = retworkx.PyDiGraph() g_b = retworkx.PyDiGraph() nodes = g_a.add_nodes_from(["a_1", "a_2"]) g_a.add_edges_from([(nodes[0], nodes[1], "a_1")]) nodes = g_b.add_nodes_from(["a_0", "a_1", "a_3"]) g_b.add_edges_from([(nodes[0], nodes[1], "a_1")]) for id_order in [False, True]: with self.subTest(id_order=id_order): self.assertFalse( retworkx.is_subgraph_isomorphic(g_a, g_b, id_order=id_order))
def test_weighted_edge_list(self): dag = retworkx.PyDiGraph() dag.add_nodes_from(list(range(4))) edge_list = [(0, 1, 'a'), (1, 2, 'b'), (0, 2, 'c'), (2, 3, 'd'), (0, 3, 'e')] dag.add_edges_from(edge_list) self.assertEqual(edge_list, dag.weighted_edge_list())
def test_edge_list(self): dag = retworkx.PyDiGraph() dag.add_nodes_from(list(range(4))) edge_list = [(0, 1, 'a'), (1, 2, 'b'), (0, 2, 'c'), (2, 3, 'd'), (0, 3, 'e')] dag.add_edges_from(edge_list) self.assertEqual([(x[0], x[1]) for x in edge_list], dag.edge_list())
def test_union_basic_merge_none(self): dag_a = retworkx.PyDiGraph() dag_b = retworkx.PyDiGraph() node_a = dag_a.add_node("a_1") dag_a.add_child(node_a, "a_2", "e_1") dag_a.add_child(node_a, "a_3", "r_2") node_b = dag_b.add_node("a_1") dag_b.add_child(node_b, "a_2", "e_1") dag_b.add_child(node_b, "a_3", "e_2") dag_c = retworkx.digraph_union(dag_a, dag_b, False, False) self.assertTrue(len(dag_c.nodes()) == 6) self.assertTrue(len(dag_c.edge_list()) == 4)
def test_directed_paper_example(self): digraph = retworkx.PyDiGraph() digraph.add_nodes_from(list(range(21))) digraph.add_edges_from_no_data(self.example_edges) res = retworkx.core_number(digraph) self.assertIsInstance(res, dict) self.assertEqual(res, self.example_core)
def _build_basis_graph(self): graph = rx.PyDiGraph() node_map = {} for key in self._get_all_keys(): name, num_qubits = key equivalences = self._get_equivalences(key) basis = frozenset(['{}/{}'.format(name, num_qubits)]) for params, decomp in equivalences: decomp_basis = frozenset('{}/{}'.format(name, num_qubits) for name, num_qubits in {(inst.name, inst.num_qubits) for inst, _, __ in decomp.data}) if basis not in node_map: basis_node = graph.add_node({'basis': basis, 'label': str(set(basis))}) node_map[basis] = basis_node if decomp_basis not in node_map: decomp_basis_node = graph.add_node({'basis': decomp_basis, 'label': str(set(decomp_basis))}) node_map[decomp_basis] = decomp_basis_node label = "%s\n%s" % ( str(params), str(decomp) if num_qubits <= 5 else '...') graph.add_edge(node_map[basis], node_map[decomp_basis], dict(label=label, fontname='Courier', fontsize=str(8))) return graph
def test_get_edge_data(self): graph = retworkx.PyDiGraph(multigraph=False) node_a = graph.add_node('a') node_b = graph.add_node('b') graph.add_edge(node_a, node_b, "Edgy") res = graph.get_edge_data(node_a, node_b) self.assertEqual("Edgy", res)
def test_remove_edge_from_index(self): graph = retworkx.PyDiGraph(multigraph=False) node_a = graph.add_node('a') node_b = graph.add_node('b') graph.add_edge(node_a, node_b, 'edgy') graph.remove_edge_from_index(0) self.assertEqual([], graph.edges())
def test_add_duplicates(self): graph = retworkx.PyDiGraph(multigraph=False) node_a = graph.add_node('a') node_b = graph.add_node('b') graph.add_edge(node_a, node_b, 'a') graph.add_edge(node_a, node_b, 'b') self.assertEqual(['b'], graph.edges())
def test_extend_from_edge_list_existing_edge(self): graph = retworkx.PyDiGraph(multigraph=False) graph.add_nodes_from(list(range(4))) edge_list = [(0, 1), (1, 2), (0, 2), (2, 3), (0, 3), (0, 1)] graph.extend_from_edge_list(edge_list) self.assertEqual(len(graph), 4) self.assertEqual([None] * 5, graph.edges())
def test_write_edge_list_empty_digraph(self): path = os.path.join(tempfile.gettempdir(), "empty.txt") graph = retworkx.PyDiGraph() graph.write_edge_list(path) self.addCleanup(os.remove, path) with open(path, "rt") as edge_file: self.assertEqual("", edge_file.read())
def test_find_cycle(self): graph = retworkx.PyDiGraph() graph.add_nodes_from(list(range(6))) graph.add_edges_from_no_data([(0, 1), (0, 3), (0, 5), (1, 2), (2, 3), (3, 4), (4, 5), (4, 0)]) res = retworkx.digraph_find_cycle(graph, 0) self.assertEqual([(0, 1), (1, 2), (2, 3), (3, 4), (4, 0)], res)
def test_extend_from_weighted_edge_list(self): graph = retworkx.PyDiGraph(multigraph=False) edge_list = [(0, 1, 'a'), (1, 2, 'b'), (0, 2, 'c'), (2, 3, 'd'), (0, 3, 'e')] graph.extend_from_weighted_edge_list(edge_list) self.assertEqual(len(graph), 4) self.assertEqual(['a', 'b', 'c', 'd', 'e'], graph.edges())
def setUp(self): self.graph = retworkx.PyDiGraph() node_a = self.graph.add_node(1) node_b = self.graph.add_node(2) self.graph.add_edge(node_a, node_b, 1) node_c = self.graph.add_node(3) self.graph.add_edge(node_a, node_c, 2)
def __init__(self, couplinglist=None, description=None): """ Create coupling graph. By default, the generated coupling has no nodes. Args: couplinglist (list or None): An initial coupling graph, specified as an adjacency list containing couplings, e.g. [[0,1], [0,2], [1,2]]. It is required that nodes are contiguously indexed starting at 0. Missed nodes will be added as isolated nodes in the coupling map. description (str): A string to describe the coupling map. """ self.description = description # the coupling map graph self.graph = rx.PyDiGraph() # a dict of dicts from node pairs to distances self._dist_matrix = None # a sorted list of physical qubits (integers) in this coupling map self._qubit_list = None # number of qubits in the graph self._size = None # a sorted list of physical qubits (integers) in this coupling map self._is_symmetric = None if couplinglist is not None: self.graph.extend_from_edge_list([tuple(x) for x in couplinglist])
def test_union_node_hole(self): first = retworkx.PyDiGraph() nodes = first.add_nodes_from([0, 1]) first.add_edges_from([(nodes[0], nodes[1], "a")]) second = retworkx.PyDiGraph() dummy = second.add_node("dummy") nodes = second.add_nodes_from([0, 1]) second.add_edges_from([(nodes[0], nodes[1], "a")]) second.remove_node(dummy) final = retworkx.digraph_union(first, second, merge_nodes=True, merge_edges=True) self.assertEqual(final.weighted_edge_list(), [(0, 1, "a")])
def test_dijkstra_path_with_no_path(self): g = retworkx.PyDiGraph() a = g.add_node('A') g.add_node('B') path = retworkx.digraph_dijkstra_shortest_paths(g, a) expected = {} self.assertEqual(expected, path)
def test_digraph_k_shortest_path_lengths(self): graph = retworkx.PyDiGraph() graph.add_nodes_from(list(range(8))) graph.add_edges_from_no_data([ (0, 1), (1, 2), (2, 3), (3, 0), (4, 5), (1, 4), (5, 6), (6, 7), (7, 5), ]) res = retworkx.digraph_k_shortest_path_lengths(graph, 1, 2, lambda _: 1) expected = { 0: 7.0, 1: 4.0, 2: 5.0, 3: 6.0, 4: 5.0, 5: 5.0, 6: 6.0, 7: 7.0, } self.assertEqual(res, expected)