From 040f8af7cf1f15fb8903f4a959525fd550b3d934 Mon Sep 17 00:00:00 2001 From: Kevin Tian Date: Wed, 3 Jul 2024 11:22:12 -0400 Subject: [PATCH] Set _active flag to false if session already closed (#1780) * Set _active flag to false if session already closed * release note * Add test --- qiskit_ibm_runtime/session.py | 3 +++ release-notes/unreleased/1780.feat.rst | 2 ++ test/integration/test_session.py | 3 +++ 3 files changed, 8 insertions(+) create mode 100644 release-notes/unreleased/1780.feat.rst diff --git a/qiskit_ibm_runtime/session.py b/qiskit_ibm_runtime/session.py index 29bad2357..fdc125183 100644 --- a/qiskit_ibm_runtime/session.py +++ b/qiskit_ibm_runtime/session.py @@ -369,6 +369,7 @@ 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( @@ -376,6 +377,8 @@ def from_id( ) session = cls(service, backend) + if state == "closed": + session._active = False session._session_id = session_id return session diff --git a/release-notes/unreleased/1780.feat.rst b/release-notes/unreleased/1780.feat.rst new file mode 100644 index 000000000..4ebc561a6 --- /dev/null +++ b/release-notes/unreleased/1780.feat.rst @@ -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. \ No newline at end of file diff --git a/test/integration/test_session.py b/test/integration/test_session.py index a0d99463c..8ffffe1d9 100644 --- a/test/integration/test_session.py +++ b/test/integration/test_session.py @@ -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)