Skip to content

Commit c6b69a2

Browse files
authored
Update NumPy APIs that will be removed in numpy-2.0 (#6693)
Executed ruff check --select NPY201 --fix ./ Incorporate fix from exported cirq sources, cl/658064952. Ref: https://numpy.org/doc/stable/numpy_2_0_migration_guide.html
1 parent cd3182a commit c6b69a2

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

cirq-core/cirq/contrib/quantum_volume/quantum_volume.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def sample_heavy_set(
147147

148148
results = results.agg(lambda meas: cirq.value.big_endian_bits_to_int(meas), axis=1)
149149
# Compute the number of outputs that are in the heavy set.
150-
num_in_heavy_set = np.sum(np.in1d(results, heavy_set)).item()
150+
num_in_heavy_set = np.sum(np.isin(results, heavy_set)).item()
151151

152152
# Return the number of Heavy outputs over the number of valid runs.
153153
return num_in_heavy_set / len(results)

cirq-core/cirq/protocols/apply_unitary_protocol.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ def apply_unitary(
390390
# Try each strategy, stopping if one works.
391391
# Also catch downcasting warnings and throw an error: #2041
392392
with warnings.catch_warnings():
393-
warnings.filterwarnings(action="error", category=np.ComplexWarning)
393+
warnings.filterwarnings(action="error", category=np.exceptions.ComplexWarning)
394394
for strat in strats:
395395
result = strat(unitary_value, args)
396396
if result is None:

cirq-core/cirq/protocols/apply_unitary_protocol_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,8 @@ def test_cast_to_complex():
714714
)
715715

716716
with pytest.raises(
717-
np.ComplexWarning, match='Casting complex values to real discards the imaginary part'
717+
np.exceptions.ComplexWarning,
718+
match='Casting complex values to real discards the imaginary part',
718719
):
719720
cirq.apply_unitary(y0, args)
720721

cirq-rigetti/cirq_rigetti/quil_input.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,8 @@ def circuit_from_quil(quil: Union[str, Program]) -> Circuit:
328328

329329
defined_gates, parameter_transformers = get_defined_gates(program)
330330

331-
kraus_model: Dict[Tuple[QubitDesignator, ...], List[NDArray[np.complex_]]] = {}
332-
confusion_maps: Dict[int, NDArray[np.float_]] = {}
331+
kraus_model: Dict[Tuple[QubitDesignator, ...], List[NDArray[np.complex128]]] = {}
332+
confusion_maps: Dict[int, NDArray[np.float64]] = {}
333333

334334
# Interpret the Pragmas
335335
for inst in program:
@@ -348,7 +348,7 @@ def circuit_from_quil(quil: Union[str, Program]) -> Circuit:
348348
raise UndefinedQuilGate(f"{gate_name} is not known.")
349349

350350
entries = np.fromstring(
351-
inst.freeform_string.strip("()").replace("i", "j"), dtype=np.complex_, sep=" "
351+
inst.freeform_string.strip("()").replace("i", "j"), dtype=np.complex128, sep=" "
352352
)
353353
dim = int(np.sqrt(len(entries)))
354354
kraus_gate_op = entries.reshape((dim, dim))
@@ -364,7 +364,7 @@ def circuit_from_quil(quil: Union[str, Program]) -> Circuit:
364364
elif inst.command == "READOUT-POVM":
365365
qubit = qubit_index(inst.args[0])
366366
entries = np.fromstring(
367-
inst.freeform_string.strip("()").replace("i", "j"), dtype=np.float_, sep=" "
367+
inst.freeform_string.strip("()").replace("i", "j"), dtype=np.float64, sep=" "
368368
)
369369
confusion_matrix = entries.reshape((2, 2)).T
370370

@@ -408,7 +408,7 @@ def circuit_from_quil(quil: Union[str, Program]) -> Circuit:
408408
)
409409
quil_memory_reference = inst.classical_reg.out()
410410
if qubit in confusion_maps:
411-
cmap: Dict[Tuple[int, ...], NDArray[np.float_]] = {(qubit,): confusion_maps[qubit]}
411+
cmap: Dict[Tuple[int, ...], NDArray[np.float64]] = {(qubit,): confusion_maps[qubit]}
412412
"""
413413
Argument "confusion_map" to "MeasurementGate" has incompatible type
414414
" Dict[Tuple[int], ndarray[Any, dtype[floating[Any]]]]"
@@ -470,12 +470,14 @@ def get_defined_gates(program: Program) -> Tuple[Dict, Dict]:
470470
p.name: a for p, a in zip(defgate.parameters, args)
471471
}
472472
else:
473-
defined_gates[defgate.name] = MatrixGate(np.asarray(defgate.matrix, dtype=np.complex_))
473+
defined_gates[defgate.name] = MatrixGate(
474+
np.asarray(defgate.matrix, dtype=np.complex128)
475+
)
474476
return defined_gates, parameter_transformers
475477

476478

477479
def kraus_noise_model_to_cirq(
478-
kraus_noise_model: Dict[Tuple[QubitDesignator, ...], List[NDArray[np.complex_]]],
480+
kraus_noise_model: Dict[Tuple[QubitDesignator, ...], List[NDArray[np.complex128]]],
479481
defined_gates: Optional[Dict[QubitDesignator, Gate]] = None,
480482
) -> InsertionNoiseModel: # pragma: no cover
481483
"""Construct a Cirq noise model from the provided Kraus operators.
@@ -527,7 +529,7 @@ def quil_expression_to_sympy(expression: ParameterDesignator):
527529
ValueError: Connect convert unknown BinaryExp.
528530
ValueError: Unrecognized expression.
529531
"""
530-
if type(expression) in {np.int_, np.float_, np.complex_, int, float, complex}:
532+
if type(expression) in {np.int_, np.float64, np.complex128, int, float, complex}:
531533
return expression
532534
elif isinstance(expression, Parameter): # pragma: no cover
533535
return sympy.Symbol(expression.name)
@@ -635,7 +637,7 @@ def num_qubits(self):
635637

636638

637639
def remove_gate_from_kraus(
638-
kraus_ops: List[NDArray[np.complex_]], gate_matrix: NDArray[np.complex_]
640+
kraus_ops: List[NDArray[np.complex128]], gate_matrix: NDArray[np.complex128]
639641
): # pragma: no cover
640642
"""Recover the kraus operators from a kraus composed with a gate.
641643
This function is the reverse of append_kraus_to_gate.

0 commit comments

Comments
 (0)