forked from rigetti/pyquil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
204 lines (169 loc) · 6.38 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
import os
from typing import Dict, Any
import numpy as np
import pytest
from qcs_sdk import QCSClient
from qcs_sdk.qpu.isa import InstructionSetArchitecture
from qcs_sdk.qvm import QVMClient
from pyquil.api import (
QVMCompiler,
BenchmarkConnection,
)
from pyquil.external.rpcq import CompilerISA
from pyquil.gates import I
from pyquil.paulis import sX
from pyquil.quantum_processor import QCSQuantumProcessor, CompilerQuantumProcessor
from pyquil.quantum_processor.transformers.graph_to_compiler_isa import (
DEFAULT_1Q_GATES,
DEFAULT_2Q_GATES,
_transform_edge_operation_to_gates,
_transform_qubit_operation_to_gates,
)
from pyquil.quil import Program
from test.unit.utils import DummyCompiler, CLIENT_PUBLIC_KEY, CLIENT_SECRET_KEY, SERVER_PUBLIC_KEY
from .. import override_qcs_config
TEST_DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "data")
override_qcs_config()
@pytest.fixture
def compiler_isa() -> CompilerISA:
"""
Configures an arbitrary ``CompilerISA`` that may be used to initialize
a ``CompilerQuantumProcessor``. Developers should create specific test cases of
``CompilerISA`` as separate fixtures in conftest.py or in the test file.
"""
gates_1q = []
for gate in DEFAULT_1Q_GATES:
gates_1q.extend(_transform_qubit_operation_to_gates(gate))
gates_2q = []
for gate in DEFAULT_2Q_GATES:
gates_2q.extend(_transform_edge_operation_to_gates(gate))
return CompilerISA.parse_obj(
{
"1Q": {
"0": {"id": 0, "gates": gates_1q},
"1": {"id": 1, "gates": gates_1q},
"2": {"id": 2, "gates": gates_1q},
"3": {"id": 3, "dead": True},
},
"2Q": {
"0-1": {"ids": [0, 1], "gates": gates_2q},
"1-2": {
"ids": [1, 2],
"gates": [
{
"operator_type": "gate",
"operator": "ISWAP",
"parameters": [],
"arguments": ["_", "_"],
}
],
},
"0-2": {
"ids": [0, 2],
"gates": [
{
"operator_type": "gate",
"operator": "CPHASE",
"parameters": ["theta"],
"arguments": ["_", "_"],
},
],
},
"0-3": {"ids": [0, 3], "dead": True},
},
}
)
@pytest.fixture()
def compiler_quantum_processor(compiler_isa: CompilerISA) -> CompilerQuantumProcessor:
return CompilerQuantumProcessor(isa=compiler_isa)
@pytest.fixture
def aspen8_compiler_isa() -> CompilerISA:
"""
Read the Aspen-8 QCS ``CompilerISA`` from file. This should be an exact conversion of
qcs_aspen8_isa to a ``CompilerISA``.
"""
return CompilerISA.parse_file(os.path.join(TEST_DATA_DIR, "compiler-isa-Aspen-8.json"))
@pytest.fixture
def qcs_aspen8_isa() -> InstructionSetArchitecture:
"""
Read the Aspen-8 QCS InstructionSetArchitecture from file and load it into
the ``InstructionSetArchitecture`` QCS API client model.
"""
with open(os.path.join(TEST_DATA_DIR, "qcs-isa-Aspen-8.json")) as f:
return InstructionSetArchitecture.from_raw(f.read())
@pytest.fixture
def qcs_aspen8_quantum_processor(qcs_aspen8_isa: InstructionSetArchitecture) -> QCSQuantumProcessor:
return QCSQuantumProcessor(quantum_processor_id="Aspen-8", isa=qcs_aspen8_isa)
@pytest.fixture
def noise_model_dict() -> Dict[str, Any]:
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 compiler(compiler_quantum_processor: CompilerQuantumProcessor, client_configuration: QCSClient):
compiler = QVMCompiler(
quantum_processor=compiler_quantum_processor, timeout=1, client_configuration=client_configuration
)
program = Program(I(0))
compiler.quil_to_native_quil(program)
return compiler
@pytest.fixture()
def dummy_compiler(qcs_aspen8_quantum_processor: QCSQuantumProcessor, client_configuration: QCSClient):
return DummyCompiler(qcs_aspen8_quantum_processor, client_configuration)
@pytest.fixture(scope="session")
def client_configuration() -> QCSClient:
return QCSClient.load()
@pytest.fixture(scope="session")
def qvm_client(client_configuration: QCSClient) -> QVMClient:
return QVMClient.new_http(client_configuration.qvm_url)
@pytest.fixture(scope="session")
def benchmarker(client_configuration: QCSClient):
bm = BenchmarkConnection(timeout=2, client_configuration=client_configuration)
bm.apply_clifford_to_pauli(Program(I(0)), sX(0))
return bm
def _str_to_bool(s: str):
"""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")