From 0a8e00acdf1b444207771d86e633bda56df69e02 Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Mon, 16 Oct 2023 13:41:30 +0100 Subject: [PATCH 1/2] Fix deprecated Numpy logic in `NormalizeRXAngles` This new pass added in gh-10634 uses some deprecated Numpy properties and has some slightly fragile exception-based logic when the required properties can be directly tested. This code issues warnings with Numpy 1.25+, which is currently not used by CI due to gh-10305. --- .../passes/optimization/normalize_rx_angle.py | 29 +++++++------------ 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/qiskit/transpiler/passes/optimization/normalize_rx_angle.py b/qiskit/transpiler/passes/optimization/normalize_rx_angle.py index 33b381fc2283..8bfb352b7834 100644 --- a/qiskit/transpiler/passes/optimization/normalize_rx_angle.py +++ b/qiskit/transpiler/passes/optimization/normalize_rx_angle.py @@ -76,25 +76,16 @@ def quantize_angles(self, qubit, original_angle): float: Quantized angle. """ - # check if there is already a calibration for a simliar angle - try: - angles = self.already_generated[qubit] # 1d ndarray of already generated angles - similar_angle = angles[ - np.isclose(angles, original_angle, atol=self.resolution_in_radian / 2) - ] - quantized_angle = ( - float(similar_angle[0]) if len(similar_angle) > 1 else float(similar_angle) - ) - except KeyError: - quantized_angle = original_angle - self.already_generated[qubit] = np.array([quantized_angle]) - except TypeError: - quantized_angle = original_angle - self.already_generated[qubit] = np.append( - self.already_generated[qubit], quantized_angle - ) - - return quantized_angle + if (angles := self.already_generated.get(qubit)) is None: + self.already_generated[qubit] = np.array([original_angle]) + return original_angle + similar_angles = angles[ + np.isclose(angles, original_angle, atol=self.resolution_in_radian / 2) + ] + if similar_angles.size == 0: + self.already_generated[qubit] = np.append(angles, original_angle) + return original_angle + return float(angles[0]) def run(self, dag): """Run the NormalizeRXAngle pass on ``dag``. From 38a26ebc00e010aec3cc1f04d5c5af01056565eb Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Mon, 16 Oct 2023 16:21:44 +0100 Subject: [PATCH 2/2] Fix return value --- qiskit/transpiler/passes/optimization/normalize_rx_angle.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qiskit/transpiler/passes/optimization/normalize_rx_angle.py b/qiskit/transpiler/passes/optimization/normalize_rx_angle.py index 8bfb352b7834..ad39cbc229c5 100644 --- a/qiskit/transpiler/passes/optimization/normalize_rx_angle.py +++ b/qiskit/transpiler/passes/optimization/normalize_rx_angle.py @@ -85,7 +85,7 @@ def quantize_angles(self, qubit, original_angle): if similar_angles.size == 0: self.already_generated[qubit] = np.append(angles, original_angle) return original_angle - return float(angles[0]) + return float(similar_angles[0]) def run(self, dag): """Run the NormalizeRXAngle pass on ``dag``.