Skip to content

Commit b0286de

Browse files
style: Apply PEP 585/604 for type annotations (#239)
Co-authored-by: ABC <[email protected]>
1 parent 798e68e commit b0286de

File tree

5 files changed

+70
-43
lines changed

5 files changed

+70
-43
lines changed

qctrlopencontrols/driven_controls/driven_control.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
"""
1616
Driven control module.
1717
"""
18+
from __future__ import annotations
19+
1820
import csv
1921
import json
2022
from typing import (
21-
Dict,
23+
Any,
2224
Optional,
2325
)
2426

@@ -374,7 +376,7 @@ def resample(self, time_step: float, name: Optional[str] = None) -> "DrivenContr
374376
name,
375377
)
376378

377-
def _qctrl_expanded_export_content(self, coordinates: str) -> Dict:
379+
def _qctrl_expanded_export_content(self, coordinates: str) -> dict[str, Any]:
378380
"""
379381
Prepare the content to be saved in Q-CTRL expanded format.
380382
@@ -386,7 +388,7 @@ def _qctrl_expanded_export_content(self, coordinates: str) -> Dict:
386388
387389
Returns
388390
-------
389-
Dict
391+
dict
390392
A dictionary containing the information of the control.
391393
"""
392394

@@ -551,7 +553,7 @@ def export_to_file(
551553

552554
def export(
553555
self, coordinates=Coordinate.CYLINDRICAL.value, dimensionless_rabi_rate=True
554-
):
556+
) -> dict[str, Any]:
555557

556558
"""
557559
Returns a dictionary formatted for plotting using the ``qctrl-visualizer`` package.

qctrlopencontrols/driven_controls/predefined.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
Module for defining commonly used driven controls.
1717
"""
1818

19-
from typing import (
20-
List,
21-
Optional,
22-
)
19+
from __future__ import annotations
20+
21+
from typing import Optional
2322

2423
import numpy as np
2524

@@ -81,14 +80,14 @@ def _get_transformed_rabi_rotation_wimperis(rabi_rotation: float) -> float:
8180

8281
def _derive_segments(
8382
angles: np.ndarray, amplitude: float = 2.0 * np.pi
84-
) -> List[List[float]]:
83+
) -> list[list[float]]:
8584
"""
8685
Derive the driven control segments from a set of rabi_rotations defined in terms of the
8786
spherical polar angles.
8887
8988
Parameters
9089
----------
91-
angles : numpy.array
90+
angles : np.ndarray
9291
angles is made of a list polar angle 2-lists formatted
9392
as [polar_angle, azimuthal_angle].
9493
All angles should be greater or equal to 0, and the polar_angles

qctrlopencontrols/dynamic_decoupling_sequences/dynamic_decoupling_sequence.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
Dynamical decoupling module.
1717
"""
1818

19+
from __future__ import annotations
20+
1921
from typing import (
20-
Dict,
22+
Any,
2123
Optional,
2224
)
2325

@@ -129,7 +131,7 @@ def __init__(
129131
self.detuning_rotations = np.asarray(detuning_rotations, dtype=float)
130132
self.name = name
131133

132-
def export(self) -> Dict:
134+
def export(self) -> dict[str, Any]:
133135
"""
134136
Returns a dictionary for plotting using the qctrl-visualizer package.
135137

qctrlopencontrols/dynamic_decoupling_sequences/predefined.py

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
Module for defining commonly used dynamical decoupling sequences.
1717
"""
1818

19-
from typing import Tuple
19+
from __future__ import annotations
2020

2121
import numpy as np
2222

@@ -30,7 +30,7 @@ def _add_pre_post_rotations(
3030
rabi_rotations: np.ndarray,
3131
azimuthal_angles: np.ndarray,
3232
detuning_rotations: np.ndarray,
33-
) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
33+
) -> tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
3434
"""
3535
Adds a pre and post X rotation at the start and end of the sequence.
3636
@@ -44,15 +44,15 @@ def _add_pre_post_rotations(
4444
4545
Parameters
4646
----------
47-
duration: float
47+
duration : float
4848
The duration of the sequence
49-
offsets : numpy.ndarray
49+
offsets : np.ndarray
5050
Offsets of the sequence.
51-
rabi_rotations: numpy.ndarray
51+
rabi_rotations : np.ndarray
5252
Rabi rotations at each of the offsets.
53-
azimuthal_angles : numpy.ndarray
53+
azimuthal_angles : np.ndarray
5454
Azimuthal angles at each of the offsets.
55-
detuning_rotations: numpy.ndarray
55+
detuning_rotations : np.ndarray
5656
Detuning rotations at each of the offsets
5757
5858
Returns
@@ -140,7 +140,9 @@ def _add_pre_post_rotations(
140140
return offsets, rabi_rotations, azimuthal_angles, detuning_rotations
141141

142142

143-
def new_ramsey_sequence(duration, pre_post_rotation=False, name=None):
143+
def new_ramsey_sequence(
144+
duration, pre_post_rotation=False, name=None
145+
) -> DynamicDecouplingSequence:
144146
r"""
145147
Creates the Ramsey sequence.
146148
@@ -198,7 +200,9 @@ def new_ramsey_sequence(duration, pre_post_rotation=False, name=None):
198200
)
199201

200202

201-
def new_spin_echo_sequence(duration, pre_post_rotation=False, name=None):
203+
def new_spin_echo_sequence(
204+
duration, pre_post_rotation=False, name=None
205+
) -> DynamicDecouplingSequence:
202206
r"""
203207
Creates the spin echo sequence.
204208
@@ -261,7 +265,7 @@ def new_spin_echo_sequence(duration, pre_post_rotation=False, name=None):
261265

262266
def new_carr_purcell_sequence(
263267
duration, offset_count, pre_post_rotation=False, name=None
264-
):
268+
) -> DynamicDecouplingSequence:
265269
r"""
266270
Creates the Carr-Purcell sequence.
267271
@@ -344,7 +348,9 @@ def new_carr_purcell_sequence(
344348
)
345349

346350

347-
def new_cpmg_sequence(duration, offset_count, pre_post_rotation=False, name=None):
351+
def new_cpmg_sequence(
352+
duration, offset_count, pre_post_rotation=False, name=None
353+
) -> DynamicDecouplingSequence:
348354
r"""
349355
Creates the Carr-Purcell-Meiboom-Gill sequence.
350356
@@ -428,7 +434,9 @@ def new_cpmg_sequence(duration, offset_count, pre_post_rotation=False, name=None
428434
)
429435

430436

431-
def new_uhrig_sequence(duration, offset_count, pre_post_rotation=False, name=None):
437+
def new_uhrig_sequence(
438+
duration, offset_count, pre_post_rotation=False, name=None
439+
) -> DynamicDecouplingSequence:
432440
r"""
433441
Creates the Uhrig sequence.
434442
@@ -507,7 +515,9 @@ def new_uhrig_sequence(duration, offset_count, pre_post_rotation=False, name=Non
507515
)
508516

509517

510-
def new_periodic_sequence(duration, offset_count, pre_post_rotation=False, name=None):
518+
def new_periodic_sequence(
519+
duration, offset_count, pre_post_rotation=False, name=None
520+
) -> DynamicDecouplingSequence:
511521
r"""
512522
Creates the periodic sequence.
513523
@@ -586,7 +596,9 @@ def new_periodic_sequence(duration, offset_count, pre_post_rotation=False, name=
586596
)
587597

588598

589-
def new_walsh_sequence(duration, paley_order, pre_post_rotation=False, name=None):
599+
def new_walsh_sequence(
600+
duration, paley_order, pre_post_rotation=False, name=None
601+
) -> DynamicDecouplingSequence:
590602
r"""
591603
Creates the Walsh sequence.
592604
@@ -709,7 +721,7 @@ def new_walsh_sequence(duration, paley_order, pre_post_rotation=False, name=None
709721

710722
def new_quadratic_sequence(
711723
duration, inner_offset_count, outer_offset_count, pre_post_rotation=False, name=None
712-
):
724+
) -> DynamicDecouplingSequence:
713725
r"""
714726
Creates the quadratic sequence.
715727
@@ -842,7 +854,7 @@ def new_quadratic_sequence(
842854

843855
def new_x_concatenated_sequence(
844856
duration, concatenation_order, pre_post_rotation=False, name=None
845-
):
857+
) -> DynamicDecouplingSequence:
846858
r"""
847859
Creates the :math:`X`-concatenated sequence.
848860
@@ -945,7 +957,7 @@ def new_x_concatenated_sequence(
945957

946958
def new_xy_concatenated_sequence(
947959
duration, concatenation_order, pre_post_rotation=False, name=None
948-
):
960+
) -> DynamicDecouplingSequence:
949961
r"""
950962
Creates the :math:`XY`-concatenated sequence.
951963
@@ -1138,7 +1150,7 @@ def _carr_purcell_meiboom_gill_offsets(
11381150
11391151
Returns
11401152
-------
1141-
numpy.ndarray
1153+
np.ndarray
11421154
The offset values.
11431155
"""
11441156

@@ -1166,7 +1178,7 @@ def _uhrig_single_axis_offsets(duration: float, offset_count: int) -> np.ndarray
11661178
11671179
Returns
11681180
-------
1169-
numpy.ndarray
1181+
np.ndarray
11701182
The offset values.
11711183
"""
11721184

@@ -1192,7 +1204,7 @@ def _concatenation_x(concatenation_sequence: int) -> np.ndarray:
11921204
11931205
Returns
11941206
-------
1195-
numpy.ndarray
1207+
np.ndarray
11961208
The offset values.
11971209
"""
11981210

@@ -1222,7 +1234,7 @@ def _concatenation_xy(concatenation_sequence) -> np.ndarray:
12221234
12231235
Returns
12241236
-------
1225-
numpy.ndarray
1237+
np.ndarray
12261238
The offset values.
12271239
"""
12281240

qctrlopencontrols/utils.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@
1515
"""
1616
Miscellaneous functions and dataclasses.
1717
"""
18+
from __future__ import annotations
19+
1820
from enum import Enum
21+
from typing import (
22+
Any,
23+
Optional,
24+
)
1925

2026
from .exceptions import ArgumentsValueError
2127

2228

23-
def create_repr_from_attributes(class_instance=None, attributes=None):
29+
def create_repr_from_attributes(class_instance=None, attributes=None) -> str:
2430

2531
"""
2632
Returns a string representation of an object.
@@ -81,30 +87,36 @@ def create_repr_from_attributes(class_instance=None, attributes=None):
8187
return repr_string
8288

8389

84-
def check_arguments(condition, description, arguments, extras=None):
90+
def check_arguments(
91+
condition: Any,
92+
description: str,
93+
arguments: dict[str, Any],
94+
extras: Optional[dict[str, Any]] = None,
95+
):
8596
"""
8697
Raises an ArgumentsValueError with the specified parameters if the given condition is false,
8798
otherwise does nothing.
8899
89100
For example, a use case may look like::
90101
91102
def log(x):
92-
check_arguments(x > 0,
93-
"x must be positive.",
94-
{"x": x})
95-
return numpy.log(x)
103+
check_arguments(
104+
x > 0, "x must be positive.", {"x": x}
105+
)
106+
return np.log(x)
96107
97108
Parameters
98109
----------
99-
condition: Any
110+
condition : Any
100111
The condition to be checked. Evaluated result of the condition must be bool.
101-
description: str
112+
description : str
102113
Error information to explain why condition fails.
103-
arguments: dict
114+
arguments : dict
104115
arguments that fail the condition. Keys should be the names of the arguments and arguments
105116
are the values.
106-
extras: dict, optional
117+
extras : dict, optional
107118
Any extra information to explain why condition fails. Defaults to None.
119+
108120
Raises
109121
------
110122
ArgumentsValueError

0 commit comments

Comments
 (0)