Giter VIP home page Giter VIP logo

textbook's Introduction

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 quantum 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 them 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

textbook's People

Contributors

algh89110377 avatar amorphedstar avatar davisclarke avatar derwind avatar emilmagni avatar frankharkins avatar hannesgith avatar haplav avatar jari604 avatar kifumi avatar nasir26 avatar nikolabintev avatar nuvinga avatar omarcostahamido avatar psschwei avatar raghav-bell avatar vabarbosa avatar veenaiyuri avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

textbook's Issues

Improve documentation

New contributors

The textbook is a good starting point for first time GitHub contributors. We should make sure the contributing docs are clear & accurate.

  • Test on OSX users
  • Test on Windows users

Textbook-specific docs

We should document things that are non-standard, or might not be obvious to experienced users working on the textbook.

  • Add info on custom syntax for widgets

[Output issue] Shor's algorithm lab

Describe the problem?
In last cell of Shor's algorithm lab at https://learn.qiskit.org/course/ch-algorithms/shors-algorithm; running the last cell does not print out the expected guess value. It prints *** Non-trivial factor found: {guess} ***
image

Expected behavior
It would be nice to see the actual guess value as shown in the sample output in the lab
image

Proposed fix
Use python string formatting (ie print(f"*** Non-trivial factor found: {guess} ***")

Last circuit on Classical Computation on a Quantum Computer not displaying correctly

Where is the problem?
Classical Computing on a Quantum Computer - Qiskit Textbook
very near the bottom of the page.
Describe the problem
The output from the last interactive box produces an image different than the original image. In other words, running (Vf.inverse().compose(copy).compose(Vf)).draw() changes the circuit shown immediately below it. Instead it shows a circuit with only three qubits.

Expected behavior
The code should display a circuit with four qubits.

Screenshots

This is what you should see (and do immediately after reloading the page):

Screenshot 2023-08-16 at 2 21 52 PM

This is the result you get from running all the code on the page:
Screenshot 2023-08-16 at 2 22 48 PM

The AerSimulator is imported from incorrect package.

Where is the problem?
https://learn.qiskit.org/course/introduction/the-atoms-of-computation

Describe the problem
In the "Your first quantum circuit" section the package that is used to import the AerSimulator is incorrect.
Expected behavior
Instead of importing the AerSimulator from qiskit.providers.aer it should be imported from qiskit_aer. At least this is what works locally.

Tasks

Superdense coding text removal

Is your suggestion related to a problem? Please describe.
In Unit 1, Lesson 4 "Entanglement in Action", 5th paragraph of "Superdense coding," there is mention of the "complementary relationship with teleportation," and claims that further discussion will occur "at the end of the section". There is no further discussion on the topic.

Describe the solution you'd like
To delete paragraph 5 of "Superdense coding" in Lesson 4, Entanglement in action. That is, delete the text "Another reason why superdense coding... at the end of the section."

Describe alternatives you've considered
Later, the alluded to discussion may be added back in, but the above fix will do for now.

Additional context
Add any other context or screenshots about the feature request here.

image

Use this repo as content for Qiskit/platypus

To close this issue, we need to remove the textbook notebooks from the notebook folder in Qiskit/platypus and include this repo as a submodule in that folder. Will also need to modify the platypus converter to handle this.

EDIT: For content contributors, we also need an easy way of previewing pages locally.

Berstein-Vazirani: change "a" for "s" in 1.3 explanation

Is your suggestion related to a problem? Please describe.
In section 1.3 of Berstein-Vazirani there is an inconsistency when explaining the input to the oracle $f_s$, as in the second to last equation it shows as if we want to obtain $a$, even though this was referred to as the initial state, and the string we want to find out is rather $s$.

This in my opinion makes it a big hard to follow. In the examples that follow, again the string is referred as $s$ and not $a$.

Describe the solution you'd like
In the last two equations of section 1.3, the $a$'s should be changed for $s$'s.

Describe alternatives you've considered
NA

Textbook setup doesn't work on Windows

Describe the bug
When executing the command "install.sh" I get the error:
ERROR: The tar file (C:\Users\A400919\AppData\Local\Temp\pip-unpack-8e74pa24\pyscf-2.0.1.tar.gz) has a file (C:\Users\A400919\AppData\Local\Temp\pip-install-1y5k_ggc\pyscf_a7e51574f1e84b7e821cb21216ad8b29\pyscf/agf2/aux.py) trying to install outside target directory (C:\Users\A400919\AppData\Local\Temp\pip-install-1y5k_ggc\pyscf_a7e51574f1e84b7e821cb21216ad8b29)
Qiskit install

To Reproduce
Steps to reproduce the behavior:

  1. Go to Github/textbook
  2. Run script 'install.sh
  3. See error above

Expected behavior
The script doesn't terminate with success, throws an error instead

Desktop (please complete the following information):

  • OS: [e.g. iOS] Windows
  • Program versions: Python 3.9.1

Additional context
According to my first assessment this is due to the file aux.py which is an invalid filename on Windows

Support installation for Linux / Windows

We include some scripts to automatically set up the Python environment (see readme), but I've only tested it on OSX. To close this issue, we need confirmation that users can run the install script to set up their environment on:

  • Linux
    • Tested on Fedora
    • Works with Ubuntu (via GitHub action)
  • Windows

and include any OS-specific instructions .

TeX-error in QAOA-tutorial

Where is the problem?
[Please paste a link to the page.]
(https://learn.qiskit.org/course/ch-applications/solving-combinatorial-optimization-problems-using-qaoa)

Describe the problem
A clear and concise description of what is wrong and why.
There's a Tex-error on this page:
$x_3 = 0$
(search for '$')

Expected behavior
(Optional) Explain what you expect to see instead.
There are TeX-errors on many pages of the tutorial, I'll start reporting them as I go through the tutorial. This one is simple, but there are other sophisticated TeX-errors.

Screenshots
(Optional) If applicable, add screenshots to help explain your problem.

Additional context / references
If needed, I could directly correct those TeX-errors. However, I don't have the necessary rights.

Cell not running in "Atoms of Computation" chapter notebook

Where is the problem?
The issue is in "Quantum States" chapter in the "Atoms of Computation Section" under the "Adding with Qiskit" section.
Here is the link:

https://learn.qiskit.org/course/ch-states/the-atoms-of-computation#adding-qiskit

Relatedly, this can also be seen in input line 15 of the notebook:

https://github.com/Qiskit/textbook/blob/main/notebooks/ch-states/atoms-computation.ipynb

Describe the problem
When trying to run this cell, which includes the command "Assemble", the cell fails and the output returns the following error message:

Traceback (most recent call last):
  Cell In[59], line 2
    counts = sim.run(qobj).result().get_counts()
  File /opt/conda/lib/python3.10/site-packages/qiskit_aer/backends/aerbackend.py:196 in run
    return self._run_qobj(circuits, validate, parameter_binds, **run_options)
TypeError: AerBackend._run_qobj() got multiple values for argument 'parameter_binds'

Use %tb to get the full traceback.
/tmp/ipykernel_103/2201439370.py:2: DeprecationWarning: Using a qobj for run() is deprecated as of qiskit-aer 0.9.0 and will be removed no sooner than 3 months from that release date. Transpiled circuits should now be passed directly using `backend.run(circuits, **run_options).
  counts = sim.run(qobj).result().get_counts()

NOTE: this output is already different from the notebook linked above. In the above notebook link, the output is still displayed. However, when I ran it today on an IBM cloud notebook, there was no output and only the error message.

Expected behavior

This code should return a histogram plot instead of error.

Additional context / references

In order to get the expected behaviour, I followed the advice described in the DeprecationWarning and replaced the code in the original cell, ie

qobj = assemble(qc_ha)
counts = sim.run(qobj).result().get_counts()
plot_histogram(counts)

with

counts = sim.run(qc_ha).result().get_counts()
plot_histogram(counts)

Explain how to cite the textbook

We should make it clear how to cite the textbook. We did have a bibtex file a while ago, but I think a link to this repo is more appropriate now.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.