-
Notifications
You must be signed in to change notification settings - Fork 7
/
circuitery.py
203 lines (168 loc) · 6.79 KB
/
circuitery.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
##########################################################################
#Quantum classifier
#Adrián Pérez-Salinas, Alba Cervera-Lierta, Elies Gil, J. Ignacio Latorre
#Code by APS
#Code-checks by ACL
#June 3rd 2019
#Universitat de Barcelona / Barcelona Supercomputing Center/Institut de Ciències del Cosmos
###########################################################################
## This file will create the circuit we will use in order to develop the quantum
# classifier. Ansätze and codings are included in this file
## How this file is related to other files
from QuantumState import QCircuit
def circuit(theta_aux, entanglement):
"""
This creates the Quantum circuit for the problem using QuantumState.QCircuit
INPUT:
-theta_aux: set of parameters needed for the circuit. It is an array with shape (qubits, layers, 3 or 6). Alpha and x are coded inside theta_aux
-entanglement: whether there is entanglement or not in the Ansätze, just 'y'/'n'
OUTPUT:
-quantum circuits coding the problem and our Ansätze
"""
hypar = theta_aux.shape #[qubits, layers, params_per_layer]
entanglement = entanglement.lower()[0]
if hypar[-1] not in [3, 6]:
raise ValueError('The number of parameters per gate is not correct')
if hypar[-1] == 3:
num_qubits = hypar[0]
if num_qubits == 1:
return _qcircuit_1qubit(theta_aux)
elif num_qubits == 2 and entanglement == 'n':
return _qcircuit_2qubit_noentanglement(theta_aux)
elif num_qubits == 2 and entanglement == 'y':
return _qcircuit_2qubit_entanglement(theta_aux)
elif num_qubits == 4 and entanglement == 'n':
return _qcircuit_4qubit_noentanglement(theta_aux)
elif num_qubits == 4 and entanglement == 'y':
return _qcircuit_4qubit_entanglement(theta_aux)
else:
raise ValueError('Not valid')
if hypar[-1] == 6:
num_qubits = hypar[0]
if num_qubits == 1:
return _double_qcircuit_1qubit(theta_aux)
elif num_qubits == 2 and entanglement == 'n':
return _double_qcircuit_2qubit_noentanglement(theta_aux)
elif num_qubits == 2 and entanglement == 'y':
return _double_qcircuit_2qubit_entanglement(theta_aux)
elif num_qubits == 4 and entanglement == 'n':
return _double_qcircuit_4qubit_noentanglement(theta_aux)
elif num_qubits == 4 and entanglement == 'y':
return _double_qcircuit_4qubit_entanglement(theta_aux)
else:
raise ValueError('Not valid')
#Auxiliary functions. Modify these functions for modifying or creating Ansätze
def _qcircuit_1qubit(theta_aux):
C = QCircuit(1)
for l in range(theta_aux.shape[1]):
C.U3(0, theta_aux[0,l,:])
return C
def _qcircuit_2qubit_noentanglement(theta_aux):
C = QCircuit(2)
for l in range(theta_aux.shape[1]):
for q in range(theta_aux.shape[0]):
C.U3(q, theta_aux[q,l,:])
return C
def _qcircuit_2qubit_entanglement(theta_aux):
C = QCircuit(2)
for l in range(theta_aux.shape[1] - 1):
for q in range(theta_aux.shape[0]):
C.U3(q, theta_aux[q,l,:])
C.Cz(0,1)
for q in range(theta_aux.shape[0]):
C.U3(q, theta_aux[q,-1,:])
return C
def _qcircuit_4qubit_noentanglement(theta_aux):
C = QCircuit(4)
for l in range(theta_aux.shape[1]):
for q in range(theta_aux.shape[0]):
C.U3(q, theta_aux[q,l,:])
return C
def _qcircuit_4qubit_entanglement(theta_aux):
C = QCircuit(4)
for l in range(theta_aux.shape[1] - 1):
for q in range(theta_aux.shape[0]):
C.U3(q, theta_aux[q,l,:])
if l%2 == 0:
C.Cz(0,1)
C.Cz(2,3)
elif l%2 == 1:
C.Cz(1,2)
C.Cz(0,3)
for q in range(theta_aux.shape[0]):
C.U3(q, theta_aux[q,-1,:])
return C
#Auxiliary functions. Modify these functions for modifying or creating Ansätze
def _double_qcircuit_1qubit(theta_aux):
C = QCircuit(1)
for l in range(theta_aux.shape[1]):
C.U3(0, theta_aux[0,l,:3])
C.U3(0, theta_aux[0,l,3:])
return C
def _double_qcircuit_2qubit_noentanglement(theta_aux):
C = QCircuit(2)
for l in range(theta_aux.shape[1]):
for q in range(theta_aux.shape[0]):
C.U3(q, theta_aux[q,l,:3])
C.U3(q, theta_aux[q,l,3:])
return C
def _double_qcircuit_2qubit_entanglement(theta_aux):
C = QCircuit(2)
for l in range(theta_aux.shape[1] - 1):
for q in range(theta_aux.shape[0]):
C.U3(q, theta_aux[q,l,:3])
C.U3(q, theta_aux[q,l,3:])
C.Cz(0,1)
for q in range(theta_aux.shape[0]):
C.U3(q, theta_aux[q,-1,:3])
C.U3(q, theta_aux[q,-1,3:])
return C
def _double_qcircuit_4qubit_noentanglement(theta_aux):
C = QCircuit(4)
for l in range(theta_aux.shape[1]):
for q in range(theta_aux.shape[0]):
C.U3(q, theta_aux[q,l,:3])
C.U3(q, theta_aux[q,l,3:])
return C
def _double_qcircuit_4qubit_entanglement(theta_aux):
C = QCircuit(4)
for l in range(theta_aux.shape[1] - 1):
for q in range(theta_aux.shape[0]):
C.U3(q, theta_aux[q,l,:3])
C.U3(q, theta_aux[q,l,3:])
if l%2 == 0:
C.Cz(0,1)
C.Cz(2,3)
elif l%2 == 1:
C.Cz(1,2)
C.Cz(0,3)
for q in range(theta_aux.shape[0]):
C.U3(q, theta_aux[q,-1,:3])
C.U3(q, theta_aux[q,-1,3:])
return C
def code_coords(theta, alpha, x): #Encoding of coordinates
"""
This functions converts theta, alpha and x in a new set of variables encoding the three of them properly
INPUT:
-theta: initial point for the theta parameters. The shape must be correct (qubits, layers, 3)
-alpha: initial point for the alpha parameters. The shape must be correct (qubits, layers, dim)
-x: one data for training, only the coordinates
OUTPUT:
-theta_aux: shifted thetas encoding alpha and x inside. Same shape as theta
"""
theta_aux = theta.copy()
qubits = theta.shape[0]
layers = theta.shape[1]
for q in range(qubits):
for l in range(layers):
if len(x) <= 3:
for i in range(len(x)):
theta_aux[q, l, i] += alpha[q, l, i] * x[i]
elif len(x) == 4:
theta_aux[q, l, 0] += alpha[q, l, 0] * x[0]
theta_aux[q, l, 1] += alpha[q, l, 1] * x[1]
theta_aux[q, l, 3] += alpha[q, l, 2] * x[2]
theta_aux[q, l, 4] += alpha[q, l, 3] * x[3]
else:
raise ValueError('Data has too many dimensions')
return theta_aux