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

Add support executing circuits with a SamplerV2 instance #1470

Merged
merged 14 commits into from
Oct 25, 2024
47 changes: 43 additions & 4 deletions docs/howtos/runtime_sessions.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
Use Experiments with Runtime sessions
=====================================
Use Experiments with Runtime sessions and sampler
=================================================

Problem
-------

You want to run experiments with a custom `SamplerV2
<https://docs.quantum.ibm.com/api/qiskit-ibm-runtime/qiskit_ibm_runtime.SamplerV2>`_ service.
dcmckayibm marked this conversation as resolved.
Show resolved Hide resolved

.. note::
All jobs, by default, run using the ``SamplerV2`` service. When calling ``exp.run`` a
``SamplerV2`` object will be automatically generated from the specified backend.
dcmckayibm marked this conversation as resolved.
Show resolved Hide resolved

Solution
--------

In this example, we will pass in a ``SamplerV2`` object to a tomography experiment.

.. note::
If a sampler object is passed to ``exp.run`` then the `run options
<https://docs.quantum.ibm.com/api/qiskit-ibm-runtime/qiskit_ibm_runtime.options.SamplerExecutionOptionsV2>`_ of the
sampler object are used. The execution options set by the experiment are ignored.

.. jupyter-input::

from qiskit_ibm_runtime import SamplerV2 as Sampler
from qiskit_experiments.library.tomography import ProcessTomography
from qiskit import QuantumCircuit

service = QiskitRuntimeService(channel="ibm_quantum")
backend = service.backend("ibm_osaka")
qc = QuantumCircuit(1)
qc.x(0)

sampler = Sampler(backed)
# set the shots in the sampler object
sampler.options.default_shots = 300
exp = ProcessTomography(qc)
# Artificially lower circuits per job, adjust value for your own application
exp.set_experiment_options(max_circuits=3)
# pass the sampler into the experiment
exp_data = exp.run(sampler)
dcmckayibm marked this conversation as resolved.
Show resolved Hide resolved

Problem
-------
Expand Down Expand Up @@ -40,5 +80,4 @@ large number of circuits that can't fit in a single job, it may be helpful to fo
# This will prevent further jobs from being submitted without terminating current jobs
backend.close_session()

Note that runtime primitives are not currently supported natively in Qiskit Experiments, so
the ``backend.run()`` path is required to run experiments.

dcmckayibm marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 4 additions & 0 deletions qiskit_experiments/framework/backend_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Since `BackendV1` and `BackendV2` do not share the same interface, this
class unifies data access for various data fields.
"""
import warnings
from qiskit.providers.models import PulseBackendConfiguration # pylint: disable=no-name-in-module
from qiskit.providers import BackendV1, BackendV2

Expand All @@ -24,6 +25,9 @@ class BackendData:

def __init__(self, backend):
"""Inits the backend and verifies version"""
warnings.filterwarnings(
"ignore", message=".*qiskit.qobj.pulse_qobj.*", category=DeprecationWarning
)
dcmckayibm marked this conversation as resolved.
Show resolved Hide resolved
self._backend = backend
self._v1 = isinstance(backend, BackendV1)
self._v2 = isinstance(backend, BackendV2)
Expand Down
10 changes: 10 additions & 0 deletions releasenotes/notes/primitives_add-1a3bcbb2f189d18e.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
prelude: >
In this release we added support for the qiskit primitives so that
qiskit-experiments will use the SamplerV2 run path.
dcmckayibm marked this conversation as resolved.
Show resolved Hide resolved
upgrade:
- |
Upgrade qiskit-experiments to use the SamplerV2 path. An option is
left in to use the old backend.run path, however, this is scheduled
to be deprecated by ibm-runtime in the near future.
dcmckayibm marked this conversation as resolved.
Show resolved Hide resolved