-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinteraction_graphs_cqasm.py
More file actions
76 lines (62 loc) · 1.75 KB
/
Copy pathinteraction_graphs_cqasm.py
File metadata and controls
76 lines (62 loc) · 1.75 KB
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
from CQASMParser import parseCQASMString, Qubit, parseCQASMFile
from networkx.drawing.nx_pydot import write_dot
import networkx as nx
import os
from os import listdir
from os.path import isfile, join
testcircuit = """
version 1.0
# this file has been automatically generated by the OpenQL compiler please do not modify it manually.
qubits 100
._adder_n4_0
x q[0]
x q[1]
h q[3]
cnot q[2],q[3]
t q[0]
t q[1]
t q[2]
tdag q[3]
cnot q[0],q[1]
cnot q[2],q[3]
cnot q[3],q[0]
cnot q[1],q[2]
cnot q[0],q[1]
cnot q[2],q[3]
tdag q[0]
tdag q[1]
tdag q[2]
t q[3]
cnot q[0],q[1]
cnot q[2],q[3]
s q[3]
cnot q[3],q[0]
h q[3]
"""
def getInterationGraph(s):
#t = parseCQASMString(s)
t = parseCQASMFile(s)
g = nx.Graph()
for subcircuit in t.subcircuits:
for instr in subcircuit.instructions:
qops = [op.index for op in instr.operands if isinstance(op, Qubit)]
assert len(qops) <= 2
if len(qops) == 2:
if g.has_edge(*qops):
g.edges[qops[0], qops[1]]["label"] += 1
else:
g.add_edge(*qops, label=1)
return g
if __name__ == "__main__":
curdir = os.path.dirname(__file__)
benchmarks_dir = 'INSERT PATH'
benchmarks = [f for f in listdir(benchmarks_dir) if join(benchmarks_dir, f)]
for b in benchmarks:
print(b)
g = getInterationGraph(benchmarks_dir + b)
b = b.replace('.qasm','')
write_dot(g, b + ".dot")
lines = open(b + ".dot","r").readlines()
second_to_last_line = (lines[-2].replace(';','')) #library bug workround
lines[-2] = second_to_last_line
open(b + '.dot', 'w').writelines(lines)