forked from tencent-quantum-lab/tensorcircuit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvqe_extra_mpo.py
146 lines (117 loc) · 3.9 KB
/
vqe_extra_mpo.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""
Demonstration of TFIM VQE with extra size in MPO formulation
"""
import time
import logging
import sys
import numpy as np
logger = logging.getLogger("tensorcircuit")
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
logger.addHandler(ch)
sys.setrecursionlimit(10000)
import tensorflow as tf
import tensornetwork as tn
import cotengra as ctg
import optax
import tensorcircuit as tc
opt = ctg.ReusableHyperOptimizer(
methods=["greedy", "kahypar"],
parallel="ray",
minimize="combo",
max_time=360,
max_repeats=4096,
progbar=True,
)
def opt_reconf(inputs, output, size, **kws):
tree = opt.search(inputs, output, size)
tree_r = tree.subtree_reconfigure_forest(
parallel="ray",
progbar=True,
num_trees=20,
num_restarts=20,
subtree_weight_what=("size",),
)
return tree_r.path()
tc.set_contractor("custom", optimizer=opt_reconf, preprocessing=True)
tc.set_dtype("complex64")
tc.set_backend("tensorflow")
# jax backend is incompatible with keras.save
dtype = np.complex64
nwires, nlayers = 150, 7 # 600, 7
Jx = np.array([1.0 for _ in range(nwires - 1)]) # strength of xx interaction (OBC)
Bz = np.array([-1.0 for _ in range(nwires)]) # strength of transverse field
hamiltonian_mpo = tn.matrixproductstates.mpo.FiniteTFI(
Jx, Bz, dtype=dtype
) # matrix product operator
hamiltonian_mpo = tc.quantum.tn2qop(hamiltonian_mpo)
def vqe_forward(param):
print("compiling")
split_conf = {
"max_singular_values": 2,
"fixed_choice": 1,
}
c = tc.Circuit(nwires, split=split_conf)
for i in range(nwires):
c.H(i)
for j in range(nlayers):
for i in range(0, nwires - 1):
c.exp1(
i,
(i + 1) % nwires,
theta=param[4 * j, i],
unitary=tc.gates._xx_matrix,
)
for i in range(nwires):
c.rz(i, theta=param[4 * j + 1, i])
for i in range(nwires):
c.ry(i, theta=param[4 * j + 2, i])
for i in range(nwires):
c.rz(i, theta=param[4 * j + 3, i])
return tc.templates.measurements.mpo_expectation(c, hamiltonian_mpo)
if __name__ == "__main__":
refresh = False
time0 = time.time()
if refresh:
tc_vg = tf.function(
tc.backend.value_and_grad(vqe_forward),
input_signature=[tf.TensorSpec([4 * nlayers, nwires], tf.float32)],
)
tc.keras.save_func(tc_vg, "./funcs/%s_%s_tfim_mpo" % (nwires, nlayers))
time1 = time.time()
print("staging time: ", time1 - time0)
tc_vg_loaded = tc.keras.load_func("./funcs/%s_%s_tfim_mpo" % (nwires, nlayers))
lr1 = 0.008
lr2 = 0.06
steps = 2000
switch = 400
debug_steps = 20
if tc.backend.name == "jax":
opt = tc.backend.optimizer(optax.adam(lr1))
opt2 = tc.backend.optimizer(optax.sgd(lr2))
else:
opt = tc.backend.optimizer(tf.keras.optimizers.Adam(lr1))
opt2 = tc.backend.optimizer(tf.keras.optimizers.SGD(lr2))
times = []
param = tc.backend.implicit_randn(stddev=0.1, shape=[4 * nlayers, nwires])
for j in range(steps):
loss, gr = tc_vg_loaded(param)
if j < switch:
param = opt.update(gr, param)
else:
if j == switch:
print("switching the optimizer")
param = opt2.update(gr, param)
if j % debug_steps == 0 or j == steps - 1:
times.append(time.time())
print("loss", tc.backend.numpy(loss))
if j > 0:
print("running time:", (times[-1] - times[0]) / j)
"""
# Baseline code: obtained from DMRG using quimb
import quimb
h = quimb.tensor.tensor_gen.MPO_ham_ising(nwires, 4, 2, cyclic=False)
dmrg = quimb.tensor.tensor_dmrg.DMRG2(m, bond_dims=[10, 20, 100, 100, 200], cutoffs=1e-10)
dmrg.solve(tol=1e-9, verbosity=1) # may require repetition of this API
"""