forked from tencent-quantum-lab/tensorcircuit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hamiltonian_building.py
132 lines (98 loc) · 2.98 KB
/
hamiltonian_building.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""
benchmark sparse hamiltonian building
"""
import time
import jax
import numpy as np
import quimb
import scipy
import tensorflow as tf
import tensorcircuit as tc
tc.set_dtype("complex128")
nwires = 20
print("--------------------")
# 1.1 tc approach for TFIM (numpy backend)
tc.set_backend("numpy")
print("hamiltonian building with tc (numpy backend)")
print("numpy version: ", np.__version__)
print("scipy version: ", scipy.__version__)
g = tc.templates.graphs.Line1D(nwires, pbc=False)
time0 = time.perf_counter()
h11 = tc.quantum.heisenberg_hamiltonian(
g, hzz=1, hxx=0, hyy=0, hz=0, hx=-1, hy=0, sparse=True, numpy=True
)
time1 = time.perf_counter()
print("tc (numpy) time: ", time1 - time0)
print("--------------------")
# 1.2 tc approach for TFIM (jax backend)
tc.set_backend("jax")
print("hamiltonian building with tc (jax backend)")
print("jax version: ", jax.__version__)
g = tc.templates.graphs.Line1D(nwires, pbc=False)
time0 = time.perf_counter()
h12 = tc.quantum.heisenberg_hamiltonian(
g, hzz=1, hxx=0, hyy=0, hz=0, hx=-1, hy=0, sparse=True
)
time1 = time.perf_counter()
print("tc (jax) time: ", time1 - time0)
time0 = time.perf_counter()
h12 = tc.quantum.heisenberg_hamiltonian(
g, hzz=1, hxx=0, hyy=0, hz=0, hx=-1, hy=0, sparse=True
)
time1 = time.perf_counter()
print("tc (jax) time (after jit): ", time1 - time0)
print("--------------------")
# 1.3 tc approach for TFIM (tensorflow backend)
tc.set_backend("tensorflow")
print("hamiltonian building with tc (tensorflow backend)")
print("tensorflow version: ", tf.__version__)
g = tc.templates.graphs.Line1D(nwires, pbc=False)
time0 = time.perf_counter()
h13 = tc.quantum.heisenberg_hamiltonian(
g, hzz=1, hxx=0, hyy=0, hz=0, hx=-1, hy=0, sparse=True
)
time1 = time.perf_counter()
print("tc (tensorflow) time: ", time1 - time0)
time0 = time.perf_counter()
h13 = tc.quantum.heisenberg_hamiltonian(
g, hzz=1, hxx=0, hyy=0, hz=0, hx=-1, hy=0, sparse=True
)
time1 = time.perf_counter()
print("tc (tensorflow) time (after jit): ", time1 - time0)
print("--------------------")
# 2. quimb approach for TFIM
print("hamiltonian building with quimb")
print("quimb version: ", quimb.__version__)
builder = quimb.tensor.SpinHam1D()
# spin operator instead of Pauli matrix
builder += 4, "Z", "Z"
builder += -2, "X"
time0 = time.perf_counter()
h2 = builder.build_sparse(nwires)
h2 = h2.tocoo()
time1 = time.perf_counter()
print("quimb time: ", time1 - time0)
def assert_equal(h1, h2):
np.testing.assert_allclose(h1.row, h2.row, atol=1e-5)
np.testing.assert_allclose(h1.col, h2.col, atol=1e-5)
np.testing.assert_allclose(h1.data, h2.data, atol=1e-5)
# numpy
assert_equal(h11, h2)
# jax
scipy_coo = scipy.sparse.coo_matrix(
(
h12.data,
(h12.indices[:, 0], h12.indices[:, 1]),
),
shape=h12.shape,
)
assert_equal(scipy_coo, h2)
# tensorflow
scipy_coo = scipy.sparse.coo_matrix(
(
h13.values,
(h13.indices[:, 0], h13.indices[:, 1]),
),
shape=h13.shape,
)
assert_equal(scipy_coo, h2)