-
Notifications
You must be signed in to change notification settings - Fork 0
/
ag.py
146 lines (115 loc) · 3.3 KB
/
ag.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
from functools import wraps # used for the drawing of the graphs
import networkx as nx
def output_architecture(func):
"""A function to generate an image of the generated architecture, just by wrapping the function with this decorator"""
@wraps(func)
def wrapper(*args, **kwargs):
g = func(*args, **kwargs)
import matplotlib.pyplot as plt # For drawing the graphs
nx.draw(g, with_labels=True, font_weight="bold")
# nx.draw_kamada_kawai(g, with_labels=True, font_weight="bold")
plt.savefig("debug/architecture.png")
return g
return wrapper
# IBM Q Tokyo (Q20)
@output_architecture
def q20():
g = nx.Graph()
g.add_nodes_from(list(range(20)))
for i in range(0, 4):
g.add_edge(i, i + 1)
g.add_edge(i + 1, i)
for i in range(5, 9):
g.add_edge(i, i + 1)
g.add_edge(i + 1, i)
for i in range(10, 14):
g.add_edge(i, i + 1)
g.add_edge(i + 1, i)
for i in range(15, 19):
g.add_edge(i, i + 1)
g.add_edge(i + 1, i)
for i in range(0, 15):
g.add_edge(i, i + 5)
g.add_edge(i + 5, i)
for i in [1, 3, 5, 7, 11, 13]:
g.add_edge(i, i + 6)
g.add_edge(i + 6, i)
for i in [2, 4, 6, 8, 12, 14]:
g.add_edge(i, i + 4)
g.add_edge(i + 4, i)
return g
# IBM Q Tokyo (Q20)
@output_architecture
def qgrid(m, n):
g = nx.Graph()
g.add_nodes_from(list(range(0, m * n - 1)))
for i in range(0, m):
for j in range(0, n):
if i < m - 1:
g.add_edge(j * m + i, j * m + i + 1)
if j < n - 1:
g.add_edge(j * m + i, (j + 1) * m + i)
return g
# IBM Q Rochester (53 qubit)
def rochester():
g = nx.Graph()
g.add_nodes_from(list(range(0, 53)))
I_1 = list(range(4)) + list(range(7, 15)) + list(range(19, 27)) + list(range(30, 38)) + list(range(42, 50))
for i in I_1:
g.add_edge(i, i + 1)
E = [
(0, 5),
(5, 9),
(4, 6),
(6, 13),
(7, 16),
(16, 19),
(11, 17),
(17, 23),
(15, 18),
(18, 27),
(21, 28),
(28, 32),
(25, 29),
(29, 36),
(30, 39),
(39, 42),
(34, 40),
(40, 46),
(38, 41),
(41, 50),
(44, 51),
(48, 52),
]
g.add_edges_from(E)
return g
@output_architecture
def sycamore():
g = nx.Graph()
g.add_nodes_from(list(range(0, 54)))
I = list(range(6, 12)) + list(range(18, 24)) + list(range(30, 36)) + list(range(42, 48))
for i in I:
for j in g.nodes():
if j in I:
continue
if i - j in [5, 6] or j - i in [6, 7]:
g.add_edge(i, j)
g.remove_node(3)
sorted(g)
if 3 in g.nodes():
raise Exception("node error")
mapping = dict()
for n in g.nodes():
if n < 3:
mapping[n] = n
else:
mapping[n] = n - 1
h = nx.relabel_nodes(g, mapping)
sorted(h)
return h
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# _________________TEST_____________________________________________________________#
if __name__ == "__main__":
AG = sycamore()
# print(AG.edges())
print(nx.adjacency_matrix(AG, weight=None))