Skip to content

Commit

Permalink
Merge pull request #187 from GenericP3rson/dev
Browse files Browse the repository at this point in the history
Reformatting Operations
  • Loading branch information
Hanrui-Wang authored Oct 15, 2023
2 parents c99b0af + c893f23 commit dd3fc64
Show file tree
Hide file tree
Showing 53 changed files with 3,768 additions and 2,124 deletions.
1 change: 0 additions & 1 deletion test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@
"""

from .utils import *

234 changes: 184 additions & 50 deletions test/algorithm/test_hamiltonian.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,74 +25,208 @@
from torchquantum.algorithm import Hamiltonian
import numpy as np


def test_hamiltonian():
coeffs = [1.0, 1.0]
paulis = ["ZZ", "ZX"]
hamil = Hamiltonian(coeffs, paulis)
assert np.allclose(
hamil.get_matrix().cpu().detach().numpy(),
hamil.get_matrix().cpu().detach().numpy(),
np.array(
[[ 1.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],
[ 1.+0.j, -1.+0.j, 0.+0.j, 0.+0.j],
[ 0.+0.j, 0.+0.j, -1.+0.j, -1.+0.j],
[ 0.+0.j, 0.+0.j, -1.+0.j, 1.+0.j]]))

[
[1.0 + 0.0j, 1.0 + 0.0j, 0.0 + 0.0j, 0.0 + 0.0j],
[1.0 + 0.0j, -1.0 + 0.0j, 0.0 + 0.0j, 0.0 + 0.0j],
[0.0 + 0.0j, 0.0 + 0.0j, -1.0 + 0.0j, -1.0 + 0.0j],
[0.0 + 0.0j, 0.0 + 0.0j, -1.0 + 0.0j, 1.0 + 0.0j],
]
),
)

coeffs = [0.6]
paulis = ["XXZ"]
hamil = Hamiltonian(coeffs, paulis)
assert np.allclose(
hamil.get_matrix().cpu().detach().numpy(),
hamil.get_matrix().cpu().detach().numpy(),
np.array(
[[ 0.0000+0.j, 0.0000+0.j, 0.0000+0.j, 0.0000+0.j, 0.0000+0.j, 0.0000+0.j,
0.6000+0.j, 0.0000+0.j],
[ 0.0000+0.j, -0.0000+0.j, 0.0000+0.j, -0.0000+0.j, 0.0000+0.j, -0.0000+0.j,
0.0000+0.j, -0.6000+0.j],
[ 0.0000+0.j, 0.0000+0.j, 0.0000+0.j, 0.0000+0.j, 0.6000+0.j, 0.0000+0.j,
0.0000+0.j, 0.0000+0.j],
[ 0.0000+0.j, -0.0000+0.j, 0.0000+0.j, -0.0000+0.j, 0.0000+0.j, -0.6000+0.j,
0.0000+0.j, -0.0000+0.j],
[ 0.0000+0.j, 0.0000+0.j, 0.6000+0.j, 0.0000+0.j, 0.0000+0.j, 0.0000+0.j,
0.0000+0.j, 0.0000+0.j],
[ 0.0000+0.j, -0.0000+0.j, 0.0000+0.j, -0.6000+0.j, 0.0000+0.j, -0.0000+0.j,
0.0000+0.j, -0.0000+0.j],
[ 0.6000+0.j, 0.0000+0.j, 0.0000+0.j, 0.0000+0.j, 0.0000+0.j, 0.0000+0.j,
0.0000+0.j, 0.0000+0.j],
[ 0.0000+0.j, -0.6000+0.j, 0.0000+0.j, -0.0000+0.j, 0.0000+0.j, -0.0000+0.j,
0.0000+0.j, -0.0000+0.j]]))

[
[
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.6000 + 0.0j,
0.0000 + 0.0j,
],
[
0.0000 + 0.0j,
-0.0000 + 0.0j,
0.0000 + 0.0j,
-0.0000 + 0.0j,
0.0000 + 0.0j,
-0.0000 + 0.0j,
0.0000 + 0.0j,
-0.6000 + 0.0j,
],
[
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.6000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
],
[
0.0000 + 0.0j,
-0.0000 + 0.0j,
0.0000 + 0.0j,
-0.0000 + 0.0j,
0.0000 + 0.0j,
-0.6000 + 0.0j,
0.0000 + 0.0j,
-0.0000 + 0.0j,
],
[
0.0000 + 0.0j,
0.0000 + 0.0j,
0.6000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
],
[
0.0000 + 0.0j,
-0.0000 + 0.0j,
0.0000 + 0.0j,
-0.6000 + 0.0j,
0.0000 + 0.0j,
-0.0000 + 0.0j,
0.0000 + 0.0j,
-0.0000 + 0.0j,
],
[
0.6000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
],
[
0.0000 + 0.0j,
-0.6000 + 0.0j,
0.0000 + 0.0j,
-0.0000 + 0.0j,
0.0000 + 0.0j,
-0.0000 + 0.0j,
0.0000 + 0.0j,
-0.0000 + 0.0j,
],
]
),
)

hamil = Hamiltonian.from_file("test/algorithm/h2.txt")

assert np.allclose(
hamil.matrix.cpu().detach().numpy(),
hamil.matrix.cpu().detach().numpy(),
np.array(
[[-1.0636533 +0.j, 0. +0.j, 0. +0.j,
0. +0.j, 0. +0.j, 0. +0.j,
0.1809312 +0.j, 0. +0.j],
[ 0. +0.j, -1.0636533 +0.j, 0. +0.j,
0. +0.j, 0. +0.j, 0. +0.j,
0. +0.j, 0.1809312 +0.j],
[ 0. +0.j, 0. +0.j, -1.8369681 +0.j,
0. +0.j, 0.1809312 +0.j, 0. +0.j,
0. +0.j, 0. +0.j],
[ 0. +0.j, 0. +0.j, 0. +0.j,
-1.8369681 +0.j, 0. +0.j, 0.1809312 +0.j,
0. +0.j, 0. +0.j],
[ 0. +0.j, 0. +0.j, 0.1809312 +0.j,
0. +0.j, -0.24521835+0.j, 0. +0.j,
0. +0.j, 0. +0.j],
[ 0. +0.j, 0. +0.j, 0. +0.j,
0.1809312 +0.j, 0. +0.j, -0.24521835+0.j,
0. +0.j, 0. +0.j],
[ 0.1809312 +0.j, 0. +0.j, 0. +0.j,
0. +0.j, 0. +0.j, 0. +0.j,
-1.0636533 +0.j, 0. +0.j],
[ 0. +0.j, 0.1809312 +0.j, 0. +0.j,
0. +0.j, 0. +0.j, 0. +0.j,
0. +0.j, -1.0636533 +0.j]]))
[
[
-1.0636533 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.1809312 + 0.0j,
0.0 + 0.0j,
],
[
0.0 + 0.0j,
-1.0636533 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.1809312 + 0.0j,
],
[
0.0 + 0.0j,
0.0 + 0.0j,
-1.8369681 + 0.0j,
0.0 + 0.0j,
0.1809312 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
],
[
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
-1.8369681 + 0.0j,
0.0 + 0.0j,
0.1809312 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
],
[
0.0 + 0.0j,
0.0 + 0.0j,
0.1809312 + 0.0j,
0.0 + 0.0j,
-0.24521835 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
],
[
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.1809312 + 0.0j,
0.0 + 0.0j,
-0.24521835 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
],
[
0.1809312 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
-1.0636533 + 0.0j,
0.0 + 0.0j,
],
[
0.0 + 0.0j,
0.1809312 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
-1.0636533 + 0.0j,
],
]
),
)
print("hamiltonian test passed!")

if __name__ == '__main__':

if __name__ == "__main__":
import pdb

pdb.set_trace()
test_hamiltonian()
Loading

0 comments on commit dd3fc64

Please sign in to comment.