-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add how-to guides about primitives #9716
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
Merged
1ucian0
merged 96 commits into
Qiskit:main
from
Guillermo-Mijares-Vilarino:add-primitive-guides
May 19, 2023
Merged
Changes from 83 commits
Commits
Show all changes
96 commits
Select commit
Hold shift + click to select a range
2f1e42f
Added how-to section
Guillermo-Mijares-Vilarino d39833c
added how to compose circuits guide
Guillermo-Mijares-Vilarino bb661fd
added how to visualize circuit guide
Guillermo-Mijares-Vilarino 59e06f9
add how to create parameterized circuit guide
Guillermo-Mijares-Vilarino 9c87269
Merge branch 'main' of https://github.com/Guillermo-Mijares-Vilarino/…
Guillermo-Mijares-Vilarino 109b049
Merge branch 'Qiskit:main' into add-how-tos
Guillermo-Mijares-Vilarino 63d6f81
add guides
Guillermo-Mijares-Vilarino 9cc7f76
Merge branch 'add-how-tos' of https://github.com/Guillermo-Mijares-Vi…
Guillermo-Mijares-Vilarino d28748a
removed latex circuit visualization
Guillermo-Mijares-Vilarino e7b2e47
fixed title underlines
Guillermo-Mijares-Vilarino 352a7c4
Fix typo
Guillermo-Mijares-Vilarino ba14c27
explicitly set cregbundle to False when appending circuit with classi…
Guillermo-Mijares-Vilarino 4cf2634
Fix typo
Guillermo-Mijares-Vilarino ed2115c
Added explanation about needed latex distribution
Guillermo-Mijares-Vilarino a7b38d8
added link to qiskit.visualization module
Guillermo-Mijares-Vilarino 861baaa
Simplified references
Guillermo-Mijares-Vilarino 9354aa0
Change 'we' to 'you'
Guillermo-Mijares-Vilarino 02298c6
Changed how_to.rst to how_to/index.rst
Guillermo-Mijares-Vilarino 0dbd593
Merge branch 'main' into add-how-tos
Guillermo-Mijares-Vilarino 6ad73b0
Removed jupyter-execute from create_a_quantum_circuit
Guillermo-Mijares-Vilarino 571db61
Removed jupyter-execute from create_a_parameterized_circuit
Guillermo-Mijares-Vilarino 2081f4a
Removed jupyter-execute from compose_quantum_circuits
Guillermo-Mijares-Vilarino 082b04e
Removed jupyter-execute from visualize_a_quantum_circuit
Guillermo-Mijares-Vilarino 205370c
Add sphinx.ext.doctest to conf.py
Guillermo-Mijares-Vilarino 84f98cf
Change header symbol for index and visualization guide
Guillermo-Mijares-Vilarino a6053bb
Added guides for sampler and estimator and simplified toctree
Guillermo-Mijares-Vilarino 3ac6be1
Fixed underline
Guillermo-Mijares-Vilarino 3b260e8
Add links to how-to create circuit
Guillermo-Mijares-Vilarino 5a3a74a
Added section about parameterized circuits to primitives how-tos
Guillermo-Mijares-Vilarino 5a30873
Remove other how-tos
Guillermo-Mijares-Vilarino 54071ee
Remove links
Guillermo-Mijares-Vilarino 89da1ac
Added reference to other implementations of primitives
Guillermo-Mijares-Vilarino 130f5ac
Update docs/how_to/use_estimator.rst
Guillermo-Mijares-Vilarino 8f83d18
Improved titles
Guillermo-Mijares-Vilarino 6daed2f
set global doctest flags and disable automatic doctests
Guillermo-Mijares-Vilarino ffbc8de
Added part about changing options
Guillermo-Mijares-Vilarino bc3da86
Merge branch 'main' into add-primitive-guides
Guillermo-Mijares-Vilarino 8fc6f30
Add new aer docs page to intersphinx
Guillermo-Mijares-Vilarino e9f7924
Added notes about circuit measurements
Guillermo-Mijares-Vilarino cdf6745
Improved notes about circuit measurements
Guillermo-Mijares-Vilarino 16977be
Merge branch 'main' into add-primitive-guides
Guillermo-Mijares-Vilarino 6f837cc
Update docs/how_to/use_estimator.rst
Guillermo-Mijares-Vilarino 9ad583a
Update docs/how_to/use_sampler.rst
Guillermo-Mijares-Vilarino 1dc2ef7
Update docs/how_to/use_sampler.rst
Guillermo-Mijares-Vilarino 25141f2
Update docs/how_to/use_sampler.rst
Guillermo-Mijares-Vilarino 9c92b80
Added numpy to intersphinx and note about doctest and plot directive
Guillermo-Mijares-Vilarino bbd57b1
Added note about quasi-probabilities
Guillermo-Mijares-Vilarino ce362e1
Merge branch 'main' into add-primitive-guides
HuangJunye 053b7c6
rename how to to How-to Guides
HuangJunye cf8def3
Update docs/how_to/use_estimator.rst
Guillermo-Mijares-Vilarino 1e1044c
Update docs/how_to/use_estimator.rst
Guillermo-Mijares-Vilarino 6a7c92c
Update docs/how_to/use_estimator.rst
Guillermo-Mijares-Vilarino 3d7ee62
Update docs/how_to/use_estimator.rst
Guillermo-Mijares-Vilarino 215a0a8
Update docs/how_to/use_estimator.rst
Guillermo-Mijares-Vilarino 919e316
Update docs/how_to/use_estimator.rst
Guillermo-Mijares-Vilarino f0dd1ab
Update docs/how_to/use_sampler.rst
Guillermo-Mijares-Vilarino 849396f
Update docs/how_to/use_estimator.rst
Guillermo-Mijares-Vilarino d514216
Update docs/how_to/use_sampler.rst
Guillermo-Mijares-Vilarino 645792f
Update docs/how_to/use_sampler.rst
Guillermo-Mijares-Vilarino 6d89eb0
Update docs/how_to/use_estimator.rst
Guillermo-Mijares-Vilarino f37e2e5
Update docs/how_to/use_estimator.rst
Guillermo-Mijares-Vilarino f27d7ee
Update docs/how_to/use_estimator.rst
Guillermo-Mijares-Vilarino 1d6e25a
Update docs/how_to/use_sampler.rst
Guillermo-Mijares-Vilarino bf39db2
Update docs/how_to/use_estimator.rst
Guillermo-Mijares-Vilarino bc24efe
Update docs/how_to/use_estimator.rst
Guillermo-Mijares-Vilarino 38ce964
Update docs/how_to/use_estimator.rst
Guillermo-Mijares-Vilarino 80213cc
Update docs/how_to/use_sampler.rst
Guillermo-Mijares-Vilarino 0e170c3
Update docs/how_to/use_sampler.rst
Guillermo-Mijares-Vilarino af5aceb
Update docs/how_to/use_sampler.rst
Guillermo-Mijares-Vilarino d653451
Update docs/how_to/use_sampler.rst
Guillermo-Mijares-Vilarino f46be30
Update docs/how_to/use_sampler.rst
Guillermo-Mijares-Vilarino 87de7c0
Update docs/how_to/use_sampler.rst
Guillermo-Mijares-Vilarino 0255ce8
Update docs/how_to/use_sampler.rst
Guillermo-Mijares-Vilarino 785bdd2
Remove generally
Guillermo-Mijares-Vilarino 47c4e27
Changed sampler initial note to mention BackendSampler
Guillermo-Mijares-Vilarino 18c90cc
Update docs/how_to/use_sampler.rst
Guillermo-Mijares-Vilarino 8906ea8
Added code example for backend primitives and link to providers page
Guillermo-Mijares-Vilarino e60b253
Merge branch 'main' into add-primitive-guides
Guillermo-Mijares-Vilarino 32ab2f3
Update intersphinx mapping to ecosystem for aer and runtime
Guillermo-Mijares-Vilarino 23fbd02
Merge branch 'main' into add-primitive-guides
Guillermo-Mijares-Vilarino f819ebe
Change rustworkx link
Guillermo-Mijares-Vilarino d4c5802
Merge branch 'main' into add-primitive-guides
HuangJunye c125f06
Merge branch 'main' into add-primitive-guides
Guillermo-Mijares-Vilarino b352958
Add "primitive" to estimator how-to title
Guillermo-Mijares-Vilarino 278df5a
Change hypothetical import to avoid confusion
Guillermo-Mijares-Vilarino e51e29e
Update docs/how_to/use_estimator.rst
Guillermo-Mijares-Vilarino 644d8c7
Include "primitive" word in sampler how-to title
Guillermo-Mijares-Vilarino 1607aac
Improve phrasing
Guillermo-Mijares-Vilarino 36d54f3
Include notice about doctest and plot directive incompatibility in sa…
Guillermo-Mijares-Vilarino 254c8de
change->modify sampler options
Guillermo-Mijares-Vilarino 3ee6158
change qiskit_provider to <some_qiskit_provider>
Guillermo-Mijares-Vilarino 004f4ad
shots->amount of shots in estimator how-to
Guillermo-Mijares-Vilarino 3d7e5a8
Add quick explanation about using multiple circuits and observables a…
Guillermo-Mijares-Vilarino 51a1260
singular
1ucian0 973f1e0
singular
1ucian0 9418b52
Merge branch 'main' into add-primitive-guides
1ucian0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| .. _how_to: | ||
|
|
||
| ############# | ||
| How-to Guides | ||
| ############# | ||
|
|
||
|
|
||
| Use the primitives | ||
| ================== | ||
|
|
||
| .. toctree:: | ||
| :maxdepth: 1 | ||
|
|
||
| use_sampler | ||
| use_estimator |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,265 @@ | ||
| ############################################### | ||
| Compute an expectation value with ``Estimator`` | ||
| ############################################### | ||
|
|
||
| This guide shows how to get the expected value of an observable for a given quantum circuit with the :class:`~qiskit.primitives.Estimator` primitive. | ||
|
|
||
| .. note:: | ||
|
|
||
| While this guide uses Qiskit’s reference implementation, the ``Estimator`` primitive can be run with any provider using :class:`~qiskit.primitives.BackendEstimator` . | ||
|
|
||
| .. code-block:: | ||
|
|
||
| from qiskit.primitives import BackendEstimator | ||
| from qiskit_provider import QiskitProvider | ||
|
Guillermo-Mijares-Vilarino marked this conversation as resolved.
Outdated
|
||
|
|
||
| provider = QiskitProvider() | ||
| backend = provider.get_backend('backend_name') | ||
| estimator = BackendEstimator(backend) | ||
|
|
||
| There are some providers that implement primitives natively (see `this page <http://qiskit.org/providers/#primitives>`_ for more details). | ||
|
|
||
|
|
||
| Initialize observable | ||
| ===================== | ||
|
|
||
| The first step is to define the observable whose expected value you want to compute. This observable can be any ``BaseOperator``, like the operators from :mod:`qiskit.quantum_info`. | ||
|
Guillermo-Mijares-Vilarino marked this conversation as resolved.
Outdated
|
||
| Among them it is preferable to use :class:`~qiskit.quantum_info.SparsePauliOp`. | ||
|
|
||
| .. testcode:: | ||
|
|
||
| from qiskit.quantum_info import SparsePauliOp | ||
|
|
||
| observable = SparsePauliOp(["II", "XX", "YY", "ZZ"], coeffs=[1, 1, -1, 1]) | ||
|
|
||
| Initialize quantum circuits | ||
|
1ucian0 marked this conversation as resolved.
Outdated
|
||
| =========================== | ||
|
|
||
| Then you need to create the :class:`~qiskit.circuit.QuantumCircuit` for which you want to obtain the expected value. | ||
|
|
||
| .. plot:: | ||
| :include-source: | ||
|
|
||
| from qiskit import QuantumCircuit | ||
|
|
||
| qc = QuantumCircuit(2) | ||
| qc.h(0) | ||
| qc.cx(0,1) | ||
| qc.draw("mpl") | ||
|
|
||
| .. testsetup:: | ||
|
|
||
|
HuangJunye marked this conversation as resolved.
|
||
| # 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) | ||
|
|
||
| .. note:: | ||
|
|
||
| The :class:`~qiskit.circuit.QuantumCircuit` you pass to :class:`~qiskit.primitives.Estimator` must not include any measurements. | ||
|
|
||
| Initialize the ``Estimator`` | ||
| ============================ | ||
|
|
||
| Then, you need to instantiate an :class:`~qiskit.primitives.Estimator`. | ||
|
|
||
| .. testcode:: | ||
|
|
||
| from qiskit.primitives import Estimator | ||
|
|
||
| estimator = Estimator() | ||
|
HuangJunye marked this conversation as resolved.
|
||
|
|
||
| Run and get results | ||
| =================== | ||
|
|
||
| Now that you have defined your ``estimator``, you can run your estimation by calling the :meth:`~qiskit.primitives.Estimator.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.EstimatorResult` object) | ||
| with the :meth:`~qiskit.providers.JobV1.result` method. | ||
|
|
||
| .. testcode:: | ||
|
|
||
| job = estimator.run(qc, observable) | ||
| result = job.result() | ||
| print(result) | ||
|
|
||
| .. testoutput:: | ||
|
|
||
| EstimatorResult(values=array([4.]), metadata=[{}]) | ||
|
|
||
| Get the expected value | ||
| ---------------------- | ||
|
|
||
| From these results you can extract the expected values with the attribute :attr:`~qiskit.primitives.EstimatorResult.values`. | ||
|
|
||
| :attr:`~qiskit.primitives.EstimatorResult.values` returns a :class:`numpy.ndarray` | ||
| whose ``i``-th element is the expectation value corresponding to the ``i``-th circuit and ``i``-th observable. | ||
|
|
||
| .. testcode:: | ||
|
|
||
| exp_value = result.values[0] | ||
| print(exp_value) | ||
|
|
||
|
Guillermo-Mijares-Vilarino marked this conversation as resolved.
|
||
| .. testoutput:: | ||
|
|
||
| 3.999999999999999 | ||
|
|
||
| Parameterized circuits with ``Estimator`` | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a how-to not a tutorial. Options in how-tos are fine |
||
| ========================================= | ||
|
|
||
| The :class:`~qiskit.primitives.Estimator` 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) | ||
| print(param_qc.draw()) | ||
|
|
||
| .. testoutput:: | ||
|
|
||
| ┌───────┐ | ||
| q_0: ┤ Ry(θ) ├──■── | ||
| └───────┘┌─┴─┐ | ||
| q_1: ─────────┤ X ├ | ||
| └───┘ | ||
|
|
||
| The main difference with 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 idea is that the ``i``-th element of the outer``list`` is the set of parameter values | ||
| that corresponds to the ``i``-th circuit and observable. | ||
|
|
||
| .. testcode:: | ||
|
|
||
| import numpy as np | ||
|
|
||
| parameter_values = [[0], [np.pi/6], [np.pi/2]] | ||
|
|
||
| job = estimator.run([param_qc]*3, [observable]*3, parameter_values=parameter_values) | ||
| values = job.result().values | ||
|
|
||
| for i in range(3): | ||
| print(f"Parameter: {parameter_values[i][0]:.5f}\t Expectation value: {values[i]}") | ||
|
|
||
| .. testoutput:: | ||
|
|
||
| Parameter: 0.00000 Expectation value: 2.0 | ||
| Parameter: 0.52360 Expectation value: 3.0 | ||
| Parameter: 1.57080 Expectation value: 4.0 | ||
|
|
||
| Change run options | ||
| ================== | ||
|
|
||
| Your workflow might require tuning primitive run options, such as shots. | ||
|
Guillermo-Mijares-Vilarino marked this conversation as resolved.
Outdated
|
||
|
|
||
| By default, the reference :class:`~qiskit.primitives.Estimator` 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.Estimator`: | ||
|
|
||
| * Set keyword arguments in the :meth:`~qiskit.primitives.Estimator.run` method. | ||
| * Modify :class:`~qiskit.primitives.Estimator` options. | ||
|
|
||
| Set keyword arguments for :meth:`~qiskit.primitives.Estimator.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.Estimator.run` method. You can do this by | ||
| passing them as keyword arguments. | ||
|
|
||
| .. testcode:: | ||
|
|
||
| job = estimator.run(qc, observable, shots=2048, seed=123) | ||
| result = job.result() | ||
| print(result) | ||
|
|
||
| .. testoutput:: | ||
|
|
||
| EstimatorResult(values=array([4.]), metadata=[{'variance': 3.552713678800501e-15, 'shots': 2048}]) | ||
|
|
||
| .. testcode:: | ||
|
|
||
| print(result.values[0]) | ||
|
|
||
| .. testoutput:: | ||
|
|
||
| 3.999999998697238 | ||
|
|
||
| Change :class:`~qiskit.primitives.Estimator` options | ||
|
Guillermo-Mijares-Vilarino marked this conversation as resolved.
Outdated
|
||
| ----------------------------------------------------- | ||
|
|
||
| If you want to keep some configuration values for several runs, it can be better to | ||
| change the :class:`~qiskit.primitives.Estimator` options. That way you can use the same | ||
| :class:`~qiskit.primitives.Estimator` object as many times as you wish without having to | ||
| rewrite the configuration values every time you use :meth:`~qiskit.primitives.Estimator.run`. | ||
|
|
||
| Modify existing :class:`~qiskit.primitives.Estimator` | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| If you prefer to change the options of an already-defined :class:`~qiskit.primitives.Estimator`, you can use | ||
| :meth:`~qiskit.primitives.Estimator.set_options` and introduce the new options as keyword arguments. | ||
|
|
||
| .. testcode:: | ||
|
|
||
| estimator.set_options(shots=2048, seed=123) | ||
|
|
||
| job = estimator.run(qc, observable) | ||
| result = job.result() | ||
| print(result) | ||
|
|
||
| .. testoutput:: | ||
|
|
||
| EstimatorResult(values=array([4.]), metadata=[{'variance': 3.552713678800501e-15, 'shots': 2048}]) | ||
|
|
||
| .. testcode:: | ||
|
|
||
| print(result.values[0]) | ||
|
|
||
| .. testoutput:: | ||
|
|
||
| 3.999999998697238 | ||
|
|
||
|
|
||
| Define a new :class:`~qiskit.primitives.Estimator` with the options | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| If you prefer to define a new :class:`~qiskit.primitives.Estimator` 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.Estimator` with the | ||
| ``options`` argument. | ||
|
|
||
| .. testcode:: | ||
|
|
||
| estimator = Estimator(options=options) | ||
|
|
||
| job = estimator.run(qc, observable) | ||
| result = job.result() | ||
| print(result) | ||
|
|
||
| .. testoutput:: | ||
|
|
||
| EstimatorResult(values=array([4.]), metadata=[{'variance': 3.552713678800501e-15, 'shots': 2048}]) | ||
|
|
||
| .. testcode:: | ||
|
|
||
| print(result.values[0]) | ||
|
|
||
| .. testoutput:: | ||
|
|
||
| 3.999999998697238 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.