דף זה טרם תורגם. התוכן מוצג באנגלית.
Quantum bits, gates, and circuits
Kifumi Numata (19 Apr 2024)
Click here to download the pdf of the original lecture. Note that some code snippets might become deprecated since these are static images.
Approximate QPU time to run this experiment is 5 seconds.
1. Introduction
Bits, gates, and circuits are the basic building blocks of quantum computing. You will learn quantum computation with the circuit model using quantum bits and gates, and also review the superposition, measurement, and entanglement.
In this lesson you will learn:
- Single-qubit gates
- Bloch sphere
- Superposition
- Measurement
- Two-qubit gates and entanglement state
At the end of this lecture, you will learn about circuit depth, which is essential for utility-scale quantum computing.
2. Computation as a diagram
When using qubits or bits, we need to manipulate them in order to turn the inputs we have into the outputs we need. For the simplest programs with very few bits, it is useful to represent this process in a diagram known as a circuit diagram.
The bottom-left figure is an example of a classical circuit, and the bottom-right figure is an example of a quantum circuit. In both cases, the inputs are on the left and the outputs are on the right, while the operations are represented by symbols. The symbols used for the operations are called “gates”, mostly for historical reasons.
3. Single-qubit quantum gate
3.1 Quantum state and Bloch sphere
A qubit's state is represented as a superposition of and . An arbitrary quantum state is represented as
where and are complex numbers such that .
and are vectors in the two-dimensional complex vector space:
Therefore, an arbitrary quantum state is also represented as
From this, we can see that the state of a quantum bit is a unit vector in a two-dimensional complex inner product space with an orthonormal basis of and . It is normalized to 1.
|\psi\rangle =\begin\{pmatrix\} \alpha \\ \beta \end\{pmatrix\} is also called the statevector.
A single-qubit quantum state is also represented as
where and are the angles of the Bloch sphere in the following figure.
In the next few code cells, we will build up basic calculations from constituent pieces in Qiskit. We'll construct an empty circuit and then add quantum operations, discussing the gates and visualizing their effects as we go.
You can run the cell by "Shift" + "Enter". Import the libraries first.
# Import the qiskit library
from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator
from qiskit.quantum_info import Statevector
from qiskit.visualization import plot_bloch_multivector
from qiskit_ibm_runtime import Sampler
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit.visualization import plot_histogram
Prepare the quantum circuit
We will create and draw a single-qubit circuit.
# Create the single-qubit quantum circuit
qc = QuantumCircuit(1)
# Draw the circuit
qc.draw("mpl")
X gate
The X gate is a rotation around the axis of the Bloch sphere. Applying the X gate to results in , and applying the X gate to results in , so it is an operation similar to the classical NOT gate, and is also known as bit flip. The matrix representation of the X gate is below.
qc = QuantumCircuit(1) # Prepare the single-qubit quantum circuit
# Apply a X gate to qubit 0
qc.x(0)
# Draw the circuit
qc.draw("mpl")
In IBM Quantum®, the initial state is set to , so the quantum circuit above in matrix representation is
Next, let's run this circuit using a statevector simulator.
# See the statevector
out_vector = Statevector(qc)
print(out_vector)
# Draw a Bloch sphere
plot_bloch_multivector(out_vector)
Statevector([0.+0.j, 1.+0.j],
dims=(2,))
Vertical vector is displayed as row vector, with complex numbers (the imaginary part is indexed by ).
H gate
The Hadamard gate is a rotation around an axis halfway between the and axes on the Bloch sphere. Applying the H gate to creates a superposition state such as . The matrix representation of the H gate is below.
qc = QuantumCircuit(1) # Create the single-qubit quantum circuit
# Apply an Hadamard gate to qubit 0
qc.h(0)
# Draw the circuit
qc.draw(output="mpl")
# See the statevector
out_vector = Statevector(qc)
print(out_vector)
# Draw a Bloch sphere
plot_bloch_multivector(out_vector)
Statevector([0.70710678+0.j, 0.70710678+0.j],
dims=(2,))
This is
This superposition state is so common and important, that it is given its own symbol:
By applying the gate on the , we created a superposition of and where measurement in the computational basis (along z in the Bloch sphere picture) would give you each state with equal probabilities.
state
You might have guessed that there is a corresponding state:
To create this state, first apply an X gate to make , then apply an H gate.
qc = QuantumCircuit(1) # Create the single-qubit quantum circuit
# Apply a X gate to qubit 0
qc.x(0)
# Apply an Hadamard gate to qubit 0
qc.h(0)
# draw the circuit
qc.draw(output="mpl")
# See the statevector
out_vector = Statevector(qc)
print(out_vector)
# Draw a Bloch sphere
plot_bloch_multivector(out_vector)
Statevector([ 0.70710678+0.j, -0.70710678+0.j],
dims=(2,))
This is
Applying the gate on results in an equal superposition of and , but the sign of is negative.
3.2 Single-qubit quantum state and unitary evolution
The actions of all the gates we have seen so far have been unitary, which means they can be represented by a unitary operator. In other words, the output state can be obtained by acting on the initial state with a unitary matrix:
A unitary matrix is a matrix satisfying
In terms of quantum computer operation, we would say that applying a quantum gate to the qubit evolves the quantum state. Common single-qubit gates include the following.
Pauli gates:
where the outer product was calculated as follows:
Other typical single-qubit gates:
The meaning and use of these are described in more detail in the Basics of Quantum Information course.
Exercise 1
Use Qiskit to create quantum circuits that prepare the states described below. Then run each circuit using the statevector simulator and display the resulting state on the Bloch sphere. As a bonus, see if you can anticipate what the final state should be based on intuition about the gates and rotations in the Bloch sphere.
(1)
(2)
(3)
Tips: Z gate can be used by
qc.z(0)
Solution:
### (1) XX|0> ###
# Create the single-qubit quantum circuit
qc = QuantumCircuit(1) ##your code goes here##
# Add a X gate to qubit 0
qc.x(0) ##your code goes here##
# Add a X gate to qubit 0
qc.x(0) ##your code goes here##
# Draw a circuit
qc.draw(output="mpl")
# See the statevector
out_vector = Statevector(qc)
print(out_vector)
# Draw a Bloch sphere
plot_bloch_multivector(out_vector)
Statevector([1.+0.j, 0.+0.j],
dims=(2,))
### (2) HH|0> ###
##your code goes here##
qc = QuantumCircuit(1)
qc.h(0)
qc.h(0)
qc.draw("mpl")
# See the statevector
out_vector = Statevector(qc)
print(out_vector)
# Draw a Bloch sphere
plot_bloch_multivector(out_vector)
Statevector([1.+0.j, 0.+0.j],
dims=(2,))
### (3) HZH|0> ###
##your code goes here##
qc = QuantumCircuit(1)
qc.h(0)
qc.z(0)
qc.h(0)
qc.draw("mpl")
# See the statevector
out_vector = Statevector(qc)
print(out_vector)
# Draw a Bloch sphere
plot_bloch_multivector(out_vector)
Statevector([0.+0.j, 1.+0.j],
dims=(2,))
3.3 Measurement
Measurement is theoretically a very complicated topic. But in practical terms, making a measurement along (as all IBM® quantum computers do) simply forces the qubit’s state either to or to and we observe the outcome.
- is the probability we will get when we measure.
- is the probability we will get when we measure.
So, and are called probability amplitudes. (see "Born rule")
For example, has an equal probability of becoming or upon measurement. has a 75% chance of becoming .
Qiskit Aer Simulator
Next, let's measure a circuit that prepares the equal probability superposition above. We should add the measurement gates, as the Qiskit Aer simulator simulates an ideal (with no noise) quantum hardware by default. Note: The Aer simulator can also apply a noise model based on real quantum computer. We will return to noise models later.
# Create a new circuit with one qubits (first argument) and one classical bits (second argument)
qc = QuantumCircuit(1, 1)
qc.h(0)
qc.measure(0, 0) # Add the measurement gate
qc.draw(output="mpl")
We are now ready to run our circuit on the Aer simulator. In this example, we will apply the default shots=1024, which means we will measure 1024 times. Then we will plot those counts in a histogram.
# Run the circuit on a simulator to get the results
# Define backend
backend = AerSimulator()
# Transpile to backend
pm = generate_preset_pass_manager(backend=backend, optimization_level=1)
isa_qc = pm.run(qc)
# Run the job
sampler = Sampler(mode=backend)
job = sampler.run([isa_qc])
result = job.result()
# Print the results
counts = result[0].data.c.get_counts()
print(counts)
# Plot the counts in a histogram
plot_histogram(counts)
{'0': 521, '1': 503}
We see that 0s and 1s were measured with a probability of almost 50% each. Although noise has not been simulated here, the states are still probabilistic. So while we expect roughly a 50-50 distribution, we will rarely find exactly that. Just as 100 flips of a coin would rarely yield exactly 50 instances of each side.
4. Multi-qubit quantum gate and entanglement
4.1 Multi-qubit quantum circuit
We can create a two-qubit quantum circuit with following code. We will apply an H gate to each qubit.
# Create the two qubits quantum circuit
qc = QuantumCircuit(2)
# Apply an H gate to qubit 0
qc.h(0)
# Apply an H gate to qubit 1
qc.h(1)
# Draw the circuit
qc.draw(output="mpl")
# See the statevector
out_vector = Statevector(qc)
print(out_vector)
Statevector([0.5+0.j, 0.5+0.j, 0.5+0.j, 0.5+0.j],
dims=(2, 2))
Note: Qiskit bit ordering
Qiskit uses Little Endian notation when ordering qubits and bits, meaning qubit 0 is the rightmost bit in the bitstrings. Example: means q0 is and q1 is . Be careful because some literature in quantum computing use the Big Endian notation (qubit 0 is the leftmost bit) and a great deal of quantum mechanics literature does, too.
Another thing to notice is that when representing a quantum circuit, is always placed at the top of the circuit. With this in mind, the quantum state of the above circuit can be written as a tensor product of single-qubit quantum state.
(