From 6302fe587e2a2192d8e64953f707e4039c582857 Mon Sep 17 00:00:00 2001 From: kevin-tian Date: Thu, 5 Jun 2025 16:19:28 -0400 Subject: [PATCH 1/4] don't reretrieve backend in session cm --- qiskit_ibm_runtime/base_primitive.py | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/qiskit_ibm_runtime/base_primitive.py b/qiskit_ibm_runtime/base_primitive.py index f9fc3ff9c..d7bc6ad20 100644 --- a/qiskit_ibm_runtime/base_primitive.py +++ b/qiskit_ibm_runtime/base_primitive.py @@ -86,19 +86,10 @@ def _get_mode_service_backend(mode: Optional[Union[BackendV2, Session, Batch]] = raise ValueError("mode must be of type Backend, Session, Batch or None") elif get_cm_session(): mode = get_cm_session() - service = mode.service # type: ignore - try: - backend = service.backend( - name=mode.backend(), # type: ignore - instance=mode._instance, # type: ignore - use_fractional_gates=mode._backend.options.use_fractional_gates, # type: ignore - ) - except (AttributeError, TypeError): - backend = service.backend( - name=mode.backend(), # type: ignore - instance=mode._instance, # type: ignore - ) - return mode, service, backend # type: ignore + service = mode.service + backend = mode._backend + + return mode, service, backend else: raise ValueError("A backend or session must be specified.") From fd5e4bd7f74c2e197d55567e0c98ed39e5baf218 Mon Sep 17 00:00:00 2001 From: kevin-tian Date: Thu, 5 Jun 2025 16:45:12 -0400 Subject: [PATCH 2/4] add release note --- release-notes/unreleased/2282.bug.rst | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 release-notes/unreleased/2282.bug.rst diff --git a/release-notes/unreleased/2282.bug.rst b/release-notes/unreleased/2282.bug.rst new file mode 100644 index 000000000..35e45f052 --- /dev/null +++ b/release-notes/unreleased/2282.bug.rst @@ -0,0 +1,4 @@ +Running jobs in a :class:`~.Session` or :class:`~.Batch` with +a context manager and initializing a primitive without any parameters will reinitialize the +Session/Batch backend. This could cause issues if the backend target has been modified so instead, +the actual backend object is now used. From ad570d95c0c559ddff9fc420e65f413c35fca0d5 Mon Sep 17 00:00:00 2001 From: Kevin Tian Date: Thu, 12 Jun 2025 12:57:13 -0400 Subject: [PATCH 3/4] Update release-notes/unreleased/2282.bug.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Elena Peña Tapia <57907331+ElePT@users.noreply.github.com> --- release-notes/unreleased/2282.bug.rst | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/release-notes/unreleased/2282.bug.rst b/release-notes/unreleased/2282.bug.rst index 35e45f052..30c32121b 100644 --- a/release-notes/unreleased/2282.bug.rst +++ b/release-notes/unreleased/2282.bug.rst @@ -1,4 +1,5 @@ -Running jobs in a :class:`~.Session` or :class:`~.Batch` with -a context manager and initializing a primitive without any parameters will reinitialize the -Session/Batch backend. This could cause issues if the backend target has been modified so instead, -the actual backend object is now used. +Fixed a bug in :class:`.BasePrimitive` where primitives instantiated inside a :class:`~.Session` or :class:`~.Batch` context manager would fetch the backend +from the service (by name) instead of using the backend passed to the :class:`~.Session` or :class:`~.Batch`. This could cause issues when the :class:`~.Session`/:class:`~.Batch` +backend was modified by users (for example, by removing a gate), because the primitives +would instead fetch the unmodified backend object from the service. After the fix, the +:class:`~.Session`/:class:`~.Batch` backend object is used directly. From f4b79655df3a8512e6d731c1f6cdb01305ee7598 Mon Sep 17 00:00:00 2001 From: kevin-tian Date: Thu, 12 Jun 2025 13:21:00 -0400 Subject: [PATCH 4/4] add test --- release-notes/unreleased/2282.bug.rst | 5 +++-- test/integration/test_session.py | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/release-notes/unreleased/2282.bug.rst b/release-notes/unreleased/2282.bug.rst index 30c32121b..4c04bdd46 100644 --- a/release-notes/unreleased/2282.bug.rst +++ b/release-notes/unreleased/2282.bug.rst @@ -1,5 +1,6 @@ -Fixed a bug in :class:`.BasePrimitive` where primitives instantiated inside a :class:`~.Session` or :class:`~.Batch` context manager would fetch the backend -from the service (by name) instead of using the backend passed to the :class:`~.Session` or :class:`~.Batch`. This could cause issues when the :class:`~.Session`/:class:`~.Batch` +Fixed a bug in :class:`.BasePrimitive` where primitives instantiated inside a :class:`~.Session` or :class:`~.Batch` context manager without the ``mode`` +parameter would fetch the backend from the service (by name) instead of using the backend passed to the :class:`~.Session` or :class:`~.Batch`. +This could cause issues when the :class:`~.Session`/:class:`~.Batch` backend was modified by users (for example, by removing a gate), because the primitives would instead fetch the unmodified backend object from the service. After the fix, the :class:`~.Session`/:class:`~.Batch` backend object is used directly. diff --git a/test/integration/test_session.py b/test/integration/test_session.py index 19dd75e99..32747fe31 100644 --- a/test/integration/test_session.py +++ b/test/integration/test_session.py @@ -17,6 +17,7 @@ from qiskit.circuit.library import RealAmplitudes from qiskit.quantum_info import SparsePauliOp +from qiskit.circuit import IfElseOp from qiskit.primitives import PrimitiveResult from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager @@ -111,3 +112,21 @@ def test_session_from_id_no_backend(self, service): if session.details().get("backend_name") == "": with self.assertRaises(IBMRuntimeError): Session.from_id(session_id=session._session_id, service=service) + + @run_integration_test + def test_session_backend(self, service): + """Test session backend is the correct backend.""" + backend = service.backend(self.dependencies.qpu) + + pm = generate_preset_pass_manager(optimization_level=1, target=backend.target) + instruction_name = "test_name" + backend.target.add_instruction(IfElseOp, name=instruction_name) + + with Session(backend=backend) as session: + sampler = SamplerV2(mode=session) + job = sampler.run([pm.run(bell())]) + self.assertIn(instruction_name, job.backend().target.operation_names) + + sampler2 = SamplerV2() + job2 = sampler2.run([pm.run(bell())]) + self.assertIn(instruction_name, job2.backend().target.operation_names)