Scalable quantum neural networks by few quantum resources
This paper focuses on the construction of a general parametric model that can
be implemented executing multiple swap tests over few qubits and applying a
suitable measurement protocol. The model turns out to be equivalent to a
two-layer feedforward neural network which can be realized combining small
quantum modules. The advantages and the perspectives of the proposed quantum
method are discussed.
Davide Pastorello, Enrico Blanzieri
# Scalable quantum neural networks by few quantum resources

###### Abstract

This paper focuses on the construction of a general parametric model that can be implemented executing multiple swap tests over few qubits and applying a suitable measurement protocol. The model turns out to be equivalent to a two-layer feedforward neural network which can be realized combining small quantum modules. The advantages and the perspectives of the proposed quantum method are discussed.

**Keywords**: Neural networks; quantum machine learning; scalable quantum computing.

## 1 Introduction

The application of quantum computation to machine learning tasks offers some interesting solutions characterized by a quantum advantage with respect to the classical counterparts in terms of time and space complexity, expressive power, generalization capability, at least on a theoretical level [1, 2, 3]. Moreover, quantum machine learning (QML) seems to be a promising path to explore in deep the possibilities offered by quantum machines and to exploit existing and near-term quantum computers for tackling real-world problems. In this respect, the research activity aimed to the development of novel QML algorithms must face the issue of the lack of large-scaled, fault-tolerant, universal quantum computers. In order to promote QML as an operative technology we should provide solutions that are not too demanding in terms of quantum resources and, in the meanwhile, they supply quantum advantages w.r.t. classical information processing. The present paper focuses on a solution which represents a trade-off between the definition of a quantum model, specifically a quantum neural network, and the availability of few quantum resources as a realistic assumption.
In this work, we assume the availability of a very specific quantum hardware that is able to perform the swap test1 over two \(k\)-qubit registers (\(k\geq 1\)). This assumption is motivated by several proposals of a physical implementation of the swap test then it turns out to be rather realistic for few qubits [5, 6]. Then, we assume to combine many copies of the quantum hardware, as modules, in order to obtain a larger structure for data processing. In particular, we argue that a combined execution of many independent swap tests, performing only local measurements, allows to create a two-layer feedforward neural network presenting some quantum advantages in terms of space complexity with the possibility to input quantum data. The main result is a general construction of a meaningful quantum model based on very few quantum resources whose scalability is allowed by the requirement of quantum coherence over a small number of qubits because a major role is played by measurement processes.

Footnote 1: The _swap test_ is a simple but useful technique to evaluate the similarity between two unknown pure states [4]. It is briefly illustrated in the next section.

The swap test is a well-known tool for developing QML techniques, so there are some proposals of quantum neural networks based on it. For instance, there are quantum neurons where the swap test is coupled to the quantum phase estimation algorithm for realizing quantum neural networks with quantum input and quantum output [7, 8]. Our approach is different, we assume only the capability of performing the swap test over few qubits for constructing neural networks where the input is a quantum state and the output is always classical. Our goal is the definition of a quantum model which can be realized assuming the availability of few quantum resources.

The paper is structured as follows: in section 2 we recall some basic notions about two-layer feedforward neural networks and the definition of swap test as the building block for the proposed modular architecture. In section 3, we define the parametric model obtained combining small modules acting on few qubits, showing its equivalence to a neural network after a suitable measurement protocol is performed. In section 4, we discuss the impact of the obtained results. In the final section, we draw some concluding remarks highlighting the perspectives of the proposed modular approach.

## 2 Background

In this section we introduce some basic notions that are crucial for the definition of the model proposed and discussed in the paper. In particular, we define the structure of the neural network we consider in the main part of the work and the necessary quantum computing fundamentals.

_Definition 1_: _Let \(X\) and \(Y\) be non-empty sets respectively called **input domain** and **output domain**. A (deterministic) **predictive model** is a function_

\[y=f(x;\theta)\qquad x\in X,\ y\in Y,\]

_where \(\theta=(\theta_{1},...,\theta_{d})\) is a set of real parameters._

In supervised learning, given a training set \(\{(x_{1},y_{1}),\cdots,(x_{m},y_{m})\}\) of pairs input-output the general task is finding the parameters \(\theta\) such that the model assigns the right output to any unlabelled input. The parameters are typically determined optimizing a _loss function_ like \(\mathcal{L}(\theta)=\sum_{i=1}^{m}d(y_{i},f(x_{i},\theta))\) where \(d\) is a metric defined over \(Y\). A remarkable example of predictive model,
where \(X=\mathbb{R}^{n}\) and \(Y=\mathbb{R}\), is the following _two-layer neural network_ with \(h\) hidden nodes:

\[f(\mathbf{x};\mathbf{p},W)=\sum_{i=1}^{h}p_{i}\varphi(\mathbf{x}\cdot\mathbf{w}_ {i}), \tag{1}\]

where \(\mathbf{w}_{i}\in\mathbb{R}^{n}\) is the \(i\)th row of the \(n\times h\) weight matrix \(W\) of the first layer, \(p_{i}\) is the \(i\)th coefficient of the second layer, and \(\varphi:\mathbb{R}\rightarrow\mathbb{R}\) is a continuous non-linear function. Despite its simplicity, this model is relevant because it is a _universal approximator_ in the sense that it can approximate, up to finite precision, any Borel function \(\mathbb{R}^{n}\rightarrow\mathbb{R}\), provided a sufficient number of hidden nodes is available [9].

Let us focus on a quadratic activation function for a two-layer network that is not very used in practice however it has been widely studied [10, 11, 12, 13] proving, in particular, that networks with quadratic activation are as expressive as networks with threshold activation and can be learned in polynomial time [10]. Moreover, the quadratic activation presents the same convexity of the rectified linear unit (ReLU) but with a wider activating zone, so the loss curves of networks using these two activations converge very similarly [13]. In addition to the choice of a quadratic activation, let us consider the _cosine similarity_ instead of the dot product as pre-activation:

\[\cos(\mathbf{x},\mathbf{w}):=\frac{\mathbf{x}\cdot\mathbf{w}}{\parallel \mathbf{x}\parallel\parallel\mathbf{w}\parallel}. \tag{2}\]

The _cosine pre-activation_ is a normalization procedure adopted to decrease the variance of the neuron that can be large as the dot product is unbounded. A large variance can make the model sensitive to the change of input distribution, thus resulting in poor generalization. Experiments show that cosine pre-activation achieves better performances, in terms of test error in classification, compared to batch, weight, and layer normalization [14]. The aim of the paper is showing that a simple quantum processing, combined in multiple copies, can be used to implement a varying-size two-layer network with cosine pre-activation and quadratic activation.

The main assumption of the work is that few quantum resources are available in the sense that we consider a quantum hardware characterized by a small number of qubits on which we can act with few quantum gates being far from the availability of a large-scaled, fault-tolerant, universal quantum computer. The central object is a well-known tool of QML, the _swap test_, that we assume to have implemented into a specific-purpose quantum hardware, characterized by an arbitrarily small number of qubits, that we call _module_. The questions we address are: _What useful can we do if we are able to build such a module with a small number of qubits? Can we construct a neural network stacking such modules with some interesting properties?_

As a postulate of quantum mechanics, any quantum system is described in a Hilbert space. We consider only the finite-dimensional case where a Hilbert space \(\mathsf{H}\) is nothing but a complex vector space equipped with an _inner product_\(\langle\ \mid\ \rangle:\mathsf{H}\times\mathsf{H}\rightarrow\mathbb{C}\). A quantum system is described in the Hilbert space \(\mathsf{H}\) in the sense that its physical states are in bijective correspondence with the projective rays in \(\mathsf{H}\). Then a (pure) quantum state can be identified to a normalized vector \(|\psi\rangle\in\mathsf{H}\) up to a multiplicative phase factor. In other words, quantum states are described by one-dimensional orthogonal projectors of the form \(\rho_{\psi}=|\psi\rangle\langle\psi|\) where \(\langle\psi|\) is an element of the dual of \(\mathsf{H}\).
Definition 2Let \(\mathsf{H}\) be a (finite-dimensional) Hilbert space and \(I\) a finite set. A **measurement process** is a collection of linear operators \(\mathcal{M}=\{E_{\alpha}\}_{\alpha\in I}\) such that \(E_{\alpha}\geq 0\) for each \(\alpha\in I\) and \(\sum_{\alpha}E_{\alpha}=\mathbb{I}\), where \(\mathbb{I}\) is the identity operator on \(\mathsf{H}\).

Given an orthonormal basis \(\{|i\rangle\}_{i=1,\ldots,n}\) of \(\mathsf{H}\), the **measurement in the basis \(\{|i\rangle\}_{i=1,\ldots,n}\)** is the measurement process defined by \(\mathcal{M}=\{|i\rangle\langle i|\}_{i=1,\ldots,n}\).

The probability of obtaining the outcome \(\alpha\in I\) by the measurement \(\mathcal{M}=\{E_{\alpha}\}_{\alpha\in I}\) on the system in the state \(|\psi\rangle\) is \(\mathbb{P}_{\psi}(\alpha)=\langle\psi|E_{\alpha}\psi\rangle\). If \(\mathcal{M}\) is a measurement in the basis \(\{|i\rangle\}_{i=1,\ldots,n}\) then the probability reduces to \(\mathbb{P}_{\psi}(i)=|\langle\psi|i\rangle|^{2}\). A _qubit_ is any quantum system described in the Hilbert space \(\mathbb{C}^{2}\), denoting the standard basis of \(\mathbb{C}^{2}\) by \(\{|0\rangle,|1\rangle\}\) we have the _computational measurement_ on a single qubit \(\mathcal{M}=\{|0\rangle\langle 0|,|1\rangle\langle 1|\}\).

Another postulate of quantum mechanics reads that, given two quantum systems \(S_{1}\) and \(S_{2}\) described in the Hilbert spaces \(\mathsf{H}_{1}\) and \(\mathsf{H}_{2}\) respectively, the Hilbert space of the composite system \(S_{1}+S_{2}\) is given by the tensor product Hilbert space \(\mathsf{H}_{1}\otimes\mathsf{H}_{2}\). Therefore there are states of the product form \(|\psi_{1}\rangle\otimes|\psi_{2}\rangle\in\mathsf{H}_{1}\otimes\mathsf{H}_{2}\) (in contract notation: \(|\psi_{1}\psi_{2}\rangle\)) called _separable states_ and states that cannot be factorized called _entangled states_. The latter describe a kind of non-local correlations without a counterpart in classical physics which are the key point of the most relevant advantages in quantum information processing.

Encoding classical data into quantum states is a crucial issue in quantum computing and specifically quantum machine learning. One of the most popular quantum encoding is the so-called _amplitude encoding_. Assume we have a data instance represented by a complex vector \(\mathbf{x}\in\mathbb{C}^{n}\) then it can be encoded into the amplitudes of a quantum state:

\[|\mathbf{x}\rangle=\frac{1}{\parallel\mathbf{x}\parallel}\sum_{i=1}^{n}x_{i}| i\rangle, \tag{3}\]

where \(\{|i\rangle\}_{i=1,\ldots,n}\) is a fixed orthonormal basis, called _computational basis_, of the Hilbert space of the considered quantum physical system. If the quantum system is made up by \(k\) qubits, say a \(k\)_-qubit register_, with Hilbert space \(\mathsf{H}=(\mathbb{C}^{2})^{\otimes k}\) then for storing \(\mathbf{x}\in\mathbb{C}^{n}\) we need \(k=\lceil\log n\rceil\) qubits and the commonly used computational basis is \(\{|d\rangle:d\in\{0,1\}^{k}\}\). Therefore, amplitude encoding exploits the exponential storing capacity of a quantum memory, but it does not allow the direct retrieval of the stored data. Indeed, the amplitudes cannot be observed, and only the probabilities \(|x_{i}|^{2}/\parallel\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\! \!\!
where \(\oplus\) is the sum modulo 2. Its circuital symbol is:

The _swap gate_ is a 2-qubit gate which exchanges the input qubits, its circuital definition in terms of CNOT gates is:

The controlled swap gate is called _Fredkin gate_, its action can be expressed in the following form in the computational basis of three qubits, where the first qubit is the control:

\[\mathsf{F}|abc\rangle=(1-a)|abc\rangle+a|acb\rangle\qquad a,b,c\in\{0,1\}. \tag{7}\]

The swap test is a very simple procedure to estimate the probability \(|\langle\psi|\varphi\rangle|^{2}\), given two unknown pure states \(|\psi\rangle\) and \(|\varphi\rangle\)[4]. The procedure requires two applications of the Hadamard gate and a controlled swap operation between two multiple qubit registers generalizing the Fredkin gate. It is straightforward to show that the controlled swap of two \(k\)-qubit registers can be implemented with \(k\) Fredkin gates controlled by the same qubit. Consider two \(k\)-qubit registers initialized in \(|\psi\rangle\) and \(|\varphi\rangle\) and a control qubit prepared in \(|0\rangle\). Assume to act with the following circuit on the \(2k+1\) qubits:

\[\begin{array}{c}\includegraphics[width=142.26378pt]{figs/control_swap_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubitqubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubitqubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubitqubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubitqubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubitqubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubitqubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubitqubit_qubit_qubitqubit_qubit_qubit_qubitqubit_qubit_qubit_qubit_qubitqubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubitqubit_qubit_qubitqubit_qubit_qubitqubit_qubit_qubitqubit_qubit_qubitqubit_qubit_qubitqubit_qubit_qubitqubit_qubitqubit_qubitqubit_qubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubit_qubitqubit_qubitqubit_qubitqubit_qubit_qubitqubit_qubitqubit_qubitqubit_qubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubit_qubitqubit_qubit_qubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubit_qubitqubit_qubitqubit_qubitqubit_qubitqubit_qubit_qubitqubit_qubit_qubitqubit_qubitqubit_qubit_qubitqubit_qubit_qubitqubit_qubitqubit_qubit_qubitqubit_qubit_qubitqubit_qubit_qubit_qubitqubit_qubitqubit_qubitqubit_qubit_qubitqubit_qubit_qubitqubit_qubit_qubitqubit_qubitqubit_qubit_qubitqubit_qubit_qubit_qubitqubit_qubit_qubit_qubit_qubit_qubitqubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit__qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_qubit_\(\)_qubit_qubit_qubit_qubit_\(\)_qubit_qubit_\(\)_qubit_\(\
where \(|\psi\rangle\) and \(|\varphi\rangle\) are \(k\)-qubit states and \(\mathbb{P}(0)\) is given by (9). In the following, we argue that independent swap tests between \(k\)-qubit registers and a suitable measurement protocol over the control qubits can be used for implementing a two-layer feedforward neural network.

## 3 The proposed model

Let us consider \(m\) copies of the process diagram (10), that we call _modules_, each module presents two \(k\)-qubit registers and one auxiliary qubit to perform the swap test. Then the total number of qubits, including the controls, is \(m(2k+1)\). Let \(\mathbf{x}\in\mathbb{R}^{N}\) be an input vector and \(\mathbf{w}\in\mathbb{R}^{N}\) be a vector of parameters, if \(k\geq\log N\) then a single module is sufficient to store \(\mathbf{x}\) and \(\mathbf{w}\) in its registers and the action of the swap test, which returns the probability \(\frac{1}{2}(1-\cos^{2}(\mathbf{x},\mathbf{w}))\) as the value of a non-linear function over \(\cos(\mathbf{x},\mathbf{w})\), realizes an elementary perceptron with quadratic activation and cosine pre-activation. However, we are not interested in implementing a single perceptron and according to the general assumption of few quantum resources available, we consider the case \(k<\log N\), so in each module we can encode the input and weight vectors only partially.

**Proposition 3**: _Let \(\mathcal{R}\) be a \(k\)-qubit register. Let \(N>2^{k}\), the number \(m\) of non-interacting2 copies of \(\mathcal{R}\) needed for storing \(\textbf{x}\in\mathbb{C}^{N}\) into a state \(\bigotimes_{i=1}^{m}|\psi_{i}\rangle\) by amplitude encoding is:_

Footnote 2: In this context, _non-interacting registers_ means that any operation on the registers, including those required for state preparation, must be performed locally on each register. Therefore, no entanglement can be produced among different \(k\)-qubit registers.

\[m=\mathcal{O}(N2^{-k}). \tag{11}\]

Proof.: In each register, we can encode at most \(2^{k}\) entries of \(\mathbf{x}\) into the amplitudes of a \(k\)-qubit state. Therefore the minimum number \(m\) of registers for storing \(\mathbf{x}\) into a pure state, where the registers are non-entangled with each other, satisfies \((m-1)2^{k}<N<m2^{k}\).

Considering \(m\) independent modules, we can encode at most \(m2^{k}\) input entries and \(m2^{k}\) weights into the available qubits (divided into the _input register_ and the _weight register_ of each module). Therefore, by proposition 3, the total number of qubits required to encode and process \(\mathbf{x}\in\mathbb{R}^{N}\) is:

\[n=\mathcal{O}\left(N(2k+1)2^{-k}\right). \tag{12}\]

Since a single module does not have an input register large enough to encode all the entries of the input \(\mathbf{x}\) (i.e. \(k<\log N\)), we have to settle for a piecewise amplitude encoding parallelizing
the modules, as a consequence the total number of qubits scales linearly in \(N\), however there is an exponential decay in the size of a single module which can be relevant in specific cases. For instance, if \(N=128\) and we have a single module with a 5-qubit input register and a 5-qubit weight register then 4 modules and 44 qubits are required to store an input vector and a weight vector. Note that in this context 44 qubits is not considered a _large number_ because the coherence must be maintained only inside a single module (that is 11 qubits, two 5-qubit registers and 1 control qubit). Thus, the parallelization of modules with few qubits cannot reach of course an exponential storing capacity but there are cases with realistic values of \(k\) and \(N\) that can be addressed efficiently by modularity.

In order to introduce the parametric model that we can implement executing swap tests in parallel, let us partition the input vector \(\mathbf{x}\in\mathbb{R}^{N}\) and the weight vector \(\mathbf{w}\in\mathbb{R}^{N}\) into pieces to feed the \(m\) modules (each one with \(2k+1\) qubits) assuming that \(N=m2^{k}\) without loss of generality:

\[\mathbf{x}=(\mathbf{x}^{(1)},...,\mathbf{x}^{(m)})\quad\text{where}\quad \mathbf{x}^{(l)}=(x_{(l-1)2^{k}+1},...,x_{l2^{k}}),\qquad l=1,...,m \tag{13}\]

\[\mathbf{w}=(\mathbf{w}^{(1)},...,\mathbf{w}^{(m)})\quad\text{where}\quad \mathbf{w}^{(l)}=(w_{(l-1)2^{k}+1},...,w_{l2^{k}}),\qquad l=1,...,m\]

\(\mathbf{x}^{(l)}\) can be stored into the \(k\)-qubit input register of the \(l\)th module and \(\mathbf{w}^{(l)}\) can be stored into the \(k\)-qubit weight register of the \(l\)th module, then the computation of the norms \(\|\,\mathbf{x}^{(l)}\,\|\) and \(\|\,\mathbf{w}^{(l)}\,\|\), for any \(l=1,...,m\), are required for the encoding. The parallelization of the modules to perform \(m\) swap tests can be easily visualized as follows:

(14)
where \(\mathbb{P}_{l}(0)\) is the probability of measuring \(0\) over the \(l\)th control qubit which can be estimated within an error \(\epsilon\) with \(\mathcal{O}(\epsilon^{-2})\) runs. The multiple swap tests depicted in (14) can be seen as a layer of perceptrons with connectivity limited by the mutual independence of the modules. On this first elementary structure, we construct the second layer applying a specific _local measurement protocol_ to the \(m\) control qubits.

By definition 2, a measurement process provides a classical probability distribution over the set of possible outcomes. In operational terms, a measurement process is repeated \(\mathcal{N}\) times and a string of outcomes \(x\in\mathbb{R}^{\mathcal{N}}\) is stored, where the probability distribution is estimated in terms of relative frequencies. In the following definition, let us assume that only \(\widetilde{\mathcal{N}}<\mathcal{N}\) outcomes are stored after \(\mathcal{N}\) repetitions of a considered measurement process \(\mathcal{M}\).

_Definition 4_: _Let \(\mathcal{M}\) be a measurement process. Assume to repeat the measurement \(\mathcal{N}\) times storing \(\widetilde{\mathcal{N}}<\mathcal{N}\) outcomes, then \(\mathcal{M}\) is called_ **lossy measurement** _with_ **efficiency**_\(p=\widetilde{\mathcal{N}}/\mathcal{N}\)._

The measurement protocol that we need for constructing the proposed model is characterized by local measurements over single qubits characterized by given efficiencies.

_Definition 5_: _Given a system composed by \(m\) qubits, a_ **local measurement protocol** _is a collection \(\{(\mathcal{M}_{l},p_{l})\}_{l=1,...,m}\) where \(\mathcal{M}_{l}\) is a lossy measurement process on the \(l\)th qubit and \(p_{l}\) its efficiency. If \(\mathcal{M}_{l}=\{|0\rangle\langle 0|,|1\rangle\langle 1|\}\) for any \(l=1,...,m\) then \(\{(\mathcal{M}_{l},p_{l})\}_{l=1,...,m}\) is called_ **computational measurement protocol**_._

In the case of the computational measurement protocol over \(m\) qubits, the outcomes provided by a single shot of the protocol form a string \(b=\{\emptyset,0,1\}^{m}\), where the symbol \(\emptyset\) denotes a missing outcome. In the case the measurement processes \(\mathcal{M}_{l}\) are lossless, the efficiency \(p_{l}\) can be controlled selecting the number \(\widetilde{\mathcal{N}}\) of times a measurement \(\mathcal{M}_{l}\) is actually performed when the overall protocol is repeated \(\mathcal{N}\) times. From the physical viewpoint, one can also select the efficiency of the measurements controlling the losses of employed channels.

Given the \(m\) modules executing the multiple (independent) swap tests depicted in diagram (14), let us define a model with parameters given by the weights \(\mathbf{w}\in\mathbb{R}^{N}\) and the efficiencies \(\mathbf{p}=(p_{1},...,p_{m})\) such that the prediction associated to the input \(\mathbf{x}\in\mathbb{R}^{N}\) is given by:

\[f(\mathbf{x};\mathbf{w},\mathbf{p})=\frac{\mathcal{N}_{0}}{\mathcal{N}}, \tag{15}\]

where \(\mathcal{N}\) is the number of repetitions of the computational measurement protocol, with efficiencies \(\mathbf{p}=(p_{1},...,p_{m})\), on the control qubits and \(\mathcal{N}_{0}\) is the total number of obtained zeros.

_Proposition 6_: _The model (15) is equivalent, up to an error \(\epsilon=\mathcal{O}(1/\sqrt{\mathcal{N}})\), to the following two-layer feedforward neural network with cosine pre-activation and activation \(\varphi(z)=\frac{1}{2}(1-z^{2})\) in the hidden neurons:_
Proof.: By the application of the computational measurement protocol with efficiencies \(\{p_{l}\}_{l=1,\ldots,m}\) to the scheme (14), the overall probability of obtaining the outcome \(0\) on the \(l\)th control qubit is:

\[\widehat{\mathbb{P}}_{l}(0)=p_{l}\,\mathbb{P}_{l}(0)=\frac{p_{l}}{2}\left(1- \cos^{2}(\mathbf{x}^{(l)},\mathbf{w}^{(l)})\right). \tag{17}\]

The probability can be estimated, up to an error \(\epsilon\), by the relative frequency \(\mathcal{N}_{0}^{(l)}/\mathcal{N}\) where \(\mathcal{N}_{0}^{(l)}\) is the number of zeros obtained on the \(l\)th qubit by \(\mathcal{N}=\mathcal{O}(\epsilon^{-2})\) repetitions of the computational measurement protocol. By construction, the output of the neural network (16) corresponds to the value \(\sum_{l}\widehat{\mathbb{P}}_{l}(0)\) that can be estimated by:

\[\sum_{l=1}^{m}\frac{\mathcal{N}_{0}^{(l)}}{\mathcal{N}}=\frac{\mathcal{N}_{0} }{\mathcal{N}}.\]

Therefore, for any input \(\mathbf{x}\) and any choice of the parameters \(\mathbf{w}\) and \(\mathbf{p}\) the output \(\mathcal{N}_{0}/\mathcal{N}\) of the model (15) corresponds to the output of the neural network (16) up to an error \(\epsilon=\mathcal{O}(1/\sqrt{\mathcal{N}})\). 

Proposition 6 states that we can construct feedforward neural networks by combination of swap tests as modules with fixed size, i.e. defined over qubit registers with given number of qubits. The neural network (16) fails the universality because of the sparse connections in the first layer, however if the length of the input is smaller than \(2^{k}\) we can recover a universal approximator since we can construct a network with an arbitrary number of hidden nodes and complete topology. More precisely, let us note that the number of hidden and output neurons can be arbitrarily increased. In fact, with additional runs of the swap tests, by permutation of the input entries and the re-initialization of the weights \(\mathbf{w}\) one can realize the connections between the input neurons and an arbitrary number of hidden neurons under the topological constraint given by the size of the modules. Collecting the outcomes obtained by the computational measurement protocol with varying efficiencies, we can define the connections from the hidden neurons and an arbitrary number of output nodes. In this respect, the following proposition enables the implementation of a complete feedforward neural network by a suitable measurement protocol.
Proposition 7: _Given a single module of size \(k\) and the positive integers \(R\) and \(Q\), let be \(\mathbf{w}_{1},...,\mathbf{w}_{R}\in\mathbb{R}^{2^{k}}\), and \(p_{rq}\in[0,1]\) for any \(r=1,...,R\) and \(q=1,...,Q\). Let us consider the following algorithm:_

```
Input:Input vector \(\mathbf{x}\in\mathbb{R}^{2^{k}}\) Result:Output vector \(\mathbf{y}\in\mathbb{R}^{Q}\)
1foreach\(r\gets 1,...,R\)do
2foreach\(q\gets 1,...,Q\)do
3run the swap test \(\mathcal{N}\) times on \(|\mathbf{x}\rangle\) and \(|\mathbf{w}_{r}\rangle\) and measure the control qubit with efficiency \(p_{rq}\);
4 collect the outcomes \(b_{rq}\in\{\emptyset,0,1\}^{\mathcal{N}}\);
5
6 end foreach\(q\gets 1,...,Q\)do
7 construct the concatenation \(b_{q}=b_{1q}b_{2q}\cdots b_{Rq}\in\{\emptyset,0,1\}^{R\times\mathcal{N}}\);
8\(y_{q}\leftarrow\frac{\mathcal{N}_{0,q}}{R\mathcal{N}}\) [\(\mathcal{N}_{0,q}\) is the number of zeros in the string \(b_{q}\)]
9
10 end foreach return\(\mathbf{y}=(y_{1},...,y_{Q})\)
```

_The output \(\mathbf{y}\in\mathbb{R}^{Q}\) corresponds, up to an error \(\mathcal{O}(1/\sqrt{R\mathcal{N}})\), to the output of a fully connected feedforward neural network, with cosine pre-activation and activation \(\varphi(z)=\frac{1}{2}(1-z^{2})\), with \(2^{k}\) input nodes, \(R\) hidden nodes, and \(Q\) output nodes._

Proof.: Let us prove the claim simply for \(k=R=Q=2\), since the generalization is straightforward. Consider a module with \(k=2\) and the execution of the following steps (\(Q=2\)):

1. run the swap test \(\mathcal{N}\) times with weights \(\mathbf{w}_{1}\in\mathbb{R}^{4}\) and measure the control qubit with efficiency \(p_{11}\). Collect the outcomes \(b_{11}\in\{\emptyset,0,1\}^{\mathcal{N}}\);
2. run the swap test \(\mathcal{N}\) times, with the same weights and measure the control qubit with efficiency \(p_{12}\). Collect the outcomes \(b_{12}\in\{\emptyset,0,1\}^{\mathcal{N}}\) ;

By proposition 6, computing the relative frequency of \(0\) over \(b_{11}\) and \(b_{12}\) respectively we obtain the estimations of the probabilities \(\frac{p_{11}}{2}(1-\cos^{2}(\mathbf{w}_{1},\mathbf{x}))\) and \(\frac{p_{12}}{2}(1-\cos^{2}(\mathbf{w}_{2},\mathbf{x}))\) implementing the following network up to an error \(\mathcal{O}(1/\sqrt{\mathcal{N}})\):

Repeating the steps 1 and 2 with weights \(\mathbf{w}_{2}\in\mathbb{R}^{4}\) and efficiencies \(p_{21}\) and \(p_{22}\), we can collect also the outcomes \(b_{21},b_{22}\in\{\emptyset,0,1\}^{\mathcal{N}}\). Computing the relative frequency of \(0\) in the strings \(b_{21}\) and \(b_{22}\), we can implement another network as well:
In order to obtain a complete network, we can combine the two networks above (\(R=2\)), providing the outputs:

\[y_{1} =\frac{p_{11}}{2}(1-\cos^{2}(\mathbf{w}_{1},\mathbf{x}))+\frac{p_{21 }}{2}(1-\cos^{2}(\mathbf{w}_{2},\mathbf{x})),\] \[y_{2} =\frac{p_{12}}{2}(1-\cos^{2}(\mathbf{w}_{1},\mathbf{x}))+\frac{p_{ 22}}{2}(1-\cos^{2}(\mathbf{w}_{2},\mathbf{x})).\]

For estimating the sum \(y_{1}\) of the two probabilities we take the sum of the relative frequencies of the outcome \(0\) in the string \(b_{11}\) and in the string \(b_{21}\) which obviously corresponds to the relative frequency of the outcome zero in the concatenation \(b_{11}b_{21}\). In the same way, we estimate \(y_{2}\) as the relative frequency of \(0\) in the string \(b_{12}b_{22}\). In other words, given an input \(\mathbf{x}\in\mathbb{R}^{4}\) and the parameters \(\mathbf{w}_{1},\mathbf{w}_{2},p_{11},p_{12},p_{21},p_{22}\), we have a model that outputs:

\[y_{1}=\frac{\mathcal{N}_{0,1}}{2\mathcal{N}}\qquad\text{and}\qquad y_{2}= \frac{\mathcal{N}_{0,2}}{2\mathcal{N}},\]

where \(\mathcal{N}_{0,i}\) is the number of zeros in the concatenation \(b_{i}=b_{1i}b_{2i}\). Therefore, \(y_{1}\) and \(y_{2}\) correspond, up to an error \(\mathcal{O}(1/\sqrt{2\mathcal{N}})\), to the outputs of the fully connected network:

where the \(8\) connections in the first layer are weighted by the components of \(\mathbf{w}_{1}\) and \(\mathbf{w}_{2}\) and the \(4\) connections in the second layer are weighted by \(p_{11},p_{12},p_{21},p_{22}\).

By direct generalization of the construction above we have that the number of steps \(Q\) establishes the number of output neurons and the number of repetitions \(R\) corresponds to the number of hidden neurons.

Let us remark that the complete topology of the network, provided by proposition 7, in the first layer is implied by the fact that we consider a single module for the calculation of the swap test. Otherwise, the connectivity in the first layer depends by the number of the considered modules and their size \(k\). On the contrary, the local measurement protocol described in the algorithm of proposition 7 always enables all-to-all connections in the second layer.
Discussion

The statements of propositions 6 and 7 provide a general procedure of constructing measurement protocols for the implementation of feedforward neural networks using the outcomes obtained running multiple swap tests between quantum states encoding the input vectors and the weights vector. In particular, proposition 6 implies that if we are able to perform independent swap test over \(k\)-qubit registers then a suitable sample is sufficient for obtaining a neural structure for processing data encoded into the amplitudes of input states. Moreover, proposition 7 establishes that, by suitable repetitions of the swap test, one can decide the number of hidden and output neurons. In particular, if the number of features in the considered dataset is sufficiently low, complete neural networks can be implemented as a direct consequence of propositions 6 and 7.

The modular structure of the model naturally enables the possibility of ensemble learning. In fact, an input vector can be trivially partitioned as described in section 3 but also random partitions can be actuated, in this case a random sampling is performed over the training set as done in bootstrap aggregating for instance. Moreover, different \(k\)-sized modules initialized with the same weights \(\mathbf{w}\in\mathbb{R}^{2^{k}}\) can be interpreted as the application of a convolutional layer followed by a local pooling as done in convolutional neural networks.

From the viewpoint of time complexity, the performances of the proposed model are bounded by the state preparation for encoding the input \(\mathbf{x}\in\mathbb{R}^{N}\) and the weights \(\mathbf{w}\in\mathbb{R}\) into quantum states since the time taken by the measurement protocols is independent from the input size and only related to accuracy and hyperparameters. The general issue of efficient state preparation is a common bottleneck of the current quantum machine learning algorithms, in particular in quantum neural networks. Indeed, the encoding of a data vector \(\mathbf{x}\in\mathbb{R}^{N}\) into the amplitudes of a \(\log N\)-qubit state \(|\mathbf{x}\rangle\) can be performed in time \(\mathcal{O}(\log N)\) only if a quantum RAM (QRAM) is available otherwise the retrieval of \(|\mathbf{x}\rangle\) and the initialization of \(|\mathbf{w}\rangle\) take time \(\mathcal{O}(N)\). From the viewpoint of the space complexity, there is not the expected exponential storage capacity because we assume the availability of a collection of small quantum register where the input can be encoded piecewise. However, the number of required qubits is tamed by an exponential decay in the size \(k\) of a single module then, for suitable values of \(N\) and \(k\), data can be represented efficiently. Moreover, even if the number of qubits is high, we do not require the coherence over all the qubits but only in any module where the swap test is performed. In addition to the advantage in the storing capacity (bounded by \(k\)) the quantum implementation of the proposed scheme offers the possibility of submitting superposition of data to the network. Moreover, even if the modules are _a priori_ uncorrelated, an input can be represented by quantum data with entanglement distributed on a high number of qubits, in this case the quantum processing of a widely entangled input creates quantum correlations among the modules. In general, the input of the model can be a quantum state encoding a feature vector as discussed but also non-artificial quantum data entangling the modules giving rise to a non-trivial execution of the neural network.

Let us remark that the global structure of neural network is also preserved when modules are of minimum size (\(k=1\)), in this case we have the lowest storing capacity and the first layer turns out to be sparse. Nevertheless, the model is still quantum then capable to process quantum data as a two-layer feedforward network.
Conclusions

In this paper we have presented a scheme for implementing a two-layer feedforward neural network using repeated swap tests over quantum registers and suitable measurement protocols. The main goal of the work is showing how to create a non-trivial quantum machine learning model when few quantum resources are available. In particular, we assume the availability of a specific-purpose quantum hardware, that we call _module_, capable of implementing the swap test (this assumption is motivated by several recent proposals of photonic platforms for this task). By combination of an arbitrary amount of these modules, a scalable neural network can be constructed where the first layer is implemented by the quantum processing itself and the second one is obtained in terms of a measurement protocol characterized by varying efficiencies in registering the outcomes. Even if the composition of independent modules does not create entanglement, an entangled state in input introduces quantum correlations in the network.

The empirical evaluation of the proposed model is not addressed in the present paper where only a theoretical construction is provided. From the experimental viewpoint, in the case of amplitude encoding of classical data a simulation of the multiple swap tests is not so useful because it mathematically corresponds to the execution of a two-layer network with quadratic activation that is a well-known object already investigated in deep. Then, experiments on real quantum hardware would be definitely more significant for revealing actual quantum advantages of the discussed proposal. On the contrary, in the case of quantum data where the input can be represented by highly entangled quantum states, a simulation could be interesting but restricted to a small total number of qubits for computational reasons. Indeed, in our opinion, the crucial experimental point will be the combination of real photonic hardware, as small modules implementing the swap test over few qubits, in order to quantify the performances of the scalable quantum neural networks constructed by modularity. In this perspective, a realization with photonic platforms can lead to an on-chip embedding of the modules increasing the technological impact of the proposed QML architecture.

## Acknowledgements

This work was partially supported by project SERICS (PE00000014) under the MUR National Recovery and Resilience Plan funded by the European Union - NextGenerationEU.
