Quantum Machine Learning Introduction
Quantum Machine Learning Introduction
The journey into Quantum Machine Learning (QML) represents a natural and seamless
evolution for anyone who has mastered the fundamentals of quantum circuit
construction and execution. The foundational workflow—mapping a problem to a
QuantumCircuit, executing it with a primitive, and analyzing the results—forms the
very bedrock upon which QML algorithms are built [User Query]. An algorithm such as
Deutsch-Jozsa, for instance, utilizes a fixed, predefined quantum circuit to
demonstrate a specific quantum principle. A Variational Quantum Classifier (VQC), the
focus of this report, employs a strikingly similar workflow but introduces a pivotal new
concept: parts of the quantum circuit are trainable [User Query]. This is not a
replacement of existing skills but rather the addition of a sophisticated new layer to
the circuits one already knows how to build and operate.
The development of VQAs reflects a crucial strategic pivot in the field of quantum
computing. Early, theoretically powerful algorithms often presuppose the existence of
large-scale, fault-tolerant quantum computers that are not yet available. The NISQ
era's constraints necessitated a different approach. Instead of demanding perfect
hardware for our algorithms, the field began designing algorithms that can tolerate,
and in some cases even leverage, the characteristics of imperfect hardware. VQAs are
the direct result of this pragmatic shift. They are hybrid quantum-classical algorithms
where a classical computer optimizes the parameters of a quantum circuit. This
iterative loop allows the algorithm to "learn" and potentially compensate for
systematic errors in the quantum device, making VQAs a practical and powerful
framework for pursuing a quantum advantage on near-term hardware. Therefore,
mastering the VQC is not merely about learning a new algorithm; it is about being
inducted into the dominant, pragmatic philosophy that defines the current state of
applied quantum computing.
A significant practical challenge for any learner entering the QML space today is
navigating the substantial architectural evolution of the Qiskit software framework.
Understanding this new landscape is an essential prerequisite for writing functional,
modern QML code.
Until early 2021, Qiskit's high-level algorithms for applications in chemistry, finance,
optimization, and machine learning were consolidated within a single, monolithic
package named [Link]. However, [Link] has been officially deprecated and
is no longer supported in modern versions of Qiskit (post-0.25.0). This change means
that a vast number of tutorials, academic papers, and code repositories available
online are now outdated. Attempting to run code from these older resources with a
current Qiskit installation will invariably lead to ModuleNotFoundError errors, a
common source of frustration for newcomers.
This transition was not a simple renaming of modules but a fundamental refactoring of
the Qiskit ecosystem into a collection of domain-specific application packages. The
goal was to create a more modular, maintainable, and extensible framework, reflecting
the maturation of the field from academic exploration to the development of
specialized scientific tools. This modular architecture is a hallmark of a professional
software ecosystem, signaling a move towards stable and sustainable tools for serious
research and industrial applications.
The New Modular Structure
The functionality once contained in [Link] has been reorganized into several key
packages :
● qiskit-machine-learning: The central library for QML applications and the
primary focus of this report. It contains implementations of Quantum Neural
Networks (QNNs), Quantum Kernels, and high-level algorithms like the VQC and
the Quantum Support Vector Classifier (QSVC).
● qiskit-optimization: For solving optimization problems.
● qiskit-nature: For applications in quantum chemistry and physics.
● qiskit-finance: For financial applications.
Furthermore, the core algorithmic components and classical optimizers that were
once part of Aqua have been moved into qiskit-terra (the foundational Qiskit package)
and a new library, qiskit_algorithms.
To aid in navigating this new ecosystem, the following table serves as a "Rosetta
Stone," mapping the conceptual components of a VQC to their modern classes and
module paths, and noting their deprecated equivalents.
The first step in any QML workflow is to "upload" classical data into the quantum
computer. This is the role of the quantum feature map, a parameterized quantum
circuit denoted as U(x), where the parameters are the features of a classical data
point x. The purpose of the feature map is to encode this classical information into a
quantum state ∣ψ(x)⟩=U(x)∣0⟩⊗n, which lives in a high-dimensional Hilbert space. The
geometry of the data within this quantum feature space is critical; a well-designed
feature map can transform a complex, non-linearly separable dataset into one that is
more easily classified. It is within this vast computational space, potentially
inaccessible to classical computers, that a quantum advantage for machine learning
may be found.
The unitary operation for the entangling component of a general Pauli feature map can be
expressed as:
$$ U_{\Phi(\vec{x})} = \exp\left(i \sum_{S \subseteq {1, \dots, n}, |S|=k} \phi_S(\vec{x}) \prod_{j
\in S} P_j\right) $$
where Pjare Pauli matrices. For the ZZFeatureMap, the interaction order is k=2, and the Pauli
products are of the form ZiZj. This structure allows the feature map to embed non-linear
relationships from the original data into the quantum state, creating a rich feature space for
classification.
If the feature map is where the data is loaded, the variational ansatz, W(θ), is the
model itself. It is a quantum circuit with a fixed structure but containing a set of
tunable classical parameters, θ, which are learned during the training process. The
"expressivity" of the ansatz—its ability to generate a wide variety of quantum
states—determines the complexity of the decision boundaries the VQC can learn. A
more expressive ansatz can, in principle, approximate more complex functions.
A prevalent and practical choice for the ansatz is the EfficientSU2 circuit. This circuit
is known as a "hardware-efficient" ansatz because its structure is designed to map
well onto the physical constraints of real NISQ devices. It typically involves layers of
single-qubit rotations and entangling gates that only act on adjacent qubits (e.g.,
linear entanglement). This locality is crucial because it reduces the number of
additional SWAP gates required during the transpilation process—the process of
rewriting a circuit to match the specific qubit connectivity of a given hardware device.
Fewer SWAP gates lead to shorter, less noisy circuits, which is paramount for
successful execution on near-term quantum computers. The EfficientSU2 circuit
consists of alternating layers of :
1. Single-qubit Rotations: These are typically general SU(2) gates, like RY(θi) and
RZ(ϕi), applied to each qubit. These parameterized gates are the "knobs" that the
classical optimizer turns during training.
2. Entangling Gates: These are typically two-qubit gates like CNOT (CX) that create
entanglement between the qubits, allowing the circuit to explore correlations and
generate complex, non-trivial quantum states necessary for solving the
classification task.
The combination of the feature map and the ansatz creates the full parameterized
quantum circuit for the VQC: UVQC(x,θ)=W(θ)U(x). The design of this complete circuit
embodies a critical trade-off that is central to all VQAs. A natural instinct is to
maximize the complexity of the circuit (e.g., by increasing the number of repetitions,
or reps, of the feature map and ansatz layers) to increase its expressivity. However,
this approach is fraught with peril. Deeper circuits are more susceptible to
decoherence and gate errors on NISQ hardware, which can corrupt the computation.
More fundamentally, they are prone to a trainability issue known as the barren
plateau phenomenon. In a barren plateau, the landscape of the loss function
becomes exponentially flat as the number of qubits or circuit depth increases. This
means the gradient of the loss function with respect to the parameters becomes
vanishingly small across most of the parameter space, providing no meaningful
direction for the classical optimizer to follow and causing the training process to stall.
Consequently, designing a VQC is not a matter of maximizing complexity, but of
finding a delicate balance: the circuit must be expressive enough to solve the problem
but simple enough to be trainable and executable on near-term hardware. This is
precisely why hardware-efficient ansaetze like EfficientSU2 are so prevalent in
research.
Instead of manually coding this entire hybrid optimization loop, a practitioner can
leverage the high-level VQC class provided by the qiskit-machine-learning library.
This class acts as a convenient wrapper that encapsulates the entire workflow. It is
instantiated by providing the pre-configured quantum and classical components: the
sampler for execution, the feature_map for data encoding, the ansatz as the trainable
model, and the optimizer for training.
The first phase of any machine learning experiment is to prepare the data. This
involves importing the necessary libraries, generating a suitable dataset, scaling the
features appropriately for our quantum model, splitting the data into training and
testing sets, and visualizing it to establish a baseline understanding.
Python
# General-purpose imports
import numpy as np
import [Link] as plt
# Scikit-learn for data generation and processing
from [Link] import make_circles
from sklearn.model_selection import train_test_split
from [Link] import MinMaxScaler
# Qiskit imports
from [Link] import Sampler
from [Link] import ZZFeatureMap, EfficientSU2
from qiskit_algorithms.optimizers import COBYLA
from qiskit_machine_learning.[Link] import VQC
from qiskit_machine_learning.utils.algorithm_globals import algorithm_globals
# Set a random seed for reproducibility
algorithm_globals.random_seed = 42
# --- 1. Data Generation ---
# Generate 100 data points forming two concentric circles
X, y = make_circles(n_samples=100, noise=0.1, factor=0.5, random_state=1)
# --- 2. Data Scaling ---
# Scale features to the range [0, pi] for angle-based encoding in the feature map
scaler = MinMaxScaler(feature_range=(0, [Link]))
X_scaled = scaler.fit_transform(X)
# --- 3. Data Splitting ---
# Split the data into training (80%) and testing (20%) sets
X_train, X_test, y_train, y_test = train_test_split(
X_scaled, y, test_size=0.2, random_state=algorithm_globals.random_seed
)
# --- 4. Data Visualization ---
[Link](figsize=(8, 6))
[Link](X_train[y_train == 0][:, 0], X_train[y_train == 0][:, 1], c='royalblue', marker='o',
label='Class 0 (Train)')
[Link](X_train[y_train == 1][:, 0], X_train[y_train == 1][:, 1], c='tomato', marker='s',
label='Class 1 (Train)')
[Link](X_test[y_test == 0][:, 0], X_test[y_test == 0][:, 1], c='darkblue', marker='o',
label='Class 0 (Test)')
[Link](X_test[y_test == 1][:, 0], X_test[y_test == 1][:, 1], c='darkred', marker='s',
label='Class 1 (Test)')
[Link]("Synthetic 'Circles' Dataset")
[Link]("Feature 1 (scaled)")
[Link]("Feature 2 (scaled)")
[Link]()
[Link](True, linestyle='--', alpha=0.7)
[Link]()
Explanation:
● Imports: All necessary modules are imported from numpy, matplotlib, sklearn,
and the modern Qiskit libraries. Note the specific imports from [Link],
qiskit_algorithms.optimizers, and qiskit_machine_learning.algorithms, which
reflect the new modular ecosystem.
● Data Generation: We use make_circles from scikit-learn to create a dataset that
is not linearly separable, providing a non-trivial challenge for our classifier.
● Data Scaling: This is a critical pre-processing step. The ZZFeatureMap uses
angle encoding, meaning it maps classical feature values to the rotation angles of
quantum gates. It is standard practice to scale the data to a suitable range, such
as [0,π] or [0,2π], to ensure the full expressive range of the rotation gates is
utilized. Here, MinMaxScaler transforms our features into the range [0,π].
● Data Splitting: The data is partitioned into a training set (which the model will
learn from) and a testing set (which will be used to evaluate the model's
performance on unseen data), a standard practice to prevent overfitting.
● Visualization: Plotting the data helps to understand its structure and provides a
visual baseline for evaluating the classifier's performance later.
Next, we define the core quantum components of our VQC: the Sampler primitive for
execution, the ZZFeatureMap for data encoding, and the EfficientSU2 circuit for the
trainable ansatz. This is the model design phase of our experiment.
Python
Explanation:
● Sampler: We instantiate a basic Sampler from [Link]. By default, this will
use a local, high-performance, statevector-based simulator to execute our
circuits and return the measurement counts.
● Feature Map: We choose the ZZFeatureMap. The feature_dimension is set to the
number of features in our data (2 in this case), which also determines the number
of qubits required. The reps parameter controls how many times the basic
encoding block is repeated. Higher reps can create a more complex mapping but
also a deeper circuit, illustrating the expressivity-noise trade-off.
● Ansatz: We use the EfficientSU2 ansatz. The reps parameter here controls the
number of repeating layers of rotation and entanglement gates, directly affecting
the number of trainable parameters and the model's expressive power. We
choose 'linear' entanglement, where qubit i is entangled with qubit i+1, a common
hardware-efficient strategy that helps mitigate noise and connectivity issues on
real devices.
● Inspection: Calling .decompose() on the circuit objects allows us to print and
visualize their underlying gate structure, making the abstract components
tangible.
4.3. Configuring and Training the VQC
Now we assemble the components into the high-level VQC class, configure the
classical optimizer, and define a callback function to monitor the training progress in
real-time. This is the experimentation phase.
Python
Explanation:
● Optimizer: We select COBYLA (Constrained Optimization By Linear
Approximation). It is a gradient-free optimizer, making it a good choice for initial
experiments as it avoids the complexities of quantum gradient calculation. We
limit it to 100 iterations (maxiter=100).
● Callback Function: This function is a powerful tool for "seeing inside" the black
box of training. The [Link] method will call this function after each iteration of
the optimizer, passing it the current weights and the objective function (loss)
value. Our function appends the value to a list and generates a plot, giving us a
live view of how the loss is decreasing over time.
● VQC Instantiation: We create an instance of the VQC class, passing all our
previously defined components: the sampler, feature_map, ansatz, optimizer, and
our callback function.
● Training: The single command [Link](X_train, y_train) starts the hybrid
quantum-classical training loop. The VQC will now iteratively execute quantum
circuits, measure the results, calculate the loss, and update the ansatz
parameters to learn the patterns in our training data.
Finally, after the model is trained, we enter the analysis and validation phase. We
evaluate its performance quantitatively on both the training and unseen test data and
visualize the decision boundary it has learned qualitatively.
Python
Explanation:
● Evaluation: The [Link]() method computes the mean accuracy of the
classification on the given data. We compute this for both the training and testing
sets. Comparing these two scores is essential for diagnosing overfitting—a
situation where the model memorizes the training data but fails to generalize to
new, unseen data.
● Decision Boundary Visualization: This is a powerful technique for
understanding a classifier's behavior. We create a fine grid of points spanning the
entire feature space. We then use our trained vqc to predict the class for every
single point on this grid. By plotting these predictions as a colored background
([Link]), we can see the regions of the space that the VQC has learned to
associate with each class. The line separating these colored regions is the
decision boundary. Overlaying our original data points provides the ultimate visual
confirmation that our quantum model has successfully learned to solve the
non-linear classification problem by separating the two circles.
Learning the VQC in isolation can lead to a narrow understanding of the QML
landscape. To provide a richer context, it is essential to contrast it with the other
major paradigm in quantum classification: the Quantum Support Vector Machine
(QSVM). This comparison reveals a fundamental strategic split in how researchers are
attempting to leverage quantum computers for machine learning.
To understand the QSVM, one must first understand its classical predecessor, the
Support Vector Machine (SVM). An SVM is a powerful classification algorithm that
finds an optimal hyperplane to separate data points of different classes. For data that
is not linearly separable, the SVM employs the "kernel trick." A kernel is a function,
K(xi,xj), that calculates the similarity (or inner product) between two data points in a
higher-dimensional feature space, without ever having to explicitly compute the
transformation into that space.
The QSVM, implemented in Qiskit as the QSVC (Quantum Support Vector Classifier),
operates on a simple but powerful principle: it replaces the classical kernel
computation with a quantum one. The process is as follows :
1. A quantum feature map, U(x), is chosen to encode classical data points xiand xj
into quantum states ∣ψ(xi)⟩ and ∣ψ(xj)⟩.
2. The quantum computer is used to estimate the overlap, or fidelity, between these
two states. This fidelity serves as the kernel value: K(xi,xj)=∣⟨ψ(xi)∣ψ(xj)⟩∣2.
3. This process is repeated for pairs of data points in the training set to construct a
full quantum kernel matrix.
4. This quantum kernel matrix is then fed into a standard, classical SVM solver, which
finds the optimal separating hyperplane in the quantum feature space.
Crucially, in this standard QSVM approach, only the kernel computation is quantum.
The optimization part of the algorithm remains entirely classical.
The distinction between the VQC and QSVM is not merely a choice between two
algorithms; it reflects two different philosophies for achieving a quantum advantage.
● The QSVM represents a more conservative, "quantum-enhanced" philosophy. It
identifies a specific, classically hard component of a well-understood and
powerful classical algorithm (the kernel computation) and replaces only that part
with a quantum subroutine. It aims to augment a proven classical framework with
a potential quantum advantage in feature representation.
● The VQC embodies a more radical, "end-to-end" quantum philosophy. It
discards the classical SVM framework entirely and instead builds a new type of
classifier from the ground up, inspired by neural networks. Here, the entire
model—from data encoding to the final transformation—is a quantum circuit, and
the training process directly optimizes the parameters of this circuit to perform
the classification task.
Role of Quantum Computer Executes the entire Computes the kernel matrix
classification circuit (feature K(xi,xj) once (or a few times)
map + ansatz) in each step of before classical training.
the optimization loop.
Research into mitigating barren plateaus is an active and critical field. Key strategies
include :
● Careful Circuit Design: Avoiding deep, unstructured ansaetze in favor of
shallower, hardware-efficient circuits with local entanglement structures can
reduce the likelihood of encountering a barren plateau.
● Parameter Initialization: Clever initialization strategies that correlate parameters
or start the optimization in a region of non-zero gradients can help the training
process get off the ground.
● Local Cost Functions: Using cost functions that depend only on measurements
of a few qubits (local observables) can prevent the global nature of the problem
from washing out gradient information, thus preserving a trainable landscape.
● Geometric Quantum Machine Learning (GQML): This emerging approach
leverages principles of symmetry in the problem to design ansaetze that are
guaranteed to have non-zero gradients, effectively engineering the circuit to
avoid barren plateaus from the start.
NISQ devices are inherently noisy. Quantum states are fragile and quickly lose their
quantum properties (decoherence) due to unwanted interactions with their
environment. Furthermore, the quantum gates used to manipulate them are imperfect.
This noise corrupts the final quantum state of the VQC, leading to incorrect
measurement statistics and a distorted evaluation of the loss function, which can
severely degrade or completely prevent successful training.
A design choice to increase expressivity, for example, by making the ansatz deeper,
directly exacerbates the impact of hardware noise and simultaneously increases the
risk of hitting a barren plateau. Addressing noise is therefore paramount. While full
quantum error correction is not feasible on NISQ hardware, a suite of techniques
known as Quantum Error Mitigation (QEM) can be used to reduce the impact of
noise on computed results. These techniques often involve running additional
calibration circuits and performing classical post-processing to extrapolate an
estimate of the ideal, noise-free result. Another fascinating research direction involves
making the model itself more robust to noise by, for example, learning quantum
observables that are inherently less sensitive to the specific noise channels present in
a device.
The VQC, and VQAs in general, represent a promising pathway toward achieving
practical quantum advantage on NISQ hardware. However, significant and
interconnected challenges related to trainability, noise, and architecture design
remain at the forefront of the field. For the ambitious learner, the path forward is clear.
The provided code should serve not as a final destination, but as a sandbox for
experimentation and deeper inquiry. The following steps are recommended for moving
from a student of quantum machine learning to an active participant in its exciting
development :
● Explore different datasets: Test the VQC on other classical machine learning
datasets from libraries like scikit-learn to understand how its performance varies
with data complexity and structure.
● Vary the quantum components: Swap the ZZFeatureMap for a PauliFeatureMap
or the EfficientSU2 ansatz for RealAmplitudes. Systematically observe how
changes in reps and the entanglement strategy affect accuracy, training time, and
the shape of the loss curve.
● Experiment with optimizers: Replace the gradient-free COBYLA with a different
optimizer like SPSA (Simultaneous Perturbation Stochastic Approximation) or one
of the gradient-based optimizers available in Qiskit to compare their convergence
properties.
● Advance to more complex models: Investigate techniques like Quantum Kernel
Training, which uses the QuantumKernelTrainer class to optimize the parameters
of a feature map itself, bridging the gap between the VQC and QSVM paradigms.
Explore the integration of VQCs with classical deep learning frameworks using the
TorchConnector, which allows quantum layers to be embedded within larger
PyTorch models.
By building upon the foundation laid in this report and actively engaging with these
open challenges and advanced techniques, one can begin to contribute to the
ongoing quest to unlock the power of quantum computation for machine learning.
Works cited