forked from tencent-quantum-lab/tensorcircuit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
circuit_compiler.py
77 lines (61 loc) · 1.88 KB
/
circuit_compiler.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
"""
compilation utilities in tensorcircuit
"""
import tensorcircuit as tc
c = tc.Circuit(3)
c.rx(0, theta=0.2)
c.rz(0, theta=-0.3)
c.ry(1, theta=0.1)
c.h(2)
c.cx(0, 1)
c.cz(2, 1)
c.x(0)
c.y(0)
c.rxx(1, 2, theta=1.7)
c0, _ = tc.compiler.qiskit_compiler.qiskit_compile(
c,
compiled_options={"optimization_level": 0, "basis_gates": ["cx", "cz", "h", "rz"]},
)
c1, _ = tc.compiler.qiskit_compiler.qiskit_compile(
c,
compiled_options={"optimization_level": 1, "basis_gates": ["cx", "cz", "h", "rz"]},
)
c2, _ = tc.compiler.qiskit_compiler.qiskit_compile(
c,
compiled_options={"optimization_level": 2, "basis_gates": ["cx", "cz", "h", "rz"]},
)
c3, _ = tc.compiler.qiskit_compiler.qiskit_compile(
c,
compiled_options={"optimization_level": 3, "basis_gates": ["cx", "cz", "h", "rz"]},
)
print(
"qiskit can become worse with higher level optimization when the target gate is not U3 but rz"
)
print("level 0:\n")
print(c0.draw())
print("level 1:\n")
print(c1.draw())
print("level 2:\n")
print(c2.draw())
print("level 3:\n")
print(c3.draw())
compiler_wo_mapping = tc.compiler.DefaultCompiler()
c4, _ = compiler_wo_mapping(c)
print(
"compiled with tc default compiler: combining the good from qiskit and our tc own"
)
# we always uuggest using DefaultCompiler for tasks on qcloud
# internally we run optimized compiling using U3 basis with qiskit which has good performance
# and we unroll u3 with rz and apply replace/prune/merge loop developed in tc to further optimize the circuit
print(c4.draw())
print("gate number comparison (last ours vs before qiskit (0, 1, 2, 3))")
for c in [c0, c1, c2, c3, c4]:
print(c.gate_count())
# if we want to apply routing/qubit mapping
compiler_w_mapping = tc.compiler.DefaultCompiler(
{"coupling_map": [[0, 2], [2, 0], [1, 0], [0, 1]]}
)
c5, info = compiler_w_mapping(c)
print("circuit with qubit mapping")
print(c5.draw())
print(info)