-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathqml_tfquantum.py
160 lines (139 loc) · 4.47 KB
/
qml_tfquantum.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
import sys
import tensorflow as tf
import cirq
import sympy
import numpy as np
import datetime
import cpuinfo
import os
import uuid
import utils
def tfquantum_benchmark(
uuid,
nwires,
nlayer,
nitrs,
timeLimit,
isgpu,
train_img,
test_img,
train_lbl,
test_lbl,
nbatch,
):
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()
import tensorflow_quantum as tfq
meta["Software"] = "tensorflow quantum"
meta["Cpuinfo"] = cpuinfo.get_cpu_info()["brand_raw"]
meta["Version"] = {
"sys": sys.version,
"cirq": cirq.__version__,
"tensorflow": tf.__version__,
"tensorflow_quantum": tfq.__version__,
"numpy": np.__version__,
}
meta["QML test parameters"] = {
"nQubits": nwires,
"nlayer": nlayer,
"nitrs": nitrs,
"timeLimit": timeLimit,
"nbatch": nbatch,
}
meta["UUID"] = uuid
meta["Benchmark Time"] = (
datetime.datetime.now().astimezone().strftime("%Y-%m-%d %H:%M %Z")
)
meta["Results"] = {}
qubits = [cirq.GridQubit(0, i) for i in range(nwires)]
my_symbol = sympy.symbols("params_0:" + str(nwires * 2 * nlayer))
model_circuit = cirq.Circuit()
for j in range(nlayer):
for i in range(nwires - 1):
model_circuit.append(
cirq.ZZPowGate(exponent=my_symbol[j * nwires * 2 + i])(
qubits[i], qubits[nwires - 1]
)
)
for i in range(nwires):
model_circuit.append(
cirq.rx(my_symbol[j * nwires * 2 + nwires + i])(qubits[i])
)
model = tf.keras.Sequential(
[
# The input is the data-circuit, encoded as a tf.string
tf.keras.layers.Input(shape=(), dtype=tf.string),
# The PQC layer returns the expected value of the readout gate, range [0,1].
tfq.layers.PQC(model_circuit, cirq.Z(qubits[nwires - 1]) * 0.5 + 0.5),
]
)
def img2circuit(img):
circuit = cirq.Circuit()
for i in range(nwires - 1):
circuit.append(cirq.rx(img[i] * np.pi)(qubits[i]))
return circuit
utils.qml_data["train_img_tfq"] = tfq.convert_to_tensor(
[img2circuit(x) for x in train_img]
)
print("img2circuit completed!")
opt = tf.keras.optimizers.Adam(0.1)
def vag_tfq(imgs, lbls):
with tf.GradientTape() as t:
t.watch(model.trainable_variables)
loss = tf.reduce_mean((tf.reshape(model(imgs), [-1]) - lbls) ** 2)
grad = t.gradient(loss, model.trainable_variables)
return loss, grad
def f(train_imgs, train_lbls):
loss, grad = vag_tfq(train_imgs, train_lbls)
opt.apply_gradients(zip(grad, model.trainable_variables))
return loss
ct, it, Nitrs = utils.qml_timing(f, nbatch, nitrs, timeLimit, tfq=True)
meta["Results"]["keras layer"] = {
"Construction time": ct,
"Iteration time": it,
"# of actual iterations": Nitrs,
}
# @tf.function
# def vag_tfq(imgs, lbls):
# with tf.GradientTape() as t:
# t.watch(model.trainable_variables)
# loss = tf.reduce_mean((tf.reshape(model(imgs), [-1]) - lbls) ** 2)
# grad = t.gradient(loss, model.trainable_variables)
# return loss, grad
# def f(train_imgs, train_lbls):
# loss, grad = vag_tfq(train_imgs, train_lbls)
# opt.apply_gradients(zip(grad, model.trainable_variables))
# return loss
# ct, it, Nitrs = utils.qml_timing(f, nbatch, nitrs, timeLimit, tfq=True)
# meta["Results"]["keras layer & tf function"] = {
# "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, nbatch = utils.arg(qml=True)
train_img, test_img, train_lbl, test_lbl = utils.mnist_data_preprocessing(n - 1)
results = tfquantum_benchmark(
_uuid,
n,
nlayer,
nitrs,
timeLimit,
isgpu,
train_img,
test_img,
train_lbl,
test_lbl,
nbatch,
)
utils.save(results, _uuid, path)