(2021.11.02) ArXiv2111.00627 H2ZIXY Pauli Spin Matrix Decomposition of Real Symmetric Matrices
(2021.11.02) ArXiv2111.00627 H2ZIXY Pauli Spin Matrix Decomposition of Real Symmetric Matrices
November 2, 2021
Abstract
We present a code in Python3 which takes a square real symmetric matrix, of arbitrary size,
and decomposes it as a tensor product of Pauli spin matrices. The application to the decom-
position of a Hamiltonian of relevance to nuclear physics for implementation on quantum
computer is given.
1 Introduction
In the field of quantum computing, practical quantum computers are often realised in terms of
qubits – two level quantum system – which can be made to undergo a series of quantum logic
gate operations. A natural mathematical way to express the operations is with the set of Pauli spin
matrices, along with the identity matrix. For example, the variational quantum eigensolver [1]
for a many-body system is often implemented by using the Jordan-Wigner transformation to turn
a creation or annihilation operator into Pauli matrices [2, 3]. There are many ways to encode a
Hamiltonian in terms of Pauli matrices [4, 5], and here we present a method to represent an N×N
real symmetric matrix as the combination of Pauli matrices that composes the given matrix through
Kronecker products. As a sample application, we apply the method to a Hamiltonian describing
the deuteron using effective field theory as previously implemented on a quantum computer under
the Jordan-Wigner mapping [6]. We note the wider interest of re-expressing matrices in a matter
suitable for quantum computing [7] as well as the relevance of our approach to the field of tensor
networks [8].
Including the Identity, I, the Pauli spin matrices can be written (in the usual z-diagonal basis)
as
1 0 0 1 0 −i 1 0
I= , X= , Y= , Z= . (1)
0 1 1 0 i 0 0 −1
An arbitrary linear combination of the I, X and Z with real coefficients, is
a0 + a3 a1
a0 I + a1 X + a3 Z = . (2)
a1 a0 − a3
One can see that any real 2×2 symmetric matrix can be represented in this form, and the three
matrices I, X , and Z form a complete basis for real symmetric 2×2 matrices. Formally one can
equate the matrix elements in (2) with the known values in the given matrix, and solve the re-
sulting set of equations for the unknowns a0 , a1 and a3 , though it is trivial to do so for the 2×2
case.
1
SciPost Physics Codebases Submission
Extending to larger matrices, the Kronecker product of Pauli matrices form suitable complete
bases, and the purpose of the present work is to take an arbitrary real symmetric N×N matrix
and to give the representation in terms of Kronecker (or tensor) products of Pauli matrices. For a
matrix which has a power of two order the decomposition is unique, since the Kronecker product
of N Pauli matrices is of order 2N . Matrices of any other order need to be padded to be of a
power of two order, and the method of padding is decribed in the following section, along with
the algorithm in general. In equating the higher order analogue of equation (2) with the given
known matrix, a set of equations for the unknowns is yielded which can be solved by any suitable
linear algebra package.
2 Algorithm
In this section the generic algoritm is given. A particular example is worked through in the fol-
lowing section.
The given input for this method is a user-supplied order N square matrix, H.
Step 1: If H is not of a power-of-two order, increase the size of H and pad the new elements with
zero. The decision to use zero is motivated by the application of this method to Hamiltonians of
systems with bound (negative energy) states whose ground state energy is sought via a variational
technique. Hence a choice of adding extra eigenvalues of zero will leave the variational minimum
ground state unchanged. A user of the code may wish to revisit the choice of padding by putting
eigenvalues of their choice in the diagonal elements of the padding region.
Step 2: From the set {I X Y Z}, generate all permutations with log2 N factors, with repetition
allowed. Here N is the order of the possibly enlarged H matrix from step 1. Remove any permu-
tations with an odd number of Y s since they will give an imaginary component. Generate the set
of Kronecker products of the permutations, and store in a dictionary.
Step 3: Loop through the elements of the Hamiltonian H. Form an equation whose left hand
side is the element of H and the right hand side is the sum of corresponding matrix elements of all
Kronecker products generated in step 2 with an unknown coefficient in front. Encode the equation
as one row of M in the matrix-vector equation M a = h where a is a vector of all the unknowns
and h is the Hamiltonian reshaped as a column vector
Step 4: Use routine from Python numpy package to solve set of equations for unknowns {an }
and construct a string representation of the matrix decomposition.
3 Usage guide
The software is supplied as a single Python3 function. We have not packaged it as part of a library
since it was not written as such, and is not allied to any other functions.
The function h2zixy takes a single argument – a NumPy [9] array – and returns a text string
containing the input matrix in Pauli matrix form.
It is expected that the user will take the function and embed in their own project as they find
most useful. The distributed file h2zixy.py file includes an if __name__ == ’__main__’
clause to work out the small example detailed in the next section.
2
SciPost Physics Codebases Submission
4 Worked example
As an example, we work through a case of a 3×3 matrix. We take the following matrix, which
representes the energy of the deuteron in an oscillator basis restricted to three oscillator states [6]:
−0.43658111 −4.28660705 0
H = −4.28660705 12.15 −7.82623792 . (3)
0 −7.82623792 19.25
This is extended to the next power of two order, i.e. 4×4 by padding with zeros::
−0.43658111 −4.28660705
0 0
−4.28660705 12.15 −7.82623792 0
H = . (4)
0 −7.82623792 19.25 0
0 0 0 0
Since the order 4 = 22 we make a list of all pairs of Pauli matrices which do not have an odd
numbers of Y matrices. This list is
I I, I X , I Z, X I, X X , X Z, Y Y, Z I, Z X , Z Z. (5)
Each of the elements in the list has a 4×4 matrix representation made, which are (from interme-
diate values not usually printed in the code):
1 0 0 0 0 1 0 0 1 0 0 0
0 1 0 0 1 0 0 0 0 −1 0 0
, , ,
II = IX = IZ =
0 0 1 0 0 0 0 1 0 0 1 0
0 0 0 1 0 0 1 0 0 0 0 −1
0 0 1 0 0 0 0 1 0 0 1 0
0 0 0 1 0 0 1 0 0 0 0 −1
XI = , XX = , XZ = ,
1 0 0 0 0 1 0 0 1 0 0 0
0 1 0 0 1 0 0 0 0 −1 0 0
(6)
0 −1
0 0 1 0 0 0 0 1 0 0
0 0 1 0 0 1 0 0 1 0 0 0
, ZI = , ZX = ,
YY =
0 1 0 0 0 0 −1 0 0 0 0 −1
−1 0 0 0 0 0 0 −1 0 0 −1 0
1 0 0 0
0 −1 0 0
ZZ = .
0 0 −1 0
0 0 0 1
Next, we loop over the ten unique matrix elements, with index i, j, of the matrix H in (4) and
for each element construct a row vector whose elements are the ten elements extracted from the
same index i, j of each of the matrices in (6) in the order given. So, for the first (top left) element,
the row vector reads
1 0 1 0 0 0 0 1 0 1 . (7)
This row vector when multiplied by a column vector of coefficients for each of the Pauli matrix
Kronecker products in (6) and equated to the top left element in H (-0.43658111) gives one of
3
SciPost Physics Codebases Submission
the simultaneous equations to be solved. By looping over all unique elements in H ten equations
for ten unknowns are constructed. The row vectors as in (7) are combined into a matrix, M . With
the elements of H packed into a 10 element column vector, h. Then the solution of M a = h for the
coefficients a gives the final answer – the coefficients of the Pauli Kronecker product representtion
of the matrix H.
The full equation for our example is
1 0 1 0 0 0 0 1 0 1 aI I −0.43658111
0 1 0 0 0 0 0 0 1 0 aI X −4.28660705
0 0 0 1 0 1 0 0 0 0 aI Z 0
0 0 0 0 1 0 −1 0 0 0 aX I 0
1 0 −1 0 0 0 0 1 0 −1 aX X 12.15
(8)
=
0 0 0 0 1 0 1 0 0 0 aX Z −7.82623792
0 0 0 1 0 −1 0 0 0 0 aY Y 0
1 0 1 0 0 0 0 −1 0 −1 a Z I 19.25
0 1 0 0 0 0 0 0 −1 0 a Z X 0
1 0 −1 0 0 0 0 −1 0 1 aZ Z 0
The solution, using the NumPy [9] routine linalg.solve gives (with coefficients written to
4 significant figures)
5 Conclusion
The Python function described in this paper performs the task of decomposing a square matrix into
Kronecker products of Pauli spin matrices is a systematic and straightforward way. It performs its
role well for cases of interest to the authors, but other users may benefit from small changes, such
as allowing padding with non-zero dummy eigenvalues; numerical output of coefficients in an
array; speedup in cases of large N; exception handling; input checking. We hope the presented
form provides a sufficient solution for some, and a helpful starting point for others.
Acknowledgements
Useful discussions with Isaac Hobday and James Benstead are acknowledged.
Author contributions The project was defined and overseen by PDS. RMNP performed the cod-
ing.
Funding information RMNP was funded by EPSRC for a summer internship during which this
project was conducted. PDS is funded by an AWE William Penney Fellowship
4
SciPost Physics Codebases Submission
A Code
In the final version of the paper, the code will be linked to a repository associated with the journal.
For the preprint version, we include a code listing here. The LaTeX source inlcudes the code in
plain text form.
1 def h2zixy ( h a m i l t o n i a n ) :
2 """ D e c o m p o s e square real s y m m e t r i c matrix into Pauli spin m a t r i c e s
3
4 argument:
5 h a m i l t o n i a n -- a square numpy real s y m m e t r i c numpy array
6
7 returns:
8 a string c o n s i s t i n g of terms each of which has a n u m e r i c a l c o e f f i c i e n t
9 m u l t i p l y i n g a K r o n e c k e r ( tensor ) p r o d u c t of Pauli spin m a t r i c e s
10 """
11
12 import i t e r t o o l s
13 import numpy as np
14
15 # c o e f f i c i e n t s s m a l l e r than eps are taken to be zero
16 eps = 1. e -5
17
18 dim = len ( h a m i l t o n i a n )
19
20 # Step 1: expand H a m i l t o n i a n to have l e a d i n g d i m e n s i o n = power of 2 and pad
21 # with zeros if n e c e s s a r y
22
23 N e x t P o w T w o = int (2** np . ceil ( np . log ( dim ) / np . log (2) ) )
24 if N e x t P o w T w o != dim :
25 diff = N e x t P o w T w o - dim
26 h a m i l t o n i a n = np . hstack (( hamiltonian , np . zeros (( dim , diff ) ) ) )
27 dim = N e x t P o w T w o
28 h a m i l t o n i a n = np . vstack (( hamiltonian , np . zeros (( diff , dim ) ) ) )
29
30 # Step 2: G e n e r a t e all tensor p r o d u c t s of the a p p r o p r i a t e length with
31 # all c o m b i n a t i o n s of I ,X ,Y , Z , e x c l u d i n g those with an odd number of Y
32 # matrices
33
34 # Pauli is a d i c t i o n a r y with the four basis 2 x2 Pauli m a t r i c e s
35 Pauli = { ’I ’ : np . array ([[1 ,0] ,[0 ,1]]) ,
36 ’X ’ : np . array ([[0 ,1] ,[1 ,0]]) ,
37 ’Y ’ : np . array ([[0 , -1 j ] ,[1 j ,0]]) ,
38 ’Z ’ : np . array ([[1 ,0] ,[0 , -1]])}
39
40 N u m T e n s o r R e p e t i t i o n s = int ( np . log ( dim ) / np . log (2) )
41 N u m T o t a l T e n s o r s = 4** N u m T e n s o r R e p e t i t i o n s
42 P a u l i K e y L i s t = []
43 K e y s T o D e l e t e = []
44 P a u l i D i c t = {}
45
46 def P a u l i D i c t V a l u e s ( l ) :
47 yield from i t e r t o o l s . p r o d u c t (*([ l ] * N u m T e n s o r R e p e t i t i o n s ) )
48
49 # G e n e r a t e list of tensor p r o d u c t s with all c o m b i n a t i o n s of Pauli
50 # m a t r i c e s i . e . ’ III ’, ’ IIX ’, ’ IIY ’, etc .
51 for x in P a u l i D i c t V a l u e s ( ’ IXYZ ’) :
52 P a u l i K e y L i s t . append ( ’ ’. join ( x ) )
53
54 for y in P a u l i K e y L i s t :
55 PauliDict[y] = 0
56
57 for key in P a u l i D i c t :
58 T e m p L i s t = []
59 P a u l i T e n s o r s = []
5
SciPost Physics Codebases Submission
6
SciPost Physics Codebases Submission
References
[1] J. R. McClean, J. Romero, R. Babbush and A. Aspuru-Guzik, The theory of vari-
ational hybrid quantum-classical algorithms, New Journal of Physics 18(2) (2016),
doi:10.1088/1367-2630/18/2/023023.
[3] P. Jordan and E. Wigner, Über das Paulische Äquivalenzverbot, Zeitschrift für Physik 47(9),
631 (1928), doi:10.1007/BF01331938.
[4] P. Siwach and P. Arumugam, Quantum simulation of nuclear Hamiltonian with a general-
ized transformation for Gray code encoding, Physical Review C 104(3), 034301 (2021),
doi:10.1103/PhysRevC.104.034301, Publisher: American Physical Society.
[8] P. Silvi, F. Tschirsich, M. Gerster, J. Jünemann, D. Jaschke, M. Rizzi and S. Montangero, The
Tensor Networks Anthology: Simulation techniques for many-body quantum lattice systems, Sci-
Post Phys. Lect. Notes p. 8 (2019), doi:10.21468/SciPostPhysLectNotes.8.
7
SciPost Physics Codebases Submission