From 86bf0360370f78b8404bf576f05ff7916b924e43 Mon Sep 17 00:00:00 2001 From: Rathish Cholarajan Date: Mon, 20 Jun 2022 14:56:54 -0400 Subject: [PATCH] Revert "Revert "(fix): deprecate class qiskit_ibm_runtime.IBMSampler for the next release (#342)" (#365)" (#385) This reverts commit d69fe47166ab2e6fcdba0ea2ef3477f717ddefb0. --- qiskit_ibm_runtime/__init__.py | 2 - qiskit_ibm_runtime/ibm_sampler.py | 69 ------------ .../deprecate-classes-ef077e50db9718bc.yaml | 2 +- .../remove-ibm-sampler-7779165095fd2a5f.yaml | 6 + test/integration/test_ibm_sampler.py | 104 ------------------ 5 files changed, 7 insertions(+), 176 deletions(-) delete mode 100644 qiskit_ibm_runtime/ibm_sampler.py create mode 100644 releasenotes/notes/remove-ibm-sampler-7779165095fd2a5f.yaml delete mode 100644 test/integration/test_ibm_sampler.py diff --git a/qiskit_ibm_runtime/__init__.py b/qiskit_ibm_runtime/__init__.py index 3acd15395..c5b084b63 100644 --- a/qiskit_ibm_runtime/__init__.py +++ b/qiskit_ibm_runtime/__init__.py @@ -228,7 +228,6 @@ def result_callback(job_id, result): RuntimeDecoder IBMRuntimeService IBMEstimator - IBMSampler """ import logging @@ -245,7 +244,6 @@ def result_callback(job_id, result): from .version import __version__ from .ibm_estimator import IBMEstimator -from .ibm_sampler import IBMSampler from .estimator import Estimator from .sampler import Sampler diff --git a/qiskit_ibm_runtime/ibm_sampler.py b/qiskit_ibm_runtime/ibm_sampler.py deleted file mode 100644 index df367dd32..000000000 --- a/qiskit_ibm_runtime/ibm_sampler.py +++ /dev/null @@ -1,69 +0,0 @@ -# This code is part of Qiskit. -# -# (C) Copyright IBM 2022. -# -# This code is licensed under the Apache License, Version 2.0. You may -# obtain a copy of this license in the LICENSE.txt file in the root directory -# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. -# -# Any modifications or derivative works of this code must retain this -# copyright notice, and modified files need to carry a notice indicating -# that they have been altered from the originals. - -"""Qiskit Runtime Sampler primitive service.""" - -import warnings - -from typing import Any, Optional, Iterable, Union - -from qiskit.circuit import QuantumCircuit, Parameter - -from .base_primitive import BasePrimitive -from .sampler import Sampler - - -class IBMSampler(BasePrimitive): - """Deprecated, use :class:`~qiskit_ibm_runtime.Sampler` instead.""" - - def __init__(self, *args: Any, **kwargs: Any): - """Initalizes IBMSampler.""" - super().__init__(*args, **kwargs) - warnings.warn( - "IBMSampler class is deprecated and will " - "be removed in a future release. " - "You can now use qiskit_ibm_runtime.Sampler class instead.", - DeprecationWarning, - stacklevel=2, - ) - - def __call__( # type: ignore[override] - self, - circuits: Union[QuantumCircuit, Iterable[QuantumCircuit]], - parameters: Optional[Iterable[Iterable[Parameter]]] = None, - skip_transpilation: bool = False, - ) -> Sampler: - """Initializes the Sampler primitive. - - Args: - circuits: a (parameterized) :class:`~qiskit.circuit.QuantumCircuit` or - a list of (parameterized) :class:`~qiskit.circuit.QuantumCircuit`. - parameters: A list of parameters of the quantum circuits - (:class:`~qiskit.circuit.parametertable.ParameterView` or - a list of :class:`~qiskit.circuit.Parameter`). - skip_transpilation: Transpilation is skipped if set to True. - False by default. - - Returns: - An instance of :class:`qiskit_ibm_runtime.sampler.Sampler`. - """ - # pylint: disable=arguments-differ - options = None - if self._backend: - options = {"backend": self._backend} - return Sampler( - circuits=circuits, - parameters=parameters, - skip_transpilation=skip_transpilation, - service=self._service, - options=options, - ) diff --git a/releasenotes/notes/0.4/deprecate-classes-ef077e50db9718bc.yaml b/releasenotes/notes/0.4/deprecate-classes-ef077e50db9718bc.yaml index 5a6e14a91..59826d1d4 100644 --- a/releasenotes/notes/0.4/deprecate-classes-ef077e50db9718bc.yaml +++ b/releasenotes/notes/0.4/deprecate-classes-ef077e50db9718bc.yaml @@ -82,4 +82,4 @@ deprecations: and :class:`~qiskit_ibm_runtime.IBMSampler` classes have been deprecated and will be removed in a future release. Use :class:`~qiskit_ibm_runtime.QiskitRuntimeService`, :class:`~qiskit_ibm_runtime.Estimator` and :class:`~qiskit_ibm_runtime.Sampler` classes - instead. See upgrade notes section for a detailed explanation with examples. + instead. See upgrade notes section for a detailed explanation with examples. \ No newline at end of file diff --git a/releasenotes/notes/remove-ibm-sampler-7779165095fd2a5f.yaml b/releasenotes/notes/remove-ibm-sampler-7779165095fd2a5f.yaml new file mode 100644 index 000000000..4daf46641 --- /dev/null +++ b/releasenotes/notes/remove-ibm-sampler-7779165095fd2a5f.yaml @@ -0,0 +1,6 @@ +--- +upgrade: + - | + ``IBMSampler`` class which was deprecated earlier is now removed. Use + :class:`~qiskit_ibm_runtime.Sampler` class going forward. + diff --git a/test/integration/test_ibm_sampler.py b/test/integration/test_ibm_sampler.py deleted file mode 100644 index 056924091..000000000 --- a/test/integration/test_ibm_sampler.py +++ /dev/null @@ -1,104 +0,0 @@ -# This code is part of Qiskit. -# -# (C) Copyright IBM 2022. -# -# This code is licensed under the Apache License, Version 2.0. You may -# obtain a copy of this license in the LICENSE.txt file in the root directory -# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. -# -# Any modifications or derivative works of this code must retain this -# copyright notice, and modified files need to carry a notice indicating -# that they have been altered from the originals. - -"""Integration tests for Sampler primitive.""" - -from qiskit.circuit import QuantumCircuit -from qiskit.circuit.library import RealAmplitudes - -from qiskit_ibm_runtime import IBMSampler, BaseSampler, SamplerResult - -from ..decorators import run_integration_test -from ..ibm_test_case import IBMIntegrationTestCase - -# TODO IBMSampler class had been deprecated, remove this file when removing IBMSampler - - -class TestIntegrationIBMSampler(IBMIntegrationTestCase): - """Integration tests for IBMSampler primitive.""" - - @run_integration_test - def test_ibm_sampler_primitive_non_parameterized_circuits(self, service): - """Verify if sampler primitive returns expected results for non-parameterized circuits.""" - - sampler_factory = IBMSampler(service=service, backend="ibmq_qasm_simulator") - - bell = QuantumCircuit(2) - bell.h(0) - bell.cx(0, 1) - bell.measure_all() - - # executes a Bell circuit - with sampler_factory(circuits=bell) as sampler: - self.assertIsInstance(sampler, BaseSampler) - - circuit_indices = [0] - result = sampler(circuit_indices=circuit_indices, parameter_values=[[]]) - self.assertIsInstance(result, SamplerResult) - self.assertEqual(len(result.quasi_dists), len(circuit_indices)) - self.assertEqual(len(result.metadata), len(circuit_indices)) - - # executes three Bell circuits - with sampler_factory([bell] * 3) as sampler: - self.assertIsInstance(sampler, BaseSampler) - - circuit_indices1 = [0, 1, 2] - result1 = sampler( - circuit_indices=circuit_indices1, parameter_values=[[]] * 3 - ) - self.assertIsInstance(result1, SamplerResult) - self.assertEqual(len(result1.quasi_dists), len(circuit_indices1)) - self.assertEqual(len(result1.metadata), len(circuit_indices1)) - - circuit_indices2 = [0, 2] - result2 = sampler( - circuit_indices=circuit_indices2, parameter_values=[[]] * 2 - ) - self.assertIsInstance(result2, SamplerResult) - self.assertEqual(len(result2.quasi_dists), len(circuit_indices2)) - self.assertEqual(len(result2.metadata), len(circuit_indices2)) - - circuit_indices3 = [1, 2] - result3 = sampler( - circuit_indices=circuit_indices3, parameter_values=[[]] * 2 - ) - self.assertIsInstance(result3, SamplerResult) - self.assertEqual(len(result3.quasi_dists), len(circuit_indices3)) - self.assertEqual(len(result3.metadata), len(circuit_indices3)) - - @run_integration_test - def test_ibm_sampler_primitive_parameterized_circuits(self, service): - """Verify if sampler primitive returns expected results for parameterized circuits.""" - - sampler_factory = IBMSampler(service=service, backend="ibmq_qasm_simulator") - - # parameterized circuit - pqc = RealAmplitudes(num_qubits=2, reps=2) - pqc.measure_all() - pqc2 = RealAmplitudes(num_qubits=2, reps=3) - pqc2.measure_all() - - theta1 = [0, 1, 1, 2, 3, 5] - theta2 = [1, 2, 3, 4, 5, 6] - theta3 = [0, 1, 2, 3, 4, 5, 6, 7] - - with sampler_factory(circuits=[pqc, pqc2]) as sampler: - self.assertIsInstance(sampler, BaseSampler) - - circuit_indices = [0, 0, 1] - result = sampler( - circuit_indices=circuit_indices, - parameter_values=[theta1, theta2, theta3], - ) - self.assertIsInstance(result, SamplerResult) - self.assertEqual(len(result.quasi_dists), len(circuit_indices)) - self.assertEqual(len(result.metadata), len(circuit_indices))