Skip to content

Commit

Permalink
Log instance on initialization and when running a job (#1150)
Browse files Browse the repository at this point in the history
* log instances

* add _default_instance & fix lint

* add test

* change var name to current_instance
  • Loading branch information
kt474 authored Oct 17, 2023
1 parent d97d9ac commit 07652f1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions qiskit_ibm_runtime/qiskit_runtime_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ def __init__(
for backend_name in hgp.backends:
if backend_name not in self._backends:
self._backends[backend_name] = None
self._current_instance = self._account.instance
if not self._current_instance:
self._current_instance = self._get_hgp().name
logger.info("Default instance: %s", self._current_instance)
QiskitRuntimeService.global_service = self

# TODO - it'd be nice to allow some kind of autocomplete, but `service.ibmq_foo`
Expand Down Expand Up @@ -988,6 +992,9 @@ def run(
# Find the right hgp
hgp = self._get_hgp(instance=qrt_options.instance, backend_name=qrt_options.backend)
hgp_name = hgp.name
if hgp_name != self._current_instance:
self._current_instance = hgp_name
logger.info("Instance selected: %s", self._current_instance)
backend = self.backend(name=qrt_options.backend, instance=hgp_name)
status = backend.status()
if status.operational is True and status.status_msg != "active":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
features:
- |
At initialization, if not passed in directly, the default ``instance`` selected by the provider
will be logged at the "INFO" level. When running a job, if the backend selected is not in
the default instance but in a different instance the user also has access to, that instance
will also be logged.
10 changes: 10 additions & 0 deletions test/integration/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,13 @@ def test_resolve_crn_for_invalid_service_instance_name(self):
token=self.dependencies.token,
instance=service_instance_name,
)

def test_logging_instance_at_init(self):
"""Test instance is logged at initialization if instance not passed in."""
with self.assertLogs("qiskit_ibm_runtime", "INFO") as logs:
QiskitRuntimeService(
channel="ibm_quantum",
url=self.dependencies.url,
token=self.dependencies.token,
)
self.assertIn("instance", logs.output[0])

0 comments on commit 07652f1

Please sign in to comment.