Introduction to Quantum Computing
The basics of quantum computing and some hands-on trying.
Prerequisites
- Basic Maths — linear algebra
- Basic quantum mechanics concepts — Superposition, the Uncertainty Principle, and Entanglement
- Basic Python
- Imagination power
Quantum Mechanics
“The mathematical framework of quantum theory has passed countless successful tests and is now universally accepted as a consistent and accurate description of all atomic phenomena.” ~ Erwin Schrödinger
We will not be talking about the advanced maths or concepts of quantum mechanics in this blog post, such as the Uncertainty Principle or the Schrödinger Equation, but I'll assume you know the main principles of Quantum Mechanics, including Superposition, Wave-Particle Duality, the Uncertainty Principle, and Entanglement. The concept of qubits and quantum circuits gates will be explained below.
Qubits
In normal semiconductor computers, each "bit" can only represent two values: 0 and 1, which can be used in calculations as low and high voltage. However, in quantum computers, "qubits" (quantum bit) are used. Now instead of representing only 0 or 1, it can now represent 0 and 1 simultaneously. It carries a "state" (written in Bra-ket notation), which is a vector defined as followed in a qubit:
Where
to ensure the state is normalized. and represents the amplitudes of the qubit in the and states respectively, and are complex numbers. To get the probability of measuring a certain state or from , we can apply the Born rule (given that and are orthonormal, which means orthogonal and normalized):
and
Now, if we have two qubits entangled, we can represent states (), each associated with a probability.
The power of quantum computing is you can represent states if you have qubits, which is an exponential growth. For example, if you have 20 qubits, you can encode a superposition over all basis states simultaneously, but in a classical computer, 20 bits can only be in 1 of the possible configurations at any given time. Similarly, a system with 266 qubits would have a state space with a dimensionality on the order of , which is roughly equivalent to the estimated number of atoms in the observable universe! When qubits are entangled, quantum gates (which are unitary operations) act on the entire state vector, modifying the amplitudes of all basis states via quantum interference. This ability to manipulate a vast, exponentially large state space is central to the potential power of quantum computing. Now we will talk about these quantum gates.
Gates
To visualize a single qubit, you can use the Bloch Sphere:
The z-axis corresponds to the probability in being measured in and . X-axis and y-axis also corresponds to being measured in , , , and respectively.
Quantum circuit gates can be visualized as rotating the state vector (the red arrow) in the bloch sphere. Here are some examples:
-
Pauli-X Gate (NOT Gate):
- Rotation: radians (180 degrees) around the X-axis.
- Effect: Transforms the state to and vice versa. On the Bloch sphere, it flips the state from the north pole to the south pole and vice versa.
-
Pauli-Y Gate:
- Rotation: radians (180 degrees) around the Y-axis.
- Effect: Applies a complex phase and swaps the states and . For example, becomes and becomes .
-
Pauli-Z Gate:
- Rotation: radians (180 degrees) around the Z-axis.
- Effect: Leaves the state unchanged and adds a phase of (equivalent to a factor of -1) to the state .
-
Hadamard Gate (H Gate):
- Rotation: This gate performs a more complex transformation, equivalent to rotating around the axis at 45 degrees from both X and Z axes, followed by a radian rotation around the Y-axis.
- Effect: Creates superpositions from basis states, turning into and into , which is the state.
-
S Gate (Phase Gate):
- Rotation: radians (90 degrees) around the Z-axis.
- Effect: Leaves the state unchanged and multiplies the state by .
-
T Gate:
- Rotation: radians (45 degrees) around the Z-axis.
- Effect: Leaves the state unchanged and multiplies the state by , which is a more subtle phase shift than the S gate.
And some parameterized gates:
- RX Gate:
- Rotation: radians around the X-axis. It can be represented as a matrix multiplication between the unitary gate matrix and the quantum states, the gate matrix is:
- RY Gate:
- Rotation: radians around the y-axis:
- RZ Gate:
- Rotation: radians around the z-axis:
And much more.
Every gate is represented as an unitary gate matrix, e.g. the Hadamard Gate:
Also, there are double-qubits gate, for example the CNOT (Controlled-NOT) gate:
Controlled gates mean if the first qubit is , then the gate (NOT in this case) on second qubit will be performed. There are even triple-qubits gates, like the Toffoli gate (a.k.a. CCNOT), which has a size gate matrix.
If you have a quantum states, say 10 qubits, and you want to apply a specific gate on a specific qubit, you can use the Kronecker product to construct a gate matrix and apply it to the quantum states, for example you want to apply the Hadamard gate on the 4th qubit:
is the identity matrix, i.e.
These quantum gates are crucial in constructing quantum circuits for algorithms, where each type of gate contributes to manipulating the qubit's state in a controlled manner to achieve various computational goals.
Simulation with Qiskit
Qiskit is an open-source framework for quantum computing. It provides the tools for creating quantum circuits, simulating them, and running them on real quantum hardware through IBM's cloud services.
Setting Up Your Environment
To start using Qiskit, make sure Python is installed on your machine, then install Qiskit and Qiskit Aer, which includes simulators that run on your local machine.
pip install qiskit qiskit-aer pylatexenc
Single Qubit Operations
Building the Circuit
We'll begin with a basic single-qubit circuit to demonstrate sequential gate operations that include rotations and a phase shift.
from qiskit import QuantumCircuit, transpile
from qiskit_aer import Aer
from qiskit.visualization import plot_bloch_multivector
# Initialize a Quantum Circuit with 1 qubit
qc_single = QuantumCircuit(1)
# Apply rotation around the X-axis
qc_single.rx(3.14159 / 4, 0) # Pi/4 rotation
# Apply rotation around the Y-axis
qc_single.ry(3.14159 / 2, 0) # Pi/2 rotation
# Apply a Z gate
qc_single.z(0)
# Visualize the circuit
print("Single Qubit Circuit:")
print(qc_single.draw(output='mpl'))
Calculating the Expected State Vector
The state vector undergoes a rotation by radians around the X-axis, followed by around the Y-axis, and finally, a phase shift due to the Z gate.
Simulation and Output
# Prepare the simulator
simulator = Aer.get_backend('statevector_simulator')
# Transpile the circuit for the simulator
transpiled_qc = transpile(qc_single, simulator)
# Run the simulation
job = simulator.run(transpiled_qc)
result = job.result()
# Get the final state vector
statevector = result.get_statevector()
print(statevector)
# Plot the state on the Bloch sphere
plot_bloch_multivector(statevector)
Multi-Qubit Operations
Building the Circuit
We will construct a five-qubit circuit with a combination of entanglement, rotations, and phase shifts to demonstrate the interplay of different quantum gates.
from qiskit import QuantumCircuit, transpile
from qiskit_aer import Aer
from qiskit.visualization import plot_bloch_multivector
from math import pi
# Initialize a Quantum Circuit with 5 qubits
qc_multi = QuantumCircuit(5)
# Apply a Hadamard gate to qubit 0
qc_multi.h(0)
# Apply a CNOT gate between qubit 0 and qubit 1
qc_multi.cx(0, 1)
# Apply a rotation around the X-axis by pi/2 on qubit 1
qc_multi.rx(pi / 2, 1)
# Apply a T gate to qubit 2
qc_multi.t(2)
# Apply a CNOT gate between qubit 1 and qubit 2
qc_multi.cx(1, 2)
# Apply a rotation around the Z-axis by pi/4 on qubit 3
qc_multi.rz(pi / 4, 3)
# Apply a Hadamard gate to qubit 4
qc_multi.h(4)
# Apply a CNOT gate between qubit 4 and qubit 3
qc_multi.cx(4, 3)
# Apply a CNOT gate between qubit 1 and qubit 4
qc_multi.cx(1, 4)
# Visualize the Circuit
print("Five-Qubit Circuit:")
print(qc_multi.draw(output='mpl'))
Calculating the Expected State Vector
Now let's calculate the expected state vector after applying the specified sequence of gates:
-
Initial State
Operation: All qubits start in .
State now: -
After
Operation: A Hadamard on qubit 0 creates an equal superposition of and (for that qubit).
State now: -
After
Operation: Qubit 0 is the control, qubit 1 is the target. If qubit 0=1, flip qubit 1.
State now: -
After
Operation: A rotation by around the ‐axis acts on qubit 1. Recall:Since the state so far is , applying to qubit 1 in each term yields four basis states:
State now: -
After
Operation: The gate adds a phase if qubit 2 is . Here, in all four terms above, qubit 2=, so this gate has no effect.
State now (unchanged): -
After
Operation: Qubit 1 is the control, qubit 2 is the target. If qubit 1=1, flip qubit 2.- .
- .
State now:
-
After
Operation: A rotation by about on qubit 3 adds a phase to the state of that qubit. However, qubit 3= for all four terms, so only a global phase results (which can be ignored).
State now (unchanged up to global phase): -
After
Operation: A Hadamard on qubit 4 (the rightmost qubit) transforms . Each existing term with qubit 4=0 splits into two terms.
State now: -
After
Operation: Qubit 4 is the control, qubit 3 is the target. If qubit 4=1, flip qubit 3.
State now: -
After
Operation: Finally, qubit 1 is the control and qubit 4 is the target. Whenever qubit 1=1, flip qubit 4.
State now (final):
This eight‐term superposition (up to any global phase) is the final state of the five‐qubit system after all gates have been applied.
Simulation and Output
# Prepare the simulator
simulator = Aer.get_backend('statevector_simulator')
# Transpile the circuit for the simulator
transpiled_qc = transpile(qc_multi, simulator)
# Run the simulation
job = simulator.run(transpiled_qc)
result = job.result()
# Get the final state vector
statevector = result.get_statevector()
print(statevector)
# Plot the state on the Bloch sphere
plot_bloch_multivector(statevector)
Tasks you may do
- Implement Shor's Algorithm for finding the prime factors of an integer.
- Implement Grover's Algorithm for an unstructured search that finds with high probability the unique input to a black box function that produces a particular output value.