Get started with Qiskit in the classroom
דף זה טרם תורגם. התוכן מוצג באנגלית.
For this Qiskit in Classrooms module, students must have a working Python environment with the following packages installed:
qiskitv2.1.0 or newerqiskit-ibm-runtimev0.40.1 or newerqiskit-aerv0.17.0 or newerqiskit.visualizationnumpypylatexenc
To set up and install the packages above, see the Install Qiskit guide. In order to run jobs on real quantum computers, students will need to set up an account with IBM Quantum® by following the steps in the Set up your IBM Cloud® account guide.
This module was tested and used 2 seconds of QPU time on a Heron v2 processor. This is an estimate only. Your actual usage may vary.
# Uncomment and modify this line as needed to install dependencies
#!pip install 'qiskit>=2.1.0' 'qiskit-ibm-runtime>=0.40.1' 'qiskit-aer>=0.17.0' 'numpy' 'pylatexenc'
Introduction
In the Qiskit in the Classroom modules, you'll have the opportunity to use a quantum computer to explore various concepts in quantum computing-adjacent fields such as quantum mechanics, computer science, chemistry, and more. This module serves as a prerequisite to the others - it introduces the fundamentals of quantum computing and how to use Qiskit to run quantum circuits.
First we'll give you a brief overview of how a classical computer works, then we'll show you how these concepts are adapted to fit the quantum computing paradigm. Finally, we'll show you how to put these concepts together to build and execute your first quantum circuit.
Classical computers
You're likely familiar with the basics of how classical computers work, but here we'll highlight a few of the key features so that we can then draw a comparison to quantum computers.
The basic units of information: bits
Classical computers process classical information, and the fundamental unit of classical information is the bit. A single bit can store the answer to one "yes/no" question. We usually represent the two binary states of a bit as "0" and "1".
Review of binary numbers
Combining bits enables you to store more information. For example, if you want to store a number from 0 to 15, you could do so with four bits in the following way:
| 0 = 0000 | 4 = 0100 | 8 = 1000 | 12 = 1100 |
| 1 = 0001 | 5 = 0101 | 9 = 1001 | 13 = 1101 |
| 2 = 0010 | 6 = 0110 | 10 = 1010 | 14 = 1110 |
| 3 = 0011 | 7 = 0111 | 11 = 1011 | 15 = 1111 |
In general, to convert from a binary number of bits to a familiar, base-10 number, you multiply the least-significant (rightmost) bit by , the next bit to the left by , then the next by , and so on, until you reach the most significant (leftmost bit), which you multiply .
So, that means that bits can be in one of different possible states.
Check your understanding
Read the question(s) below, think about your answer, then click the triangle to reveal the solution.
How many bits would you need to represent the number 86? Write out the bitstring that encodes this number in binary.
Answer:
Remember, bits allows you to represent the numbers through , so using six bits would get us up to . That's not quite enough. We add one more bit to get up to . Now let's break 86 down into powers of 2:
Fundamental operations: gates
Now, a computer needs to be able to do something with the bits in order to, well, compute. Binary gates are the operations that form the fundamental building blocks of all the more complicated algorithms and codes.
Single-bit gate:
NOT
When you have only one bit, there's only one way you can transform its state: flip the state from 0 to 1 or from 1 to 0. We call this the "NOT" gate. The effect of this gate — and the rest of the gates we'll discuss below — can be represented in a so-called "truth table," with columns for the input and output states of the qubits. The truth table for the NOT gate is:
| Input | Output |
|---|---|
| 0 | 1 |
| 1 | 0 |
Multi-bit gates:
AND
AND is a two-bit gate that takes two input bits and outputs a single bit. It outputs 1 if both input bits are 1, and 0 otherwise:
| Input | Output |
|---|---|
| 00 | 0 |
| 01 | 0 |
| 10 | 0 |
| 11 | 1 |
OR
OR is another two-bit gate with a single output bit. It outputs 1 if either of the bits are 1:
| Input | Output |
|---|---|
| 00 | 0 |
| 01 | 1 |
| 10 | 1 |
| 11 | 1 |
XOR
XOR stands for "exclusive OR" and it is like the OR gate, but outputs 1 if only one of the input bits are 1. It outputs 0 if they are either both 1 or both 0:
| Input | Output |
|---|---|
| 00 | 0 |
| 01 | 1 |
| 10 | 1 |
| 11 | 0 |
Measurements:
Typically, when learning about classical computing, not much attention is paid to the process of reading out the state of the bits. This is because it's not very complex from a conceptual perspective. You can measure the bits at anytime before, during, or after a computation, and it doesn't affect the outcome. This is not the case in quantum computing, as we will discuss below.
Circuits:
By combining the gates above, you can do any sort of operation you want on a computer. Let's take a simple example: Using the AND and XOR gates, you can construct the half-adder circuit, which calculates the sum of two bits. This is represented in a logical circuit diagram, where the wires represent the bits and the gates operating on the bits are shown as symbols on the corresponding wires:
So, the two bits are copied and fed through both an AND gate and an XOR gate. The result of the XOR gate is the "sum bit" (S), which remains in the ones place of the binary number, and the result of the AND gate is the "carry bit" (C), which is the value of the next most significant digit in the binary number. Here is the truth table:
| Sum () | Carry () | ||
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
Check your understanding
Read the question(s) below, think about your answer, then click the triangle to reveal the solution.
Verify that the above truth table yields the correct solution for an adder circuit. That is, for each of the four options of A and B, verify that .
Answer:
Quantum computers
Bits qubits
Just as bits are the fundamental units of classical information, quantum bits, or "qubits," are the fundamental units of quantum information. Like the classical bit, the state of a qubit can be either 0 or 1, which we typically denote as and . But unlike the classical bit, a quantum bit can also be in a superposition of both the state and the state at the same time. In general, a qubit can be in any state of the form:
where and are complex amplitudes with .
The quantum phase
Because and are complex, they can each be written as where is called the phase. If we multiply the whole state by the same overall phase factor, nothing physically changes — this is called a global phase, and it has no observable consequences.
For that reason, it is conventional to “factor out” , giving:
where is the relative phase of the quantum state, which does have observable consequences.
This phase plays a very important role in quantum computing, and you will explore its various consequences in the subsequent Qiskit in the Classroom modules.
Multiple qubits
While the state of multiple bits could simply be expressed as a string of 0s and 1s, the state of multiple qubits gets a little more complicated because of the principles of superposition and entanglement.
Recall that bits could be in one of possible states ranging from binary numbers 000...000 to 111...111. But now, because of the principle of superposition, qubits can be in a superposition of all of these states at once!
This can be expressed as
where, as in the classical case, the state corresponds to the state where each qubit is in the right combination of 0s and 1s to yield the binary number . These are known as the "computational basis states" of the quantum system. For example, a three-qubit state can be written as a superposition of its eight computational basis states:
Each qubit in the system is denoted with an index through . The convention is to read the qubit states from right to left, so that the state of qubit is the rightmost state and the state of qubit is the leftmost. This is known as "little-endian" notation, and it may seem counterintuitive at first, since we're used to reading left to right.
Check your understanding
Read the question(s) below, think about your answer, then click the triangle to reveal the solution.
At first glance, it may seem counterintuitive to order the qubits from right to left as in little-endian notation, but it's actually a very logical thing to do! Explain why. (Recall our discussion above of binary to base-10 number conversion.)
Answer:
If we order qubits from right to left, so that qubit 0 is the farthest right and qubit N-1 is the farthest left, It is logical to associate qubit with the least-significant bit, which is multiplied by and qubit with the most-significant bit, which is multiplied by .
Entanglement
As we mentioned earlier, another key feature of qubits is that they can be entangled with one another. Let's take an example of a two-qubit state, where and :
So, the state of qubit 0 can be either or with equal probability, and same with the state of qubit 1. But these probabilities are no longer independent of one another. If we find the state of qubit 0 to be , then we know that qubit 1 will also be in . This is true no matter how far apart one another, which is why the act of measuring an entangled state is sometimes referred to as "spooky action at a distance."
Entanglement can also take other forms. For example, the state
produces opposite outcomes every time: if one qubit is measured , the other is guaranteed to be found in the state.
Check your understanding
Read the question(s) below, think about your answer, then click the triangle to reveal the solution.
Is the state entangled? Why or why not?
Answer:
It is not entangled. While the outcomes are always the same when you measure both qubits, this is only because each qubit is always fixed in the state . The result of measuring one qubit does not actually depend on the other — both are just always .
In general, if you can describe each qubit’s state separately and then multiply them together like this:
Then it is known as a "product state" and is not entangled.
Vector notation
It is often helpful to use vectors and matrices to see how the quantum state transforms under different operations. In this representation, our quantum states will be vectors, and our quantum gates (discussed in the next section) will be matrices that transform the vectors.
For a single qubit, the vector form of the states are chosen to be: In this way, an arbitrary state can be written as
For a general, -qubit state, we will need a -dimensional vector, with basis states ordered as you might expect, in ascending binary value: