Computing the gradients with respect to all parameters of a quantum
  neural network using a single circuit
When computing the gradients of a quantum neural network using the
parameter-shift rule, the cost function needs to be calculated twice for the
gradient with respect to a single adjustable parameter of the network. When the
total number of parameters is high, the quantum circuit for the computation has
to be adjusted and run for many times. Here we propose an approach to compute
all the gradients using a single circuit only, with a much reduced circuit
depth and less classical registers. We also demonstrate experimentally, on both
real quantum hardware and simulator, that our approach has the advantages that
the circuit takes a significantly shorter time to compile than the conventional
approach, resulting in a speedup on the total runtime.
Guang Ping He
Computing the gradients with respect to all parameters of a quantum neural network using a single circuit

###### Abstract

When computing the gradients of a quantum neural network using the parameter-shift rule, the cost function needs to be calculated twice for the gradient with respect to a single adjustable parameter of the network. When the total number of parameters is high, the quantum circuit for the computation has to be adjusted and run for many times. Here we propose an approach to compute all the gradients using a single circuit only, with a much reduced circuit depth and less classical registers. We also demonstrate experimentally, on both real quantum hardware and simulator, that our approach has the advantages that the circuit takes a significantly shorter time to compile than the conventional approach, resulting in a speedup on the total runtime.

Artificial intelligence technology is making incredible progress nowadays. One of the main reason is that classical artificial neural networks are made very feasible with the advance of computer science in recent years. On the other hand, though it is widely believed that quantum neural networks may breathe new life into the researches, they are still not as practical as their classical counterparts due to the scale and performance of currently available quantum computers. Especially, highly effective methods have been developed for computing the gradients of the cost function with respect to the adjustable parameters of classical neural networks, e.g., the back-propagation algorithm [1; 2]. A distinct feature is that the cost function needs to be calculated only once, and all the gradients will be readily deduced, so that classical neural networks can be trained fast and efficiently. But for quantum neural networks, computing the gradients is considerably less convenient [3]. Effective algorithms were found for certain network architectures only [4]. For other general architectures, the parameter-shift rule [5; 6] seems to be the best algorithm found for this task so far. But it needs to calculate the cost function twice for computing the gradient with respect to a single parameter of the network (which will be further elaborated below). The values of some parameters for each calculation need adjustment too. Consequently, the quantum circuit for calculating the cost function has to be modified many times in order to obtain the gradients with respect to all parameters in each single round of training of the quantum network.

In this paper, we will propose an approach in which the gradients with respect to all parameters of a quantum neural network can be computing simultaneously using a single circuit only. The main purpose is to break the limit on the scale of computation on real quantum hardware. As it is well-known, the number of adjustable parameters of a powerful neural network is generally very high. If we have to run two quantum circuits to compute the gradient with respect to a single parameter, then the total number of unique executed circuits could easily exceed the capacity of any real quantum computer in existence. Of course, the execution of these circuits can be broken down into many "jobs". But it is obviously inconvenient when we are using quantum cloud computation platforms. Especially, if the jobs are submitted separately, then there could be a long waiting time between the execution of the jobs. For example, for free users on the IBM Quantum Experience online platform, this waiting time could vary from 10 minutes to 4 hours. For paying users, as also pointed out in Ref. [6], "quantum hardware devices are typically billed and queued per unique circuit". A conventional method for reducing the number of unique circuits is to "stack" many circuits into a single job. In the Qiskit software development kit for running IBM quantum computers and simulators with Python programs, this can be done using the "append" function [7]. But there is also a restriction on its usage on real quantum computers. For example, on the "ibmq_quito" backend, the number of circuits allowed to be appended is limited to 100. To break this limit, the Qiskit's "compose" function can serve as an alternative. According to our experience, it runs much slower than the "append" method does. But it can indeed stack more circuits into one single job so that the gradients with respect to more parameters can be computed together.

In the following, we will use this direct stacking method based on the "compose" function (called "the conventional approach") as a reference, to compare with the performance of our proposed method that uses a single circuit only (called "the improved approach").

## Results

**The variational quantum circuit**

There are various types of quantum neural networks. Among them, variational quantum circuits (VQCs) [8; 9; 10; 11; 12; 13] have the advantage that it can be executed on current or near-term quantum computers. In this paper, we will use it as an example to illustrate how our improved approach can be applied to computing the gradients with respect to its parameters. As shown in Fig.1(a), the gen
eral structure of a VQC can be divided into three blocks: the feature map, the ansatz, and the measurement of the observable, which gives the value of the cost function.

Note that the goal of this paper is to provide a method for computing the gradients, instead of finding a VQC for a specific application. To this end, the details of the feature map and the measurement are less relevant, and our approach does not need to modify them. Thus, we will leave their description in the Method section. The ansatz is the most important block, because it contains all the adjustable parameters with respect to whom the gradients need to be computed. Our example uses the _RealAmplitudes_ ansatz [14], which is the default ansatz in Qiskit's VQC implementation. As pointed out in Ref. [13], this ansatz has also been used in a VQC with proven advantages over traditional feedforward neural networks in terms of both capacity and trainability [10]. The typical structure of the _RealAmplitudes_ ansatz is illustrated in Fig.1(b).

**The parameter-shift rule**

Finding the gradients is a must for optimizing neural networks for certain applications. In our example, the original parameter-shift rule proposed in Ref. [5] will be used, where the exact gradient of the cost function \(f=f(\vec{\theta})\) with respect to the parameters \(\vec{\theta}=(\theta_{1},...\theta_{n})\) of the quantum gates is obtained as

\[\frac{\partial f}{\partial\theta_{i}}=r\left(f\left(\vec{\theta}+s\vec{e}_{i} \right)-f\left(\vec{\theta}-s\vec{e}_{i}\right)\right) \tag{1}\]

for \(i=1,...,n\), where \(r=1/2\) and \(s=\pi/2\) for all the rotation and phase gates available in the Qiskit library.

This equation shows that computing the gradient with respect to a single parameter \(\theta_{i}\) needs to calculate the cost function \(f\) twice, each time with a different set of parameters, i.e., \(\vec{\theta}+s\vec{e}_{i}=(\theta_{1},...,\theta_{i}+s,...\theta_{n})\) and \(\vec{\theta}-s\vec{e}_{i}=(\theta_{1},...,\theta_{i}-s,...\theta_{n})\). Therefore, when the total number of parameters of a VQC is \(n\), computing all the gradients will have to run \(2n\) circuits with different values of the parameters. To stack these circuits into a single job, the aforementioned conventional approach is simply to connect them one after another using Qiskit's "compose" function, as illustrated in Fig.2. Each circuit is independently executed and measured before the next one is re-initialized and parameterized with new values, and the measurement results are stored in different classical registers.

**Our improved approach**

Now it will be shown how to compute all the \(2n\) shifted cost functions as well as the original unshifted cost function \(f(\vec{\theta})\) using a single circuit. Two additional control qubits \(q_{a}\) and \(q_{b}\) need to be added to the circuit, regardless the number of qubits and parameters in the original ansatz. Then for each existing single-parameter gate \(RY(\theta_{i})\) (\(i=1,...,n\)), we added two blocks to the qubit it rotates and the above two additional control qubits, to turn the parameter from \(\theta_{i}\) to \(\theta_{i}+s\) and \(\theta_{i}-s\), respectively. Each block contains 2 controlled-RY gates, 1 CX (two-qubit controlled-NOT) gate, 1 measurement and 1 reset operation. Fig.3 showcases an example of the resultant circuit. For simplicity, only three RY gates in the _RealAmplitudes_ ansatz of the original VQC (corresponding to the three RY gates before any of the red dashed

Figure 1: (a) The general structure of a variational quantum circuit (VQC). All qubits \(q_{i}\) (\(i=1,...,n\)) are initialized in the state \(|0\rangle\), then pass through three blocks: the feature map, the ansatz and the observable, which apply certain unitary transformations. Finally they are measured in the computational basis, and the result is sent to the classical register \(c\). All the adjustable parameters \(\vec{\theta}=(\theta_{1},...\theta_{n})\) are contained in the ansatz. (b) An example of the _RealAmplitudes_ ansatz on three qubits, with two repetitions and full entanglement. \(RY(\theta_{i})\) (\(i=1,...,9\)) are single-qubit RY rotation gates, with the adjustable parameter \(\theta_{i}\) denoting the rotation angle about the \(y\)-axis. The blue icons are CX (controlled-NOT) gates. The red dashed boxes denote where the additional circuit of our improved approach shown in Fig.3 will be added.

Figure 2: Diagram of the conventional approach, in which the circuits of many VQCs are stacked in serial using Qiskit’s “compose” function. Each black box contains all the components of the complete VQC shown in Fig.1(a), i.e., the feature map, the ansatz, and the observable along with the measurement. But the adjustable parameters \(\vec{\theta}=(\theta_{1},...\theta_{n})\) for each ansatz take different values.
boxes in Fig.1(b)) are studied, and shown with the two additional blocks added to each of them. The detailed workflow of the circuit is elaborated in the Method section.

### Theoretical advantages

_a. Circuit depth_

Comparing with the conventional approach, a significant advantage of our improved approach is that the depth of the entire circuit is reduced, as estimated below.

For each single input data, let \(\Lambda\) denote the depth of the circuit for calculating the cost function once using the conventional approach, and \(n\) denote the number of the adjustable parameters. Define

\[\lambda=\frac{\Lambda}{n}. \tag{2}\]

In many ansatz, \(\lambda\) remains non-trivial even for high \(n\). (We prove this in the Method section by using the _RealAmplitudes_ ansatz as an example.) Recall that computing the gradient with respect to a single parameter using the parameter-shift rule needs to calculate the cost function twice. Thus, the total depth of the stacked circuit for computing all the \(n\) gradients and the unshifted cost function using the conventional approach is

\[D_{conv}=\Lambda(2n+1)=\lambda n(2n+1)\propto O(n^{2}). \tag{3}\]

On the other hand, when using our improved approach, from Fig.3 we can see that a total of 10 operations were added to each existing parameterized RY gate of the original ansatz. Therefore, to compute all the \(n\) gradients and the unshifted cost function, the total depth of our circuit is simply

\[D_{impr}=\Lambda+10n=\lambda n+10n\propto O(n). \tag{4}\]

This result shows that our improved approach has the advantage that it takes a lower circuit depth when the number of parameters \(n\) is high, and the improvement will be more significant with the increase of \(n\).

_b. Number of classical bits in the stacked circuits_

In the conventional approach, for each single input data, running the circuit for calculating the cost function once (either shifted or unshifted) takes \(Q\) classical bits to store the final measurement result of the \(Q\) qubits. When all the \(2n+1\) circuits for computing all the \(n\) gradients and the unshifted cost function are stacked together so that it can be submitted as a single job on the real hardware, the total number of required classical bits is

\[N_{conv}=Q(2n+1). \tag{5}\]

In our improved approach, the \(Q\) qubits of the original ansatz are measured only once at the very end of the circuit. The additional qubit \(q_{b}\) is measured twice in the two additional blocks added to each of the \(n\) parameterized gates of the original ansatz, which takes \(2n\) classical bits to store the results. At the end of the circuit, both the additional qubits \(q_{a}\) and \(q_{b}\) do not need to be measured. But for simplicity, we added these two measurements in our code too (not shown in Fig.3), which may also serve as an additional monitoring of the running of the circuit. Consequently, the total number of classical bits required in our approach is

\[N_{impr}=Q+2n+2, \tag{6}\]

which is smaller than that of the conventional approach since generally \(Q\geq 2\). This is another advantage of our approach.

### Theoretical disadvantages

Our improved approach requires two more qubits than the conventional approach. It is considerable on free

Figure 3: The quantum circuit for our improve approach. The section inside the red dashed box can be put into any one of the three red dashed boxes in Fig.1(b). Here \(q_{a}\) and \(q_{b}\) are the additional control qubits. They need to be initialized in the states \(\left|1\right\rangle_{q_{a}}\) and \(\left|0\right\rangle_{q_{b}}\) only once at the very beginning of Fig.1(b). The two green/blue/purple dashed boxes will create the shift \(\pm s\) to the gates \(RY(\theta_{1})/RY(\theta_{2})/RY(\theta_{3})\), respectively, with equal probabilities as controlled by \(q_{a}\) and \(q_{b}\). Each of these dashed boxes starts with a controlled-RY gate \(CRY(\gamma_{j})\), where \(\gamma_{j}\) is defined in Eq. (16). Then a measurement is made on \(q_{b}\), with the result recorded in the classical registers. Another controlled-RY gate \(CRY(\pm s)\) is placed between \(q_{b}\) and \(q_{i}\) (\(i=1,2,3\)), followed by a CX gate between \(q_{b}\) and \(q_{a}\). Finally, \(q_{b}\) is reset to the state \(\left|0\right\rangle_{q_{b}}\).
quantum computation platforms, which generally offers a total of 5\({}^{\text{-}}\)6 qubits only. But on near-term quantum devices that could really play a role in practical applications, the number of qubits has to be much higher, so that taking two more qubits should not make much difference.

What really matters is that our approach takes more shots than the conventional approach does in order to compute the gradients up to approximately the same precision. This is because in the conventional approach, when the stacked circuit is executed for \(s\) shots, each of the shifted and unshifted cost functions is calculated for exactly \(s\) shots too. But in our approach, when executing the circuit once, only one of the cost functions is calculated, depending on which of the additional blocks was activated by chance. To ensure that each cost function will be calculated for approximately \(s\) shots, our circuit needs to be executed for about \(s^{\prime}=s(2n+1)\) shots in total. This surely increases the runtime of the program. But recall that our circuit has a lower depth, which will take less time to compile. Therefore, whether the approach can result in a speedup on the total runtime depends on the competition between these two factors, and will be studied experimentally below.

Also, in our approach whether an additional block will be activated is controlled by the measurement result of the qubit \(q_{b}\), where quantum uncertainty takes effect. Consequently, executing the circuit for \(s^{\prime}=s(2n+1)\) shots does not mean that each cost function will be calculated for \(s\) shots exactly. Some statistical fluctuations are inevitable. Nevertheless, the relationship between the number of shots and the precision of the results is also statistical and subjected to quantum uncertainty. A fluctuation of the precision cannot be avoided even if all individual shot numbers are strictly aligned to the same value. Thus, we do not see the need to take extra efforts trying to level off the individual shot numbers of all the cost functions so that they equal exactly to each other.

**Experiments**

To compare the performance of our improved approach and the conventional one, we tested them experimentally on both real quantum hardware and simulator.

First, the validity of our improved approach is checked using a VQC with 8-dimensional classical input data, which takes \(Q=3\) qubits to encode when using the amplitude encoding method (see the Method section). The _RealAmplitudes_ ansatz in this VQC contains 1 repetition with full entanglement. Thus, it has \(n=6\) adjustable parameters. As it is complicated to calculate analytically the exact amplitude distribution of the output states, we run the conventional approach for this VQC on quantum simulator for \(s=3\times 10^{6}\) shots instead, and use the result of the gradients as the reference values, which should be a good approximation of the exact values. Then we run our improved approach for the same VQC for \(s^{\prime}=500\times(2n+1)=6500\) shots, so that each cost function could be calculated for about \(s=500\) shots in average (for simplicity, in the following when we say that the average number of shots for our improved approach is \(s\), we mean that the actual number of shots is \(s^{\prime}=s(2n+1)\)). For comparison, the conventional approach is also run for another \(s=500\) shots. The numbers of input data are all taken to be \(m=20\) in these experiments. The results of the gradients are shown in Table 1.

From the results we can see that when the (average) number of shots is \(s=500\), both approaches result in similar precision when comparing with the reference values. (The precision for the gradient \(grad(\theta_{2})\) with respect to the second adjustable parameter \(\theta_{2}\) is low for both approaches, probably because the point happens to meet the barren plateau.) Thus, it is proven that the modified quantum circuit (i.e., Fig.3) in our improved approach works correctly as desired.

Secondly, we studied the individual number of shots for each single cost function that was actually calculated in the above experiment using our improved approach. The result is shown in Fig.4. It is found that the fluctuation of the individual number of shots falls within the range \([445,587]\), with a standard deviation 22.6, which is only 4.52% of the average value \(s=500\), indicating that all the cost functions were calculated to almost the same precision.

Finally, the runtimes of both approaches are compared using 3 experiments:

Exp.1: The VQC used in Table 1 and Fig.4, run on quantum simulator.

Exp.2: The same VQC run on real quantum hardware (the "ibmq_quito" backend).

Exp.3: A VQC with 784-dimensional classical input data, with the _RealAmplitudes_ ansatz containing \(r=2\) repetitions and full entanglement, run on quantum simulator. Note that this VQC takes \(Q=10\) qubits to encode each input data with the amplitude encoding method, so that the total number of adjustable parameters is \(n=30\)

\begin{table}
\begin{tabular}{l|l|l|l} \hline  & Reference & Conventional & Improved \\ Gradient & value & approach & approach \\ \hline \(grad(\theta_{1})\) & 0.1527 & 0.1589 & 0.1573 \\ \(grad(\theta_{2})\) & -0.0029 & -0.0050 & 0.0010 \\ \(grad(\theta_{3})\) & -0.2667 & -0.2691 & -0.2702 \\ \(grad(\theta_{4})\) & -0.0836 & -0.0897 & -0.0841 \\ \(grad(\theta_{5})\) & 0.1074 & 0.1064 & 0.1034 \\ \(grad(\theta_{6})\) & -0.1812 & -0.1755 & -0.1800 \\ \hline \end{tabular}
\end{table}
Table 1: Precision comparison between the two approaches on the obtained value of the gradient \(grad(\theta_{i})\) with respect to the adjustable parameter \(\theta_{i}\) (\(i=1,...,6\)). The VQC takes 8-dimensional classical input data, with the _RealAmplitudes_ ansatz containing \(r=1\) repetition and full entanglement. The number of input data is \(m=20\). The reference values are obtained by running the conventional approach for \(s=3\times 10^{6}\) shots. The values in the last two columns are both obtained by taking \(s=500\).
as given by Eq. (17).

Let \(t_{c}\) (\(t_{r}\)) denote the time spent on compiling (running) the circuit. The sum \(t_{s}=t_{c}+t_{r}\) can serve as a good measure of the performance of the approaches, as the other parts of the computer programs (e.g., reading the input data and initial parameters, calculating the cost functions and gradients from the raw counting result of quantum simulator or real hardware, and exporting the results to the output files) run very fast and take almost the same amount of time for both approaches. The results are shown in Fig.5. The number of shots for each data point is fixed as \(s=500\) (i.e., the actual number of shots is \(s^{\prime}=500(2n+1)\) for our improved approach).

The following observations can be found in all these experiments.

(1) The values of \(t_{r}\) show that our improved approach always takes more time to run than the conventional approach does, either on simulator or real hardware. The difference is about 3\(\char 674\) times (in Exp.1 and Exp.2) to 24 times (in Exp.3). This is not surprising, because the improved one needs to be run for more shots to reach the same precision.

(2) On the contrary, the values of \(t_{c}\) show that our improved approach saves a tremendous amount of time for compiling the circuit, which is only about 4% (in Exp.1 and Exp.2) to 0.1% (in Exp.3) of that of the conventional approach. This is owed to the significantly reduced circuit depth, as shown in Eq. (4).

(3) As the result of the competition between \(t_{c}\) and \(t_{r}\), the total runtime \(t_{s}\) of the conventional approach is shorter when \(m\) (the number of input data) is low. But as \(m\) increases, our improved approach will eventually become faster. In Exp.1, the turning point occurs at around \(m=15\) when both approaches take about 8 seconds to complete. After that, \(t_{s}\) of our approach increases approximately in a linear way, while \(t_{s}\) of the conventional one rises dramatically, mostly due to the long compiling time \(t_{c}\). In both Exp.2 and Exp.3, the same behavior can be observed. The turning points occur relatively later though. This is because the real quantum hardware used in Exp.2 runs about 10\(\char 670\)30 times slower than the simulator used in Exp.1, and Exp.3 has a more complicated circuit than that of Exp.1 though it is also run on simulator. Consequently, both Exp.2 and Exp.3 have a longer \(t_{r}\) than that of Exp.1. Meanwhile, all the three experiments have similar \(t_{c}\) values because the compiling of the circuits are always done on classical computers. Therefore, the slower the quantum hardware or simulator is, the later the turning point of the total runtime \(t_{s}\) occurs.

In the age of big data, for real applications of neural networks, the number of input data is generally much higher than what was used here. As a result, we can see that our improved approach has the advantage that it can save the total runtime. This advantage is expected to be even more obvious with the advance of real quantum computers, because they will surely run faster in the future than they do today, resulting in a shorter \(t_{r}\) so that the turning point of \(t_{s}\) could occur even earlier.

It is also worth noting that if Qiskit's "append" function were used for stacking the circuits, the "ibmq_quito" real quantum backend only allows 100 circuits to be appended in a single job. Since the VQC in Exp.2 has 6 adjustable parameters, there are totally 13 shifted and unshifted cost functions to be calculated for each single input data. It means that using the "append" function can handle only \(m=[100/13]=7\) input data at the most. But Exp.2 (i.e., Fig.5(b)) shows that both the conventional approach (using the "compose" function) and our improve approach can accomplish the computation for at least \(m=140\) input data in a single job with no problem. Thus, they manage to break the limit on real quantum hardware.

## IV Discussion

In summary, it is proven experimentally that our improved approach manages to increase the number of gradients that can be computed in each single job, breaking the limit that can be reached using Qiskit's "append" function. More importantly, it has a smaller circuit depth and requires less classical registers for storing the measurement results when comparing with the conventional approach using Qiskit's "compose" function. This shortens the time spent for compiling the quantum circuit significantly. Therefore, the total runtime of the program can also be saved, especially when the number of input data is high and the structure of the quantum circuit is complicated.

Though we only demonstrated our improved approach for VQCs where each parameterized quantum gate \(U(\theta)\)

Figure 4: Individual number of shots actually computed for each cost function in our improved approach, corresponding to the experiment shown in the last column of Table 1. The average number of shots is taken to be \(s=500\). The number of input data is \(m=20\). The cost function with index 1 is the unshifted cost function, while the other 12 cost functions are all shifted ones. The dots with the same color represent the number of shots for the same input data point.
satisfies

\[U(\theta+s)=U(\theta)U(s) \tag{7}\]

(i.e., the gates covered by the original parameter-shift rule [5]), it can also be modified to compute the derivatives for any other quantum gate that requires the general parameter-shift rules [6]. For example, suppose that the first parameterized gate \(RY(\theta_{1})\) on the qubit \(q_{1}\) in Fig.3 is replaced by a more general gate \(U^{\prime}(\theta_{1})\) that does not satisfy Eq. (7). To compute \(U^{\prime}(\theta+s)\), all we need is to replace the controlled-RY gate \(CRY(s)\) (controlled by \(q_{b}\) and acting on \(q_{1}\)) in the first block right behind \(RY(\theta_{1})\) in Fig.3 by two controlled-gates \((U^{\prime}(\theta_{1}))^{-1}\) and \(U^{\prime}(\theta_{1}+s)\) in serial, both of which are also controlled by \(q_{b}\) and acting on \(q_{1}\). Here \((U^{\prime}(\theta_{1}))^{-1}\) is the reverse operation of \(U^{\prime}(\theta_{1})\) such that \((U^{\prime}(\theta_{1}))^{-1}U^{\prime}(\theta_{1})=I\). We can see that once the measurement on \(q_{b}\) before them (i.e., the measurement operator in the first block of Fig.3) results in \(\left|1\right\rangle_{q_{b}}\), then these two gates will be activated. Combining with the original \(U^{\prime}(\theta_{1})\) gate, the complete operation on the qubit \(q_{1}\) will be \(U^{\prime}(\theta_{1}+s)(U^{\prime}(\theta_{1}))^{-1}U^{\prime}(\theta_{1})=U ^{\prime}(\theta_{1}+s)\). Thus, the shift of such a general parameterized quantum gate can also be computed with our approach.

Our approach also suggests the following improvement on real quantum computers. From Fig.3 we can see that the purpose of the first two operations in each block (the controlled-RY gate \(CRY(\gamma_{j})\) and the measurement) is to turn the state of \(q_{b}\) into \(\left|1\right\rangle_{q_{b}}\) with a certain probability. If there is a classical random number generator (even if it generates pseudorandom numbers only), it can accomplish the same task while further reducing the circuit depth. Also, the next two quantum controlled gates make use of the states \(\left|0\right\rangle\) and \(\left|1\right\rangle\) of \(q_{a}\) and \(q_{b}\) only, without needing their quantum superpositions. They can be replaced by classical IF gates (i.e., controlled gates that take classical control bits instead of control qubits, while the target registers can be either quantum or classical) and both \(q_{a}\) and \(q_{b}\) can be classical registers too, so that less quantum resource is required. Unfortunately, classical random number generator and the IF gate are currently unavailable on real quantum hardware (the latter is available on the simulator though). Our approach shows that there is the need for adding these operations in the future, so that the performance of real quantum computers for certain tasks can be further improved.

## Methods

**The input data sets**

In our experiments, the 8-dimensional classical input data set is generated randomly. That is, each data point \(x\) contains 8 random numbers \(\{x_{1},...,x_{8}\}\) uniformly distributed over the interval \([0,1)\).

The 784-dimensional classical input is taken as the Modified National Institute of Standards and Technology (MNIST) data set [15]. It is a widely-used resource for machine learning research [16], which contains 70000

Figure 5: Runtimes as a function of the number of input data \(m\). The dashed lines represent the time \(t_{c}\) spent on compiling the circuit, the dotted lines represent the time \(t_{r}\) spent on running the circuit, and the solid lines represent the total runtime \(t_{s}=t_{c}+t_{r}\). All blue lines stand for the conventional approach, and all green lines stand for our improved approach. (a) Exp.1: The VQC used in the experiment in Fig.4, with 8-dimensional classical input data and the _RealAmplitudes_ ansatz containing \(r=1\) repetition and full entanglement, run on quantum simulator. (b) Exp.2: The same VQC as that of Exp.1, run on real quantum hardware. (c) Exp.3: A VQC with 784-dimensional classical input data, with the _RealAmplitudes_ ansatz containing \(r=2\) repetitions and full entanglement, run on quantum simulator. Note that the green solid and dotted lines almost overlap with each other since \(t_{c}\gg t_{r}\) so that \(t_{s}\simeq t_{c}\) for our improved approach in this case.
greyscale \(28\times 28=784\) pixel images of handwritten digits \(0\hbox{${}^{\circ}$}9\).

**The feature map**

Our feature map uses the amplitude encoding method [17], where each \(d\)-dimensional classical input data point \(x=\left\{x_{1},...,x_{d}\right\}\) is encoded as the amplitudes of a \(Q\)-qubit quantum state

\[\left|\Phi_{x}\right\rangle=C_{norm}\sum_{i=1}^{d}x_{i}\left|i\right\rangle. \tag{8}\]

Here \(d=2^{Q}\), \(\left|i\right\rangle\) is the \(i\)th computational basis state, and \(C_{norm}\) is the normalization constant.

**The cost function**

Though the form of the cost function plays an important role on the overall performance of a neural network, it is less relevant to the current work because our goal is to test the runtime of the improved approach only, which depends solely on the structure of the ansatz. Therefore, the cost function of our networks (either using the improved approach or the conventional one) is simply taken as

\[Cost=\frac{1}{m}\sum_{x}\left|a-y(x)\right| \tag{9}\]

where \(m\) is the number of input data, \(a\) is the vector of outputs from the network when \(x\) is input, and \(y(x)\) is the desired output, i.e., the label of the input \(x\), as contained in the MNIST data set.

Like the MNIST data set, we also give each data in our 8-dimensional input dataset a label, so that our program can serve as a classifier if needed. But since our current data are generated randomly, we simply label all data as "2" without actual meaning.

**Workflow of the circuit of our improved approach**

Here we explain in details how the circuit in Fig.3 works.

The role of the first additional control qubit \(q_{a}\) is to record whether any of the additional blocks has been activated. If it is in the state \(\left|\psi\right\rangle_{q_{a}}=\left|1\right\rangle_{q_{a}}\), then it means that no block has ever been activated yet. Or if it is in the state \(\left|\psi\right\rangle_{q_{a}}=\left|0\right\rangle_{q_{a}}\), then it implies that one of the blocks was already activated. The second additional control qubit \(q_{b}\) acts like a quantum dice, which controls the probability for each block to be activated. At the very beginning of the whole VQC, \(q_{a}\) and \(q_{b}\) are initialized in the states \(\left|1\right\rangle_{q_{a}}\) and \(\left|0\right\rangle_{q_{b}}\), respectively.

Next, in the first green block right behind the first RY gate \(RY(\theta_{1})\), the first gate \(CRY(\gamma_{0})\) is a controlled-RY gate with \(q_{a}\) (\(q_{b}\)) serving as the control (target) qubit. It introduces a rotation angle

\[\gamma_{0}=2\arcsin\sqrt{\frac{1}{N-0}} \tag{10}\]

about the \(y\)-axis to \(q_{b}\) only when \(\left|\psi\right\rangle_{q_{a}}=\left|1\right\rangle_{q_{a}}\). Here \(N=2n+1\). Then the state of \(q_{b}\) becomes

\[\left|\psi\right\rangle_{q_{b}} = \cos\frac{\gamma_{0}}{2}\left|0\right\rangle_{q_{b}}+\sin\frac{ \gamma_{0}}{2}\left|1\right\rangle_{q_{b}} \tag{11}\] \[= \sqrt{1-\frac{1}{N-0}}\left|0\right\rangle_{q_{b}}+\sqrt{\frac{1 }{N-0}}\left|1\right\rangle_{q_{b}}.\]

A measurement in the computational basis \(\left\{\left|0\right\rangle,\left|1\right\rangle\right\}\) is then made to \(q_{b}\), and the result is sent to a classical register. With probability \(1/N\) the measurement result will be \(\left|1\right\rangle_{q_{b}}\). In this case, the controlled-RY gate \(CRY(s)\) next to the measurement will be activated, giving a rotation angle \(s=\pi/2\) to \(q_{1}\) (i.e., the qubit on which the gate \(RY(\theta_{1})\) of the original ansatz is applied). Since the RY gates has the property

\[RY(s)RY(\theta_{1})=RY(\theta_{1}+s), \tag{12}\]

we can see that a shift \(s\) is successfully introduced to the parameter \(\theta_{1}\). Meanwhile, with the next CX gate where \(q_{b}\) (\(q_{a}\)) serves as the control (target) qubit, the state of \(q_{a}\) is turned from \(\left|1\right\rangle_{q_{a}}\) into \(\left|0\right\rangle_{q_{a}}\), indicating that the current block was activated. Also, at the end of this block, the state of \(q_{b}\) is reset to \(\left|0\right\rangle_{q_{b}}\) regardless the measurement result. Consequently, all the rest blocks that follows will be bypassed. After executing the entire circuit, the final result will give the cost function \(f\left(\vec{\theta}+s\vec{e_{1}}\right)\).

On the other hand, with probability \(1-(1/N)\) the measurement result on \(q_{b}\) in this block will be \(\left|0\right\rangle_{q_{b}}\), then the two aforementioned controlled gates in the same block following this measurement will not take effect at all, so that the states of \(q_{a}\) and \(q_{1}\) remain unchanged, allowing the second green block to be activated.

Similarly, in the second green block, since the state of \(q_{b}\) was reset to \(\left|0\right\rangle_{q_{b}}\), the first gate \(CRY(\gamma_{1})\) introduces a rotation angle

\[\gamma_{1}=2\arcsin\sqrt{\frac{1}{N-1}} \tag{13}\]

to \(q_{b}\) when \(\left|\psi\right\rangle_{q_{a}}=\left|1\right\rangle_{q_{a}}\), turning the state of \(q_{b}\) into

\[\left|\psi\right\rangle_{q_{b}} = \cos\frac{\gamma_{1}}{2}\left|0\right\rangle_{q_{b}}+\sin\frac{ \gamma_{1}}{2}\left|1\right\rangle_{q_{b}} \tag{14}\] \[= \sqrt{1-\frac{1}{N-1}\left|0\right\rangle_{q_{b}}}+\sqrt{\frac{1 }{N-1}}\left|1\right\rangle_{q_{b}}.\]

Then \(q_{b}\) is also measured in the computational basis, and the result is stored in another classical register. With probability \(1/(N-1)\) the result will be \(\left|1\right\rangle_{q_{b}}\). Note that this case occurs only when the first block was not activated, i.e., the measurement result of \(q_{b}\) in the first block was \(\left|0\right\rangle_{q_{b}}\), which occurred with probability \(1-(1/N)\). Thus, the total probability for finding \(q_{b}\) in \(\left|1\right\rangle_{q_{b}}\) at this stage (i.e., the second block is activated) is

\[prob_{2}=\left(1-\frac{1}{N}\right)\frac{1}{N-1}=\frac{1}{N}, \tag{15}\]
which equals to the probability for activating the first block. When this result indeed occurs, the next gate \(CRY(-s)\) will introduce the rotation angle \(-s=-\pi/2\) to \(q_{1}\), and the CX gate next to it will turn the state of \(q_{a}\) into \(\left|0\right\rangle_{q_{a}}\), making the rest blocks bypassed. Then the final result of the entire circuit will be the cost function \(f\left(\vec{\theta}-s\vec{e_{1}}\right)\).

For the same reasons, by setting the rotation angle of the first controlled-RY gate \(CRY(\gamma_{j})\) in the \((j+1)\)th additional block as

\[\gamma_{j}=2\arcsin\sqrt{\frac{1}{N-j}} \tag{16}\]

(\(j=0,...,N-2\)), it can be proven that every block stands the same probability \(1/N\) to be activated. The CX gates on \(q_{b}\) and \(q_{a}\) also ensure that only one block at the most will be activated. As a result, executing the whole circuit for one shot will output the shifted cost function \(f\left(\vec{\theta}+s\vec{e_{j}}\right)\) or \(f\left(\vec{\theta}-s\vec{e_{j}}\right)\) for a single \(j\) only. When none of the block was activated (which also occurs with probability \(1/N\)), the circuit outputs the unshifted cost function \(f\left(\vec{\theta}\right)\). By reading the classical registers \(c\) that store the measurement results of \(q_{b}\) in every block to see which block was activated, we can learn which cost function was calculated. Running the circuit for many shots will then provide enough shots for each of these cost functions, so that all gradients in Eq. (1) can be obtained with the desired precision.

**Proving that \(\lambda\) in Eq. (2) is non-trivial**

In the _RealAmplitudes_ ansatz, let \(Q\) denote the total number of the qubits. When \(Q\) is fixed, increasing \(n\) means increasing the number of repetition \(r\) in this ansatz. This is because the ansatz starts with a parameterized RY gate for each of the \(Q\) qubits, then each repetition adds another parameterized RY gate to each qubit. Thus, the total number of the parameters satisfies

\[n=Q(r+1). \tag{17}\]

Suppose that the circuit depth of each additional repetition (including the RY gates and the CX gates in the entanglement section) is \(\Delta\). Then

\[\Lambda=\Lambda_{0}+r\Delta=\Lambda_{0}+(\frac{n}{Q}-1)\Delta=(\Lambda_{0}- \Delta)+n\frac{\Delta}{Q}, \tag{18}\]

where \(\Lambda_{0}\) denotes the depth of the other parts of the circuit which is not included in \(\Delta\), e.g., the feature map, the observable, the final measurement, and the very first RY gate on each qubit within the ansatz. In this case, we yield

\[\lambda=\frac{(\Lambda_{0}-\Delta)+n\frac{\Delta}{Q}}{n}=\frac{(\Lambda_{0}- \Delta)}{n}+\frac{\Delta}{Q}. \tag{19}\]

A neural network that is useful in practice generally has a large number of parameters. When \(n\rightarrow\infty\) we have \(\lim_{n\rightarrow\infty}\lambda=\Delta/Q\). Thus it is proven that \(\lambda\) stays non-trivial.

**Program details**

The programs for 8-dimensional input data using quantum simulator were run on a personal laptop with a 2.8 GHz quad-core Intel i7-3840QM processor and 16 GB \(1,600\) MHz DDR3 memory.

The programs for 784-dimensional input data using quantum simulator were run on a Tianhe-2 supercomputer node with a 2.2 GHz Intel Xeon E5-2692 v2 processor and 64 GB memory.

In these simulations, Qiskit's "qasm_simulator" backend was used as the quantum simulator.

All programs using real hardware were run on the "ibmq_quito" 5-qubit backend of IBM Quantum Experience online platform.

When running on quantum simulator, both \(t_{c}\) (the time spent on compiling the quantum circuit) and \(t_{r}\) (the time spent on running the circuit) are recorded automatically by the programs. But when running on real quantum hardware, \(t_{r}\) is obtained from the job output files downloaded from IBM Quantum Experience platform instead. This is because the value of \(t_{r}\) recorded by the programs in this case also includes the time spent on waiting in the queue for the job to be run, which is not the correct measure for the performance of the approaches being used.

In both approaches, the circuits for different input data are stacked together using Qiskit's "compose" function.

The initial values of all adjustable parameters of the VQCs were generated randomly (uniformly distributed over the interval \([0,\pi)\)) and stored beforehand. Then we always loaded these same values into every computer programs in every run.

**Data availability**

The raw experimental data of this study are publicly available at [https://github.com/gphehub/grad2210](https://github.com/gphehub/grad2210).

**Code availability**

The software codes for generating the experimental data are publicly available at [https://github.com/gphehub/grad2210](https://github.com/gphehub/grad2210).
