forked from rigetti/pyquil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
224 lines (191 loc) · 6.49 KB
/
conftest.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import shutil
import numpy as np
import pytest
from requests import RequestException
from pyquil.api import (
QVMConnection,
QVMCompiler,
ForestConnection,
get_benchmarker,
local_forest_runtime,
)
from pyquil.api._config import PyquilConfig
from pyquil.api._errors import UnknownApiError
from pyquil.api._compiler import QuilcNotRunning, QuilcVersionMismatch
from pyquil.api._qvm import QVMNotRunning, QVMVersionMismatch
from pyquil.device import Device
from pyquil.gates import I
from pyquil.paulis import sX
from pyquil.quil import Program
@pytest.fixture
def isa_dict():
return {
"1Q": {"0": {"type": "Xhalves"}, "1": {}, "2": {}, "3": {"dead": True}},
"2Q": {
"0-1": {},
"1-2": {"type": "ISWAP"},
"0-2": {"type": "CPHASE"},
"0-3": {"dead": True},
},
}
@pytest.fixture
def specs_dict():
return {
"1Q": {
"0": {
"f1QRB": 0.99,
"f1QRB_std_err": 0.01,
"f1Q_simultaneous_RB": 0.98,
"f1Q_simultaneous_RB_std_err": 0.02,
"fRO": 0.93,
"T1": 20e-6,
"T2": 15e-6,
},
"1": {
"f1QRB": 0.989,
"f1QRB_std_err": 0.011,
"f1Q_simultaneous_RB": 0.979,
"f1Q_simultaneous_RB_std_err": 0.021,
"fRO": 0.92,
"T1": 19e-6,
"T2": 12e-6,
},
"2": {
"f1QRB": 0.983,
"f1QRB_std_err": 0.017,
"f1Q_simultaneous_RB": 0.973,
"f1Q_simultaneous_RB_std_err": 0.027,
"fRO": 0.95,
"T1": 21e-6,
"T2": 16e-6,
},
"3": {
"f1QRB": 0.988,
"f1QRB_std_err": 0.012,
"f1Q_simultaneous_RB": 0.978,
"f1Q_simultaneous_RB_std_err": 0.022,
"fRO": 0.94,
"T1": 18e-6,
"T2": 11e-6,
},
},
"2Q": {
"0-1": {"fBellState": 0.90, "fCZ": 0.89, "fCZ_std_err": 0.01, "fCPHASE": 0.88},
"1-2": {"fBellState": 0.91, "fCZ": 0.90, "fCZ_std_err": 0.12, "fCPHASE": 0.89},
"0-2": {"fBellState": 0.92, "fCZ": 0.91, "fCZ_std_err": 0.20, "fCPHASE": 0.90},
"0-3": {"fBellState": 0.89, "fCZ": 0.88, "fCZ_std_err": 0.03, "fCPHASE": 0.87},
},
}
@pytest.fixture
def noise_model_dict():
return {
"gates": [
{
"gate": "I",
"params": (5.0,),
"targets": (0, 1),
"kraus_ops": [[[[1.0]], [[1.0]]]],
"fidelity": 1.0,
},
{
"gate": "RX",
"params": (np.pi / 2.0,),
"targets": (0,),
"kraus_ops": [[[[1.0]], [[1.0]]]],
"fidelity": 1.0,
},
],
"assignment_probs": {"1": [[1.0, 0.0], [0.0, 1.0]], "0": [[1.0, 0.0], [0.0, 1.0]]},
}
@pytest.fixture
def device_raw(isa_dict, noise_model_dict, specs_dict):
return {
"isa": isa_dict,
"noise_model": noise_model_dict,
"specs": specs_dict,
"is_online": True,
"is_retuning": False,
}
@pytest.fixture
def test_device(device_raw):
return Device("test_device", device_raw)
@pytest.fixture(scope="session")
def qvm():
try:
qvm = QVMConnection(random_seed=52)
qvm.run(Program(I(0)), [])
return qvm
except (RequestException, QVMNotRunning, UnknownApiError) as e:
return pytest.skip("This test requires QVM connection: {}".format(e))
except QVMVersionMismatch as e:
return pytest.skip("This test requires a different version of the QVM: {}".format(e))
@pytest.fixture()
def compiler(test_device):
try:
config = PyquilConfig()
compiler = QVMCompiler(endpoint=config.quilc_url, device=test_device, timeout=1)
compiler.quil_to_native_quil(Program(I(0)))
return compiler
except (RequestException, QuilcNotRunning, UnknownApiError, TimeoutError) as e:
return pytest.skip("This test requires compiler connection: {}".format(e))
except QuilcVersionMismatch as e:
return pytest.skip("This test requires a different version of quilc: {}".format(e))
@pytest.fixture(scope="session")
def forest():
try:
connection = ForestConnection()
connection._wavefunction(Program(I(0)), 52)
return connection
except (RequestException, UnknownApiError) as e:
return pytest.skip("This test requires a Forest connection: {}".format(e))
@pytest.fixture(scope="session")
def benchmarker():
try:
bm = get_benchmarker(timeout=2)
bm.apply_clifford_to_pauli(Program(I(0)), sX(0))
return bm
except (RequestException, TimeoutError) as e:
return pytest.skip(
"This test requires a running local benchmarker endpoint (ie quilc): {}".format(e)
)
@pytest.fixture(scope="session")
def local_qvm_quilc():
"""Execute test with local qvm and quilc running"""
if shutil.which("qvm") is None or shutil.which("quilc") is None:
return pytest.skip(
"This test requires 'qvm' and 'quilc' executables to be installed locally."
)
with local_forest_runtime() as context:
yield context
def _str_to_bool(s):
"""Convert either of the strings 'True' or 'False' to their Boolean equivalent"""
if s == "True":
return True
elif s == "False":
return False
else:
raise ValueError("Please specify either True or False")
def pytest_addoption(parser):
parser.addoption(
"--use-seed",
action="store",
type=_str_to_bool,
default=True,
help="run operator estimation tests faster by using a fixed random seed",
)
parser.addoption(
"--runslow", action="store_true", default=False, help="run tests marked as being 'slow'"
)
def pytest_configure(config):
config.addinivalue_line("markers", "slow: mark test as slow to run")
def pytest_collection_modifyitems(config, items):
if config.getoption("--runslow"):
# --runslow given in cli: do not skip slow tests
return
skip_slow = pytest.mark.skip(reason="need --runslow option to run")
for item in items:
if "slow" in item.keywords:
item.add_marker(skip_slow)
@pytest.fixture()
def use_seed(pytestconfig):
return pytestconfig.getoption("use_seed")