Quantum Transformer: Accelerating model inference via quantum linear algebra

Naixu Guo, Zhan Yu, Matthew Choi, Yizhan Han, Aman Agrawal, Kouhei Nakaji, Alán Aspuru-Guzik, Patrick Rebentrost

TL;DR

This paper introduces a quantum algorithm for Transformer inference that achieves a polynomial speedup, particularly in the input sequence length $N$. By developing novel quantum subroutines for element-wise matrix functions and utilizing the block-encoding framework, the authors demonstrate that fault-tolerant quantum computers could significantly reduce the computational overhead of large language models (LLMs).

What Problem Does This Paper Address?

Large Language Models (LLMs) like GPT-4 and Llama have revolutionized AI, but their inference costs—in terms of both time and energy—are astronomical. The core of these models is the Transformer architecture, which relies heavily on self-attention mechanisms.

Classically, the computational complexity of self-attention scales quadratically with the input sequence length ($N$) and the embedding dimension ($d$). As we move toward processing longer documents and more complex reasoning tasks, this $O(N^2d)$ scaling becomes a major bottleneck. The authors aim to leverage quantum linear algebra to break this classical scaling and provide a more efficient path for model inference.

Methods

The authors work within the framework of fault-tolerant quantum computing, specifically utilizing block encodings. This technique represents a non-unitary matrix (like a weight matrix or an input sequence) as a sub-block of a larger unitary matrix that can be executed on a quantum computer.

Key technical contributions include:

  • Quantum Subroutines for Transformer Blocks: The authors developed specific quantum circuits for self-attention, residual connections, layer normalization (LN), and feed-forward networks (FFN).
  • Element-wise Matrix Functions: A major hurdle in quantum linear algebra is that standard techniques like Quantum Singular Value Transformation (QSVT) apply functions to the singular values of a matrix, not its individual elements. The authors developed a new method to implement element-wise functions (like softmax and GELU activations) by combining Hadamard products with linear combinations of unitaries (LCU).
  • Amplitude Encoding: The final output is prepared as a quantum state where the amplitudes correspond to the classical output vector, which can then be measured or passed to subsequent layers.
FIG. 1. Overview of the quantum transformer architecture. Same as the original decoder-only transformer architecture, the quantum transformer consists of a self-attention and a feed-forward network sub-layer, incorporating residual connection with layer normalization. The inputs of the quantum transformer are block encodings of the input sequence and pre-trained weight matrices, from which the relevant matrices for the transformer are constructed (query Q, key K, and value V ). Given the input block encodings, we construct the corresponding quantum subroutines and combine them to our final result on obtaining the classical output vector corresponding to the j-th token. multilayer architecture can be achieved by iterating the procedure for each token j ∈[N] and producing a new block encoding of input sequence for the next layer.

Datasets

The authors used several datasets and open-source models to validate their theoretical assumptions about matrix norms:

  • MMLU (Massive Multitask Language Understanding): Used to analyze how the norms of input sequence matrices ($S$) scale in real-world applications.
  • Genomic Benchmarks (GB): Specifically the promoter detection task, used to evaluate "quantum-friendly" (normalized) transformer models.
  • Open-source LLMs: Norm analysis was performed on a wide range of models including BERT, RoBERTa, GPT, GPT2, TinyLlama, Llama2-7B, and Mistral-7B.

Key Insights and Ideas

  • Norm-Dominant Complexity: The complexity of the quantum algorithm is primarily determined by the matrix norm of the input sequence. Numerical experiments on real LLMs showed that this norm scales as $O(\sqrt{N})$, which is much better than the worst-case mathematical scaling.
  • "Quantum-Friendly" Training: The authors found that training Transformers with sub-normalized weight matrices (spectral or Frobenius normalization) does not significantly degrade performance on tasks like genomic sequence classification. This is crucial because normalized weights lead to faster quantum inference.
  • Dimension Independence: A surprising "aha" moment is that row-wise arithmetic (like softmax and nonlinear activations) can be achieved with a query complexity that is independent of the embedding dimension $d$.
  • Robustness to Dequantization: The paper rigorously shows a polynomial separation between their quantum algorithm and classical randomized "dequantized" algorithms, suggesting the quantum advantage is robust.

Key Results

  • Quantum Speedup: For a single-layer transformer, the quantum algorithm achieves a complexity of $\tilde{O}(\sqrt{N} d \log^2(1/\epsilon))$. Compared to the classical $O(Nd^2)$ for single-token prediction, this represents a nearly quadratic speedup in the sequence length $N$.
  • Multilayer Scaling: For a $k$-layer transformer, the complexity is $\tilde{O}(kN^{3/2}d)$. In practical scenarios where $N \gg d$, this remains significantly faster than classical inference.
  • Empirical Validation: Figure 3 demonstrates that across various popular LLMs, the spectral and Frobenius norms of the input sequence scale sublinearly, supporting the $O(\sqrt{N})$ assumption required for the quantum speedup.
Scaling of matrix norms with sequence length N across different LLMs, showing sublinear growth.

Limitations and Future Work

  • Hardware Requirements: The proposed algorithms require fault-tolerant quantum computers with a large number of qubits and high-fidelity gates, which are not yet available.
  • Multilayer Coherence: While the paper provides a path for multilayer transformers, the current approach uses an "incoherent" method (measuring and reloading data between layers). A fully coherent multilayer implementation might offer even better scaling but currently faces exponential complexity challenges.
  • Quantum Training: This work focuses on inference. The authors suggest that "Parameterized Block Encodings" (PBEs) could be used for training, but this remains an open area for future research, particularly regarding issues like barren plateaus.
  • QRAM Assumption: The most significant speedups assume the existence of Quantum Random Access Memory (QRAM), though the authors discuss some regimes where an advantage exists even without it.

Summary updated

BibTeX
@misc{guo2024quantum,
  author = {Naixu Guo and Zhan Yu and Matthew Choi and Yizhan Han and Aman Agrawal and Kouhei Nakaji and Alán Aspuru-Guzik and Patrick Rebentrost},
  title = {{Quantum Transformer: Accelerating model inference via quantum linear algebra}},
  year = {2024},
  eprint = {2402.16714},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2402.16714},
}
Figure