Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add backend refresh() method #1955

Merged
merged 14 commits into from
Oct 24, 2024
4 changes: 2 additions & 2 deletions qiskit_ibm_runtime/api/clients/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def is_qctrl_enabled(self) -> bool:
"""
return self._api.is_qctrl_enabled()

def backend_configuration(self, backend_name: str) -> Dict[str, Any]:
def backend_configuration(self, backend_name: str, refresh: bool = False) -> Dict[str, Any]:
"""Return the configuration of the IBM backend.

Args:
Expand All @@ -305,7 +305,7 @@ def backend_configuration(self, backend_name: str) -> Dict[str, Any]:
Returns:
Backend configuration.
"""
if backend_name not in self._configuration_registry:
if backend_name not in self._configuration_registry or refresh:
self._configuration_registry[backend_name] = self._api.backend(
backend_name
).configuration()
Expand Down
12 changes: 12 additions & 0 deletions qiskit_ibm_runtime/ibm_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
from .utils.backend_decoder import (
defaults_from_server_data,
properties_from_server_data,
configuration_from_server_data,
)
from .utils.deprecation import issue_deprecation_msg
from .utils.options import QASM2Options, QASM3Options
Expand Down Expand Up @@ -357,6 +358,17 @@ def target_history(self, datetime: Optional[python_datetime] = None) -> Target:
self._convert_to_target(refresh=True)
return self._target

def refresh(self) -> None:
"""Retrieve the newest backend configuration and refresh the current backend target."""
if config := configuration_from_server_data(
raw_config=self._service._api_client.backend_configuration(self.name, refresh=True),
instance=self._instance,
):
self._configuration = config
self._get_properties(datetime=python_datetime.now())
self._get_defaults()
self._convert_to_target(refresh=True)

def properties(
self, refresh: bool = False, datetime: Optional[python_datetime] = None
) -> Optional[BackendProperties]:
Expand Down
2 changes: 2 additions & 0 deletions release-notes/unreleased/1955.feat.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added a new method, ``backend.refresh()`` that refreshes the
current backend target with the latest updates from the server.
10 changes: 10 additions & 0 deletions test/integration/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ def test_backend_target_history(self):
self.assertIsNotNone(backend.target_history())
self.assertIsNotNone(backend.target_history(datetime=datetime.now() - timedelta(30)))

@production_only
def test_backend_target_refresh(self):
"""Test refreshing the backend target."""
backend = self.backend
with self.subTest(backend=backend.name):
old_target = backend.target
backend.refresh()
new_target = backend.target
self.assertNotEqual(old_target, new_target)

def test_backend_max_circuits(self):
"""Check if the max_circuits property is set."""
backend = self.backend
Expand Down