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

Revert "(fix): deprecate class qiskit_ibm_runtime.IBMSampler for the next release" #365

Merged
merged 1 commit into from
Jun 8, 2022
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
2 changes: 2 additions & 0 deletions qiskit_ibm_runtime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ def result_callback(job_id, result):
RuntimeDecoder
IBMRuntimeService
IBMEstimator
IBMSampler
"""

import logging
Expand All @@ -244,6 +245,7 @@ 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

Expand Down
69 changes: 69 additions & 0 deletions qiskit_ibm_runtime/ibm_sampler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# 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,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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.
6 changes: 0 additions & 6 deletions releasenotes/notes/remove-ibm-sampler-7779165095fd2a5f.yaml

This file was deleted.

104 changes: 104 additions & 0 deletions test/integration/test_ibm_sampler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# 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))