Skip to content

Commit d4bd788

Browse files
kt474ElePT
andauthored
Don't re-retrieve backend in session cm (#2282)
* don't reretrieve backend in session cm * add release note * Update release-notes/unreleased/2282.bug.rst Co-authored-by: Elena Peña Tapia <57907331+ElePT@users.noreply.github.com> * add test --------- Co-authored-by: Elena Peña Tapia <57907331+ElePT@users.noreply.github.com>
1 parent 605448b commit d4bd788

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

qiskit_ibm_runtime/base_primitive.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,10 @@ def _get_mode_service_backend(mode: Optional[Union[BackendV2, Session, Batch]] =
8686
raise ValueError("mode must be of type Backend, Session, Batch or None")
8787
elif get_cm_session():
8888
mode = get_cm_session()
89-
service = mode.service # type: ignore
90-
try:
91-
backend = service.backend(
92-
name=mode.backend(), # type: ignore
93-
instance=mode._instance, # type: ignore
94-
use_fractional_gates=mode._backend.options.use_fractional_gates, # type: ignore
95-
)
96-
except (AttributeError, TypeError):
97-
backend = service.backend(
98-
name=mode.backend(), # type: ignore
99-
instance=mode._instance, # type: ignore
100-
)
101-
return mode, service, backend # type: ignore
89+
service = mode.service
90+
backend = mode._backend
91+
92+
return mode, service, backend
10293
else:
10394
raise ValueError("A backend or session must be specified.")
10495

release-notes/unreleased/2282.bug.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Fixed a bug in :class:`.BasePrimitive` where primitives instantiated inside a :class:`~.Session` or :class:`~.Batch` context manager without the ``mode``
2+
parameter would fetch the backend from the service (by name) instead of using the backend passed to the :class:`~.Session` or :class:`~.Batch`.
3+
This could cause issues when the :class:`~.Session`/:class:`~.Batch`
4+
backend was modified by users (for example, by removing a gate), because the primitives
5+
would instead fetch the unmodified backend object from the service. After the fix, the
6+
:class:`~.Session`/:class:`~.Batch` backend object is used directly.

test/integration/test_session.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from qiskit.circuit.library import RealAmplitudes
1818
from qiskit.quantum_info import SparsePauliOp
1919

20+
from qiskit.circuit import IfElseOp
2021
from qiskit.primitives import PrimitiveResult
2122
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
2223

@@ -98,3 +99,21 @@ def test_session_from_id_no_backend(self, service):
9899
if session.details().get("backend_name") == "":
99100
with self.assertRaises(IBMRuntimeError):
100101
Session.from_id(session_id=session._session_id, service=service)
102+
103+
@run_integration_test
104+
def test_session_backend(self, service):
105+
"""Test session backend is the correct backend."""
106+
backend = service.backend(self.dependencies.qpu)
107+
108+
pm = generate_preset_pass_manager(optimization_level=1, target=backend.target)
109+
instruction_name = "test_name"
110+
backend.target.add_instruction(IfElseOp, name=instruction_name)
111+
112+
with Session(backend=backend) as session:
113+
sampler = SamplerV2(mode=session)
114+
job = sampler.run([pm.run(bell())])
115+
self.assertIn(instruction_name, job.backend().target.operation_names)
116+
117+
sampler2 = SamplerV2()
118+
job2 = sampler2.run([pm.run(bell())])
119+
self.assertIn(instruction_name, job2.backend().target.operation_names)

0 commit comments

Comments
 (0)