-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
0cba9b6
commit 3219df2
Showing
7 changed files
with
89 additions
and
138 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,26 @@ | ||
import pytest | ||
import numpy as np | ||
import pytest | ||
|
||
from tnpy.model import RandomHeisenberg | ||
from tnpy.operators import FullHamiltonian | ||
|
||
|
||
class TestRandomHeisenberg: | ||
|
||
@pytest.fixture(scope='class') | ||
@pytest.fixture(scope="class") | ||
def model(self): | ||
return RandomHeisenberg(n=6, h=0.5, seed=2022) | ||
|
||
@pytest.fixture(scope='class') | ||
@pytest.fixture(scope="class") | ||
def offset(self): | ||
return 0.5 | ||
|
||
@pytest.fixture(scope='class') | ||
@pytest.fixture(scope="class") | ||
def shifted_model(self, model, offset): | ||
return RandomHeisenberg( | ||
n=model.n, h=model.h, seed=model.seed, offset=offset | ||
) | ||
return RandomHeisenberg(n=model.n, h=model.h, seed=model.seed, offset=offset) | ||
|
||
def test_offset(self, model, offset, shifted_model): | ||
np.testing.assert_allclose( | ||
FullHamiltonian(model.mpo).matrix - offset * np.eye(2 ** model.n), | ||
FullHamiltonian(model.mpo).matrix - offset * np.eye(2**model.n), | ||
FullHamiltonian(shifted_model.mpo).matrix, | ||
atol=1e-12 | ||
atol=1e-12, | ||
) |
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
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 |
---|---|---|
@@ -1,119 +1,98 @@ | ||
import pytest | ||
import numpy as np | ||
import pytest | ||
|
||
from tnpy.operators import SpinOperators, FullHamiltonian | ||
from tnpy.model import XXZ, RandomHeisenberg | ||
from tnpy.operators import FullHamiltonian, SpinOperators | ||
|
||
|
||
class TestSpinOperators: | ||
|
||
def test_spin_half_ops(self): | ||
spin_half_ops = SpinOperators() | ||
np.testing.assert_array_equal( | ||
np.array( | ||
[[0, 1], | ||
[1, 0]] | ||
), | ||
spin_half_ops.Sp + spin_half_ops.Sm | ||
np.array([[0, 1], [1, 0]]), spin_half_ops.Sp + spin_half_ops.Sm | ||
) | ||
np.testing.assert_array_equal( | ||
np.array( | ||
[[0, -1j], | ||
[1j, 0]] | ||
), | ||
-1j * (spin_half_ops.Sp - spin_half_ops.Sm) | ||
np.array([[0, -1j], [1j, 0]]), -1j * (spin_half_ops.Sp - spin_half_ops.Sm) | ||
) | ||
|
||
|
||
class TestMatrixProductOperator: | ||
|
||
@pytest.mark.parametrize("model", [ | ||
RandomHeisenberg(n=4, h=0), | ||
RandomHeisenberg(n=4, h=0.5) | ||
]) | ||
@pytest.mark.parametrize("model", [RandomHeisenberg(n=4, h=0), RandomHeisenberg(n=4, h=0.5)]) | ||
def test_square(self, model): | ||
bilayer_mpo = model.mpo.square() | ||
assert bilayer_mpo[0].shape == (25, 2, 2) | ||
assert bilayer_mpo[1].shape == (25, 25, 2, 2) | ||
assert bilayer_mpo[2].shape == (25, 25, 2, 2) | ||
assert bilayer_mpo[3].shape == (25, 2, 2) | ||
ham = FullHamiltonian(model.mpo).matrix | ||
np.testing.assert_allclose( | ||
ham @ ham, | ||
FullHamiltonian(bilayer_mpo).matrix, | ||
atol=1e-12 | ||
) | ||
np.testing.assert_allclose(ham @ ham, FullHamiltonian(bilayer_mpo).matrix, atol=1e-12) | ||
|
||
@pytest.mark.parametrize("n", [2, 4, 6]) | ||
@pytest.mark.parametrize("h", [0, 0.5, 1]) | ||
def test_multiply_scalar(self, n, h): | ||
mpo = RandomHeisenberg(n=n, h=h).mpo | ||
np.testing.assert_array_equal( | ||
-1 * FullHamiltonian(mpo).matrix, | ||
FullHamiltonian(-1 * mpo).matrix | ||
-1 * FullHamiltonian(mpo).matrix, FullHamiltonian(-1 * mpo).matrix | ||
) | ||
|
||
|
||
class TestFullHamiltonian: | ||
|
||
@pytest.fixture(scope='class', params=[ | ||
{ | ||
"n": 2, | ||
"ham": FullHamiltonian(RandomHeisenberg(n=2, h=0).mpo), | ||
"data": np.array( | ||
[[0.25, 0, 0, 0], | ||
[0, -0.25, 0.5, 0], | ||
[0, 0.5, -0.25, 0], | ||
[0, 0, 0, 0.25]] | ||
) | ||
}, | ||
{ | ||
"n": 3, | ||
"ham": FullHamiltonian(RandomHeisenberg(n=3, h=0).mpo), | ||
"data": np.array( | ||
[[0.5, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 0, 0.5, 0, 0, 0, 0, 0], | ||
[0, 0.5, -0.5, 0, 0.5, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0.5, 0, 0], | ||
[0, 0, 0.5, 0, 0, 0, 0, 0], | ||
[0, 0, 0, 0.5, 0, -0.5, 0.5, 0], | ||
[0, 0, 0, 0, 0, 0.5, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 0.5]] | ||
) | ||
}, | ||
{ | ||
"n": 2, | ||
"ham": FullHamiltonian(XXZ(n=2, delta=0.5).mpo), | ||
"data": np.array( | ||
[[-0.125, 0, 0, 0], | ||
[0, 0.125, -0.5, 0], | ||
[0, -0.5, 0.125, 0], | ||
[0, 0, 0, -0.125]] | ||
) | ||
}, | ||
{ | ||
"n": 3, | ||
"ham": FullHamiltonian(XXZ(n=3, delta=0.5).mpo), | ||
"data": np.array( | ||
[[-0.25, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 0, -0.5, 0, 0, 0, 0, 0], | ||
[0, -0.5, 0.25, 0, -0.5, 0, 0, 0], | ||
[0, 0, 0, 0, 0, -0.5, 0, 0], | ||
[0, 0, -0.5, 0, 0, 0, 0, 0], | ||
[0, 0, 0, -0.5, 0, 0.25, -0.5, 0], | ||
[0, 0, 0, 0, 0, -0.5, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, -0.25]] | ||
) | ||
} | ||
]) | ||
@pytest.fixture( | ||
scope="class", | ||
params=[ | ||
{ | ||
"n": 2, | ||
"ham": FullHamiltonian(RandomHeisenberg(n=2, h=0).mpo), | ||
"data": np.array( | ||
[[0.25, 0, 0, 0], [0, -0.25, 0.5, 0], [0, 0.5, -0.25, 0], [0, 0, 0, 0.25]] | ||
), | ||
}, | ||
{ | ||
"n": 3, | ||
"ham": FullHamiltonian(RandomHeisenberg(n=3, h=0).mpo), | ||
"data": np.array( | ||
[ | ||
[0.5, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 0, 0.5, 0, 0, 0, 0, 0], | ||
[0, 0.5, -0.5, 0, 0.5, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0.5, 0, 0], | ||
[0, 0, 0.5, 0, 0, 0, 0, 0], | ||
[0, 0, 0, 0.5, 0, -0.5, 0.5, 0], | ||
[0, 0, 0, 0, 0, 0.5, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 0.5], | ||
] | ||
), | ||
}, | ||
{ | ||
"n": 2, | ||
"ham": FullHamiltonian(XXZ(n=2, delta=0.5).mpo), | ||
"data": np.array( | ||
[[-0.125, 0, 0, 0], [0, 0.125, -0.5, 0], [0, -0.5, 0.125, 0], [0, 0, 0, -0.125]] | ||
), | ||
}, | ||
{ | ||
"n": 3, | ||
"ham": FullHamiltonian(XXZ(n=3, delta=0.5).mpo), | ||
"data": np.array( | ||
[ | ||
[-0.25, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 0, -0.5, 0, 0, 0, 0, 0], | ||
[0, -0.5, 0.25, 0, -0.5, 0, 0, 0], | ||
[0, 0, 0, 0, 0, -0.5, 0, 0], | ||
[0, 0, -0.5, 0, 0, 0, 0, 0], | ||
[0, 0, 0, -0.5, 0, 0.25, -0.5, 0], | ||
[0, 0, 0, 0, 0, -0.5, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, -0.25], | ||
] | ||
), | ||
}, | ||
], | ||
) | ||
def model(self, request): | ||
return request.param | ||
|
||
def test_n_sites(self, model): | ||
assert model["ham"].n_sites == model["n"] | ||
|
||
def test_matrix(self, model): | ||
np.testing.assert_array_equal( | ||
model["ham"].matrix, | ||
model["data"] | ||
) | ||
np.testing.assert_array_equal(model["ham"].matrix, model["data"]) |
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