From f21372721a67b2788929e676e40242a7ecba862a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= Date: Mon, 27 Nov 2023 13:45:16 +0100 Subject: [PATCH 1/4] Deprecate utils --- qiskit/utils/arithmetic.py | 43 +++++++++++++++++++++++++++++++ qiskit/utils/circuit_utils.py | 7 +++++ qiskit/utils/entangler_map.py | 13 ++++++++++ qiskit/utils/name_unnamed_args.py | 7 +++++ 4 files changed, 70 insertions(+) diff --git a/qiskit/utils/arithmetic.py b/qiskit/utils/arithmetic.py index 23a838721f9e..63aa9915416e 100644 --- a/qiskit/utils/arithmetic.py +++ b/qiskit/utils/arithmetic.py @@ -16,8 +16,15 @@ from typing import List, Tuple import numpy as np +from qiskit.utils.deprecation import deprecate_func +@deprecate_func( + since="0.46.0", + package_name="qiskit", + additional_msg="This function was only used in the context of qiskit.opflow/qiskit.algorithms, " + "two modules deprecated since Qiskit Terra 0.24.0.", +) def normalize_vector(vector): """ Normalize the input state vector. @@ -25,6 +32,12 @@ def normalize_vector(vector): return vector / np.linalg.norm(vector) +@deprecate_func( + since="0.46.0", + package_name="qiskit", + additional_msg="This function was only used in the context of qiskit.opflow/qiskit.algorithms, " + "two modules deprecated since Qiskit Terra 0.24.0.", +) def is_power_of_2(num): """ Check if the input number is a power of 2. @@ -32,6 +45,12 @@ def is_power_of_2(num): return num != 0 and ((num & (num - 1)) == 0) +@deprecate_func( + since="0.46.0", + package_name="qiskit", + additional_msg="This function was only used in the context of qiskit.opflow/qiskit.algorithms, " + "two modules deprecated since Qiskit Terra 0.24.0.", +) def log2(num): """ Compute the log2 of the input number. Use bit operation if the input is a power of 2. @@ -47,6 +66,12 @@ def log2(num): return np.log2(num) +@deprecate_func( + since="0.46.0", + package_name="qiskit", + additional_msg="This function was only used in the context of qiskit.opflow/qiskit.algorithms, " + "two modules deprecated since Qiskit Terra 0.24.0.", +) def is_power(num, return_decomposition=False): """ Check if num is a perfect power in O(n^3) time, n=ceil(logN) @@ -80,6 +105,12 @@ def is_power(num, return_decomposition=False): return False +@deprecate_func( + since="0.46.0", + package_name="qiskit", + additional_msg="This function was only used in the context of qiskit.opflow/qiskit.algorithms, " + "two modules deprecated since Qiskit Terra 0.24.0.", +) def next_power_of_2_base(n): """ Return the base of the smallest power of 2 no less than the input number @@ -95,6 +126,12 @@ def next_power_of_2_base(n): return base +@deprecate_func( + since="0.46.0", + package_name="qiskit", + additional_msg="This function was only used in the context of qiskit.opflow/qiskit.algorithms, " + "two modules deprecated since Qiskit Terra 0.24.0.", +) def transpositions(permutation: List[int]) -> List[Tuple[int, int]]: """Return a sequence of transpositions, corresponding to the permutation. @@ -132,6 +169,12 @@ def transpositions(permutation: List[int]) -> List[Tuple[int, int]]: return res +@deprecate_func( + since="0.46.0", + package_name="qiskit", + additional_msg="This function was only used in the context of qiskit.opflow/qiskit.algorithms, " + "two modules deprecated since Qiskit Terra 0.24.0.", +) def triu_to_dense(triu: np.ndarray) -> np.ndarray: """Converts upper triangular part of matrix to dense matrix. diff --git a/qiskit/utils/circuit_utils.py b/qiskit/utils/circuit_utils.py index 2fe140d3780d..11c25b87c917 100644 --- a/qiskit/utils/circuit_utils.py +++ b/qiskit/utils/circuit_utils.py @@ -13,8 +13,15 @@ """Circuit utility functions""" import numpy as np +from qiskit.utils.deprecation import deprecate_func +@deprecate_func( + since="0.46.0", + package_name="qiskit", + additional_msg="This function was only used in the context of qiskit.opflow/qiskit.algorithms, " + "two modules deprecated since Qiskit Terra 0.24.0.", +) def summarize_circuits(circuits): """Summarize circuits based on QuantumCircuit, and five metrics are summarized. - Number of qubits diff --git a/qiskit/utils/entangler_map.py b/qiskit/utils/entangler_map.py index 1cd750398ccb..0fadf7c2a696 100644 --- a/qiskit/utils/entangler_map.py +++ b/qiskit/utils/entangler_map.py @@ -14,8 +14,15 @@ This module contains the definition of creating and validating entangler map based on the number of qubits. """ +from qiskit.utils.deprecation import deprecate_func +@deprecate_func( + since="0.46.0", + package_name="qiskit", + additional_msg="This function was only used in the context of qiskit.opflow/qiskit.algorithms, " + "two modules deprecated since Qiskit Terra 0.24.0.", +) def get_entangler_map(map_type, num_qubits, offset=0): """Utility method to get an entangler map among qubits. @@ -67,6 +74,12 @@ def get_entangler_map(map_type, num_qubits, offset=0): return ret +@deprecate_func( + since="0.46.0", + package_name="qiskit", + additional_msg="This function was only used in the context of qiskit.opflow/qiskit.algorithms, " + "two modules deprecated since Qiskit Terra 0.24.0.", +) def validate_entangler_map(entangler_map, num_qubits, allow_double_entanglement=False): """Validate a user supplied entangler map and converts entries to ints. diff --git a/qiskit/utils/name_unnamed_args.py b/qiskit/utils/name_unnamed_args.py index 4e153dcfefd2..f70828db1ba5 100644 --- a/qiskit/utils/name_unnamed_args.py +++ b/qiskit/utils/name_unnamed_args.py @@ -13,8 +13,15 @@ """Tool to name unnamed arguments.""" import functools +from qiskit.utils.deprecation import deprecate_func +@deprecate_func( + since="0.46.0", + package_name="qiskit", + additional_msg="This function was only used in the context of qiskit.opflow/qiskit.algorithms, " + "two modules deprecated since Qiskit Terra 0.24.0.", +) def name_args(mapping, skip=0): """Decorator to convert unnamed arguments to named ones. From a7a2a34cc25d5296a709259c2a9ac9e289381b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= Date: Mon, 27 Nov 2023 14:18:13 +0100 Subject: [PATCH 2/4] Fix tests, add reno --- ...over-algorithm-utils-d77253426257f41f.yaml | 13 ++++++ test/python/algorithms/test_entangler_map.py | 40 ++++++++++--------- test/python/test_util.py | 4 +- 3 files changed, 37 insertions(+), 20 deletions(-) create mode 100644 releasenotes/notes/deprecate-leftover-algorithm-utils-d77253426257f41f.yaml diff --git a/releasenotes/notes/deprecate-leftover-algorithm-utils-d77253426257f41f.yaml b/releasenotes/notes/deprecate-leftover-algorithm-utils-d77253426257f41f.yaml new file mode 100644 index 000000000000..a432c40a3333 --- /dev/null +++ b/releasenotes/notes/deprecate-leftover-algorithm-utils-d77253426257f41f.yaml @@ -0,0 +1,13 @@ +--- +deprecations: + - | + The following tools in :mod:`qiskit.utils` have been deprecated: + + * Utils in ``qiskit.utils.arithmetic.py`` + * Utils in ``qiskit.utils.circuit_utils.py`` + * Utils in ``qiskit.utils.entangler_map.py`` + * Utils in ``qiskit.utils.name_unnamed_args.py`` + + These functions were used exclusively in the context of ``qiskit.algorithms`` and + ``qiskit.opflow``, and will be removed following the removals of + ``qiskit.algorithms`` and ``qiskit.opflow`` in Qiskit 1.0. diff --git a/test/python/algorithms/test_entangler_map.py b/test/python/algorithms/test_entangler_map.py index 56dd5100c64f..1377b30e1146 100644 --- a/test/python/algorithms/test_entangler_map.py +++ b/test/python/algorithms/test_entangler_map.py @@ -24,7 +24,8 @@ class TestEntanglerMap(QiskitAlgorithmsTestCase): def test_map_type_linear(self): """,ap type linear test""" ref_map = [[0, 1], [1, 2], [2, 3]] - entangler_map = get_entangler_map("linear", 4) + with self.assertWarns(DeprecationWarning): + entangler_map = get_entangler_map("linear", 4) for (ref_src, ref_targ), (exp_src, exp_targ) in zip(ref_map, entangler_map): self.assertEqual(ref_src, exp_src) @@ -33,7 +34,8 @@ def test_map_type_linear(self): def test_map_type_full(self): """map type full test""" ref_map = [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]] - entangler_map = get_entangler_map("full", 4) + with self.assertWarns(DeprecationWarning): + entangler_map = get_entangler_map("full", 4) for (ref_src, ref_targ), (exp_src, exp_targ) in zip(ref_map, entangler_map): self.assertEqual(ref_src, exp_src) @@ -41,27 +43,29 @@ def test_map_type_full(self): def test_validate_entangler_map(self): """validate entangler map test""" - valid_map = [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]] - self.assertTrue(validate_entangler_map(valid_map, 4)) + with self.assertWarns(DeprecationWarning): - valid_map_2 = [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3], [3, 2]] - self.assertTrue(validate_entangler_map(valid_map_2, 4, True)) + valid_map = [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]] + self.assertTrue(validate_entangler_map(valid_map, 4)) - invalid_map = [[0, 4], [4, 2], [0, 3], [1, 2], [1, 3], [2, 3]] - with self.assertRaises(ValueError): - validate_entangler_map(invalid_map, 4) + valid_map_2 = [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3], [3, 2]] + self.assertTrue(validate_entangler_map(valid_map_2, 4, True)) - invalid_map_2 = [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3], [3, 2]] - with self.assertRaises(ValueError): - validate_entangler_map(invalid_map_2, 4) + invalid_map = [[0, 4], [4, 2], [0, 3], [1, 2], [1, 3], [2, 3]] + with self.assertRaises(ValueError): + validate_entangler_map(invalid_map, 4) - wrong_type_map = {0: [1, 2, 3], 1: [2, 3]} - with self.assertRaises(TypeError): - validate_entangler_map(wrong_type_map, 4) + invalid_map_2 = [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3], [3, 2]] + with self.assertRaises(ValueError): + validate_entangler_map(invalid_map_2, 4) - wrong_type_map_2 = [(0, 1), (0, 2), (0, 3)] - with self.assertRaises(TypeError): - validate_entangler_map(wrong_type_map_2, 4) + wrong_type_map = {0: [1, 2, 3], 1: [2, 3]} + with self.assertRaises(TypeError): + validate_entangler_map(wrong_type_map, 4) + + wrong_type_map_2 = [(0, 1), (0, 2), (0, 3)] + with self.assertRaises(TypeError): + validate_entangler_map(wrong_type_map_2, 4) if __name__ == "__main__": diff --git a/test/python/test_util.py b/test/python/test_util.py index b574ff8390b0..4c9b53833ed1 100644 --- a/test/python/test_util.py +++ b/test/python/test_util.py @@ -40,5 +40,5 @@ def test_triu_to_dense(self): symm = (m + m.T) / 2 triu = [[symm[i, j] for i in range(j, n)] for j in range(n)] - - self.assertTrue(np.array_equal(symm, triu_to_dense(triu))) + with self.assertWarns(DeprecationWarning): + self.assertTrue(np.array_equal(symm, triu_to_dense(triu))) From b0b8797f013092d2c7550b82a897fc7d572c1ea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= Date: Mon, 27 Nov 2023 14:27:05 +0100 Subject: [PATCH 3/4] Update deprecation message --- qiskit/utils/arithmetic.py | 14 +++++++------- qiskit/utils/circuit_utils.py | 2 +- qiskit/utils/entangler_map.py | 4 ++-- qiskit/utils/name_unnamed_args.py | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/qiskit/utils/arithmetic.py b/qiskit/utils/arithmetic.py index 63aa9915416e..b1f2fa9a8657 100644 --- a/qiskit/utils/arithmetic.py +++ b/qiskit/utils/arithmetic.py @@ -23,7 +23,7 @@ since="0.46.0", package_name="qiskit", additional_msg="This function was only used in the context of qiskit.opflow/qiskit.algorithms, " - "two modules deprecated since Qiskit Terra 0.24.0.", + "two modules deprecated and planned to be removed in Qiskit 1.0.", ) def normalize_vector(vector): """ @@ -36,7 +36,7 @@ def normalize_vector(vector): since="0.46.0", package_name="qiskit", additional_msg="This function was only used in the context of qiskit.opflow/qiskit.algorithms, " - "two modules deprecated since Qiskit Terra 0.24.0.", + "two modules deprecated and planned to be removed in Qiskit 1.0.", ) def is_power_of_2(num): """ @@ -49,7 +49,7 @@ def is_power_of_2(num): since="0.46.0", package_name="qiskit", additional_msg="This function was only used in the context of qiskit.opflow/qiskit.algorithms, " - "two modules deprecated since Qiskit Terra 0.24.0.", + "two modules deprecated and planned to be removed in Qiskit 1.0.", ) def log2(num): """ @@ -70,7 +70,7 @@ def log2(num): since="0.46.0", package_name="qiskit", additional_msg="This function was only used in the context of qiskit.opflow/qiskit.algorithms, " - "two modules deprecated since Qiskit Terra 0.24.0.", + "two modules deprecated and planned to be removed in Qiskit 1.0.", ) def is_power(num, return_decomposition=False): """ @@ -109,7 +109,7 @@ def is_power(num, return_decomposition=False): since="0.46.0", package_name="qiskit", additional_msg="This function was only used in the context of qiskit.opflow/qiskit.algorithms, " - "two modules deprecated since Qiskit Terra 0.24.0.", + "two modules deprecated and planned to be removed in Qiskit 1.0.", ) def next_power_of_2_base(n): """ @@ -130,7 +130,7 @@ def next_power_of_2_base(n): since="0.46.0", package_name="qiskit", additional_msg="This function was only used in the context of qiskit.opflow/qiskit.algorithms, " - "two modules deprecated since Qiskit Terra 0.24.0.", + "two modules deprecated and planned to be removed in Qiskit 1.0.", ) def transpositions(permutation: List[int]) -> List[Tuple[int, int]]: """Return a sequence of transpositions, corresponding to the permutation. @@ -173,7 +173,7 @@ def transpositions(permutation: List[int]) -> List[Tuple[int, int]]: since="0.46.0", package_name="qiskit", additional_msg="This function was only used in the context of qiskit.opflow/qiskit.algorithms, " - "two modules deprecated since Qiskit Terra 0.24.0.", + "two modules deprecated and planned to be removed in Qiskit 1.0.", ) def triu_to_dense(triu: np.ndarray) -> np.ndarray: """Converts upper triangular part of matrix to dense matrix. diff --git a/qiskit/utils/circuit_utils.py b/qiskit/utils/circuit_utils.py index 11c25b87c917..346b9d0c7cf6 100644 --- a/qiskit/utils/circuit_utils.py +++ b/qiskit/utils/circuit_utils.py @@ -20,7 +20,7 @@ since="0.46.0", package_name="qiskit", additional_msg="This function was only used in the context of qiskit.opflow/qiskit.algorithms, " - "two modules deprecated since Qiskit Terra 0.24.0.", + "two modules deprecated and planned to be removed in Qiskit 1.0.", ) def summarize_circuits(circuits): """Summarize circuits based on QuantumCircuit, and five metrics are summarized. diff --git a/qiskit/utils/entangler_map.py b/qiskit/utils/entangler_map.py index 0fadf7c2a696..e5e53a26e911 100644 --- a/qiskit/utils/entangler_map.py +++ b/qiskit/utils/entangler_map.py @@ -21,7 +21,7 @@ since="0.46.0", package_name="qiskit", additional_msg="This function was only used in the context of qiskit.opflow/qiskit.algorithms, " - "two modules deprecated since Qiskit Terra 0.24.0.", + "two modules deprecated and planned to be removed in Qiskit 1.0.", ) def get_entangler_map(map_type, num_qubits, offset=0): """Utility method to get an entangler map among qubits. @@ -78,7 +78,7 @@ def get_entangler_map(map_type, num_qubits, offset=0): since="0.46.0", package_name="qiskit", additional_msg="This function was only used in the context of qiskit.opflow/qiskit.algorithms, " - "two modules deprecated since Qiskit Terra 0.24.0.", + "two modules deprecated and planned to be removed in Qiskit 1.0.", ) def validate_entangler_map(entangler_map, num_qubits, allow_double_entanglement=False): """Validate a user supplied entangler map and converts entries to ints. diff --git a/qiskit/utils/name_unnamed_args.py b/qiskit/utils/name_unnamed_args.py index f70828db1ba5..fc778616bec4 100644 --- a/qiskit/utils/name_unnamed_args.py +++ b/qiskit/utils/name_unnamed_args.py @@ -20,7 +20,7 @@ since="0.46.0", package_name="qiskit", additional_msg="This function was only used in the context of qiskit.opflow/qiskit.algorithms, " - "two modules deprecated since Qiskit Terra 0.24.0.", + "two modules deprecated and planned to be removed in Qiskit 1.0.", ) def name_args(mapping, skip=0): """Decorator to convert unnamed arguments to named ones. From 96edf4584b848d1b73bb2db824c4cbd1eb85f933 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= Date: Tue, 28 Nov 2023 10:27:10 +0100 Subject: [PATCH 4/4] Update reno --- ...precate-leftover-algorithm-utils-d77253426257f41f.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/releasenotes/notes/deprecate-leftover-algorithm-utils-d77253426257f41f.yaml b/releasenotes/notes/deprecate-leftover-algorithm-utils-d77253426257f41f.yaml index a432c40a3333..a9af79953ee1 100644 --- a/releasenotes/notes/deprecate-leftover-algorithm-utils-d77253426257f41f.yaml +++ b/releasenotes/notes/deprecate-leftover-algorithm-utils-d77253426257f41f.yaml @@ -3,10 +3,10 @@ deprecations: - | The following tools in :mod:`qiskit.utils` have been deprecated: - * Utils in ``qiskit.utils.arithmetic.py`` - * Utils in ``qiskit.utils.circuit_utils.py`` - * Utils in ``qiskit.utils.entangler_map.py`` - * Utils in ``qiskit.utils.name_unnamed_args.py`` + * Utils in ``qiskit.utils.arithmetic`` + * Utils in ``qiskit.utils.circuit_utils`` + * Utils in ``qiskit.utils.entangler_map`` + * Utils in ``qiskit.utils.name_unnamed_args`` These functions were used exclusively in the context of ``qiskit.algorithms`` and ``qiskit.opflow``, and will be removed following the removals of