Skip to content

Commit

Permalink
Don't create new session in from_id() (#1896)
Browse files Browse the repository at this point in the history
* Don't create new session in `from_id()`

* Add release note

* change new_session to private

* add unit test

* Use class variable

---------

Co-authored-by: ptristan3 <44805021+ptristan3@users.noreply.github.com>
  • Loading branch information
kt474 and ptristan3 committed Sep 4, 2024
1 parent 1d219a9 commit 3032b9a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
4 changes: 3 additions & 1 deletion qiskit_ibm_runtime/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ class Batch(Session):
<https://docs.quantum.ibm.com/guides/run-jobs-batch>`_" page.
"""

_create_new_session = True

def __init__(
self,
service: Optional[QiskitRuntimeService] = None,
Expand Down Expand Up @@ -131,7 +133,7 @@ def __init__(

def _create_session(self) -> Optional[str]:
"""Create a session."""
if isinstance(self._service, QiskitRuntimeService):
if isinstance(self._service, QiskitRuntimeService) and Batch._create_new_session:
session = self._service._api_client.create_session(
self.backend(), self._instance, self._max_time, self._service.channel, "batch"
)
Expand Down
9 changes: 6 additions & 3 deletions qiskit_ibm_runtime/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class Session:
print(f"Counts: {pub_result.data.cr.get_counts()}")
"""

_create_new_session = True

def __init__(
self,
service: Optional[QiskitRuntimeService] = None,
Expand All @@ -104,7 +106,6 @@ def __init__(
This value must be less than the
`system imposed maximum
<https://docs.quantum.ibm.com/guides/max-execution-time>`_.
Raises:
ValueError: If an input value is invalid.
"""
Expand Down Expand Up @@ -165,7 +166,7 @@ def __init__(

def _create_session(self) -> Optional[str]:
"""Create a session."""
if isinstance(self._service, QiskitRuntimeService):
if isinstance(self._service, QiskitRuntimeService) and Session._create_new_session:
session = self._service._api_client.create_session(
self.backend(), self._instance, self._max_time, self._service.channel, "dedicated"
)
Expand Down Expand Up @@ -367,7 +368,7 @@ def from_id(cls, session_id: str, service: QiskitRuntimeService) -> "Session":
"""

response = service._api_client.session_details(session_id)
backend = response.get("backend_name")
backend = service.backend(response.get("backend_name"))
mode = response.get("mode")
state = response.get("state")
class_name = "dedicated" if cls.__name__.lower() == "session" else cls.__name__.lower()
Expand All @@ -376,7 +377,9 @@ def from_id(cls, session_id: str, service: QiskitRuntimeService) -> "Session":
f"Input ID {session_id} has execution mode {mode} instead of {class_name}."
)

cls._create_new_session = False
session = cls(service, backend)
cls._create_new_session = True
if state == "closed":
session._active = False
session._session_id = session_id
Expand Down
2 changes: 2 additions & 0 deletions release-notes/unreleased/1896.bug.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed an issue where ``Session.from_id()`` would create
a new empty session.
2 changes: 2 additions & 0 deletions test/unit/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ def test_session_from_id(self):
session_id = "123"
session = Session.from_id(session_id=session_id, service=service)
session.run(program_id="foo", inputs={})
session._create_session = MagicMock()
self.assertTrue(session._create_session.assert_not_called)
self.assertEqual(session.session_id, session_id)

def test_correct_execution_mode(self):
Expand Down

0 comments on commit 3032b9a

Please sign in to comment.