Skip to content
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

Remove use of dtype aliases to be removed in Numpy 2 #10890

Merged
merged 1 commit into from
Oct 9, 2023
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
22 changes: 11 additions & 11 deletions qiskit/pulse/library/continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def constant(times: np.ndarray, amp: complex) -> np.ndarray:
times: Times to output pulse for.
amp: Complex pulse amplitude.
"""
return np.full(len(times), amp, dtype=np.complex_)
return np.full(len(times), amp, dtype=np.complex128)


def zero(times: np.ndarray) -> np.ndarray:
Expand All @@ -50,7 +50,7 @@ def square(times: np.ndarray, amp: complex, freq: float, phase: float = 0) -> np
phase: Pulse phase.
"""
x = times * freq + phase / np.pi
return amp * (2 * (2 * np.floor(x) - np.floor(2 * x)) + 1).astype(np.complex_)
return amp * (2 * (2 * np.floor(x) - np.floor(2 * x)) + 1).astype(np.complex128)


def sawtooth(times: np.ndarray, amp: complex, freq: float, phase: float = 0) -> np.ndarray:
Expand All @@ -63,7 +63,7 @@ def sawtooth(times: np.ndarray, amp: complex, freq: float, phase: float = 0) ->
phase: Pulse phase.
"""
x = times * freq + phase / np.pi
return amp * 2 * (x - np.floor(1 / 2 + x)).astype(np.complex_)
return amp * 2 * (x - np.floor(1 / 2 + x)).astype(np.complex128)


def triangle(times: np.ndarray, amp: complex, freq: float, phase: float = 0) -> np.ndarray:
Expand All @@ -76,7 +76,7 @@ def triangle(times: np.ndarray, amp: complex, freq: float, phase: float = 0) ->
phase: Pulse phase.
"""
return amp * (-2 * np.abs(sawtooth(times, 1, freq, phase=(phase - np.pi / 2) / 2)) + 1).astype(
np.complex_
np.complex128
)


Expand All @@ -89,7 +89,7 @@ def cos(times: np.ndarray, amp: complex, freq: float, phase: float = 0) -> np.nd
freq: Pulse frequency, units of 1/dt.
phase: Pulse phase.
"""
return amp * np.cos(2 * np.pi * freq * times + phase).astype(np.complex_)
return amp * np.cos(2 * np.pi * freq * times + phase).astype(np.complex128)


def sin(times: np.ndarray, amp: complex, freq: float, phase: float = 0) -> np.ndarray:
Expand All @@ -101,7 +101,7 @@ def sin(times: np.ndarray, amp: complex, freq: float, phase: float = 0) -> np.nd
freq: Pulse frequency, units of 1/dt.
phase: Pulse phase.
"""
return amp * np.sin(2 * np.pi * freq * times + phase).astype(np.complex_)
return amp * np.sin(2 * np.pi * freq * times + phase).astype(np.complex128)


def _fix_gaussian_width(
Expand Down Expand Up @@ -171,9 +171,9 @@ def gaussian(
ret_x: Return centered and standard deviation normalized pulse location.
$x=(times-center)/sigma.
"""
times = np.asarray(times, dtype=np.complex_)
times = np.asarray(times, dtype=np.complex128)
x = (times - center) / sigma
gauss = amp * np.exp(-(x**2) / 2).astype(np.complex_)
gauss = amp * np.exp(-(x**2) / 2).astype(np.complex128)

if zeroed_width is not None:
gauss = _fix_gaussian_width(
Expand Down Expand Up @@ -295,9 +295,9 @@ def sech(
ret_x: Return centered and standard deviation normalized pulse location.
$x=(times-center)/sigma$.
"""
times = np.asarray(times, dtype=np.complex_)
times = np.asarray(times, dtype=np.complex128)
x = (times - center) / sigma
sech_out = amp * sech_fn(x).astype(np.complex_)
sech_out = amp * sech_fn(x).astype(np.complex128)

if zeroed_width is not None:
sech_out = _fix_sech_width(
Expand Down Expand Up @@ -384,7 +384,7 @@ def gaussian_square(
functools.partial(constant, amp=amp),
]
condlist = [times <= square_start, times >= square_stop]
return np.piecewise(times.astype(np.complex_), condlist, funclist)
return np.piecewise(times.astype(np.complex128), condlist, funclist)


def drag(
Expand Down
2 changes: 1 addition & 1 deletion qiskit/pulse/library/samplers/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def call_sampler(duration: int, *args, **kwargs) -> Waveform:
"""Replace the call to the continuous function with a call to the sampler applied
to the analytic pulse function."""
sampled_pulse = sample_function(continuous_pulse, duration, *args, **kwargs)
return np.asarray(sampled_pulse, dtype=np.complex_)
return np.asarray(sampled_pulse, dtype=np.complex128)

# Update type annotations for wrapped continuous function to be discrete
call_sampler = _update_annotations(call_sampler)
Expand Down
6 changes: 3 additions & 3 deletions qiskit/pulse/library/waveform.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(
"""

super().__init__(duration=len(samples), name=name, limit_amplitude=limit_amplitude)
samples = np.asarray(samples, dtype=np.complex_)
samples = np.asarray(samples, dtype=np.complex128)
self.epsilon = epsilon
self._samples = self._clip(samples, epsilon=epsilon)

Expand Down Expand Up @@ -78,7 +78,7 @@ def _clip(self, samples: np.ndarray, epsilon: float = 1e-7) -> np.ndarray:
# first try normalizing by the abs value
clip_where = np.argwhere(to_clip)
clip_angle = np.angle(samples[clip_where])
clipped_samples = np.exp(1j * clip_angle, dtype=np.complex_)
clipped_samples = np.exp(1j * clip_angle, dtype=np.complex128)

# if norm still exceed one subtract epsilon
# required for some platforms
Expand All @@ -87,7 +87,7 @@ def _clip(self, samples: np.ndarray, epsilon: float = 1e-7) -> np.ndarray:
if np.any(to_clip_epsilon):
clip_where_epsilon = np.argwhere(to_clip_epsilon)
clipped_samples_epsilon = (1 - epsilon) * np.exp(
1j * clip_angle[clip_where_epsilon], dtype=np.complex_
1j * clip_angle[clip_where_epsilon], dtype=np.complex128
)
clipped_samples[clip_where_epsilon] = clipped_samples_epsilon

Expand Down
2 changes: 1 addition & 1 deletion qiskit/quantum_info/states/quantum_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def _index_to_ket_array(

if string_labels:
max_dim = max(dims)
char_kets = np.asarray(kets, dtype=np.unicode_)
char_kets = np.asarray(kets, dtype=np.str_)
str_kets = char_kets[0]
for row in char_kets[1:]:
if max_dim > 10:
Expand Down
2 changes: 1 addition & 1 deletion qiskit/result/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def _list_to_complex_array(complex_list):
Raises:
QiskitError: If inner most array of input nested list is not of length 2.
"""
arr = np.asarray(complex_list, dtype=np.complex_)
arr = np.asarray(complex_list, dtype=np.complex128)
if not arr.shape[-1] == 2:
raise QiskitError("Inner most nested list is not of length 2.")

Expand Down
12 changes: 6 additions & 6 deletions qiskit/transpiler/synthesis/aqc/fast_gradient/fast_gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ def __init__(self, num_qubits: int, cnots: np.ndarray):
# array of F-layers:
self._f_layers = np.asarray([object()] * num_qubits, dtype=LayerBase)
# 4x4 C-gate matrices:
self._c_gates = np.full((self.num_cnots, 4, 4), fill_value=0, dtype=np.cfloat)
self._c_gates = np.full((self.num_cnots, 4, 4), fill_value=0, dtype=np.complex128)
# derivatives of 4x4 C-gate matrices:
self._c_dervs = np.full((self.num_cnots, 4, 4, 4), fill_value=0, dtype=np.cfloat)
self._c_dervs = np.full((self.num_cnots, 4, 4, 4), fill_value=0, dtype=np.complex128)
# 4x4 F-gate matrices:
self._f_gates = np.full((num_qubits, 2, 2), fill_value=0, dtype=np.cfloat)
self._f_gates = np.full((num_qubits, 2, 2), fill_value=0, dtype=np.complex128)
# derivatives of 4x4 F-gate matrices:
self._f_dervs = np.full((num_qubits, 3, 2, 2), fill_value=0, dtype=np.cfloat)
self._f_dervs = np.full((num_qubits, 3, 2, 2), fill_value=0, dtype=np.complex128)
# temporary NxN matrices:
self._tmp1 = np.full((dim, dim), fill_value=0, dtype=np.cfloat)
self._tmp2 = np.full((dim, dim), fill_value=0, dtype=np.cfloat)
self._tmp1 = np.full((dim, dim), fill_value=0, dtype=np.complex128)
self._tmp2 = np.full((dim, dim), fill_value=0, dtype=np.complex128)

# Create layers of 2-qubit gates.
for q in range(self.num_cnots):
Expand Down
12 changes: 6 additions & 6 deletions qiskit/transpiler/synthesis/aqc/fast_gradient/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def __init__(self, num_qubits: int, k: int, g2x2: Optional[np.ndarray] = None):
super().__init__()

# 2x2 gate matrix (1-qubit gate).
self._gmat = np.full((2, 2), fill_value=0, dtype=np.cfloat)
self._gmat = np.full((2, 2), fill_value=0, dtype=np.complex128)
if isinstance(g2x2, np.ndarray):
np.copyto(self._gmat, g2x2)

Expand Down Expand Up @@ -114,7 +114,7 @@ def __init__(self, num_qubits: int, j: int, k: int, g4x4: Optional[np.ndarray] =
super().__init__()

# 4x4 gate matrix (2-qubit gate).
self._gmat = np.full((4, 4), fill_value=0, dtype=np.cfloat)
self._gmat = np.full((4, 4), fill_value=0, dtype=np.complex128)
if isinstance(g4x4, np.ndarray):
np.copyto(self._gmat, g4x4)

Expand Down Expand Up @@ -151,7 +151,7 @@ def init_layer1q_matrices(thetas: np.ndarray, dst: np.ndarray) -> np.ndarray:
Returns the "dst" array.
"""
n = thetas.shape[0]
tmp = np.full((4, 2, 2), fill_value=0, dtype=np.cfloat)
tmp = np.full((4, 2, 2), fill_value=0, dtype=np.complex128)
for k in range(n):
th = thetas[k]
a = make_rz(th[0], out=tmp[0])
Expand All @@ -176,9 +176,9 @@ def init_layer1q_deriv_matrices(thetas: np.ndarray, dst: np.ndarray) -> np.ndarr
Returns the "dst" array.
"""
n = thetas.shape[0]
y = np.asarray([[0, -0.5], [0.5, 0]], dtype=np.cfloat)
z = np.asarray([[-0.5j, 0], [0, 0.5j]], dtype=np.cfloat)
tmp = np.full((5, 2, 2), fill_value=0, dtype=np.cfloat)
y = np.asarray([[0, -0.5], [0.5, 0]], dtype=np.complex128)
z = np.asarray([[-0.5j, 0], [0, 0.5j]], dtype=np.complex128)
tmp = np.full((5, 2, 2), fill_value=0, dtype=np.complex128)
for k in range(n):
th = thetas[k]
a = make_rz(th[0], out=tmp[0])
Expand Down
18 changes: 9 additions & 9 deletions qiskit/transpiler/synthesis/aqc/fast_gradient/pmatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ def __init__(self, num_qubits: int):
dim = 2**num_qubits
self._mat = np.empty(0)
self._dim = dim
self._temp_g2x2 = np.zeros((2, 2), dtype=np.cfloat)
self._temp_g4x4 = np.zeros((4, 4), dtype=np.cfloat)
self._temp_g2x2 = np.zeros((2, 2), dtype=np.complex128)
self._temp_g4x4 = np.zeros((4, 4), dtype=np.complex128)
self._temp_2x2 = self._temp_g2x2.copy()
self._temp_4x4 = self._temp_g4x4.copy()
self._identity_perm = np.arange(dim, dtype=np.int64)
self._left_perm = self._identity_perm.copy()
self._right_perm = self._identity_perm.copy()
self._temp_perm = self._identity_perm.copy()
self._temp_slice_dim_x_2 = np.zeros((dim, 2), dtype=np.cfloat)
self._temp_slice_dim_x_4 = np.zeros((dim, 4), dtype=np.cfloat)
self._temp_slice_dim_x_2 = np.zeros((dim, 2), dtype=np.complex128)
self._temp_slice_dim_x_4 = np.zeros((dim, 4), dtype=np.complex128)
self._idx_mat = self._init_index_matrix(dim)
self._temp_block_diag = np.zeros(self._idx_mat.shape, dtype=np.cfloat)
self._temp_block_diag = np.zeros(self._idx_mat.shape, dtype=np.complex128)

def set_matrix(self, mat: np.ndarray):
"""
Expand Down Expand Up @@ -222,7 +222,7 @@ def mul_left_q2(self, layer: Layer2Q, temp_mat: np.ndarray):
# Update left permutation:
self._left_perm[:] = inv_perm

def product_q1(self, layer: Layer1Q, tmp1: np.ndarray, tmp2: np.ndarray) -> np.cfloat:
def product_q1(self, layer: Layer1Q, tmp1: np.ndarray, tmp2: np.ndarray) -> np.complex128:
"""
Computes and returns: ``Trace(mat @ C) = Trace(mat @ P^T @ gmat @ P) =
Trace((P @ mat @ P^T) @ gmat) = Trace(C @ (P @ mat @ P^T)) =
Expand Down Expand Up @@ -254,9 +254,9 @@ def product_q1(self, layer: Layer1Q, tmp1: np.ndarray, tmp2: np.ndarray) -> np.c
tmp3[:, :] = tmp2[i : i + 2, i : i + 2]
_sum += np.dot(gmat_t.ravel(), tmp3.ravel())

return np.cfloat(_sum)
return np.complex128(_sum)

def product_q2(self, layer: Layer2Q, tmp1: np.ndarray, tmp2: np.ndarray) -> np.cfloat:
def product_q2(self, layer: Layer2Q, tmp1: np.ndarray, tmp2: np.ndarray) -> np.complex128:
"""
Computes and returns: ``Trace(mat @ C) = Trace(mat @ P^T @ gmat @ P) =
Trace((P @ mat @ P^T) @ gmat) = Trace(C @ (P @ mat @ P^T)) =
Expand Down Expand Up @@ -287,7 +287,7 @@ def product_q2(self, layer: Layer2Q, tmp1: np.ndarray, tmp2: np.ndarray) -> np.c
bldia = self._temp_block_diag
np.take(tmp2.ravel(), self._idx_mat.ravel(), axis=0, out=bldia.ravel())
bldia *= gmat.reshape(-1, gmat.size)
return np.cfloat(np.sum(bldia))
return np.complex128(np.sum(bldia))

def finalize(self, temp_mat: np.ndarray) -> np.ndarray:
"""
Expand Down
2 changes: 1 addition & 1 deletion test/python/pulse/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def test_play_sample_pulse(self):
def test_play_array_pulse(self):
"""Test play instruction on an array directly."""
d0 = pulse.DriveChannel(0)
test_array = np.array([0.0, 0.0], dtype=np.complex_)
test_array = np.array([0.0, 0.0], dtype=np.complex128)

with pulse.build() as schedule:
pulse.play(test_array, d0)
Expand Down
26 changes: 13 additions & 13 deletions test/python/pulse/test_continuous_pulses.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_constant(self):

constant_arr = continuous.constant(times, amp=amp)

self.assertEqual(constant_arr.dtype, np.complex_)
self.assertEqual(constant_arr.dtype, np.complex128)
np.testing.assert_equal(constant_arr, amp)
self.assertEqual(len(constant_arr), samples)

Expand All @@ -39,7 +39,7 @@ def test_zero(self):
times = np.linspace(0, 10, 50)
zero_arr = continuous.zero(times)

self.assertEqual(zero_arr.dtype, np.complex_)
self.assertEqual(zero_arr.dtype, np.complex128)
np.testing.assert_equal(zero_arr, 0.0)
self.assertEqual(len(zero_arr), 50)

Expand All @@ -53,7 +53,7 @@ def test_square(self):
# with new phase
square_arr_phased = continuous.square(times, amp=amp, freq=freq, phase=np.pi / 2)

self.assertEqual(square_arr.dtype, np.complex_)
self.assertEqual(square_arr.dtype, np.complex128)

self.assertAlmostEqual(square_arr[0], amp)
# test constant
Expand All @@ -74,7 +74,7 @@ def test_sawtooth(self):
# with new phase
sawtooth_arr_phased = continuous.sawtooth(times, amp=amp, freq=freq, phase=np.pi / 2)

self.assertEqual(sawtooth_arr.dtype, np.complex_)
self.assertEqual(sawtooth_arr.dtype, np.complex128)

self.assertAlmostEqual(sawtooth_arr[0], 0.0)
# test slope
Expand All @@ -97,7 +97,7 @@ def test_triangle(self):
# with new phase
triangle_arr_phased = continuous.triangle(times, amp=amp, freq=freq, phase=np.pi / 2)

self.assertEqual(triangle_arr.dtype, np.complex_)
self.assertEqual(triangle_arr.dtype, np.complex128)

self.assertAlmostEqual(triangle_arr[0], 0.0)
# test slope
Expand All @@ -121,7 +121,7 @@ def test_cos(self):
# with new phase
cos_arr_phased = continuous.cos(times, amp=amp, freq=freq, phase=np.pi / 2)

self.assertEqual(cos_arr.dtype, np.complex_)
self.assertEqual(cos_arr.dtype, np.complex128)

# Assert starts at 1
self.assertAlmostEqual(cos_arr[0], amp)
Expand All @@ -144,7 +144,7 @@ def test_sin(self):
# with new phase
sin_arr_phased = continuous.sin(times, amp=0.5, freq=1 / 5, phase=np.pi / 2)

self.assertEqual(sin_arr.dtype, np.complex_)
self.assertEqual(sin_arr.dtype, np.complex128)

# Assert starts at 1
self.assertAlmostEqual(sin_arr[0], 0.0)
Expand Down Expand Up @@ -173,7 +173,7 @@ def test_gaussian(self):
rescale_amp=True,
)

self.assertEqual(gaussian_arr.dtype, np.complex_)
self.assertEqual(gaussian_arr.dtype, np.complex128)

center_time = np.argmax(gaussian_arr)
self.assertAlmostEqual(times[center_time], center)
Expand All @@ -196,7 +196,7 @@ def test_gaussian_deriv(self):
gaussian_deriv_arr = continuous.gaussian_deriv(times, amp, center, sigma)
gaussian_arr = gaussian_deriv_arr * deriv_prefactor

self.assertEqual(gaussian_deriv_arr.dtype, np.complex_)
self.assertEqual(gaussian_deriv_arr.dtype, np.complex128)

self.assertAlmostEqual(
continuous.gaussian_deriv(np.array([0]), amp, center, sigma)[0], 0, places=5
Expand All @@ -215,7 +215,7 @@ def test_sech(self):
sech_arr = continuous.sech(times, amp, center, sigma)
sech_arr_zeroed = continuous.sech(np.array([-1, center, duration + 1]), amp, center, sigma)

self.assertEqual(sech_arr.dtype, np.complex_)
self.assertEqual(sech_arr.dtype, np.complex128)

center_time = np.argmax(sech_arr)
self.assertAlmostEqual(times[center_time], center)
Expand All @@ -234,7 +234,7 @@ def test_sech_deriv(self):

sech_deriv_arr = continuous.sech_deriv(times, amp, center, sigma)

self.assertEqual(sech_deriv_arr.dtype, np.complex_)
self.assertEqual(sech_deriv_arr.dtype, np.complex128)

self.assertAlmostEqual(
continuous.sech_deriv(np.array([0]), amp, center, sigma)[0], 0, places=3
Expand All @@ -249,7 +249,7 @@ def test_gaussian_square(self):
times, dt = np.linspace(0, 20, 2001, retstep=True)
gaussian_square_arr = continuous.gaussian_square(times, amp, center, width, sigma)

self.assertEqual(gaussian_square_arr.dtype, np.complex_)
self.assertEqual(gaussian_square_arr.dtype, np.complex128)

self.assertEqual(gaussian_square_arr[1000], amp)
# test half gaussian rise/fall
Expand Down Expand Up @@ -302,6 +302,6 @@ def test_drag(self):
times, amp, center, sigma, beta=beta, zeroed_width=2 * (center + 1), rescale_amp=True
)

self.assertEqual(drag_arr.dtype, np.complex_)
self.assertEqual(drag_arr.dtype, np.complex128)

np.testing.assert_equal(drag_arr, gaussian_arr)
Loading