• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

qiskit-community / qiskit-algorithms / 7891798718

13 Feb 2024 07:33PM CUT coverage: 90.066%. Remained the same
7891798718

push

github

web-flow
Remove remaining qiskit.org links (backport #152) (#154)

Co-authored-by: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com>
Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>

6446 of 7157 relevant lines covered (90.07%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.22
/qiskit_algorithms/utils/algorithm_globals.py
1
# This code is part of a Qiskit project.
2
#
3
# (C) Copyright IBM 2019, 2023.
4
#
5
# This code is licensed under the Apache License, Version 2.0. You may
6
# obtain a copy of this license in the LICENSE.txt file in the root directory
7
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
8
#
9
# Any modifications or derivative works of this code must retain this
10
# copyright notice, and modified files need to carry a notice indicating
11
# that they have been altered from the originals.
12

13
"""
1✔
14
utils.algorithm_globals
15
=======================
16
Common (global) properties used across qiskit_algorithms.
17

18
.. currentmodule:: qiskit_algorithms.utils.algorithm_globals
19

20
Includes:
21

22
  * Random number generator and random seed.
23

24
    Algorithms can use the generator for random values, as needed, and it
25
    can be seeded here for reproducible results when using such an algorithm.
26
    This is often important, for example in unit tests, where the same
27
    outcome is desired each time (reproducible) and not have it be variable
28
    due to randomness.
29

30
Attributes:
31
    random_seed (int | None): Random generator seed (read/write).
32
    random (np.random.Generator): Random generator (read-only)
33
"""
34

35
from __future__ import annotations
1✔
36

37
import warnings
1✔
38

39
import numpy as np
1✔
40

41

42
class QiskitAlgorithmGlobals:
1✔
43
    """Global properties for algorithms."""
44

45
    # The code is done to work even after some future removal of algorithm_globals
46
    # from Qiskit (qiskit.utils). All that is needed in the future, after that, if
47
    # this is updated, is just the logic in the except blocks.
48
    #
49
    # If the Qiskit version exists this acts a redirect to that (it delegates the
50
    # calls off to it). In the future when that does not exist this has similar code
51
    # in the except blocks here, as noted above, that will take over. By delegating
52
    # to the Qiskit instance it means that any existing code that uses that continues
53
    # to work. Logic here in qiskit_algorithms though uses this instance and the
54
    # random check here has logic to warn if the seed here is not the same as the Qiskit
55
    # version so we can detect direct usage of the Qiskit version and alert the user to
56
    # change their code to use this. So simply changing from:
57
    #     from qiskit.utils import algorithm_globals
58
    # to
59
    #     from qiskit_algorithm.utils import algorithm_globals
60

61
    def __init__(self) -> None:
1✔
62
        self._random_seed: int | None = None
1✔
63
        self._random = None
1✔
64

65
    @property
1✔
66
    def random_seed(self) -> int | None:
1✔
67
        """Random seed property (getter/setter)."""
68
        try:
1✔
69
            from qiskit.utils import algorithm_globals as qiskit_globals
1✔
70

71
            return qiskit_globals.random_seed
1✔
72

73
        except ImportError:
×
74
            return self._random_seed
×
75

76
    @random_seed.setter
1✔
77
    def random_seed(self, seed: int | None) -> None:
1✔
78
        """Set the random generator seed.
79

80
        Args:
81
            seed: If ``None`` then internally a random value is used as a seed
82
        """
83
        try:
1✔
84
            from qiskit.utils import algorithm_globals as qiskit_globals
1✔
85

86
            qiskit_globals.random_seed = seed
1✔
87
            # Mirror the seed here when set via this random_seed. If the seed is
88
            # set on the qiskit.utils instance then we can detect it's different
89
            self._random_seed = seed
1✔
90

91
        except ImportError:
×
92
            self._random_seed = seed
×
93
            self._random = None
×
94

95
    @property
1✔
96
    def random(self) -> np.random.Generator:
1✔
97
        """Return a numpy np.random.Generator (default_rng) using random_seed."""
98
        try:
1✔
99
            from qiskit.utils import algorithm_globals as qiskit_globals
1✔
100

101
            if self._random_seed != qiskit_globals.random_seed:
1✔
102
                # If the seeds are different - likely this local is None and the qiskit.utils
103
                # algorithms global was seeded directly then we will warn to use this here as
104
                # the Qiskit version is planned to be removed in a future version of Qiskit.
105
                warnings.warn(
×
106
                    "Using random that is seeded via qiskit.utils algorithm_globals is deprecated "
107
                    "since version 0.2.0. Instead set random_seed directly to "
108
                    "qiskit_algorithms.utils algorithm_globals.",
109
                    category=DeprecationWarning,
110
                    stacklevel=2,
111
                )
112

113
            return qiskit_globals.random
1✔
114

115
        except ImportError:
×
116
            if self._random is None:
×
117
                self._random = np.random.default_rng(self._random_seed)
×
118
            return self._random
×
119

120

121
# Global instance to be used as the entry point for globals.
122
algorithm_globals = QiskitAlgorithmGlobals()
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc