Skip to content

Update NumPy APIs that will be removed in numpy-2.0 #6693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cirq-core/cirq/contrib/quantum_volume/quantum_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def sample_heavy_set(

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

# Return the number of Heavy outputs over the number of valid runs.
return num_in_heavy_set / len(results)
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/protocols/apply_unitary_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ def apply_unitary(
# Try each strategy, stopping if one works.
# Also catch downcasting warnings and throw an error: #2041
with warnings.catch_warnings():
warnings.filterwarnings(action="error", category=np.ComplexWarning)
warnings.filterwarnings(action="error", category=np.exceptions.ComplexWarning)
for strat in strats:
result = strat(unitary_value, args)
if result is None:
Expand Down
3 changes: 2 additions & 1 deletion cirq-core/cirq/protocols/apply_unitary_protocol_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,8 @@ def test_cast_to_complex():
)

with pytest.raises(
np.ComplexWarning, match='Casting complex values to real discards the imaginary part'
np.exceptions.ComplexWarning,
match='Casting complex values to real discards the imaginary part',
):
cirq.apply_unitary(y0, args)

Expand Down
20 changes: 11 additions & 9 deletions cirq-rigetti/cirq_rigetti/quil_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ def circuit_from_quil(quil: Union[str, Program]) -> Circuit:

defined_gates, parameter_transformers = get_defined_gates(program)

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

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

entries = np.fromstring(
inst.freeform_string.strip("()").replace("i", "j"), dtype=np.complex_, sep=" "
inst.freeform_string.strip("()").replace("i", "j"), dtype=np.complex128, sep=" "
)
dim = int(np.sqrt(len(entries)))
kraus_gate_op = entries.reshape((dim, dim))
Expand All @@ -364,7 +364,7 @@ def circuit_from_quil(quil: Union[str, Program]) -> Circuit:
elif inst.command == "READOUT-POVM":
qubit = qubit_index(inst.args[0])
entries = np.fromstring(
inst.freeform_string.strip("()").replace("i", "j"), dtype=np.float_, sep=" "
inst.freeform_string.strip("()").replace("i", "j"), dtype=np.float64, sep=" "
)
confusion_matrix = entries.reshape((2, 2)).T

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


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


def remove_gate_from_kraus(
kraus_ops: List[NDArray[np.complex_]], gate_matrix: NDArray[np.complex_]
kraus_ops: List[NDArray[np.complex128]], gate_matrix: NDArray[np.complex128]
): # pragma: no cover
"""Recover the kraus operators from a kraus composed with a gate.
This function is the reverse of append_kraus_to_gate.
Expand Down