Source code for qiskit_experiments.framework.backend_data
# This code is part of Qiskit.## (C) Copyright IBM 2021.## This code is licensed under the Apache License, Version 2.0. You may# obtain a copy of this license in the LICENSE.txt file in the root directory# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.## Any modifications or derivative works of this code must retain this# copyright notice, and modified files need to carry a notice indicating# that they have been altered from the originals."""Backend data access helper classThis class was introduced to unify backend data access to either `BackendV1` and `BackendV2`objects, wrapped by an object of this class. This class remains useful as aninterface to backend objects for adjusting to provider-specific quirks."""importwarningsfromqiskit.providersimportBackendV2fromqiskit_experiments.framework.deprecationimportwarn_from_qe
[docs]classBackendData:"""Class for providing joint interface for accessing backend data"""def__init__(self,backend):"""Inits the backend and verifies version"""self._backend=backendself._v1=Falseself._v2=isinstance(backend,BackendV2)ifnotself._v2:try:fromqiskit.providersimportBackendV1self._v1=isinstance(backend,BackendV1)ifself._v1:warn_from_qe(("Support for BackendV1 with Qiskit Experiments is ""deprecated and will be removed in a future release. ""Please update to using BackendV2 backends."),DeprecationWarning,)exceptImportError:pass@propertydefname(self):"""Returns the backend name"""ifself._v1:returnself._backend.name()elifself._v2:returnself._backend.namereturnstr(self._backend)@propertydefgranularity(self):"""Returns the backend's time constraint granularity"""try:ifself._v1:returnself._backend.configuration().timing_constraints.get("granularity",1)elifself._v2:returnself._backend.target.granularityexceptAttributeError:return1return1@propertydefdt(self):"""Returns the backend's input time resolution"""ifself._v1:try:returnself._backend.configuration().dtexceptAttributeError:returnNoneelifself._v2:returnself._backend.dtreturnNone@propertydefmax_circuits(self):"""Returns the backend's max experiments value"""ifself._v1:returngetattr(self._backend.configuration(),"max_experiments",None)elifself._v2:withwarnings.catch_warnings():# qiskit-ibm-runtime deprecated max_circuits:# https://github.com/Qiskit/qiskit-ibm-runtime/pull/2166# Suppress the warning so that we don't trigger it for the user# on every experiment run.## Remove this warning filter if qiskit-ibm-runtime backends# change to reporting max_circuits as None without a warning.warnings.filterwarnings("ignore",message=".*qiskit-ibm-runtime.*",category=DeprecationWarning,)max_circuits=getattr(self._backend,"max_circuits",None)returnmax_circuitsreturnNone@propertydefcoupling_map(self):"""Returns the backend's coupling map"""ifself._v1:returngetattr(self._backend.configuration(),"coupling_map",[])elifself._v2:coupling_map=self._backend.coupling_mapifcoupling_mapisNone:returncoupling_mapreturnlist(coupling_map.get_edges())return[]@propertydefversion(self):"""Returns the backend's version"""ifself._v1:returngetattr(self._backend,"version",None)elifself._v2:returnself._backend.versionreturnNone@propertydefprovider(self):"""Returns the backend's provider"""try:ifself._v1:returnself._backend.provider()elifself._v2:returnself._backend.providerexceptAttributeError:returnNonereturnNone@propertydefnum_qubits(self):"""Returns the backend's number of qubits"""ifself._v1:returnself._backend.configuration().num_qubitselifself._v2:# meas_freq_est is currently not part of the BackendV2returnself._backend.num_qubitsreturnNone
[docs]defqubit_t1(self,qubit:int)->float:"""Return the T1 value for a qubit from the backend properties Args: qubit: the qubit index to return T1 for Returns: The T1 value """ifself._v1:returnself._backend.properties().qubit_property(qubit)["T1"][0]ifself._v2:returnself._backend.qubit_properties(qubit).t1returnfloat("nan")