Skip to content

Commit

Permalink
improve typehint by PEP-563
Browse files Browse the repository at this point in the history
  • Loading branch information
ikkoham committed Dec 8, 2021
1 parent c688128 commit 71d263b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
# that they have been altered from the originals.

"""The hyper-cubic lattice"""
from __future__ import annotations

from dataclasses import asdict
from itertools import product
from math import pi
from typing import Dict, List, Optional, Tuple, Union
from typing import Optional, Union

import numpy as np
from retworkx import PyGraph
Expand Down Expand Up @@ -51,11 +53,11 @@ class HyperCubicLattice(Lattice):

def __init__(
self,
size: Tuple[int, ...],
edge_parameter: Union[complex, Tuple[complex, ...]] = 1.0,
size: tuple[int, ...],
edge_parameter: Union[complex, tuple[complex, ...]] = 1.0,
onsite_parameter: complex = 0.0,
boundary_condition: Union[
BoundaryCondition, Tuple[BoundaryCondition, ...]
BoundaryCondition, tuple[BoundaryCondition, ...]
] = BoundaryCondition.OPEN,
) -> None:
"""
Expand Down Expand Up @@ -144,7 +146,7 @@ def dim(self) -> int:
return self._dim

@property
def size(self) -> Tuple[int, ...]:
def size(self) -> tuple[int, ...]:
"""Lengths of each dimension.
Returns:
Expand All @@ -153,7 +155,7 @@ def size(self) -> Tuple[int, ...]:
return self._size

@property
def edge_parameter(self) -> Union[complex, Tuple[complex, ...]]:
def edge_parameter(self) -> Union[complex, tuple[complex, ...]]:
"""Weights on the edges in each direction.
Returns:
Expand All @@ -171,7 +173,7 @@ def onsite_parameter(self) -> complex:
return self._onsite_parameter

@property
def boundary_condition(self) -> Union[BoundaryCondition, Tuple[BoundaryCondition, ...]]:
def boundary_condition(self) -> Union[BoundaryCondition, tuple[BoundaryCondition, ...]]:
"""Boundary condition for each dimension.
Returns:
Expand All @@ -195,19 +197,19 @@ def _coordinate_to_index(self, coord: np.ndarray) -> int:
base = np.array([np.prod(size[:i]) for i in range(dim)], dtype=int)
return np.dot(coord, base).item()

def _self_loops(self) -> List[Tuple[int, int, complex]]:
def _self_loops(self) -> list[tuple[int, int, complex]]:
"""Return a list consisting of the self-loops on all the nodes.
Returns:
List[Tuple[int, int, complex]] : List of the self-loops.
List of the self-loops.
"""
num_nodes = np.prod(self._size)
return [(node_a, node_a, self._onsite_parameter) for node_a in range(num_nodes)]

def _bulk_edges(self) -> List[Tuple[int, int, complex]]:
def _bulk_edges(self) -> list[tuple[int, int, complex]]:
"""Return a list consisting of the edges in the bulk, which don't cross the boundaries.
Returns:
List[Tuple[int, int, complex]] : List of weighted edges that don't cross the boundaries.
List of weighted edges that don't cross the boundaries.
"""
list_of_edges = []
size = self._size
Expand All @@ -224,14 +226,14 @@ def _bulk_edges(self) -> List[Tuple[int, int, complex]]:
list_of_edges.append((node_a, node_b, edge_parameter[i]))
return list_of_edges

def _create_boundary_edges(self) -> List[Tuple[int, int, complex]]:
def _create_boundary_edges(self) -> list[tuple[int, int, complex]]:
"""Return a list consisting of the edges that cross the boundaries
depending on the boundary conditions.
Raises:
ValueError: Given boundary condition is invalid values.
Returns:
List[Tuple[int, int, complex]]: List of weighted edges that cross the boundaries.
List of weighted edges that cross the boundaries.
"""
list_of_edges = []
size = self._size
Expand Down Expand Up @@ -263,7 +265,7 @@ def _create_boundary_edges(self) -> List[Tuple[int, int, complex]]:
)
return list_of_edges

def _default_position(self) -> Optional[Dict[int, List[float]]]:
def _default_position(self) -> Optional[dict[int, list[float]]]:
"""Return a dictionary of default positions for visualization of
a one- or two-dimensional lattice.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
# that they have been altered from the originals.

"""General Lattice."""
from __future__ import annotations

from copy import deepcopy
from dataclasses import asdict, dataclass
from typing import Callable, List, Optional, Sequence, Tuple, Union
from typing import Callable, Optional, Sequence, Union

import numpy as np
from qiskit.exceptions import MissingOptionalLibraryError
Expand All @@ -39,7 +41,7 @@ class LatticeDrawStyle:
pos: Optional[dict] = None

# Matplotlib Axes object
ax: Optional["Axes"] = None # pylint:disable=invalid-name
ax: Optional[Axes] = None # pylint:disable=invalid-name

with_labels: bool = False

Expand All @@ -51,9 +53,9 @@ class LatticeDrawStyle:

node_color: Union[
str,
Tuple[float, float, float],
Tuple[float, float, float],
List[Union[str, Tuple[float, float, float], Tuple[float, float, float, float]]],
tuple[float, float, float],
tuple[float, float, float],
list[Union[str, tuple[float, float, float], tuple[float, float, float, float]]],
] = "#1f78b4"

node_shape: str = "o"
Expand Down Expand Up @@ -147,14 +149,14 @@ def weighted_edge_list(self) -> WeightedEdgeList:
"""Return a list of weighted edges."""
return self.graph.weighted_edge_list()

def copy(self) -> "Lattice":
def copy(self) -> Lattice:
"""Return a copy of the lattice."""
return deepcopy(self)

@classmethod
def from_nodes_and_edges(
cls, num_nodes: int, weighted_edges: List[Tuple[int, int, complex]]
) -> "Lattice":
cls, num_nodes: int, weighted_edges: list[tuple[int, int, complex]]
) -> Lattice:
"""Return an instance of Lattice from the number of nodes and the list of edges.
Args:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
# that they have been altered from the originals.

"""The square lattice"""
from typing import Tuple, Union
from __future__ import annotations

from typing import Union

from .boundary_condition import BoundaryCondition
from .hyper_cubic_lattice import HyperCubicLattice
Expand All @@ -24,10 +26,10 @@ def __init__(
self,
rows: int,
cols: int,
edge_parameter: Union[complex, Tuple[complex, complex]] = 1.0,
edge_parameter: Union[complex, tuple[complex, complex]] = 1.0,
onsite_parameter: complex = 0.0,
boundary_condition: Union[
BoundaryCondition, Tuple[BoundaryCondition, BoundaryCondition]
BoundaryCondition, tuple[BoundaryCondition, BoundaryCondition]
] = BoundaryCondition.OPEN,
) -> None:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@
# that they have been altered from the originals.

"""The triangular lattice"""
from __future__ import annotations

from dataclasses import asdict
from itertools import product
from math import pi
from typing import Dict, List, Optional, Tuple, Union
from typing import Optional, Union

import numpy as np
from retworkx import PyGraph

from .lattice import LatticeDrawStyle, Lattice
from .boundary_condition import BoundaryCondition
from .lattice import Lattice, LatticeDrawStyle


class TriangularLattice(Lattice):
Expand All @@ -41,21 +43,22 @@ def _coordinate_to_index(self, coord: np.ndarray) -> int:
base = np.array([np.prod(size[:i]) for i in range(dim)], dtype=int)
return np.dot(coord, base).item()

def _self_loops(self) -> List[Tuple[int, int, complex]]:
def _self_loops(self) -> list[tuple[int, int, complex]]:
"""Return a list consisting of the self-loops on all the nodes.
Returns:
List[Tuple[int, int, complex]] : List of the self-loops.
List of the self-loops.
"""
size = self.size
onsite_parameter = self.onsite_parameter
num_nodes = np.prod(size)
return [(node_a, node_a, onsite_parameter) for node_a in range(num_nodes)]

def _bulk_edges(self) -> List[Tuple[int, int, complex]]:
def _bulk_edges(self) -> list[tuple[int, int, complex]]:
"""Return a list consisting of the edges in th bulk, which don't cross the boundaries.
Returns:
List[Tuple[int, int, complex]] : List of weighted edges that don't cross the boundaries.
List of weighted edges that don't cross the boundaries.
"""
size = self.size
edge_parameter = self.edge_parameter
Expand All @@ -79,14 +82,14 @@ def _bulk_edges(self) -> List[Tuple[int, int, complex]]:
list_of_edges.append((node_a, node_b, edge_parameter[i]))
return list_of_edges

def _boundary_edges(self) -> List[Tuple[int, int, complex]]:
def _boundary_edges(self) -> list[tuple[int, int, complex]]:
"""Return a list consisting of the edges that cross the boundaries
depending on the boundary conditions.
Raises:
ValueError: Given boundary condition is invalid values.
Returns:
List[Tuple[int, int, complex]]: List of weighted edges that cross the boundaries.
List of weighted edges that cross the boundaries.
"""
list_of_edges = []
size = self.size
Expand Down Expand Up @@ -132,11 +135,11 @@ def _boundary_edges(self) -> List[Tuple[int, int, complex]]:
)
return list_of_edges

def _default_position(self) -> Dict[int, List[float]]:
def _default_position(self) -> dict[int, list[float]]:
"""Return a dictionary of default positions for visualization of a two-dimensional lattice.
Returns:
Dict[int, List[float]] : The keys are the labels of lattice points,
The keys are the labels of lattice points,
and the values are two-dimensional coordinates.
"""
size = self.size
Expand All @@ -162,7 +165,7 @@ def __init__(
self,
rows: int,
cols: int,
edge_parameter: Union[complex, Tuple[complex, complex, complex]] = 1.0,
edge_parameter: Union[complex, tuple[complex, complex, complex]] = 1.0,
onsite_parameter: complex = 0.0,
boundary_condition: BoundaryCondition = BoundaryCondition.OPEN,
) -> None:
Expand Down

0 comments on commit 71d263b

Please sign in to comment.