You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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]>
Copy file name to clipboardExpand all lines: docs/howtos/runtime_sessions.rst
+22-20Lines changed: 22 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,29 +1,30 @@
1
-
Use Experiments with Runtime sessions
2
-
=====================================
1
+
Use Experiments with Sampler
2
+
=============================
3
3
4
4
Problem
5
5
-------
6
6
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.
9
14
10
15
Solution
11
16
--------
12
17
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.
19
19
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.
23
24
24
25
.. jupyter-input::
25
26
26
-
from qiskit_ibm_runtime import QiskitRuntimeService
27
+
from qiskit_ibm_runtime import SamplerV2 as Sampler
27
28
from qiskit_experiments.library.tomography import ProcessTomography
28
29
from qiskit import QuantumCircuit
29
30
@@ -32,13 +33,14 @@ large number of circuits that can't fit in a single job, it may be helpful to fo
32
33
qc = QuantumCircuit(1)
33
34
qc.x(0)
34
35
35
-
backend.open_session()
36
+
sampler = Sampler(backed)
37
+
# set the shots in the sampler object
38
+
sampler.options.default_shots = 300
36
39
exp = ProcessTomography(qc)
37
40
# Artificially lower circuits per job, adjust value for your own application
38
41
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
+
42
46
43
-
Note that runtime primitives are not currently supported natively in Qiskit Experiments, so
44
-
the ``backend.run()`` path is required to run experiments.
0 commit comments