-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathconftest.py
196 lines (173 loc) · 7.86 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
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest
import qualtran.testing as qlt_testing
from qualtran import BloqExample
def assert_bloq_example_make_for_pytest(bloq_ex: BloqExample):
"""Wrap `assert_bloq_example_make`.
Anything other than PASS is a test failure.
"""
try:
qlt_testing.assert_bloq_example_make(bloq_ex)
except qlt_testing.BloqCheckException as bce:
# No special skip logic
raise bce from bce
def assert_bloq_example_decompose_for_pytest(bloq_ex: BloqExample):
"""Wrap `assert_bloq_example_decompose`.
`NA` or `MISSING` result in the test being skipped.
"""
try:
qlt_testing.assert_bloq_example_decompose(bloq_ex)
except qlt_testing.BloqCheckException as bce:
if bce.check_result is qlt_testing.BloqCheckResult.NA:
pytest.skip(bce.msg)
if bce.check_result is qlt_testing.BloqCheckResult.MISSING:
pytest.skip(bce.msg)
raise bce from bce
def assert_equivalent_bloq_example_counts_for_pytest(bloq_ex: BloqExample):
try:
qlt_testing.assert_equivalent_bloq_example_counts(bloq_ex)
except qlt_testing.BloqCheckException as bce:
if bce.check_result in [
qlt_testing.BloqCheckResult.UNVERIFIED,
qlt_testing.BloqCheckResult.NA,
qlt_testing.BloqCheckResult.MISSING,
]:
pytest.skip(bce.msg)
if bce.check_result == qlt_testing.BloqCheckResult.FAIL:
pytest.xfail("We are not yet enforcing the 'counts' check.")
raise bce from bce
def assert_bloq_example_serializes_for_pytest(bloq_ex: BloqExample):
if bloq_ex.name in [
'prep_sparse',
'thc_prep',
'modexp',
'apply_z_to_odd',
'select_pauli_lcu',
'sel_hubb',
'walk_op',
'thc_walk_op', # thc_prep does not serialize
'qubitization_qpe_chem_thc', # too slow
'walk_op_chem_sparse',
'qubitization_qpe_sparse_chem', # too slow
'qubitization_qpe_ising',
'trott_unitary',
'symbolic_hamsim_by_gqsp',
'gf16_addition', # cannot serialize QGF
'gf2_addition_symbolic', # cannot serialize QGF
'gf16_add_k', # cannot serialize QGF
'gf2_add_k_symbolic', # cannot serialize QGF
'gf16_multiplication', # cannot serialize QGF
'gf2_multiplication_symbolic', # cannot serialize QGF
'gf16_square', # cannot serialize QGF
'gf2_square_symbolic', # cannot serialize QGF
'gf16_inverse', # cannot serialize QGF
'gf2_inverse_symbolic', # cannot serialize QGF
'gf_poly_split', # cannot serialize QGF and QGFPoly
'gf_poly_join', # cannot serialize QGF and QGFPoly
'gf2_poly_4_8_add_k', # cannot serialize QGF and QGFPoly
'gf2_poly_add_k_symbolic', # cannot serialize QGF and QGFPoly
'gf2_poly_4_8_add', # cannot serialize QGF and QGFPoly
'gf2_poly_add_symbolic', # cannot serialize QGF and QGFPoly
'gqsp_1d_ising',
'auto_partition',
'unitary_block_encoding',
'unitary_block_encoding_properties',
'tensor_product_block_encoding',
'tensor_product_block_encoding_properties',
'tensor_product_block_encoding_symb',
'product_block_encoding',
'product_block_encoding_properties',
'product_block_encoding_symb',
'apply_lth_bloq',
'linear_combination_block_encoding',
'phase_block_encoding',
'state_prep_alias_symb', # cannot serialize Shaped
'sparse_matrix_block_encoding',
'sparse_matrix_symb_block_encoding',
'sparse_matrix_hermitian_block_encoding',
'sparse_matrix_symb_hermitian_block_encoding',
'sparse_state_prep_alias_symb', # cannot serialize Shaped
'sparse_permutation', # contains nested tuple of inhomogeneous shape
'permutation_cycle_symb', # cannot serialize Shaped
'permutation_cycle_symb_N', # sympy variable assumptions dropped by serialized
'permutation_symb', # cannot serialize shaped
'permutation_symb_with_cycles', # Object arrays cannot be saved when allow_pickle=False
'sparse_permutation_with_symbolic_N', # setting an array element with a sequence.
'state_prep_via_rotation_symb', # cannot serialize HasLength
'state_prep_via_rotation_symb_phasegrad', # cannot serialize Shaped
'sparse_state_prep_via_rotations', # cannot serialize Permutation
'sparse_state_prep_via_rotations_with_large_target_bitsize', # setting an array element with a sequence.
'explicit_matrix_block_encoding', # cannot serialize AutoPartition
'symmetric_banded_matrix_block_encoding', # cannot serialize AutoPartition
'chebyshev_poly_even',
'scaled_chebyshev_poly_even',
'scaled_chebyshev_poly_odd',
'black_box_select', # cannot serialize AutoPartition
'black_box_prepare', # cannot serialize AutoPartition
'kaiser_window_state_symbolic', # Split cannot have a symbolic data type.
'ctrl_on_symbolic_cv', # cannot serialize Shaped
'ctrl_on_symbolic_cv_multi', # cannot serialize Shaped
'ctrl_on_symbolic_n_ctrls', # cannot serialize Shaped
'has_duplicates_symb_len', # cannot serialize HasLength
'symm_diff_symb', # round trip fail: sympy assumptions not serialized
'symm_diff_equal_size_symb', # round trip fail: sympy assumptions not serialized
]:
pytest.xfail("Skipping serialization test for bloq examples that cannot yet be serialized.")
if bloq_ex.name in [
'ecc',
'ec_pe',
'ec_pe_small',
'ec_add_r',
'ec_add_r_small',
'ec_window_add',
'ec_add',
]:
pytest.xfail("Skipping serialization test for bloqs that use ECPoint.")
try:
qlt_testing.assert_bloq_example_serializes(bloq_ex)
except qlt_testing.BloqCheckException as bce:
raise bce from bce
def assert_bloq_example_qtyping_for_pytest(bloq_ex: BloqExample):
try:
qlt_testing.assert_bloq_example_qtyping(bloq_ex)
except qlt_testing.BloqCheckException as bce:
if bce.check_result is qlt_testing.BloqCheckResult.NA:
pytest.skip(bce.msg)
if bce.check_result is qlt_testing.BloqCheckResult.UNVERIFIED:
pytest.skip(bce.msg)
_TESTFUNCS = [
('make', assert_bloq_example_make_for_pytest),
('decompose', assert_bloq_example_decompose_for_pytest),
('counts', assert_equivalent_bloq_example_counts_for_pytest),
('serialize', assert_bloq_example_serializes_for_pytest),
('qtyping', assert_bloq_example_qtyping_for_pytest),
]
@pytest.fixture(scope="module", params=_TESTFUNCS, ids=[name for name, func in _TESTFUNCS])
def bloq_autotester(request):
name, func = request.param
func.check_name = name
return func