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

Use backend service in session if known #601

Merged
merged 2 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions qiskit_ibm_runtime/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ def __init__(
"""Session constructor.

Args:
service: Optional instance of the ``QiskitRuntimeService`` class,
defaults to ``QiskitRuntimeService()`` which tries to initialize
service: Optional instance of the ``QiskitRuntimeService`` class.
If ``None``, the service associated with the backend, if known, is used.
Otherwise ``QiskitRuntimeService()`` is used to initialize
your default saved account.
backend: Optional instance of :class:`qiskit_ibm_runtime.IBMBackend` class or
string name of backend. If not specified, a backend will be selected
Expand All @@ -90,7 +91,14 @@ def __init__(
ValueError: If an input value is invalid.
"""

self._service = service or QiskitRuntimeService()
if service is None:
self._service = (
backend.service
if isinstance(backend, IBMBackend)
else QiskitRuntimeService()
)
else:
self._service = service

if self._service.channel == "ibm_quantum" and not backend:
raise ValueError('"backend" is required for ``ibm_quantum`` channel.')
Expand Down
7 changes: 7 additions & 0 deletions releasenotes/notes/use-backend-service-020daf6e4a4d044e.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fixes:
- |
If a :class:`qiskit_ibm_runtime.IBMBackend` instance is passed to
the :class:`qiskit_ibm_runtime.Session` constructor, the service used
to initialize the ``IBMBackend`` instance is used for the session
instead of the default account service.
7 changes: 7 additions & 0 deletions test/unit/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ def test_passing_ibm_backend(self):
session = Session(service=MagicMock(), backend=backend)
self.assertEqual(session.backend(), "ibm_gotham")

def test_using_ibm_backend_service(self):
"""Test using service from an IBMBackend instance."""
backend = MagicMock(spec=IBMBackend)
backend.name = "ibm_gotham"
session = Session(backend=backend)
self.assertEqual(session.service, backend.service)

def test_max_time(self):
"""Test max time."""
max_times = [
Expand Down