From 4d5fedb97fcadb526cf0a2c7b019bdd840101619 Mon Sep 17 00:00:00 2001 From: kevin-tian Date: Mon, 10 Jun 2024 14:38:46 -0400 Subject: [PATCH 1/3] Backend required param when using cloud channel --- qiskit_ibm_runtime/base_primitive.py | 33 +++------------------------- qiskit_ibm_runtime/session.py | 8 +------ test/unit/test_session.py | 5 +---- 3 files changed, 5 insertions(+), 41 deletions(-) diff --git a/qiskit_ibm_runtime/base_primitive.py b/qiskit_ibm_runtime/base_primitive.py index e61b5d683..a518c5ba0 100644 --- a/qiskit_ibm_runtime/base_primitive.py +++ b/qiskit_ibm_runtime/base_primitive.py @@ -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. @@ -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( diff --git a/qiskit_ibm_runtime/session.py b/qiskit_ibm_runtime/session.py index aa93ea74b..29bad2357 100644 --- a/qiskit_ibm_runtime/session.py +++ b/qiskit_ibm_runtime/session.py @@ -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)}") diff --git a/test/unit/test_session.py b/test/unit/test_session.py index 9079302a8..ab0088b47 100644 --- a/test/unit/test_session.py +++ b/test/unit/test_session.py @@ -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): From 8bb329ea3c5964bea33150b0ed58cb379388c5c4 Mon Sep 17 00:00:00 2001 From: kevin-tian Date: Mon, 10 Jun 2024 15:51:00 -0400 Subject: [PATCH 2/3] Remove unecessary unit tests --- test/unit/test_ibm_primitives.py | 29 ------------------- test/unit/test_ibm_primitives_v2.py | 22 --------------- test/unit/test_runtime_ws.py | 44 ----------------------------- test/unit/test_session.py | 14 +-------- 4 files changed, 1 insertion(+), 108 deletions(-) diff --git a/test/unit/test_ibm_primitives.py b/test/unit/test_ibm_primitives.py index 4ab9a765f..10d96f985 100644 --- a/test/unit/test_ibm_primitives.py +++ b/test/unit/test_ibm_primitives.py @@ -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() diff --git a/test/unit/test_ibm_primitives_v2.py b/test/unit/test_ibm_primitives_v2.py index cb691c777..8c73d9e91 100644 --- a/test/unit/test_ibm_primitives_v2.py +++ b/test/unit/test_ibm_primitives_v2.py @@ -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.""" diff --git a/test/unit/test_runtime_ws.py b/test/unit/test_runtime_ws.py index 0b9dab26f..277d0a653 100644 --- a/test/unit/test_runtime_ws.py +++ b/test/unit/test_runtime_ws.py @@ -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, @@ -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.""" diff --git a/test/unit/test_session.py b/test/unit/test_session.py index ab0088b47..0b956187a 100644 --- a/test/unit/test_session.py +++ b/test/unit/test_session.py @@ -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 @@ -131,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") From ce0960fdc605635f2984cde5dbb9967d0b863bd9 Mon Sep 17 00:00:00 2001 From: kevin-tian Date: Thu, 13 Jun 2024 09:37:17 -0400 Subject: [PATCH 3/3] Remove integration test --- test/integration/test_job.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/test/integration/test_job.py b/test/integration/test_job.py index 8fa108619..81bd42a0c 100644 --- a/test/integration/test_job.py +++ b/test/integration/test_job.py @@ -46,17 +46,6 @@ def test_run_program(self, service): self.assertEqual(JobStatus.DONE, job.status()) self.assertTrue(job.result()) - @run_integration_test - @production_only - def test_run_program_cloud_no_backend(self, service): - """Test running a cloud program with no backend.""" - - if self.dependencies.channel == "ibm_quantum": - self.skipTest("Not supported on ibm_quantum") - - job = self._run_program(service, backend="") - self.assertTrue(job.backend(), f"Job {job.job_id()} has no backend.") - @run_integration_test @quantum_only def test_run_program_log_level(self, service):