Go to file
Matthew Treinish 8982dbde15
Fully port FilterOpNodes to Rust (#13052)
* Fully port FilterOpNodes to Rust

This commit ports the FilterOpNodes pass to rust. This pass is
exceedingly simple and just runs a filter function over all the op
nodes and removes nodes that match the filter. However, the API for
the class exposes that filter function interface as a user provided
Python callable. So for the current pass we need to retain that python
callback. This limits the absolute performance of this pass because
we're bottlenecked by calling python.

Looking to the future, this commit adds a rust native method to
DAGCircuit to perform this filtering with a rust predicate FnMut. It
isn't leveraged by the python implementation because of layer mismatch
for the efficient rust interface and Python working with `DAGOpNode`
objects. A function using that interface is added to filter labeled
nodes. In the preset pass manager we only use FilterOpNodes to remove
nodes with a specific label (which is used to identify temporary
barriers created by qiskit). In a follow up we should consider
leveraging this new function to build a new pass specifically for
this use case.

Fixes #12263
Part of #12208

* Make filter_op_nodes() infallible

The filter_op_nodes() method originally returned a Result<()> to handle
a predicate that was fallible. This was because the original intent for
the method was to use it with Python callbacks in the predicate. But
because of differences between the rust API and the Python API this
wasn't feasible as was originally planned. So this Result<()> return
wasn't used anymore. This commit reworks it to make the
filter_op_nodes() infallible and the predicate a user provides also only
returns `bool` and not `Result<bool>`.

* Rename filter_labelled_op to filter_labeled_op
2024-09-05 23:00:08 +00:00
.azure Remove legacy-editable option from setuptools builds (#13059) 2024-08-30 14:25:27 +00:00
.binder Spellcheck Done [Unitary Hack 2024] (#12501) 2024-06-19 15:50:03 +00:00
.cargo Fix Rustfmt and clippy with latest rust stable (#12339) 2024-05-06 19:08:19 +00:00
.github Remove legacy-editable option from setuptools builds (#13059) 2024-08-30 14:25:27 +00:00
crates Fully port FilterOpNodes to Rust (#13052) 2024-09-05 23:00:08 +00:00
docs Bump main branch version post 1.2 branch (#12815) 2024-07-25 16:47:38 +00:00
examples Rename `BasicAer` to `BasicProvider` and refactor interface to follow `BackendV2` (#11422) 2024-01-30 22:53:27 +00:00
qiskit Fully port FilterOpNodes to Rust (#13052) 2024-09-05 23:00:08 +00:00
releasenotes Restrict `Split2QUnitaries` to run on `UnitaryGate` (#13095) 2024-09-05 16:34:48 +00:00
test Restrict `Split2QUnitaries` to run on `UnitaryGate` (#13095) 2024-09-05 16:34:48 +00:00
tools Oxidize commutation checker (#12870) 2024-09-03 13:37:57 +00:00
.clippy.toml Use hashbrown::HashSet instead of ahash::HashSet (#12951) 2024-08-13 16:41:26 +00:00
.editorconfig
.git-blame-ignore-revs Ignore bump to `black~=24.1` in `git blame` (#11714) 2024-02-02 02:22:57 +00:00
.gitignore Add GitHub Actions documentation-deployment pipeline (#10610) 2023-08-14 19:35:14 +00:00
.local-spellings
.mailmap
.mergify.yml Bump main branch version post 1.2 branch (#12815) 2024-07-25 16:47:38 +00:00
.stestr.conf
CITATION.bib Update CITATION.bib file to point to Qiskit whitepaper (#12415) 2024-05-22 15:12:23 +00:00
CODE_OF_CONDUCT.md Point Code of Conduct to the docs site (#12786) 2024-07-22 08:06:16 +00:00
CONTRIBUTING.md Update docs links for new URL structure (#12896) 2024-08-02 20:53:09 +00:00
Cargo.lock Oxidize commutation checker (#12870) 2024-09-03 13:37:57 +00:00
Cargo.toml Bump indexmap from 2.4.0 to 2.5.0 (#13068) 2024-09-02 12:14:16 +00:00
DEPRECATION.md Update docs links for new URL structure (#12896) 2024-08-02 20:53:09 +00:00
LICENSE.txt
MAINTAINING.md quantum-computing.ibm.com is going to disappear, eventually (#11830) 2024-02-19 14:52:02 +00:00
MANIFEST.in Remove provider-specific fake backends,`FakeProvider` class and related tools in 1.0 (#11376) 2024-02-01 19:27:05 -05:00
Makefile
README.md Update docs links for new URL structure (#12896) 2024-08-02 20:53:09 +00:00
SECURITY.md Update docs links for new URL structure (#12896) 2024-08-02 20:53:09 +00:00
asv.conf.json Fix asv installation command (#11758) 2024-02-08 21:43:05 +00:00
azure-pipelines.yml Reconfigure CI pipelines for PR and merge queue (#11526) 2024-01-17 18:30:34 +00:00
constraints.txt Fix pass-manager drawing tests for pydot 3.0 (#12775) 2024-07-16 13:24:21 +00:00
pyproject.toml Adding MCX synthesis plugins (#12961) 2024-08-22 10:06:09 +00:00
qiskit_bot.yaml Remove Eric from Rust bot notifications (#12596) 2024-06-18 09:12:04 +00:00
requirements-dev.txt Bump pylint to 3.2.2 (#12520) 2024-06-06 20:53:46 +00:00
requirements-optional.txt Add infrastructure for gates, instruction, and operations in Rust (#12459) 2024-06-13 10:48:40 +00:00
requirements.txt Use rustworkx 0.15.0 features in DAGCircuit.remove_op_node (#12756) 2024-07-11 11:58:47 +00:00
rust-toolchain.toml Bump MSRV to 1.70 (#11493) 2024-01-05 17:22:33 +00:00
setup.py Spellcheck Done [Unitary Hack 2024] (#12501) 2024-06-19 15:50:03 +00:00
tox.ini Allow overriding Rust toolchain in `tox` runs (#12827) 2024-07-26 21:03:52 +00:00

README.md

Qiskit

License Current Release Extended Support Release Downloads Coverage Status PyPI - Python Version Minimum rustc 1.70 Downloads DOI

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

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

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

https://docs.quantum.ibm.com/

Installation

[!WARNING] Do not try to upgrade an existing Qiskit 0.* environment to Qiskit 1.0 in-place. Read more.

We encourage installing Qiskit via pip:

pip install qiskit

Pip will handle all dependencies automatically and you will always install the latest (and well-tested) version.

To install from source, follow the instructions in the documentation.

Create your first quantum program in Qiskit

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:

import numpy as np
from qiskit import QuantumCircuit

# 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 simple example makes an entangled state known as a GHZ state (|000\rangle + i|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, 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:

# 2. Add the classical output in the form of measurement of all qubits
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}")

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

# 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}")

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 circuit on hardware requires rewriting to the basis gates and connectivity of the quantum hardware. The tool that does this is the transpiler, 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]].

from qiskit import transpile
qc_transpiled = transpile(qc_example, basis_gates = ['cz', 'sx', 'rz'], coupling_map =[[0, 1], [1, 2]] , optimization_level=3)

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:

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:

You can refer to the documentation of these packages for further instructions on how to get access and use these systems.

Contribution Guidelines

If you'd like to contribute to Qiskit, please take a look at our contribution guidelines. By participating, you are expected to uphold our code of conduct.

We use GitHub issues for tracking requests and bugs. Please join the Qiskit Slack community for discussion, comments, and questions. For questions related to running or using Qiskit, Stack Overflow has a qiskit. For questions on quantum computing with Qiskit, use the qiskit tag in the Quantum Computing Stack Exchange (please, read first the guidelines on how to ask in that forum).

Authors and Citation

Qiskit is the work of many people who contribute to the project at different levels. If you use Qiskit, please cite as per the included BibTeX file.

Changelog and Release Notes

The changelog for a particular release is dynamically generated and gets written to the release page on Github for each release. For example, you can find the page for the 0.46.0 release here:

https://github.com/Qiskit/qiskit/releases/tag/0.46.0

The changelog for the current release can be found in the releases tab: Releases The changelog provides a quick overview of notable changes for a given release.

Additionally, as part of each release, detailed release notes are written to document in detail what has changed as part of a release. This includes any documentation on potential breaking changes on upgrade and new features. See all release notes here.

Acknowledgements

We acknowledge partial support for Qiskit development from the DOE Office of Science National Quantum Information Science Research Centers, Co-design Center for Quantum Advantage (C2QA) under contract number DE-SC0012704.

License

Apache License 2.0