-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
119 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import numpy as np | ||
from dataclasses import dataclass, field | ||
from beartype.typing import List | ||
from abc import abstractmethod | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Symmetry: | ||
label: str = field(default="") | ||
|
||
|
||
# trait | ||
@dataclass(frozen=True) | ||
class AbelianSym(Symmetry): | ||
|
||
@abstractmethod | ||
def combine_rule(self, A: int, B: int) -> int: | ||
raise NotImplementedError("not implement for abstract type trait.") | ||
|
||
@abstractmethod | ||
def check_qnums(self, qnums: List[int]) -> bool: | ||
raise NotImplementedError("not implement for abstract type trait.") | ||
|
||
def combine_qnums(self, qnums_a: List[int], qnums_b: List[int]) -> List[int]: | ||
mesh_b, mesh_a = np.meshgrid(qnums_b, qnums_a) | ||
return self.combine_rule(mesh_a.flatten(), mesh_b.flatten()) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class U1(AbelianSym): | ||
""" | ||
Unitary Symmetry class. | ||
The U1 symmetry can have quantum number represent as arbitrary unsigned integer. | ||
Fusion rule for combine two quantum number: | ||
q1 + q2 | ||
""" | ||
|
||
def combine_rule(self, A: int, B: int) -> int: | ||
return A + B | ||
|
||
def __str__(self) -> str: | ||
return f"U1 label={self.label}" | ||
|
||
def check_qnums(self, qnums: List[int]) -> bool: | ||
return True | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Zn(AbelianSym): | ||
""" | ||
Z(n) Symmetry class. | ||
The Z(n) symmetry can have integer quantum number, with n > 1. | ||
Fusion rule for combine two quantum number: | ||
(q1 + q2)%n | ||
""" | ||
|
||
n: int = field(default=2) | ||
|
||
def __post_init__(self): | ||
if self.n < 2: | ||
raise ValueError( | ||
"Symmetry.Zn", "[ERROR] discrete symmetry Zn must have n >= 2." | ||
) | ||
|
||
def combine_rule(self, A: int, B: int) -> int: | ||
return (A + B) % self.n | ||
|
||
def __str__(self): | ||
return f"Z{self.n} label={self.label}" | ||
|
||
def check_qnums(self, qnums: List[int]) -> bool: | ||
return np.all([(q >= 0) and (q < self.n) for q in qnums]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import numpy as np | ||
from cytnx_torch.symmetry import U1, Zn | ||
|
||
|
||
def test_U1(): | ||
|
||
s1 = U1(label="a") | ||
|
||
q1 = [0, 1, 2] | ||
q2 = [0, -1, 3] | ||
|
||
assert s1.check_qnums(q1) | ||
assert s1.check_qnums(q2) | ||
|
||
assert np.all(s1.combine_qnums(q1, q2) == [0, -1, 3, 1, 0, 4, 2, 1, 5]) | ||
|
||
|
||
def test_Zn(): | ||
|
||
s1 = Zn(label="x", n=3) | ||
|
||
q1 = [0, 1, 2] | ||
q2 = [2, 0] | ||
|
||
assert s1.check_qnums(q1) | ||
assert s1.check_qnums(q2) | ||
|
||
assert np.all(s1.combine_qnums(q1, q2) == [2, 0, 0, 1, 1, 2]) |