Skip to content
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

Fix missing fractional gates in primitive in session #1922

Merged
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
12 changes: 11 additions & 1 deletion qiskit_ibm_runtime/base_primitive.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,17 @@ def _get_mode_service_backend(
elif get_cm_session():
mode = get_cm_session()
service = mode.service # type: ignore
backend = service.backend(name=mode.backend(), instance=mode._instance) # 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
else:
raise ValueError("A backend or session must be specified.")
Expand Down
1 change: 1 addition & 0 deletions release-notes/unreleased/1922.but.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug that we cannot run primitives in the session context with fractional gates.
15 changes: 14 additions & 1 deletion test/unit/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
from unittest.mock import MagicMock, patch

from qiskit_ibm_runtime.fake_provider import FakeManila
from qiskit_ibm_runtime import Session
from qiskit_ibm_runtime import Session, SamplerV2
from qiskit_ibm_runtime.ibm_backend import IBMBackend
from qiskit_ibm_runtime.exceptions import IBMRuntimeError
from qiskit_ibm_runtime.utils.default_session import _DEFAULT_SESSION

from .mock.fake_runtime_service import FakeRuntimeService
from .mock.fake_api_backend import FakeApiBackendSpecs
from ..ibm_test_case import IBMTestCase
from ..utils import get_mocked_backend

Expand Down Expand Up @@ -160,3 +161,15 @@ def test_correct_execution_mode(self):
_ = FakeRuntimeService(channel="ibm_quantum", token="abc")
session = Session(backend="common_backend")
self.assertEqual(session.details()["mode"], "dedicated")

def test_cm_session_fractional(self):
"""Test instantiating primitive inside session context manager with the fractional optin."""
service = FakeRuntimeService(
channel="ibm_quantum",
token="abc",
backend_specs=[FakeApiBackendSpecs(backend_name="FakeFractionalBackend")],
)
backend = service.backend("fake_fractional", use_fractional_gates=True)
with Session(backend=backend) as _:
primitive = SamplerV2()
self.assertTrue(primitive._backend.options.use_fractional_gates)