-
Notifications
You must be signed in to change notification settings - Fork 0
/
circuit.py
executable file
·311 lines (197 loc) · 7.69 KB
/
circuit.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import config
from qiskit import QuantumCircuit, execute, Aer, IBMQ
from qiskit.tools.monitor import job_monitor
from qiskit.extensions.standard.barrier import Barrier
from qiskit.aqua.components.initial_states import Custom
from torch import Tensor
from numpy import ndarray
from numpy import array
from numpy import pi, sqrt
from numpy import power, abs
from copy import deepcopy
##
def prop_circuit(params, timestep, mode='theoretical', backend=None, hm_trials=1000, show_details=False):
if type(params)==Tensor: params = params.detach().numpy()
if type(params)==ndarray: params = params.flatten()
if type(timestep)==Tensor: timestep = timestep.detach().numpy()
if type(timestep)==ndarray: timestep = timestep.flatten()
c = make_circuit(params,timestep)
if mode == 'theoretical':
return [abs(e)**2 for e in run_circuit(c,backend='state_vector')]
elif mode == 'experimental':
return array([v for k,v in run_circuit(c,backend=backend,hm_trials=hm_trials,display_job_status=show_details).items()])
##
def phase_encoder(c, timestep):
hm_qbits = len(c.qubits)
reversed_qbit_ids = list(reversed(range(hm_qbits)))
distance_checks = [2**i for i in reversed_qbit_ids]
c.h(reversed_qbit_ids)
for i,e in enumerate(timestep):
if e != 0:
c_e = QuantumCircuit(hm_qbits,hm_qbits)
xs_to = []
for j,dist in enumerate(distance_checks):
if i < dist:
c_e.x(reversed_qbit_ids[j])
xs_to.append(reversed_qbit_ids[j])
else:
i -=dist
mcz(c_e,reversed_qbit_ids)
for x_to in reversed(xs_to):
c_e.x(x_to)
c += c_e
c.barrier()
def amplitude_modifier(hm_qbits=None):
if not hm_qbits: hm_qbits = config.hm_qbits
the_circuit = []
prev_circuit = None
for current_qbit in reversed(range(hm_qbits)):
if current_qbit == hm_qbits-1:
circuit = [['RY', current_qbit]]
prev_circuit = deepcopy(circuit)
the_circuit.extend(circuit)
else:
circuit = []
circuit.append(['CX', hm_qbits-1, current_qbit])
for i, element in enumerate(prev_circuit):
prev_circuit[i] = [e if ii == 0 else e - 1 for ii, e in enumerate(element)]
circuit += reversed(prev_circuit)
half_circuit = deepcopy(circuit)
circuit.append(['CX', hm_qbits-1, current_qbit])
circuit += list(reversed(half_circuit))[:-1]
prev_circuit = deepcopy(circuit)
the_circuit.extend(circuit)
return the_circuit
##
def make_circuit(params, timestep, hm_layers=config.circuit_layers):
params = params * 2*pi
c = QuantumCircuit(config.hm_qbits, config.hm_qbits)
if config.reconstruct_qstate:
c += Custom(config.hm_qbits,state_vector=[e for e in sqrt(timestep)]+[0]*(config.statevec_size-config.timestep_size)).construct_circuit()
#dis very finally. #c.initialize([e for e in sqrt(timestep)]+[0]*(config.statevec_size-config.timestep_size), range(config.hm_qbits))
ctr = 0
for i in range(hm_layers):
if config.ansatz_mode == 0:
the_circuit = amplitude_modifier()
for element in the_circuit:
if element[0] == 'RY':
c.ry(params[ctr],element[1])
ctr +=1
elif element[0] == 'CX':
c.cx(element[1],element[2])
elif config.ansatz_mode == 1:
for id in range(config.hm_qbits):
c.u3(params[ctr], params[ctr+1], params[ctr+2], id)
ctr +=3
make_entangle(c)
c.barrier()
return c
def make_entangle(c):
if config.entangle_mode == -1:
pass
elif config.entangle_mode == 0:
for id in range(config.hm_qbits-1):
c.cx(id, id+1)
c.cx(config.hm_qbits-1, 0)
elif config.entangle_mode == 1:
wire_ids = list(range(config.hm_qbits))
for id_from in wire_ids:
for id_to in wire_ids:
if id_from != id_to:
c.cx(id_from, id_to)
##
def swap_bit_rank(c):
hm_qbits = c.num_qubits
max_index = hm_qbits-1
for i in range(hm_qbits//2):
c.swap(i,max_index-i)
def mcz(c, *args):
c.h(args[-1])
c.mcx(args[:-1],args[-1])
c.h(args[-1])
##
def find_backend(show_details=False, avoid_16=True):
min_qbits = config.hm_qbits
min_pending = 999_999
min_backend = None
try:
IBMQ.load_account()
provider = IBMQ.get_provider("ibm-q")
except Exception as e:
print(f'Error: could not get IBMQ provider, {e}')
assert None
if config.preferred_backend:
return config.preferred_backend
for backend in provider.backends():
hm_jobs = backend.status().pending_jobs
try:
qbit_count = len(backend.properties().qubits)
if hm_jobs <= min_pending and qbit_count >= min_qbits:
if avoid_16 and qbit_count != 16:
min_pending = hm_jobs
min_backend = backend
except: pass
try:
assert min_backend
if show_details: print(f'picked backend: {min_backend.name()}')
return min_backend
except:
print(f'Error: Backend supporting {min_qbits} qbits not found, defaulting to QASM \n')
return provider.get_backend('ibmq_qasm_simulator')
def run_circuit(circuit,
measure = True,
hm_trials = 1_000,
backend = None,
reverse=False, normalize=True, fill_zero_counts=True, order=True,
draw=False, draw_optimized=False, display_job_status=False,
):
if not backend:
backend = Aer.get_backend('qasm_simulator')
elif type(backend) == str:
if 'state' in backend:
backend = Aer.get_backend('statevector_simulator')
state_vector = execute(circuit, backend).result().get_statevector()
return state_vector
else:
try:
from warnings import filterwarnings
filterwarnings("ignore")
IBMQ.load_account()
provider = IBMQ.get_provider("ibm-q")
backend = provider.get_backend(backend)
except Exception as e:
print(f'Error: could not get IBMQ provider, {e}')
assert None
if display_job_status:
print(f'running on: {backend.name()}')
if measure:
circuit.measure(range(len(circuit.clbits)), range(len(circuit.clbits)))
if draw:
print(circuit.draw())
circuit.data = [e for e in circuit.data if type(e[0]) != Barrier]
if draw_optimized:
print(circuit.draw())
job = execute(circuit, backend=backend, shots=hm_trials)
if display_job_status:
job_monitor(job)
try:
result = job.result()
counts = result.get_counts(circuit)
if fill_zero_counts:
hm_qbits = len(list(counts.keys())[0])
for nr in range(2**hm_qbits):
s = str(bin(nr))[2:]
if len(s) != hm_qbits:
for _ in range(hm_qbits-len(s)):
s = "0"+s
if s not in counts.keys():
counts[s] = 0.0
if reverse:
counts = {k[-1::-1]:v for k, v in counts.items()}
if normalize:
counts = {k:v/hm_trials for k, v in counts.items()}
if order:
counts = dict(sorted(counts.items()))
return counts
except Exception as e: print(f'Error: job failed with {job.error_message()}, {e}')
##