Utilities (qiskit.utils
)¶
|
Dynamically insert the deprecation message into |
|
Decorator to indicate an argument has been deprecated in some way. |
|
Deprecated. |
|
Decorator to indicate a function has been deprecated. |
|
Deprecated. |
Basic hardware information about the local machine. |
|
Checks whether the current process is the main one |
|
|
Given a SI unit prefix and value, apply the prefix to convert to standard SI unit. |
|
Given a SI unit value, find the most suitable prefix to scale the value. |
|
Wrap the functionality the instance- or class method |
Algorithm Utilities¶
Summarize circuits based on QuantumCircuit, and five metrics are summarized. |
|
Utility method to get an entangler map among qubits. |
|
Validate a user supplied entangler map and converts entries to ints. |
|
Check if IBMQ is installed. |
|
Check if Aer is installed. |
|
Decorator to convert unnamed arguments to named ones. |
|
Class for global properties. |
Deprecated: Quantum Backend including execution setting. |
A QuantumInstance holds the Qiskit backend as well as a number of compile and
runtime parameters controlling circuit compilation and execution. Quantum
algorithms
are run on a device or simulator by passing a QuantumInstance setup with the desired
backend etc.
Optional Depedency Checkers (qiskit.utils.optionals
)¶
Qiskit Terra, and many of the other Qiskit components, have several features that are enabled only
if certain optional dependencies are satisfied. This module is a collection of objects that can
be used to test if certain functionality is available, and optionally raise
MissingOptionalLibraryError
if the functionality is not available.
Available Testers¶
Qiskit Components¶
|
|
|
The |
|
|
|
Qiskit TOQM provides transpiler passes for the Time-optimal Qubit mapping algorithm. |
External Python Libraries¶
|
python-constraint <https://github.com/python-constraint/python-constraint>__ is a constraint satisfaction problem solver, used in the :class:`~.CSPLayout transpiler pass. |
|
The IBM CPLEX Optimizer is a
high-performance mathematical programming solver for linear, mixed-integer and quadratic
programming. It is required by the |
|
CVXPY is a Python package for solving convex optimization
problems. It is required for calculating diamond norms with
|
|
IBM Decision Optimization CPLEX Modelling is a library for prescriptive
analysis. Like CPLEX, it is required for the |
|
The test suite has additional features that are available if the optional fixtures module is installed. This generally also needs
|
|
If the IPython kernel is available, certain additional visualisations and line magics are made available. |
|
Monitoring widgets for jobs running on external backends can be provided if ipywidgets is available. |
|
Some methods of gradient calculation within |
|
Qiskit Terra provides several visualisation tools in the |
|
No longer used by Terra. Internally, Qiskit now uses the high-performance rustworkx library as a core dependency, and during the change-over period, it was sometimes convenient to convert things into the Python-only NetworkX format. Some tests of application modules, such as Qiskit Nature still use NetworkX. |
|
NLopt is a nonlinear optimization library,
used by the global optimizers in the |
|
PIL is a Python image-manipulation library. Qiskit actually uses the pillow fork of PIL if it is available when generating
certain visualizations, for example of both |
|
For some graph visualisations, Qiskit uses pydot as an
interface to GraphViz (see |
|
Various LaTeX-based visualizations, especially the circuit drawers, need access to the pylatexenc project to work correctly. |
|
The functions |
|
Qiskit Terra provides several visualisation tools in the |
|
Some of the gradient functions in |
|
Some of the optimisers in |
|
SQSnobFit is a library for the "stable noisy
optimization by branch and fit" algorithm. It is used by the |
|
Symengine is a fast C++ backend for the
symbolic-manipulation library Sympy. Qiskit uses
special methods from Symengine to accelerate its handling of
|
|
Qiskit Terra's test suite has more advanced functionality available if the optional testtools library is installed. This is generally only needed for Qiskit developers. |
|
Tweedledum is an extension library for
synthesis and optimization of circuits that may involve classical oracles. Qiskit Terra's
|
|
Z3 is a theorem prover, used in the
|
External Command-Line Tools¶
|
For some graph visualisations, Qiskit uses the GraphViz
visualisation tool via its |
|
Visualisation tools that use LaTeX in their output, such as the circuit drawers, require
|
|
Visualisation tools that convert LaTeX-generated files into rasterised images use the
|
Lazy Checker Classes¶
Each of the lazy checkers is an instance of LazyDependencyManager
in one of its two
subclasses: LazyImportTester
and LazySubprocessTester
. These should be imported
from utils
directly if required, such as:
from qiskit.utils import LazyImportTester
- class LazyDependencyManager(*, name=None, callback=None, install=None, msg=None)[source]¶
A mananger for some optional features that are expensive to import, or to verify the existence of.
These objects can be used as Booleans, such as
if x
, and will evaluateTrue
if the dependency they test for is available, andFalse
if not. The presence of the dependency will only be tested when the Boolean is evaluated, so it can be used as a runtime test in functions and methods without requiring an import-time test.These objects also encapsulate the error handling if their dependency is not present, so you can do things such as:
from qiskit.utils import LazyImportManager HAS_MATPLOTLIB = LazyImportManager("matplotlib") @HAS_MATPLOTLIB.require_in_call def my_visualisation(): ... def my_other_visualisation(): # ... some setup ... HAS_MATPLOTLIB.require_now("my_other_visualisation") ... def my_third_visualisation(): if HAS_MATPLOTLIB: from matplotlib import pyplot else: ...
In all of these cases,
matplotlib
is not imported until the functions are entered. In the case of the decorator,matplotlib
is tested for import when the function is called for the first time. In the second and third cases, the loader attempts to importmatplotlib
when therequire_now()
method is called, or when the Boolean context is evaluated. For therequire
methods, an error is raised if the library is not available.This is the base class, which provides the Boolean context checking and error management. The concrete classes
LazyImportTester
andLazySubprocessTester
provide convenient entry points for testing that certain symbols are importable from modules, or certain command-line tools are available, respectively.- প্যারামিটার
name -- the name of this optional dependency.
callback -- a callback that is called immediately after the availability of the library is tested with the result. This will only be called once.
install -- how to install this optional dependency. Passed to
MissingOptionalLibraryError
as thepip_install
parameter.msg -- an extra message to include in the error raised if this is required.
- abstract _is_available()[source]¶
Subclasses of
LazyDependencyManager
should override this method to implement the actual test of availability. This method should return a Boolean, whereTrue
indicates that the dependency was available. This method will only ever be called once.- রিটার্ন টাইপ
bool
- disable_locally()[source]¶
Create a context, during which the value of the dependency manager will be
False
. This means that within the context, any calls to this object will behave as if the dependency is not available, including raising errors. It is valid to call this method whether or not the dependency has already been evaluated. This is most useful in tests.
- require_in_call(feature_or_callable: Callable) Callable [source]¶
- require_in_call(feature_or_callable: str) Callable[[Callable], Callable]
Create a decorator for callables that requires that the dependency is available when the decorated function or method is called.
- প্যারামিটার
feature_or_callable (str or Callable) -- the name of the feature that requires these dependencies. If this function is called directly as a decorator (for example
@HAS_X.require_in_call
as opposed to@HAS_X.require_in_call("my feature")
), then the feature name will be taken to be the function name, or class and method name as appropriate.- রিটার্নস
a decorator that will make its argument require this dependency before it is called.
- রিটার্ন টাইপ
Callable
- require_in_instance(feature_or_class: Type) Type [source]¶
- require_in_instance(feature_or_class: str) Callable[[Type], Type]
A class decorator that requires the dependency is available when the class is initialised. This decorator can be used even if the class does not define an
__init__
method.- প্যারামিটার
feature_or_class (str or Type) -- the name of the feature that requires these dependencies. If this function is called directly as a decorator (for example
@HAS_X.require_in_instance
as opposed to@HAS_X.require_in_instance("my feature")
), then the feature name will be taken as the name of the class.- রিটার্নস
a class decorator that ensures that the wrapped feature is present if the class is initialised.
- রিটার্ন টাইপ
Callable
- require_now(feature)[source]¶
Eagerly attempt to import the dependencies in this object, and raise an exception if they cannot be imported.
- প্যারামিটার
feature (str) -- the name of the feature that is requiring these dependencies.
- রেইজেস
MissingOptionalLibraryError -- if the dependencies cannot be imported.
- class LazyImportTester(name_map_or_modules, *, name=None, callback=None, install=None, msg=None)[source]¶
A lazy dependency tester for importable Python modules. Any required objects will only be imported at the point that this object is tested for its Boolean value.
- প্যারামিটার
name_map_or_modules (Union[str, Dict[str, Iterable[str]], Iterable[str]]) -- if a name map, then a dictionary where the keys are modules or packages, and the values are iterables of names to try and import from that module. It should be valid to write
from <module> import <name1>, <name2>, ...
. If simply a string or iterable of strings, then it should be valid to writeimport <module>
for each of them.- রেইজেস
ValueError -- if no modules are given.
- class LazySubprocessTester(command, *, name=None, callback=None, install=None, msg=None)[source]¶
A lazy checker that a command-line tool is available. The command will only be run once, at the point that this object is checked for its Boolean value.
- প্যারামিটার
command (Union[str, Iterable[str]]) -- the strings that make up the command to be run. For example,
["pdflatex", "-version"]
.- রেইজেস
ValueError -- if an empty command is given.