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

Deprecate leftover algorithm/opflow utils #11323

Merged
merged 4 commits into from
Nov 28, 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
43 changes: 43 additions & 0 deletions qiskit/utils/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,41 @@

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 and planned to be removed in Qiskit 1.0.",
)
def normalize_vector(vector):
"""
Normalize the input state 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 and planned to be removed in Qiskit 1.0.",
)
def is_power_of_2(num):
"""
Check if the input number is a power of 2.
"""
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 and planned to be removed in Qiskit 1.0.",
)
def log2(num):
"""
Compute the log2 of the input number. Use bit operation if the input is a power of 2.
Expand All @@ -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 and planned to be removed in Qiskit 1.0.",
)
def is_power(num, return_decomposition=False):
"""
Check if num is a perfect power in O(n^3) time, n=ceil(logN)
Expand Down Expand Up @@ -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 and planned to be removed in Qiskit 1.0.",
)
def next_power_of_2_base(n):
"""
Return the base of the smallest power of 2 no less than the input number
Expand All @@ -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 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.

Expand Down Expand Up @@ -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 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.

Expand Down
7 changes: 7 additions & 0 deletions qiskit/utils/circuit_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 and planned to be removed in Qiskit 1.0.",
)
def summarize_circuits(circuits):
"""Summarize circuits based on QuantumCircuit, and five metrics are summarized.
- Number of qubits
Expand Down
13 changes: 13 additions & 0 deletions qiskit/utils/entangler_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 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.

Expand Down Expand Up @@ -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 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.

Expand Down
7 changes: 7 additions & 0 deletions qiskit/utils/name_unnamed_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 and planned to be removed in Qiskit 1.0.",
)
def name_args(mapping, skip=0):
"""Decorator to convert unnamed arguments to named ones.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
deprecations:
- |
The following tools in :mod:`qiskit.utils` have been deprecated:

* 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
``qiskit.algorithms`` and ``qiskit.opflow`` in Qiskit 1.0.
40 changes: 22 additions & 18 deletions test/python/algorithms/test_entangler_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -33,35 +34,38 @@ 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)
self.assertEqual(ref_targ, exp_targ)

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__":
Expand Down
4 changes: 2 additions & 2 deletions test/python/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)))