Skip to content

Commit

Permalink
Add support executing circuits with a SamplerV2 instance (qiskit-comm…
Browse files Browse the repository at this point in the history
…unity#1470)

This change adds a `sampler` argument to `BaseExperiment.run()` which
causes that `BaseSamplerV2` instance to be used to run the experiment's
circuits. Additionally, if no `sampler` is passed,
`BaseExperiment.run()` defaults to instantiating a
`qiskit_ibm_runtime.SamplerV2` object and running the circuits with
that. `qiskit_ibm_runtime.SamplerV2` can execute using IBM Quantum's
sampler primitive and can also fall back to calling `backend.run` for
backends that are not representing IBM Quantum devices. A `backend_run`
parameter is also added to `BaseExperiment.run()` which causes the old
`backend.run()` execution path to be used. For sampler execution, the
job results are translated back into the format expected from
`backend.run` so that the remaining analysis code can be used unchanged.
Some additional changes have been made to account for differences
between `backend.run` job result classes and primitive job result
classes.

---------

Co-authored-by: Will Shanks <[email protected]>
  • Loading branch information
dcmckayibm and wshanks authored Oct 25, 2024
1 parent 02572a3 commit bd05f2a
Show file tree
Hide file tree
Showing 38 changed files with 719 additions and 99 deletions.
7 changes: 7 additions & 0 deletions docs/howtos/artifacts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ Viewing artifacts
Here we run a parallel experiment consisting of two :class:`.T1` experiments in parallel and then view the output
artifacts as a list of :class:`.ArtifactData` objects accessed by :meth:`.ExperimentData.artifacts`:

.. jupyter-execute::
:hide-code:

# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()

.. jupyter-execute::

from qiskit_ibm_runtime.fake_provider import FakePerth
Expand Down
42 changes: 22 additions & 20 deletions docs/howtos/runtime_sessions.rst
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
Use Experiments with Runtime sessions
=====================================
Use Experiments with Sampler
=============================

Problem
-------

You want to run experiments in a `Runtime session
<https://docs.quantum.ibm.com/run/sessions>`_ so that jobs can run in close temporal proximity.
You want to run experiments with a custom :class:`qiskit.primitives.BaseSamplerV2` service.
A sampler can be instantiated with a backend, session or batch, which allows one to
run an experiment in different execution modes.

.. note::
All jobs, by default, run using the :class:`qiskit_ibm_runtime.SamplerV2` class. When calling ``exp.run`` a
:class:`qiskit_ibm_runtime.SamplerV2` object will be automatically generated to wrap the specified backend.

Solution
--------

.. note::
This guide requires :external+qiskit_ibm_runtime:doc:`qiskit-ibm-runtime <index>` version 0.15 and up, which can be installed with ``python -m pip install qiskit-ibm-runtime``.
For how to migrate from the older ``qiskit-ibm-provider`` to :external+qiskit_ibm_runtime:doc:`qiskit-ibm-runtime <index>`,
consult the `migration guide <https://docs.quantum.ibm.com/api/migration-guides/qiskit-runtime-from-provider>`_.\

Use the :class:`~qiskit_ibm_runtime.IBMBackend` object in :external+qiskit_ibm_runtime:doc:`index`, which supports sessions.
In this example, we will pass in a :class:`qiskit_ibm_runtime.SamplerV2` object to a tomography experiment.

In this example, we will set the ``max_circuits`` property to an artificially low value so that the experiment will be
split into multiple jobs that run sequentially in a single session. When running real experiments with a
large number of circuits that can't fit in a single job, it may be helpful to follow this usage pattern:
.. note::
If a sampler object is passed to :meth:`qiskit_experiments.framework.BaseExperiment.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 QiskitRuntimeService
from qiskit_ibm_runtime import SamplerV2 as Sampler
from qiskit_experiments.library.tomography import ProcessTomography
from qiskit import QuantumCircuit

Expand All @@ -32,13 +33,14 @@ large number of circuits that can't fit in a single job, it may be helpful to fo
qc = QuantumCircuit(1)
qc.x(0)

backend.open_session()
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)
exp_data = exp.run(backend)
# This will prevent further jobs from being submitted without terminating current jobs
backend.close_session()
# pass the sampler into the experiment
exp_data = exp.run(sampler=sampler)



Note that runtime primitives are not currently supported natively in Qiskit Experiments, so
the ``backend.run()`` path is required to run experiments.
7 changes: 7 additions & 0 deletions docs/manuals/characterization/t1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ for qubit 0.
packages to run simulations. You can install them with ``python -m pip
install qiskit-aer qiskit-ibm-runtime``.

.. jupyter-execute::
:hide-code:

# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()

.. jupyter-execute::

import numpy as np
Expand Down
7 changes: 7 additions & 0 deletions docs/manuals/characterization/t2ramsey.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ pure T1/T2 relaxation noise model.
packages to run simulations. You can install them with ``python -m pip
install qiskit-aer qiskit-ibm-runtime``.

.. jupyter-execute::
:hide-code:

# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()

.. jupyter-execute::

# A T1 simulator
Expand Down
7 changes: 7 additions & 0 deletions docs/manuals/characterization/tphi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ From the :math:`T_1` and :math:`T_2` estimates, we compute the results for
packages to run simulations. You can install them with ``python -m pip
install qiskit-aer qiskit-ibm-runtime``.

.. jupyter-execute::
:hide-code:

# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()

.. jupyter-execute::

import numpy as np
Expand Down
7 changes: 7 additions & 0 deletions docs/manuals/measurement/readout_mitigation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ experiments to generate the corresponding mitigators.
packages to run simulations. You can install them with ``python -m pip
install qiskit-aer qiskit-ibm-runtime``.

.. jupyter-execute::
:hide-code:

# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()

.. jupyter-execute::

import numpy as np
Expand Down
7 changes: 7 additions & 0 deletions docs/manuals/measurement/restless_measurements.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ they use always starts with the qubits in the ground state.
This tutorial requires the :external+qiskit_ibm_runtime:doc:`qiskit-ibm-runtime <index>` package to model a
backend. You can install it with ``python -m pip install qiskit-ibm-runtime``.

.. jupyter-execute::
:hide-code:

# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()

.. jupyter-execute::

from qiskit_ibm_runtime.fake_provider import FakePerth
Expand Down
7 changes: 7 additions & 0 deletions docs/manuals/verification/quantum_volume.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ z_value = 2), and at least 100 trials have been ran.
packages to run simulations. You can install them with ``python -m pip
install qiskit-aer qiskit-ibm-runtime``.

.. jupyter-execute::
:hide-code:

# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()

.. jupyter-execute::

from qiskit_experiments.framework import BatchExperiment
Expand Down
7 changes: 7 additions & 0 deletions docs/manuals/verification/randomized_benchmarking.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ explanation on the RB method, which is based on Refs. [1]_ [2]_.
packages to run simulations. You can install them with ``python -m pip
install qiskit-aer qiskit-ibm-runtime``.

.. jupyter-execute::
:hide-code:

# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()

.. jupyter-execute::

import numpy as np
Expand Down
7 changes: 7 additions & 0 deletions docs/manuals/verification/state_tomography.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ complete basis of measurement operators.

We first initialize a simulator to run the experiments on.

.. jupyter-execute::
:hide-code:

# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()

.. jupyter-execute::

from qiskit_aer import AerSimulator
Expand Down
7 changes: 7 additions & 0 deletions docs/tutorials/custom_experiment.rst
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,13 @@ To test our code, we first simulate a noisy backend with asymmetric readout erro
You can install it with ``python -m pip install qiskit-aer``.


.. jupyter-execute::
:hide-code:

# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()

.. jupyter-execute::

from qiskit_aer import AerSimulator, noise
Expand Down
7 changes: 7 additions & 0 deletions docs/tutorials/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ backend, real or simulated, that you can access through Qiskit.
packages to run simulations. You can install them with ``python -m pip
install qiskit-aer qiskit-ibm-runtime``.

.. jupyter-execute::
:hide-code:

# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
from qiskit_experiments.test.patching import patch_sampler_test_support
patch_sampler_test_support()

.. jupyter-execute::

from qiskit_ibm_runtime.fake_provider import FakePerth
Expand Down
9 changes: 8 additions & 1 deletion 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,11 +25,17 @@ class BackendData:

def __init__(self, backend):
"""Inits the backend and verifies version"""

self._backend = backend
self._v1 = isinstance(backend, BackendV1)
self._v2 = isinstance(backend, BackendV2)

if self._v2:
self._parse_additional_data()
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", message=".*qiskit.qobj.pulse_qobj.*", category=DeprecationWarning
)
self._parse_additional_data()

def _parse_additional_data(self):
# data specific parsing not done yet in upstream qiskit
Expand Down
Loading

0 comments on commit bd05f2a

Please sign in to comment.