Skip to content

Don't re-retrieve backend in session cm #2282

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

Merged
merged 5 commits into from
Jun 17, 2025
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
17 changes: 4 additions & 13 deletions qiskit_ibm_runtime/base_primitive.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand Down
6 changes: 6 additions & 0 deletions release-notes/unreleased/2282.bug.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
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.
19 changes: 19 additions & 0 deletions test/integration/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -98,3 +99,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)