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

Migrate open source docs (attempt #2) #118

Merged
merged 142 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
142 commits
Select commit Hold shift + click to select a range
700f9e6
add FAQ (Qiskit/qiskit-metapackage#219)
derivation May 2, 2019
e916155
hone getting started (Qiskit/qiskit-metapackage#279)
SooluThomas May 21, 2019
594b5ad
0.11 IBMQ Provider Doc updates (Qiskit/qiskit-metapackage#367)
Jul 16, 2019
6c37ecd
Standardize the links in .rst files (Qiskit/qiskit-metapackage#391)
SooluThomas Jul 31, 2019
a34bd2e
Update faq.rst (Qiskit/qiskit-metapackage#428)
jaygambetta Aug 18, 2019
e93d134
Add FAQ entry for source dependencies (Qiskit/qiskit-metapackage#528)
diego-plan9 Sep 2, 2019
11a22dc
fix typo in FAQ (Qiskit/qiskit-metapackage#593)
derivation Sep 26, 2019
91a189e
start new format
nonhermitian Apr 26, 2020
0cbd9a5
update 2
nonhermitian Apr 27, 2020
2b6d39b
updates
nonhermitian Apr 28, 2020
b4974cb
aer tutorials
nonhermitian Apr 29, 2020
6ce241c
add start here back
nonhermitian Apr 29, 2020
064d3d5
updates
nonhermitian Apr 29, 2020
dd69ba4
add aqua optim and reorg aer
nonhermitian Apr 29, 2020
c37c8d1
Add other optimization notebooks and fixes
nonhermitian May 8, 2020
7b6d90d
Merge pull request Qiskit/qiskit-tutorials#896 from nonhermitian/add_…
nonhermitian May 8, 2020
2669bbb
Set up CI with Azure Pipelines (Qiskit/qiskit-tutorials#894)
mtreinish May 8, 2020
653d498
fix terra adv 1
nonhermitian May 8, 2020
33b325f
Merge pull request Qiskit/qiskit-tutorials#898 from nonhermitian/fix_…
nonhermitian May 8, 2020
eaa1357
reorg
nonhermitian May 11, 2020
858ebea
Merge pull request Qiskit/qiskit-tutorials#906 from nonhermitian/reorg1
nonhermitian May 11, 2020
5c03bb3
fix spacing around equations in circuits_1
nonhermitian May 11, 2020
20c9ce8
Update 4_transpiler_passes_and_passmanager.ipynb
nonhermitian May 11, 2020
9fee6f1
Merge pull request Qiskit/qiskit-tutorials#908 from nonhermitian/fix_…
nonhermitian May 11, 2020
879005b
Merge branch 'master' into fix_transpiler_notebook
nonhermitian May 11, 2020
8dcd94b
Merge pull request Qiskit/qiskit-tutorials#910 from nonhermitian/fix_…
nonhermitian May 22, 2020
6942aca
Re-stating that MSQubit is indexed with 0.
3yakuya Jun 24, 2020
32cee0b
Adding spacing around operator and argument.
3yakuya Jun 24, 2020
153183f
Spaces around operator
3yakuya Jun 29, 2020
1119ef8
Merge pull request Qiskit/qiskit-tutorials#926 from 3yakuya/master
nonhermitian Jul 1, 2020
42e5253
add working pulse tutorials
nonhermitian Jul 13, 2020
78d989c
Merge pull request Qiskit/qiskit-tutorials#937 from nonhermitian/add_…
nonhermitian Jul 13, 2020
86c9bfc
Update 1_getting_started_with_qiskit.ipynb
ssangskriti Jul 26, 2020
5ecf9b3
Merge pull request Qiskit/qiskit-tutorials#971 from ssangskriti/patch-1
nonhermitian Jul 27, 2020
adca019
Added line before plot_state_city
Vismai-Khanderao Jul 28, 2020
e9ab698
Matched code block order of functions to list
Vismai-Khanderao Jul 28, 2020
9f3d4d7
Merge pull request Qiskit/qiskit-tutorials#972 from Vismai-Khanderao/…
nonhermitian Jul 31, 2020
0745115
Fix a typo
Aug 1, 2020
180e355
Update 1_getting_started_with_qiskit.ipynb
gunchamalik Aug 4, 2020
aed384c
Fix typo in density matrix designation on tutorials/circuits/1_gettin…
zodiacfireworks Aug 5, 2020
172ebad
Merge pull request Qiskit/qiskit-tutorials#979 from gunchamalik/patch-1
nonhermitian Aug 6, 2020
057dba9
Merge branch 'master' into patch-1
nonhermitian Aug 6, 2020
ed0f108
Remove broken line from transpiler passes tutorial
mtreinish Aug 7, 2020
f531189
Merge pull request Qiskit/qiskit-tutorials#985 from mtreinish/unblock…
nonhermitian Aug 7, 2020
53872bb
Merge branch 'master' into patch-1
nonhermitian Aug 7, 2020
9e89439
Merge pull request Qiskit/qiskit-tutorials#974 from wh5a/patch-1
nonhermitian Aug 7, 2020
8a50ff2
Merge branch 'master' into master
nonhermitian Aug 7, 2020
f6d986a
Correction in quantum register declaration for composite Gates exampl…
Emixem Aug 12, 2020
8ca0cd8
rm usage of deprecated gate locations
Cryoris Aug 13, 2020
7fa88fd
Merge branch 'master' into gate-deprecations
Cryoris Aug 13, 2020
cf19b1e
Merge pull request Qiskit/qiskit-tutorials#1017 from Cryoris/gate-dep…
mtreinish Aug 14, 2020
b3720c3
Resolved deprecation warnings in transpiler tutorial notebook (Qiskit…
wagnersj Sep 21, 2020
5dbfa05
Fixed issue 987 by updating 1_getting_started_with_qiskit.ipynb noteb…
brunormzg Sep 21, 2020
52a0562
Fixed issue 1065 by correcting the typo
neont21 Oct 24, 2020
d637c39
Combine advanced circuits and pulse (Qiskit/qiskit-tutorials#1086)
nonhermitian Nov 14, 2020
24151ed
Remove deprecated DAGCircuit method usage. (Qiskit/qiskit-tutorials#1…
kdk Dec 1, 2020
9e73477
Merge branch 'master' into master
SooluThomas Jan 11, 2021
ff17021
Merge pull request Qiskit/qiskit-tutorials#1066 from neont21/master
SooluThomas Jan 11, 2021
ecc041a
Reindex and rename files
lcapelluto Jan 19, 2021
0a7437b
Add new page about pulse gates
lcapelluto Jan 19, 2021
f431dcd
Modify existing files
lcapelluto Jan 19, 2021
f7278a5
Add note about openpulse being enabled for pulse gates
lcapelluto Jan 20, 2021
013148e
Add note about scheduling
lcapelluto Jan 20, 2021
9cd2a08
Run pulse gates on 0.16.2
lcapelluto Jan 26, 2021
dbed36d
Merge pull request Qiskit/qiskit-tutorials#1115 from lcapelluto/updat…
SooluThomas Jan 26, 2021
c93b86e
Merge branch 'master' into update-pulse-docs-part2
SooluThomas Jan 26, 2021
73d59ec
Execute on terra version 0.16.2
lcapelluto Jan 26, 2021
0e9948b
Merge pull request Qiskit/qiskit-tutorials#1116 from lcapelluto/updat…
SooluThomas Jan 26, 2021
930ca23
Terra is removing deprecated interactive visualizations (Qiskit/qiski…
Mar 18, 2021
f3578a8
Add usage of backend in pulse builder. (Qiskit/qiskit-tutorials#1134)
taalexander Mar 26, 2021
b522fe8
Fixed Some Minor bugs in tutorials (Qiskit/qiskit-tutorials#1149)
divshacker Mar 29, 2021
6b3d7ac
Editing the initial tutorial with updates to show circuit basics. (Qi…
jaygambetta Apr 7, 2021
9c72e26
Fix math mode, toc, and spelling of several sections (Qiskit/qiskit-t…
t-imamichi Apr 7, 2021
de03bd7
Backend should run transpiled circuit (Qiskit/qiskit-tutorials#1170)
Cryoris Apr 23, 2021
689c0c0
Update 2_plotting_data_in_qiskit.ipynb (Qiskit/qiskit-tutorials#1175)
jaleipekoglu May 3, 2021
d979d9f
Remove Aqua references from notebooks (Qiskit/qiskit-tutorials#1162)
manoelmarques Jun 8, 2021
2539b2a
Update Building Pulse Schedules tutorial to add some notes about in w…
lcapelluto Jun 23, 2021
b354b43
Removed %matplotlib inline (Qiskit/qiskit-tutorials#1200)
divshacker Jun 24, 2021
b9a05a0
Added extra cell for Alternative way of changing array to latex (Qisk…
divshacker Jul 6, 2021
2a6866c
fist -> first (Qiskit/qiskit-tutorials#1186)
Cryoris Jul 9, 2021
3cb3a14
Removing `%matplotlib inline` from different tutorials (Qiskit/qiski…
divshacker Jul 9, 2021
4081111
Fix typo quant-info -> quant_info module and remove extra space (Qisk…
apupier Jul 9, 2021
c09c9bd
fix json of getting_stared (Qiskit/qiskit-tutorials#1240)
t-imamichi Jul 15, 2021
ca07202
replace execute with transpile + backend.run (Qiskit/qiskit-tutorials…
Aug 4, 2021
ec057be
Update pulse draw() usage (Qiskit/qiskit-tutorials#1253)
nkanazawa1989 Nov 15, 2021
26eb5a9
Update pulse draw method in gathering system information tutorial (Qi…
nkanazawa1989 Nov 18, 2021
0a2ff8c
Fix small render error (Qiskit/qiskit-tutorials#1278)
clausia Mar 7, 2022
b1e7054
apital Letter
NG-Glen Mar 23, 2022
928b354
Merge pull request Qiskit/qiskit-tutorials#1291 from NG-Glen/patch-16
Mar 23, 2022
10870da
Fix deprecated ref to node.condition
enavarro51 Apr 8, 2022
92b4bfc
Merge pull request Qiskit/qiskit-tutorials#1303 from enavarro51/remov…
kdk Apr 18, 2022
df633c8
Add max execution time to faq (Qiskit/qiskit-ibm-runtime#294)
HuangJunye May 2, 2022
26ac1bd
Fix Qasmsimulator deprecation warnings (Qiskit/qiskit-tutorials#1305)
king-p3nguin May 17, 2022
d6e2830
Fixing Qiskit/qiskit-tutorials#1326 (Qiskit/qiskit-tutorials#1327)
InfamousPlatypus May 30, 2022
b98d3a9
Update operator tutorial to avoid deprecated Pauli construction (Qisk…
mtreinish Jun 23, 2022
9b1a342
Update pulse gate tutorial for changes in fake backends
mtreinish Sep 12, 2022
c4134a4
Merge pull request Qiskit/qiskit-tutorials#1356 from mtreinish/update…
Sep 21, 2022
d746ddf
Update more pulse gate tutorial for changes in fake backends (Qiskit/…
mtreinish Sep 21, 2022
24ad0ed
Deleted unused import (Qiskit/qiskit-tutorials#1374)
king-p3nguin Dec 9, 2022
e348409
qiskit.providers.aer is qiskit_aer in Aer 0.11 (Qiskit/qiskit-tutoria…
Jan 10, 2023
a903eb2
Small fixes of 1_getting_started_with_qiskit.ipynb (Qiskit/qiskit-tut…
filippotramonto Jan 12, 2023
2eac665
Replace deprecated module `qiskit.test.mock` (Qiskit/qiskit-tutorials…
king-p3nguin Jan 25, 2023
a82e45a
qubit {j} to qubit $Q_{j}$ more readable (Qiskit/qiskit-tutorials#1301)
NG-Glen Jan 25, 2023
13a3eb2
Update 2_plotting_data_in_qiskit.ipynb (Qiskit/qiskit-tutorials#1297)
NG-Glen Jan 25, 2023
c73f420
Fix toctree issues with documentation (Qiskit/qiskit-metapackage#1638)
mtreinish Feb 8, 2023
112cac6
Use Vale to lint docs writing (Qiskit/qiskit-ibm-runtime#739)
frankharkins Mar 7, 2023
c0b8fd0
Fix a sentence in contributing to qiskit docs (Qiskit/qiskit-metapack…
SooluThomas Mar 23, 2023
0de4635
Add Migration Guide for `qiskit.algorithms` (Qiskit/qiskit#9557)
ElePT Apr 5, 2023
a635fb1
Add Migration Guide for Opflow (Qiskit/qiskit#9549)
ElePT Apr 6, 2023
4210ca5
Add Migration Guide for `QuantumInstance` (Qiskit/qiskit#9554)
ElePT Apr 13, 2023
6e0207d
Consistently describe how long a program can run (Qiskit/qiskit-ibm-r…
beckykd May 15, 2023
26d3072
Fixed spelling mistake in text for Qiskit Visualizations (Qiskit/qisk…
EmilMagni May 18, 2023
f2b0ee2
Update note about pulse gate support (Qiskit/qiskit-tutorials#1455)
wshanks May 18, 2023
9722edb
Add how-to guides about primitives (Qiskit/qiskit#9716)
Guillermo-Mijares-Vilarino May 19, 2023
c7baa9e
Point to the CITATION file in terra (Qiskit/qiskit-metapackage#1733)
May 22, 2023
4e583ac
some phrasing in the migration guides (Qiskit/qiskit#10083)
May 30, 2023
7888265
Added explanation section to Qiskit-terra (Qiskit/qiskit#8685)
Guillermo-Mijares-Vilarino Jun 1, 2023
81f61e4
Remove hello-world from docs (Qiskit/qiskit-ibm-runtime#878)
jyu00 Jun 2, 2023
f432bda
edit (Qiskit/qiskit-ibm-runtime#895)
beckykd Jun 8, 2023
076dadf
Update typo in `qi_migration.rst` (Qiskit/qiskit#10292)
SooluThomas Jun 15, 2023
e19406b
Fix typo in 01_circuit_basics.ipynb (Qiskit/qiskit-tutorials#1474)
eltociear Jul 11, 2023
63769b7
qiskit.test.mock is deprecated (Qiskit/qiskit-tutorials#1349)
WhiteSymmetry Jul 11, 2023
7568281
Clarify misleading description of a layer of operations. (Qiskit/qisk…
3yakuya Jul 17, 2023
aaf21a9
Fixes Broken links (Qiskit/qiskit-tutorials#1488)
divshacker Jul 26, 2023
4a78528
fix missing backticks for plot_histogram() (Qiskit/qiskit-tutorials#1…
soon-teh Jul 27, 2023
22a406c
Merge documentation and benchmarks from metapackage
jakelishman Aug 11, 2023
693efe8
Migrate documentation and benchmarks from metapackage (Qiskit/qiskit#…
mtreinish Aug 11, 2023
f55ab26
Closes Qiskit/qiskit-tutorials#1189 Replacing 'state matrix' by 'dens…
shil-m Aug 16, 2023
4c95034
Fixed a code typo in 02_operators_overview.ipynb (Qiskit/qiskit-tutor…
elberttl Aug 17, 2023
f1f24b9
Fix typos in 04_transpiler_passes_and_passmanager.ipynb (Qiskit/qiski…
Eric-Arellano Aug 21, 2023
94a5297
Update docs on max_execution_time (Qiskit/qiskit-ibm-runtime#1029)
jyu00 Aug 23, 2023
3d7d466
Use date on max_execution_time change (Qiskit/qiskit-ibm-runtime#1030)
jyu00 Aug 23, 2023
eecfa92
Merge remote-tracking branch 'qiskit-tutorials/master' into merge-tut…
Eric-Arellano Sep 8, 2023
5f4668f
Update tutorials with the latest builds
Eric-Arellano Aug 28, 2023
c7b3824
Move qiskit-tutorials into this repository (Qiskit/qiskit#10710)
mtreinish Sep 8, 2023
0a069d3
Open plan updates (Qiskit/qiskit-ibm-runtime#1105)
beckykd Sep 26, 2023
e0e0b39
Change qpu complex wording (Qiskit/qiskit-ibm-runtime#1109)
beckykd Sep 28, 2023
9196e18
Add IBM Cloud channel (Qiskit/qiskit-ibm-runtime#1113)
beckykd Sep 28, 2023
7273fac
Deprecate ``qiskit.extensions`` (Qiskit/qiskit#10725)
Cryoris Sep 29, 2023
2ed06d2
Fix approximation=True usage in opflow migration guide (Qiskit/qiskit…
rht Oct 4, 2023
a6d2479
Merge remote-tracking branch 'qiskit/main' into migrate-open-source-rnd2
Eric-Arellano Oct 9, 2023
aa67737
Merge remote-tracking branch 'runtime/main' into migrate-open-source-…
Eric-Arellano Oct 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions qiskit-docs/explanation/endianness.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#########################
Order of qubits in Qiskit
#########################

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.

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`.

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

.. math::

U = \begin{pmatrix} u_{00} & u_{01} \\ u_{10} & u_{11} \end{pmatrix}

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

.. math::

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}

while in a physics textbook it would be written as

.. math::

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}


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`.

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``.

Let's try this for a 3-qubit Quantum Fourier Transform (:class:`~.QFT`).

.. plot::
:include-source:
:context:

from qiskit.circuit.library import QFT

qft = QFT(3)
qft.decompose().draw('mpl')

.. plot::
:include-source:
:context: close-figs

qft.decompose().draw('mpl', reverse_bits=True)
78 changes: 78 additions & 0 deletions qiskit-docs/faq.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
.. _faq:

==========================
Frequently Asked Questions
==========================

**Q: How should I cite Qiskit in my research?**

**A:** Please cite Qiskit by using the included `BibTeX file
<https://github.com/Qiskit/qiskit-terra/blob/main/CITATION.bib>`__.

|

**Q: Why do I receive the error message** ``AttributeError: QuantumCircuit object has no attribute save_state``
**when using ``save_*``method on a circuit?**

**A:** The ``save_*`` instructions are part of Qiskit Aer project,
a high performance simulator for quantum circuits. These instructions do not
exist outside of Qiskit Aer and are added dynamically to the
:class:`~.QuantumCircuit` class by Qiskit Aer on import. If you would like to
use these instructions you must first ensure that you have imported
``qiskit_aer`` in your program before trying to call these methods. You
can refer to :mod:`qiskit_aer.library` for the details of these custom
instructions included with Qiskit Aer.

**Q: Why do my results from real devices differ from my results from the simulator?**

**A:** The simulator runs jobs as though is was in an ideal environment; one
without noise or decoherence. However, when jobs are run on the real devices
there is noise from the environment and decoherence, which causes the qubits
to behave differently than what is intended.

|

**Q: Why do I receive the error message,** ``No Module 'qiskit'`` **when using Jupyter Notebook?**

**A:** If you used ``pip install qiskit`` and set up your virtual environment in
Anaconda, then you may experience this error when you run a tutorial
in Jupyter Notebook. If you have not installed Qiskit or set up your
virtual environment, you can follow the :ref:`installation` steps.

The error is caused when trying to import the Qiskit package in an
environment where Qiskit is not installed. If you launched Jupyter Notebook
from the Anaconda-Navigator, it is possible that Jupyter Notebook is running
in the base (root) environment, instead of in your virtual
environment. Choose a virtual environment in the Anaconda-Navigator from the
**Applications on** dropdown menu. In this menu, you can see
all of the virtual environments within Anaconda, and you can
select the environment where you have Qiskit installed to launch Jupyter
Notebook.

|

**Q: Why am I getting a compilation error while installing ``qiskit``?**

**A:** Qiskit depends on a number of other open source Python packages, which
are automatically installed when doing ``pip install qiskit``. Depending on
your system's platform and Python version, it is possible that a particular
package does not provide pre-built binaries for your system. You can refer
to :ref:`platform_support` for a list of platforms supported by Qiskit, some
of which may need an extra compiler. In cases where there are
no precompiled binaries available ``pip`` will attempt to compile the package
from source, which in turn might require some extra dependencies that need to
be installed manually.

If the output of ``pip install qiskit`` contains similar lines to:

.. code:: sh

Failed building wheel for SOME_PACKAGE
...
build/temp.linux-x86_64-3.5/_openssl.c:498:30: fatal error
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

please check the documentation of the package that failed to install (in the
example code, ``SOME_PACKAGE``) for information on how to install the libraries
needed for compiling from source.
255 changes: 255 additions & 0 deletions qiskit-docs/how_to/use_sampler.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
###############################################################
Compute circuit output probabilities with ``Sampler`` primitive
###############################################################

This guide shows how to get the probability distribution of a quantum circuit with the :class:`~qiskit.primitives.Sampler` primitive.

.. note::

While this guide uses Qiskit’s reference implementation, the ``Sampler`` primitive can be run with any provider using :class:`~qiskit.primitives.BackendSampler`.

.. code-block::

from qiskit.primitives import BackendSampler
from <some_qiskit_provider> import QiskitProvider

provider = QiskitProvider()
backend = provider.get_backend('backend_name')
sampler = BackendSampler(backend)

There are some providers that implement primitives natively (see `this page <http://qiskit.org/providers/#primitives>`_ for more details).

Initialize quantum circuits
===========================

The first step is to create the :class:`~qiskit.circuit.QuantumCircuit`\ s from which you want to obtain the probability distribution.

.. plot::
:include-source:

from qiskit import QuantumCircuit

qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0,1)
qc.measure_all()
qc.draw("mpl")

.. testsetup::

# This code is repeated (but hidden) because we will need to use the variables with the extension sphinx.ext.doctest (testsetup/testcode/testoutput directives)
# and we can't reuse the variables from the plot directive above because they are incompatible.
# The plot directive is used to draw the circuit with matplotlib and the code is shown because of the include-source flag.

from qiskit import QuantumCircuit

qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0,1)
qc.measure_all()

.. note::

The :class:`~qiskit.circuit.QuantumCircuit` you pass to :class:`~qiskit.primitives.Sampler` has to include measurements.

Initialize the ``Sampler``
==========================

Then, you need to create a :class:`~qiskit.primitives.Sampler` instance.

.. testcode::

from qiskit.primitives import Sampler

sampler = Sampler()

Run and get results
===================

Now that you have defined your ``sampler``, you can run it by calling the :meth:`~qiskit.primitives.Sampler.run` method,
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)
with the :meth:`~qiskit.providers.JobV1.result` method.

.. testcode::

job = sampler.run(qc)
result = job.result()
print(result)

.. testoutput::

SamplerResult(quasi_dists=[{0: 0.4999999999999999, 3: 0.4999999999999999}], metadata=[{}])

While this example only uses one :class:`~qiskit.circuit.QuantumCircuit`, if you want to sample multiple circuits you can
pass a ``list`` of :class:`~qiskit.circuit.QuantumCircuit` instances to the :meth:`~qiskit.primitives.Sampler.run` method.

Get the probability distribution
--------------------------------

From these results you can extract the quasi-probability distributions with the attribute :attr:`~qiskit.primitives.SamplerResult.quasi_dists`.

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.
``result.quasi_dists[i]`` is the quasi-probability distribution of the ``i``-th circuit.

.. note::

A quasi-probability distribution differs from a probability distribution in that negative values are also allowed.
However the quasi-probabilities must sum up to 1 like probabilities.
Negative quasi-probabilities may appear when using error mitigation techniques.

.. testcode::

quasi_dist = result.quasi_dists[0]
print(quasi_dist)

.. testoutput::

{0: 0.4999999999999999, 3: 0.4999999999999999}

Probability distribution with binary outputs
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If you prefer to see the output keys as binary strings instead of decimal numbers, you can use the
:meth:`~qiskit.result.QuasiDistribution.binary_probabilities` method.

.. testcode::

print(quasi_dist.binary_probabilities())

.. testoutput::

{'00': 0.4999999999999999, '11': 0.4999999999999999}

Parameterized circuit with ``Sampler``
========================================

The :class:`~qiskit.primitives.Sampler` primitive can be run with unbound parameterized circuits like the one below.
You can also manually bind values to the parameters of the circuit and follow the steps
of the previous example.

.. testcode::

from qiskit.circuit import Parameter

theta = Parameter('θ')
param_qc = QuantumCircuit(2)
param_qc.ry(theta, 0)
param_qc.cx(0,1)
param_qc.measure_all()
print(param_qc.draw())

.. testoutput::

┌───────┐ ░ ┌─┐
q_0: ┤ Ry(θ) ├──■───░─┤M├───
└───────┘┌─┴─┐ ░ └╥┘┌─┐
q_1: ─────────┤ X ├─░──╫─┤M├
└───┘ ░ ║ └╥┘
meas: 2/══════════════════╩══╩═
0 1

The main difference from the previous case is that now you need to specify the sets of parameter values
for which you want to evaluate the expectation value as a ``list`` of ``list``\ s of ``float``\ s.
The ``i``-th element of the outer ``list`` is the set of parameter values
that corresponds to the ``i``-th circuit.

.. testcode::

import numpy as np

parameter_values = [[0], [np.pi/6], [np.pi/2]]

job = sampler.run([param_qc]*3, parameter_values=parameter_values)
dists = job.result().quasi_dists

for i in range(3):
print(f"Parameter: {parameter_values[i][0]:.5f}\t Probabilities: {dists[i]}")

.. testoutput::

Parameter: 0.00000 Probabilities: {0: 1.0}
Parameter: 0.52360 Probabilities: {0: 0.9330127018922194, 3: 0.0669872981077807}
Parameter: 1.57080 Probabilities: {0: 0.5000000000000001, 3: 0.4999999999999999}

Change run options
==================

Your workflow might require tuning primitive run options, such as the amount of shots.

By default, the reference :class:`~qiskit.primitives.Sampler` class performs an exact statevector
calculation based on the :class:`~qiskit.quantum_info.Statevector` class. However, this can be
modified to include shot noise if the number of ``shots`` is set.
For reproducibility purposes, a ``seed`` will also be set in the following examples.

There are two main ways of setting options in the :class:`~qiskit.primitives.Sampler`:

* Set keyword arguments in the :meth:`~qiskit.primitives.Sampler.run` method.
* Modify :class:`~qiskit.primitives.Sampler` options.

Set keyword arguments for :meth:`~qiskit.primitives.Sampler.run`
----------------------------------------------------------------

If you only want to change the settings for a specific run, it can be more convenient to
set the options inside the :meth:`~qiskit.primitives.Sampler.run` method. You can do this by
passing them as keyword arguments.

.. testcode::

job = sampler.run(qc, shots=2048, seed=123)
result = job.result()
print(result)

.. testoutput::

SamplerResult(quasi_dists=[{0: 0.5205078125, 3: 0.4794921875}], metadata=[{'shots': 2048}])

Modify :class:`~qiskit.primitives.Sampler` options
---------------------------------------------------

If you want to keep some configuration values for several runs, it can be better to
change the :class:`~qiskit.primitives.Sampler` options. That way you can use the same
:class:`~qiskit.primitives.Sampler` object as many times as you wish without having to
rewrite the configuration values every time you use :meth:`~qiskit.primitives.Sampler.run`.

Modify existing :class:`~qiskit.primitives.Sampler`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If you prefer to change the options of an already-defined :class:`~qiskit.primitives.Sampler`, you can use
:meth:`~qiskit.primitives.Sampler.set_options` and introduce the new options as keyword arguments.

.. testcode::

sampler.set_options(shots=2048, seed=123)

job = sampler.run(qc)
result = job.result()
print(result)

.. testoutput::

SamplerResult(quasi_dists=[{0: 0.5205078125, 3: 0.4794921875}], metadata=[{'shots': 2048}])

Define a new :class:`~qiskit.primitives.Sampler` with the options
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If you prefer to define a new :class:`~qiskit.primitives.Sampler` with new options, you need to
define a ``dict`` like this one:

.. testcode::

options = {"shots": 2048, "seed": 123}

And then you can introduce it into your new :class:`~qiskit.primitives.Sampler` with the
``options`` argument.

.. testcode::

sampler = Sampler(options=options)

job = sampler.run(qc)
result = job.result()
print(result)

.. testoutput::

SamplerResult(quasi_dists=[{0: 0.5205078125, 3: 0.4794921875}], metadata=[{'shots': 2048}])
Loading