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

Qiskit / qiskit / 13573516292

27 Feb 2025 06:39PM UTC coverage: 88.103% (+0.2%) from 87.86%
13573516292

Pull #13435

github

web-flow
Merge 3a8a5ca9d into b83298bdf
Pull Request #13435: Remove Provider ABC (deprecated in 1.1)

8 of 10 new or added lines in 1 file covered. (80.0%)

40 existing lines in 4 files now uncovered.

77504 of 87970 relevant lines covered (88.1%)

345744.7 hits per line

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

88.89
/qiskit/providers/basic_provider/basic_provider.py
1
# This code is part of Qiskit.
2
#
3
# (C) Copyright IBM 2017, 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

14
"""Provider for basic simulator backends, formerly known as `BasicAer`."""
15

16
from __future__ import annotations
1✔
17

18
from collections.abc import Callable
1✔
19
from collections import OrderedDict
1✔
20
from typing import Type
1✔
21

22
import logging
1✔
23

24
from qiskit.exceptions import QiskitError
1✔
25
from qiskit.providers.backend import Backend
1✔
26
from qiskit.providers.exceptions import QiskitBackendNotFoundError
1✔
27
from qiskit.providers.providerutils import filter_backends
1✔
28

29
from .basic_simulator import BasicSimulator
1✔
30

31

32
logger = logging.getLogger(__name__)
1✔
33

34
SIMULATORS = [BasicSimulator]
1✔
35

36

37
class BasicProvider:
1✔
38
    """Provider for test simulators."""
39

40
    def __init__(self) -> None:
1✔
41
        super().__init__()
1✔
42

43
        # Populate the list of test backends (simulators)
44
        self._backends = self._verify_backends()
1✔
45

46
    def get_backend(self, name=None, **kwargs):
1✔
47
        """Return a single backend matching the specified filtering.
48
        Args:
49
            name (str): name of the backend.
50
            **kwargs: dict used for filtering.
51
        Returns:
52
            Backend: a backend matching the filtering.
53
        Raises:
54
            QiskitBackendNotFoundError: if no backend could be found or
55
                more than one backend matches the filtering criteria.
56
        """
57
        backends = self.backends(name, **kwargs)
1✔
58
        if len(backends) > 1:
1✔
NEW
59
            raise QiskitBackendNotFoundError("More than one backend matches the criteria")
×
60
        if not backends:
1✔
NEW
61
            raise QiskitBackendNotFoundError("No backend matches the criteria")
×
62
        return backends[0]
1✔
63

64
    def backends(self, name: str | None = None, filters: Callable | None = None) -> list[Backend]:
1✔
65
        """Return a list of backends matching the specified filtering.
66
        Args:
67
            name: name of the backend.
68
            filters: callable for filtering.
69
        Returns:
70
            list[Backend]: a list of Backends that match the filtering
71
                criteria.
72
        """
73
        backends = self._backends.values()
1✔
74
        if name:
1✔
75
            available = [
1✔
76
                backend.name() if backend.version == 1 else backend.name for backend in backends
77
            ]
78
            if name not in available:
1✔
79
                raise QiskitBackendNotFoundError(
1✔
80
                    f"The '{name}' backend is not installed in your system."
81
                )
82
        return filter_backends(backends, filters=filters)
1✔
83

84
    def _verify_backends(self) -> OrderedDict[str, Backend]:
1✔
85
        """
86
        Return the test backends in `BACKENDS` that are
87
        effectively available (as some of them might depend on the presence
88
        of an optional dependency or on the existence of a binary).
89

90
        Returns:
91
            dict[str:Backend]: a dict of test backend instances for
92
                the backends that could be instantiated, keyed by backend name.
93
        """
94
        ret = OrderedDict()
1✔
95
        for backend_cls in SIMULATORS:
1✔
96
            backend_instance = self._get_backend_instance(backend_cls)
1✔
97
            backend_name = backend_instance.name
1✔
98
            ret[backend_name] = backend_instance
1✔
99
        return ret
1✔
100

101
    def _get_backend_instance(self, backend_cls: Type[Backend]) -> Backend:
1✔
102
        """
103
        Return an instance of a backend from its class.
104

105
        Args:
106
            backend_cls (class): backend class.
107
        Returns:
108
            Backend: a backend instance.
109
        Raises:
110
            QiskitError: if the backend could not be instantiated.
111
        """
112
        # Verify that the backend can be instantiated.
113
        try:
1✔
114
            backend_instance = backend_cls(provider=self)
1✔
115
        except Exception as err:
×
116
            raise QiskitError(f"Backend {backend_cls} could not be instantiated: {err}") from err
×
117

118
        return backend_instance
1✔
119

120
    def __str__(self) -> str:
1✔
121
        return "BasicProvider"
×
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

© 2026 Coveralls, Inc