def test_empty_isomorphic(self): g_a = retworkx.PyGraph() g_b = retworkx.PyGraph() for id_order in [False, True]: with self.subTest(id_order=id_order): self.assertTrue( retworkx.is_isomorphic(g_a, g_b, id_order=id_order))
def test_from_nodes_and_edges(self): """Test from_nodes_edges.""" graph = PyGraph(multigraph=False) graph.add_nodes_from(range(6)) weighted_edge_list = [ (0, 1, 1.0 + 1.0j), (0, 2, -1.0), (2, 3, 2.0), (4, 2, -1.0), (4, 4, 3.0), (2, 5, -1.0), ] graph.add_edges_from(weighted_edge_list) lattice = Lattice(graph) target_num_nodes = 6 target_weighted_edge_list = [ (2, 5, -1.0), (4, 4, 3), (4, 2, -1.0), (2, 3, 2.0), (0, 2, -1.0), (0, 1, 1.0 + 1.0j), ] target_lattice = Lattice.from_nodes_and_edges( target_num_nodes, target_weighted_edge_list) self.assertTrue( is_isomorphic(lattice.graph, target_lattice.graph, edge_matcher=lambda x, y: x == y))
def test_from_parameters(self): """Test from_parameters.""" coupling_matrix = np.array([[1.0, 1.0 + 1.0j, 2.0 - 2.0j], [1.0 - 1.0j, 0.0, 0.0], [2.0 + 2.0j, 0.0, 1.0]]) ism = cast(IsingModel, IsingModel.from_parameters(coupling_matrix)) with self.subTest("Check the graph."): target_graph = PyGraph(multigraph=False) target_graph.add_nodes_from(range(3)) target_weight = [(0, 0, 1.0), (0, 1, 1.0 + 1.0j), (0, 2, 2.0 - 2.0j), (2, 2, 1.0)] target_graph.add_edges_from(target_weight) self.assertTrue( is_isomorphic(ism.lattice.graph, target_graph, edge_matcher=lambda x, y: x == y)) with self.subTest("Check the coupling matrix."): assert_array_equal(ism.coupling_matrix(), coupling_matrix) with self.subTest("Check the second q op representation."): coupling = [ ("Z_0 Z_1", 1.0 + 1.0j), ("Z_0 Z_2", 2.0 - 2.0j), ("X_0", 1.0), ("X_2", 1.0), ] ham = coupling self.assertSetEqual(set(ham), set(ism.second_q_ops().to_list()))
def test_empty_isomorphic(self): dag_a = retworkx.PyDAG() dag_b = retworkx.PyDAG() for id_order in [False, True]: with self.subTest(id_order=id_order): self.assertTrue( retworkx.is_isomorphic(dag_a, dag_b, id_order=id_order))
def test_isomorphic_parallel_edges_with_edge_matcher(self): graph = retworkx.PyGraph() graph.extend_from_weighted_edge_list([(0, 1, "a"), (0, 1, "b"), (1, 2, "c")]) self.assertTrue( retworkx.is_isomorphic(graph, graph, edge_matcher=lambda x, y: x == y))
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_isomorphic_compare_nodes(self): g_a = retworkx.PyGraph() g_b = retworkx.PyGraph() for id_order in [False, True]: with self.subTest(id_order=id_order): self.assertTrue( retworkx.is_isomorphic(g_a, g_b, lambda x, y: x == y, id_order=id_order))
def test_isomorphic_node_count_not_equal(self): g_a = retworkx.PyGraph() g_b = retworkx.PyGraph() nodes = g_a.add_nodes_from(['a_1', 'a_2', 'a_3']) g_a.add_edges_from([(nodes[0], nodes[1], 'a_1')]) nodes = g_b.add_nodes_from(['a_0', 'a_1']) g_b.add_edges_from([(nodes[0], nodes[1], 'a_1')]) g_b.remove_node(nodes[0]) self.assertFalse(retworkx.is_isomorphic(g_a, g_b))
def test_uniform_parameters(self): """Test uniform_parameters.""" graph = PyGraph(multigraph=False) graph.add_nodes_from(range(3)) weighted_edge_list = [ (0, 1, 1.0 + 1.0j), (0, 2, -1.0), (1, 1, 2.0), ] graph.add_edges_from(weighted_edge_list) lattice = Lattice(graph) uniform_ism = cast( IsingModel, IsingModel.uniform_parameters( lattice, uniform_interaction=1.0 + 1.0j, uniform_onsite_potential=0.0, ), ) with self.subTest("Check the graph."): target_graph = PyGraph(multigraph=False) target_graph.add_nodes_from(range(3)) target_weight = [ (0, 1, 1.0 + 1.0j), (0, 2, 1.0 + 1.0j), (0, 0, 0.0), (1, 1, 0.0), (2, 2, 0.0), ] target_graph.add_edges_from(target_weight) self.assertTrue( is_isomorphic(uniform_ism.lattice.graph, target_graph, edge_matcher=lambda x, y: x == y)) with self.subTest("Check the coupling matrix."): coupling_matrix = uniform_ism.coupling_matrix() target_matrix = np.array([[0.0, 1.0 + 1.0j, 1.0 + 1.0j], [1.0 - 1.0j, 0.0, 0.0], [1.0 - 1.0j, 0.0, 0.0]]) assert_array_equal(coupling_matrix, target_matrix) with self.subTest("Check the second q op representation."): coupling = [ ("Z_0 Z_1", 1.0 + 1.0j), ("Z_0 Z_2", 1.0 + 1.0j), ("X_0", 0.0), ("X_1", 0.0), ("X_2", 0.0), ] ham = coupling self.assertSetEqual(set(ham), set(uniform_ism.second_q_ops().to_list()))
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_default_weight(self): weightless_graph = retworkx.PyGraph() weightless_graph.extend_from_edge_list([ (0, 1), (0, 2), (0, 3), (0, 4), (1, 5), (2, 6), (3, 7), (4, 8) ]) # MST of the graph is itself mst_graph_default_weight = retworkx.minimum_spanning_tree( weightless_graph) mst_graph_weight_2 = retworkx.minimum_spanning_tree(weightless_graph, default_weight=2.0) self.assertTrue( retworkx.is_isomorphic( weightless_graph, mst_graph_default_weight, )) self.assertTrue( retworkx.is_isomorphic( weightless_graph, mst_graph_weight_2, ))
def test_isomorphic_mismatch_node_data_undirected(self): g_a = retworkx.PyGraph() g_b = retworkx.PyGraph() 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 = g_b.add_nodes_from(['b_1', 'b_2', 'b_3']) g_b.add_edges_from([(nodes[0], nodes[1], 'b_1'), (nodes[1], nodes[2], 'b_2')]) self.assertTrue(retworkx.is_isomorphic(g_a, g_b))
def test_isomorphic_compare_nodes_identical_undirected(self): g_a = retworkx.PyGraph() g_b = retworkx.PyGraph() 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 = 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')]) self.assertTrue(retworkx.is_isomorphic(g_a, g_b, lambda x, y: x == y))
def test_isomorphic_mismatch_node_data(self): dag_a = retworkx.PyDAG() dag_b = retworkx.PyDAG() node_a = dag_a.add_node('a_1') dag_a.add_child(node_a, 'a_2', 'a_1') dag_a.add_child(node_a, 'a_3', 'a_2') node_b = dag_b.add_node('b_1') dag_b.add_child(node_b, 'b_2', 'b_1') dag_b.add_child(node_b, 'b_3', 'b_2') self.assertTrue(retworkx.is_isomorphic(dag_a, dag_b))
def test_isomorphic_identical(self): dag_a = retworkx.PyDAG() dag_b = retworkx.PyDAG() node_a = dag_a.add_node('a_1') dag_a.add_child(node_a, 'a_2', 'a_1') dag_a.add_child(node_a, 'a_3', 'a_2') node_b = dag_b.add_node('a_1') dag_b.add_child(node_b, 'a_2', 'a_1') dag_b.add_child(node_b, 'a_3', 'a_2') self.assertTrue(retworkx.is_isomorphic(dag_a, dag_b))
def test_isomorphic_compare_nodes_mismatch_node_data(self): dag_a = retworkx.PyDAG() dag_b = retworkx.PyDAG() node_a = dag_a.add_node('a_1') dag_a.add_child(node_a, 'a_2', 'a_1') dag_a.add_child(node_a, 'a_3', 'a_2') node_b = dag_b.add_node('b_1') dag_b.add_child(node_b, 'b_2', 'b_1') dag_b.add_child(node_b, 'b_3', 'b_2') self.assertFalse( retworkx.is_isomorphic(dag_a, dag_b, lambda x, y: x == y))
def test_multigraph(self): graph = retworkx.PyGraph(multigraph=True) graph.extend_from_edge_list([(0, 0), (0, 1), (1, 1), (2, 2), (1, 0)]) expected_graph = retworkx.PyGraph(multigraph=True) expected_graph.extend_from_edge_list([(0, 2), (1, 2)]) complement_graph = retworkx.complement(graph) self.assertTrue( retworkx.is_isomorphic( expected_graph, complement_graph, ))
def test_init(self): """Test init.""" graph = PyGraph(multigraph=False) graph.add_nodes_from(range(6)) weighted_edge_list = [ (0, 1, 1.0 + 1.0j), (0, 2, -1.0), (2, 3, 2.0), (2, 4, -1.0), (4, 4, 3.0), (2, 5, -1.0), ] graph.add_edges_from(weighted_edge_list) lattice = Lattice(graph) with self.subTest("Check the type of lattice."): self.assertIsInstance(lattice, Lattice) with self.subTest("Check graph."): target_graph = PyGraph(multigraph=False) target_graph.add_nodes_from(range(6)) target_weighted_edge_list = [ (4, 4, 3.0), (0, 1, 1 + 1j), (2, 3, 2.0), (2, 4, -1.0), (2, 5, -1.0), (0, 2, -1), ] target_graph.add_edges_from(target_weighted_edge_list) self.assertTrue( is_isomorphic(lattice.graph, target_graph, edge_matcher=lambda x, y: x == y)) with self.subTest("Check the number of nodes."): self.assertEqual(lattice.num_nodes, 6) with self.subTest("Check the set of nodes."): self.assertSetEqual(set(lattice.node_indexes), set(range(6))) with self.subTest("Check the set of weights."): target_set = { (0, 1, 1 + 1j), (4, 4, 3), (2, 5, -1.0), (0, 2, -1.0), (2, 3, 2.0), (2, 4, -1.0), } self.assertEqual(set(lattice.weighted_edge_list), target_set)
def test_init(self): """Test init.""" graph = PyGraph(multigraph=False) graph.add_nodes_from(range(3)) weighted_edge_list = [ (0, 1, 1.0 + 1.0j), (0, 2, -1.0), (1, 1, 2.0), ] graph.add_edges_from(weighted_edge_list) lattice = Lattice(graph) fhm = FermiHubbardModel(lattice, onsite_interaction=10.0) with self.subTest("Check the graph."): self.assertTrue( is_isomorphic(fhm.lattice.graph, lattice.graph, edge_matcher=lambda x, y: x == y)) with self.subTest("Check the hopping matrix"): hopping_matrix = fhm.hopping_matrix() target_matrix = np.array([[0.0, 1.0 + 1.0j, -1.0], [1.0 - 1.0j, 2.0, 0.0], [-1.0, 0.0, 0.0]]) assert_array_equal(hopping_matrix, target_matrix) with self.subTest("Check the second q op representation."): hopping = [ ("+_0 -_2", 1.0 + 1.0j), ("-_0 +_2", -(1.0 - 1.0j)), ("+_0 -_4", -1.0), ("-_0 +_4", 1.0), ("+_1 -_3", 1.0 + 1.0j), ("-_1 +_3", -(1.0 - 1.0j)), ("+_1 -_5", -1.0), ("-_1 +_5", 1.0), ("+_2 -_2", 2.0), ("+_3 -_3", 2.0), ] interaction = [ ("+_0 -_0 +_1 -_1", 10.0), ("+_2 -_2 +_3 -_3", 10.0), ("+_4 -_4 +_5 -_5", 10.0), ] ham = hopping + interaction self.assertSetEqual( set(ham), set(fhm.second_q_ops(display_format="sparse").to_list()))
def test_isomorphic_node_count_not_equal(self): g_a = retworkx.PyGraph() g_b = retworkx.PyGraph() nodes = g_a.add_nodes_from(["a_1", "a_2", "a_3"]) g_a.add_edges_from([(nodes[0], nodes[1], "a_1")]) nodes = g_b.add_nodes_from(["a_0", "a_1"]) g_b.add_edges_from([(nodes[0], nodes[1], "a_1")]) g_b.remove_node(nodes[0]) for id_order in [False, True]: with self.subTest(id_order=id_order): self.assertFalse( retworkx.is_isomorphic(g_a, g_b, id_order=id_order))
def test_from_parameters(self): """Test from_parameters.""" hopping_matrix = np.array([[1.0, 1.0 + 1.0j, 2.0 + 2.0j], [1.0 - 1.0j, 0.0, 0.0], [2.0 - 2.0j, 0.0, 1.0]]) onsite_interaction = 10.0 fhm = FermiHubbardModel.from_parameters(hopping_matrix, onsite_interaction) with self.subTest("Check the graph."): target_graph = PyGraph(multigraph=False) target_graph.add_nodes_from(range(3)) target_weight = [(0, 0, 1.0), (0, 1, 1.0 + 1.0j), (0, 2, 2.0 + 2.0j), (2, 2, 1.0)] target_graph.add_edges_from(target_weight) self.assertTrue( is_isomorphic(fhm.lattice.graph, target_graph, edge_matcher=lambda x, y: x == y)) with self.subTest("Check the hopping matrix."): assert_array_equal(fhm.hopping_matrix(), hopping_matrix) with self.subTest("Check the second q op representation."): hopping = [ ("+_0 -_2", 1.0 + 1.0j), ("-_0 +_2", -(1.0 - 1.0j)), ("+_0 -_4", 2.0 + 2.0j), ("-_0 +_4", -(2.0 - 2.0j)), ("+_1 -_3", 1.0 + 1.0j), ("-_1 +_3", -(1.0 - 1.0j)), ("+_1 -_5", 2.0 + 2.0j), ("-_1 +_5", -(2.0 - 2.0j)), ("+_0 -_0", 1.0), ("+_1 -_1", 1.0), ("+_4 -_4", 1.0), ("+_5 -_5", 1.0), ] interaction = [ ("+_0 -_0 +_1 -_1", onsite_interaction), ("+_2 -_2 +_3 -_3", onsite_interaction), ("+_4 -_4 +_5 -_5", onsite_interaction), ] ham = hopping + interaction self.assertSetEqual( set(ham), set(fhm.second_q_ops(display_format="sparse").to_list()))
def test_isomorphic_mismatch_node_data(self): dag_a = retworkx.PyDAG() dag_b = retworkx.PyDAG() node_a = dag_a.add_node("a_1") dag_a.add_child(node_a, "a_2", "a_1") dag_a.add_child(node_a, "a_3", "a_2") node_b = dag_b.add_node("b_1") dag_b.add_child(node_b, "b_2", "b_1") dag_b.add_child(node_b, "b_3", "b_2") for id_order in [False, True]: with self.subTest(id_order=id_order): self.assertTrue( retworkx.is_isomorphic(dag_a, dag_b, id_order=id_order))
def test_isomorphic_removed_nodes_in_second_graph(self): g_a = retworkx.PyGraph() g_b = retworkx.PyGraph() 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 = g_b.add_nodes_from(['a_0', 'a_2', 'a_1', 'a_3']) g_b.add_edges_from([(nodes[0], nodes[1], 'e_01'), (nodes[0], nodes[3], 'e_03'), (nodes[2], nodes[1], 'a_1'), (nodes[1], nodes[3], 'a_2')]) g_b.remove_node(nodes[0]) self.assertTrue(retworkx.is_isomorphic(g_a, g_b, lambda x, y: x == y))
def test_isomorphic_compare_edges_identical(self): dag_a = retworkx.PyDAG() dag_b = retworkx.PyDAG() node_a = dag_a.add_node('a_1') dag_a.add_child(node_a, 'a_2', 'a_1') dag_a.add_child(node_a, 'a_3', 'a_2') node_b = dag_b.add_node('a_1') dag_b.add_child(node_b, 'a_2', 'a_1') dag_b.add_child(node_b, 'a_3', 'a_2') self.assertTrue( retworkx.is_isomorphic(dag_a, dag_b, edge_matcher=lambda x, y: x == y))
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_isomorphic_mismatch_node_data(self): g_a = retworkx.PyGraph() g_b = retworkx.PyGraph() 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 = g_b.add_nodes_from(["b_1", "b_2", "b_3"]) g_b.add_edges_from([(nodes[0], nodes[1], "b_1"), (nodes[1], nodes[2], "b_2")]) for id_order in [False, True]: with self.subTest(id_order=id_order): self.assertTrue( retworkx.is_isomorphic(g_a, g_b, id_order=id_order))
def test_empty(self): N = 5 graph = retworkx.PyGraph() graph.add_nodes_from([i for i in range(N)]) expected_graph = retworkx.PyGraph() 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_from_networkx(self): """Test initialization from a networkx graph.""" graph = nx.Graph() graph.add_nodes_from(range(5)) graph.add_edges_from([(i, i + 1) for i in range(4)]) lattice = Lattice(graph) target_graph = PyGraph() target_graph.add_nodes_from(range(5)) target_graph.add_edges_from([(i, i + 1, 1) for i in range(4)]) self.assertTrue( is_isomorphic(lattice.graph, target_graph, edge_matcher=lambda x, y: x == y))
def test_graph(self): """Test if analytically derived SyndromeGraph is correct.""" error = ( "Error: The analytical SyndromeGraph does not coincide " + "with the brute force SyndromeGraph in d=7, T=2 RepetitionCode.") code = RepetitionCode(7, 2) graph_new = GraphDecoder(code, brute=False).S graph_old = GraphDecoder(code, brute=True).S test_passed = True for node in graph_new.nodes(): test_passed &= node in graph_old.nodes() for node in graph_old.nodes(): test_passed &= node in graph_new.nodes() test_passed &= rx.is_isomorphic(graph_new, graph_old, lambda x, y: x == y) self.assertTrue(test_passed, error)
def test_isomorphic_compare_nodes_with_removals_deepcopy(self): dag_a = retworkx.PyDAG() dag_b = retworkx.PyDAG() qr_0_in = dag_a.add_node("qr[0]") qr_1_in = dag_a.add_node("qr[1]") cr_0_in = dag_a.add_node("cr[0]") qr_0_out = dag_a.add_node("qr[0]") qr_1_out = dag_a.add_node("qr[1]") cr_0_out = dag_a.add_node("qr[0]") cu1 = dag_a.add_child(qr_0_in, "cu1", "qr[0]") dag_a.add_edge(qr_1_in, cu1, "qr[1]") measure_0 = dag_a.add_child(cr_0_in, "measure", "cr[0]") dag_a.add_edge(cu1, measure_0, "qr[0]") measure_1 = dag_a.add_child(cu1, "measure", "qr[1]") dag_a.add_edge(measure_0, measure_1, "cr[0]") dag_a.add_edge(measure_1, qr_1_out, "qr[1]") dag_a.add_edge(measure_1, cr_0_out, "cr[0]") dag_a.add_edge(measure_0, qr_0_out, "qr[0]") dag_a.remove_node(cu1) dag_a.add_edge(qr_0_in, measure_0, "qr[0]") dag_a.add_edge(qr_1_in, measure_1, "qr[1]") qr_0_in = dag_b.add_node("qr[0]") qr_1_in = dag_b.add_node("qr[1]") cr_0_in = dag_b.add_node("cr[0]") qr_0_out = dag_b.add_node("qr[0]") qr_1_out = dag_b.add_node("qr[1]") cr_0_out = dag_b.add_node("qr[0]") measure_0 = dag_b.add_child(cr_0_in, "measure", "cr[0]") dag_b.add_edge(qr_0_in, measure_0, "qr[0]") measure_1 = dag_b.add_child(qr_1_in, "measure", "qr[1]") dag_b.add_edge(measure_1, qr_1_out, "qr[1]") dag_b.add_edge(measure_1, cr_0_out, "cr[0]") dag_b.add_edge(measure_0, measure_1, "cr[0]") dag_b.add_edge(measure_0, qr_0_out, "qr[0]") for id_order in [False, True]: with self.subTest(id_order=id_order): self.assertTrue( retworkx.is_isomorphic( copy.deepcopy(dag_a), copy.deepcopy(dag_b), lambda x, y: x == y, id_order=id_order, ) )