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: