-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweighted_topology.py
619 lines (541 loc) · 18.6 KB
/
weighted_topology.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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
"""
Takes a circuit (qasm file) and a physical topology and produces a hybrid
logical-physical topology.
"""
from __future__ import annotations
import itertools
from logging import log
import pickle
from posix import listdir
import re
from sys import intern
from typing import Any, Dict, Sequence, Tuple
from re import match, findall
from pickle import load
from networkx.classes.function import degree
from util import get_mapping_results, get_original_count, get_remapping_results, load_block_circuit, load_block_topology
from networkx import Graph, shortest_path_length
import networkx
from networkx.algorithms.shortest_paths.generic import shortest_path
from itertools import combinations
from bqskit import Circuit
from bqskit.ir.lang.qasm2.qasm2 import OPENQASM2Language
from networkx.generators.ego import ego_graph
def check_multi(qasm_line) -> tuple[int] | None:
"""
Determine if a line of QASM code is a multi-qubit interaction. If it is,
return a tuple of ints (control, target).
"""
if bool(match("cx", qasm_line)) or bool(match("swap", qasm_line)):
# line is in the form - cx q[<control>], q[<target>];
q = findall('\d+', qasm_line)
u = min(int(q[0]), int(q[1]))
v = max(int(q[0]), int(q[1]))
return (u,v)
else:
return None
def is_same(a : Sequence[int], b : Sequence[int]) -> bool:
"""True if edges are equivalent."""
if (a[0], a[1]) == (b[0], b[1]) or (a[1], a[0]) == (b[0], b[1]):
return True
else:
return False
def get_logical_operations(
circuit: Circuit,
qudit_group: Sequence[int] | None = None,
) -> Sequence[Sequence[int]]:
logical_operations = []
for op in circuit:
if len(op.location) > 1:
# TODO: handle multi qubit gates
if qudit_group is not None:
a = min([qudit_group[op.location[0]],
qudit_group[op.location[1]]])
b = max([qudit_group[op.location[0]],
qudit_group[op.location[1]]])
else:
a = min([op.location[0], op.location[1]])
b = max([op.location[0], op.location[1]])
logical_operations.append((a,b))
return logical_operations
def get_frequencies(
circuit: Circuit,
qudit_group: Sequence[int] | None = None,
) -> Dict[Tuple, int]:
frequencies = {}
logical_operations = []
for op in circuit:
if len(op.location) > 1:
# TODO: handle multi qubit gates
#for edge in combinations(op.location, 2):
# logical_operations.append(edge)
if qudit_group is not None:
a = min([qudit_group[op.location[0]],
qudit_group[op.location[1]]])
b = max([qudit_group[op.location[0]],
qudit_group[op.location[1]]])
else:
a = min([op.location[0], op.location[1]])
b = max([op.location[0], op.location[1]])
logical_operations.append((a,b))
to_count = set(logical_operations)
for edge in to_count:
frequencies[edge] = logical_operations.count(edge)
return frequencies
def is_internal(
physical_topology: Graph,
qudit_group: Sequence[int],
edge: tuple[int],
) -> bool:
# Check for a path in the subgraph if no blocksize is provided
subgraph = physical_topology.subgraph(qudit_group)
try:
return shortest_path_length(subgraph,edge[0],edge[1]) < len(qudit_group)
except networkx.exception.NetworkXNoPath:
return False
def get_external_edges(
logical_operations : Sequence[Sequence[int]],
physical_topology : Graph,
qudit_group : Sequence[int],
) -> Sequence[Sequence[int]]:
"""
Gates that require a logical edge to be inserted into the hybrid topology.
"""
return [
(u,v) for (u,v) in logical_operations if not
is_internal(physical_topology, qudit_group, (u,v))
]
def get_indirect_edges(
logical_operations : Sequence[Sequence[int]],
physical_topology : Graph,
qudit_group : Sequence[int],
) -> Sequence[Sequence[int]]:
"""
Gates that can be implemented on physical edges but non adjacent vertices.
"""
direct = get_direct_edges(logical_operations, physical_topology)
internal = [
edge for edge in logical_operations if
is_internal(physical_topology, qudit_group, edge)
]
indirect = [
(u,v) for (u,v) in internal if
(u,v) not in direct and (v,u) not in direct
]
return indirect
def get_direct_edges(
logical_operations : Sequence[Sequence[int]],
physical_topology : Graph,
) -> Sequence[Sequence[int]]:
"""
Gates that correspond directly to edges in the physical topology.
"""
return [
(u,v) for (u,v) in logical_operations if (u,v) in
physical_topology.edges or (v,u) in physical_topology.edges
]
def collect_stats_tuples(
circuit : Circuit,
physical_graph : Graph,
kernel : Graph | Sequence[tuple[int]],
qudit_group : Sequence[int],
blocksize : int | None = None,
options : dict[str, Any] | None = None,
) -> Sequence:
# NOTE: may break if idle qudit are removed
blocksize = len(qudit_group) if blocksize is None else blocksize
logical_ops = get_logical_operations(circuit, qudit_group)
direct = get_direct_edges(logical_ops, physical_graph)
indirect = get_indirect_edges(logical_ops, physical_graph, qudit_group)
external = get_external_edges(logical_ops, physical_graph, qudit_group)
active_qudits = circuit.get_active_qudits()
total_ops = sum([len(direct), len(indirect), len(external)])
pre_stats = (
# active qudits
len(active_qudits),
# direct ops
len(direct),
# indirect ops
len(indirect),
# external ops
len(external),
# cnot count
total_ops,
)
subtopology_stats = (
# number of edges
len(kernel),
# Kernel name
kernel_name(kernel, blocksize)
)
return (pre_stats, subtopology_stats)
def kernel_name(kernel, blocksize) -> str:
if len(kernel) == 4:
kernel_type = "ring"
elif len(kernel) < 3:
kernel_type = "linear"
else:
# kernel is a star if there is a degree 3 vertex
degrees = {x:0 for x in range(blocksize)}
for edge in kernel:
degrees[edge[0]] += 1
degrees[edge[1]] += 1
if max(degrees.values()) == 3:
kernel_type = "star"
elif max(degrees.values()) == 2:
kernel_type = "linear"
else:
kernel_type = "unknown"
return kernel_type
def collect_stats(
circuit : Circuit,
physical_graph : Graph,
kernel: Sequence[tuple[int]],
qudit_group : Sequence[int],
blocksize : int | None = None,
options : dict[str, Any] | None = None,
) -> str:
# NOTE: may break if idle qudit are removed
blocksize = len(qudit_group) if blocksize is None else blocksize
logical_ops = get_logical_operations(circuit, qudit_group)
direct = get_direct_edges(logical_ops, physical_graph)
indirect = get_indirect_edges(logical_ops, physical_graph, qudit_group)
external = get_external_edges(logical_ops, physical_graph, qudit_group)
active_qudits = circuit.get_active_qudits()
kernel_type = kernel_name(kernel, blocksize)
stats = (
f"INFO -\n"
f" blocksize: {blocksize}\n"
f" block: {qudit_group}\n"
f" active: {len(active_qudits)}\n"
f" cnot count: {len(logical_ops)}\n"
f"OPERATION COUNTS & VOLUME-\n"
f" direct ops : {len(direct)}\n"
f" indirect ops : {len(indirect)}\n"
f" external ops : {len(external)}\n"
f"SUBTOPOLOGY -\n"
f" kernel : {kernel_type}"
f" {kernel}"
f" number of edges : {len(list(kernel))}\n"
)
total_ops = sum([len(direct), len(indirect), len(external)])
if options is not None:
options["direct_ops"] += len(direct)
options["indirect_ops"] += len(indirect)
options["external_ops"] += len(external)
if total_ops > options["max_block_length"]:
options["max_block_length"] = total_ops
if total_ops < options["min_block_length"] or \
options["min_block_length"] == 0:
options["min_block_length"] = total_ops
return stats
def get_num_vertex_uses(logical_operations, num_qudits) -> dict[int,int]:
degrees = {x:0 for x in range(num_qudits)}
for a,b in logical_operations:
degrees[a] += 1
degrees[b] += 1
return degrees
# NOTE: only to be used for 4 qudits
def best_line_kernel(op_set, freqs) -> Sequence[tuple[int]]:
edges = sorted(list(op_set), key=lambda x: freqs[x], reverse=True)
kernel_edges = []
if len(edges) < 3:
return edges
else:
used_qudits = set([])
for i in range(2):
kernel_edges.append(edges[i])
used_qudits.add(edges[i][0])
used_qudits.add(edges[i][1])
for i in range(2,len(edges)):
if edges[i][0] in used_qudits and edges[i][1] in used_qudits:
if len(kernel_edges) == 2 and len(used_qudits) == 4:
kernel_edges.append(edges[i])
break
else:
kernel_edges.append(edges[i])
break
return kernel_edges
# NOTE: only to be used for 4 qudits
def best_ring_kernel(op_set, freqs) -> Sequence[tuple[int]]:
# Keep the first 3 edges. If there's a star, remove the third edge
# and form a ring. If there's a line, add the last edge to make it
# a ring.
edges = sorted(list(op_set), key=lambda x: freqs[x], reverse=True)
kernel_edges = [edges[x] for x in range(3)]
degrees = get_num_vertex_uses(kernel_edges, 4)
v = sorted(degrees.keys(), key=lambda x: degrees[x], reverse=True)
# Handling star
if degrees[v[0]] == 3:
removed_edge = kernel_edges.pop(-1)
corner_vertex = removed_edge[0] if removed_edge[0] != v[0] \
else removed_edge[1]
# Add other 2 edges to make a ring
for i in range(1,4):
if corner_vertex != v[i]:
kernel_edges.append((corner_vertex, v[i]))
# Handling line
else:
# add edge between the two vertices with degree 1
kernel_edges.append((v[-1], v[-2]))
return kernel_edges
def best_star_kernel(vertex_uses) -> Sequence[tuple[int]]:
# Return the star graph with the most used vertex in the center.
q = sorted(vertex_uses.keys(), key=lambda x: vertex_uses[x],
reverse=True)
return [(q[1],q[0]), (q[2],q[0]), (q[3],q[0])]
def select_linear_kernel(
circuit_file : str,
qudit_group : Sequence[int],
options : dict[str],
) -> Graph | None:
# Convert the physical topology to a networkx graph
circuit = load_block_circuit(circuit_file, options)
logical_ops = get_logical_operations(circuit)
op_set = set(logical_ops)
freqs = get_frequencies(circuit)
return best_line_kernel(op_set, freqs)
def select_falcon_kernel(
circuit_file : str,
qudit_group : Sequence[int],
options : dict[str],
) -> Graph | None:
"""
Given a qasm file and a physical topology, produce a hybrid topology where
logical edges are added for unperformable gates.
Args:
circuit_file (str): Path to the file specifying the circuit block.
qudit_group (Sequence[int]): Only qudit_group members will be used as
end points of logical edges.
options (dict[str]):
blocksize (int): Defines the size of qudit groups
is_qasm (bool): Whether the circuit_file is qasm or pickle.
kernel_dir (str): Directory in which the kernel files are stored.
Returns:
kernel_edge_set (set[tuple[int]]): Edges to be used for synthesis.
Raises:
ValueError: If `blocksize` key is not in `options`.
"""
if "blocksize" not in options:
raise ValueError("The `blocksize` entry in `options` is required.")
elif options["blocksize"] > 4:
raise RuntimeError(
"Only blocksizes up to 4 are currently supported."
)
# Convert the physical topology to a networkx graph
circuit = load_block_circuit(circuit_file, options)
logical_ops = get_logical_operations(circuit)
op_set = set(logical_ops)
freqs = get_frequencies(circuit)
vertex_uses = get_num_vertex_uses(logical_ops, len(qudit_group))
vertex_degrees = get_num_vertex_uses(op_set, len(qudit_group))
# Handle the case where there are no multi-qubit gates
if len(op_set) == 0:
return []
# TODO: Generalize kernel selection for blocksizes > 4
# Return linear 3 graph, with most used qudit in center
if len(qudit_group) == 2 or len(qudit_group) == 3:
return list(op_set)
elif len(qudit_group) == 4:
number_edges = len(op_set)
# If there are 2 edges and 4 active qudits, then they are disjoint,
# just return the edges that are already used. This should not happen
# with the greedy partitioner.
if number_edges == 2:
return list(op_set)
# If there are 3 edges an 4 active qudits, we can have a star or a line.
# If a vertex has degree 3, we have a star, else we have a line.
elif number_edges == 3:
if max(vertex_degrees.keys(), key=lambda x: vertex_degrees[x]) == 3:
return best_star_kernel(vertex_uses)
# Order the edges so that the most frequent ones are first, this should
# be achieved by keeping the original ordering of the logical ops
else:
return list(op_set)
# If there are 4 edges, we can have a ring or a dipper. Based off tests run,
# we should just return a ring in this scenario. Because this uses the
# falcon topology, we instead have to return a line or star. Tests show
# that lines tend to be better.
elif number_edges == 4:
return best_line_kernel(op_set, freqs)
# If there are 5 edges, We have a ring with a bridge. Tests show that if
# there is a vertex that is used more than the others (by about 3), then
# we should select a star. Otherwise select a ring. Add edges based on
# their frequencies.
elif number_edges == 5:
v_uses_list = sorted(vertex_uses.values(), reverse=True)
if v_uses_list[0] - 3 >= v_uses_list[1]:
return best_star_kernel(vertex_uses)
else:
return best_line_kernel(op_set, freqs)
# We have K_4
# Return a ring
else:
return best_line_kernel(op_set, freqs)
def select_mesh_kernel(
circuit_file : str,
qudit_group : Sequence[int],
options : dict[str],
) -> Graph | None:
"""
Given a qasm file and a physical topology, produce a hybrid topology where
logical edges are added for unperformable gates.
Args:
circuit_file (str): Path to the file specifying the circuit block.
qudit_group (Sequence[int]): Only qudit_group members will be used as
end points of logical edges.
options (dict[str]):
blocksize (int): Defines the size of qudit groups
is_qasm (bool): Whether the circuit_file is qasm or pickle.
kernel_dir (str): Directory in which the kernel files are stored.
Returns:
kernel_edge_set (set[tuple[int]]): Edges to be used for synthesis.
Raises:
ValueError: If `blocksize` key is not in `options`.
"""
if "blocksize" not in options:
raise ValueError("The `blocksize` entry in `options` is required.")
elif options["blocksize"] > 4:
raise RuntimeError(
"Only blocksizes up to 4 are currently supported."
)
# Convert the physical topology to a networkx graph
circuit = load_block_circuit(circuit_file, options)
logical_ops = get_logical_operations(circuit)
op_set = set(logical_ops)
freqs = get_frequencies(circuit)
vertex_uses = get_num_vertex_uses(logical_ops, len(qudit_group))
vertex_degrees = get_num_vertex_uses(op_set, len(qudit_group))
# Handle the case where there are no multi-qubit gates
if len(op_set) == 0:
return []
# TODO: Generalize kernel selection for blocksizes > 4
# Return linear 3 graph, with most used qudit in center
if len(qudit_group) == 2 or len(qudit_group) == 3:
return list(op_set)
elif len(qudit_group) == 4:
number_edges = len(op_set)
# If there are 2 edges and 4 active qudits, then they are disjoint,
# just return the edges that are already used. This should not happen
# with the greedy partitioner.
if number_edges == 2:
return list(op_set)
# If there are 3 edges an 4 active qudits, we can have a star or a line.
# If a vertex has degree 3, we have a star, else we have a line.
elif number_edges == 3:
if max(vertex_degrees.keys(), key=lambda x: vertex_degrees[x]) == 3:
return best_star_kernel(vertex_uses)
# Order the edges so that the most frequent ones are first, this should
# be achieved by keeping the original ordering of the logical ops
else:
return list(op_set)
# If there are 4 edges, we can have a ring or a dipper. Based off tests run,
# we should just return a ring in this scenario.
elif number_edges == 4:
return best_ring_kernel(op_set, freqs)
# If there are 5 edges, We have a ring with a bridge. Tests show that if
# there is a vertex that is used more than the others (by about 3), then
# we should select a star. Otherwise select a ring. Add edges based on
# their frequencies.
elif number_edges == 5:
v_uses_list = sorted(vertex_uses.values(), reverse=True)
if v_uses_list[0] - 3 >= v_uses_list[1]:
return best_star_kernel(vertex_uses)
else:
return best_ring_kernel(op_set, freqs)
# We have K_4
# Return a ring
else:
return best_ring_kernel(op_set, freqs)
def run_stats(
options : dict[str, Any],
post_stats : bool = False,
resynthesized : bool = False,
) -> str:
# Get the subtopology files
sub_files = listdir(options["subtopology_dir"])
sub_files.remove(f"summary.txt")
sub_files = sorted(sub_files)
# Get the block files
if not post_stats:
block_files = listdir(options["partition_dir"])
block_files.remove(f"structure.pickle")
else:
if not resynthesized:
blocks = listdir(options["synthesis_dir"])
else:
blocks = listdir(options["resynthesis_dir"])
block_files = []
for bf in blocks:
if bf.endswith(".qasm"):
block_files.append(bf)
block_files = sorted(block_files)
# Init all the needed variables
options["direct_ops"] = 0
options["indirect_ops"] = 0
options["external_ops"] = 0
# Get the qudit group
with open(f"{options['partition_dir']}/structure.pickle", "rb") as f:
structure = pickle.load(f)
## Run collect_stats on each block
#for block_num in range(len(block_files)):
# # Get BQSKIT circuit
# if not post_stats:
# with open(f"{options['partition_dir']}/{block_files[block_num]}",
# "r") as qasm:
# circ = OPENQASM2Language().decode(qasm.read())
# elif not resynthesized:
# with open(f"{options['synthesis_dir']}/{block_files[block_num]}",
# "r") as qasm:
# circ = OPENQASM2Language().decode(qasm.read())
# else:
# with open(f"{options['resynthesis_dir']}/{block_files[block_num]}",
# "r") as qasm:
# circ = OPENQASM2Language().decode(qasm.read())
#
# # Get physical graph
# with open(options["coupling_map"], "rb") as graph:
# physical = pickle.load(graph)
# pgraph = Graph()
# pgraph.add_edges_from(physical)
# # Get hybrid graph
# with open(f"{options['subtopology_dir']}/{sub_files[block_num]}",
# "rb") as graph:
# hybrid = pickle.load(graph)
#
# collect_stats(
# circ,
# pgraph,
# hybrid,
# structure[block_num],
# options = options,
# )
if resynthesized:
string = "REPLACE-\n"
elif post_stats:
string = "POST-\n"
else:
string = "PRE-\n"
string += (
f" direct ops : {options['direct_ops']}\n"
f" indirect ops : {options['indirect_ops']}\n"
f" external ops : {options['external_ops']}\n"
)
if post_stats:
string += get_mapping_results(options)
elif resynthesized:
string += get_remapping_results(options)
else:
string += get_original_count(options)
return string
def select_kernel(
circuit_file : str,
qudit_group : Sequence[int],
options : dict[str],
) -> Graph | None:
if options["coupling_map"] == "mesh":
return select_mesh_kernel(circuit_file, qudit_group, options)
elif options["coupling_map"] == "falcon":
return select_falcon_kernel(circuit_file, qudit_group, options)
else:
return select_linear_kernel(circuit_file, qudit_group, options)