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

Set _active flag to false if session already closed #1780

Merged
merged 4 commits into from
Jul 3, 2024
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
3 changes: 3 additions & 0 deletions qiskit_ibm_runtime/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,13 +369,16 @@ def from_id(
response = service._api_client.session_details(session_id)
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()
if mode != class_name:
raise IBMInputValueError(
f"Input ID {session_id} has execution mode {mode} instead of {class_name}."
)

session = cls(service, backend)
if state == "closed":
session._active = False
session._session_id = session_id
return session

Expand Down
2 changes: 2 additions & 0 deletions release-notes/unreleased/1780.feat.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
If jobs are run in a session created with :meth:`QiskitRuntimeService.Session.from_id` where the
session is already closed, the jobs are rejected immediately.
3 changes: 3 additions & 0 deletions test/integration/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ def test_session_from_id(self, service):

new_session = Session.from_id(session_id=session._session_id, service=service)
self.assertEqual(session._session_id, new_session._session_id)
self.assertTrue(new_session._active)
new_session.close()
self.assertFalse(new_session._active)

with self.assertRaises(IBMInputValueError):
Batch.from_id(session_id=session._session_id, service=service)