-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathvqe_pennylane.py
183 lines (154 loc) · 5.63 KB
/
vqe_pennylane.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import sys
import pennylane as qml
import tensorflow as tf
import numpy as np
import cpuinfo
import datetime
import os
import jax
import uuid
import utils
def H_tfim(J, h, nwires, bc=True):
if bc:
coeffs = [h] * nwires + [J] * nwires
obs = [qml.PauliX(i) for i in range(nwires)] + [
qml.PauliZ(i) @ qml.PauliZ((i + 1) % nwires) for i in range(nwires)
]
else:
coeffs = [h] * nwires + [J] * (nwires - 1)
obs = [qml.PauliX(i) for i in range(nwires)] + [
qml.PauliZ(i) @ qml.PauliZ((i + 1) % nwires) for i in range(nwires - 1)
]
return qml.Hamiltonian(coeffs, obs, True)
def pennylane_benchmark(uuid, nwires, nlayer, nitrs, timeLimit, isgpu, minus=1):
meta = {}
if isgpu == 0:
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
meta["isgpu"] = "off"
else:
gpu = tf.config.list_physical_devices("GPU")
tf.config.experimental.set_memory_growth(device=gpu[0], enable=True)
meta["isgpu"] = "on"
meta["Gpuinfo"] = utils.gpuinfo()
meta["Software"] = "pennylane"
meta["minus"] = minus
meta["Cpuinfo"] = cpuinfo.get_cpu_info()["brand_raw"]
meta["Version"] = {
"sys": sys.version,
"tensorflow": tf.__version__,
"pennylane": qml.__version__,
"numpy": np.__version__,
}
meta["VQE test parameters"] = {
"nQubits": nwires,
"nlayer": nlayer,
"nitrs": nitrs,
"timeLimit": timeLimit,
}
meta["UUID"] = uuid
meta["Benchmark Time"] = (
datetime.datetime.now().astimezone().strftime("%Y-%m-%d %H:%M %Z")
)
meta["Results"] = {}
Htfim = H_tfim(1.0, -1.0, nwires, False)
# def circuit(params, wires):
# for i in range(nwires):
# qml.Hadamard(wires=i)
# for j in range(nlayer):
# for i in range(nwires-1):
# qml.IsingZZ(params[2 * nwires * j + i], wires=[i, (i + 1) % nwires])
# for i in range(nwires):
# qml.RX(params[2 * nwires * j + nwires + i], wires=i)
dev = qml.device("default.qubit.jax", wires=nwires)
@jax.jit
@qml.qnode(dev, interface="jax")
def jax_expval(params):
for i in range(nwires):
qml.Hadamard(wires=i)
for j in range(nlayer):
for i in range(nwires - minus):
qml.IsingZZ(params[i + j * 2 * nwires], wires=[i, (i + 1) % nwires])
for i in range(nwires):
qml.RX(params[nwires + i + j * 2 * nwires], wires=i)
return qml.expval(Htfim)
vag = jax.value_and_grad(jax_expval)
params = jax.numpy.array(np.random.normal(size=[nlayer * 2 * nwires]))
def f():
return vag(params)[0].block_until_ready()
ct, it, Nitrs = utils.timing(f, nitrs, timeLimit)
meta["Results"]["jax_expval"] = {
"Construction time": ct,
"Iteration time": it,
"# of actual iterations": Nitrs,
}
# tf device below
dev = qml.device("default.qubit.tf", wires=nwires)
@tf.function
@qml.qnode(dev, interface="tf")
def tf_expval(params):
for i in range(nwires):
qml.Hadamard(wires=i)
for j in range(nlayer):
for i in range(nwires - minus):
qml.IsingZZ(params[i + j * 2 * nwires], wires=[i, (i + 1) % nwires])
for i in range(nwires):
qml.RX(params[nwires + i + j * 2 * nwires], wires=i)
return qml.expval(Htfim)
params = tf.Variable(np.random.normal(size=[nlayer * 2 * nwires]), dtype=tf.float32)
def f():
with tf.GradientTape() as t:
t.watch(params)
e = tf_expval(params)
grad = t.gradient(e, [params])
# opt_tf.apply_gradients(zip(grad, [params]))
return e
ct, it, Nitrs = utils.timing(f, nitrs, timeLimit)
meta["Results"]["tf_expval"] = {
"Construction time": ct,
"Iteration time": it,
"# of actual iterations": Nitrs,
}
print("begin testing lightning")
dev = qml.device("lightning.qubit", wires=nwires)
@qml.qnode(dev, diff_method="adjoint")
def lt_expval(params):
for i in range(nwires):
qml.Hadamard(wires=i)
for j in range(nlayer):
for i in range(nwires - minus):
qml.IsingZZ(params[i + j * 2 * nwires], wires=[i, (i + 1) % nwires])
for i in range(nwires):
qml.RX(params[nwires + i + j * 2 * nwires], wires=i)
return qml.expval(Htfim)
vag = qml.grad(lt_expval, argnum=0)
params = np.random.normal(size=[nlayer * 2 * nwires])
def f():
return vag(params)
ct, it, Nitrs = utils.timing(f, nitrs, timeLimit)
meta["Results"]["lightning.cpu"] = {
"Construction time": ct,
"Iteration time": it,
"# of actual iterations": Nitrs,
}
# using tf interface and expvalcost
# params.assign(np.random.normal(size=[nlayer * 2 * nwires]))
# cost_fn_tf = qml.ExpvalCost(circuit, Htfim, dev, interface="tf")
# def f():
# with tf.GradientTape() as t:
# t.watch(params)
# e = cost_fn_tf(params)
# grad = t.gradient(e, [params])
# opt_tf.apply_gradients(zip(grad, [params]))
# ct, it, Nitrs = utils.timing(f, nitrs, timeLimit)
# meta["Results"]["tf_costfn"] = {
# "Construction time": ct,
# "Iteration time": it,
# "# of actual iterations": Nitrs,
# }
print(meta)
return meta
if __name__ == "__main__":
_uuid = str(uuid.uuid4())
n, nlayer, nitrs, timeLimit, isgpu, minus, path = utils.arg()
results = pennylane_benchmark(_uuid, n, nlayer, nitrs, timeLimit, isgpu, minus)
utils.save(results, _uuid, path)