-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_compiler.py
147 lines (126 loc) · 3.34 KB
/
test_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
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
import sys
import os
import pytest
import numpy as np
# from pytest_lazyfixture import lazy_fixture as lf
thisfile = os.path.abspath(__file__)
modulepath = os.path.dirname(os.path.dirname(thisfile))
sys.path.insert(0, modulepath)
import tensorcircuit as tc
def test_qsikit_compiler():
try:
import qiskit as _
except ImportError:
pytest.skip("qiskit is not installed")
from tensorcircuit.compiler.qiskit_compiler import qiskit_compile
c = tc.Circuit(2)
c.x(1)
c.cx(0, 1)
c1, info = qiskit_compile(
c,
info=None,
output="qasm",
compiled_options={
"basis_gates": ["cz", "rz", "h"],
"optimization_level": 3,
"coupling_map": [[0, 2], [2, 0], [1, 0], [0, 1]],
},
)
assert "cz" in c1
print(info["logical_physical_mapping"])
c = tc.Circuit(2)
c.x(1)
c.cx(0, 1)
c.measure_instruction(1)
c1, info = qiskit_compile(
c,
info=None,
output="tc",
compiled_options={
"basis_gates": ["cx", "rz", "h"],
"optimization_level": 3,
"coupling_map": [[0, 2], [2, 0], [1, 0], [0, 1]],
"initial_layout": [1, 2],
},
)
for inst in c1.to_qir():
if inst["name"] == "h":
assert inst["index"][0] == 2
print(c1.draw())
assert info["logical_physical_mapping"][1] in [0, 2]
print(info)
c2, info2 = qiskit_compile(
c1,
info=info,
output="tc",
compiled_options={
"basis_gates": ["cx", "rz", "h"],
"optimization_level": 3,
"coupling_map": [[0, 2], [2, 0], [1, 0], [0, 1]],
},
)
assert info2["positional_logical_mapping"] == {0: 1}
print(c2.draw())
def test_composed_compiler():
from tensorcircuit.compiler import DefaultCompiler
c = tc.Circuit(3)
c.rx(0)
c.cx(0, 1)
c.cz(1, 0)
c.rxx(0, 2, theta=0.2)
c.measure_instruction(2)
c.measure_instruction(0)
default_compiler = DefaultCompiler()
c1, info = default_compiler(c)
print(c1.draw())
assert c1.gate_count_by_condition(lambda qir: qir["name"] == "cnot") == 3
assert info["positional_logical_mapping"][0] == 2
default_compiler = DefaultCompiler(
{
"basis_gates": ["h", "rz", "cz"],
"optimization_level": 2,
"coupling_map": [[0, 1], [1, 2]],
}
)
c1, info = default_compiler(c)
assert c1.gate_count_by_condition(lambda qir: qir["name"] == "cnot") == 0
print(info)
def test_replace_r():
c = tc.Circuit(3)
c.rz(0, theta=0.1)
c.cx(0, 2)
c.ry(1)
c.rxx(1, 0, theta=0.2)
c.rx(0, theta=3.9)
c.ry(1, theta=-0.2)
c.rzz(1, 0, theta=-0.3)
c.ryy(1, 0, theta=-0.6)
c.rx(2)
print(c.draw())
c1 = tc.compiler.simple_compiler.replace_r(c)
print(c1.draw())
np.testing.assert_allclose(c.matrix(), c1.matrix(), atol=1e-5)
def test_default_compiler():
c = tc.Circuit(3)
c.cx(0, 1)
c.rx(0, theta=1e-5)
c.x(1)
c.y(1)
c.z(1)
c.h(1)
c.cz(2, 0)
c.h(1)
c.cz(2, 0)
c.s(2)
c.sd(2)
c.s(2)
c.s(2)
c.y(2)
c.ry(2, theta=0.1)
c.t(2)
c.td(2)
c.ry(2, theta=-0.1)
c.rz(1, theta=0.3)
c1, _ = tc.compiler.simple_compiler.simple_compile(c)
print(c1.draw())
assert c1.gate_count() == 3