From f2e24e49829360af4381cb0ffc46f62e343054fa Mon Sep 17 00:00:00 2001 From: Kevin Tian Date: Wed, 6 Dec 2023 10:28:13 -0500 Subject: [PATCH] Don't block for the first job in a session (#1170) * don't block if backend not selected * add reno * add test --- qiskit_ibm_runtime/session.py | 6 +++++- .../backend-blocking-job-70ebcf44855cbdfd.yaml | 8 ++++++++ test/integration/test_session.py | 13 +++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/backend-blocking-job-70ebcf44855cbdfd.yaml diff --git a/qiskit_ibm_runtime/session.py b/qiskit_ibm_runtime/session.py index 7b9905ac7..58b6eaa08 100644 --- a/qiskit_ibm_runtime/session.py +++ b/qiskit_ibm_runtime/session.py @@ -23,6 +23,7 @@ from .runtime_job import RuntimeJob from .utils.result_decoder import ResultDecoder from .ibm_backend import IBMBackend +from .exceptions import RuntimeJobTimeoutError from .utils.default_session import set_cm_session from .utils.deprecation import deprecate_arguments @@ -180,7 +181,10 @@ def run( self._setup_lock.release() if self._backend is None: - self._backend = job.backend().name + try: + self._backend = job.backend(0).name + except RuntimeJobTimeoutError: + self._backend = None return job diff --git a/releasenotes/notes/backend-blocking-job-70ebcf44855cbdfd.yaml b/releasenotes/notes/backend-blocking-job-70ebcf44855cbdfd.yaml new file mode 100644 index 000000000..c9cf202fb --- /dev/null +++ b/releasenotes/notes/backend-blocking-job-70ebcf44855cbdfd.yaml @@ -0,0 +1,8 @@ +--- +fixes: + - | + Fixed an issue where the first job in a cloud channel session without a backend passed + in would block other jobs from starting. This would happen because the first job would + attempt to retrieve the backend from the job which required the job to be in a + completed state. + diff --git a/test/integration/test_session.py b/test/integration/test_session.py index b8c086a59..4af012f8a 100644 --- a/test/integration/test_session.py +++ b/test/integration/test_session.py @@ -101,6 +101,19 @@ def test_session_from_id(self, service): job = sampler.run(ReferenceCircuits.bell(), shots=400) self.assertEqual(session_id, job.session_id) + @run_integration_test + def test_session_no_backend(self, service): + """Test session without passing in backend.""" + if self.dependencies.channel == "ibm_quantum": + self.skipTest("Not supported on ibm_quantum") + with Session(service) as session: + sampler = Sampler(session=session) + job1 = sampler.run(ReferenceCircuits.bell(), shots=400) + job2 = sampler.run(ReferenceCircuits.bell(), shots=400) + self.assertTrue(job1.backend()) + self.assertTrue(job2.backend()) + self.assertEqual(job1.backend().name, job2.backend().name) + class TestBackendRunInSession(IBMIntegrationTestCase): """Integration tests for Backend.run in Session."""