-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteraction_graph.py
60 lines (48 loc) · 1.2 KB
/
interaction_graph.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
from parsing.CQASMParser import parseCQASMString, Qubit
from networkx.drawing.nx_pydot import write_dot
import networkx as nx
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)
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__":
g = getInterationGraph(testcircuit)
write_dot(g, "result.dot")