Esempio n. 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))
Esempio n. 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)
Esempio n. 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)
Esempio n. 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))
    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()))
Esempio n. 6
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)
    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()))
Esempio n. 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))
Esempio n. 9
0
    def _generate_lattice_from_parameters(interaction_matrix: np.ndarray):
        # make a graph from the interaction matrix.
        # This should be replaced by from_adjacency_matrix of retworkx.
        shape = interaction_matrix.shape
        if len(shape) != 2 or shape[0] != shape[1]:
            raise ValueError(
                f"Invalid shape of `interaction_matrix`, {shape},  is given."
                "It must be a square matrix.")

        graph = PyGraph(multigraph=False)
        graph.add_nodes_from(range(shape[0]))
        for source_index in range(shape[0]):
            for target_index in range(source_index, shape[0]):
                weight = interaction_matrix[source_index, target_index]
                if not weight == 0.0:
                    graph.add_edge(source_index, target_index, weight)
        return Lattice(graph)
Esempio n. 10
0
    def _generate_lattice_from_uniform_parameters(
        lattice: Lattice,
        uniform_interaction: complex,
        uniform_onsite_potential: complex,
    ) -> Lattice:
        graph = lattice.graph
        for node_a, node_b, _ in graph.weighted_edge_list():
            if node_a != node_b:
                graph.update_edge(node_a, node_b, uniform_interaction)

        for node_a in graph.node_indexes():
            if graph.has_edge(node_a, node_a):
                graph.update_edge(node_a, node_a, uniform_onsite_potential)
            else:
                graph.add_edge(node_a, node_a, uniform_onsite_potential)

        return Lattice(graph)
Esempio n. 11
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))
Esempio n. 12
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()))