Skip to content

Commit 1cd4e15

Browse files
Merge pull request #116 from ColibrITD-SAS/fix-typecheck
Fix typecheck errors
2 parents 9748a08 + e29a8a4 commit 1cd4e15

File tree

3 files changed

+24
-28
lines changed

3 files changed

+24
-28
lines changed

mpqp/tools/circuit.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@
3939
from qiskit.circuit.quantumcircuitdata import CircuitInstruction
4040

4141

42-
# @typechecked
43-
# FIXME: Resolve type-checking errors encountered during test execution.
42+
@typechecked
4443
def random_circuit(
4544
gate_classes: Optional[Sequence[type[Gate]]] = None,
4645
nb_qubits: int = 5,
@@ -85,7 +84,7 @@ def random_circuit(
8584
rng = np.random.default_rng(seed)
8685

8786
if nb_gates is None:
88-
nb_gates = rng.integers(5, 10)
87+
nb_gates = int(rng.integers(5, 10))
8988

9089
qcircuit = QCircuit(nb_qubits)
9190
for _ in range(nb_gates):

mpqp/tools/display.py

+20-22
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ def state_vector_ket_shape(sv: npt.NDArray[np.complex64]) -> str:
3131
)[2:]
3232

3333

34-
# @typechecked
35-
# FIXME: Resolve type-checking errors encountered during test execution.
36-
def with_sign(val: np.complex64) -> str:
34+
@typechecked
35+
def with_sign(val: Union[np.complex64, np.complex128]) -> str:
3736
"""Sometimes, we want values under a specific format, in particular
3837
``<sign> <value>``. Where value is as simple as possible (*e.g.* no period
3938
or no imaginary part if there is no need).
@@ -57,18 +56,20 @@ def with_sign(val: np.complex64) -> str:
5756
return "+ " + str_rounded
5857

5958

60-
# @typechecked
61-
# FIXME: Resolve type-checking errors encountered during test execution.
62-
def _remove_null_imag(val: np.complex64) -> np.complex64 | np.float32 | int:
59+
@typechecked
60+
def _remove_null_imag(
61+
val: np.complex64 | np.complex128,
62+
) -> np.complex64 | np.complex128 | np.float32 | int:
6363
val = np.round(val, 3)
6464
if val.imag != 0:
6565
return val
6666
return _remove_unnecessary_decimals(val.real)
6767

6868

69-
# @typechecked
70-
# FIXME: Resolve type-checking errors encountered during test execution.
71-
def _remove_unnecessary_decimals(val: np.float32 | int) -> np.float32 | int:
69+
@typechecked
70+
def _remove_unnecessary_decimals(
71+
val: np.float32 | np.float64 | int,
72+
) -> np.float32 | int:
7273
val = np.float32(val)
7374
if val.is_integer():
7475
return int(val)
@@ -190,9 +191,8 @@ def clean_1D_array(
190191
)
191192

192193

193-
# @typechecked
194-
# FIXME: Resolve type-checking errors encountered during test execution.
195-
def clean_number_repr(number: complex, round: int = 7):
194+
@typechecked
195+
def clean_number_repr(number: Union[complex, np.complex64], round: int = 7):
196196
"""Cleans and formats a number. This function rounds the parts of
197197
complex numbers and formats them as integers if appropriate. It returns a
198198
string representation of the number.
@@ -234,8 +234,7 @@ def clean_number_repr(number: complex, round: int = 7):
234234
return f"{str(real_part)}{str(imag_part)}"
235235

236236

237-
# @typechecked
238-
# FIXME: Resolve type-checking errors encountered during test execution.
237+
@typechecked
239238
def clean_matrix(matrix: Matrix, round: int = 5, align: bool = True):
240239
"""Cleans and formats elements of a 2D matrix. This function rounds the
241240
parts of the numbers in the matrix and formats them as integers if
@@ -250,9 +249,9 @@ def clean_matrix(matrix: Matrix, round: int = 5, align: bool = True):
250249
A string representation of the cleaned matrix.
251250
252251
Examples:
253-
>>> print(clean_matrix([[1.234567895546, 2.3456789645645, 3.45678945645],
254-
... [1+5j, 0+1j, 5.],
255-
... [1.223123425+0.95113462364j, 2.0, 3.0]]))
252+
>>> print(clean_matrix(np.array([[1.234567895546, 2.3456789645645, 3.45678945645],
253+
... [1+5j, 0+1j, 5.],
254+
... [1.223123425+0.95113462364j, 2.0, 3.0]])))
256255
[[1.23457 , 2.34568, 3.45679],
257256
[1+5j , 1j , 5 ],
258257
[1.22312+0.95113j, 2 , 3 ]]
@@ -280,8 +279,7 @@ def clean_matrix(matrix: Matrix, round: int = 5, align: bool = True):
280279
)
281280

282281

283-
# @typechecked
284-
# FIXME: Resolve type-checking errors encountered during test execution.
282+
@typechecked
285283
def pprint(matrix: Matrix, round: int = 5, align: bool = True):
286284
"""Print a cleans and formats elements of a matrix. It rounds the real parts of complex numbers
287285
in the matrix places and formats them as integers if they are whole numbers. It returns a
@@ -293,9 +291,9 @@ def pprint(matrix: Matrix, round: int = 5, align: bool = True):
293291
align: Whether to align the elements for a cleaner output.
294292
295293
Example:
296-
>>> pprint([[1.234567895546, 2.3456789645645, 3.45678945645],
297-
... [1+5j, 0+1j, 5.],
298-
... [1.223123425+0.95113462364j, 2.0, 3.0]])
294+
>>> pprint(np.array([[1.234567895546, 2.3456789645645, 3.45678945645],
295+
... [1+5j, 0+1j, 5.],
296+
... [1.223123425+0.95113462364j, 2.0, 3.0]]))
299297
[[1.23457 , 2.34568, 3.45679],
300298
[1+5j , 1j , 5 ],
301299
[1.22312+0.95113j, 2 , 3 ]]

mpqp/tools/theoretical_simulation.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,7 @@ def exp_id_dist(
216216
return float(jensenshannon(mpqp_counts, noisy_probs * sum(mpqp_counts)))
217217

218218

219-
# @typechecked
220-
# FIXME: Resolve type-checking errors encountered during test execution.
219+
@typechecked
221220
def validate_noisy_circuit(
222221
circuit: QCircuit,
223222
shots: int = 1024,
@@ -233,7 +232,7 @@ def validate_noisy_circuit(
233232
Returns:
234233
Weather our noise pipeline matches the theory or not.
235234
"""
236-
return exp_id_dist(circuit, shots, device) <= trust_int(circuit)
235+
return bool(exp_id_dist(circuit, shots, device) <= trust_int(circuit))
237236

238237

239238
@typechecked

0 commit comments

Comments
 (0)