Abstract
We introduce a simple algorithm that efficiently computes tensor products of Pauli matrices. This is done by tailoring the calculations to this specific case, which allows to avoid unnecessary calculations. The strength of this strategy is benchmarked against state-of-the-art techniques, showing a remarkable acceleration. As a side product, we provide an optimized method for one key calculus in quantum simulations: the Pauli basis decomposition of Hamiltonians.
Similar content being viewed by others
Avoid common mistakes on your manuscript.
1 Introduction
Pauli matrices [1] are one of the most important and well-known set of matrices within the field of quantum physics. They are particularly important in both physics and chemistry when used to describe Hamiltonians of many-body spin glasses [2,3,4,5,6,7] or for quantum simulations [8,9,10,11,12,13]. The vast majority of these systems are out of analytic control so that they are usually simulated through exact diagonalization which requires their Hamiltonians to be written in its matrix form. While this task may be regarded as a trivial matter in a mathematical sense, it involves the calculation of an exponentially growing number of operations. Furthermore, description of quantum systems via Matrix Product States (MPS) [14], Density Matrix Renormalization Group (DMRG) [15] and Projected Entangled Pair States (PEPS) [16] also involves large-scale Hamiltonians, as well as Lanczos method [17], whose formulation has been efficiently encoded on quantum hardware recently [18].
In this work, we present the PauliComposer (PC) algorithm which significantly expedites this calculation. It exploits the fact that any Pauli word only has one element different from zero per row and column, so a number of calculations can be avoided. Additionally, each matrix entry can be computed without performing any multiplications. Even though the exponential scaling of the Hilbert space cannot be avoided, PC can boost inner calculations where several tensor products involving Pauli matrices appear. In particular, those that appear while building Hamiltonians as weighted sums of Pauli strings or decomposing an operator in the Pauli basis.
The PC algorithm could be implemented in computational frameworks in which this sort of operations is crucial, such as the Python modules Qiskit [19], PennyLane [20], OpenFermion [21] and Cirq [22]. It can also potentially be used in many other applications, such as the Pauli basis decomposition of the Fock space [23] and conventional computation of Ising model Hamiltonians to solve optimization problems [24,25,26,27], among others.
The rest of the article is organized as follows: in Sect. 2 we describe the algorithm formulation in depth, showing a pseudocode-written routine for its computation. In Sect. 3, a set of tests is performed to show that a remarkable speed-up can be achieved when compared to state-of-the-art techniques. In Sect. 4, we show how this PC algorithm can be used to solve relevant problems. Finally, the conclusions drawn from the presented results are given in Sect. 5. We provide proofs for several statements and details of the algorithm in the appendices.
2 Algorithm formulation
In this section, we discuss the PC algorithm formulation in detail. Pauli matrices are hermitian, involutory and unitary matrices that together with the identity form the set \(\sigma _{\{0, 1, 2, 3\}} = {\{I,X, Y, Z\}}\). Given an input string \(x = x_{n-1}\dots x_0\in \{0,1,2,3\}^n\), the PC algorithm constructs
Let us denote its matrix elements as \(P_{j,k}(x)\) with \(j, k=0,\dots ,2^n-1\). It is important to remark that for each row j, there will be a single column k(j) such that \(P_{j, k(j)} \ne 0\) (see Appendix 1). The solution amounts to a map from the initial Pauli string to the positions and values of the \(2^n\) nonzero elements. This calculation will be done sequentially; hence, the complexity of the algorithm will be bounded from below by this number.
As a first step, it is worth noting that Pauli string matrices are either real (all elements are \(\pm 1\)) or purely imaginary (all are \(\pm i\)). This depends on \(n_Y\), the number of Y operators in P(x). We can redefine \(\tilde{Y}:=iY\), so that \(\smash [t]{\tilde{\sigma }_{\{0,1,2,3\}}=\{I, X, \tilde{Y}, Z\}}\) and \(\smash [t]{\tilde{P}(x):=\tilde{\sigma }_{x_{n-1}}\otimes \dots \otimes \tilde{\sigma }_{x_0}}\). As a result, every entry in \(\smash [t]{\tilde{P}(x)}\) will be \(\pm 1\). This implies that there is no need to compute any multiplication: the problem reduces to locating the nonzero entries in \(\smash [t]{\tilde{P}(x)}\) and tracking sign changes. The original P(x) can be recovered as \(\smash [t]{P(x)=(-i)^{n_Y\text { mod }4}\tilde{P}(x)}\).
We will now present an iterative procedure to compute \(\tilde{P}\) by finding for each row j the nonzero column number k(j) and its corresponding value \(\smash [t]{\tilde{P}_{j,k(j)}}\). For the first row, \(j=0\), the nonzero element \(\smash [t]{\tilde{P}_{0,k(0)}}\), can be found at
where \([\,\cdot \,]_{10}\) is the decimal representation of a bit string and \(y(x_i)\) tracks the diagonality of \(\sigma _{x_i}\), where \(y(x_i)\) is equal to 0 if \(x_i=\{0,3\}\) (thus \(\sigma _{x_i}\in \{I,Z\}\)) and 1 otherwise (thus \(\sigma _{x_i}\in \{X,Y\}\)). The value of this entry is
The following entries can be computed iteratively. At the end of stage l, with \(l=0, \cdots , n-1\), all nonzero elements in the first \(2^{l+1}\) rows of \(\smash [t]{P_{j,k(j)}}\) will have been computed using the information given by the substring \(x_l \dots x_0\). At the next step, \(l+1\), the following \(2^l\) rows are filled using the ones that had already been computed, where the row–column relation k(j) is given by
The second term of the RHS of this relation takes into account the way that the blocks of zeros returned at stage l affect the new relative location of the nonzero blocks within the new \(2^{l+1}\times 2^{l+1}\) subcomposition. Its corresponding values are obtained from the previous ones, up to a possible change of sign given by
with \(\epsilon _l\) equal to 1 if \(x_l\in \{0,1\}\) and \(-1\) otherwise. This \(\epsilon _l\) is nothing but a parameter that takes into account if \(\sigma _{x_l}\) introduces a sign flip. In Algorithm 1 a pseudocode that summarizes the presented algorithm using (2)-(5), is shown.
For the particular case of diagonal Pauli strings (only I and Z matrices), there is no need to compute the row–column relation k(j), just the sign assignment is enough. Even if this is also the case for anti-diagonal matrices, we focus on the diagonal case due to its relevance in combinatorial problems [24,25,26,27]. See Algorithm 2 for the pseudocode of this case (PDC stands for PauliDiagonalComposer).
The PC algorithm is able to circumvent the calculation of a significant amount of operations. When generic Kronecker product routines (see Appendix 2) are used for the same task, the amount of multiplications needed for computing a Pauli string is \(\mathcal {O}[n2^{2n}]\) and \(\mathcal {O}[n2^n]\) for dense and sparse matrices, respectively. In contrast, the PC algorithm, considering the worst-case scenarios, needs
-
\(\{I, Z\}^{\otimes n}\): \(\mathcal {O}[2^n]\) changes of sign.
-
Otherwise: \(\mathcal {O}[2^n]\) sums and \(\mathcal {O}[2^n]\) changes of sign.
In all cases, our algorithm can significantly outperform those that are not specifically designed for Pauli matrices.
On top of that, this method is also advantageous for computing weighted Pauli strings. Following (3), \(W:=\omega P\), with arbitrary \(\omega \), can be computed by defining \(W_{0, k(0)}= \omega (-i)^{n_Y\text { mod }4}\) which avoids having to do any extra multiplication. This change is reflected in Algorithm 1 by changing line 6 to \(m(0)\leftarrow \omega (-i)^{n_Y\text { mod }4}\) and line 4 to \(m(0)\leftarrow \omega \) in Algorithm 2. This is specially important as it can be used to compute Hamiltonians written as a weighted sum of Pauli strings, where \(H=\sum _x \omega _xP(x)\).
3 Benchmarking
In this section, we analyze the improvement that the PC strategy introduces against other known algorithms labeled as Naive (regular Kronecker product), Algorithm 993 (Alg993) [28], Mixed and Tree [29, 30]. Further details can be found in Appendix 2. We benchmark these algorithms using MATLAB [31] as it is proficient at operating with matrices (it incorporates optimized routines of the well-known BLAS and LAPACK libraries [32, 33]). The PC avoids matrix operations, and thus, it would not be ideal to implement it using MATLAB. Instead, we use Python [34] since many quantum computing libraries are written in this language [19,20,21,22]. See Table 1 for a full description of the computational resources used.
Concerning memory needs, with this algorithm only \(2^n\) nonzero elements out of \(2^{2n}\) are stored. This is exactly the same as using sparse matrices, thus, no major improvement is to be expected. As for the computational time, we compare how different algorithms behave as the length n of the Pauli string increases. In Fig. 1 execution times for general and diagonal Pauli strings are shown. For the PC methods, we use the PC routine (Algorithm 1) for the general case and the PDC routine (Algorithm 2) for the diagonal one. In accordance to our theoretical analysis, the PC algorithm proves to be the best performing routine.
On a more technical note, when using the PC routine, matrices with complex values (\(n_Y\) odd) take twice as much time as real-valued ones (\(n_Y\) even). Consequently, we compute their execution times separately and then average them. Moreover, it is convenient to choose when to use PC or PDC as the latter can be up to 10 times faster.
4 Real use cases of the PauliComposer algorithm
The PC algorithm can be used to perform useful calculations in physics. In this section, the Pauli basis decomposition of a Hamiltonian and the construction of a Hamiltonian as a sum of weighted Pauli strings are discussed in detail. Another worth mentioning scenario is the digital implementation of the complex exponential of a Pauli string, i.e., \(e^{-i \theta P(x)}=\cos (\theta )I -i \sin (\theta )P(x)\).
Pauli basis decomposition of a Hamiltonian.—The decomposition of a Hamiltonian written as a \(2^n\times 2^n\) matrix into the Pauli basis is a common problem in quantum computing. Given a general Hamiltonian H, this decomposition can be written as \(H=\sum _x \omega _x P(x)\) with \(x = x_{n-1}\dots x_0\) and P(x) as in (1). The coefficients \(\omega _x\) are obtained from the orthogonal projection as
Following the discussion in Sect. 2, the double sum collapses to a single one in (6) since there is only one nonzero element per row and column. Each of these weights can be computed independently, which allows for a parallel implementation. Additionally, in some special cases, it can be known in advance if some \(\omega _x\) will vanish:
-
If H is symmetric, strings with an odd number of Y matrices can be avoided (\(2^{n-1}(2^n+1)\) terms).
-
If H is diagonal, only strings composed by I and Z will contribute (\(2^n\) terms).
The operations made by PauliDecomposer (PD) are
-
If H is diagonal (\(\mathcal {O}[2^n]\) strings): \(\mathcal {O}[2^{2n}]\) operations.
-
Otherwise (\(\mathcal {O}[2^{2n}]\) strings): \(\mathcal {O}[2^{3n}]\) operations.
This PD algorithm checks if the input matrix satisfies one of the aforementioned cases and computes the coefficients using the PC routine and (6), discarding all vanishing Pauli strings. This workflow considerably enhances our results, especially for diagonal matrices.
In Table 2 and Fig. 2, we tested the most extended methods for decomposing matrices into weighted sums of Pauli strings against PD, using Python [34] to compare their performance. In particular, we used the SparsePauliOp class from Qiskit [19] and the decompose_hamiltonian function from PennyLane [20] (only works with hermitian Hamiltonians). To the best of authors’ knowledge, both routines are based on Naive approach without inspecting the input matrix nature before proceeding.
Four types of random \(2^n\times 2^n\) matrices were generated, namely non-hermitian \(H_\text {NH}\), hermitian \(H_\text {H}\), symmetric \(H_\text {S}\) and diagonal \(H_\text {D}\) matrices. The PD vastly outperforms Qiskit and PennyLane routines, specially for the symmetric and diagonal cases.
Building of a Hamiltonian as a sum of weighted Pauli strings.—Many Hamiltonians are written in terms of weighted Pauli strings. Our method can compute weighted Pauli strings directly without extra computations. In Fig. 3, we show a performance comparison of the presented methods for computing Hamiltonians written as sums of weighted Pauli strings. The Hamiltonian used is similar to the one proposed in [27],
being the corresponding weights \(\mathbf {\alpha }\) and \(\mathbf {\beta }\) arbitrary and \(\sigma ^i_3\) as defined in (B2). This Hamiltonian is computed using Algorithm 3, which uses the PDC routine (see Algorithm 2) with two inputs: the string \(x\in \{0,3\}^n\) to compute and the weights to consider. In the PDC case, we use two strategies: compute each weighted term of (7) directly and compute each Pauli string and then multiply it by its corresponding weight (solid and dashed lines in Fig. 3, respectively). This is done by changing lines 6 to \(H\leftarrow H+\alpha _i\texttt {PDC(}str_1\texttt {)}\) and 10 to \(H\leftarrow H+\beta _{ij}{} \texttt {PDC(}str_2\texttt {)}\) in Algorithm 3 for the second one. There is no significant difference between both methods.
5 Conclusions
The fast and reliable computation of tensor products of Pauli matrices is crucial in the field of quantum mechanics and, in particular, of quantum computing. In this article, we propose a novel algorithm with proven theoretical and experimental enhancements over similar methods of this key yet computationally tedious task. This is achieved by taking advantage of the properties of Pauli matrices and the tensor product definition, which implies that one can avoid trivial operations such as multiplying constants by one and waste time computing elements with value zero that could be known in advance.
Concerning memory resources, it is convenient to store the obtained results as sparse matrices since only \(2^n\) out of \(2^{2n}\) entries will not be zero for a Pauli string of length n, i.e., the density of the resultant matrix will be \(2^{-n}\) (see Appendix 1).
Our benchmark tests suggest that the PauliComposer algorithm and its variants can achieve a remarkable acceleration when compared to the most well-known methods for the same purpose both for single Pauli strings and real use cases. In particular, the most considerable outperformance can be seen in Table 2 for the symmetric and diagonal matrix decomposition over the Pauli basis.
Finally, its simple implementation (Algorithm 1-2) can potentially allow to integrate the PC routines into quantum simulation packages to enhance inner calculations.
Data and Code Availability
The data used in the current study is available upon reasonable request from the corresponding authors. The code used can be found at https://github.com/sebastianvromero/PauliComposer.
References
Pauli, W.: Zur Quantenmechanik des Magnetischen Elektrons. Z. Phys. 43, 601 (1927)
Heisenberg, W.: Zur Theorie des Ferromagnetismus. Z. Phys. 49, 619 (1928)
Bethe, H.: Zur Theorie der Metalle. Z. Phys. 71, 205 (1931)
Sherrington, D., Kirkpatrick, S.: Solvable model of a spin-glass. Phys. Rev. Lett. 35, 1792 (1975)
Panchenko, D.: The Sherrington–Kirkpatrick model: an overview. J. Stat. Phys. 149, 362 (2012)
Hubbard, J., Flowers, B.H.: Electron Correlations in Narrow Energy Bands. Proc. R. Soc. Lond. Ser. A Math. Phys. Sci. 276, 238 (1963)
Altland, A., Simons, B.: Second quantization. In: Condensed Matter Field Theory (Cambridge University Press, 2006) pp. 39–93
Jordan, P., Wigner, E.: Über das Paulische Äquivalenzverbot. Z. Phys. 47, 631 (1928)
Bravyi, S.B., Kitaev, A.Y.: Fermionic quantum computation. Ann. Phys. 298, 210 (2002)
Seeley, J.T., Richard, M.J., Love, P.J.: The Bravyi–Kitaev transformation for quantum computation of electronic structure. J. Chem. Phys. 137, 224109 (2012)
Tranter, A., Sofia, S., Seeley, J., Kaicher, M., McClean, J., Babbush, R., Coveney, P.V., Mintert, F., Wilhelm, F., Love, P.J.: The Bravyi–Kitaev transformation: properties and applications. Int. J. Quantum Chem. 115, 1431 (2015)
Tranter, A., Love, P.J., Mintert, F., Coveney, P.V.: A cmparison of the Bravyi–Kitaev and Jordan–Wigner transformations for the quantum simulation of quantum chemistry. J. Chem. Theory Comput. 14, 5617 (2018)
Steudtner, M., Wehner, S.: Fermion-to-qubit mappings with varying resource requirements for quantum simulation. New J. Phys. 20, 063010 (2018)
Östlund, S., Rommer, S.: Thermodynamic limit of density matrix renormalization. Phys. Rev. Lett. 75, 3537 (1995)
White, S.R.: Density matrix formulation for quantum renormalization groups. Phys. Rev. Lett. 69, 2863 (1992)
Verstraete, F., Cirac, J.I.: arxiv:cond-mat/0407066 (2004)
Lanczos, C.: An iteration method for the solution of the eigenvalue problem of linear differential and integral operators. J. Res. Natl. Bur. Stand. B 45, 255 (1950)
Kirby, W., Motta, M., Mezzacapo, A.: Exact and efficient Lanczos method on a quantum computer. Quantum 7, 1018 (2023)
Qiskit, Qiskit: An Open-source Framework for Quantum Computing (2021)
PennyLane, PennyLane: Automatic Differentiation of Hybrid Quantum-Classical Computations (2018)
OpenFermion, OpenFermion: The Electronic Structure Package for Quantum Computers (2017)
Cirq, Cirq (2022)
Liu, R., Vidal Romero, S., Oregi, I., Osaba, E., Villar-Rodriguez, E., Ban, Y.: Digital quantum simulation and circuit learning for the generation of coherent states. Entropy 24, 1529 (2022)
Lucas, A.: Ising formulations of many NP problems. Front. Phys. 2, 5 (2014)
Osaba, E., Villar-Rodriguez, E., Oregi, I.: A systematic literature review of quantum computing for routing problems. IEEE Access 10, 55805 (2022)
Vidal Romero, S., Osaba, E., Villar-Rodriguez, E., Oregi, I., Ban, Y.: Hybrid approach for solving real-world bin packing problem instances using quantum annealers. Sci. Rep. 13, 11777 (2023)
Garcia de Andoin, M., Osaba, E., Oregi, I., Villar-Rodriguez, E., Sanz, M.: Hybrid quantum-classical heuristic for the bin packing problem. In: Proceedings of the Genetic and Evolutionary Computation Conference Companion, GECCO ’22, pp. 2214–2222 (2022)
Fackler, P.L.: Trans, Algorithm 993: Efficient Computation with Kronecker Products, A.C.M.: Math. Softw. 45, 1 (2019)
Horn, R.A., Johnson, C.R.: Matrix equations and the kronecker product, in Topics in Matrix Analysis (Cambridge University Press, 1991) p. 239–297
Burrus, C.S.: Implementing Kronecker Products Efficiently, in Automatic Generation of Prime Length FFT Programs (OpenStax CNX, 2009) pp. 41–49
MATLAB version 9.12.0.1884302 (R2022a), The Mathworks, Inc., Natick, Massachusetts (2022)
Lawson, C.L., Hanson, R.J., Kincaid, D.R., Krogh, F.T.: Trans, Basic Linear Algebra Subprograms for Fortran Usage, A.C.M.: Math. Softw. 5, 308 (1979)
Anderson, E., Bai, Z., Bischof, C., Blackford, S., Demmel, J., Dongarra, J., Du Croz, J., Greenbaum, A., Hammarling, S., McKenney, A., Sorensen, D.: LAPACK users’ guide, 3rd ed., Software, environments, tools (Society for Industrial and Applied Mathematics, 1999)
Python, Python: A Dynamic. Open Source Programming Language, Python Software Foundation (2022)
Harris, Charles R., Jarrod Millman, K., et al., Array Programming with NumPy, Nature 585, 357 (2020)
SciPy, SciPy 1.0: Fundamental Algorithms for Scientific Computing in Python, Nature Methods 17, 261 (2020)
Acknowledgements
We thank Javier Mas Solé, Yue Ban and Mikel García de Andoin for the helpful discussions. This research is funded by the project “BRTA QUANTUM: Hacia una especialización armonizada en tecnologías cuánticas en BRTA” (expedient no. KK-2022/00041). The work of JSS has received support from Xunta de Galicia (Centro singular de investigación de Galicia accreditation 2019–2022) by European Union ERDF, from the Spanish Research State Agency (grant PID2020-114157GB-100) and from MICIN with funding from the European Union NextGenerationEU (PRTR-C17.I1) and the Galician Regional Government with own funding through the “Planes Complementarios de I+D+I con las Comunidades Autónomas” in Quantum Communication.
Author information
Authors and Affiliations
Corresponding author
Additional information
Publisher's Note
Springer Nature remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.
Appendices
Appendix: Some proofs regarding Pauli strings
In this section, we prove two key properties of Pauli strings on which our algorithm is based.
Proposition 1
A Pauli string P(x) of length n given by (1) has only \(2^n\) nonzero entries.
Proof
With the help of Fig. 4, we can compute the number of zeros in the resulting matrix as
In other words, P(x) will have only \(2^n\) nonzero terms. We can prove (A1) by induction easily: since \(n_0(n=1)\) is true, if we assume that \(n_0(n)\) holds we can see that
also holds true.
Corollary 1.1
A Pauli string P(x) of length n given by (1) has only one nonzero entry per row and column.
Proof
Since the tensor product of unitary matrices is also unitary, then \(|\!\det P(x)|=1\). From Th. 1, only \(2^n\) entries of the resulting \(2^n\times 2^n\) matrix are nonzero. So the logical conclusion to be drawn is that the unique way to locate them without having a row and a column full of zeros, thus returning a zero determinant, is that each row and column must have only one nonzero entry.
Standard methods for computing tensor products
In this appendix, we briefly review the well-established algorithms that were used in the benchmark [28,29,30]. First, one can consider what we call the Naive algorithm, which consists on performing the calculations directly. It is clearly highly inefficient as it scales in the number of operations as \(\mathcal {O}[n2^n]\) for sparse Pauli matrices. Second, the Mixed algorithm uses the mixed-product property
with
to simplify the calculation into a simple product of block diagonal matrices. Based on this procedure, Alg993 is presented in [28]. It can be shown that this method performs over \(\mathcal {O}[n2^n]\) operations. Besides that, as Fig. 1 suggests, the fact that it requires to transpose and reshape several matrices has a non-negligible effect that fatally increases its computation time. Finally, the Tree routine starts storing pairs of tensor products as
and proceeds with the resultant matrices following the same logic, which allows to compute (1) by iteratively grouping its terms by pairs. For better results, this method can be parallelized.
Rights and permissions
Open Access This article is licensed under a Creative Commons Attribution 4.0 International License, which permits use, sharing, adaptation, distribution and reproduction in any medium or format, as long as you give appropriate credit to the original author(s) and the source, provide a link to the Creative Commons licence, and indicate if changes were made. The images or other third party material in this article are included in the article's Creative Commons licence, unless indicated otherwise in a credit line to the material. If material is not included in the article's Creative Commons licence and your intended use is not permitted by statutory regulation or exceeds the permitted use, you will need to obtain permission directly from the copyright holder. To view a copy of this licence, visit http://creativecommons.org/licenses/by/4.0/.
About this article
Cite this article
Vidal Romero, S., Santos-Suárez, J. PauliComposer: compute tensor products of Pauli matrices efficiently. Quantum Inf Process 22, 449 (2023). https://doi.org/10.1007/s11128-023-04204-w
Received:
Accepted:
Published:
DOI: https://doi.org/10.1007/s11128-023-04204-w