Пример #1
0
    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))
Пример #2
0
    def test_to_adjacency_matrix(self):
        """Test to_adjacency_matrix."""
        graph = PyGraph(multigraph=False)
        graph.add_nodes_from(range(3))
        weighted_edge_list = [(0, 1, 1.0 + 1.0j), (0, 2, -1.0), (2, 2, 3)]
        graph.add_edges_from(weighted_edge_list)
        lattice = Lattice(graph)

        target_matrix = np.array([[0, 1 + 1j, -1.0], [1 - 1j, 0, 0],
                                  [-1.0, 0, 3.0]])
        assert_array_equal(lattice.to_adjacency_matrix(weighted=True),
                           target_matrix)

        target_matrix = np.array([[0, 1, 1], [1, 0, 0], [1, 0, 1]])
        assert_array_equal(lattice.to_adjacency_matrix(), target_matrix)
Пример #3
0
    def test_nonnumeric_weight_raises(self):
        """Test the initialization with a graph with non-numeric edge weights raises."""
        graph = PyGraph(multigraph=False)
        graph.add_nodes_from(range(3))
        graph.add_edges_from([(0, 1, 1), (1, 2, "banana")])

        with self.assertRaises(ValueError):
            _ = Lattice(graph)
Пример #4
0
 def test_copy(self):
     """Test test_copy."""
     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)
     lattice_copy = lattice.copy()
     self.assertTrue(
         is_isomorphic(lattice_copy.graph,
                       graph,
                       edge_matcher=lambda x, y: x == y))
Пример #5
0
    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()))
Пример #6
0
    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()))
Пример #7
0
    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)
Пример #8
0
    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))
Пример #9
0
    def test_edges_removed(self):
        """Test the initialization with a graph where edges have been removed."""
        graph = PyGraph(multigraph=False)
        graph.add_nodes_from(range(3))
        graph.add_edges_from([(0, 1, 1), (1, 2, 1)])
        graph.remove_edge_from_index(0)

        lattice = Lattice(graph)

        target_graph = PyGraph(multigraph=False)
        target_graph.add_nodes_from(range(3))
        target_graph.add_edges_from([(1, 2, 1)])

        self.assertTrue(
            is_isomorphic(lattice.graph,
                          target_graph,
                          edge_matcher=lambda x, y: x == y))
Пример #10
0
    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)
        ism = IsingModel(lattice)

        with self.subTest("Check the graph."):
            self.assertTrue(
                is_isomorphic(ism.lattice.graph,
                              lattice.graph,
                              edge_matcher=lambda x, y: x == y))

        with self.subTest("Check the coupling matrix"):
            coupling_matrix = ism.coupling_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(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),
                ("X_1", 2.0),
            ]

            ham = coupling

            self.assertSetEqual(set(ham), set(ism.second_q_ops().to_list()))