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

Update README.md to include sampler and estimator #10584

Merged
merged 55 commits into from
Aug 21, 2023
Merged
Changes from 53 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
4107156
Update README.md to include sampler and estimator
jaygambetta Aug 8, 2023
4a185e1
Update README.md
jaygambetta Aug 8, 2023
8a35507
Update README.md
jaygambetta Aug 8, 2023
b0b9b54
Update README.md
jaygambetta Aug 8, 2023
f6a1e51
Update README.md
jaygambetta Aug 8, 2023
fb6e69b
Update README.md
jaygambetta Aug 8, 2023
e9f2e6e
Update README.md
jaygambetta Aug 8, 2023
bd39ef3
Update README.md
jaygambetta Aug 8, 2023
6b4ad11
Update README.md
jaygambetta Aug 8, 2023
37c45b1
Update README.md
jaygambetta Aug 8, 2023
40148a4
Update README.md
jaygambetta Aug 8, 2023
970cc63
Update README.md
jaygambetta Aug 8, 2023
2da714f
Update README.md
jaygambetta Aug 8, 2023
b04c478
Update README.md
jaygambetta Aug 8, 2023
f697354
Update README.md
jaygambetta Aug 8, 2023
206ad1d
Update README.md
jaygambetta Aug 8, 2023
7ce7ed5
Update README.md
jaygambetta Aug 8, 2023
f0e95f1
Update README.md
jaygambetta Aug 8, 2023
45294cb
Update README.md
jaygambetta Aug 8, 2023
f22fdb0
Update README.md
jaygambetta Aug 8, 2023
2201e8b
Update README.md
jaygambetta Aug 8, 2023
834a03b
Update README.md
jaygambetta Aug 8, 2023
5ea063e
Update README.md
jaygambetta Aug 8, 2023
cd0ba9f
Update README.md
jaygambetta Aug 8, 2023
34f2dea
Update README.md
jaygambetta Aug 8, 2023
a42a5aa
Update README.md
jaygambetta Aug 8, 2023
541170c
Update README.md
jaygambetta Aug 8, 2023
c8bb7e9
Update README.md
jaygambetta Aug 8, 2023
43d0478
Update README.md
jaygambetta Aug 8, 2023
e06c30e
Update README.md
jaygambetta Aug 8, 2023
9811d34
Update README.md
jaygambetta Aug 8, 2023
4fcfc98
Update README.md
jaygambetta Aug 8, 2023
cf857b7
Update README.md
jaygambetta Aug 8, 2023
14f0f11
Update README.md
jaygambetta Aug 8, 2023
0d9b380
Update README.md
jaygambetta Aug 8, 2023
c7f10bd
Update README.md
jaygambetta Aug 8, 2023
ddeab4b
Update README.md
jaygambetta Aug 8, 2023
8730b93
Update README.md
jaygambetta Aug 9, 2023
cd0b0bb
Update README.md
jaygambetta Aug 9, 2023
924d6b2
Update README.md
jaygambetta Aug 9, 2023
e1d9ba6
Update README.md
jaygambetta Aug 9, 2023
dd4b564
Update README.md
jaygambetta Aug 9, 2023
6867538
Update README.md
jaygambetta Aug 9, 2023
75ccf69
Update README.md
jaygambetta Aug 9, 2023
21fbaa7
Update README.md
jaygambetta Aug 9, 2023
8afefbc
Update README.md
jaygambetta Aug 9, 2023
79f386c
Update README.md
jaygambetta Aug 9, 2023
6ac3ad0
Update README.md
jaygambetta Aug 10, 2023
e679cdb
Update README.md
jaygambetta Aug 10, 2023
a3c20e3
Apply suggestions from code review
blakejohnson Aug 11, 2023
443ced0
Apply suggestions from code review
blakejohnson Aug 11, 2023
de7ae3e
Merge branch 'main' into jaygambetta-patch-1
1ucian0 Aug 16, 2023
bae8154
new example
1ucian0 Aug 16, 2023
0d6bc53
Update README.md
1ucian0 Aug 16, 2023
e42821f
Update README.md
blakejohnson Aug 16, 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
101 changes: 66 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
[![Downloads](https://pepy.tech/badge/qiskit-terra)](https://pypi.org/project/qiskit-terra/)<!--- long-description-skip-end -->
[![DOI](https://zenodo.org/badge/161550823.svg)](https://zenodo.org/badge/latestdoi/161550823)

**Qiskit** is an open-source framework for working with noisy quantum computers at the level of pulses, circuits, and algorithms.
**Qiskit** is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.

This framework allows for building, transforming, and visualizing quantum circuits. It also contains a compiler that supports
different quantum computers and a common interface for running programs on different quantum computer architectures.
This library is the core component of Qiskit, which contains the building blocks for creating and working with quantum circuits, quantum operators, and primitive functions (sampler and estimator).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This library is the core component of Qiskit, which contains the building blocks for creating and working with quantum circuits, quantum operators, and primitive functions (sampler and estimator).
This library is the core component of Qiskit, which contains the building blocks for creating and working with quantum circuits, quantum operators, and primitive functions (Sampler and Estimator).

It also contains a transpiler that supports optimizing quantum circuits and a quantum information toolbox for creating advanced quantum operators.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any chance at least one of these "quantum"s in this sentence isn't necessary?


For more details on how to use Qiskit you can refer to the documentation located here:
For more details on how to use Qiskit, refer to the documentation located here:

<https://qiskit.org/documentation/>

Expand All @@ -30,61 +30,92 @@ Pip will handle all dependencies automatically and you will always install the l

To install from source, follow the instructions in the [documentation](https://qiskit.org/documentation/contributing_to_qiskit.html#install-install-from-source-label).

## Creating Your First Quantum Program in Qiskit
## Create your first quantum program in Qiskit

Now that Qiskit is installed, it's time to begin working with Qiskit. To do this
we create a `QuantumCircuit` object to define a basic quantum program.
Now that Qiskit is installed, it's time to begin working with Qiskit. The essential parts of a quantum program are:
1. Define and build a quantum circuit that represents the quantum state
2. Define the classical output by measurements or a set of observable operators
3. Depending on the output, use the primitive function `sampler` to sample outcomes or the `estimator` to estimate values.

Create an example quantum circuit using the `QuantumCircuit` class:

```python
import numpy as np
from qiskit import QuantumCircuit
qc = QuantumCircuit(2, 2)
qc.h(0)
qc.cx(0, 1)
qc.measure([0,1], [0,1])

# 1. A quantum circuit for preparing the quantum state |000> + i |111>
qc_example = QuantumCircuit(3)
qc_example.h(0) # generate superpostion
qc_example.p(np.pi/2,0) # add quantum phase
qc_example.cx(0,1) # 0th-qubit-Controlled-NOT gate on 1st qubit
qc_example.cx(0,2) # 0th-qubit-Controlled-NOT gate on 2nd qubit
```

This example makes an entangled state, also called a [Bell state](https://en.wikipedia.org/wiki/Bell_state).
This simple example makes an entangled state known as a [GHZ state](https://en.wikipedia.org/wiki/Greenberger%E2%80%93Horne%E2%80%93Zeilinger_state) $(|000\rangle + |111\rangle)/\sqrt{2}$. It uses the standard quantum gates: Hadamard gate (`h`), Phase gate (`p`), and CNOT gate (`cx`).

Once you've made your first quantum circuit, you can then simulate it.
To do this, first we need to compile your circuit for the target backend we're going to run
on. In this case we are leveraging the built-in `BasicAer` simulator. However, this
simulator is primarily for testing and is limited in performance and functionality (as the name
implies). You should consider more sophisticated simulators, such as [`qiskit-aer`](https://github.com/Qiskit/qiskit-aer/),
for any real simulation work.
Once you've made your first quantum circuit, choose which primitive function you will use. Starting with `sampler`,
we use `measure_all(inplace=False)` to get a copy of the circuit in which all the qubits are measured:

```python
from qiskit import transpile
from qiskit.providers.basicaer import QasmSimulatorPy
backend_sim = QasmSimulatorPy()
transpiled_qc = transpile(qc, backend_sim)
# 2. Add the classical output to be measurement in a different circuit
1ucian0 marked this conversation as resolved.
Show resolved Hide resolved
qc_measured = qc_example.measure_all(inplace=False)

# 3. Execute using the Sampler primitive
from qiskit.primitives.sampler import Sampler
sampler = Sampler()
job = sampler.run(qc_measured, shots=1000)
result = job.result()
print(f" > Quasi probability distribution: {result.quasi_dists}")
```

After compiling the circuit we can then run this on the ``backend`` object with:
Running this will give an outcome similar to `{0: 0.497, 7: 0.503}` which is `000` 50% of the time and `111` 50% of the time up to statistical fluctuations.
Copy link
Member

@1ucian0 1ucian0 Aug 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, sampler.run(qc_compose) will give the probability directly ({0: 0.5, 7: 0.5}). On one hand, this avoids the statistical fluctuation and does not introduce shots. On the other, it might distort the idea of a sample.

If shots stays, I would suggest something like:

Suggested change
Running this will give an outcome similar to `{0: 0.497, 7: 0.503}` which is `000` 50% of the time and `111` 50% of the time up to statistical fluctuations.
Running this will run the computation 1000 times and sample the output to produce `{0: 0.497, 7: 0.503}` or similar, meaning that the result was `000` 50% of the time and `111` the other 50% of the shots.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want sampler.run(qc_compose) to produce an error or just {} as there are no measurements to sample from.

Copy link
Member

@1ucian0 1ucian0 Aug 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My comment is about shots.

If a circuit in sampler.run(circuit) does have measurements, it currently raises: Some classical bits are not used for measurements.

Currently, the reference implementation does:

  • circuit has no classical bits: raises with circuit does not have any classical bit
  • circuit has a classical bit: raises with but they are not all measured: Some classical bits are not used for measurements.

I think a case for {} is reasonable, at least initially. Would you like discuss this in depth in an issue?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, let's move this discussion to an issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I also think we should leave the text as is.

Copy link
Member

@1ucian0 1ucian0 Aug 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

circuit has a classical bit: raises with but they are not all measured: Some classical bits are not used for measurements.

This should be a warning instead of an exception. If no measured, {"0":1} is the result, as the assuption is that classical bits are initialed with zero (document that assumption).

Let's continue in #10706

To illustrate the power of Estimator, we now use the quantum information toolbox to create the operator $XXY+XYX+YXX-YYY$ and pass it to the `run()` function, along with our quantum circuit. Note the Estimator requires a circuit _**without**_ measurement, so we use the `qc_example` circuit we created earlier.

```python
result = backend_sim.run(transpiled_qc).result()
print(result.get_counts(qc))
# 2. define the observable to be measured
from qiskit.quantum_info import SparsePauliOp
operator = SparsePauliOp.from_list([("XXY", 1), ("XYX", 1), ("YXX", 1), ("YYY", -1)])

# 3. Execute using the Estimator primitive
from qiskit.primitives import Estimator
estimator = Estimator()
job = estimator.run(qc_example, operator, shots=1000)
result = job.result()
print(f" > Expectation values: {result.values}")
```

The output from this execution will look similar to this:
Running this will give the outcome `4`. For fun, try to assign a value of +/- 1 to each single-qubit operator X and Y
and see if you can achieve this outcome. (Spoiler alert: this is not possible!)

Using the Qiskit-provided `qiskit.primitives.Sampler` and `qiskit.primitives.Estimator` will not take you very far. The power of quantum computing cannot be simulated
on classical computers and you need to use real quantum hardware to scale to larger quantum circuits. However, running a quantum
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
on classical computers and you need to use real quantum hardware to scale to larger quantum circuits. However, running a quantum
on classical computers; you need real quantum hardware to scale to larger quantum circuits. However, running a quantum

circuit on hardware requires rewriting them to the basis gates and connectivity of the quantum hardware.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
circuit on hardware requires rewriting them to the basis gates and connectivity of the quantum hardware.
circuit on hardware requires rewriting to the basis gates and connectivity of the quantum hardware.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(the "them" was a little ambiguous but reads ok when just removing it)

The tool that does this is the [transpiler](https://qiskit.org/documentation/apidoc/transpiler.html)
and Qiskit includes transpiler passes for synthesis, optimization, mapping, and scheduling. However, it also includes a
default compiler which works very well in most examples. The following code will map the example circuit to the `basis_gates = ['cz', 'sx', 'rz']` and a linear chain of qubits $0 \rightarrow 1 \rightarrow 2$ with the `coupling_map =[[0, 1], [1, 2]]`.

```python
{'00': 513, '11': 511}
from qiskit import transpile
qc_transpiled = transpile(qc_example, basis_gates = ['cz', 'sx', 'rz'], coupling_map =[[0, 1], [1, 2]] , optimization_level=3)
```

For further examples of using Qiskit you can look at the tutorials in the documentation here:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For further examples of using Qiskit you can look at the tutorials in the documentation here:
For further examples, visit the tutorials in the documentation here:


<https://qiskit.org/documentation/tutorials.html>

### Executing your code on a real quantum chip
### Executing your code on real quantum hardware

Qiskit provides an abstraction layer that lets users run quantum circuits on hardware from any vendor that provides a compatible interface.
The best way to use Qiskit is with a runtime environment that provides optimized implementations of `sampler` and `estimator` for a given hardware platform. This runtime may involve using pre- and post-processing, such as optimized transpiler passes with error suppression, error mitigation, and, eventually, error correction built in. A runtime implements `qiskit.primitives.BaseSampler` and `qiskit.primitives.BaseEstimator` interfaces. For example,
some packages that provide implementations of a runtime primitive implementation are:

* https://github.com/Qiskit/qiskit-ibm-runtime

You can also use Qiskit to execute your code on a **real quantum processor**.
Qiskit provides an abstraction layer that lets users run quantum circuits on hardware from any
vendor that provides an interface to their systems through Qiskit. Using these ``providers`` you can run any Qiskit code against
real quantum computers. Some examples of published provider packages for running on real hardware are:
Qiskit also provides a lower-level abstract interface for describing quantum backends. This interface, located in
``qiskit.providers``, defines an abstract `BackendV2` class that providers can implement to represent their
hardware or simulators to Qiskit. The backend class includes a common interface for executing circuits on the backends; however, in this interface each provider may perform different types of pre- and post-processing and return outcomes that are vendor-defined. Some examples of published provider packages that interface with real hardware are:

* https://github.com/Qiskit/qiskit-ibmq-provider
* https://github.com/Qiskit-Partners/qiskit-ionq
* https://github.com/Qiskit/qiskit-ibm-provider
* https://github.com/qiskit-community/qiskit-ionq
* https://github.com/Qiskit-Partners/qiskit-aqt-provider
blakejohnson marked this conversation as resolved.
Show resolved Hide resolved
* https://github.com/qiskit-community/qiskit-braket-provider
* https://github.com/qiskit-community/qiskit-quantinuum-provider
Expand Down