-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathNbTrolsModel.py
275 lines (224 loc) · 7.65 KB
/
NbTrolsModel.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import numpy as np
import numpy.random as npr
import scipy.stats as ss
import utilities as ut
import math
from Model import *
class NbTrolsModel(Model):
"""
In this class and its twin class NoNbTrolsModel, we consider Quantum
circuits of the following kind. Below we represent them in Qubiter ASCII
picture notation in ZL convention, for nb=3 and na=4
[--nb---] [----na-----]
NbTrols (nb Controls) model:
|0> |0> |0> |0> |0> |0> |0>
NOTA P(x) next
|---|---|---|---|---|---Ry
|---|---|---|---|---Ry--%
|---|---|---|---Ry--%---%
|---|---|---Ry--%---%---%
NOTA P(y|x) next
|---|---Ry--%---%---%---%
|---Ry--%---%---%---%---%
Ry--%---%---%---%---%---%
M M M
NoNbTrols (no nb Controls) model:
|0> |0> |0> |0> |0> |0> |0>
NOTA P(x) next
|---|---|---|---|---|---Ry
|---|---|---|---|---Ry--%
|---|---|---|---Ry--%---%
|---|---|---Ry--%---%---%
NOTA P(y|x) next
|---|---Ry--%---%---%---%
|---Ry--|---%---%---%---%
Ry--|---|---%---%---%---%
M M M
A gate |---|---Ry--%---%---%---% is called an MP_Y Multiplexor,
or plexor for short. In Ref.1 (Qubiter repo at github), see Rosetta Stone
pdf and Quantum CSD Compiler folder for more info about multiplexors.
In NbTrols and NoNbtrols models, each layer of a list1 corresponds to a
single plexor. We list plexors (layers) in a list1 in order of
increasing distance between the Ry target qubit and the 0th qubit.
Note that the expansion of a multiplexor into elementary gates (cnots
and single qubit rotations) contains a huge number of gates (exp in the
number of controls). However, such expansions can be shortened by
approximating the multiplexors, using, for instance, the technique of
Ref.2.
Ref.3 explains the motivation for choosing this model. This model is in
fact guaranteed to fully parametrize P(x) and P(y|x).
One can train these circuits in two steps. Consider the NbTrolsModel
circuit as an example. The data consists of many rows with one (y,
x) pair per row.
1. fit P(x) as follows: use the first na=4 gates of the circuit, let the
x part of each row of the data be the output at qubits in range(na).
2. fit P(y | x) as follows: use gates from na=4 to the last one at na +
nb = 7, use pair (y, x) of each row of the data, let x be the input at
qubits in range(na) and y the output at qubits in range(na, na+nb). (
ELBO would be different for steps 1, 2)
An alternative training method is to fit P(y, x) all at once. Use all
na+nb=7 gates, use pair (y, x) of each row of the data, let (y,
x) be the output at qubits in range(na+nb).
The circuits given above are for finding a fit of both P(x) and P(y|x).
However, if one wants to use a physical hardware device as a classifier,
then one should omit the beginning part of the circuits (the parts that
represent P(x)), and feed the input x into the first na qubits. In other
words, for classifying, use the following circuits instead of the ones
above:
[--nb---] [----na-----]
NbTrols (nb Controls) model:
|0> |0> |0>
|---|---Ry--%---%---%---%
|---Ry--%---%---%---%---%
Ry--%---%---%---%---%---%
M M M
NoNbTrols (no nb Controls) model:
|0> |0> |0>
|---|---Ry--%---%---%---%
|---Ry--|---%---%---%---%
Ry--|---|---%---%---%---%
M M M
References
----------
1. https://github.com/artiste-qb-net/qubiter
2. Oracular Approximation of Quantum Multiplexors and Diagonal Unitary
Matrices, by Robert R. Tucci, https://arxiv.org/abs/0901.3851
3. Code Generator for Quantum Simulated Annealing, by Robert R. Tucci,
https://arxiv.org/abs/0908.1633 , Appendix B
4. "Quantum Edward Algebra.pdf", pdf included in this repo
"""
def __init__(self, nb, na):
"""
Constructor
Parameters
----------
nb : int
na : int
Returns
-------
None
"""
Model.__init__(self, nb, na)
def get_shapes1(self):
"""
Returns a list of the shapes of the elements of a list1.
Returns
-------
list[tuple]
"""
na = self.na
nb = self.nb
return [(1 << k,) for k in range(na+nb)]
@staticmethod
def static_prob_x(x, na, list1_angs, verbose=False):
"""
Returns probability of input x, P(x).
Parameters
----------
x : int
x is an int in range(powna).
na : int
list1_angs : list[np.array]
verbose : bool
Returns
-------
float
"""
prob = 1.
# x = input in decimal
xbin = ut.dec_to_bin_vec(x, na)
num_trols = 0
# print(".,.", list1_angs)
for angs in list1_angs[0:na]:
xlast_bit = xbin[num_trols]
if num_trols == 0:
xrest = 0
angs1 = float(angs)
else:
xrest = ut.bin_vec_to_dec(xbin[: num_trols])
angs1 = angs[xrest]
factor = ut.ang_to_cs2_prob(angs1, xlast_bit)
if verbose:
print('num_trols=', num_trols,
"xrest_bin, xlast_bit=",
xbin[: num_trols],
xlast_bit)
prob *= factor
num_trols += 1
if verbose:
print("\txbin, prob:", xbin, prob)
return prob
def prob_x(self, x,
list1_angs,
verbose=False):
"""
Returns probability of input x, P(x).
Parameters
----------
x : int
x is an int in range(powna).
list1_angs : list[np.array]
verbose : bool
Returns
-------
float
"""
return NbTrolsModel.static_prob_x(x, self.na, list1_angs, verbose)
def prob_y_given_x_and_angs_prior(self, y, x,
list1_angs,
verbose=False):
"""
Returns the probability of y given x and list1_angs, P(y | x,
list1_angs).
Parameters
----------
y : int
int in range(pownb)
x : int
int in range(powna)
list1_angs : list[np.array]
verbose : str
Returns
-------
float
"""
na = self.na
nb = self.nb
prob = 1.
# y = output in decimal
ybin = ut.dec_to_bin_vec(y, nb)
# x = input in decimal
xbin = ut.dec_to_bin_vec(x, na)
num_trols = na
for angs in list1_angs[na:]:
ylast_bit = ybin[num_trols-na]
if num_trols == na:
x_yrest_bin = x
x_yrest = x
else:
x_yrest_bin = np.concatenate([xbin,
ybin[: num_trols-na]])
x_yrest = ut.bin_vec_to_dec(x_yrest_bin)
factor = ut.ang_to_cs2_prob(angs[x_yrest], ylast_bit)
if verbose:
print('num_trols=', num_trols,
"x_yrest_bin, ylast_bit=",
x_yrest_bin,
ylast_bit)
prob *= factor
num_trols += 1
if verbose:
print('\txbin, ybin, prob:', xbin, ybin, prob)
return prob
if __name__ == "__main__":
def main():
nsam = 10
na = 2
nb = 3
mod = NbTrolsModel(nb, na)
y_nsam_nb, x_nsam_na = mod.gen_toy_data(nsam, verbose=True)
print("x_nsam_na:")
print(x_nsam_na)
print("\ny_nsam_nb:")
print(y_nsam_nb)
main()