Skip to content

Commit 796a68f

Browse files
committed
add tests
1 parent 554e17f commit 796a68f

11 files changed

+512
-12
lines changed

c3/c3objs.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ def __init__(
8888
if min_val is None and max_val is None:
8989
if value.any():
9090
minmax = [0.9 * value, 1.1 * value]
91-
min_val = np.min(minmax, axis=0)
92-
max_val = np.max(minmax, axis=0)
91+
min_val = np.min(minmax)
92+
max_val = np.max(minmax)
9393
else:
9494
# When would this case be reached?
9595
min_val = -1
@@ -138,7 +138,7 @@ def __sub__(self, other):
138138

139139
def __rsub__(self, other):
140140
out_val = copy.deepcopy(self)
141-
out_val.set_value(self.get_value() - other, extend_bounds=True)
141+
out_val.set_value(other - self.get_value(), extend_bounds=True)
142142
return out_val
143143

144144
def __mul__(self, other):
@@ -267,8 +267,9 @@ def set_value(self, val, extend_bounds=False) -> None:
267267
if extend_bounds and tf.math.abs(tmp) > 1:
268268
min_val, max_val = self.get_limits()
269269
# Extra bounds included to not be directly at border due to differentiability
270-
min_val = tf.math.reduce_min([val * 0.9, min_val])
271-
max_val = tf.math.reduce_max([val * 1.1, max_val])
270+
minmax = [val * 0.9, val * 1.1, min_val, max_val]
271+
min_val = tf.math.reduce_min(minmax)
272+
max_val = tf.math.reduce_max(minmax)
272273
self.set_limits(min_val, max_val)
273274
tmp = 2 * (val * self.pref - self.offset) / self.scale - 1
274275

c3/libraries/envelopes.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def pwc_symmetric(t, params):
127127

128128
@env_reg_deco
129129
def delta_pulse(t, params):
130-
"Pulse shape which gives an output only at a given time bin"
130+
"""Pulse shape which gives an output only at a given time bin"""
131131
t_sig = tf.cast(params["t_sig"].get_value(), tf.float64)
132132
shape = tf.zeros_like(t)
133133
for t_s in t_sig:
@@ -342,7 +342,7 @@ def slepian_fourier(t, params):
342342
x = tf.identity(t)
343343
x = tf.where(t > (t_final + plateau) / 2, t - plateau / 2, x)
344344
x = tf.where(t < (t_final - plateau) / 2, t + plateau / 2, x)
345-
x = tf.where(np.abs(t - t_final / 2) < plateau / 2, (t_final) / 2, x)
345+
x = tf.where(np.abs(t - t_final / 2) < plateau / 2, t_final / 2, x)
346346
length = params["risefall"].get_value() * 2
347347
else:
348348
x = tf.identity(t)
@@ -366,7 +366,7 @@ def slepian_fourier(t, params):
366366
@env_reg_deco
367367
def flattop_risefall_1ns(t, params):
368368
"""Flattop gaussian with fixed width of 1ns."""
369-
params["risefall"] = 1e-9
369+
params["risefall"] = Qty(1e-9, unit="s")
370370
return flattop_risefall(t, params)
371371

372372

@@ -511,7 +511,7 @@ def gaussian_der(t, params):
511511
/ sigma ** 2
512512
)
513513
norm = tf.sqrt(2 * np.pi * sigma ** 2) * tf.math.erf(
514-
t_final / (tf.sqrt(8) * sigma)
514+
t_final / (tf.cast(tf.sqrt(8.0), tf.float64) * sigma)
515515
) - t_final * tf.exp(-(t_final ** 2) / (8 * sigma ** 2))
516516
return gauss_der / norm
517517

c3/system/chip.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Component class and subclasses for the components making up the quantum device."""
2+
import warnings
23

34
import numpy as np
45
import tensorflow as tf
@@ -234,7 +235,7 @@ def get_Lindbladian(self, dims):
234235
gamma = (0.5 / self.params["t2star"].get_value()) ** 0.5
235236
L = gamma * self.collapse_ops["t2star"]
236237
Ls.append(L)
237-
if Ls == []:
238+
if not Ls:
238239
raise Exception("No T1 or T2 provided")
239240
return tf.cast(sum(Ls), tf.complex128)
240241

@@ -344,11 +345,10 @@ def __init__(
344345
if temp:
345346
self.params["temp"] = temp
346347
if "d" not in self.params.keys() and "gamma" not in self.params.keys():
347-
print(
348+
warnings.warn(
348349
"C3:WANING: No junction asymmetry specified, setting symmetric SQUID"
349350
" for tuning."
350351
)
351-
self.params["d"] = 0
352352

353353
def get_factor(self, phi_sig=0):
354354
pi = tf.constant(np.pi, tf.float64)

c3/test_transmon_expanded.py

Whitespace-only changes.

test/envelopes.pickle

102 KB
Binary file not shown.

test/instruction.pickle

296 KB
Binary file not shown.

test/test_envelopes.py

+266
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
import pickle
2+
3+
from c3.c3objs import Quantity
4+
from c3.libraries.envelopes import envelopes
5+
import numpy as np
6+
import pytest
7+
8+
ts = np.linspace(0, 10e-9, 100)
9+
10+
with open("test/envelopes.pickle", "rb") as filename:
11+
test_data = pickle.load(filename)
12+
13+
14+
@pytest.mark.unit
15+
def test_pwc_shape():
16+
params = {
17+
"t_bin_start": Quantity(1e-10),
18+
"t_bin_end": Quantity(9.9e-9),
19+
"t_final": Quantity(10e-9),
20+
"inphase": Quantity([0, 0.1, 0.3, 0.5, 0.1, 1.1, 0.4, 0.1]),
21+
}
22+
np.testing.assert_allclose(
23+
actual=envelopes["pwc_shape"](t=ts, params=params),
24+
desired=test_data["pwc_shape"],
25+
)
26+
27+
np.testing.assert_allclose(
28+
actual=envelopes["pwc_symmetric"](t=ts, params=params),
29+
desired=test_data["pwc_symmetric"],
30+
)
31+
32+
np.testing.assert_allclose(
33+
actual=envelopes["pwc_shape_plateau"](t=ts, params=params),
34+
desired=test_data["pwc_shape_plateau1"],
35+
)
36+
37+
params["width"] = Quantity(5e-9)
38+
np.testing.assert_allclose(
39+
actual=envelopes["pwc_shape_plateau"](t=ts, params=params),
40+
desired=test_data["pwc_shape_plateau2"],
41+
)
42+
43+
44+
@pytest.mark.unit
45+
def test_delta_pulse():
46+
params = {
47+
"t_sig": Quantity(
48+
[
49+
0.5e-9,
50+
]
51+
),
52+
"t_final": Quantity(10e-9),
53+
}
54+
np.testing.assert_allclose(
55+
actual=envelopes["delta_pulse"](t=ts, params=params),
56+
desired=test_data["delta_pulse"],
57+
)
58+
59+
60+
@pytest.mark.unit
61+
def test_fourier():
62+
params = {
63+
"amps": Quantity([0.5, 0.2]),
64+
"freqs": Quantity([1e6, 1e10]),
65+
"phases": Quantity([0, 1]),
66+
"t_final": Quantity(10e-9),
67+
}
68+
69+
np.testing.assert_allclose(
70+
actual=envelopes["fourier_sin"](t=ts, params=params),
71+
desired=test_data["fourier_sin"],
72+
)
73+
74+
np.testing.assert_allclose(
75+
actual=envelopes["fourier_cos"](t=ts, params=params),
76+
desired=test_data["fourier_cos"],
77+
)
78+
79+
params = {
80+
"width": Quantity(9e-9),
81+
"fourier_coeffs": Quantity([1, 0.5, 0.2]),
82+
"offset": Quantity(0.1),
83+
"amp": Quantity(0.5),
84+
"t_final": Quantity(10e-9),
85+
}
86+
87+
np.testing.assert_allclose(
88+
actual=envelopes["slepian_fourier"](t=ts, params=params),
89+
desired=test_data["slepian_fourier"],
90+
)
91+
92+
params["risefall"] = Quantity(4e-9)
93+
np.testing.assert_allclose(
94+
actual=envelopes["slepian_fourier"](t=ts, params=params),
95+
desired=test_data["slepian_fourier_risefall"],
96+
)
97+
98+
params["sin_coeffs"] = Quantity([0.3])
99+
np.testing.assert_allclose(
100+
actual=envelopes["slepian_fourier"](t=ts, params=params),
101+
desired=test_data["slepian_fourier_sin"],
102+
)
103+
104+
105+
@pytest.mark.unit
106+
def test_flattop():
107+
params = {
108+
"risefall": Quantity(2e-9),
109+
"t_final": Quantity(10e-9),
110+
}
111+
112+
np.testing.assert_allclose(
113+
actual=envelopes["trapezoid"](t=ts, params=params),
114+
desired=test_data["trapezoid"],
115+
)
116+
117+
np.testing.assert_allclose(
118+
actual=envelopes["flattop_risefall"](t=ts, params=params),
119+
desired=test_data["flattop_risefall"],
120+
)
121+
122+
np.testing.assert_allclose(
123+
actual=envelopes["flattop_risefall_1ns"](t=ts, params=params),
124+
desired=test_data["flattop_risefall_1ns"],
125+
)
126+
127+
params = {
128+
"ramp": Quantity(2e-9),
129+
"t_up": Quantity(1e-9),
130+
"t_down": Quantity(10e-9),
131+
"t_final": Quantity(10e-9),
132+
}
133+
134+
np.testing.assert_allclose(
135+
actual=envelopes["flattop_variant"](t=ts, params=params),
136+
desired=test_data["flattop_variant"],
137+
)
138+
139+
params = {
140+
"risefall": Quantity(2e-9),
141+
"t_up": Quantity(1e-9),
142+
"t_down": Quantity(10e-9),
143+
"t_final": Quantity(10e-9),
144+
}
145+
146+
np.testing.assert_allclose(
147+
actual=envelopes["flattop"](t=ts, params=params), desired=test_data["flattop"]
148+
)
149+
150+
151+
@pytest.mark.unit
152+
def test_flattop_cut():
153+
params = {
154+
"risefall": Quantity(2e-9),
155+
"t_up": Quantity(1e-9),
156+
"t_down": Quantity(10e-9),
157+
"t_final": Quantity(10e-9),
158+
}
159+
160+
np.testing.assert_allclose(
161+
actual=envelopes["flattop_cut"](t=ts, params=params),
162+
desired=test_data["flattop_cut"],
163+
)
164+
165+
params = {
166+
"risefall": Quantity(2e-9),
167+
"width": Quantity(9e-9),
168+
"t_final": Quantity(10e-9),
169+
}
170+
171+
np.testing.assert_allclose(
172+
actual=envelopes["flattop_cut_center"](t=ts, params=params),
173+
desired=test_data["flattop_cut_center"],
174+
)
175+
176+
177+
@pytest.mark.unit
178+
def test_gaussian():
179+
params = {
180+
"t_final": Quantity(10e-9),
181+
"sigma": Quantity(5e-9),
182+
}
183+
184+
np.testing.assert_allclose(
185+
actual=envelopes["gaussian_sigma"](t=ts, params=params),
186+
desired=test_data["gaussian_sigma"],
187+
)
188+
189+
np.testing.assert_allclose(
190+
actual=envelopes["gaussian"](t=ts, params=params), desired=test_data["gaussian"]
191+
)
192+
193+
np.testing.assert_allclose(
194+
actual=envelopes["gaussian_nonorm"](t=ts, params=params),
195+
desired=test_data["gaussian_nonorm"],
196+
)
197+
198+
np.testing.assert_allclose(
199+
actual=envelopes["gaussian_der_nonorm"](t=ts, params=params),
200+
desired=test_data["gaussian_der_nonorm"],
201+
)
202+
203+
np.testing.assert_allclose(
204+
actual=envelopes["gaussian_der"](t=ts, params=params),
205+
desired=test_data["gaussian_der"],
206+
)
207+
208+
np.testing.assert_allclose(
209+
actual=envelopes["drag_sigma"](t=ts, params=params),
210+
desired=test_data["drag_sigma"],
211+
)
212+
213+
np.testing.assert_allclose(
214+
actual=envelopes["drag_der"](t=ts, params=params), desired=test_data["drag_der"]
215+
)
216+
217+
np.testing.assert_allclose(
218+
actual=envelopes["drag"](t=ts, params=params), desired=test_data["drag"]
219+
)
220+
221+
222+
@pytest.mark.unit
223+
def test_cosine():
224+
params = {
225+
"t_final": Quantity(10e-9),
226+
}
227+
228+
np.testing.assert_allclose(
229+
actual=envelopes["cosine"](t=ts, params=params), desired=test_data["cosine"]
230+
)
231+
232+
params = {
233+
"t_final": Quantity(10e-9),
234+
"t_rise": Quantity(2e-9),
235+
}
236+
237+
np.testing.assert_allclose(
238+
actual=envelopes["cosine_flattop"](t=ts, params=params),
239+
desired=test_data["cosine_flattop"],
240+
)
241+
242+
243+
@pytest.mark.unit
244+
def test_nodrive():
245+
params = {}
246+
np.testing.assert_allclose(
247+
actual=envelopes["no_drive"](t=ts, params=params), desired=test_data["no_drive"]
248+
)
249+
250+
np.testing.assert_allclose(
251+
actual=envelopes["rect"](t=ts, params=params), desired=test_data["rect"]
252+
)
253+
254+
255+
# test_pwc_envelope()
256+
test_pwc_shape()
257+
test_delta_pulse()
258+
test_fourier()
259+
test_flattop()
260+
test_flattop_cut()
261+
test_cosine()
262+
test_gaussian()
263+
test_nodrive()
264+
265+
with open("test/envelopes.pickle", "wb") as filename:
266+
pickle.dump(test_data, filename)

0 commit comments

Comments
 (0)