-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathgradient_benchmark.py
198 lines (162 loc) · 6.56 KB
/
gradient_benchmark.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"""
Gradient evaluation comparison between qiskit and tensorcircut
"""
import time
import json
from functools import reduce
from operator import xor
import numpy as np
from qiskit.opflow import X, StateFn
from qiskit.circuit import QuantumCircuit, ParameterVector
from qiskit.opflow.gradients import Gradient, QFI, Hessian
import tensorcircuit as tc
from tensorcircuit import experimental
def benchmark(f, *args, trials=10):
time0 = time.time()
r = f(*args)
time1 = time.time()
for _ in range(trials):
r = f(*args)
time2 = time.time()
if trials > 0:
time21 = (time2 - time1) / trials
else:
time21 = 0
ts = (time1 - time0, time21)
print("staging time: %.6f s" % ts[0])
if trials > 0:
print("running time: %.6f s" % ts[1])
return r, ts
def grad_qiskit(n, l, trials=2):
hamiltonian = reduce(xor, [X for _ in range(n)])
wavefunction = QuantumCircuit(n)
params = ParameterVector("theta", length=3 * n * l)
for j in range(l):
for i in range(n - 1):
wavefunction.cnot(i, i + 1)
for i in range(n):
wavefunction.rx(params[3 * n * j + i], i)
for i in range(n):
wavefunction.rz(params[3 * n * j + i + n], i)
for i in range(n):
wavefunction.rx(params[3 * n * j + i + 2 * n], i)
# Define the expectation value corresponding to the energy
op = ~StateFn(hamiltonian) @ StateFn(wavefunction)
grad = Gradient().convert(operator=op, params=params)
def get_grad_qiskit(values):
value_dict = {params: values}
grad_result = grad.assign_parameters(value_dict).eval()
return grad_result
return benchmark(get_grad_qiskit, np.ones([3 * n * l]), trials=trials)
def qfi_qiskit(n, l, trials=0):
wavefunction = QuantumCircuit(n)
params = ParameterVector("theta", length=3 * n * l)
for j in range(l):
for i in range(n - 1):
wavefunction.cnot(i, i + 1)
for i in range(n):
wavefunction.rx(params[3 * n * j + i], i)
for i in range(n):
wavefunction.rz(params[3 * n * j + i + n], i)
for i in range(n):
wavefunction.rx(params[3 * n * j + i + 2 * n], i)
nat_grad = QFI().convert(operator=StateFn(wavefunction), params=params)
def get_qfi_qiskit(values):
value_dict = {params: values}
grad_result = nat_grad.assign_parameters(value_dict).eval()
return grad_result
return benchmark(get_qfi_qiskit, np.ones([3 * n * l]), trials=trials)
def hessian_qiskit(n, l, trials=0):
hamiltonian = reduce(xor, [X for _ in range(n)])
wavefunction = QuantumCircuit(n)
params = ParameterVector("theta", length=3 * n * l)
for j in range(l):
for i in range(n - 1):
wavefunction.cnot(i, i + 1)
for i in range(n):
wavefunction.rx(params[3 * n * j + i], i)
for i in range(n):
wavefunction.rz(params[3 * n * j + i + n], i)
for i in range(n):
wavefunction.rx(params[3 * n * j + i + 2 * n], i)
# Define the expectation value corresponding to the energy
op = ~StateFn(hamiltonian) @ StateFn(wavefunction)
grad = Hessian().convert(operator=op, params=params)
def get_hs_qiskit(values):
value_dict = {params: values}
grad_result = grad.assign_parameters(value_dict).eval()
return grad_result
return benchmark(get_hs_qiskit, np.ones([3 * n * l]), trials=trials)
def grad_tc(n, l, trials=10):
def f(params):
c = tc.Circuit(n)
for j in range(l):
for i in range(n - 1):
c.cnot(i, i + 1)
for i in range(n):
c.rx(i, theta=params[3 * n * j + i])
for i in range(n):
c.rz(i, theta=params[3 * n * j + i + n])
for i in range(n):
c.rx(i, theta=params[3 * n * j + i + 2 * n])
return tc.backend.real(c.expectation(*[[tc.gates.x(), [i]] for i in range(n)]))
get_grad_tc = tc.backend.jit(tc.backend.grad(f))
return benchmark(get_grad_tc, tc.backend.ones([3 * n * l], dtype="float32"))
def qfi_tc(n, l, trials=10):
def s(params):
c = tc.Circuit(n)
for j in range(l):
for i in range(n - 1):
c.cnot(i, i + 1)
for i in range(n):
c.rx(i, theta=params[3 * n * j + i])
for i in range(n):
c.rz(i, theta=params[3 * n * j + i + n])
for i in range(n):
c.rx(i, theta=params[3 * n * j + i + 2 * n])
return c.state()
get_qfi_tc = tc.backend.jit(experimental.qng(s, mode="fwd"))
return benchmark(get_qfi_tc, tc.backend.ones([3 * n * l], dtype="float32"))
def hessian_tc(n, l, trials=10):
def f(params):
c = tc.Circuit(n)
for j in range(l):
for i in range(n - 1):
c.cnot(i, i + 1)
for i in range(n):
c.rx(i, theta=params[3 * n * j + i])
for i in range(n):
c.rz(i, theta=params[3 * n * j + i + n])
for i in range(n):
c.rx(i, theta=params[3 * n * j + i + 2 * n])
return tc.backend.real(c.expectation(*[[tc.gates.x(), [i]] for i in range(n)]))
get_hs_tc = tc.backend.jit(tc.backend.hessian(f))
return benchmark(get_hs_tc, tc.backend.ones([3 * n * l], dtype="float32"))
results = {}
for n in [4, 6, 8, 10, 12]:
for l in [2, 4, 6]:
_, ts = grad_qiskit(n, l)
results[str(n) + "-" + str(l) + "-" + "grad" + "-qiskit"] = ts[1]
_, ts = qfi_qiskit(n, l)
results[str(n) + "-" + str(l) + "-" + "qfi" + "-qiskit"] = ts[0]
_, ts = hessian_qiskit(n, l)
results[str(n) + "-" + str(l) + "-" + "hs" + "-qiskit"] = ts[0]
with tc.runtime_backend("tensorflow"):
_, ts = grad_tc(n, l)
results[str(n) + "-" + str(l) + "-" + "grad" + "-tc-tf"] = ts
_, ts = qfi_tc(n, l)
results[str(n) + "-" + str(l) + "-" + "qfi" + "-tc-tf"] = ts
_, ts = hessian_tc(n, l)
results[str(n) + "-" + str(l) + "-" + "hs" + "-tc-tf"] = ts
with tc.runtime_backend("jax"):
_, ts = grad_tc(n, l)
results[str(n) + "-" + str(l) + "-" + "grad" + "-tc-jax"] = ts
_, ts = qfi_tc(n, l)
results[str(n) + "-" + str(l) + "-" + "qfi" + "-tc-jax"] = ts
_, ts = hessian_tc(n, l)
results[str(n) + "-" + str(l) + "-" + "hs" + "-tc-jax"] = ts
print(results)
with open("gradient_results.data", "w") as f:
json.dump(results, f)
with open("gradient_results.data", "r") as f:
print(json.load(f))