Skip to content

Commit

Permalink
Removed support for backend as session parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
merav-aharoni committed Sep 19, 2023
1 parent 73e9949 commit 889e073
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 51 deletions.
50 changes: 5 additions & 45 deletions qiskit_ibm_runtime/base_primitive.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from .session import get_cm_session
from .constants import DEFAULT_DECODERS
from .qiskit_runtime_service import QiskitRuntimeService
from .utils.deprecation import issue_deprecation_msg

# pylint: disable=unused-import,cyclic-import
from .session import Session
Expand Down Expand Up @@ -70,7 +69,6 @@ def __init__(
# The base class, however, uses a `_run_options` which is an instance of
# qiskit.providers.Options. We largely ignore this _run_options because we use
# a nested dictionary to categorize options.

self._session: Optional[Session] = None
self._service: QiskitRuntimeService = None
self._backend: Optional[IBMBackend] = None
Expand All @@ -90,39 +88,11 @@ def __init__(
self._backend = self._service.backend(
name=self._session.backend(), instance=self._session._instance
)
elif isinstance(session, IBMBackend):
issue_deprecation_msg(
msg="Passing a backend instance as the ``session`` parameter is deprecated",
version="0.10.0",
remedy="Please pass it as the ``backend`` parameter instead.",
)
self._service = session.service
self._backend = session
elif isinstance(session, str):
issue_deprecation_msg(
msg="Passing a backend name as the ``session`` parameter is deprecated",
version="0.10.0",
remedy="Please pass it as the ``backend`` parameter instead.",
)
self._service = (
QiskitRuntimeService()
if QiskitRuntimeService.global_service is None
else QiskitRuntimeService.global_service
)
self._backend = self._service.backend(session)
elif isinstance(backend, Session):
issue_deprecation_msg(
msg="``session`` is no longer the first parameter when initializing "
"a Qiskit Runtime primitive",
version="0.10.0",
remedy="Please use ``session=session`` instead.",
)
self._session = backend
self._service = self._session.service
self._backend = self._service.backend(
name=self._session.backend(), instance=self._session._instance
)
elif isinstance(backend, IBMBackend):
return
elif session is not None:
raise ValueError("session must be of type Session or None")

if isinstance(backend, IBMBackend):
self._service = backend.service
self._backend = backend
elif isinstance(backend, str):
Expand All @@ -148,16 +118,6 @@ def __init__(
raise ValueError(
"A backend or session must be specified when not using ibm_cloud channel."
)
# self._first_run = True
# self._circuits_map = {}
# if self.circuits:
# for circuit in self.circuits:
# circuit_id = _hash(
# json.dumps(_circuit_key(circuit), cls=RuntimeEncoder)
# )
# if circuit_id not in self._session._circuits_map:
# self._circuits_map[circuit_id] = circuit
# self._session._circuits_map[circuit_id] = circuit

def _run_primitive(self, primitive_inputs: Dict, user_kwargs: Dict) -> RuntimeJob:
"""Run the primitive.
Expand Down
11 changes: 5 additions & 6 deletions test/unit/test_ibm_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,11 @@ def test_init_with_session_backend_str(self):
for cls in primitives:
with self.subTest(primitive=cls), patch(
"qiskit_ibm_runtime.base_primitive.QiskitRuntimeService"
) as mock_service:
with self.assertWarns(DeprecationWarning):
mock_service.reset_mock()
mock_service.global_service = None
):
with self.assertRaises(ValueError) as exc:
inst = cls(session=backend_name)
mock_service.assert_called_once()
self.assertIsNone(inst.session)
self.assertIn("session must be of type Session or None", str(exc.exception))

def test_init_with_backend_instance(self):
"""Test initializing a primitive with a backend instance."""
Expand All @@ -201,9 +199,10 @@ def test_init_with_backend_instance(self):
runtime_options = service.run.call_args.kwargs["options"]
self.assertEqual(runtime_options["backend"], backend.name)

with self.assertWarns(DeprecationWarning):
with self.assertRaises(ValueError) as exc:
inst = cls(session=backend)
self.assertIsNone(inst.session)
self.assertIn("session must be of type Session or None", str(exc.exception))

def test_init_with_backend_session(self):
"""Test initializing a primitive with both backend and session."""
Expand Down

0 comments on commit 889e073

Please sign in to comment.