Skip to content

Commit f3fb804

Browse files
Eric-ArellanoderivationSooluThomasLaura Zdanskijaygambetta
authored
Migrate open source docs (Qiskit#91)
First part of Qiskit#22 and Qiskit#23. This copies over the open source files we plan to use in this repository. They are not actually used yet and still require some work like converting RST to MDX, but it gets the files and their Git history here to start the process. Qiskit docs are placed in the folder `qiskit-docs/` and Runtime docs in `runtime-docs`. These are only for unprocessed docs. Once a document is ready, we will move it to the official `docs/` folder and have it go live. This uses `git-filter-repo` to preserve all the Git history and author attribution from the respective open source repositories. It uses the below Python script and this [zipped folder](https://github.com/Qiskit/documentation/files/12835669/migrate-open-source.zip), using an approach very similar to @jakelishman's original work in Qiskit/qiskit#10611. This PR has no additional commits. It solely copies over the other repositories into this one. ``` # Runtime files docs/faqs/max_execution_time.rst docs/faqs/open_source_vs_ibm_cloud_primitives.rst ``` ``` # Qiskit files # start/ docs/migration_guides/algorithms_migration.rst docs/migration_guides/opflow_migration.rst docs/migration_guides/qi_migration.rst # build/ docs/tutorials/circuits/01_circuit_basics.ipynb docs/tutorials/circuits/1_getting_started_with_qiskit.ipynb docs/tutorials/circuits/2_plotting_data_in_qiskit.ipynb docs/tutorials/circuits_advanced/01_advanced_circuits.ipynb docs/tutorials/circuits_advanced/02_operators_overview.ipynb docs/tutorials/circuits_advanced/05_pulse_gates.ipynb docs/tutorials/circuits_advanced/06_building_pulse_schedules.ipynb docs/tutorials/circuits_advanced/07_pulse_scheduler.ipynb docs/explanation/endianness.rst # transpile/ docs/tutorials/circuits_advanced/04_transpiler_passes_and_passmanager.ipynb # test/ docs/how_to/use_estimator.rst docs/how_to/use_sampler.rst # run/ docs/tutorials/circuits_advanced/08_gathering_system_information.ipynb # other/ docs/faq.rst # Tutorial renames from the original qiskit-tutorials repo docs/tutorials/terra/fundamentals/1_getting_started_with_qiskit.ipynb docs/tutorials/terra/fundamentals/2_plotting_data_in_qiskit.ipynb docs/tutorials/terra/fundamentals/3_summary_of_quantum_operations.ipynb docs/tutorials/terra/advanced/1_advanced_circuits.ipynb docs/tutorials/terra/advanced/2_operators_overview.ipynb docs/tutorials/terra/advanced/3_advanced_circuit_visualization.ipynb docs/tutorials/terra/advanced/4_transpiler_passes_and_passmanager.ipynb docs/tutorials/circuits_advanced/1_advanced_circuits.ipynb docs/tutorials/circuits_advanced/2_operators_overview.ipynb docs/tutorials/circuits_advanced/3_advanced_circuit_visualization.ipynb docs/tutorials/circuits_advanced/4_transpiler_passes_and_passmanager.ipynb ``` Migration script: ```python import subprocess from tempfile import TemporaryDirectory from pathlib import Path QISKIT_ROOT = Path("../qiskit") RUNTIME_ROOT = Path("../qiskit-ibm-runtime") DOCS_ROOT = Path("./").resolve() # Expects script author to have this folder created. MIGRATION_FOLDER = DOCS_ROOT / "migrate-open-source" MAILMAP = MIGRATION_FOLDER / "mailmap.txt" def main() -> None: with TemporaryDirectory() as tmpdir: qiskit_dir = clone_repo(tmpdir, repo_name="qiskit") git_filter_repo( qiskit_dir, "qiskit-files.txt", "qiskit-messages.txt", dest_folder="qiskit-docs", ) merge_repo(qiskit_dir, remote_name="qiskit") runtime_dir = clone_repo(tmpdir, repo_name="qiskit-ibm-runtime") git_filter_repo( runtime_dir, "runtime-files.txt", "runtime-messages.txt", dest_folder="runtime-docs", ) merge_repo(runtime_dir, remote_name="runtime") def clone_repo(tmpdir: str, repo_name: str) -> Path: subprocess.run( [ "git", "clone", "--no-tags", "--single-branch", f"https://github.com/Qiskit/{repo_name}.git", ], check=True, cwd=tmpdir, ) return Path(tmpdir, repo_name) def git_filter_repo( repo_path: str, files_txt_name: str, msg_txt_file: str, dest_folder: str ) -> None: subprocess.run( [ "python3", MIGRATION_FOLDER / "git-filter-repo", "--paths-from-file", MIGRATION_FOLDER / files_txt_name, "--replace-message", MIGRATION_FOLDER / msg_txt_file, "--mailmap", MAILMAP, "--path-rename", f"docs/:{dest_folder}/", ], check=True, cwd=repo_path, ) def merge_repo(repo_path: str, remote_name: str) -> None: subprocess.run(["git", "remote", "add", remote_name, repo_path], check=True) subprocess.run(["git", "fetch", remote_name]) subprocess.run( ["git", "merge", "--allow-unrelated-histories", f"{remote_name}/main"] ) subprocess.run(["git", "remote", "remove", remote_name]) main() ``` --------- Co-authored-by: Paul Kassebaum <paul.kassebaum@ibm.com> Co-authored-by: Soolu Thomas <soolu.thomas@ibm.com> Co-authored-by: Laura Zdanski <Laura.Zdanski@ibm.com> Co-authored-by: Jay M. Gambetta <jay.gambetta@us.ibm.com> Co-authored-by: Diego M. Rodríguez <diego.plan9@gmail.com> Co-authored-by: Paul Nation <nonhermitian@gmail.com> Co-authored-by: Matthew Treinish <mtreinish@kortar.org> Co-authored-by: 3yakuya <3yakuya@outlook.com> Co-authored-by: Sristy Sangskriti <sristysangskriti007@gmail.com> Co-authored-by: Vismai Khanderao <59114226+Vismai-Khanderao@users.noreply.github.com> Co-authored-by: Wei Hu <wei.hu@kfquantum.com> Co-authored-by: gunchamalik <gunchamalik@gmail.com> Co-authored-by: zodiacfireworks <martin.vuelta@gmail.com> Co-authored-by: Emixem <64846036+Emixem@users.noreply.github.com> Co-authored-by: Julien Gacon <jul@zurich.ibm.com> Co-authored-by: wagnersj <wagnerse@ca.ibm.com> Co-authored-by: Bruno E. Ramírez Galindo <47431792+brunormzg@users.noreply.github.com> Co-authored-by: Peter J <peter.j@kakao.com> Co-authored-by: Kevin Krsulich <kevin.krsulich@ibm.com> Co-authored-by: Lauren Capelluto <lcapelluto@users.noreply.github.com> Co-authored-by: Luciano Bello <luciano.bello@ibm.com> Co-authored-by: Thomas Alexander <talexander@ibm.com> Co-authored-by: Divyanshu Singh <55018955+divshacker@users.noreply.github.com> Co-authored-by: Takashi Imamichi <31178928+t-imamichi@users.noreply.github.com> Co-authored-by: jaleipekoglu <47296000+jaleipekoglu@users.noreply.github.com> Co-authored-by: SooluThomas <soolu.elto@gmail.com> Co-authored-by: Manoel Marques <manoel@us.ibm.com> Co-authored-by: Eli Arbel <arbel@il.ibm.com> Co-authored-by: Parmeet Singh <76438148+singhmeet11@users.noreply.github.com> Co-authored-by: Aurélien Pupier <apupier@redhat.com> Co-authored-by: Ali Javadi-Abhari <ali.javadi@ibm.com> Co-authored-by: Kevin Krsulich <kevin@krsulich.net> Co-authored-by: Naoki Kanazawa <39517270+nkanazawa1989@users.noreply.github.com> Co-authored-by: Claudia Zendejas-Morales <ing.claudia@gmail.com> Co-authored-by: Glen <96159532+NG-Glen@users.noreply.github.com> Co-authored-by: Edwin Navarro <enavarro@comcast.net> Co-authored-by: Junye Huang <h.jun.ye@gmail.com> Co-authored-by: Rathish Cholarajan <rathishc24@gmail.com> Co-authored-by: Kazuki Tsuoka <103920010+king-p3nguin@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: InfamousPlatypus <45645300+InfamousPlatypus@users.noreply.github.com> Co-authored-by: Luciano Bello <bel@zurich.ibm.com> Co-authored-by: Filippo Tramonto <11334590+filippotramonto@users.noreply.github.com> Co-authored-by: Kazuki Tsuoka <kazukitsuoka@g.ecc.u-tokyo.ac.jp> Co-authored-by: Kevin Hartman <kevin@hart.mn> Co-authored-by: Frank Harkins <frankharkins@users.noreply.github.com> Co-authored-by: Rebecca Dimock <66339736+beckykd@users.noreply.github.com> Co-authored-by: Elena Peña Tapia <57907331+ElePT@users.noreply.github.com> Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com> Co-authored-by: Declan Millar <declan.millar@ibm.com> Co-authored-by: Julien Gacon <gaconju@gmail.com> Co-authored-by: Guillermo-Mijares-Vilarino <106545082+Guillermo-Mijares-Vilarino@users.noreply.github.com> Co-authored-by: Kevin Tian <kevin.tian@ibm.com> Co-authored-by: Emil Magni <emilmagni@gmail.com> Co-authored-by: Will Shanks <wshaos@posteo.net> Co-authored-by: Jim Garrison <jim@garrison.cc> Co-authored-by: Abby Mitchell <23662430+javabster@users.noreply.github.com> Co-authored-by: Frank Harkins <frankharkins@hotmail.co.uk> Co-authored-by: Jessie Yu <jessieyu@us.ibm.com> Co-authored-by: Ikko Eltociear Ashimine <eltociear@gmail.com> Co-authored-by: Mehmet Keçeci <WhiteSymmetry@users.noreply.github.com> Co-authored-by: Kuba Pilch <6464505+3yakuya@users.noreply.github.com> Co-authored-by: Soon Teh <11854984+soon-teh@users.noreply.github.com> Co-authored-by: Jake Lishman <jake.lishman@ibm.com> Co-authored-by: Shilpa Mahato <61791872+shil-m@users.noreply.github.com> Co-authored-by: Elbert <67720913+elberttl@users.noreply.github.com> Co-authored-by: Bochen "Daniel" Tan <33230390+tbcdebug@users.noreply.github.com> Co-authored-by: Kevin Tian <kt474@cornell.edu> Co-authored-by: Alexander Ivrii <alexi@il.ibm.com> Co-authored-by: rht <rhtbot@protonmail.com>
1 parent 8a82d8a commit f3fb804

18 files changed

Lines changed: 18627 additions & 0 deletions
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#########################
2+
Order of qubits in Qiskit
3+
#########################
4+
5+
While most physics textbooks represent an :math:`n`-qubit system as the tensor product :math:`Q_0\otimes Q_1 \otimes ... \otimes Q_{n-1}`, where :math:`Q_j` is the :math:`j^{\mathrm{th}}` qubit, Qiskit uses the inverse order, that is, :math:`Q_{n-1}\otimes ... \otimes Q_1 \otimes Q_{0}`. As explained in `this video <https://www.youtube.com/watch?v=EiqHj3_Avps>`_ from `Qiskit's YouTube channel <https://www.youtube.com/@qiskit>`_, this is done to follow the convention in classical computing, in which the :math:`n^{\mathrm{th}}` bit or most significant bit (MSB) is placed on the left (with index 0) while the least significant bit (LSB) is placed on the right (index :math:`n-1`). This ordering convention is called little-endian while the one from the physics textbooks is called big-endian.
6+
7+
This means that if we have, for example, a 3-qubit system with qubit 0 in state :math:`|1\rangle` and qubits 1 and 2 in state :math:`|0\rangle`, Qiskit would represent this state as :math:`|001\rangle` while most physics textbooks would represent this state as :math:`|100\rangle`.
8+
9+
The matrix representation of any multi-qubit gate is also affected by this different qubit ordering. For example, if we consider the single-qubit gate
10+
11+
.. math::
12+
13+
U = \begin{pmatrix} u_{00} & u_{01} \\ u_{10} & u_{11} \end{pmatrix}
14+
15+
And we want a controlled version :math:`C_U` whose control qubit is qubit 0 and whose target is qubit 1, following Qiskit's ordering its matrix representation would be
16+
17+
.. math::
18+
19+
C_U = \begin{pmatrix} 1 & 0 & 0 & 0 \\0 & u_{00} & 0 & u_{01} \\ 0 & 0 & 1 & 0 \\ 0 & u_{10} & 0& u_{11} \end{pmatrix}
20+
21+
while in a physics textbook it would be written as
22+
23+
.. math::
24+
25+
C_U = \begin{pmatrix} 1 & 0 & 0 & 0 \\0 & 1 & 0 & 0 \\ 0 & 0 & u_{00} & u_{01} \\ 0 & 0 & u_{00} & u_{01} \end{pmatrix}
26+
27+
28+
For more details about how this ordering of MSB and LSB affects the matrix representation of any particular gate, check its entry in the circuit :mod:`~qiskit.circuit.library`.
29+
30+
This different order can also make the circuit corresponding to an algorithm from a textbook a bit more complicated to visualize. Fortunately, Qiskit provides a way to represent a :class:`~.QuantumCircuit` with the most significant qubits on top, just like in the textbooks. This can be done by setting the ``reverse_bits`` argument of the :meth:`~.QuantumCircuit.draw` method to ``True``.
31+
32+
Let's try this for a 3-qubit Quantum Fourier Transform (:class:`~.QFT`).
33+
34+
.. plot::
35+
:include-source:
36+
:context:
37+
38+
from qiskit.circuit.library import QFT
39+
40+
qft = QFT(3)
41+
qft.decompose().draw('mpl')
42+
43+
.. plot::
44+
:include-source:
45+
:context: close-figs
46+
47+
qft.decompose().draw('mpl', reverse_bits=True)

qiskit-docs/faq.rst

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
.. _faq:
2+
3+
==========================
4+
Frequently Asked Questions
5+
==========================
6+
7+
**Q: How should I cite Qiskit in my research?**
8+
9+
**A:** Please cite Qiskit by using the included `BibTeX file
10+
<https://github.com/Qiskit/qiskit-terra/blob/main/CITATION.bib>`__.
11+
12+
|
13+
14+
**Q: Why do I receive the error message** ``AttributeError: QuantumCircuit object has no attribute save_state``
15+
**when using ``save_*``method on a circuit?**
16+
17+
**A:** The ``save_*`` instructions are part of Qiskit Aer project,
18+
a high performance simulator for quantum circuits. These instructions do not
19+
exist outside of Qiskit Aer and are added dynamically to the
20+
:class:`~.QuantumCircuit` class by Qiskit Aer on import. If you would like to
21+
use these instructions you must first ensure that you have imported
22+
``qiskit_aer`` in your program before trying to call these methods. You
23+
can refer to :mod:`qiskit_aer.library` for the details of these custom
24+
instructions included with Qiskit Aer.
25+
26+
**Q: Why do my results from real devices differ from my results from the simulator?**
27+
28+
**A:** The simulator runs jobs as though is was in an ideal environment; one
29+
without noise or decoherence. However, when jobs are run on the real devices
30+
there is noise from the environment and decoherence, which causes the qubits
31+
to behave differently than what is intended.
32+
33+
|
34+
35+
**Q: Why do I receive the error message,** ``No Module 'qiskit'`` **when using Jupyter Notebook?**
36+
37+
**A:** If you used ``pip install qiskit`` and set up your virtual environment in
38+
Anaconda, then you may experience this error when you run a tutorial
39+
in Jupyter Notebook. If you have not installed Qiskit or set up your
40+
virtual environment, you can follow the :ref:`installation` steps.
41+
42+
The error is caused when trying to import the Qiskit package in an
43+
environment where Qiskit is not installed. If you launched Jupyter Notebook
44+
from the Anaconda-Navigator, it is possible that Jupyter Notebook is running
45+
in the base (root) environment, instead of in your virtual
46+
environment. Choose a virtual environment in the Anaconda-Navigator from the
47+
**Applications on** dropdown menu. In this menu, you can see
48+
all of the virtual environments within Anaconda, and you can
49+
select the environment where you have Qiskit installed to launch Jupyter
50+
Notebook.
51+
52+
|
53+
54+
**Q: Why am I getting a compilation error while installing ``qiskit``?**
55+
56+
**A:** Qiskit depends on a number of other open source Python packages, which
57+
are automatically installed when doing ``pip install qiskit``. Depending on
58+
your system's platform and Python version, it is possible that a particular
59+
package does not provide pre-built binaries for your system. You can refer
60+
to :ref:`platform_support` for a list of platforms supported by Qiskit, some
61+
of which may need an extra compiler. In cases where there are
62+
no precompiled binaries available ``pip`` will attempt to compile the package
63+
from source, which in turn might require some extra dependencies that need to
64+
be installed manually.
65+
66+
If the output of ``pip install qiskit`` contains similar lines to:
67+
68+
.. code:: sh
69+
70+
Failed building wheel for SOME_PACKAGE
71+
...
72+
build/temp.linux-x86_64-3.5/_openssl.c:498:30: fatal error
73+
compilation terminated.
74+
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
75+
76+
please check the documentation of the package that failed to install (in the
77+
example code, ``SOME_PACKAGE``) for information on how to install the libraries
78+
needed for compiling from source.

qiskit-docs/how_to/use_sampler.rst

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
###############################################################
2+
Compute circuit output probabilities with ``Sampler`` primitive
3+
###############################################################
4+
5+
This guide shows how to get the probability distribution of a quantum circuit with the :class:`~qiskit.primitives.Sampler` primitive.
6+
7+
.. note::
8+
9+
While this guide uses Qiskit’s reference implementation, the ``Sampler`` primitive can be run with any provider using :class:`~qiskit.primitives.BackendSampler`.
10+
11+
.. code-block::
12+
13+
from qiskit.primitives import BackendSampler
14+
from <some_qiskit_provider> import QiskitProvider
15+
16+
provider = QiskitProvider()
17+
backend = provider.get_backend('backend_name')
18+
sampler = BackendSampler(backend)
19+
20+
There are some providers that implement primitives natively (see `this page <http://qiskit.org/providers/#primitives>`_ for more details).
21+
22+
Initialize quantum circuits
23+
===========================
24+
25+
The first step is to create the :class:`~qiskit.circuit.QuantumCircuit`\ s from which you want to obtain the probability distribution.
26+
27+
.. plot::
28+
:include-source:
29+
30+
from qiskit import QuantumCircuit
31+
32+
qc = QuantumCircuit(2)
33+
qc.h(0)
34+
qc.cx(0,1)
35+
qc.measure_all()
36+
qc.draw("mpl")
37+
38+
.. testsetup::
39+
40+
# This code is repeated (but hidden) because we will need to use the variables with the extension sphinx.ext.doctest (testsetup/testcode/testoutput directives)
41+
# and we can't reuse the variables from the plot directive above because they are incompatible.
42+
# The plot directive is used to draw the circuit with matplotlib and the code is shown because of the include-source flag.
43+
44+
from qiskit import QuantumCircuit
45+
46+
qc = QuantumCircuit(2)
47+
qc.h(0)
48+
qc.cx(0,1)
49+
qc.measure_all()
50+
51+
.. note::
52+
53+
The :class:`~qiskit.circuit.QuantumCircuit` you pass to :class:`~qiskit.primitives.Sampler` has to include measurements.
54+
55+
Initialize the ``Sampler``
56+
==========================
57+
58+
Then, you need to create a :class:`~qiskit.primitives.Sampler` instance.
59+
60+
.. testcode::
61+
62+
from qiskit.primitives import Sampler
63+
64+
sampler = Sampler()
65+
66+
Run and get results
67+
===================
68+
69+
Now that you have defined your ``sampler``, you can run it by calling the :meth:`~qiskit.primitives.Sampler.run` method,
70+
which returns an instance of :class:`~.PrimitiveJob` (subclass of :class:`~qiskit.providers.JobV1`). You can get the results from the job (as a :class:`~qiskit.primitives.SamplerResult` object)
71+
with the :meth:`~qiskit.providers.JobV1.result` method.
72+
73+
.. testcode::
74+
75+
job = sampler.run(qc)
76+
result = job.result()
77+
print(result)
78+
79+
.. testoutput::
80+
81+
SamplerResult(quasi_dists=[{0: 0.4999999999999999, 3: 0.4999999999999999}], metadata=[{}])
82+
83+
While this example only uses one :class:`~qiskit.circuit.QuantumCircuit`, if you want to sample multiple circuits you can
84+
pass a ``list`` of :class:`~qiskit.circuit.QuantumCircuit` instances to the :meth:`~qiskit.primitives.Sampler.run` method.
85+
86+
Get the probability distribution
87+
--------------------------------
88+
89+
From these results you can extract the quasi-probability distributions with the attribute :attr:`~qiskit.primitives.SamplerResult.quasi_dists`.
90+
91+
Even though there is only one circuit in this example, :attr:`~qiskit.primitives.SamplerResult.quasi_dists` returns a list of :class:`~qiskit.result.QuasiDistribution`\ s.
92+
``result.quasi_dists[i]`` is the quasi-probability distribution of the ``i``-th circuit.
93+
94+
.. note::
95+
96+
A quasi-probability distribution differs from a probability distribution in that negative values are also allowed.
97+
However the quasi-probabilities must sum up to 1 like probabilities.
98+
Negative quasi-probabilities may appear when using error mitigation techniques.
99+
100+
.. testcode::
101+
102+
quasi_dist = result.quasi_dists[0]
103+
print(quasi_dist)
104+
105+
.. testoutput::
106+
107+
{0: 0.4999999999999999, 3: 0.4999999999999999}
108+
109+
Probability distribution with binary outputs
110+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
111+
112+
If you prefer to see the output keys as binary strings instead of decimal numbers, you can use the
113+
:meth:`~qiskit.result.QuasiDistribution.binary_probabilities` method.
114+
115+
.. testcode::
116+
117+
print(quasi_dist.binary_probabilities())
118+
119+
.. testoutput::
120+
121+
{'00': 0.4999999999999999, '11': 0.4999999999999999}
122+
123+
Parameterized circuit with ``Sampler``
124+
========================================
125+
126+
The :class:`~qiskit.primitives.Sampler` primitive can be run with unbound parameterized circuits like the one below.
127+
You can also manually bind values to the parameters of the circuit and follow the steps
128+
of the previous example.
129+
130+
.. testcode::
131+
132+
from qiskit.circuit import Parameter
133+
134+
theta = Parameter('θ')
135+
param_qc = QuantumCircuit(2)
136+
param_qc.ry(theta, 0)
137+
param_qc.cx(0,1)
138+
param_qc.measure_all()
139+
print(param_qc.draw())
140+
141+
.. testoutput::
142+
143+
┌───────┐ ░ ┌─┐
144+
q_0: ┤ Ry(θ) ├──■───░─┤M├───
145+
└───────┘┌─┴─┐ ░ └╥┘┌─┐
146+
q_1: ─────────┤ X ├─░──╫─┤M├
147+
└───┘ ░ ║ └╥┘
148+
meas: 2/══════════════════╩══╩═
149+
0 1
150+
151+
The main difference from the previous case is that now you need to specify the sets of parameter values
152+
for which you want to evaluate the expectation value as a ``list`` of ``list``\ s of ``float``\ s.
153+
The ``i``-th element of the outer ``list`` is the set of parameter values
154+
that corresponds to the ``i``-th circuit.
155+
156+
.. testcode::
157+
158+
import numpy as np
159+
160+
parameter_values = [[0], [np.pi/6], [np.pi/2]]
161+
162+
job = sampler.run([param_qc]*3, parameter_values=parameter_values)
163+
dists = job.result().quasi_dists
164+
165+
for i in range(3):
166+
print(f"Parameter: {parameter_values[i][0]:.5f}\t Probabilities: {dists[i]}")
167+
168+
.. testoutput::
169+
170+
Parameter: 0.00000 Probabilities: {0: 1.0}
171+
Parameter: 0.52360 Probabilities: {0: 0.9330127018922194, 3: 0.0669872981077807}
172+
Parameter: 1.57080 Probabilities: {0: 0.5000000000000001, 3: 0.4999999999999999}
173+
174+
Change run options
175+
==================
176+
177+
Your workflow might require tuning primitive run options, such as the amount of shots.
178+
179+
By default, the reference :class:`~qiskit.primitives.Sampler` class performs an exact statevector
180+
calculation based on the :class:`~qiskit.quantum_info.Statevector` class. However, this can be
181+
modified to include shot noise if the number of ``shots`` is set.
182+
For reproducibility purposes, a ``seed`` will also be set in the following examples.
183+
184+
There are two main ways of setting options in the :class:`~qiskit.primitives.Sampler`:
185+
186+
* Set keyword arguments in the :meth:`~qiskit.primitives.Sampler.run` method.
187+
* Modify :class:`~qiskit.primitives.Sampler` options.
188+
189+
Set keyword arguments for :meth:`~qiskit.primitives.Sampler.run`
190+
----------------------------------------------------------------
191+
192+
If you only want to change the settings for a specific run, it can be more convenient to
193+
set the options inside the :meth:`~qiskit.primitives.Sampler.run` method. You can do this by
194+
passing them as keyword arguments.
195+
196+
.. testcode::
197+
198+
job = sampler.run(qc, shots=2048, seed=123)
199+
result = job.result()
200+
print(result)
201+
202+
.. testoutput::
203+
204+
SamplerResult(quasi_dists=[{0: 0.5205078125, 3: 0.4794921875}], metadata=[{'shots': 2048}])
205+
206+
Modify :class:`~qiskit.primitives.Sampler` options
207+
---------------------------------------------------
208+
209+
If you want to keep some configuration values for several runs, it can be better to
210+
change the :class:`~qiskit.primitives.Sampler` options. That way you can use the same
211+
:class:`~qiskit.primitives.Sampler` object as many times as you wish without having to
212+
rewrite the configuration values every time you use :meth:`~qiskit.primitives.Sampler.run`.
213+
214+
Modify existing :class:`~qiskit.primitives.Sampler`
215+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
216+
217+
If you prefer to change the options of an already-defined :class:`~qiskit.primitives.Sampler`, you can use
218+
:meth:`~qiskit.primitives.Sampler.set_options` and introduce the new options as keyword arguments.
219+
220+
.. testcode::
221+
222+
sampler.set_options(shots=2048, seed=123)
223+
224+
job = sampler.run(qc)
225+
result = job.result()
226+
print(result)
227+
228+
.. testoutput::
229+
230+
SamplerResult(quasi_dists=[{0: 0.5205078125, 3: 0.4794921875}], metadata=[{'shots': 2048}])
231+
232+
Define a new :class:`~qiskit.primitives.Sampler` with the options
233+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
234+
235+
If you prefer to define a new :class:`~qiskit.primitives.Sampler` with new options, you need to
236+
define a ``dict`` like this one:
237+
238+
.. testcode::
239+
240+
options = {"shots": 2048, "seed": 123}
241+
242+
And then you can introduce it into your new :class:`~qiskit.primitives.Sampler` with the
243+
``options`` argument.
244+
245+
.. testcode::
246+
247+
sampler = Sampler(options=options)
248+
249+
job = sampler.run(qc)
250+
result = job.result()
251+
print(result)
252+
253+
.. testoutput::
254+
255+
SamplerResult(quasi_dists=[{0: 0.5205078125, 3: 0.4794921875}], metadata=[{'shots': 2048}])

0 commit comments

Comments
 (0)