Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HexagonalLattice Class #1027

Merged
merged 21 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion qiskit_nature/second_q/hamiltonians/lattices/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2021, 2022.
# (C) Copyright IBM 2021, 2023.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand All @@ -24,6 +24,7 @@
SquareLattice
TriangularLattice
HyperCubicLattice
HexagonalLattice

"""

Expand All @@ -33,6 +34,7 @@
from .line_lattice import LineLattice
from .square_lattice import SquareLattice
from .triangular_lattice import TriangularLattice
from .hexagonal_lattice import HexagonalLattice

__all__ = [
"BoundaryCondition",
Expand All @@ -42,4 +44,5 @@
"SquareLattice",
"TriangularLattice",
"HyperCubicLattice",
"HexagonalLattice",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2021, 2023.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""The hexagonal lattice"""
from rustworkx import generators

from .lattice import Lattice


class HexagonalLattice(Lattice):
"""Hexagonal lattice."""

def __init__(
self,
rows: int,
cols: int,
edge_parameter: complex = 1.0,
onsite_parameter: complex = 0.0,
) -> None:
"""
Args:
rows: Length of the x direction.
cols: Length of the y direction.
mrossinek marked this conversation as resolved.
Show resolved Hide resolved
edge_parameter: Weight on all the edges, specified as a single value.
Defaults to 1.0.
onsite_parameter: Weight on the self-loops, which are edges connecting a node to itself.
Defaults to 0.0.
"""
self._rows = rows
self._cols = cols
self._edge_parameter = edge_parameter
self._onsite_parameter = onsite_parameter

graph = generators.hexagonal_lattice_graph(rows, cols, multigraph=False)

# Add edge weights
for idx in range(graph.num_edges()):
graph.update_edge_by_index(idx, self._edge_parameter)

# Add self loops
for node in range(graph.num_nodes()):
graph.add_edges_from([(node, node, self._onsite_parameter)])

super().__init__(graph)

@property
def edge_parameter(self) -> complex:
"""Weights on all edges.

Returns:
the parameter for the edges.
"""
return self._edge_parameter

@property
def onsite_parameter(self) -> complex:
"""Weight on the self-loops (edges connecting a node to itself).

Returns:
the parameter for the self-loops.
"""
return self._onsite_parameter
133 changes: 133 additions & 0 deletions test/second_q/hamiltonians/lattices/test_hexagonal_lattice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2021, 2023.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""Test for HyperCubicLattice."""
javabster marked this conversation as resolved.
Show resolved Hide resolved
from test import QiskitNatureTestCase
import numpy as np
from numpy.testing import assert_array_equal
from rustworkx import PyGraph, is_isomorphic
from qiskit_nature.second_q.hamiltonians.lattices import HexagonalLattice


class TestHexagonalLattice(QiskitNatureTestCase):
"""Test HexagonalLattice"""

def test_init(self):
"""Test init."""
rows = 1
cols = 2
edge_parameter = 0 + 1.42j
onsite_parameter = 1.0

hexa = HexagonalLattice(rows, cols, edge_parameter, onsite_parameter)

with self.subTest("Check the graph."):
target_graph = PyGraph(multigraph=False)
target_graph.add_nodes_from(range(10))
weighted_edge_list = [
mrossinek marked this conversation as resolved.
Show resolved Hide resolved
(0, 1, 1.42j),
(1, 2, 1.42j),
(3, 4, 1.42j),
(4, 5, 1.42j),
(5, 6, 1.42j),
(7, 8, 1.42j),
(8, 9, 1.42j),
(0, 3, 1.42j),
(2, 5, 1.42j),
(4, 7, 1.42j),
(6, 9, 1.42j),
(0, 0, 1.0),
(1, 1, 1.0),
(2, 2, 1.0),
(3, 3, 1.0),
(4, 4, 1.0),
(5, 5, 1.0),
(6, 6, 1.0),
(7, 7, 1.0),
(8, 8, 1.0),
(9, 9, 1.0),
]
target_graph.add_edges_from(weighted_edge_list)
self.assertTrue(
is_isomorphic(hexa.graph, target_graph, edge_matcher=lambda x, y: x == y)
)

with self.subTest("Check the number of nodes."):
self.assertEqual(hexa.num_nodes, 10)

with self.subTest("Check the set of nodes."):
self.assertSetEqual(set(hexa.node_indexes), set(range(10)))

with self.subTest("Check the set of weights."):
target_set = {
(0, 1, 1.42j),
(1, 2, 1.42j),
(3, 4, 1.42j),
(4, 5, 1.42j),
(5, 6, 1.42j),
(7, 8, 1.42j),
(8, 9, 1.42j),
(0, 3, 1.42j),
(2, 5, 1.42j),
(4, 7, 1.42j),
(6, 9, 1.42j),
(0, 0, 1.0),
(1, 1, 1.0),
(2, 2, 1.0),
(3, 3, 1.0),
(4, 4, 1.0),
(5, 5, 1.0),
(6, 6, 1.0),
(7, 7, 1.0),
(8, 8, 1.0),
(9, 9, 1.0),
}
self.assertSetEqual(set(hexa.weighted_edge_list), target_set)

with self.subTest("Check the adjacency matrix."):
target_matrix = np.zeros((10, 10), dtype=complex)
np.fill_diagonal(target_matrix, 1.0)

neg_indices = [
(1, 0),
(2, 1),
(3, 0),
(4, 3),
(5, 2),
(5, 4),
(6, 5),
(7, 4),
(8, 7),
(9, 6),
(9, 8),
]
pos_indices = [
(0, 1),
(0, 3),
(1, 2),
(2, 5),
(3, 4),
(4, 5),
(4, 7),
(5, 6),
(6, 9),
(7, 8),
(8, 9),
]

for idx1, idx2 in neg_indices:
target_matrix[idx1, idx2] = 0 - 1.42j

for idx3, idx4 in pos_indices:
target_matrix[idx3, idx4] = 0 + 1.42j

assert_array_equal(hexa.to_adjacency_matrix(weighted=True), target_matrix)
javabster marked this conversation as resolved.
Show resolved Hide resolved