-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutility.py
518 lines (446 loc) · 20.6 KB
/
utility.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# Copyright 2017 Quantum Information Science, University of Parma, Italy. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =============================================================================
__author__ = "Davide Ferrari"
__copyright__ = "Copyright 2017, Quantum Information Science, University of Parma, Italy"
__license__ = "Apache"
__version__ = "2.0"
__email__ = "davide.ferrari8@studenti.unipr.it"
import os
from time import sleep
from devices import *
import logging
import myLogger
import operator
import sys
sys.path.append( # solve the relative dependencies if you clone QISKit from the Git repo and use like a global.
"../qiskit-sdk-py")
from qiskit import QuantumProgram
import Qconfig
logger = logging.getLogger('utility')
logger.addHandler(myLogger.MyHandler())
logger.setLevel(logging.CRITICAL)
logger.propagate = False
class Utility(object):
def __init__(self, coupling_map):
self.__coupling_map = dict()
self.__inverse_coupling_map = dict()
self.__plain_map = dict()
self.__path = dict()
self.__n_qubits = 0
self.__ranks = dict()
self.__connected = dict()
self.__most_connected = []
if coupling_map:
self.__coupling_map = coupling_map.copy()
logger.log(logging.DEBUG, 'init() - coupling_map:\n%s', str(self.__coupling_map))
self.invert_graph(coupling_map, self.__inverse_coupling_map)
logger.log(logging.DEBUG, 'init() - inverse coupling map:\n%s', str(self.__inverse_coupling_map))
for i in coupling_map:
self.__plain_map.update({i: self.__inverse_coupling_map[i] + coupling_map[i]})
logger.debug('init() - plain map:\n%s', str(self.__plain_map))
self.start_explore(self.__coupling_map, self.__ranks)
self.__most_connected = self.find_max(self.__ranks)
self.create_path(self.__most_connected[0], plain_map=self.__plain_map)
else:
logger.critical('init() - Null argument: coupling_map')
exit(1)
def close(self):
self.__ranks.clear()
self.__inverse_coupling_map.clear()
self.__coupling_map.clear()
self.__path.clear()
self.__most_connected.clear()
def explore(self, source, visiting, visited, ranks):
for next in self.__coupling_map[visiting]:
if next not in visited[source]:
visited[source].append(next)
if next not in ranks:
ranks.update({next: 0})
ranks[next] = ranks[next] + 1
self.explore(source, next, visited, ranks)
# TODO Try using some sort of centrality algorithm
def start_explore(self, graph, ranks):
visited = dict()
for source in graph:
visited.update({source: []})
self.explore(source, source, visited, ranks)
# create an inverted coupling-map for further use
@staticmethod
def invert_graph(graph, inverse_graph=None):
if inverse_graph is None:
inverse_graph = {}
for end in graph:
for start in graph[end]:
if start not in inverse_graph:
inverse_graph.update({start: [end]})
else:
inverse_graph[start].append(end)
for node in graph:
if node not in inverse_graph:
inverse_graph.update({node: []})
# find the most connected qubit
@staticmethod
def find_max(ranks):
logger.debug('ranks:\n%s', str(ranks))
most_connected = max(ranks.items(), key=operator.itemgetter(1))[0]
found = [most_connected, ranks[most_connected]]
logger.debug('max: %s', str(found))
return found
# create a valid path that connect qubits used in the circuit
def create_path(self, start, plain_map):
self.__path.update({start: -1})
to_connect = [start]
max = len(self.__coupling_map)
logger.debug('create_path() - max:\n%s', str(max))
count = max - 1
changed = True
visiting = 0
while count > 0:
logger.debug('create_path() - visiting:\n%s - %s', str(visiting), str(to_connect[visiting]))
# for visiting in to_connect:
if count <= 0:
break
for node in plain_map[to_connect[visiting]]:
if count <= 0:
break
if node not in self.__path:
self.__path.update({node: to_connect[visiting]})
count -= 1
logger.debug('create_path() - path:\n%s', str(self.__path))
if node not in to_connect:
to_connect.append(node)
visiting += 1
logger.debug('create_path() - path:\n%s', str(self.__path))
def cx(self, circuit, control_qubit, target_qubit, control, target):
if target in self.__coupling_map[control]:
logger.log(logging.VERBOSE, 'cx() - cnot: (%s, %s)', str(control), str(target))
circuit.cx(control_qubit, target_qubit)
elif control in self.__coupling_map[target]:
logger.log(logging.VERBOSE, 'cx() - inverse-cnot: (%s, %s)', str(control), str(target))
circuit.h(control_qubit)
circuit.h(target_qubit)
circuit.cx(target_qubit, control_qubit)
circuit.h(control_qubit)
circuit.h(target_qubit)
else:
logger.critical('cx() - Cannot connect qubit %s to qubit %s', str(control), str(target))
exit(3)
# place cnot gates based on the path created in create_path method
def place_cx(self, circuit, quantum_r, oracle='11'):
if not oracle == '00':
logger.log(logging.VERBOSE, 'place_cx() - oracle != 00')
stop = self.__n_qubits // 2
for qubit in self.__connected:
if self.__connected[qubit] != -1:
if oracle == '11':
logger.log(logging.VERBOSE, 'place_cx() - oracle = 11')
self.cx(circuit, quantum_r[qubit], quantum_r[self.__connected[qubit]], qubit,
self.__connected[qubit])
elif oracle == '10':
logger.log(logging.VERBOSE, 'place_cx() - oracle = 10')
if stop > 0:
self.cx(circuit, quantum_r[qubit], quantum_r[self.__connected[qubit]], qubit,
self.__connected[qubit])
stop -= 1
# place Hadamard gates
def place_h(self, circuit, start, quantum_r, initial=True, x=True):
for qubit in self.__connected:
if qubit != start:
circuit.h(quantum_r[qubit])
else:
if initial is True:
if x is True:
circuit.x(quantum_r[qubit])
else:
circuit.h(quantum_r[qubit])
# place Pauli-X gates
def place_x(self, circuit, quantum_r):
sorted_c = sorted(self.__connected.items(), key=operator.itemgetter(0))
logger.log(logging.VERBOSE, 'place_x() - sorted_c:\n%s', str(sorted_c))
s_0 = self.__n_qubits // 2
i = 0
count = self.__n_qubits - 1
for qubit in sorted_c:
if count <= 0:
break
if i >= s_0:
circuit.x(quantum_r[qubit[0]])
else:
circuit.iden(quantum_r[qubit[0]])
i += 1
i = 0
for qubit in sorted_c:
if i >= s_0:
circuit.iden(quantum_r[qubit[0]])
else:
circuit.x(quantum_r[qubit[0]])
i += 1
# final measure
def measure(self, circuit, quantum_r, classical_r):
for qubit in self.__connected:
circuit.measure(quantum_r[qubit], classical_r[qubit])
# create the circuit
def create(self, circuit, quantum_r, classical_r, n_qubits, x=True, oracle='11'):
self.__n_qubits = n_qubits
max_qubits = len(self.__path)
logger.debug('create() - N qubits: %s', str(self.__n_qubits))
logger.debug('create() - Max qubits: %s', str(max_qubits))
if max_qubits < self.__n_qubits:
logger.critical('create() - Can use only up to %s qubits', str(max_qubits))
exit(2)
count = self.__n_qubits
for qubit in self.__path:
if count <= 0:
break
self.__connected.update({qubit: self.__path[qubit]})
count -= 1
logger.debug('create() - connected:\n%s', str(self.__connected))
self.place_h(circuit, self.__most_connected[0], quantum_r, x=x)
self.place_cx(circuit, quantum_r, oracle=oracle)
self.place_h(circuit, self.__most_connected[0], quantum_r, initial=False)
if x is True:
self.place_x(circuit, quantum_r)
self.measure(circuit, quantum_r, classical_r)
def envariance(self, circuit, quantum_r, classical_r, n_qubits):
self.create(circuit, quantum_r, classical_r, n_qubits)
sorted_c = sorted(self.__connected.items(), key=operator.itemgetter(0))
connected = list(zip(*sorted_c))[0]
logger.debug('envariance() - connected:\n%s', str(connected))
self.__n_qubits = 0
self.__connected.clear()
return connected
def parity(self, circuit, quantum_r, classical_r, n_qubits, oracle='11'):
self.create(circuit, quantum_r, classical_r, n_qubits, x=False, oracle=oracle)
connected = list(self.__connected.keys())
logger.debug('parity() - connected:\n%s', str(connected))
self.__n_qubits = 0
self.__connected.clear()
return connected
# launch envariance experiment on the given device
def envariance_exec(execution, device, utility, n_qubits, num_shots=1024, directory='Data_Envariance/'):
os.makedirs(os.path.dirname(directory), exist_ok=True)
size = 0
results = dict()
if device == qx2 or device == qx4:
if n_qubits <= 5:
size = 5
# device = 'ibmqx_qasm_simulator'
else:
logger.critical('launch_exp() - Too much qubits for %s !', device)
exit(1)
elif device == qx3 or device == qx5:
if n_qubits <= 16:
size = 16
# device = 'ibmqx_qasm_simulator'
else:
logger.critical('launch_exp() - Too much qubits for %s !', device)
exit(2)
elif device == online_sim:
if n_qubits <= 5:
size = 5
elif n_qubits <= 16:
size = 16
else:
logger.critical('launch_exp() - Unknown device.')
exit(3)
Q_program = QuantumProgram()
try:
Q_program.set_api(Qconfig.APItoken, Qconfig.config["url"]) # set the APIToken and API url
except ConnectionError:
sleep(900)
logger.critical('API Exception occurred, retrying\nQubits %d - Execution %d - Shots %d', n_qubits, execution,
num_shots)
envariance_exec(execution, device, utility, n_qubits=n_qubits, num_shots=num_shots, directory=directory)
return
quantum_r = Q_program.create_quantum_register("qr", size)
classical_r = Q_program.create_classical_register("cr", size)
circuit = Q_program.create_circuit("envariance", [quantum_r], [classical_r])
connected = utility.envariance(circuit=circuit, quantum_r=quantum_r, classical_r=classical_r, n_qubits=n_qubits)
QASM_source = Q_program.get_qasm("envariance")
logger.debug('launch_exp() - QASM:\n%s', str(QASM_source))
while True:
try:
backend_status = Q_program.get_backend_status(device)
if ('available' in backend_status and backend_status['available'] is False) \
or ('busy' in backend_status and backend_status['busy'] is True):
logger.critical('%s currently offline, waiting...', device)
while Q_program.get_backend_status(device)['available'] is False:
sleep(1800)
logger.critical('%s is back online, resuming execution', device)
except ConnectionError:
logger.critical('Error getting backend status, retrying...')
sleep(900)
continue
except ValueError:
logger.critical('Backend is not available, waiting...')
sleep(900)
continue
break
if Q_program.get_api().get_my_credits()['remaining'] < 3:
logger.critical('Qubits %d - Execution %d - Shots %d ---- Waiting for credits to replenish...',
n_qubits, execution, num_shots)
while Q_program.get_api().get_my_credits()['remaining'] < 3:
sleep(900)
logger.critical('Credits replenished, resuming execution')
try:
result = Q_program.execute(["envariance"], backend=device, wait=2, timeout=1000, shots=num_shots, max_credits=5)
except Exception:
sleep(900)
logger.critical('Exception occurred, retrying\nQubits %d - Execution %d - Shots %d', n_qubits, execution,
num_shots)
envariance_exec(execution, device, utility, n_qubits=n_qubits, num_shots=num_shots, directory=directory)
return
try:
counts = result.get_counts("envariance")
except Exception:
logger.critical('Exception occurred, retrying\nQubits %d - Execution %d - Shots %d', n_qubits, execution,
num_shots)
envariance_exec(execution, device, utility, n_qubits=n_qubits, num_shots=num_shots, directory=directory)
return
logger.debug('launch_exp() - counts:\n%s', str(counts))
sorted_c = sorted(counts.items(), key=operator.itemgetter(1), reverse=True)
filename = directory + device + '/' + 'execution' + str(
execution) + '/' + device + '_' + str(num_shots) + '_' + str(
n_qubits) + '_qubits_envariance.txt'
os.makedirs(os.path.dirname(filename), exist_ok=True)
out_f = open(filename, 'w')
# store counts in txt file and xlsx file
out_f.write('VALUES\t\tCOUNTS\n\n')
stop = n_qubits // 2
for i in sorted_c:
reverse = i[0][::-1]
sorted_v = []
for n in range(n_qubits - stop):
sorted_v.append(reverse[connected[n + stop]])
for n in range(stop):
sorted_v.append(reverse[connected[n]])
value = ''.join(str(v) for v in sorted_v)
results.update({value: i[1]})
out_f.write(value + '\t' + str(i[1]) + '\n')
out_f.close()
# launch parity experiment on the given device
def parity_exec(execution, device, utility, n_qubits, oracle='11', num_shots=1024, directory='Data_Parity/'):
os.makedirs(os.path.dirname(directory), exist_ok=True)
size = 0
results = dict()
if device == qx2 or device == qx4:
if n_qubits <= 5:
size = 5
# device = 'ibmqx_qasm_simulator'
else:
logger.critical('launch_exp() - Too much qubits for %s !', device)
exit(1)
elif device == qx3 or device == qx5:
if n_qubits <= 16:
size = 16
# device = 'ibmqx_qasm_simulator'
else:
logger.critical('launch_exp() - Too much qubits for %s !', device)
exit(2)
elif device == online_sim:
if n_qubits <= 5:
size = 5
elif n_qubits <= 16:
size = 16
else:
logger.critical('launch_exp() - Unknown device.')
exit(3)
Q_program = QuantumProgram()
try:
Q_program.set_api(Qconfig.APItoken, Qconfig.config["url"]) # set the APIToken and API url
except ConnectionError:
sleep(900)
logger.critical('API Exception occurred, retrying\nQubits %d - Oracle %s - Execution %d - Queries %d', n_qubits,
oracle,
execution, num_shots)
parity_exec(execution, device, utility, n_qubits=n_qubits, oracle=oracle, num_shots=num_shots, directory=directory)
return
quantum_r = Q_program.create_quantum_register("qr", size)
classical_r = Q_program.create_classical_register("cr", size)
circuit = Q_program.create_circuit('parity', [quantum_r], [classical_r])
connected = utility.parity(circuit=circuit, quantum_r=quantum_r, classical_r=classical_r, n_qubits=n_qubits,
oracle=oracle)
QASM_source = Q_program.get_qasm('parity')
logger.debug('launch_exp() - QASM:\n%s', str(QASM_source))
while True:
try:
backend_status = Q_program.get_backend_status(device)
if ('available' in backend_status and backend_status['available'] is False) \
or ('busy' in backend_status and backend_status['busy'] is True):
logger.critical('%s currently offline, waiting...', device)
while Q_program.get_backend_status(device)['available'] is False:
sleep(1800)
logger.critical('%s is back online, resuming execution', device)
except ConnectionError:
logger.critical('Error getting backend status, retrying...')
sleep(900)
continue
except ValueError:
logger.critical('Backend is not available, waiting...')
sleep(900)
continue
break
if Q_program.get_api().get_my_credits()['remaining'] < 3:
logger.critical('Qubits %d - Oracle %s - Execution %d - Queries %d ---- Waiting for credits to replenish...',
n_qubits, oracle,
execution, num_shots)
while Q_program.get_api().get_my_credits()['remaining'] < 3:
sleep(900)
logger.critical('Credits replenished, resuming execution')
try:
result = Q_program.execute(['parity'], backend=device, wait=2, timeout=1000, shots=num_shots, max_credits=5)
except Exception:
sleep(900)
logger.critical('Exception occurred, retrying\nQubits %d - Oracle %s - Execution %d - Queries %d', n_qubits, oracle,
execution, num_shots)
parity_exec(execution, device, utility, n_qubits=n_qubits, oracle=oracle, num_shots=num_shots, directory=directory)
return
try:
counts = result.get_counts('parity')
except Exception:
logger.critical('Exception occurred, retrying\nQubits %d - Oracle %s - Execution %d - Queries %d', n_qubits, oracle,
execution, num_shots)
parity_exec(execution, device, utility, n_qubits=n_qubits, oracle=oracle, num_shots=num_shots, directory=directory)
return
logger.debug('launch_exp() - counts:\n%s', str(counts))
sorted_c = sorted(counts.items(), key=operator.itemgetter(1), reverse=True)
filename = directory + device + '/' + oracle + '/' + 'execution' + str(
execution) + '/' + device + '_' + str(
num_shots) + 'queries_' + oracle + '_' + str(
n_qubits) + '_qubits_parity.txt'
os.makedirs(os.path.dirname(filename), exist_ok=True)
out_f = open(filename, 'w')
# store counts in txt file and xlsx file
out_f.write('VALUES\t\tCOUNTS\n\n')
logger.debug('launch_exp() - oredred_q:\n%s', str(connected))
stop = n_qubits // 2
for i in sorted_c:
reverse = i[0][::-1]
logger.log(logging.VERBOSE, 'launch_exp() - reverse in for 1st loop: %s', str(reverse))
sorted_v = [reverse[connected[0]]]
logger.log(logging.VERBOSE, 'launch_exp() - connected[0] in 1st for loop: %s', str(connected[0]))
logger.log(logging.VERBOSE, 'launch_exp() - sorted_v in 1st for loop: %s', str(sorted_v))
for n in range(stop):
sorted_v.append(reverse[connected[n + 1]])
logger.log(logging.VERBOSE, 'launch_exp() - connected[n+1], sorted_v[n+1] in 2nd for loop: %s,%s',
str(connected[n + 1]), str(sorted_v[n + 1]))
if (n + stop + 1) != n_qubits:
sorted_v.append(reverse[connected[n + stop + 1]])
logger.log(logging.VERBOSE, 'launch_exp() - connected[n+stop+1], sorted_v[n+2] in 2nd for loop: %s%s',
str(connected[n + stop + 1]), str(sorted_v[n + 2]))
value = ''.join(str(v) for v in sorted_v)
results.update({value: i[1]})
out_f.write(value + '\t' + str(i[1]) + '\n')
out_f.close()