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

Backend required param when using cloud channel #1739

Merged
merged 4 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
33 changes: 3 additions & 30 deletions qiskit_ibm_runtime/base_primitive.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,7 @@ def __init__(
name=self._mode.backend(), instance=self._mode._instance
)
else:
self._service = (
QiskitRuntimeService()
if QiskitRuntimeService.global_service is None
else QiskitRuntimeService.global_service
)
if self._service.channel != "ibm_cloud":
raise ValueError(
"A backend or session must be specified when not using ibm_cloud channel."
)
issue_deprecation_msg(
"Not providing a backend is deprecated",
"0.22.0",
"Passing in a backend will be required, please provide a backend.",
3,
)
raise ValueError("A backend or session must be specified.")

def _run(self, pubs: Union[list[EstimatorPub], list[SamplerPub]]) -> RuntimeJobV2:
"""Run the primitive.
Expand Down Expand Up @@ -332,21 +318,8 @@ def __init__(
name=self._session.backend(), instance=self._session._instance
)
else:
self._service = (
QiskitRuntimeService()
if QiskitRuntimeService.global_service is None
else QiskitRuntimeService.global_service
)
if self._service.channel != "ibm_cloud":
raise ValueError(
"A backend or session must be specified when not using ibm_cloud channel."
)
issue_deprecation_msg(
"Not providing a backend is deprecated",
"0.21.0",
"Passing in a backend will be required, please provide a backend.",
3,
)
raise ValueError("A backend or session must be specified.")

# Check if initialized within a IBMBackend session. If so, issue a warning.
if get_cm_provider_session():
warnings.warn(
Expand Down
8 changes: 1 addition & 7 deletions qiskit_ibm_runtime/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,7 @@ def __init__(
if isinstance(backend, str):
self._backend = self._service.backend(backend)
elif backend is None:
if self._service.channel == "ibm_quantum":
raise ValueError('"backend" is required for ``ibm_quantum`` channel.')
issue_deprecation_msg(
"Not providing a backend is deprecated",
"0.21.0",
"Passing in a backend will be required, please provide a backend.",
)
raise ValueError('"backend" is required')
else:
raise ValueError(f"Invalid backend type {type(backend)}")

Expand Down
29 changes: 0 additions & 29 deletions test/unit/test_ibm_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,35 +195,6 @@ def test_init_with_backend_session(self):
inst.run(**get_primitive_inputs(inst, backend=backend))
session.run.assert_called_once()

def test_init_with_no_backend_session_cloud(self):
"""Test initializing a primitive without backend or session for cloud channel."""
primitives = [Sampler, Estimator]

for cls in primitives:
with self.subTest(primitive=cls), patch(
"qiskit_ibm_runtime.base_primitive.QiskitRuntimeService"
) as mock_service:
mock_service_inst = MagicMock()
mock_service_inst.channel = "ibm_cloud"
mock_service.return_value = mock_service_inst
mock_service.reset_mock()
mock_service.global_service = None
inst = cls()
mock_service.assert_called_once()
self.assertIsNone(inst.session)

def test_init_with_no_backend_session_quantum(self):
"""Test initializing a primitive without backend or session for quantum channel."""
primitives = [Sampler, Estimator]

for cls in primitives:
with self.subTest(primitive=cls), patch(
"qiskit_ibm_runtime.base_primitive.QiskitRuntimeService"
) as mock_service:
mock_service.reset_mock()
with self.assertRaises(ValueError):
_ = cls()

def test_default_session_context_manager(self):
"""Test getting default session within context manager."""
# service = MagicMock()
Expand Down
22 changes: 0 additions & 22 deletions test/unit/test_ibm_primitives_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,28 +195,6 @@ def test_init_with_backend_session(self, primitive):
inst.run(**get_primitive_inputs(inst))
session.run.assert_called_once()

@data(EstimatorV2, SamplerV2)
def test_init_with_no_backend_session_cloud(self, primitive):
"""Test initializing a primitive without backend or session for cloud channel."""
with patch("qiskit_ibm_runtime.base_primitive.QiskitRuntimeService") as mock_service:
mock_service_inst = MagicMock()
mock_service_inst.channel = "ibm_cloud"
mock_service.return_value = mock_service_inst
mock_service.reset_mock()
mock_service.global_service = None
inst = primitive()
mock_service.assert_called_once()
self.assertIsNone(inst.mode)

@data(EstimatorV2, SamplerV2)
def test_init_with_no_backend_session_quantum(self, primitive):
"""Test initializing a primitive without backend or session for quantum channel."""

with patch("qiskit_ibm_runtime.base_primitive.QiskitRuntimeService") as mock_service:
mock_service.reset_mock()
with self.assertRaises(ValueError):
_ = primitive()

@data(EstimatorV2, SamplerV2)
def test_default_session_context_manager(self, primitive):
"""Test getting default session within context manager."""
Expand Down
44 changes: 0 additions & 44 deletions test/unit/test_runtime_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,12 @@
import time
from unittest.mock import MagicMock

from qiskit.quantum_info import SparsePauliOp
from qiskit_ibm_runtime import (
RuntimeJob,
QiskitRuntimeService,
Sampler,
Estimator,
Session,
Options,
)
from qiskit_ibm_runtime.api.client_parameters import ClientParameters
from qiskit_ibm_runtime.exceptions import RuntimeInvalidStateError
from qiskit_ibm_runtime.ibm_backend import IBMBackend

from ..utils import bell
from .mock.fake_runtime_client import BaseFakeRuntimeClient
from .mock.ws_handler import (
websocket_handler,
Expand Down Expand Up @@ -75,42 +67,6 @@ def result_callback(job_id, interim_result):
self.assertEqual(JOB_PROGRESS_RESULT_COUNT, len(results))
self.assertFalse(job._ws_client.connected)

def test_primitive_interim_result_callback(self):
"""Test primitive interim result callback."""

def result_callback(job_id, interim_result):
nonlocal results
results.append(interim_result)
self.assertEqual(JOB_ID_PROGRESS_DONE, job_id)

def _patched_run(callback, *args, **kwargs): # pylint: disable=unused-argument
backend = MagicMock(spec=IBMBackend)
backend.name = None
return self._get_job(callback=callback, backend=backend)

service = MagicMock(spec=QiskitRuntimeService)
service.run = _patched_run
service._channel_strategy = None
service._api_client = MagicMock()

circ = bell()
obs = SparsePauliOp.from_list([("IZ", 1)])
primitives = [Sampler, Estimator]
sub_tests = [
({"options": Options(environment={"callback": result_callback})}, {}),
({}, {"callback": result_callback}),
]

for cls in primitives:
for options, callback in sub_tests:
with self.subTest(primitive=cls, options=options, callback=callback):
results = []
inst = cls(session=Session(service=service), **options)
job = inst.run(circ, observables=obs, **callback)
time.sleep(JOB_PROGRESS_RESULT_COUNT + 2)
self.assertEqual(JOB_PROGRESS_RESULT_COUNT, len(results))
self.assertFalse(job._ws_client.connected)

def test_stream_results(self):
"""Test streaming results."""

Expand Down
19 changes: 2 additions & 17 deletions test/unit/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from unittest.mock import MagicMock, patch

from qiskit_ibm_runtime.fake_provider import FakeManila
from qiskit_ibm_runtime import Session, QiskitRuntimeService
from qiskit_ibm_runtime import Session
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
Expand Down Expand Up @@ -47,11 +47,8 @@ def test_missing_backend(self):
with self.assertRaises(ValueError):
Session(service=service)

def test_missing_backend_cloud_warning(self):
"""Test warning if no backend provided on cloud channel."""
service = MagicMock()
service.channel = "ibm_cloud"
with self.assertWarns(DeprecationWarning):
with self.assertRaises(ValueError):
Session(service=service)

def test_passing_ibm_backend(self):
Expand Down Expand Up @@ -134,18 +131,6 @@ def test_context_manager(self):
session.cancel()
self.assertFalse(session._active)

def test_default_backend(self):
"""Test default backend set."""
job = MagicMock()
job.backend().name.return_value = "ibm_gotham" # pylint: disable=no-value-for-parameter
service = MagicMock(spec=QiskitRuntimeService)
service.run.return_value = job
service.channel = "ibm_cloud"

session = Session(service=service)
session.run(program_id="foo", inputs={})
self.assertEqual(session.backend(), "ibm_gotham")

def test_global_service(self):
"""Test that global service is used in Session"""
_ = FakeRuntimeService(channel="ibm_quantum", token="abc")
Expand Down