Skip to content

Commit fd8b154

Browse files
authored
fix: Fix type issue with NumPy(#219)
1 parent 7680dda commit fd8b154

File tree

8 files changed

+202
-182
lines changed

8 files changed

+202
-182
lines changed

qctrlopencontrols/constants.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818

1919
import numpy as np
2020

21-
SIGMA_X = np.array([[0.0, 1.0], [1.0, 0.0]], dtype=np.complex)
22-
SIGMA_Y = np.array([[0.0, -1.0j], [1.0j, 0.0]], dtype=np.complex)
23-
SIGMA_Z = np.array([[1.0, 0.0], [0.0, -1.0]], dtype=np.complex)
24-
SIGMA_M = np.array([[0.0, 1.0], [0.0, 0.0]], dtype=np.complex)
25-
SIGMA_P = np.array([[0.0, 0.0], [1.0, 0.0]], dtype=np.complex)
21+
SIGMA_X = np.array([[0.0, 1.0], [1.0, 0.0]], dtype=complex)
22+
SIGMA_Y = np.array([[0.0, -1.0j], [1.0j, 0.0]], dtype=complex)
23+
SIGMA_Z = np.array([[1.0, 0.0], [0.0, -1.0]], dtype=complex)
24+
SIGMA_M = np.array([[0.0, 1.0], [0.0, 0.0]], dtype=complex)
25+
SIGMA_P = np.array([[0.0, 0.0], [1.0, 0.0]], dtype=complex)
2626

2727
# Matplotlib format of data for plotting
2828
MATPLOTLIB = "matplotlib"

qctrlopencontrols/driven_controls/driven_control.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def __init__(
102102

103103
self.name = name
104104

105-
durations = np.asarray(durations, dtype=np.float)
105+
durations = np.asarray(durations, dtype=float)
106106

107107
# check if all the durations are greater than zero
108108
check_arguments(
@@ -139,9 +139,10 @@ def __init__(
139139
if detunings is None:
140140
detunings = np.zeros(duration_count)
141141

142-
rabi_rates = np.asarray(rabi_rates, dtype=np.float64)
143-
azimuthal_angles = np.asarray(azimuthal_angles, dtype=np.float64)
144-
detunings = np.asarray(detunings, dtype=np.float64)
142+
# for backward compatibility as these variable could be list
143+
rabi_rates = np.asarray(rabi_rates, dtype=float)
144+
azimuthal_angles = np.asarray(azimuthal_angles, dtype=float)
145+
detunings = np.asarray(detunings, dtype=float)
145146

146147
# check if all the rabi_rates are greater than zero
147148
check_arguments(
@@ -362,7 +363,7 @@ def resample(self, time_step: float, name: Optional[str] = None) -> "DrivenContr
362363
)
363364

364365
count = int(np.ceil(self.duration / time_step))
365-
durations = [time_step] * count
366+
durations = np.repeat(time_step, count)
366367
times = np.arange(count) * time_step
367368

368369
indices = np.digitize(times, bins=np.cumsum(self.durations))

qctrlopencontrols/driven_controls/predefined.py

Lines changed: 92 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ def new_primitive_control(
150150
)
151151

152152
return DrivenControl(
153-
rabi_rates=[maximum_rabi_rate],
154-
azimuthal_angles=[azimuthal_angle],
155-
detunings=[0],
156-
durations=[rabi_rotation / maximum_rabi_rate],
153+
rabi_rates=np.array([maximum_rabi_rate]),
154+
azimuthal_angles=np.array([azimuthal_angle]),
155+
detunings=np.array([0]),
156+
durations=np.array([rabi_rotation / maximum_rabi_rate]),
157157
name=name,
158158
)
159159

@@ -216,15 +216,19 @@ def new_bb1_control(
216216
phi_p = _get_transformed_rabi_rotation_wimperis(rabi_rotation)
217217
rabi_rotations = [rabi_rotation, np.pi, 2 * np.pi, np.pi]
218218

219-
rabi_rates = [maximum_rabi_rate] * 4
220-
azimuthal_angles = [
221-
azimuthal_angle,
222-
azimuthal_angle + phi_p,
223-
azimuthal_angle + 3 * phi_p,
224-
azimuthal_angle + phi_p,
225-
]
226-
detunings = [0] * 4
227-
durations = [rabi_rotation / maximum_rabi_rate for rabi_rotation in rabi_rotations]
219+
rabi_rates = np.repeat(maximum_rabi_rate, 4)
220+
azimuthal_angles = np.asarray(
221+
[
222+
azimuthal_angle,
223+
azimuthal_angle + phi_p,
224+
azimuthal_angle + 3 * phi_p,
225+
azimuthal_angle + phi_p,
226+
]
227+
)
228+
detunings = np.repeat(0, 4)
229+
durations = np.asarray(
230+
[rabi_rotation / maximum_rabi_rate for rabi_rotation in rabi_rotations]
231+
)
228232

229233
return DrivenControl(
230234
rabi_rates=rabi_rates,
@@ -294,16 +298,18 @@ def new_sk1_control(
294298
phi_p = _get_transformed_rabi_rotation_wimperis(rabi_rotation)
295299
rabi_rotations = [rabi_rotation, 2 * np.pi, 2 * np.pi]
296300

297-
rabi_rates = [maximum_rabi_rate] * 3
298-
azimuthal_angles = [
299-
azimuthal_angle,
300-
azimuthal_angle - phi_p,
301-
azimuthal_angle + phi_p,
302-
]
303-
detunings = [0] * 3
304-
durations = [
305-
rabi_rotation_ / maximum_rabi_rate for rabi_rotation_ in rabi_rotations
306-
]
301+
rabi_rates = np.repeat(maximum_rabi_rate, 3)
302+
azimuthal_angles = np.asarray(
303+
[
304+
azimuthal_angle,
305+
azimuthal_angle - phi_p,
306+
azimuthal_angle + phi_p,
307+
]
308+
)
309+
detunings = np.repeat(0, 3)
310+
durations = np.asarray(
311+
[rabi_rotation_ / maximum_rabi_rate for rabi_rotation_ in rabi_rotations]
312+
)
307313

308314
return DrivenControl(
309315
rabi_rates=rabi_rates,
@@ -414,16 +420,18 @@ def degrees_to_radians(angle_in_degrees):
414420

415421
rabi_rotations = [theta_1, theta_2, theta_3]
416422

417-
rabi_rates = [maximum_rabi_rate] * 3
418-
azimuthal_angles = [
419-
azimuthal_angle + phi_1,
420-
azimuthal_angle + phi_2,
421-
azimuthal_angle + phi_3,
422-
]
423-
detunings = [0] * 3
424-
durations = [
425-
rabi_rotation_ / maximum_rabi_rate for rabi_rotation_ in rabi_rotations
426-
]
423+
rabi_rates = np.repeat(maximum_rabi_rate, 3)
424+
azimuthal_angles = np.asarray(
425+
[
426+
azimuthal_angle + phi_1,
427+
azimuthal_angle + phi_2,
428+
azimuthal_angle + phi_3,
429+
]
430+
)
431+
detunings = np.repeat(0, 3)
432+
durations = np.asarray(
433+
[rabi_rotation_ / maximum_rabi_rate for rabi_rotation_ in rabi_rotations]
434+
)
427435

428436
return DrivenControl(
429437
rabi_rates=rabi_rates,
@@ -500,12 +508,14 @@ def new_corpse_control(
500508
rabi_rotation / 2.0 - k,
501509
]
502510

503-
rabi_rates = [maximum_rabi_rate] * 3
504-
azimuthal_angles = [azimuthal_angle, azimuthal_angle + np.pi, azimuthal_angle]
505-
detunings = [0] * 3
506-
durations = [
507-
rabi_rotation_ / maximum_rabi_rate for rabi_rotation_ in rabi_rotations
508-
]
511+
rabi_rates = np.repeat(maximum_rabi_rate, 3)
512+
azimuthal_angles = np.asarray(
513+
[azimuthal_angle, azimuthal_angle + np.pi, azimuthal_angle]
514+
)
515+
detunings = np.repeat(0, 3)
516+
durations = np.asarray(
517+
[rabi_rotation_ / maximum_rabi_rate for rabi_rotation_ in rabi_rotations]
518+
)
509519

510520
return DrivenControl(
511521
rabi_rates=rabi_rates,
@@ -598,19 +608,21 @@ def new_corpse_in_bb1_control(
598608
np.pi,
599609
]
600610

601-
rabi_rates = [maximum_rabi_rate] * 6
602-
azimuthal_angles = [
603-
azimuthal_angle,
604-
azimuthal_angle + np.pi,
605-
azimuthal_angle,
606-
azimuthal_angle + phi_p,
607-
azimuthal_angle + 3 * phi_p,
608-
azimuthal_angle + phi_p,
609-
]
610-
detunings = [0] * 6
611-
durations = [
612-
rabi_rotation_ / maximum_rabi_rate for rabi_rotation_ in rabi_rotations
613-
]
611+
rabi_rates = np.repeat(maximum_rabi_rate, 6)
612+
azimuthal_angles = np.asarray(
613+
[
614+
azimuthal_angle,
615+
azimuthal_angle + np.pi,
616+
azimuthal_angle,
617+
azimuthal_angle + phi_p,
618+
azimuthal_angle + 3 * phi_p,
619+
azimuthal_angle + phi_p,
620+
]
621+
)
622+
detunings = np.repeat(0, 6)
623+
durations = np.asarray(
624+
[rabi_rotation_ / maximum_rabi_rate for rabi_rotation_ in rabi_rotations]
625+
)
614626

615627
return DrivenControl(
616628
rabi_rates=rabi_rates,
@@ -701,18 +713,20 @@ def new_corpse_in_sk1_control(
701713
2 * np.pi,
702714
]
703715

704-
rabi_rates = [maximum_rabi_rate] * 5
705-
azimuthal_angles = [
706-
azimuthal_angle,
707-
azimuthal_angle + np.pi,
708-
azimuthal_angle,
709-
azimuthal_angle - phi_p,
710-
azimuthal_angle + phi_p,
711-
]
712-
detunings = [0] * 5
713-
durations = [
714-
rabi_rotation_ / maximum_rabi_rate for rabi_rotation_ in rabi_rotations
715-
]
716+
rabi_rates = np.repeat(maximum_rabi_rate, 5)
717+
azimuthal_angles = np.asarray(
718+
[
719+
azimuthal_angle,
720+
azimuthal_angle + np.pi,
721+
azimuthal_angle,
722+
azimuthal_angle - phi_p,
723+
azimuthal_angle + phi_p,
724+
]
725+
)
726+
detunings = np.repeat(0, 5)
727+
durations = np.asarray(
728+
[rabi_rotation_ / maximum_rabi_rate for rabi_rotation_ in rabi_rotations]
729+
)
716730

717731
return DrivenControl(
718732
rabi_rates=rabi_rates,
@@ -867,10 +881,12 @@ def degrees_to_radians(angle_in_degrees):
867881
total_angles = np.vstack(_total_angles)
868882
rabi_rotations = total_angles[:, 0]
869883

870-
rabi_rates = [maximum_rabi_rate] * 9
884+
rabi_rates = np.repeat(maximum_rabi_rate, 9)
871885
azimuthal_angles = total_angles[:, 1]
872-
detunings = [0] * 9
873-
durations = [rabi_rotation / maximum_rabi_rate for rabi_rotation in rabi_rotations]
886+
detunings = np.repeat(0, 9)
887+
durations = np.asarray(
888+
[rabi_rotation / maximum_rabi_rate for rabi_rotation in rabi_rotations]
889+
)
874890

875891
return DrivenControl(
876892
rabi_rates=rabi_rates,
@@ -959,10 +975,12 @@ def new_wamf1_control(
959975
rabi_rotations = [theta_plus, theta_minus, theta_minus, theta_plus]
960976
segment_duration = theta_plus / maximum_rabi_rate
961977

962-
rabi_rates = [rabi_rotation / segment_duration for rabi_rotation in rabi_rotations]
963-
azimuthal_angles = [azimuthal_angle] * 4
964-
detunings = [0] * 4
965-
durations = [segment_duration] * 4
978+
rabi_rates = np.asarray(
979+
[rabi_rotation / segment_duration for rabi_rotation in rabi_rotations]
980+
)
981+
azimuthal_angles = np.repeat(azimuthal_angle, 4)
982+
detunings = np.repeat(0, 4)
983+
durations = np.repeat(segment_duration, 4)
966984

967985
return DrivenControl(
968986
rabi_rates=rabi_rates,
@@ -1199,7 +1217,9 @@ def new_modulated_gaussian_control(
11991217
* (maximum_full_rotation_angle / maximum_rotation_angle)
12001218
)
12011219

1202-
azimuthal_angles = [0 if v >= 0 else np.pi for v in modulated_gaussian_segments]
1220+
azimuthal_angles = np.asarray(
1221+
[0 if v >= 0 else np.pi for v in modulated_gaussian_segments]
1222+
)
12031223

12041224
return DrivenControl(
12051225
rabi_rates=np.abs(modulated_gaussian_segments),

qctrlopencontrols/dynamic_decoupling_sequences/dynamic_decoupling_sequence.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def __init__(
9595
{"offsets": offsets, "duration": duration},
9696
)
9797

98-
rabi_rotations = np.asarray(rabi_rotations, dtype=np.float)
98+
rabi_rotations = np.asarray(rabi_rotations, dtype=float)
9999
check_arguments(
100100
np.all(rabi_rotations >= 0),
101101
"Rabi rotations must be non-negative.",
@@ -125,8 +125,8 @@ def __init__(
125125
self.duration = duration
126126
self.offsets = offsets
127127
self.rabi_rotations = rabi_rotations
128-
self.azimuthal_angles = np.asarray(azimuthal_angles, dtype=np.float)
129-
self.detuning_rotations = np.asarray(detuning_rotations, dtype=np.float)
128+
self.azimuthal_angles = np.asarray(azimuthal_angles, dtype=float)
129+
self.detuning_rotations = np.asarray(detuning_rotations, dtype=float)
130130
self.name = name
131131

132132
def export(self) -> Dict:
@@ -474,10 +474,10 @@ def convert_dds_to_driven_control(
474474
if np.allclose(pulse_start_ends, 0.0):
475475
# the original sequence should be a free evolution
476476
return DrivenControl(
477-
rabi_rates=[0.0],
478-
azimuthal_angles=[0.0],
479-
detunings=[0.0],
480-
durations=[sequence_duration],
477+
rabi_rates=np.array([0.0]),
478+
azimuthal_angles=np.array([0.0]),
479+
detunings=np.array([0.0]),
480+
durations=np.array([sequence_duration]),
481481
name=name,
482482
)
483483

qctrlopencontrols/dynamic_decoupling_sequences/predefined.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ def _add_pre_post_rotations(
103103

104104
# parameters for pre-post pulses
105105
rabi_value = np.pi / 2
106-
detuning_value = 0
107-
initial_azimuthal = 0 # for pre-pulse
108-
final_azimuthal = 0 # for post-pulse
106+
detuning_value = 0.0
107+
initial_azimuthal = 0.0 # for pre-pulse
108+
final_azimuthal = 0.0 # for post-pulse
109109

110110
# The sequence will preserve the state |0> is it has an even number
111111
# of X and Y pi-pulses
@@ -183,16 +183,16 @@ def new_ramsey_sequence(duration, pre_post_rotation=False, name=None):
183183
{"duration": duration},
184184
)
185185

186-
offsets = []
187-
rabi_rotations = []
188-
azimuthal_angles = []
189-
detuning_rotations = []
190-
191186
if pre_post_rotation:
192187
offsets = duration * np.array([0.0, 1.0])
193188
rabi_rotations = np.array([np.pi / 2, np.pi / 2])
194189
azimuthal_angles = np.array([0.0, np.pi])
195190
detuning_rotations = np.zeros((2,))
191+
else:
192+
offsets = np.array([])
193+
rabi_rotations = np.array([])
194+
azimuthal_angles = np.array([])
195+
detuning_rotations = np.array([])
196196

197197
return DynamicDecouplingSequence(
198198
duration=duration,
@@ -682,11 +682,11 @@ def new_walsh_sequence(duration, paley_order, pre_post_rotation=False, name=None
682682
** binary_order[hamming_weight - 1 - i]
683683
)
684684

685-
walsh_relative_offsets = []
685+
_walsh_relative_offsets = []
686686
for i in range(samples - 1):
687687
if walsh_array[i] != walsh_array[i + 1]:
688-
walsh_relative_offsets.append((i + 1) * (1.0 / samples))
689-
walsh_relative_offsets = np.array(walsh_relative_offsets, dtype=np.float)
688+
_walsh_relative_offsets.append((i + 1) * (1.0 / samples))
689+
walsh_relative_offsets = np.array(_walsh_relative_offsets, dtype=float)
690690

691691
offsets = duration * walsh_relative_offsets
692692
rabi_rotations = np.full(offsets.shape, np.pi)

0 commit comments

Comments
 (0)