Skip to content

Commit

Permalink
Update fake backend refresh() (#2020)
Browse files Browse the repository at this point in the history
* Update fake backend refresh()

* release note

---------

Co-authored-by: ptristan3 <[email protected]>
  • Loading branch information
kt474 and ptristan3 authored Dec 4, 2024
1 parent 7215ab2 commit 37dee1a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 43 deletions.
86 changes: 43 additions & 43 deletions qiskit_ibm_runtime/fake_provider/fake_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
)
from .. import QiskitRuntimeService
from ..utils.backend_encoder import BackendEncoder
from ..utils.backend_decoder import configuration_from_server_data

from ..models import (
BackendProperties,
Expand Down Expand Up @@ -590,54 +591,53 @@ def refresh(self, service: QiskitRuntimeService) -> None:
backends = service.backends(prod_name)
real_backend = backends[0]

real_props = real_backend.properties()
real_config = real_backend.configuration()
real_defs = real_backend.defaults()
real_props = real_backend.properties(refresh=True)
real_config = configuration_from_server_data(
raw_config=service._api_client.backend_configuration(prod_name, refresh=True)
)
real_defs = real_backend.defaults(refresh=True)

updated_config = real_config.to_dict()
updated_config["backend_name"] = self.backend_name

if updated_config != self._conf_dict:

if real_config:
config_path = os.path.join(self.dirname, self.conf_filename)
with open(config_path, "w", encoding="utf-8") as fd:
fd.write(json.dumps(real_config.to_dict(), cls=BackendEncoder))

if real_props:
props_path = os.path.join(self.dirname, self.props_filename)
with open(props_path, "w", encoding="utf-8") as fd:
fd.write(json.dumps(real_props.to_dict(), cls=BackendEncoder))

if real_defs:
defs_path = os.path.join(self.dirname, self.defs_filename)
with open(defs_path, "w", encoding="utf-8") as fd:
fd.write(json.dumps(real_defs.to_dict(), cls=BackendEncoder))

if self._target is not None:
self._conf_dict = self._get_conf_dict_from_json() # type: ignore[unreachable]
self._set_props_dict_from_json()
self._set_defs_dict_from_json()

updated_configuration = BackendConfiguration.from_dict(self._conf_dict)
updated_properties = BackendProperties.from_dict(self._props_dict)
updated_defaults = PulseDefaults.from_dict(self._defs_dict)

self._target = convert_to_target(
configuration=updated_configuration,
properties=updated_properties,
defaults=updated_defaults,
include_control_flow=True,
include_fractional_gates=True,
)
if real_config:
config_path = os.path.join(self.dirname, self.conf_filename)
with open(config_path, "w", encoding="utf-8") as fd:
fd.write(json.dumps(real_config.to_dict(), cls=BackendEncoder))

if real_props:
props_path = os.path.join(self.dirname, self.props_filename)
with open(props_path, "w", encoding="utf-8") as fd:
fd.write(json.dumps(real_props.to_dict(), cls=BackendEncoder))

logger.info(
"The backend %s has been updated from version %s to %s version.",
self.backend_name,
version,
real_props.backend_version,
if real_defs:
defs_path = os.path.join(self.dirname, self.defs_filename)
with open(defs_path, "w", encoding="utf-8") as fd:
fd.write(json.dumps(real_defs.to_dict(), cls=BackendEncoder))

if self._target is not None:
self._conf_dict = self._get_conf_dict_from_json() # type: ignore[unreachable]
self._set_props_dict_from_json()
self._set_defs_dict_from_json()

updated_configuration = BackendConfiguration.from_dict(self._conf_dict)
updated_properties = BackendProperties.from_dict(self._props_dict)
updated_defaults = PulseDefaults.from_dict(self._defs_dict)

self._target = convert_to_target(
configuration=updated_configuration,
properties=updated_properties,
defaults=updated_defaults,
include_control_flow=True,
include_fractional_gates=True,
)
else:
logger.info("There are no available new updates for %s.", self.backend_name)

logger.info(
"The backend %s has been updated from version %s to %s version.",
self.backend_name,
version,
real_props.backend_version,
)

except Exception as ex: # pylint: disable=broad-except
logger.warning("The refreshing of %s has failed: %s", self.backend_name, str(ex))
2 changes: 2 additions & 0 deletions release-notes/unreleased/2020.bug.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed an issue where ``FakeBackendV2.refresh()`` wouldn't always
refresh the backend properties, defaults, and configuration.

0 comments on commit 37dee1a

Please sign in to comment.