Skip to content

Commit bd05f2a

Browse files
dcmckayibmwshanks
andauthored
Add support executing circuits with a SamplerV2 instance (#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]>
1 parent 02572a3 commit bd05f2a

38 files changed

+719
-99
lines changed

docs/howtos/artifacts.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ Viewing artifacts
2525
Here we run a parallel experiment consisting of two :class:`.T1` experiments in parallel and then view the output
2626
artifacts as a list of :class:`.ArtifactData` objects accessed by :meth:`.ExperimentData.artifacts`:
2727

28+
.. jupyter-execute::
29+
:hide-code:
30+
31+
# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
32+
from qiskit_experiments.test.patching import patch_sampler_test_support
33+
patch_sampler_test_support()
34+
2835
.. jupyter-execute::
2936

3037
from qiskit_ibm_runtime.fake_provider import FakePerth

docs/howtos/runtime_sessions.rst

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1-
Use Experiments with Runtime sessions
2-
=====================================
1+
Use Experiments with Sampler
2+
=============================
33

44
Problem
55
-------
66

7-
You want to run experiments in a `Runtime session
8-
<https://docs.quantum.ibm.com/run/sessions>`_ so that jobs can run in close temporal proximity.
7+
You want to run experiments with a custom :class:`qiskit.primitives.BaseSamplerV2` service.
8+
A sampler can be instantiated with a backend, session or batch, which allows one to
9+
run an experiment in different execution modes.
10+
11+
.. note::
12+
All jobs, by default, run using the :class:`qiskit_ibm_runtime.SamplerV2` class. When calling ``exp.run`` a
13+
:class:`qiskit_ibm_runtime.SamplerV2` object will be automatically generated to wrap the specified backend.
914

1015
Solution
1116
--------
1217

13-
.. note::
14-
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``.
15-
For how to migrate from the older ``qiskit-ibm-provider`` to :external+qiskit_ibm_runtime:doc:`qiskit-ibm-runtime <index>`,
16-
consult the `migration guide <https://docs.quantum.ibm.com/api/migration-guides/qiskit-runtime-from-provider>`_.\
17-
18-
Use the :class:`~qiskit_ibm_runtime.IBMBackend` object in :external+qiskit_ibm_runtime:doc:`index`, which supports sessions.
18+
In this example, we will pass in a :class:`qiskit_ibm_runtime.SamplerV2` object to a tomography experiment.
1919

20-
In this example, we will set the ``max_circuits`` property to an artificially low value so that the experiment will be
21-
split into multiple jobs that run sequentially in a single session. When running real experiments with a
22-
large number of circuits that can't fit in a single job, it may be helpful to follow this usage pattern:
20+
.. note::
21+
If a sampler object is passed to :meth:`qiskit_experiments.framework.BaseExperiment.run` then the `run options
22+
<https://docs.quantum.ibm.com/api/qiskit-ibm-runtime/qiskit_ibm_runtime.options.SamplerExecutionOptionsV2>`_ of the
23+
sampler object are used. The execution options set by the experiment are ignored.
2324

2425
.. jupyter-input::
2526

26-
from qiskit_ibm_runtime import QiskitRuntimeService
27+
from qiskit_ibm_runtime import SamplerV2 as Sampler
2728
from qiskit_experiments.library.tomography import ProcessTomography
2829
from qiskit import QuantumCircuit
2930

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

35-
backend.open_session()
36+
sampler = Sampler(backed)
37+
# set the shots in the sampler object
38+
sampler.options.default_shots = 300
3639
exp = ProcessTomography(qc)
3740
# Artificially lower circuits per job, adjust value for your own application
3841
exp.set_experiment_options(max_circuits=3)
39-
exp_data = exp.run(backend)
40-
# This will prevent further jobs from being submitted without terminating current jobs
41-
backend.close_session()
42+
# pass the sampler into the experiment
43+
exp_data = exp.run(sampler=sampler)
44+
45+
4246

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

docs/manuals/characterization/t1.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ for qubit 0.
3434
packages to run simulations. You can install them with ``python -m pip
3535
install qiskit-aer qiskit-ibm-runtime``.
3636

37+
.. jupyter-execute::
38+
:hide-code:
39+
40+
# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
41+
from qiskit_experiments.test.patching import patch_sampler_test_support
42+
patch_sampler_test_support()
43+
3744
.. jupyter-execute::
3845

3946
import numpy as np

docs/manuals/characterization/t2ramsey.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ pure T1/T2 relaxation noise model.
6262
packages to run simulations. You can install them with ``python -m pip
6363
install qiskit-aer qiskit-ibm-runtime``.
6464

65+
.. jupyter-execute::
66+
:hide-code:
67+
68+
# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
69+
from qiskit_experiments.test.patching import patch_sampler_test_support
70+
patch_sampler_test_support()
71+
6572
.. jupyter-execute::
6673

6774
# A T1 simulator

docs/manuals/characterization/tphi.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ From the :math:`T_1` and :math:`T_2` estimates, we compute the results for
2525
packages to run simulations. You can install them with ``python -m pip
2626
install qiskit-aer qiskit-ibm-runtime``.
2727

28+
.. jupyter-execute::
29+
:hide-code:
30+
31+
# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
32+
from qiskit_experiments.test.patching import patch_sampler_test_support
33+
patch_sampler_test_support()
34+
2835
.. jupyter-execute::
2936

3037
import numpy as np

docs/manuals/measurement/readout_mitigation.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ experiments to generate the corresponding mitigators.
3535
packages to run simulations. You can install them with ``python -m pip
3636
install qiskit-aer qiskit-ibm-runtime``.
3737

38+
.. jupyter-execute::
39+
:hide-code:
40+
41+
# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
42+
from qiskit_experiments.test.patching import patch_sampler_test_support
43+
patch_sampler_test_support()
44+
3845
.. jupyter-execute::
3946

4047
import numpy as np

docs/manuals/measurement/restless_measurements.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ they use always starts with the qubits in the ground state.
6262
This tutorial requires the :external+qiskit_ibm_runtime:doc:`qiskit-ibm-runtime <index>` package to model a
6363
backend. You can install it with ``python -m pip install qiskit-ibm-runtime``.
6464

65+
.. jupyter-execute::
66+
:hide-code:
67+
68+
# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
69+
from qiskit_experiments.test.patching import patch_sampler_test_support
70+
patch_sampler_test_support()
71+
6572
.. jupyter-execute::
6673

6774
from qiskit_ibm_runtime.fake_provider import FakePerth

docs/manuals/verification/quantum_volume.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ z_value = 2), and at least 100 trials have been ran.
2929
packages to run simulations. You can install them with ``python -m pip
3030
install qiskit-aer qiskit-ibm-runtime``.
3131

32+
.. jupyter-execute::
33+
:hide-code:
34+
35+
# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
36+
from qiskit_experiments.test.patching import patch_sampler_test_support
37+
patch_sampler_test_support()
38+
3239
.. jupyter-execute::
3340

3441
from qiskit_experiments.framework import BatchExperiment

docs/manuals/verification/randomized_benchmarking.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ explanation on the RB method, which is based on Refs. [1]_ [2]_.
1616
packages to run simulations. You can install them with ``python -m pip
1717
install qiskit-aer qiskit-ibm-runtime``.
1818

19+
.. jupyter-execute::
20+
:hide-code:
21+
22+
# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
23+
from qiskit_experiments.test.patching import patch_sampler_test_support
24+
patch_sampler_test_support()
25+
1926
.. jupyter-execute::
2027

2128
import numpy as np

docs/manuals/verification/state_tomography.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ complete basis of measurement operators.
1414

1515
We first initialize a simulator to run the experiments on.
1616

17+
.. jupyter-execute::
18+
:hide-code:
19+
20+
# Temporary workaround for missing support in Qiskit and qiskit-ibm-runtime
21+
from qiskit_experiments.test.patching import patch_sampler_test_support
22+
patch_sampler_test_support()
23+
1724
.. jupyter-execute::
1825

1926
from qiskit_aer import AerSimulator

0 commit comments

Comments
 (0)