← Back to Tutorials Chapter 16

Quantum Machine Learning

Quantum Machine Learning (QML) sits at the intersection of quantum computing and machine learning. It explores how quantum algorithms can solve ML problems faster or more effectively than classical approaches, and how ML can improve quantum systems.

Quantum Computing Fundamentals

Qubits

A qubit (quantum bit) is the fundamental unit of quantum information. Unlike a classical bit that is either 0 or 1, a qubit exists in a superposition of both states simultaneously. This property, combined with entanglement, gives quantum computers their potential power.

Superposition

Superposition allows a qubit to be in a combination of |0⟩ and |1⟩ states simultaneously, represented as α|0⟩ + β|1⟩ where α and β are probability amplitudes. When measured, the qubit collapses to either 0 or 1 with probabilities |α|² and |β|².

Entanglement

Entanglement is a quantum phenomenon where two or more qubits become correlated such that the state of one instantly determines the state of the other, regardless of distance. This allows quantum computers to process information in ways impossible for classical computers.

QML with Python

Pennylane

Pennylane is a cross-platform Python library for differentiable quantum computing. It integrates seamlessly with classical ML frameworks like PyTorch and TensorFlow, enabling the construction of hybrid quantum-classical models.

import pennylane as qml
import numpy as np

# Create a quantum device with 2 qubits
dev = qml.device('default.qubit', wires=2)

@qml.qnode(dev)
def quantum_circuit(x, weights):
    qml.RX(x[0], wires=0)
    qml.RY(x[1], wires=1)
    qml.CNOT(wires=[0, 1])
    qml.RZ(weights[0], wires=0)
    qml.RX(weights[1], wires=1)
    return qml.expval(qml.PauliZ(0))

x = np.array([0.5, 0.3])
weights = np.array([0.1, 0.2])
result = quantum_circuit(x, weights)
print(f"Quantum circuit output: {result:.4f}")

Qiskit

Qiskit is IBM's open-source framework for quantum computing. It provides tools for building, simulating, and running quantum circuits on IBM quantum hardware. Qiskit Machine Learning extends it with QML algorithms.

from qiskit import QuantumCircuit, transpile
from qiskit_aer import AerSimulator
from qiskit.visualization import plot_histogram

# Create a simple quantum circuit
qc = QuantumCircuit(2, 2)
qc.h(0)            # Hadamard gate on qubit 0 (creates superposition)
qc.cx(0, 1)        # CNOT gate (entangles qubits)
qc.measure([0, 1], [0, 1])

# Simulate
simulator = AerSimulator()
compiled = transpile(qc, simulator)
result = simulator.run(compiled, shots=1024).result()
counts = result.get_counts()
print(f"Measurement results: {counts}")

Hybrid Quantum-Classical Models

Current quantum computers are limited, so most practical QML uses hybrid models. A classical neural network processes most of the data, while a quantum circuit handles a specialized subtask. The entire pipeline can be trained end-to-end using gradient-based optimization.

Variational Quantum Circuits (VQC)

VQCs are parameterized quantum circuits optimized by classical optimization algorithms. They are the quantum analogue of neural networks, where gate parameters (rotation angles) are learned from data. VQCs are well-suited for near-term quantum devices.

Quantum Kernel Methods

Quantum kernel methods use quantum computers to compute kernel functions that are hard to estimate classically. By mapping classical data into a quantum Hilbert space, these kernels can capture complex patterns that classical SVMs cannot, potentially offering a quantum advantage for certain datasets.

Quantum ML diagram
Note: Quantum computing is still in its early stages. Current devices have limited qubits, high error rates, and short coherence times. Most QML research is done on simulators, and practical advantages over classical ML are still being discovered.
Pro Tip: Start with Pennylane's default.qubit simulator to learn QML concepts before moving to real quantum hardware. IBM offers free access to quantum computers through the IBM Quantum Experience for educational use.
Exercise: Use Pennylane to build a variational quantum classifier for a simple binary classification problem. Generate two interleaving moons using sklearn.datasets.make_moons. Create a quantum circuit with 4 qubits, encode the 2D data using angle encoding, and train the circuit parameters to classify the data. Compare accuracy with a classical SVM.