Non unitary and nonlinear quantum generative large model design and fast large-scale training without back propagation
Authors: Tianze Zhu, Xiaofeng Jiang, Min Liao, Luying Zhang, Ruixi Liu, Sen Kuang, Yongzhe Lin, Chenhao Zhou, Shuangwu Chen, Huasen He, Xiaobin Tan, Quan Zheng, Jian Yang
Organizations: Department of Automation, University of Science and Technology of China, Hefei, 230027, Anhui, China. · Institute of Artificial Intelligence, Hefei Comprehensive National Science Center, Hefei, 230026, Anhui, China. · Institute of Advanced Technology, University of Science and Technology of China, Hefei, 230027, Anhui, China.
Transformer-based large models require huge computational and storage costs. Quantum computation is expected to address that due to its potential to speed up classical algorithms exponentially. Key challenges of implementing quantum large models include the linear, unitary nature of qubit evolutions contrast with the non-unitary matrices and nonlinear functions in classical Transformers, as well as the difficulty of implementing gradient back-propagation on quantum hardware. Here we propose a fully quantum large model containing all the Transformer layers, implementing the non-unitary transformations through SVD-decomposition-based quantum circuits, and approximated nonlinear activation functions through pre-trained parameterized quantum circuits. We also propose a quantum circuit training algorithm based on multi-agent Reinforcement Learning, which can train large-scale parameterized quantum circuits without back-propagation. The dependency between the inference time complexity and the model's hidden dimension has been reduced from polynomial to logarithmic, showing an exponential advantage. The inference executions on the quantum hardware verify our model's 300-1000 times speed-up, and the measurements on the sequence-to-sequence tasks shows its learning ability. The training algorithm achieves only 25% complexity of the best SOTA quantum circuit training algorithms, especially this ratio decreases fast as the parameter scale increases. Our work not only proposes a quantum large model that can significantly reduce the computational cost, but also promotes the quantum computation towards general computing.
Figures & tables
Figure 1: Overview of the quantum Transformer architecture. (a) The encoding layer. A classical tokenizer and trainable embedding table generate token vectors. Amplitude embedding encodes the vectors onto the qubits, and positional encoding supply the positional information. (b) The self-attention layer. 3m−2 simultaneous attention projection circuits, grouped into GQ⊗m−1 , GK⊗m−1 and GV⊗m , processes the qubits, followed by quantum mixing circuits mixing the quantum states of the qubits depending on Q-K fidelity, as well as the Softmax approximation circuits. (c) The feed-forward layer. It uses a quantum circuit structure of SVD decomposition as its linear building blocks. The full layer contains two such circuits with a Sigmoid approximation circuits.
Layer
Theoretical calculation number
Measured execution time
classical
quantum
classical
quantum
Self-Attention
206208
315
0.321
0.452
FFN
385536
1044
0.183
0.303
Table 1: The time complexity and time consuming of the implemented quantum large model on the Tianyan-287 quantum chip, with the scale of 4 qubits per token (corresponding to hidden dimension d=16 ) and sequence length m=3 , compared with the classical Transformer with the same scale.
Figure 2: Comparisons of the performance of MARL-QCT, SPSA and parameter-shift on quantum circuit parameter tuning . The experiment results show that MARL-QCT is able to reach a higher fidelity with sufficiently lower time consuming, establishing a distinct advantage compared to the SOTA algorithm. (a) Results for the SU(16) circuit task, tuning 255 parameters. Starting from the rough tuned parameters with fidelity 0.806949, MARL-QCT reaches the fidelity 0.953760 in 24000 iterations, while SPSA and parameter-shift reach 0.867119 and 0.899941. MARL-QCT thus gives a 143.99% and 57.87% more fidelity improvement. Meanwhile, MARL-QCT takes only 33.71% circuit calls of the parameter-shift to reach the target fidelity 0.95, while SPSA cannot reach the target fidelity because of the gradient estimation noise. (b) Results for the GQ circuit task, tuning 1020 parameters. Starting from the rough tuned parameters with fidelity 0.800053, MARL-QCT can reach the fidelity 0.957973 in 36000 iterations, while SPSA and parameter-shift reach 0.910945 and 0.917277. MARL-QCT thus gives a 42.41% and 34.72% more fidelity improvement. Meanwhile, MARL-QCT takes only 22.78% circuit callings of the parameter-shift to reach the target fidelity 0.95, while SPSA still cannot reach the target fidelity.
Figure 3: Token accuracy for Dyck, SCAN-length and PCFG task . Figure a, b and c show Dyck-4, SCAN-length and PCFG, respectively, with training on the left and validation on the right. The validation token accuracy for the 3 tasks reaches 96.69%, 67.56% and 25.76% respectively. The results shows that our quantum large model under the tested scale can learn the Dyck task effectively, sometimes produce error when encountering complex input sequence analysis in SCAN-length task, while the accuracy is limited by the small model scale in PCFG.
Figure 4: The structure of the quantum encoding layer. A classical tokenizer convert the input natural sequence into token sequence, followed by the quantum embedding lookup consist of a trainable lookup table and the quantum amplitude encoding circuits GAE . GAE encodes the corresponding rows in the lookup table into the quantum state of the qubits. Finally, positional encoding is applied.
Figure 5: The structure of the positional encoding circuit GP1 to GPm . They consist of X-axis rotation gates applied to each qubit. For the qubits corresponding to the k -th group of characters, the rotation angles are k times the angles used in the positional encoding circuit for the first group of characters.
Figure 6: Diagram of the structure of the self-attention layer. The 3m−2 copies of the encoded qubits ∣ψE1⟩ to ∣ψEm⟩ pass through the attention-head-circuits GQ , GK and GV to acquire the query states, the key states and the value states relatively, with query These states pass through the mixing circuits consist of SwapTest circuits [ 33 , 34 ] and Strong Entangling circuits [ 35 ] to mix the value states according to the inner product of the corresponding query states and key states. Finally, measurements are performed on the SwapTest auxiliary qubits to eliminate the entanglement between the auxiliary qubits and value states and reset the auxiliary qubits to ∣0⟩ to let the following operations reuse them. The mixed value states finally pass the Softmax circuits GNL_sm to form the output states of the attention layer ∣ψA1⟩ to ∣ψAm⟩ .
Figure 7: Detail of preparing the states ∣ψQj⟩ , ∣ψKj⟩ and ∣ψVj⟩ , 1≤j≤m , showing the general structure of the quantum circuits GQi , GKi and GVi , 1≤i≤128 . The structure of UQ , UK , UV , VQ† , VK† and VV† is shown in Fig. 9 and 18 while the structure of DQ , DK and DV is shown in Fig. 8 .
Figure 8: The quantum circuit implementing the non-unitary diagonal operation with matrix form D=diag(d1,d2,…,d2q) on q data qubits. The Hadamard gate on the ancillary qubit turns the input state ∣ψ⟩⊗∣0⟩ into ∣ψ⟩⊗(∣0⟩+∣1⟩)/2 . The controlled phase operations and the second Hadamard gate produce cos(θ)∣ψ⟩⊗∣0⟩+isin(θ)∣ψ⟩⊗∣1⟩ . The phase gate Panc removes the factor i , giving cos(θ)∣ψ⟩⊗∣0⟩+sin(θ)∣ψ⟩⊗∣1⟩ . The operation succeeds when measurement of the ancillary qubit gives 0.
Figure 9: The quantum circuit diagram of the unitary part in the circuit GQi , GKi , GVi , 1≤i≤128 . The circuit follows the pattern of SU(16) circuit layers and bridge layers, marked by the blue box and the red box on the diagram respectively. The SU(16) circuits in a single layer are arranged to operate in parallel to accommodate more trainable parameters in lower circuit depth, and bridge layers locate at the middle of the SU(16) circuit layers to isolate the SU(16) circuits, thus avoiding any parameter redundancy.
Figure 10: The structure of the SU(16) circuit proposed by David Wierichs, et. al [ 31 ] . The circuit has the lowest depth and fewest number of CNOT gates known currently.
n
SU(16) layer number
Ksamp
Pnom
Peff
4
1
(checked directly by Eq. 20 )
255
255
8
2
16
773
773
8
5
32
2072
2072
8
9
32
3634
3634
8
13
64
5196
5196
10
2
8
1030
1030
Table 2: The results of the effective parameter number checks using Eq. 24 . Here n , Ksamp , Pnom , and Peff denote the qubit number of the circuit, the number of randomly sampled input states, the nominal parameter number, and the lower bound of the effective parameter number, respectively. Ksamp is chosen to ensure the condition Ksamp2n+1≥P . All the lower bounds of the effective parameter number reach the nominal parameter number, showing that the bridge layers are completely effective, and no parameter redundancy exists in the unitary part of GQi , GKi , GVi , 1≤i≤128 and GWi , 1≤i≤2 .
Figure 11: Quantum circuit implementing the controlled non-unitary diagonal operation C7(i)D(i) . The seven control qubits select the diagonal transformation, while the ancillary qubits support the probabilistic diagonal operation and amplitude adjustment.
Figure 12: The quantum circuit diagram of the multi-head attention circuit GQ . It contains 128 attention heads, realized by the 7-qubit sub-circuits GQ1 to GQ128 . The controlled sub-circuit sequence implements the block-diagonal transformation of Eq. 7 . Each of the sub-circuit is constructed by the SVD quantum circuit structure shown in Fig. 7 so that the attention head matrices can be arbitrary, overcoming the unitary constraint of the quantum state evolution.
Figure 13: Detail of the quantum mixing circuits in the self-attention layer. They take the outputs of the attention-head circuits as input and pass them through a series of ordered SwapTest and entangling circuits. (a) General structure of the quantum mixing circuit. For token position j , the m−1 copies of the query state ∣ψQj⟩ pass through SwapTest circuits with the key states ∣ψKℓ⟩ , ℓ=j . The fidelity between the query states and key states are encoded in the density operators of the auxiliary qubits. Copies of the value states ∣ψVℓ⟩ , 1≤ℓ≤m , are then mixed by the entangling circuits under the control of these auxiliary qubits. Finally, the SwapTest auxiliary qubits are measured and reset. (b) Detailed arrangement of the SwapTest and entangling circuits in (a).
Figure 14: The quantum structure of the Strongly Entangling circuit from the paper [ 35 ] . It consists single-bit general rotations gates on all the qubits at the beginning, followed by the CNOT gates perform on the adjacent qubits on sequence.
Figure 16: The training of a 4-qubit Softmax Approximation Circuit. Parameter-shift is used to acquire the parameter gradients, and Adam optimizer is used to update the circuit parameters. After 1600 parameter updatings, the fidelity of the circuit can reach 0.984, effectively approximates the Softmax function.
Figure 17: The diagram of the structure of the FFN layer. The output qubits ∣ψA1⟩,…,∣ψAm⟩ from the self-attention layer go into a GW1 - Sigmoid - GW2 - Sigmoid circuit to form the output of the FFN layer. The GW1 and GW2 are the quantum circuits that perform the equivalent transformation of a single classical neural network layer on the quantum states of the qubits. They are constructed by the SVD decomposition, illustrated in the lower diagram. The Sigmoid circuits are pre-trained quantum circuits that approximate the Sigmoid function transformation.
Figure 18: The quantum circuit diagram of the unitary part in the circuit GWi , 1≤i≤2 . The circuit follows the pattern of SU(16) circuit layers and bridge layers, marked by the blue box and the red box on the diagram respectively. The SU(16) circuits in a single layer are arranged to operate in parallel to accommodate more trainable parameters in lower circuit depth, and bridge layers locate at the middle of the SU(16) circuit layers to isolate the SU(16) circuits, thus avoiding any parameter redundancy.
Figure 19: The training of a 4-qubit Sigmoid Approximation Circuit. Parameter-shift is used to acquire the parameter gradients, and Adam optimizer is used to update the circuit parameters. After 1600 parameter updatings, the fidelity of the circuit can reach 0.983, effectively approximates the Sigmoid function.
Layer
Circuit depth
Time complexity
Time complexity of classical Transformer
Encoding
Subject to the quantum hardware
O(m+(log2d)2) in worst case
O(md)
Self-Attention
Eq. 60
O((m+p)log2d−plog2p)
O(m2d)
FFN
Eq. 61
O(log2d)
O(md2)
Table 3: The time complexity of the encoding layer, the self-attention layer and the FFN layer of our quantum large model, compared with the classical Transformer. Our quantum large model achieves an exponential acceleration in the self-attention layer and the FFN layer while keeping the relatively same time consuming magnitude with the classical ones.
Layer
Theoretical calculation number
Measured execution time
Chip usage time (s)
classical
quantum
classical
quantum
Self-Attention
206208
315
0.321
0.452
202.706
FFN
385536
1044
0.183
0.303
83.482
Table 4: The time complexity and time consuming of the implemented quantum large model on the Tianyan-287 quantum chip, with the scale of d=16 , m=3 , compared with the classical Transformer with the same scale. For the chip usage time, Each task contains 10 shots of circuit executions, and we repeat all the experiments 3 times. The table shows the average.
Figure 20: Comparisons of the performance of MARL-QCT, SPSA and parameter-shift on quantum circuit parameter tuning . The experiment results show that MARL-QCT is able to reach a higher fidelity with sufficiently lower time consuming, establishing a distinct advantage compared to the SOTA algorithm. (a) Results for the SU(16) circuit task, tuning 255 parameters. Starting from the rough tuned parameters with fidelity 0.806949, MARL-QCT reaches the fidelity 0.953760 in 24000 iterations, while SPSA and parameter-shift reach 0.867119 and 0.899941. MARL-QCT thus gives a 143.99% and 57.87% more fidelity improvement. Meanwhile, MARL-QCT takes only 33.71% circuit calls of the parameter-shift to reach the target fidelity 0.95, while SPSA cannot reach the target fidelity because of the gradient estimation noise. (b) Results for the GQ circuit task, tuning 1020 parameters. Starting from the rough tuned parameters with fidelity 0.800053, MARL-QCT can reach the fidelity 0.957973 in 36000 iterations, while SPSA and parameter-shift reach 0.910945 and 0.917277. MARL-QCT thus gives a 42.41% and 34.72% more fidelity improvement. Meanwhile, MARL-QCT takes only 22.78% circuit callings of the parameter-shift to reach the target fidelity 0.95, while SPSA still cannot reach the target fidelity.
Figure 21: Teacher-forced token accuracy for Dyck, SCAN-length and PCFG task. Figure a, b and c show Dyck-4, SCAN-length and PCFG, respectively, with training on the left and validation on the right. The validation token accuracy for the 3 tasks reaches 96.69%, 67.56% and 25.76% respectively. The results shows that our quantum large model under the tested scale can learn the Dyck task effectively, sometimes produce error when encountering complex input sequence analysis in SCAN-length task, while not able to learn the PCFG task due to a small model scale.
Multiverse Computing, Parque Científico y Tecnológico de Gipuzkoa, Paseo May 7 de Miramón 170, 20014, Donostia / San Sebastián, Spain · Department of Basic Sciences, Tecnun – University of Navarra, San Sebastián, Spain · Multiverse Computing, Centre for Social Innovation, 192 Spadina Avenue, ON M5T 2C2, Toronto, Canada +2