{'fraction_non_alphanumeric': 0.035704804884523496, 'fraction_numerical': 0.008561189275285373, 'mean_word_length': 4.215991692627207, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
QDNN: DNN with Quantum Neural Network Layers

The deep neural network (DNN) became the most important and powerful machine learning method in recent years. In this paper, we introduce a general quantum DNN, which consists of fully quantum structured layers with better representation power than the classical DNN and still keeps the advantages of the classical DNN such as the non-linear activation, the multi-layer structure, and the efficient backpropagation training algorithm. We prove that the quantum structured layer can not be simulated efficiently by classical computers unless universal quantum computing can be classically simulated efficiently and hence our quantum DNN has more representation power than the classical DNN. Moreover, our quantum DNN can be used on near-term noisy intermediate scale quantum (NISQ) processors. A numerical experiment for image classification based on quantum DNN is given, where high accurate 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 [1,2]. Noisy intermediate scale quantum (NISQ) [3] devices will be the only quantum devices that can be used in near-term, where we can only use a limited number of qubits without error correcting. So developing NISQ algorithms is a new challenge.
In recent years, deep neural network [16] became the most important and powerful method in machine learning, which was widely applied in computer vision [17], natural language processing [18], and many other fields. The basic unit of DNN is the perception, which is an affine transform together with an activation function. The non-linearity of the activation function and the depth give the DNN more representation power. Approaches have been proposed to build classical DNNs on quantum computers [19][20][21]. They achieved quantum speed-up under certain assumptions. But the structure of classical DNNs is still used, since only some operations are speeded up by quantum algorithms, for instance, to speedup the inner product using the swap test [20].
In this paper, we introduce the first quantum analog to classical DNN, which consists of fully quantum structured layers with better representation power than the classical DNN and 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 main contribution of this paper is to introduce the concept of quantum neural network layer (QNNL) as a quantum analog to the classic neural network layer in DNN. As all quantum gates are unitary and hence linear, the main difficulty of building a QNNL is introducing non-linearity. We solved this problem by encoding the input vector to a quantum state non-linearly with a PQC. A QNNL is a quantum circuit which is totally different from the classical neural network layer. A quantum DNN (QDNN) can be easily built with QNNLs, since the input and output of a QNNL are classical values.
The advantage of introducing QNNLs is that we can access vectors of exponential dimensional Hilbert spaces with only polynomial resources on a quantum computer. We proved that this model can not be classically simulated efficiently unless universal quantum computing can be classically simulated efficiently. So QDNNs have more representation power than classical DNNs. We also give training algorithms of QDNNs which are similar to backpropagation (BP) algorithm. Moreover, QNNLs use the hybrid quantum-classical scheme. Hence, a QDNN with a reasonable size can be trained efficiently on NISQ processors. Finally, a numerical experiment for an image recognition is given using QDNNs, where high accurate rate is achieved.
We finally remark that all tasks using DNN can be turned into quantum algorithms with more representation powers by replacing the DNN by QDNN.

Hybrid quantum-classical algorithm
The hybrid quantum-classical algorithm scheme [22] consists of a quantum part and a classical part. In the quantum part, one uses parametric quantum circuits (PQCs) to prepare quantum states using quantum processors. In the classical part, one uses classical computers for optimizing the parameters of the PQCs in the quantum parts.

Hybrid quantum-classical scheme based on PQCs
PQCs are quantum circuits with parametric gates. In general, a PQC is of the form where θ = (θ 1 , . . . , θ l ) are the parameters, each U j (θ j ) is a rotation gate U j (θ j ) = exp(−i θj 2 H j ), and H j is a 1-qubit or a 2-qubits gate such that H 2 j = I. For example, in this paper we will use the Pauli gates X, Y, Z, and the CNOT gate.
In practical tasks such as VQE [23] and quantum machine learning [12], we want to find a quantum state |ψ with certain desired properties. This can be done with the following three steps based on the hybrid quantum-classical scheme. First, we need to choose an appropriate ansatz, that is, designing the circuit structure of a PQC U ( θ). All parameters θ are initialized randomly. Then we apply this PQC to a fixed initial state |ϕ 0 , for instance |0 . Second, by measuring the final state |ψ = U ( θ) |ϕ 0 repeatedly, we can estimate the expected value L = ψ| H |ψ for an Hamiltonian H. H will be designed differently in different tasks. In many tasks, the ground state of H is our goal. To achieve this goal, in the final step, we optimize the loss function L, by updating parameters θ on classical computers. In summary, a hybrid quantum-classical scheme, as shown in Figure 1, consists of a PQC U (θ) and a loss function of the form L = ψ| H |ψ together with a classical algorithm for updating parameters, where H is a Hamiltonian.

Optimization in hybrid quantum-classical scheme
There are many methods for optimizing the loss function for a hybrid quantumclassical scheme based on PQCs. Some are gradient-based [24] and some are gradient-free [25]. We will focus on gradient-based algorithms in this paper.
The gradient ∇ θ L can be estimated by shifting parameters of PQCs without changing the circuit structure. The detail of the gradient estimation algorithm can be found in Appendix A. Once the gradient is obtained, we can use the gradient descent method to update the parameters.

DNNs with quantum neural network layers
In this section, we will introduce the concepts of quantum neural network layer (QNNL) and quantum DNN, and give a training algorithm for the quantum DNN.

QNNL and 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 transform 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 transforms.
However, all quantum gates are unitary matrices and hence linear. So the key point of developing QNNLs is introducing non-linearity.
Suppose that the input data x ∈ R n is classical. We introduce non-linearity to our QNNL by encoding the input x to a quantum state |ψ( x) non-linearly. Concretely, we will use a PQC for this process. Choose a PQC U ( x) with at most O(n) qubits and apply it to an initial state |ψ 0 . We obtain a quantum state encoded from x. The PQC is naturally non-linear in the parameters. For example, the encoding process from x to |ψ(x) is non-linear. Moreover we can compute the gradient of each component of x efficiently. This is very important, since we need the gradient of the input in each layer when training the QNNL. The encoding step is the analog to the classical activation step. After encoding the input data, we apply a linear transform as the analog of linear transform 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 step. We assume that the number of parameters in Finally, the output of a QNNL will be computed as follow. We choose m of fixed Hamiltonians H j (which means we will not change them during training), j = 1, . . . , m, and output Here, the bias term b = (b 1 , . . . , b m ) is an analog of bias in classical DNNs. Also, each y j is a hybrid quantum-classical scheme with PQC U and Hamiltonian OUTPUT Q Figure 2: The structure of a QNNL Q 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 Hamiltonianss 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 (1) and (2), and shown in Figure 2. Note that a QNNL is a function with classic input and output, and can be determined by a tuple with parameters ( − → W , b). Notice that the QNNLs activate before affine transforms while classical DNNLs activate after affine transforms. But this difference can be ignored when we consider multi-layers.
Since the input and output of QNNLs are classical, these QNNLs can be naturally embedded in classical DNNs. A DNN consists of the composition of multiple compatible QNNLs and classical DNN layers is called quantum DNN (QDNN):

Training algorithms of QDNNs
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, output is y. Refer to (1) and (2)

Representation power of QDNNs
In this section, we will show that QDNNs have more representation power than that of classical DNNs.
According to the definition of QNNLs in (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. 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 y , 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.
As it is generally believed that quantum computers can not be simulated efficiently by classical computers, according to Theorem 1, there exist QNNLs which cannot be classically simulated efficiently. Hence, this QNNLs can represent functions which cannot be represented by classical DNNs with polynomial units. Thus, adding QNNLs to DNNs will enhance the representation power of DNNs.

Numerical experiments
In this section, we will use QDNN 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.
We uses the Julia package Yao.jl as a quantum simulator [26] in our experiments. All data were collected on a desktop PC with Intel CPU i7-4790 and 4GB RAM.

Details of the model
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. Each layer is made of 3 parts: encoder, transform, and output.

Input layer
The input layer uses an 8-qubit circuit. The encoder will accept an input vector x ∈ R 64 and use a PQC to map it to a quantum state |ψ( x) = U in ( x) |0 . The structure of the PQC is like Ent is a CNOT circuit which introduces entanglement to the circuit. In our experiment, Ent is of the following form. . . .

• •
The transform in the input layer is similar to the encoder. The structure of the transform is like Also, the transform parameters − → W in will be input to V in as the same way as x were input to U in .
The output of the input layer is of the form and H j,M denotes 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. The structure of the hidden layer is almost the same as the input layer, but with less qubit gates.
The encoder is of the form The transform is The output of the hidden layer is

Output layer
The output layer uses 4 qubits. The structure of the output layer is also similar to the input layer. The only difference is that in the output is classification result.
The encoder is The transform is like The output of the output layer is We do not add bias term here, and it will output a vector in R 2 . Moreover, if the input x is from an image of digit 0, the output y should be close to |0 , otherwise it should be close to |1 after training.
In conclusion, the settings of these three layers are shown in table 1.

# of qubits
where D is the training set.

Experiment results
All parameters were initialized randomly in (−π, π). We use Adam optimizer [27] to update parameters. We train this QDNN for 400 iterations with batch size of 240. In the first 200 of iterations, the hyper parameters of Adam is set to be η = 0.01, β 1 = 0.9, β 2 = 0.999. In the later 200 of iterations, we change η to 0.001. The values of the loss function on the training set and test set during training is shown in Figure 3. The accurate rate of this QDNN on the test set rise to 99.15% after training.

Discussion
We introduced 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. Because the model is based on hybrid quantum-classical scheme, it can be realized on NISQ processors. As a result, the QDNN has more representation powers than classical DNNs and still keeps most of the advantages of the classical DNNs.
Due to the limited power the classical simulator for quantum computation, only QDNNs with a small number of qubits can be used in practice. As a consequence, we only trained a model for a simple task in our experiment. If we have more quantum resources in the future, we can access exponential dimensional feature Hilbert spaces [13] with QDNNs and only uses polynomial size of parameters. Hence, we believe that QDNNs will help us to extract features more efficiently in exponential dimensional feature Hilbert spaces. This idea is similar to ideas of kernel methods [28,29].

A Gradient Estimation of PQCs
Without the loss of generality, suppose that a PQC has the form U ( θ) = U j (θ j ) . . . U 1 (θ 1 ).
Given a Hamiltonian M , the expection value is defined by the following equation, The goal in hybrid quantum-classical computing is usually optimizing L. We can use gradient descent for this problem. Thus, we need estimate