-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathtest_quantity.py
195 lines (144 loc) · 4.31 KB
/
test_quantity.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
"""Unit tests for Quantity class"""
import hjson
import numpy as np
import pytest
from c3.c3objs import Quantity, hjson_encode
amp = Quantity(value=0.0, min_val=-1.0, max_val=+1.0, unit="V")
amp_dict = {
"value": 0.0,
"min_val": -1.0,
"max_val": 1.0,
"unit": "V",
"symbol": "\\alpha",
}
freq = Quantity(
value=5.6e9, min_val=5.595e9, max_val=5.605e9, unit="Hz 2pi", symbol="\\omega"
)
freq_dict = {
"value": 5.6e9,
"min_val": 5.595e9,
"max_val": 5.605e9,
"unit": "Hz 2pi",
"symbol": "\\omega",
}
gate_time = Quantity(
value=5.3246e-9, min_val=2e-9, max_val=10e-9, unit="s", symbol=r"t_g"
)
matrix = Quantity(
value=[[0, 1], [1, 0]],
min_val=[[0, 0], [0, 0]],
max_val=[[1, 1], [1, 1]],
unit="",
symbol=r"M",
)
@pytest.mark.unit
def test_qty_2pi() -> None:
assert freq.asdict() == freq_dict
@pytest.mark.unit
def test_qty_set_2pi() -> None:
freq.set_value(5.602e9)
assert freq.get_value() - 5.602e9 * 2 * np.pi < 1e-8
@pytest.mark.unit
def test_qty_asdict() -> None:
assert amp.asdict() == amp_dict
@pytest.mark.unit
def test_qty_write_cfg() -> None:
print(hjson.dumps(amp.asdict(), default=hjson_encode))
@pytest.mark.unit
def test_qty_read_cfg() -> None:
assert Quantity(**amp_dict).asdict() == amp.asdict()
@pytest.mark.unit
def test_qty_str() -> None:
assert str(gate_time) == "5.325 ns "
@pytest.mark.unit
def test_qty_item() -> None:
assert gate_time[0] == gate_time.numpy()
@pytest.mark.unit
def test_qty_set() -> None:
gate_time.set_value(7e-9)
assert gate_time.get_value() - 7e-9 < 1e-15
@pytest.mark.unit
def test_qty_max() -> None:
gate_time.set_opt_value(1.0)
assert gate_time.get_value() - 10e-9 < 1e-15
@pytest.mark.unit
def test_qty_min() -> None:
gate_time.set_opt_value(-1.0)
assert gate_time.get_value() - 2e-9 < 1e-15
@pytest.mark.unit
def test_qty_get_opt() -> None:
gate_time.set_value(6e-9)
assert gate_time.get_opt_value() < 1e-15
@pytest.mark.unit
def test_qty_matrix_str() -> None:
assert str(matrix) == "0.000 1.000 1.000 0.000 "
@pytest.mark.unit
def test_qty_matrix_list() -> None:
assert matrix.tolist() == [[0, 1], [1, 0]]
@pytest.mark.unit
def test_qty_matrix_item() -> None:
assert matrix[0][1] == 1
@pytest.mark.unit
def test_qty_matrix_set() -> None:
matrix.set_value([[1.0, 0.0], [0.0, 1.0]])
assert (matrix.numpy() == [[1, 0], [0, 1]]).all()
@pytest.mark.unit
def test_qty_matrix_set_opt() -> None:
assert (matrix.get_opt_value().numpy() == np.array([1.0, -1.0, -1.0, 1.0])).all()
@pytest.mark.unit
def test_qty_np_conversions() -> None:
a = Quantity(value=3, unit="unit")
assert repr(a) == "3.000 unit"
assert np.mod(a, 2) == 1.0
assert type(a.numpy()) is np.float64 or type(a.numpy()) is np.ndarray
assert a + a == 6
np.array([a]) # test conversion
np.array(a)
float(a)
assert np.mod([a], 2) == np.array([[1.0]])
assert a.tolist() == [3.0]
b = Quantity(np.array([0.0000001, 0.00001]))
np.array([b])
c = Quantity([0, 0.1], min_val=0, max_val=1)
assert len(c) == 2
assert c.shape == (2,)
@pytest.mark.unit
def test_qty_math() -> None:
a = 0.5
b = Quantity(2)
assert a + b == 2.5
assert b + a == 2.5
assert a - b == -1.5
assert b - a == 1.5
assert a * b == 1.0
assert b * a == 1.0
np.testing.assert_allclose(a**b, 0.25)
assert b**a == 2**0.5
np.testing.assert_allclose(a / b, 0.25)
assert b / a == 4.0
assert b % a == 0
qty = Quantity(3, min_val=0, max_val=5)
qty.subtract(1.3)
np.testing.assert_allclose(qty, 1.7)
qty = Quantity(3, min_val=0, max_val=5)
qty.add(0.3)
np.testing.assert_allclose(qty, 3.3)
@pytest.mark.unit
def get_and_set() -> None:
np.testing.assert_allclose(
Quantity(0.3, min_val=0, max_val=1).get_opt_value(), [-0.4]
)
for val in np.linspace(0, 2):
a = Quantity(val, min_val=-1, max_val=2)
opt_val = a.get_opt_value()
a.set_opt_value(opt_val)
np.testing.assert_allclose(a, val)
@pytest.mark.unit
def test_qty_matrix_float() -> None:
with pytest.raises(NotImplementedError):
float(matrix)
@pytest.mark.unit
def test_pihalf() -> None:
"Don't use prefixes with pi."
pihalf = Quantity(1500, unit="pi")
assert str(pihalf) == "1500.0 pi "