††thanks: These authors contributed equally††thanks: These authors contributed
equally††thanks: These authors contributed equally

# sQUlearn – A Python Library for Quantum Machine Learning

David A. Kreplin<EMAIL_ADDRESS>Fraunhofer Institute for
Manufacturing Engineering and Automation IPA, Nobelstraße 12, D-70569
Stuttgart, Germany  Moritz Willmann  Fraunhofer Institute for Manufacturing
Engineering and Automation IPA, Nobelstraße 12, D-70569 Stuttgart, Germany
Jan Schnabel  Fraunhofer Institute for Manufacturing Engineering and
Automation IPA, Nobelstraße 12, D-70569 Stuttgart, Germany  Frederic Rapp
Fraunhofer Institute for Manufacturing Engineering and Automation IPA,
Nobelstraße 12, D-70569 Stuttgart, Germany  Marco Roth
<EMAIL_ADDRESS>Fraunhofer Institute for Manufacturing
Engineering and Automation IPA, Nobelstraße 12, D-70569 Stuttgart, Germany

###### Abstract

sQUlearn introduces a user-friendly, NISQ-ready Python library for quantum
machine learning (QML), designed for seamless integration with classical
machine learning tools like scikit-learn. The library’s dual-layer
architecture serves both QML researchers and practitioners, enabling efficient
prototyping, experimentation, and pipelining. sQUlearn provides a
comprehensive toolset that includes both quantum kernel methods and quantum
neural networks, along with features like customizable data encoding
strategies, automated execution handling, and specialized kernel
regularization techniques. By focusing on NISQ-compatibility and end-to-end
automation, sQUlearn aims to bridge the gap between current quantum computing
capabilities and practical machine learning applications.

##  I Introduction

Machine Learning (ML) is a remarkably successful discipline that has been
rapidly adopted broadly in science, industry and society, and is widely
believed to have the potential to completely transform a wide range of
industries in the upcoming years [1]. While its recent ascent to prominence is
notable, the roots of ML extend back to the early 1960s [2], reflecting a path
of varied progress and challenges. Besides some initial set-backs [3] the
foundations of today’s deep neural networks that drive a lot of the recent
advancements have already been established almost half a century ago [4, 5].
The reasons for the accelerated breakthroughs of the past decade are
essentially threefold: (a) an increased computational resources, (b) the
availability of large-scale data, and (c) the emergence of development tools
that abstract away low-level complexity. While these factors have enabled
remarkable breakthroughs, the evolving landscape of ML presents new frontiers,
one of which is quantum machine learning (QML).

Quantum machine learning has emerged as an innovative approach that explores
different capabilities and potentials within the field, leveraging the
principles of quantum mechanics to enhance computational power and efficiency
[6]. Some techniques seek to accelerate performance by executing quantum
variants of linear algebra procedures [7, 8, 9, 10], effectively using the
quantum computer as an hardware accelerator, similar to a GPU. However, these
methods usually involve deep quantum circuits with many gates. The resulting
complexity often exceeds the capabilities of current noisy intermediate-scale
quantum (NISQ) hardware [11]. This has spawned an increased interest in NISQ-
compatible models which are often not just quantum-enhanced versions of
classical algorithms but rather models that have an intrinsic quantum nature.
Quantum kernel methods, for example, have been shown to have the potential to
outperform their classical counterparts in specific tasks, making QML an
attractive domain for further research and practical applications [12, 13].

Despite these successes, the situation of QML today is comparable to that of
classical ML a few decades ago. Translating the success factors (a)–(c) of
classical ML to QML, we observe that the progress of the hardware developments
poses a major bottleneck for the adaptability of QML algorithms for practical
usage [cf. (a)]. In terms of data [cf. (b)], we find that QML can mostly
benefit from the tremendous amounts of classical data available, although this
is not necessarily true for quantum data [6]. As for development tools, most
of the tools for QML are extensions of low-level quantum computing packages
that require exhaustive knowledge in quantum computing and ML and often
require manipulations on the qubit level [14, 15, 16, 17, 18]. In this work,
we aim to bridge the gap for the success factor (c) and introduce sQUlearn, an
easy-to-use and NISQ-ready python library for QML which aims to democratizing
access to QML.

Given the close relationship between the fields of ML and QML, we strive for
high compatibility with already available tools. scikit-learn [19] is a
widely-used Python library for ML, well known for its simple and effective API
[20]. Leveraging this, sQUlearn provides an scikit-learn interface for QML
methods which allows for a seamless integration into a wide range of available
tools ranging from scikit-learn-based pipelines over MLOps [21, 22] to AutoML
[23, 24]. Besides technical streamlining, this approach also has the benefit
of a shallow learning curve for practitioners already familiar with tools for
classical ML. The library offers a variety of high-level implementations of
quantum neural networks (QNN) [25] and quantum kernel methods [26] in various
flavors such as quantum support vector machines (QSVM) [27] and quantum
Gaussian processes [28] which can be utilized for classification and
regression.

Figure 1: Schematic overview of sQUlearn. sQUlearn offers scikit-learn
compatible high-level interfaces for various kernel methods and QNNs. They
build on top of the low-level interfaces of the QNN engine (Sec. III.1) and
the quantum kernel engine (Sec. III.2). The executor is used to run
experiments on simulated and real backends in the Qiskit environment.

These high-level methods are based on low-level implementations which provide
advanced tools for QML researchers. These features include a flexible QNN
engine that allows for arbitrary differentiation, tools for designing quantum
circuits such as simple encoding circuit creation and analysis including the
detection and removal of redundant parameters [29]. In terms of quantum kernel
methods both, fidelty-type (FQK) [27] and projected quantum kernels (PQK) [30]
are featured in a general and versatile implementation. The execution and
circuit-management is based on Qiskit [14]. Therefore, all algorithms can
easily be executed on IBM hardware and Qiskit-based simulators. The provided
execution tools include several convenience features such as automated session
handling and allow for quick transition from simulations to real quantum
computers. An overview of sQUlearns structure is shown in Fig. 1.

sQUlearn is developed to accommodate users with knowledge from diverse
backgrounds, enabling effective utilization regardless of the user’s primary
field of expertise. For people already familiar with ML and scikit-learn, the
manuscript provides colored info boxes which give a quick overview of how the
high-level methods can be used. The remainder of this manuscript is structured
as follows. Section II relates sQUlearn to similar work and frameworks.
Section III introduces the fundamental concepts behind QNNs and quantum kernel
methods and details their respective implementation in sQUlearn. An
illustrative example involving some of sQUlearn’s unique aspects is
demonstrated in Sec. IV. Finally, Sec. V concludes the manuscript.

##  II Similar Work

In the following, we provide an overview of the most commonly used QML-related
Python packages. Afterwards, we highlight the unique features that set
sQUlearn apart.

Qiskit [14] is a general open-source software development kit for gate based
quantum computing. It offers a direct hardware access to a wide selection of
backends with superconducting qubits and several simulators. Qiskit has a
dedicated ML package called "Qiskit Machine Learning" that offers various low-
level implementations for quantum kernels and also a high-level QSVM
implementation. The quantum kernels are restricted to fidelity kernels.
Additionally, high- and low-level implementations of QNNs are available.
Although the high-level implementation provides a simple interface, it
enforces a strict separation of input encodings and the ansatz with trainable
weights, limiting features such as combining input data with weights or using
repeated layers for data re-uploading. While the low-level QNN implementation
offers a large flexibility in the circuit design, derivatives are, however,
limited to first-order due to the absence of higher-order derivatives in
Qiskit primitives. Furthermore, the observables in the QNN can not be
parameterized, making them non-trainable.

TensorFlow Quantum [15] is an extension of Cirq [17] that primarily focusses
on low-level development. It integrates with the classical ML library
TensorFlow, enabling automatic gradient back-propagation for quantum circuits
[31]. This facilitates the training of hybrid quantum-classical ML models and
allows the use of the Keras API for QML models [32]. TensorFlow Quantum
focuses on QNNs and does not provide pre-built quantum kernel methods. Cirq,
on the other hand, is a general quantum computing framework for gate-based
quantum computing, offering the capability to implement quantum circuits and
run them on various simulators and hardware architectures.

PennyLane [16] is a quantum computing framework with a focus on differentiable
programming and QML. It offers automatic differentiation for quantum circuits,
enabling the implementation of quantum circuits for various hybrid QML
methods. PennyLane furthermore offers multiple templates for data encoding and
entangling layers, that can be used to construct a parameterized quantum
circuit. It also offers integration into classical ML libraries such as
PyTorch [33] and Keras allowing the construction of complex and hybrid ML
models. However, simple high-level interfaces are not available. Nevertheless,
quantum kernels constructed using PennyLane can be utilized as input for
scikit-learn’s high-level kernel implementation. PennyLane offers various
built-in simulators as well as a plugin structure to connect to other
simulator- and hardware architectures. The usage of automatic differentiation
libraries enables a fast computation of derivatives of quantum circuits for
noise-free simulations.

QuASK (Quantum Advantage Seeker with Kernels) [18] is a python framework to
run QML experiments with quantum kernels. The software is built on top of
PennyLane for defining quantum circuit software and utilizes scikit-learn. It
is designed to automate each phase of a quantum kernel QML experiment from the
selection of the dataset (both classical and quantum) and its preprocessing to
calculating metrics of resulting Gram matrices such as the geometric
difference [30] to provide an assessment about potential quantum advantage.
Parameterized quantum kernels (FQK and PQK) can be trained using gradient-
descent-based optimization, grid search or genetic algorithms. The PQK
implementation of QuASK is restricted to linear PQKs and radial basis function
(RBF) outer kernels, both based on one-particle reduced density matrices (see
Sec. III.2.1 for details).

sQUlearn distinguishes itself from these frameworks in several aspects: It is
the only framework that provides a unified high- and low-level implementation
for fidelity and projected quantum kernels as well as QNNs. Furthermore, it
offers several unique features such as the simplified generation of encoding
circuits from strings, flexible QNN architectures capable of calculating
arbitrary derivatives, automatic shot-adjustments and variance-regularization.
Additionally, sQUlearn provides an extensive flexibility in creating PQKs. A
large variety of outer kernels beside the RBF kernel are available, as for
example the Matern kernel or the Rational Quadratic kernel. Moreover, with its
automated session handling for real quantum hardware, sQUlearn has a focus on
employing and training ML models on currently available backends, allowing
users to explore NISQ hardware while focusing on problem-solving rather than
managing computational resources. sQUlearn serves as a one-stop-shop for
today’s QML algorithms, integrating them into a single library and offering a
seamless interface with classic ML packages like scikit-learn. Finally,
through its dual-layer approach sQUlearn caters to both QML researchers and
practitioners. Advanced users can access low-level functionalities for
research, while practitioners can quickly implement ML models using high-level
APIs and the wide variety of available models.

##  III The sQUlearn Library

In the following, we consider supervised learning problems with a
classical111Although we assume classical data here, sQUlearn can cope with
quantum states as input as well. dataset $\mathcal{D}=(X,y)$, where
$X=\\{\mathbf{x}_{1},\dots,\mathbf{x}_{N}\\}$ is a set of $N$ data points
$\mathbf{x}_{i}\in\mathcal{X}$ with labels $y_{i}\in\mathcal{Y}$. Here,
$\mathcal{X}$ is the data space which is here chosen as a subspace of
$\mathbb{R}^{d}$ for simplicity. Typical scenarios for the output domain
include regression problems with $\mathcal{Y}\subset{\mathbb{R}}$ and
classification tasks with $\mathcal{Y}\subset\mathbb{N}$, although sQUlearn
supports multidimensional output as well.

To enable quantum processing, the data is encoded into a quantum state

$\ket{\psi(\mathbf{x},\pmb{\theta})}=U(\mathbf{x},\pmb{\theta})\ket{0}\,,$ (1)

In practise the encoding operator $U(\mathbf{x},\pmb{\theta})$ is implemented
by a quantum circuit which manipulates an initial $n$-qubit quantum state
$\ket{0}$ based on the data $\mathbf{x}\in\mathcal{X}$. While various
proposals for encoding such as encoding data directly into the amplitudes of
the wavefunction or bit-wise into the basis states have been suggested [34],
the predominant approach for NISQ compatible algorithms is based on encoding
the components $x^{i}$ of the data vectors $\mathbf{x}$ as angles in one or
two-qubit rotations. Typically, these operations are supplemented with data
independent gates parameterized by some parameters
$\pmb{\theta}=(\theta_{1},\dots,\theta_{K})$, with $\theta_{i}\in\mathbb{R}$.
The resulting circuit is often referred to as encoding circuit. Since this is
the defining object for a wide range of algorithms, sQUlearn provides several
pre-implemented encoding circuits (see Info Box III) and offers a variety of
tools for analysing and creating custom encodings. Redundant parameters can be
identified and removed automatically from the encoding circuit. This is
achieved by utilizing the Quantum Fisher Information Matrix [35, 36] and an
black-box implementation of the Algorithm introduced in Ref. 29. Furthermore,
differentiation with respect to arbitrary parameters or data vector components
is implemented for encoding circuits by employing the parameter-shift rule.

[htb]  Info Box 1: Data Encoding and Feature Maps Data encoding circuits are
used to embed classical data into a quantum state and manipulate the quantum
state via trainable parameters $\pmb{\theta}$. They are a key component of
many QML algorithms, and the design of a good data encoding circuit is crucial
for the performance of the algorithm. In sQUlearn, encoding circuits are a
mandatory input for QNNs or quantum kernel programs. sQUlearn offers a wide
range of pre-implemented quantum encoding circuits, which can be combined to
create more sophisticated encodings. For example, the encoding circuit
introduced in Ref. 37 can be created and displayed with the following code ⬇
from squlearn.encoding_circuit import ( HighDimEncodingCircuit)
encodig_circuit = HighDimEncodingCircuit( num_qubits=5, num_features=23,
num_layers=2) encodig_circuit.draw("mpl")

The two core classes of algorithms in sQUlearn are QNNs and quantum kernel
methods. While both methods can be shown to be equivalent in theory [38], in
practise they have their individual advantages and disadvantages. In the first
approach, short-depth parametrized quantum circuits are executed on a quantum
computer, with the optimization of the parameters being conducted classically.
The second approach relies on the quantum computer only for evaluating the
Gram matrix, while the subsequent processes typically employ established
classical algorithms such as support vector machines (SVM) [39] or kernel
ridge regression (KRR) [40]. In sQUlearn, QNNs and kernel methods are driven
by two separate low-level implementations which will be explained in the
following.

###  III.1 Quantum Neural Network Engine

Figure 2: Prototypical architecture of a QNN. Classical data is embedded into
quantum states using encoding operations $S_{l}(\mathbf{x})$. Parameterized
quantum operations $W_{l}(\theta)$ are then applied to process the data.
Often, this structure is repeated for several layers. After the final layer, a
measurement is performed and an expectation value $f$ is calculated, see Eq.
(2). The parameters $\pmb{\theta}$ are then optimized using a classical
optimizer to minimize a suitable empirical loss function $\mathcal{L}$, Eq.
(3). The figure is adapted freely from Ref. [41].

In this section, we briefly introduce the basic principles of QNNs and
demonstrate how they are implemented in the sQUlearn library.

####  III.1.1 Background

Quantum neural networks are variational models that calculate an expectation
value to optimizes a cost function. An example of a QNN can be seen in Fig. 2.
Data is embedded into a quantum state using a data encoding circuit Eq. (1).
The encoding circuit $U(\mathbf{x},\pmb{\theta})$ often comprises a layered
structure
$U(\mathbf{x},\pmb{\theta})=\prod_{l=1}^{L}S_{l}(\mathbf{x})W_{l}(\pmb{\theta}_{l})$,
where $S_{i}(\mathbf{x})$ denotes a data-dependent operation,222In principle,
$S_{j}(\mathbf{x})$ can depend on additional parameters
$S_{j}(\mathbf{x})\equiv S_{j}(\mathbf{x},\vartheta)$.
$W_{i}(\pmb{\theta}_{i})$ are parameterized operators and $L$ is the number of
layers. The resulting state $\rho(\mathbf{x},\pmb{\theta})$ is measured
repeatably to calculate the expectation value of some observable $O$

$f(\mathbf{x},\pmb{\theta})=\tr\left(\rho(\mathbf{x},\pmb{\theta})O\right)\,.$
(2)

The parameters $\pmb{\theta}$ are optimized by minimizing the empirical loss

$\mathcal{L}(\pmb{\theta})=\frac{1}{N}\sum_{i=1}^{N}\ell(\mathbf{x}_{i},y_{i},\pmb{\theta})\,,$
(3)

where $\ell$ is a suitable loss function such as the Euclidean distance
between $f(\mathbf{x}_{i})$ and $y_{i}$. The minimization of $\mathcal{L}$ is
typically performed utilizing gradient-based techniques like stochastic
gradient descent [42]. The gradient is usually obtained using either finite
differences or by exploiting the parameter shift rule [43]. If a parameter
$\theta$ appears in a single rotation gate
$R_{\alpha}(\theta)=\exp(-i\theta\sigma_{\alpha}/2)$, with $\sigma_{\alpha}$
being a Pauli operator with $\alpha=x,y,z$, the parameter-shift rule can be
expressed as follows [43, 44]:

$\frac{d}{d\theta}\braket{\tilde{\psi}}{{R}_{\alpha}[\varphi(\theta)]{\tilde{O}}{R}_{\alpha}[\varphi(\theta)]}{\tilde{\psi}}\\!=\\!\frac{1}{2}\varphi^{\prime}(\theta)\\!\big{[}\\!\braket{{O}}^{+}\\!\\!\\!-\braket{{O}}^{-}\\!\big{]}\\!.$
(4)

Here, the term $\braket{{O}}^{\pm}$ denotes the expectation value of ${O}$
with the parameter $\theta$ shifted by $\pm\pi/2$:

$\braket{{O}}^{\pm}=\braket{\tilde{\psi}}{{R}_{\alpha}(\varphi(\theta)\pm\tfrac{\pi}{2}){\tilde{O}}{R}_{\alpha}(\varphi(\theta)\pm\tfrac{\pi}{2})}{\tilde{\psi}}.$
(5)

In this example, the function $\varphi(\theta)$ is an optional and typically
non-linear map for the input data. The operator ${\tilde{O}}$ encompasses all
gates applied after the rotation gate ${R}_{\alpha}$ that depends on $\theta$.
Similarly, $\ket{\tilde{\psi}}$ includes all gates applied before the rotation
gate. If the parameter $\theta$ is present in multiple gates, the product rule
of differentiation is applied, resulting in an additional summation over all
occurrences. Higher-order derivatives can be obtained through successive
applications of the parameter-shift rule. The parameter-shift rule yields an
exact gradient and calls for the same two circuit evaluations as the finite
differences approach when the parameters are exclusively found in a single
rotation gate.

Quantum neural networks have been applied to a variety of problems [6, 45] and
have been shown to have interesting properties in terms of expressivity [46]
and data efficiency [47]. sQUlearn offers a custom QNN implementation based on
Qiskit circuits and operators. One of the core features of sQUlearn’s QNN
engine is the ability to take arbitrary derivatives which readily allows for
the application in advanced tasks such as solving differential equations [44].
The training process is significantly streamlined and automated, allowing for
the use of various classical optimizers, mini-batching and stochastic gradient
descent and advanced regularization techniques such as the one in [48]. The
QNN implementation allows for a straightforward generalization to multiple
outputs [49]. In designing sQUlearn, we have paid particular attention to
facilitating training on real quantum hardware, as detailed in Sec. III.3.

[htb]  Info Box 2: QNNs and Observables In sQUlearn, a QNN is created when
observables are combined with a trainable encoding circuit. Observables are
quantum mechanical operators employed to determine expectation values of a
quantum state. In sQUlearn, observables can be constructed from Pauli
operators. A variety of preconfigured observables is available, and in
addition, custom observables can be effortlessly constructed from strings.
Furthermore, arithmetic operations between observables are also implemented.
Training the QNN optimizes the trainable parameters of the encoding circuit
and the observables to minimize a loss function that quantifies the quality of
the QNN. Derivatives with respect to parameters or input data can be obtained
using the parameter-shift rule. The construction and training of a QNN in
sQulearn can be achieved with just a few lines of code: ⬇ from squlearn import
Executor from squlearn.encoding_circuit import ( YZ_CX_EncodingCircuit) from
squlearn.observables import IsingHamiltonian from squlearn.optimizers import
Adam from squlearn.qnn import QNNRegressor, SquaredLoss pqc =
YZ_CX_EncodingCircuit( num_qubits=5, num_features=2, num_layers=2) obs =
IsingHamiltonian(num_qubits=5) qnn = QNNRegressor( pqc, obs, Executor(),
SquaredLoss(), Adam()) qnn.fit(x_train, y_train) y_test = qnn.predict(x_test)
The example first initializes the encoding circuit introduced in Ref. 50 and
an Ising Hamiltonian as an observable. Afterwards the QNNRegressor is
initialized; alternatively the classifier QNNClassifier could be used for a
classification problem. The training of the QNN with training data x_train and
labels y_train is executed by calling the fit function. The last line shows
the inference of the QNN on some test data x_test utilizing the predict
method.

####  III.1.2 Implementation

In sQUlearn, QNNs are constructed from parameterized data encoding circuits
$U(\mathbf{x},\pmb{\theta})$ and observables $O$ for defining the outputs, see
Eqs. (1) and (2). sQUlearn provides dedicated data structures for the
convenient utilization and construction of both of these elements. Complex
objects can be created by assembling predefined components or by employing
custom implementations specified through strings. Additionally, sQUlearn
offers flexibility in adjusting hyperparameters after initializing the QNN,
such as the number of qubits or the number of layers in the encoding circuit.
It is possible to optimize these hyperparameters through methods like grid
search implemented in scikit-learn [19] or similar approaches. This is
demonstrated for PQKs (which are based on the low-level QNN engine) in Sec.
IV.

Figure 3: Schematic overview of the QNN engine in sQUlearn: The central box in
the diagram represents the high-level implementation that features the
training and evaluation of QNNs with the scikit-learn interface. The elements
surrounding the box are essential for initializing the high-level methods, and
include the encoding circuit, output observables, the Executor, the loss
function, and the optimizer. Within the high-level implementation, the
encoding circuit, observables, and the Executor are utilized to set-up the
low-level QNN implementation. This low-level implementation is also available
as a standalone object in sQUlearn, and it is responsible for the evaluation
of the QNN and its derivatives. Utilizing the scikit-learn RegressorMixin or
ClassifierMixin creates the sQUlearn high-level QNN implementations, namely
QNNRegressor and QNNClassifier, designed for regression and classification
tasks.

sQUlearn offers the computation of arbitrary derivatives with respect to both
parameters and features of the data encoding circuits. Similarly, observables
can be parametrized which makes them fully differentiable as well. This is
realized through a custom data structure designed to handle circuit outputs
and expectation values with arithmetic operations. We employ the parameter-
shift rule to calculate circuit derivatives and apply it successively for
higher-order derivatives. Furthermore, we optimize efficiency by caching all
circuit and observable derivatives, ensuring they are computed only once. The
evaluation of expectation values is facilitated using Qiskit’s Estimator and
Sampler primitives, all managed by our Executor class (refer to Sec. III.3).

Error mitigation is provided through these primitives, making all options
implemented in Qiskit accessible. However, if the aim is to execute the QNN on
a real quantum backend, it is highly advisable to train the QNN on that
specific backend to enhance result accuracy. This approach allows for the
optimization of parameters to effectively counteract systematic errors
inherent in the real quantum hardware [51, 25, 48]. The evaluation routines
are optimized to ensure that each circuit is assessed only once, especially
when measurements can be utilized in multiple expectation values. This
includes scenarios where there are multiple observables with the same
measurement basis, observable derivatives, or variance calculations. This
feature is available when measuring probabilities utilizing the Sampler
primitive instead of expectation values. This becomes particularly useful on
real quantum hardware to reduce the number of costly circuit evaluations.
Furthermore, to minimize the need for repeated evaluations, all calculated
expectation values are cached.

sQUlearn also offers two high-level implementations for QNNs and their
training: the QNNRegressor, designed for optimizing regression tasks, and the
QNNClassifier, tailored for classification problems, respectively (see Info
box III.1.1). To utilize these high-level routines, they need to be
initialized by specifying an encoding circuit, observables, the Executor, the
loss function, and an optimizer, as illustrated in Fig. 3. Training and
inference processes align with the familiar scikit-learn interface, using the
functions fit and predict.

In the following, we provide a code example illustrating how to set up and
train a QNN. In this specific setup, we solve a regression task for a one
dimensional function. We also incorporate variance regularization to mitigate
the finite-sampling noise in the output and adaptively control the number of
shots that are employed in the gradient circuit evaluation as outlined in Ref.
[48]. The full executable code is displayed in Appendix A.

⬇

encoding_circuit = ChebyshevPQC(

num_qubits=10, num_features=1, num_layers=3)

observable = SummedPaulis(num_qubits=10)

qnn = QNNRegressor(

encoding_circuit,

observable,

Executor("qasm_simulator"),

SquaredLoss(),

Adam(options={"lr": 0.3}),

variance=0.005,

shot_control=ShotsFromRSTD())

qnn.fit(x_train, y_train)

qnn.predict(x_test)

The example begins by initializing the encoding circuit with ten qubits and
three layers. The number of features is equal to one since $X$ is one
dimensional. In this example, the same feature is encoded in gates applied to
each of the ten qubits, repetitively. Next, the observable is defined as the
sum of single Pauli operators, each acting on a single qubit (with the default
being the Pauli-$Z$ operator). The QNN for regression is set up using the
QNNRegressor class. This includes the QasmSimulator-based Executor for circuit
execution, the loss function (squared error loss), and the optimizer (Adam).
Optional settings for variance regularization and adaptive shot control, named
variance and shot_control, are specified. Finally, the QNN is trained on the
training data (x_train and labels y_train) using the fit function. Predictions
for a given test dataset (x_test) are obtained with the predict method.

###  III.2 Quantum Kernel Engine

Quantum kernel methods are appealing because they allow QML to be formally
embedded into the rich and powerful mathematical framework of classical kernel
theory [38, 52]. The following first outlines the most important theoretical
aspects of quantum kernels before discussing their implementation within the
sQUlearn package as well as the interface to the respective classical kernel
methods (cf. Info box III.2.2).

####  III.2.1 Background

The key idea of kernel methods is to find and analyze patterns by transforming
data into a high (potentially infinite) dimensional feature space
$\mathcal{F}$, where the learning problem attains a trivial form. The mapping
of the data from the original input space $\mathcal{X}$ to $\mathcal{F}$ is
accomplished by a feature map $\phi:\mathcal{X}\to\mathcal{F}$. The feature
space can be accessed through inner products of the feature vectors
$\phi(\mathbf{x})$, which is a function $K$ of two data points
$\mathbf{x},\mathbf{x}^{\prime}$ called the kernel, i.e.,

$K(\mathbf{x},\mathbf{x}^{\prime})=\langle\phi(\mathbf{x}),\phi(\mathbf{x}^{\prime})\rangle_{\mathcal{F}}\,.$
(6)

This operation avoids evaluating the feature map explicitly and thus makes the
computation efficient. This is known as the kernel trick. One of the main
achievements of classical kernel theory is the representer theorem [52]. It
guarantees that the function which minimizes a regularized empirical risk Eq.
(3) can always represented as a finite (length of the training sample)
weighted linear combination of the kernel between some
$\mathbf{x}\in\mathcal{X}$ and the training data $X$. It can be shown that for
a convex loss function, this is a convex optimization problem, which makes
them interesting in principle, although they usually scale unfavorable with
the amount of training data [52].

Quantum kernel methods can be formulated as a classical kernel method (e.g.
SVM) whose kernels are computed by a quantum computer. Using quantum kernels
instead of classical kernels holds out the prospect of designing ML models
that are able to deal with complex problems that are out of reach for
conventional ML methods [12]. This is achieved by embedding data into a
quantum state using a quantum feature map. In practice, this corresponds to
applying a data encoding circuit as defined in Eq. (1). Note that here we have
omitted the explicit dependence of the quantum state on (variational)
parameters for clarity. Nevertheless, it is important to keep in mind that if
the encoding circuit includes variational parameters $\pmb{\theta}$, the
kernel matrix depends on these parameters, too. This can be exploited to
create a more suitable embedding into the feature space, as shown in the
example in Sec. III.2.2.

In quantum computing, access to the quantum Hilbert space is given by
measurements, which can also be expressed by inner products of quantum states,
cf. Fig. 4. Thus, the kernel function can be defined using the native geometry
of the quantum state space: the Hilbert-Schmidt inner product
$\tr[\rho(\mathbf{x})\rho(\mathbf{x}^{\prime})]$. For pure states this reduces
to

$K^{\rm
FQK}(\mathbf{x},\mathbf{x}^{\prime})=\absolutevalue{\braket{\psi(\mathbf{x})}{\psi(\mathbf{x}^{\prime})}}^{2}\,,$
(7)

which, as it is based on a fidelity-type metric, is often called the fidelity
quantum kernel (FQK) [30]. Rewriting $K^{\rm
FQK}(\mathbf{x},\mathbf{x}^{\prime})=\absolutevalue{\bra{0}U^{\dagger}(\mathbf{x})U(\mathbf{x}^{\prime})\ket{0}}^{2}$
reveals that in practise every Gram matrix element can be obtained by
measuring the zero state after applying the quantum feature map and its
Hermitian conjugate [53].

With increasing problem size, FQKs can potentially suffer from exponential
concentration leading to quantum models that can become untrainable [54]. To
alleviate this problem, Ref. [30] introduced a family of projected quantum
kernels (PQK) which project the quantum states to an approximate classical
representation by using, e.g., reduced physical observables, cf. Fig. 4. A PQK
can be thought of defining features in a classical vector space by taking a
detour through a quantum Hilbert space. The result can be hard to compute
without a quantum computer due to the quantum detour but still retains
desirable properties of the classical feature space. They thus have several
appealing properties compared to FQKs.

Figure 4: Schematic illustration of the function principle of quantum kernel
methods, which can be formally embedded into the rich mathematical framework
of conventional kernel theory. Data points $\mathbf{x}$ are mapped to the
quantum Hilbert space by encoding them into a quantum state
$\ket{\psi(\mathbf{x})}$. Access to the quantum Hilbert space is granted by
measurements. Left: In quantum mechanics, the straightforward way to express
measurements is given by the Hilbert-Schmidt inner product. Quantum kernels
which are defined using this fidelity-type metric are thus referred to as
fidelity quantum kernels (FQKs). For pure states this reduces to Eq. (7).
Right: When projecting the quantum states to an approximate classical
representation using, e.g., reduced physical observables this gives rise to a
family of projected quantum kernels (PQKs). One of the simplest forms of
defining PQKs is to measure $k$-particle reduced density matrices, cf. Eq.
(8), and process the result with a classical kernel function $h$. The
resulting quantum kernel matrices (either FQK or PQK) are subsequently passed
to a classical kernel algorithm such as a SVM or a GP.

A simple way of defining a PQK is based on measuring $k$-particle reduced
density matrices ($k$-RDMs),

$\rho_{S_{k}}(\mathbf{x})=\mathrm{tr}_{j\notin
S_{k}}\left[\rho(\mathbf{x})\right]\,,$ (8)

where $\mathrm{tr}_{j\notin S_{k}}$ is the partial trace over all qubits not
in a subset $S_{k}$ of $k$ qubits. The result is then processed in a classical
kernel function $h$. Since Eq. (8) just depends on one data point at a time
and the pair-wise Gram matrix is only calculated classically, for a proper
choice of $h$, PQKs admit a linear scaling in terms of the needed quantum
computing resources.

Measuring $k$-RDMs with respect to some observable, corresponds to evaluating
a QNN for a $k$-local cost operator $O_{k}$. Thus, in sQUlearn, PQKs are
generally defined as

$K^{\rm
PQK}(\mathbf{x},\mathbf{x}^{\prime})=h\left[f_{k}(\mathbf{x}),f_{k}(\mathbf{x}^{\prime})\right]\,,$
(9)

where $f_{k}$ is given in Eq. (2) with a local observable $O_{k}$.

As an example, the default PQK implementation in sQUlearn is given by
measuring the 1-RDM on all qubits with respect to all Pauli operators
$P\in\\{\sigma_{x},\sigma_{y},\sigma_{z}\\}$, i.e.,

$K^{\rm
PQK}(\mathbf{x},\mathbf{x}^{\prime})=\exp\left(-\gamma\sum_{k,P}\left\\{\tr[P\rho_{k}(\mathbf{x}\bm{)}]-\tr[P\rho_{k}(\mathbf{x}^{\prime})]\right\\}^{2}\right)\,,$
(10)

where $\gamma\in\mathbb{R_{+}}$ is a hyperparameter and
$\rho_{k}(\mathbf{x})=\mathrm{tr}_{j\neq k}\left[\rho(\mathbf{x})\right]$ is
the 1-RDM for qubit $k$, i.e., the partial trace of the quantum state
$\rho(\mathbf{x})$ over all qubits except for the $k$-th qubit. This results
in an RBF-like structure of the kernel.

####  III.2.2 Implementation

The quantum kernel engine of sQUlearn is designed and implemented as
schematically shown in Fig. 5. In terms of high-level kernelized ML methods,
sQUlearn provides several conventional kernel algorithms based on quantum
kernel matrices for solving regression and classification problems. These
methods come with an analogue easy-to-use interface like the scikit-learn
estimators as demonstrated in Info Box III.2.2.

The user can choose between FQKs and PQKs which can be constructed from
various (parametrized) data encoding circuits and which can be computed on
either simulators or real backends via the Executor class (cf. Sec III.3).

The FidelityKernel class is a wrapper to Qiskit’s quantum kernel
implementations. Depending on the user definition of the Executor and the
choice of the data encoding circuit (i.e., the presence/absence of trainable
parameters), sQUlearn automatically selects an implementation based on the
proper Qiskit quantum kernel class.

Since sQUlearn implements PQKs by evaluating QNNs for $k$-local cost
operators, cf. Eq. (9), the ProjectedQuantumKernel class builds upon
sQUlearn’s low-level QNN implementation. Here, the QNN results are used as
features for some outer kernel. The default PQK setting within sQUlearn is
given in Eq. (10) but in addition, there are is a wide selection of additional
outer kernels available and cost operators can be customized. A corresponding
example is given in Appendix B.1, demonstrating how to define a custom cost
operator as well as changing the outer kernel.

Both fidelity and projected kernels can be used with quantum feature maps that
include trainable parameters yielding quantum kernels that become
variationally trainable. They can be optimized using several optimization
strategies. sQUlearn offers loss functions including kernel target alignment
and negative-log-likelihood, along with several optimizers such as Adam and
SLSQP. Beyond that, the composition of multiple quantum kernels is possible,
allowing for tailored solutions.

The following example illustrates the variational quantum kernel training
procedure; assuming some training data (x_train and y_train) have to be
previously defined.

⬇

feature_map = ChebyshevPQC(

num_qubits=4, num_features=1, num_layers=1)

q_kernel = ProjectedQuantumKernel(

feature_map, Executor())

target_alignment = TargetAlignment(

quantum_kernel=q_kernel)

optimizer = KernelOptimizer(

loss=target_alignment,

optimizer=Adam(options={"maxiter": 20, "lr": 0.1}))

q_krr = QKRR(q_kernel)

q_krr.fit(x_train, y_train)

q_krr.predict(x_train)

In this example, a PQK is trained using the target alignment [55, 56, 57] as a
loss function and Adam as an optimizer. The kernel with the optimized
parameters is then used for a KRR. The code assumes the availability of
suitable training and test data. The full executable code can be found in
Appendix B.2.

Kernel matrices that are calculated on noisy backends (real quantum hardware
or noisy simulators) are prone to lose their positive semi-definiteness. To
preserve this necessary property, both the FidelityKernel and the
ProjectedQuantumKernel implementation feature regularization techniques such
as thresholding or Tikhonov-regularization [58, 57]. For FQKs, sQUlearn
features an additional option for mitigating depolarizing noise according to
Ref. [57]. Appendix B.3 provides an example showing how these options can be
used to improve results obtained in the presence of noise.

[htb]  Info Box 3: Quantum Kernel Methods NISQ-based quantum kernel methods
compute the Gram matrix with a quantum computer and pass it to a conventional
kernel algorithm such as an SVM. sQUlearn offers several out-of-the-box
quantum kernel methods such as quantum SVMs for classifciation (QSVC) and
regression (QSVR), quantum Gaussian processes (QGP) for classification (QGPC)
and regression (QGPR) as well as a quantum kernel ridge regression routine
(QKRR). The user can choose between FQKs and PQKs, see Eqs. (7) and (9),
respectively. Setting up a quantum kernel method can be done with just a few
lines of code ⬇ from squlearn.encoding_circuit import ( YZ_CX_EncodingCircuit)
from squlearn.kernel import FidelityKernel, QSVC from squlearn.util import
Executor feature_map = YZ_CX_EncodingCircuit( num_qubits=5, num_features=2,
num_layers=2) q_kernel = FidelityKernel( feature_map, executor=Executor())
model = QSVC(q_kernel) This initializes a QSVC using a FQK as defined in Eq.
(7). To use a PQK instead, the FidelityKernel class can be exchanged with a
ProjectedQuantumKernel class. The resulting estimator can then be used like
any scikit-learn estimator, e.g. fitting data with model.fit() and predicting
with model.predict().

Figure 5: Schematic overview of sQUlearn’s quantum kernel engine
implementation including all dependencies on other program parts. To define a
quantum kernel, the user first needs to provide an encoding circuit object
which is used for embedding data into the quantum Hilbert space (cf. Info Box
III). After defining a corresponding circuit, one can either define a
FidelityKernel, or a ProjectedQuantumKernel. The latter makes use of the QNN
Engine, cf. Fig. 3 and the Observables class. With the Executor the Gram
matrix can be evaluated on real quantum computers or a simulator backend with
automatic session handling. If the encoding circuit also contains trainable
parameters, sQUlearn provides optimization routines to optionally optimize
these parameters. With the kernel object created, or stored in from a previous
calculation the high level methods (blue) can be initialized and executed (cf.
Info Box III.2.2).

###  III.3 Backend Integration

sQUlearn provides a simple and unified approach for executing QML tasks on
real quantum computers or simulators. This is achieved by centralizing all
quantum job executions into a single module: the Executor class. It enables
users to execute longer quantum programs without the need for constant
monitoring and supervision, since it takes care of resubmitting failed jobs
and automatically re-initializing expired sessions. In the case of stochastic
finite-sampling simulations, such as the QasmSimulator, a seed can be set at
the beginning to provide reproducible results.

Currently, the Executor supports the IBM Quantum platform [59] and relies on
Qiskit’s primitive interface that enables the computation of expectation
values or probabilities with error mitigation on IBM Quantum hardware. The
creation of primitives is handled behind the scenes in each low-level method
of sQUlearn. Consequently, users are only required to initialize the Executor
class, simplifying the process of prototyping QML applications. Transitioning
from a local simulator environment to real quantum hardware is accomplished
with minimal adjustments during the Executor initialization, leaving the rest
of the program untouched.

The initialization of the Executor can be done in multiple ways, including
using a Qiskit backend, a session, or a pre-configured primitive. Initializing
with a backend also opens the door to other quantum providers, as long as
access is possible via the Qiskit’s backend class definition. In the future,
we plan to enhance the compatibility with quantum hardware beyond IBM Quantum.

Results from quantum computations are automatically cached locally and can be
reloaded when necessary. This feature proves especially useful in cases of
unexpected interruptions that require a restart, for example a training of a
QNN. Event logging offers insights into background processes during longer
runs.

The implementation of the Executor is achieved by providing Primitives that
inherit and override Qiskit’s Sampler and Estimator primitives and route every
execution through the central Executor. Since these primitives are derived
from Qiskit, they can be utilized in other parts of Qiskit as well. It’s
important to emphasize that sQUlearn’s Executor functionality is not limited
to QML applications alone; it can also be employed in other quantum programs,
such as variational quantum eigensolver [60] or similar programs derived from
Qiskit’s Sampler and Estimator primitives.

In the following example, we demonstrate the necessary steps to run the code
provided in Info Box III.2.2 on actual quantum hardware, specifically using
the IBM quantum backend ibm_brisbane.

⬇

service = QiskitRuntimeService(channel="ibm_quantum")

executor = Executor(service.get_backend("ibm_brisbane"))

executor.set_primitive_options(resilience_level=1)

feature_map = YZ_CX_EncodingCircuit(

num_qubits=3, num_features=1, num_layers=2)

q_kernel = FidelityKernel(feature_map, executor=executor)

model = QSVR(q_kernel)

First, a connection to the IBM Quantum service is established. It is assumed
that the user has previously set up access to the IBM Quantum
platform.333Refer to Ref. [61] for instructions. Subsequently, the Executor is
initialized with the backend obtained from the Qiskit service. The following
steps, such as session creation and primitive initialization, are handled
internally within the Executor. Error mitigation can be configured directly in
the settings of the Executor. The remaining part of the example is analogous
to Info Box III.2.2, except that the new Executor is utilized. The executable
code including the imports is presented in Appendix C. The example illustrates
how sQUlearn enables a effortless transition from the simulation environment
to the real backend, requiring only changes in the Executor initialization.

##  IV Example: Creating a ML pipeline

In this example, we demonstrate how sQUlearn based algorithms can be embedded
in standard ML workflows by performing a architecture search and hyperpameter
optimization using scikit-learn methods. We illustrate this by optimizing a
QSVM over several feature maps, circuit parameters and classical
hyperparameters of the SVM.

We start by creating a classification dataset with $100$ data points and $10$
features which are split into training and test sets with a ratio of $0.2$.

⬇

from sklearn.datasets import make_classification

from sklearn.model_selection import train_test_split

X, y = make_classification(

n_samples=100, n_features=10, random_state=0)

X_train, X_test, y_train, y_test = train_test_split(

X, y, test_size=0.2, random_state=0)

Next, we initialise a QSVC using a PQK with the high-dimensional encoding
circuit shown in Info Box III.

⬇

from squlearn import Executor

from squlearn.encoding_circuit import (

HighDimEncodingCircuit)

from squlearn.kernel import ProjectedQuantumKernel, QSVC

high_dim_ec = HighDimEncodingCircuit(

num_qubits=4, num_features=3, num_layers=2)

pqk = ProjectedQuantumKernel(

encoding_circuit=high_dim_ec,

executor=Executor("statevector_simulator"))

qsvc = QSVC(quantum_kernel=pqk)

Most of the common encoding schemes require a certain scaling in order to
preserve injectivity of the embedding. To automatically handle this, we
include the QSVC in a scikit-learn pipeline that preprocesses the data with a
MinMaxScaler that normalizes the data between $0$ and $1$. Today’s QML methods
performed on NISQ computers are often limited regarding the dimensionalty of
the dataset. We can account for by including a feature selection algorithm in
the pipeline, reducing the number of features from $10$ to $3$. The resulting
pipeline automatically performs all necessary preprocesing, enabling the QSVC
to be applied to a wide range of datasets.

⬇

from sklearn.feature_selection import SelectKBest

from sklearn.pipeline import Pipeline

from sklearn.preprocessing import MinMaxScaler

scaler = MinMaxScaler(feature_range=(0.01, 0.99))

feature_selection = SelectKBest(k=3)

pipeline = Pipeline([

("scaler", scaler),

("feature_selection", feature_selection),

("qsvc", qsvc)])

Since the encoding circuit and its structure is one of the most important
properties of a quantum kernel method, we benchmark the HighDimEncodingCircuit
against a different encoding circuit. To demonstrate sQUlearn’s capabilities,
we do not resort to a predefined encoding circuit but rather define one from
scratch using the LayeredEncodingCircuit. This feature allows us to create
encoding circuits using string-based input which is shown in the following:

⬇

from squlearn.encoding_circuit import (

LayeredEncodingCircuit)

layered_ec = LayeredEncodingCircuit.from_string(

"Ry(x)-Rz(x)-Rx(p)-cx",

num_qubits=4,

num_features=3,

num_layers=2)

Here, Rx, Ry and Ry are rotation gates where each gate in the string is
applied to all the qubits, and therefore, forms a layer (hence the name
LayeredEnodingCircuit). The symbols x and p indicate features and trainable
parameters, respectively. The number of features is set to three since the
dimensionality is reduced to three in the scikit-learn pipeline by the feature
selection. The automatic mapping of feature vector components to qubits is
managed by the implementation, with each feature being placed sequentially in
various gates, one at a time. This process occurs in a loop, ensuring that the
placement is repeated after the last component is reached. The trainable
parameters are considered to be different for every single gate, and the
dimension of parameter vector is determined automatically. The layered
encoding circuit supports not only the displayed rotation and CNOT gates but
also various other one- and two-qubit gates. Furthermore, it allows for custom
non-linear input encoding functions and different layers. For more detailed
information on the construction, please refer to the sQUlearn documentation
[49].

Having set up the pipeline and the encoding circuits which we want to compare,
we now initialize a grid search to identify the optimal circuit configuration
within a predefined search space. In this search, we not only include two
different encoding circuits but also optimize over the number of qubits and
layers, the outer kernel of the PQK as well as the classical hyperparameters
of the SVM in a single operation.

⬇

from sklearn.model_selection import GridSearchCV

param_grid = {

"qsvc__encoding_circuit": [layered_ec, high_dim_ec],

"qsvc__outer_kernel": ["Gaussian", "DotProduct"],

"qsvc__num_qubits": [3, 4],

"qsvc__num_layers": [1, 2, 3],

"qsvc__C": [0.01, 0.1, 1.0]}

grid_search = GridSearchCV(pipeline, param_grid, cv=5)

grid_search.fit(X_train, y_train)

In the code above, the prefix qsvc__ is required when using a scikit-learn
pipeline instead of an estimator directly. The example returns the trained
QSVC with an optimal configuration of the encoding circuit and the SVM within
the defined search space. Similar hyperparameter optimizations, as
demonstrated in the example, can be extended to all sQUlearn high-level
methods. The effortless integration into established ML workflows not only
simplifies but also enhances the development and optimization of intricate QML
models, and it is one of the unique features of sQUlearn.

##  V Conclusion

In this work, we have introduced sQUlearn, an open-source Python library aimed
at simplifying and streamlining the development of QML models. Designed for
compatibility with existing ML tools, sQUlearn offers a scikit-learn-like
interface, enabling seamless integration into a broad array of pipelines and
frameworks. Our library targets both practitioners and researchers, offering
high-level implementations for various state-of-the-art algorithms and low-
level features for advanced development. It provides a comprehensive suite of
tools designed for effortless creation and training of custom QML models that
leverage the power of QNNs and quantum kernels. Built on top of Qiskit,
sQUlearn not only benefits from an extensive quantum computing ecosystem but
also allows for execution on IBM quantum hardware and Qiskit-compatible
simulators.

The version described in this paper is sQUlearn 0.5.0. For comprehensive
documentation, sample use-cases, and installation guidelines, we direct
readers to the sQUlearn website [49]. As with any initial software release, in
particular in a rapidly evolving field such as QML, expect the capabilities
and performance of sQUlearn to expand over time, responding to the changing
needs and discoveries in both the quantum and classical ML communities. We
thus anticipate future versions of sQUlearn to incorporate advancements such
as optimization techniques tailored for NISQ devices, access to various more
quantum hardware and simulators, data-preprocessing designed for QML
algorithms, and finally, additional QML models that leverage quantum computing
capabilities.

## Code availability

sQUlearn is an open-source software project licensed under the Apache License
2.0. The source code is accessible on
GitHub.444https://github.com/squlearn/squlearn Additional tutorials,
instructions, and an installation guide can be accessed through the
documentation.555https://squlearn.github.io All shown examples have been
written and tested with sQUlearn 0.5.0.

###### Acknowledgements.

This work was supported by the German Federal Ministry of Economic Affairs and
Climate Action through the projects AutoQML (grant no. 01MQ22002A) and AQUAS
(grant no. 01MQ22003D), as well as the German Federal Ministry of Education
and Research through the project H2Giga $\text{Degrad-EL}^{3}$ (grant no.
03HY110D). The authors would like to thank André Kempinger for his
contributions to sQUlearn.

##  Appendix A QNN Example

The following examples displays the training and inference of a QNN (cf.
example in Sec. III.1.2) by curve fitting the absolute value function with a
shot-based simulator. Furthermore, the code demonstrates the set-up of the
variance regularization and an automatic shot adjustment. The output of the
printed figure is displayed in Fig. 6.

⬇

import numpy as np

import matplotlib.pyplot as plt

from squlearn import Executor

from squlearn.encoding_circuit import ChebyshevPQC

from squlearn.observables import SummedPaulis

from squlearn.qnn import (

QNNRegressor,

SquaredLoss,

ShotsFromRSTD)

from squlearn.optimizers import Adam

# Initilize encoding circuit

encoding_circuit = ChebyshevPQC(

num_qubits=10, num_features=1, num_layers=3)

# Initilize observable

observable = SummedPaulis(num_qubits=10)

# Set-up QNN regression

qnn = QNNRegressor(

encoding_circuit,

observable,

Executor("qasm_simulator"),

SquaredLoss(),

Adam(options={"lr": 0.3}),

variance=0.005,

shot_control=ShotsFromRSTD())

# Set training data

x_train = np.arange(-0.7, 0.8, 0.1)

y_train = np.abs(x_train)

# Train the model

qnn.fit(x_train, y_train)

# Data for testing the trained model

x_test = np.arange(-0.8, 0.81, 0.01)

y_test = qnn.predict(x_test)

# Generate a plot of the QNN inference

plt.plot(x_train, y_train, "x", label="Training data")

plt.plot(x_test, y_test, label="Prediction")

plt.title("Inference of the QNN on a noisy simulator")

plt.xlabel("$x$")

plt.ylabel("QNN Output")

plt.legend()

plt.show()

Figure 6: Output of the QNN example (cf. Sec. A)

##  Appendix B Quantum Kernel Examples

This section provides the complete code for the examples referenced in the
main text in Sec. III.2.2.

###  B.1 Projected Quantum Kernel Example

The following example shows how to change the measurement operators for
evaluating expectation values in PQK calculations. Moreover, the following
code demonstrates how to change the functional form of the outer kernel.
Gaussian Process regression is used as estimator. The results are shown in
Fig. 7.

⬇

import numpy as np

import matplotlib.pyplot as plt

from qiskit.primitives import Estimator

from squlearn.encoding_circuit import (

YZ_CX_EncodingCircuit)

from squlearn.observables import CustomObservable

from squlearn.kernel import (

ProjectedQuantumKernel,

QGPR)

from squlearn.util import Executor

# Generate training & test data

def func(x):

return x * np.exp(np.sin(10 * x))

x_train = np.linspace(-1, 1, 25)

y_train = func(x_train)

x_test = np.linspace(-1, 1, 200)

y_test = func(x_test)

feature_map = YZ_CX_EncodingCircuit(

num_qubits=4, num_features=1, num_layers=2)

# Create custom observables

measurements = []

measurements.append(CustomObservable(4, "ZZZZ"))

measurements.append(CustomObservable(4, "YYYY"))

measurements.append(CustomObservable(4, "XXXX"))

# Use Matern outer kernel with nu=0.5

# as a outer kernel hyperparameter

kernel = ProjectedQuantumKernel(

feature_map,

executor=Executor(Estimator()),

measurement=measurements,

outer_kernel="matern",

nu=0.5)

q_gpr = QGPR(kernel)

q_gpr.fit(x_train, y_train)

y_pred = q_gpr.predict(x_test)

plt.plot(x_train, y_train, "x", label="Training data")

plt.plot(x_test, y_pred, label="Prediction")

plt.title("Inference of the QGPR")

plt.xlabel("$x$")

plt.ylabel("QGPR mean prediction")

plt.legend()

plt.show()

Figure 7: Output of the PQK example.

###  B.2 Quantum Kernel Optimization Example

The following provides the complete code for running the kernel optimization
example introduced in Sec. III.2.2. The results can be seen in Fig. 8

⬇

import matplotlib.pylab as plt

import numpy as np

from squlearn.encoding_circuit import ChebyshevPQC

from squlearn.kernel import (

ProjectedQuantumKernel,

QKRR)

from squlearn.kernel.optimization import (

KernelOptimizer,

TargetAlignment)

from squlearn.optimizers import Adam

from squlearn.util import Executor

x_train = np.linspace(-1, 1, 10).reshape(-1, 1)

x_test = np.linspace(-1, 1, 100).reshape(-1, 1)

y_train = np.sin(x_train.ravel() * np.pi)

feature_map = ChebyshevPQC(

num_qubits=4, num_features=1, num_layers=1)

q_kernel = ProjectedQuantumKernel(

feature_map, Executor())

q_krr = QKRR(q_kernel)

q_krr.fit(x_train, y_train)

prediction_untrained = q_krr.predict(x_test)

target_alignment = TargetAlignment(

quantum_kernel=q_kernel)

optimizer = KernelOptimizer(

loss=target_alignment,

optimizer=Adam(options={"maxiter": 20, "lr": 0.1}))

optimizer.run_optimization(X=x_train, y=y_train)

q_krr = QKRR(q_kernel)

q_krr.fit(x_train, y_train)

prediction = q_krr.predict(x_test)

plt.plot(x_train, y_train, "x", label="Training data")

plt.plot(

x_test,

prediction_untrained,

label="Prediction non-optimized")

plt.plot(

x_test, prediction, label="Prediction optimized")

plt.title("Inference of the QKRR")

plt.xlabel("$x$")

plt.ylabel("QKRR Output")

plt.legend()

plt.show()

Figure 8: Output of the quantum kernel optimization example (cf. Sec. III.2.2

###  B.3 Quantum Kernel Regularization and Mitigation Example

In general, the concept of regularization is crucial for quantum kernel
methods. This importance arises from the fact that kernels calculated on
quantum computers (or simulators) can lose their positive semi-definiteness
when exposed to noise. However, positive semi-definiteness is a central and
necessary property in kernel theory (cf. Sec.III.2.1). To uphold this
essential characteristic, both the FidelityKernel and the
ProjectedQuantumKernel implementations in sQUlearn incorporate the two
regularization techniques outlined in Ref. [57]. Moreover, when computing
Fidelity Quantum Kernels (FQKs) on real backends, sQUlearn includes an option
to mitigate depolarizing noise, as outlined in Ref. [57]. The formula for this
mitigation technique is derived by leveraging the observation that, in the
ideal scenario, all diagonal elements of training kernel matrices equal one.
While this property is inherently fulfilled for Projected Quantum Kernels
(PQKs) by construction, it is often violated for FQKs.

The utilization of both regularization and the depolarizing noise mitigation
option is exemplified in the code below. Regularization is illustrated by
printing the corresponding eigenvalues, while mitigation is showcased through
the display of diagonal elements in the kernel matrices.

⬇

import numpy as np

from qiskit.providers.fake_provider import FakeManilaV2

from squlearn.encoding_circuit import (

YZ_CX_EncodingCircuit)

from squlearn.kernel import FidelityKernel

from squlearn.util import Executor

# Generate some data to evaluate a matrix

x = np.linspace(-1.5, 1.5, 20)

enc_circ = YZ_CX_EncodingCircuit(

num_qubits=5, num_features=1, num_layers=3)

# Define the executor with a fake backend

# and set the desired number of shots

executor = Executor(FakeManilaV2(), shots=500)

# Compute the Kernel and print out the eigenvalues

FQK = FidelityKernel(

enc_circ,

executor=executor,

evaluate_duplicates="all")

kernel_matrix_1 = FQK.evaluate(x.reshape(-1, 1))

print("Eigenvalues without regularization: ",

np.linalg.eigvalsh(kernel_matrix_1))

# Switch to Tikhonov regularization and print out the

# eigenvalues for comparison

FQK.set_params(regularization="tikhonov")

kernel_matrix_2 = FQK.evaluate(x.reshape(-1, 1))

print("Eigenvalues with regularization: ",

np.linalg.eigvalsh(kernel_matrix_2))

# We now show how to mitigate depolarization noise

# in the kernel matrix

FQK_mitigated = FidelityKernel(

enc_circ,

executor=executor,

evaluate_duplicates="all",

mit_depol_noise="msplit")

kernel_matrix_3 = FQK_mitigated.evaluate(

x.reshape(-1, 1))

print("The diagonal of the unmitigated matrix is: ",

np.diag(kernel_matrix_1))

print("The diagonal of mitigated matrix is: ",

np.diag(kernel_matrix_3))

##  Appendix C Executor Example

The following example demonstrates the configuration of the Executor to
execute the code provided in Info Box III.2.2 on actual quantum hardware
(refer to the example in Sec. III.3). Furthermore, it includes the setup of
options for error mitigation.

⬇

from qiskit_ibm_runtime import QiskitRuntimeService

from squlearn import Executor

from squlearn.encoding_circuit import (

YZ_CX_EncodingCircuit)

from squlearn.kernel import FidelityKernel, QSVR

service = QiskitRuntimeService(channel="ibm_quantum")

executor = Executor(

service.get_backend("ibm_brisbane"))

executor.set_primitive_options(resilience_level=1)

feature_map = YZ_CX_EncodingCircuit(

num_qubits=3, num_features=1, num_layers=2)

q_kernel = FidelityKernel(

feature_map, executor=executor)

model = QSVR(q_kernel)

## References

  * Chui  _et al._ [2023] M. Chui, E. Hazan, R. Roberts, A. Singla, and K. Smaje, The economic potential of generative ai (2023).
  * Samuel [1959] A. L. Samuel, Some studies in machine learning using the game of checkers, IBM Journal of Research and Development 3, 210 (1959).
  * Crevier [1993] D. Crevier,  _AI: the tumultuous history of the search for artificial intelligence_ (Basic Books, Inc., 1993).
  * Linnainmaa [1976] S. Linnainmaa, Taylor expansion of the accumulated rounding error, BIT Numerical Mathematics 16, 146 (1976).
  * Werbos [1982] P. Werbos, Applications of advances in nonlinear sensitivity analysis, System modeling and optimization , 762–770 (1982).
  * Cerezo  _et al._ [2022] M. Cerezo, G. Verdon, H.-Y. Huang, L. Cincio, and P. J. Coles, Challenges and opportunities in quantum machine learning, Nature Computational Science 2, 567 (2022).
  * Harrow  _et al._ [2009] A. W. Harrow, A. Hassidim, and S. Lloyd, Quantum algorithm for linear systems of equations, Phys. Rev. Lett. 103, 150502 (2009).
  * Rebentrost  _et al._ [2014] P. Rebentrost, M. Mohseni, and S. Lloyd, Quantum support vector machine for big data classification, Phys. Rev. Lett. 113, 130503 (2014).
  * Zhao  _et al._ [2019] Z. Zhao, J. K. Fitzsimons, and J. F. Fitzsimons, Quantum-assisted gaussian process regression, Phys. Rev. A 99, 052331 (2019).
  * Liu  _et al._ [2023] J. Liu, M. Liu, J.-P. Liu, Z. Ye, Y. Wang, Y. Alexeev, J. Eisert, and L. Jiang, Towards provably efficient quantum algorithms for large-scale machine-learning models (2023), arXiv:2303.03428 [quant-ph] .
  * Preskill [2018] J. Preskill, Quantum computing in the NISQ era and beyond, Quantum 2, 79 (2018).
  * Liu  _et al._ [2021] Y. Liu, S. Arunachalam, and K. Temme, A rigorous and robust quantum speed-up in supervised machine learning, Nature Physics 17, 1013 (2021).
  * Huang  _et al._ [2022] H.-Y. Huang, M. Broughton, J. Cotler, S. Chen, J. Li, M. Mohseni, H. Neven, R. Babbush, R. Kueng, J. Preskill, and J. R. McClean, Quantum advantage in learning from experiments, Science (New York, N.Y.) 376, 1182 (2022).
  * Qiskit Community [2017] Qiskit Community, Qiskit: An open-source framework for quantum computing (2017).
  * Broughton  _et al._ [2021] M. Broughton, G. Verdon, T. McCourt, A. J. Martinez, J. H. Yoo, S. V. Isakov, P. Massey, R. Halavati, M. Y. Niu, A. Zlokapa, and others, Tensorflow quantum: A software framework for quantum machine learning (2021), arXiv:2003.02989 [quant-ph] .
  * Bergholm  _et al._ [2022] V. Bergholm, J. Izaac, M. Schuld, C. Gogolin, S. Ahmed, V. Ajith, M. S. Alam, G. Alonso-Linaje, B. AkashNarayanan, A. Asadi, and others, Pennylane: Automatic differentiation of hybrid quantum-classical computations (2022), arXiv:1811.04968 [quant-ph] .
  * Cirq Developers [2023] Cirq Developers, Cirq (2023).
  * Di Marcantonio  _et al._ [2023] F. Di Marcantonio, M. Incudini, D. Tezza, and M. Grossi, Quantum advantage seeker with kernels (QuASK): a software framework to speed up the research in quantum machine learning, Quantum Machine Intelligence 5, 10.1007/s42484-023-00107-2 (2023).
  * Pedregosa  _et al._ [2011] F. Pedregosa, G. Varoquaux, A. Gramfort, V. Michel, B. Thirion, O. Grisel, M. Blondel, P. Prettenhofer, R. Weiss, V. Dubourg, and others, Scikit-learn: Machine learning in Python, Journal of Machine Learning Research 12, 2825 (2011).
  * Buitinck  _et al._ [2013] L. Buitinck, G. Louppe, M. Blondel, F. Pedregosa, A. Mueller, O. Grisel, V. Niculae, P. Prettenhofer, A. Gramfort, J. Grobler, _et al._ , Api design for machine learning software: experiences from the scikit-learn project (2013), arXiv:1309.0238 [cs.LG] .
  * Kreuzberger  _et al._ [2022] D. Kreuzberger, N. Kühl, and S. Hirschl, Machine learning operations (mlops): Overview, definition, and architecture (2022), arXiv:arXiv:2205.02302 [scs.LG] .
  * Zaharia  _et al._ [2018] M. A. Zaharia, A. Chen, A. Davidson, A. Ghodsi, S. A. Hong, A. Konwinski, S. Murching, T. Nykodym, P. Ogilvie, M. Parkhe, F. Xie, and C. Zumar, Accelerating the Machine Learning Lifecycle with MLflow, IEEE Data Eng. Bull. 41, 39 (2018).
  * Zöller and Huber [2021] M. Zöller and M. F. Huber, Benchmark and survey of automated machine learning frameworks (2021), arXiv:arXiv:1904.12054v5 [cs.LG] .
  * Liaw  _et al._ [2018] R. Liaw, E. Liang, R. Nishihara, P. Moritz, J. E. Gonzalez, and I. Stoica, Tune: A research platform for distributed model selection and training, arXiv preprint arXiv:1807.05118 (2018).
  * Cerezo  _et al._ [2021] M. Cerezo, A. Arrasmith, R. Babbush, S. C. Benjamin, S. Endo, K. Fujii, J. R. McClean, K. Mitarai, X. Yuan, L. Cincio, and P. J. Coles, Variational quantum algorithms, Nature Reviews Physics 10.1038/s42254-021-00348-9 (2021).
  * Schuld and Killoran [2019] M. Schuld and N. Killoran, Quantum machine learning in feature hilbert spaces, Phys. Rev. Lett. 122, 040504 (2019).
  * Havlíček  _et al._ [2019] V. Havlíček, A. D. Córcoles, K. Temme, A. W. Harrow, A. Kandala, J. M. Chow, and J. M. Gambetta, Supervised learning with quantum-enhanced feature spaces, Nature 567, 209 (2019).
  * Rapp and Roth [2023] F. Rapp and M. Roth, Quantum gaussian process regression for bayesian optimization, arXiv preprint arXiv:2304.12923 (2023).
  * Haug  _et al._ [2021] T. Haug, K. Bharti, and M. Kim, Capacity and quantum geometry of parametrized quantum circuits, PRX Quantum 2, 040309 (2021).
  * Huang  _et al._ [2021] H.-Y. Huang, M. Broughton, M. Mohseni, R. Babbush, S. Boixo, H. Neven, and J. Mcclean, Power of data in quantum machine learning, Nature Communications 12 (2021).
  * Abadi  _et al._ [2015] M. Abadi, A. Agarwal, P. Barham, E. Brevdo, Z. Chen, C. Citro, G. S. Corrado, A. Davis, J. Dean, M. Devin, and others, TensorFlow: Large-scale machine learning on heterogeneous systems (2015), software available from tensorflow.org.
  * Chollet  _et al._ [2015] F. Chollet _et al._ , Keras, https://keras.io (2015).
  * Paszke  _et al._ [2019] A. Paszke, S. Gross, F. Massa, A. Lerer, J. Bradbury, G. Chanan, T. Killeen, Z. Lin, N. Gimelshein, L. Antiga, and others, PyTorch: An Imperative Style, High-Performance Deep Learning Library, in  _Advances in Neural Information Processing Systems 32_, edited by H. Wallach, H. Larochelle, A. Beygelzimer, F. d’Alché Buc, E. Fox, and R. Garnett (Curran Associates, Inc., 2019) pp. 8024–8035.
  * Schuld and Petruccione [2018] M. Schuld and F. Petruccione,  _Supervised learning with quantum computers_ , Vol. 17 (Springer, 2018).
  * Liu  _et al._ [2019] J. Liu, H. Yuan, X.-M. Lu, and X. Wang, Quantum fisher information matrix and multiparameter estimation, Journal of Physics A: Mathematical and Theoretical 53, 023001 (2019).
  * Meyer [2021] J. J. Meyer, Fisher Information in Noisy Intermediate-Scale Quantum Applications, Quantum 5, 539 (2021).
  * Peters  _et al._ [2021] E. Peters, J. Caldeira, A. Ho, S. Leichenauer, M. Mohseni, H. Neven, P. Spentzouris, D. Strain, and G. N. Perdue, Machine learning of high dimensional data on a noisy quantum processor, npj Quantum Information 7, 161 (2021).
  * Schuld [2021] M. Schuld, Supervised quantum machine learning models are kernel methods (2021), arXiv:2101.11020 [quant-ph] .
  * Vapnik [1999] V. Vapnik,  _The nature of statistical learning theory_ (Springer science & business media, 1999).
  * Murphy [2012] K. P. Murphy,  _Machine learning: a probabilistic perspective_ (MIT press, 2012).
  * Mangini  _et al._ [2021] S. Mangini, F. Tacchino, D. Gerace, D. Bajoni, and C. Macchiavello, Quantum computing models for artificial neural networks, Europhysics Letters 134, 10002 (2021).
  * Harrow and Napp [2021] A. W. Harrow and J. C. Napp, Low-depth gradient measurements can improve convergence in variational hybrid quantum-classical algorithms, Phys. Rev. Lett. 126, 140502 (2021).
  * Mitarai  _et al._ [2018] K. Mitarai, M. Negoro, M. Kitagawa, and K. Fujii, Quantum circuit learning, Phys. Rev. A 98, 032309 (2018).
  * Kyriienko  _et al._ [2021] O. Kyriienko, A. E. Paine, and V. E. Elfving, Solving nonlinear differential equations with differentiable quantum circuits, Phys. Rev. A 103, 052416 (2021).
  * Gujju  _et al._ [2023] Y. Gujju, A. Matsuo, and R. Raymond, Quantum machine learning on near-term quantum devices: Current state of supervised and unsupervised techniques for real-world applications (2023), arXiv:2307.00908 [quant-ph] .
  * Schuld  _et al._ [2021] M. Schuld, R. Sweke, and J. J. Meyer, The effect of data encoding on the expressive power of variational quantum-machine-learning models, Phys. Rev. A 103, 032430 (2021).
  * Caro  _et al._ [2022] M. C. Caro, H.-Y. Huang, M. Cerezo, K. Sharma, A. Sornborger, L. Cincio, and P. J. Coles, Generalization in quantum machine learning from few training data, Nature communications 13, 4919 (2022).
  * Kreplin and Roth [2023] D. A. Kreplin and M. Roth, Reduction of finite sampling noise in quantum neural networks (2023), arXiv:2306.01639 [quant-ph] .
  * squ [2023] sQUlearn Documentation – 0.5.0, https://squlearn.github.io (2023), accessed: 2023-11-14.
  * Haug  _et al._ [2023] T. Haug, C. N. Self, and M. S. Kim, Quantum machine learning of large datasets using randomized measurements, Machine Learning: Science and Technology 4, 015005 (2023).
  * McClean  _et al._ [2016] J. R. McClean, J. Romero, R. Babbush, and A. Aspuru-Guzik, The theory of variational hybrid quantum-classical algorithms, New Journal of Physics 18, 023023 (2016).
  * Schölkopf and Smola [2002] B. Schölkopf and A. Smola,  _Learning with Kernels: Support Vector Machines, Regularization, Optimization, and Beyond_, Adaptive computation and machine learning (MIT Press, 2002).
  * Jerbi  _et al._ [2023] S. Jerbi, L. J. Fiderer, H. Poulsen Nautrup, J. M. Kübler, H. J. Briegel, and V. Dunjko, Quantum machine learning beyond kernel methods, Nat. Commun. 14, 517 (2023).
  * Thanasilp  _et al._ [2022] S. Thanasilp, S. Wang, M. Cerezo, and Z. Holmes, Exponential concentration and untrainability in quantum kernel methods (2022), arXiv:2208.11060 [quant-ph] .
  * Kandola and Shawe-Taylor [2003] J. S. Kandola and J. Shawe-Taylor, Refining kernels for regression and uneven classification problems, in  _Proceedings of the Ninth International Workshop on Artificial Intelligence and Statistics_, Proceedings of Machine Learning Research, Vol. R4, edited by C. M. Bishop and B. J. Frey (PMLR, 2003) pp. 157–162.
  * Cristianini  _et al._ [2006] N. Cristianini, J. Kandola, A. Elisseeff, and J. Shawe-Taylor, On kernel target alignment, in  _Innovations in Machine Learning: Theory and Applications_, edited by D. E. Holmes and L. C. Jain (Springer Berlin Heidelberg, Berlin, Heidelberg, 2006) pp. 205–256.
  * Hubregtsen  _et al._ [2022] T. Hubregtsen, D. Wierichs, E. Gil-Fuster, P.-J. H. S. Derks, P. K. Faehrmann, and J. J. Meyer, Training quantum embedding kernels on near-term quantum computers, Phys. Rev. A 106, 042431 (2022).
  * Wang  _et al._ [2021] X. Wang, Y. Du, Y. Luo, and D. Tao, Towards understanding the power of quantum kernels in the NISQ era, Quantum 5, 531 (2021).
  * IBM Quantum [2023] IBM Quantum, https://quantum-computing.ibm.com (2023).
  * Peruzzo  _et al._ [2014] A. Peruzzo, J. McClean, P. Shadbolt, M.-H. Yung, X.-Q. Zhou, P. J. Love, A. Aspuru-Guzik, and J. L. O’Brien, A variational eigenvalue solver on a photonic quantum processor, Nature Communications 5, 4213 (2014).
  * qis [2023] Getting started – Qiskit Runtime IBM Client 0.14.0, https://qiskit.org/ecosystem/ibm-runtime/getting_started.html (2023), accessed: 2023-11-14.