QDNN: deep neural networks with quantum layers

In this paper, a quantum extension of classical deep neural network (DNN) is introduced, which is called QDNN and consists of quantum structured layers. It is proved that the QDNN can uniformly approximate any continuous function and has more representation power than the classical DNN. Moreover, the QDNN still keeps the advantages of the classical DNN such as the non-linear activation, the multi-layer structure, and the efficient backpropagation training algorithm. Furthermore, the QDNN uses parameterized quantum circuits (PQCs) as the basic building blocks and hence can be used on near-term noisy intermediate-scale quantum (NISQ) processors. A numerical experiment for an image classification task based on QDNN is given, where a high accuracy rate is achieved.


Introduction
Quantum computers use the principles of quantum mechanics for computing, which are more powerful than classical computers in many computing problems (Shor 1994;Grover 1996). Many quantum machine learning algorithms, such as quantum support vector machine, quantum principal component analysis, and quantum Boltzmann machine, were developed (Wiebe et al. 2012;Schuld et al. 2015a;Biamonte et al. 2017;Rebentrost et al. 2014;Lloyd et al. 2014;Amin et al. 2018;Gao et al. 2018), and these algorithms were shown to be more efficient than their classical versions.
In recent years, DNNs (LeCun et al. 2015) became the most important and powerful method in machine learning, which were widely applied in computer vision (Voulodimos et al. 2018), natural language processing (Socher et al. 2012), and many other fields. The basic unit of DNN is the perceptron, which is an affine transformation together with an activation function. The non-linearity of the activation function and the depth give the DNN much representation  (Hornik 1991;Leshno et al. 1993). Approaches were proposed to build classical DNNs on quantum computers Zhao et al. 2019;Kerenidis et al. 2020). They achieved quantum speed-up under certain assumptions. But the structure of classical DNNs is still used, and only some local operations are speeded up by quantum algorithms. For instance, the inner product was speedup using the swap test (Zhao et al. 2019).
Several quantum analogs of DNNs were proposed. In Schuld et al. (2015b) and Cao et al. (2017), quantum analogs of the perceptron are demonstrated. However, methods of building complex DNNs with these quantum perceptrons are not developed. In Wan et al. (2017), a quantum generalization of the feedforward neural network is proposed. However, non-linearity is not introduced in this model. In Tacchino et al. (2020), the non-linearity is introduced with measurement, as a consequence, the training cost is increased. In Steinbrecher et al. (2019), a quantum analog of the DNN which can be run on optical quantum devices was proposed. A quantum analog of deep convolutional neural networks was proposed in Li et al. (2020). In many of these approaches, the inputs or outputs are quantum states, and hence the quantum random access memory (QRAM) (Giovannetti et al. 2008;Aaronson 2015) is used. However, QRAM is difficult to be implemented on noisy intermediate-scale quantum (NISQ) (Preskill 2018) devices. It is known that NISQ will be the only quantum devices that can be used in the near-term, where only a limited number of qubits without error-correcting can be used.
Recently, parameterized quantum circuits (PQCs) were widely considered, because PQCs can be efficiently implemented on NISQ devices. Several NISQ quantum machine learning models based on PQCs, such as quantum generative adversarial networks, quantum circuit Born machine, and quantum kernel methods, were proposed (Lloyd and Weedbrook 2018;Dallaire-Demers and Killoran 2018;Liu and Wang 2018;Havlíček et al. 2019;Benedetti et al. 2019). Several approaches are shown that PQCs have the potential abilities in machine learning tasks including approximating functions (Mitarai et al. 2018), classification (Farhi and Neven 2018;Schuld et al. 2020), and data generating (Liu and Wang 2018;Situ et al. 2020). PQCs are also called quantum neural networks (QNNs) because of its layerwise circuit structure, and QNNs are used for machine learning tasks (Farhi and Neven 2018;Beer et al. 2020). Several different structures of the PQC were proposed (Grant et al. 2018;Liu et al. 2019a;Cong et al. 2019).
In this paper, we introduce the quantum deep neural network (QDNN) which is a composition of multiple quantum neural network layers (QNNLs). We prove that the QDNN can uniformly approximate any continuous function and has more representation power than the classical DNN. Unlike other approaches of quantum analogs of DNNs, our QDNN still keeps the advantages of the classical DNN such as the non-linear activation, the multi-layer structure, and the efficient backpropagation training algorithm. The inputs and the outputs of the QDNN are both classical which makes the QDNN more practical. Because the QNNL is based on PQCs, the QDNN has the potential to be used on NISQ processors. As shown in our experiments, a QDNN with a small number (eight) of qubits can be used in image classification. In summary, QDNN provides a new class of neural networks which can be used in near-term quantum computers and is more powerful than classical DNNs.
The structure of the QNNL is similar to that of the QNN. There usually exists only one Hamiltonian in the model of QNN while there exist multiple Hamiltonians in the QNNL and a bias term will be added at the output. The multiple dimensional output of the QNNL makes it possible to build multi-layer structure in QDNN. We use QNNLs as building blocks of the QDNN and we use multiple PQCs which will be trained simultaneously. The universal approximation property of QDNN comes from its multi-layer structure. The QNN can be regarded as a special 1-layer QDNN and has no universal approximation property.
Our paper is organized as follows. We define the QNNL in Section 2.1. The definition of the QDNN and its training algorithms are presented in Section 2.2. In Section 3, we discuss the representation power and potential quantum advantages of the QDNN. In Section 4, a numerical experiment for an image classification task based on QDNN is used to show the effectiveness of QDNN.

The QDNN
A DNN consists of a large number of neural network layers, and each neural network layer is a non-linear function is an affine transformation and σ is a non-linear activation function. The power of DNNs comes from the non-linearity of the activation function. Without activation functions, DNNs will be nothing more than affine transformations.
However, all quantum gates are unitary matrices and hence linear. So the key point of developing QNNLs is introducing non-linearity.

Quantum neural network layers
We build QNNLs using the hybrid quantum-classical algorithm scheme (McClean et al. 2016), which is widely used in many NISQ quantum algorithms (Liu and Wang 2018;Liu et al. 2019b). As shown in Fig. 1, a hybrid quantumclassical algorithm scheme consists of a quantum part and a classical part. In the quantum part, parameterized quantum circuits (PQCs) are used to prepare quantum states with quantum processors. In the classical part, parameters of the PQCs are optimized using classical computers.
A PQC is a quantum circuit with parametric gates, which is of the form As shown in Fig. 1, once fixed an ansatz circuit U( θ) and a Hamiltonian H , we can define the loss function of the form L = 0|U † ( θ)H U( θ)|0 . Then, we can optimize L by updating parameters θ using optimization algorithms Nakanishi et al. 2019). With gradientbased algorithms , one can efficiently compute the gradient information ∂L ∂ θ which is essentially important in our model. Hence, we will focus on gradientbased algorithms in this paper. Now, we are going to define a QNNL, which consists of 3 parts: the encoder, the transformation, and the output, as shown in Fig. 2.
(1) The encoder. For a classical input data x ∈ R n , we introduce non-linearity to our QNNL by encoding the input x to a quantum state |ψ( x) non-linearly. Precisely, we choose a PQC U( x) with at most O(n) qubits and apply it to an initial state |ψ 0 to obtain a quantum state encoded from x. The PQC is naturally non-linear in the parameters. For example, the encoding process is non-linear. Moreover, we can compute the gradient of each component of x efficiently. The gradient of the input in each layer is necessary when training the QDNN. The encoding step is the analog to the classical activation step.
(2) The transformation. After encoding the input data, we apply a linear transformation as the analog of the linear transformation in the classical DNNs. This part is natural on quantum computers because all quantum gates are linear. We use another PQC V ( − → W ) with parameters − → W for this purpose. We assume that the number of parameters in V ( − → W ) is O(poly(n)).
(3) The output. Finally, the output of a QNNL will be computed as follow. We choose m fixed Hamiltonians H j , j = 1, . . . , m, and output Note that the expectation value of a Hamiltonian is a linear function of the density matrix. Here, the bias is an analog of bias in classical DNNs. Also, each y j is a hybrid quantum-classical scheme with a PQC U( x)V ( − → W ) and Hamiltonians H j .
To compute the output efficiently, we assume that the expectation value of each of these Hamiltonians can be computed in O(poly(n, 1 ε )), where ε is the precision. It is easy to show that all Hamiltonians of the following form satisfy this assumption where H i are tensor products of Pauli matrices or k-local Hamiltonians.
In summary, a QNNL is a function : R n → R m defined by Eqs. 1) and 2, and shown in Fig. 2. Note that a QNNL is a function with classic input and output, and can be determined by a tuple , b). Notice that the QNNLs activate before affine transformations while classical DNNLs activate after affine transformations. But this difference can be ignored when considering multi-layers.

QDNN and its training algorithms
Since the input and output of QNNLs are classical values, they can be implemented without the assumption of QRAM. Furthermore, the QNNLs can be naturally embedded in classical DNNs. A neural network consists of the composition of multiple compatible QNNLs and classical DNN layers is called quantum DNN (QDNN): is a classical or a quantum layer from . . , l} are the parameters of the QDNN.
We will use gradient descent to update the parameters. In classical DNNs, the gradient of parameters in each layer is computed by the backpropagation algorithm (BP). Suppose that we have a QDNN. Consider a QNNL Q with parameters W , b, whose input is x and output is y. Refer to Eqs. 1 and 2 for details. To

Representation power of QDNNs
In this section, we will consider the representation power of the QDNN. We will show that QDNN can approximate any continuous function similar to the classical DNN. Moreover, if quantum computing can not be classically simulated efficiently, the QDNN has more representation power than the classical DNN.

Universal approximation property of QDNNs
The universal approximation theorem ensures that DNNs can approximate any continuous function (Cybenko 1989;Hornik 1991;Leshno et al. 1993;Pinkus 1999). Since the class of QDNNs is an extension of the class of classical DNNs, the universal approximation theorem can be applied to the QDNN trivially. Now, we show that QDNNs with only QNNLs also have the universal approximation property. Consider two cases -DNN with only QNNLs.
-DNN with QNNLs and affine layers.
In the first case, let us consider a special type of QNNLs which can represent monomials (Mitarai et al. 2018). Consider the circuit and the Hamiltonian H 0 = |0 0|. The expectation value of 0|U † (x)H 0 U(x)|0 is the monomial x for x ∈ [0, 1]. For multivariable monomial x = x m 1 1 · · · x m k k , we use the circuit and the Hamiltonian H 0 = |0 . . . 0 0 . . . 0|, where ⊗ is the tensor product and Similarly, the expectation value of With the above results and Stone-Weierstrass theorem (Stone 1948), we can deduce the following theorem.

Theorem 1 The QDNN with only QNNLs can uniformly approximate any continuous function
Now, let us consider the second case. As the affine transformation can map the hypercube [0, 1] k to [a 1 , b 1 ] × · · · × [a k , b k ] for any a k < b k . Hence we have the following result.

Corollary 1 The QDNN with QNNLs and affine layers can uniformly approximate any continuous function
Also, the QNNL can be used as a non-linear activation function. For example, we consider a QNNL Q ac with the input circuit and the Hamiltonian where the projection is on the j th qubit for j = 1, . . . , m. By simple computation, we have By the universal approximation property (Leshno et al. 1993;Kratsios 2019), neural networks with non-polynomial activation functions can approximate any continuous function f : R k → R l . Thus, the QDNN with QNNLs and affine layers can approximate any continuous function. Similar to the classical case, QDNNs with one quantum layer can approximate any continuous function (Hornik 1991). However, if we restrict the number of parameters to be polynomial, then there exist functions which can be approximated with large-depth DNNs and cannot be approximated by small-depth neural networks (Eldan and Shamir 2016;Daniely 2017;Vardi and Shamir 2020), and this is the reason to use multilayer QDNNs.

Quantum advantages
According to the definition of QNNLs in Eq. 2, each element of the outputs in a QNNL is of the form In general, estimation of |ψ 0 on a classical computer will be difficult by the following theorem.
Theorem 2 Estimation (6) with precision c < 1 3 is BQPhard, where BQP is the bounded-error quantum polynomial time complexity class.
Proof Consider any language L ∈ BQP. There exists a polynomial-time Turing machine which takes x ∈ {0, 1} n as input and outputs a polynomial-sized quantum circuit C(x).
Moreover, x ∈ L if and only if the measurement result of C(x)|0 of the first qubit has the probability ≥ 2 3 to be |1 . Because {R X , R Z , CNOT} are universal quantum gates, C(x) can be expressed as a polynomial-sized PQC: U x ( θ) = C(x) with proper parameters. Consider H = Z⊗I ⊗· · ·⊗I , then if and only if x ∈ L, and if and only if x / ∈ L.
Given inputs, computing the outputs of classical DNNs is polynomial time. Hence, functions represented by classical DNNs are characterized by the complexity class Ppoly. On the other hand, computing the outputs of QDNNs is BQPhard in general according to Theorem 2. The functions represented by QDNNs are characterized by a complexity class that has a lower bound BQP/poly. Here, BQP/poly is the problems which can be solved by polynomial sized quantum circuits with bounded error probability (Aaronson et al. 2005). Under the hypothesis that quantum computers cannot be simulated efficiently by classical computers, which is generally believed, there exists a function represented by a QDNN which cannot be computed by classical circuits of polynomial size. Hence, QDNNs have more representation power than DNNs.

Experimental results
We will use QDNNs to conduct a numerical experiment for an image classification task. The data comes from the MNIST data set. We built a QDNN with 3 QNNLs. The goal of this QDNN is to recognize the digit in the image is either 0 or 1 as a classifier.

Experiment details
The data in the MNIST is 28 × 28 = 784 dimensional images. This dimension is too large for the current quantum simulator. Hence, we first resize the image to 8 × 8 pixels. We use three QNNLs in our QDNN, which will be called the input layer, the hidden layer, and the output layer, respectively.
In the experiments, we use trainable QDNNs by adopting local Hamiltonians and small depth structure. We set the ansatz circuit to be the one in Fig. 3, which is similar to the hardware efficient ansatz (Kandala et al. 2017) and has small depth. Because of the small depth of the ansatz and the local Hamiltonian, the QDNN is trainable (Cerezo et al. 2020). Also, the small depth makes the model possible to be run on NISQ devices. The hyperparameters D T and D E will be chosen depending on the problem to be solved.

Input layer
The input layer uses an 8-qubit circuit which accepts an input vector x ∈ R 64 and outputs a vector h 1 ∈ R 24 . The structure in Fig. 3a to be the transformation circuit in this layer. The output of the input layer is of the form ( x) and H j,M denote the result obtained by applying the operator M on the j -th qubit for M ∈ {X, Y, Z}.

Hidden layer
The hidden layer uses 6 qubits. It accepts an vector h 1 ∈ R 24 and outputs a vector h 2 ∈ R 12 . The structure shown in Fig. 3b is used, with D E = 1, D T = 4. Because there are 30 parameters in the encoder, we set the last column of R Z gates to be R Z (0). Similar to the input layer, the output of the hidden layer is

Output layer
The output layer uses 4 qubits. We use the structure in Fig. 3c with D E = 1, D T = 2. Because there are 20 parameters in the encoder, we set the last column of R Z and R X gates to be R Z (0) and R X (0). The output of the output layer is (11) Notice that we do not add bias term here, and it will output a vector in R 2 . Moreover, after training, we hope to see if the input x is from an image of digit 0, the output y should be close to 1 0 , otherwise it should be close to 0 1 .
In conclusion, the settings of these three layers are shown in Table 1. Finally, the loss function is defined as where D is the training set.

Experiments result
We used the Julia package Yao.jl (Luo et al. 2019) as a quantum simulator in our experiments. All data were collected on a desktop PC with Intel CPU i7-4790 and 4GB RAM. All parameters were initialized randomly in (−π, π). We use Adam optimizer (Kingma and Ba 2014) to update parameters. We train this QDNN for 200 iterations with batch size of 240. We set the number of samples when evaluating the expectation value of Hamiltonian each time to 100, 1000, 1000 and ∞. The hyper parameters of Adam is set to be η = 0.01, β 1 = 0.9, β 2 = 0.999.
The values of the loss function on the training set during training is shown in Fig˙4. The loss function and accurate rate of this QDNN on both training set and test set after training are shown in Table 2. It shows that when the number of samples reaches 1000, we can train the QDNN with high performance.

Discussion
We introduce the model of QNNL and built QDNN with QNNLs. We proved that QDNNs have more representation power than classical DNNs. We presented a practical gradient-based training algorithm as the analog of BP algorithms. As a result, the QDNN still keeps most of the advantages of the classical DNNs. Because the model is based on the hybrid quantum-classical scheme, it has the potential to be realized on NISQ processors.
Since we use a classical simulator on a desktop PC for quantum computation, only QDNNs with a small number of qubits can be used and only simple examples can be demonstrated. Quantum hardware is developing fast. Google achieved quantum supremacy by using a superconducting quantum processor with 53 qubits (Arute et al. 2019). From Table 1, only 8 qubits are used in our experiments described in the preceding section, so in principle, our image classification experiment can be implemented in Google's quantum processor. However, due to the lack of access to a real quantum computer, we are not able to give simulation tests on how the QDNN works with noises. With quantum computing resources, we can access exponential dimensional feature Hilbert spaces  with QDNNs and only use polynomial-size parameters. Hence, we believe that QDNNs will help us to extract features more efficiently than DNNs.