ISSN 03617688, Programming and Computer Software, 2010, Vol. 36, No. 2, pp. 111–116. © Pleiades Publishing, Ltd., 2010. Original Russian Text © V.P. Gerdt, A.N. Prokopenya, 2010, published in Programmirovanie, 2010, Vol. 36, No. 2.
Some Algorithms for Calculating Unitary Matrices for Quantum Circuits V. P. Gerdta and A. N. Prokopenyab a
Laboratory of Information Technologies, Joint Institute for Nuclear Research, Dubna, Moscow oblast, 141980 Russia email:
[email protected] bBrest State Technical University, ul. Moskovskaya 267, Brest, 224017 Belarus email:
[email protected] Received July 1, 2009
Abstract—Algorithms for calculating unitary matrices determined by quantum circuits are discussed. The algorithms are used in the program QuantumCircuit designed for modeling quantum circuits. Practical implementations of the algorithms as functions written in the language of the Mathematica system are sug gested. DOI: 10.1134/S036176881002009X
1. INTRODUCTION Interest in quantum informatics and quantum cal culations has considerably grown recently [1]. This is explained, first of all, by potential capability of quan tum computers to perform some calculations, such as factorization of integers and calculation of discrete logarithm [2], superdense information coding [3], and search of an element in a nonstructured database [4], much more efficiently compared to the classical computer. However, the quantum computer capable of solving practical problems has not been developed yet, and the majority of studies in this field are theoretical ones. In this regard, of interest is development of clas sical simulators of the quantum computer, which can be used for searching and testing new effective quan tum algorithms. In [5], the QuantumCircuit program written in the language of the Mathematica system [6] is presented. By means of this program, one can easily construct various quantum circuits and calculate the corre sponding unitary matrices. With this matrix at hand, one can find the terminal state of the quantum mem ory register by its given initial state and to check the operation of the algorithm determined by the quan tum circuit. Note that, in the general case of an n qubit register, the corresponding unitary matrix has dimension 2n × 2n, which implies exponential growth of resources required for its calculation. Therefore, for modeling quantum circuits on a classical computer, efficient algorithms for calculating unitary matrices are required, which makes development of such algo rithms topical. In this work, we analyze algorithms for calculating unitary matrices used in the QuantumCircuit pro gram. It should be noted that this program is universal
in the sense that it allows one to model quantum cir cuits consisting of an arbitrary number of qubits and basic logical elements. In the program, all information on a quantum circuit is coded in a symbolic matrix, the elements of which denote logical operations on one or several qubits. For example, the circuit shown in Fig. 1 (we follow the notation used in [1]) is associ ated with the matrix ⎛ ⎞ ⎜ H C 1 C H⎟ mat = ⎜ H C 1 C H ⎟ . ⎜ ⎟ ⎝ 1 NSN 1 ⎠
(1)
Comparing matrix (1) with the circuit depicted in Fig. 1, it is easy to see that symbols H, S, and 1 denote the Hadamard gate, phase gate, and the identity transfor mation of qubits. The Toffoli gate is presented in matrix mat by the column, in which symbols C and N denote the control and target qubits, respectively. Each column of matrix mat determines logical gates used for transformation of qubits on a current step of
111
|a1〉
H
H
|b1〉
|a2〉
H
H
|b2〉
|a3〉
S
|b3〉
Fig. 1. Quantum circuit on three qubits containing four Hadamard gates (H), phase gate (S), and two Toffoli gates (CCNOT).
112
GERDT, PROKOPENYA
calculations and contains either several onequbit gates or one multiqubit gate. Recall that the quantum circuits are read from left to right and, to determine the qubit state, the standard Dirac notation |a〉 (see [1]) is usually used. The state of each qubit is described by a vector in a twodimen sional Hilbert space, and each onequbit logical ele ment is associated with a unitary matrix of dimension 2 × 2. In accordance with Fig. 1, on the first step of computations, the input state of the quantum register |a1〉, |a2〉, |a3〉 is transformed under the action of three onequbit logical gates H, H, and 1. In this case, the unitary matrix determining register transformation is calculated as a tensor product of three unitary 2 × 2 matrices corresponding to the gates H, H, and 1. On the second step, the qubits interact with one another by means of the threequbit Toffoli gate, the control and target qubits of which are determined by the cor responding symbols in the second column of matrix (1). Matrix representation for the Toffoli element and for other multiqubit logical gates can be obtained by directly defining rules of transformation of basis vec tors in a 2ndimensional Hilbert space. Thus, the cal culation of the unitary matrix corresponding to the quantum circuit depicted in Fig. 1 reduces to succes sive calculation of five matrices specified by the col umns of matrix mat from left to right and subsequent multiplication of these matrices. In what follows, we consider in detail some algorithms that can efficiently calculate unitary matrices corresponding to arbitrary quantum circuits and variants of their practical imple mentation using the computer algebra system Mathe matica 7.0 as a program tool. 2. UNITARY MATRIX DETERMINED BY ONEQUBIT ELEMENTS The qubit is a quantum system the state of which is characterized by the vector |a〉 in a twodimensional Hilbert space. Two different qubit states, which are usually denoted as |0〉 and |1〉, form an orthonormal basis in this space such that an arbitrary state |a〉 can be represented as the superposition |a〉 = α |0〉 + β |1〉, where α and β are complex numbers satisfying the normalization condition |α|2 + |β|2 = 1. For the basis states, the following standard vector representation is used: ⎛ ⎞ |0〉 = ⎜ 1 ⎟ , ⎝ 0⎠
⎛ ⎞ |1〉 = ⎜ 0 ⎟ . ⎝ 1⎠
(2)
In so doing, matrix representation for onequbit logi cal elements in basis (2) is obtained in an ordinary way (see, for example, [1]). Further, we will need matrices for the identity transformation (matI), Hadamard gate (matH), Pauli X gate (matX), and phase gate (matS):
⎛ ⎞ matI = ⎜ 1 0 ⎟ , ⎝ 01⎠
⎞ 1 ⎛ matH = ⎜ 1 1 ⎟ , 2 ⎝ 1 –1 ⎠ (3)
⎛ ⎞ matX = ⎜ 0 1 ⎟ , ⎝ 10⎠
⎛ ⎞ matS = ⎜ 1 0 ⎟ . ⎝ 0 i ⎠
It should be noted that expressions of form (3) may be viewed a definition of the matrices written in the lan guage of the Mathematica system. The state of the quantum register containing n qubits is described by a vector in a 2ndimensional Hil bert space the basis vectors of which are defined by the relation |a 1〉 |a 2〉… |a n〉 ≡ |a 1 a 2 …a n〉 (4) = |a 1〉 ⊗ |a 2〉 ⊗ … ⊗ |a n〉, where aj = 0, 1 (j = 1, …, n) and the symbol ⊗ denotes tensor product of vectors. Since any combination of digits of form (a1a2…an) determines some nbit num ber k from the interval 0 ≤ k < 2n, the basis vectors of the register can be written in a shorter form as |k〉n. Using definition (4) and vectors (2), it is easy to see that the basis vector |k〉n is a column the (k + 1)th com ponent of which is 1 and all other components are equal to zero. For example, for n = 3, the vector rep resentation for the basis vector |5〉3 has the following form: |5〉 3 ≡ |101〉 = |1〉 ⊗ |0〉 ⊗ |1〉 ⎛ ⎞ ⎛ ⎞ ⎛ ⎞ = ⎜ 0 ⎟ ⊗⎜ 1 ⎟ ⊗⎜ 0⎟ ⎝ 1⎠ ⎝ 0⎠ ⎝ 1⎠ ⎛ ⎜ ⎜ ⎜ ⎛ 0⎞ ⎜ ⎜ ⎟ ⎛ ⎞ ⎜ 0 0 ⎜ ⎟ = ⊗⎜ ⎟ = ⎜ ⎜ ⎟ ⎝ ⎠ ⎜ 1 ⎜ 1⎟ ⎜ ⎝ 0⎠ ⎜ ⎜ ⎜ ⎝
0 0 0 0 0 1 0 0
⎞ ⎟ ⎟ ⎟ ⎟ ⎟ ⎟. ⎟ ⎟ ⎟ ⎟ ⎟ ⎠
Since only one component of any basis vector (4) is different from zero, it makes sense to define basis vec tors as sparse arrays, which are represented in the Mathematica system with the help of function Sparse Array. Then, an arbitrary basis state |k〉n of the register containing n qubits can be written as basisVector [ k –, n – ] (5) n := SparseArray [ k + 1 1, 2 ]. The first argument of function SparseArray is the sub stitution rule in which number k + 1 specifies number
PROGRAMMING AND COMPUTER SOFTWARE
Vol. 36
No. 2
2010
SOME ALGORITHMS FOR CALCULATING UNITARY MATRICES
113
gates = {H, H, 1}; matU1 = gateChoose[ gates[[1]] ]; Do[ matU1 = ArrayFlatten[ Outer[ Times, matU1, gateChoose[ gates[[ j]] ] ] ], {j, 2, Length[ gates ] } ] Fig. 2. Commands for generating the unitary matrix determined by the first column of matrix (1).
gateCN[ n–, kn–, kc–?ListQ ] := Block[ {b1, u0, rules}, b1 = Table[ IntegerDigits[j, 2, n], {j, 0, 2n – 1} ]; rules = Table[ {FromDigits[ ReplacePart[b1[[j]], kn Mod[Apply[Times, b1[[j, kc]] ] + b1[[j, kn]], 2 ] ], 2 ] + 1, j} {j, 2n} ]; u0 = SparseArray[ rules, {2n, 2n} ]; u0 ]
1,
Fig. 3. Function for calculating the unitary matrix corresponding to the nqubit gate CNOT.
of the vector component that is equal to 1 and the sec ond argument specifies the vector dimension. The use of function SparseArray, which stores information on only nonzero elements of the array, makes it possible to save memory and model quantum circuits contain ing many qubits. It should be noted, however, that, if we add together a sparse matrix and an ordinary matrix or multiply them, the result will be an ordinary matrix. For this reason, it makes sense to define matrices (3) as sparse ones, although all elements of matrix matH, for example, are nonzero. In so doing, function SparseAr ray can be used in different ways, as can be seen from the following example: masI = SparseArray[ { { 1, 1 } { 2, 2 } 1 }, { 2, 2 } ];
1,
masH = SparseArray [ { { 1/ 2, 1/ 2 },
(6)
{ 1/ 2, – 1/ 2 } } ]. The definition of sparse matrix matI is similar to the definition of the sparse vector (5), where the first argu ment of the SparseArray function specifies the list of locations of nonzero matrix elements, and the second argument, dimension of the matrix. The second exam ple shows that an ordinary matrix can be transformed to a sparse matrix by placing it as an argument of the SparseArray function. Let us assume that a quantum circuit contains n qubits and the state of each qubit is transformed under the action of certain onequbit gates. In this case, the unitary matrix determining transformation of the entire quantum register is found as a tensor product of n matrices corresponding to the onequbit gates. Recall that, in the QuantumCircuit program, these elements are defined by symbols belonging to one col umn of the matrix in which all information about the quantum circuit is coded (matrix mat for the circuit depicted in Fig. 1). Hence, on the first stage of the cal culations of the unitary matrix determined by several onequbit gates, it is required to associate each symbol PROGRAMMING AND COMPUTER SOFTWARE
of the column under consideration with a matrix of form (3). To perform this operation, we define func tion gateChoose[x], which compares symbol x with the symbols used for denoting onequbit gates and gener ates the corresponding 2 × 2 matrix gateChoose [ x – ] := Which[ MatchQ [ x, 1 ], matI, MatchQ [ x, H ], matH, MatchQ [ x, X ], matX, MatchQ [ x, S ], matS ]. On the second stage, the tensor product of two matri ces is cyclically calculated, which is conveniently implemented in Mathematica by means of functions Outer and ArrayFlatten. Command Outer[Times, mat1, mat2] results in the replacement of each element of matrix mat1 by matrix mat2 multiplied by this element. As a result, we obtain a matrix written in a block form, which is reduced then to the ordinary form by means of the ArrayFlatten function. As an example, let us define list gates containing three symbols H, H, and 1 and calculate unitary matrix matU1 determined by the first column of matrix mat (Fig. 2). If matrices matI and matH are defined in the form (6), the resulting unitary matrix matU1 will also be represented in the form of a sparse matrix. Note that function gateChoose[x] recognizes only four symbols corresponding to matrices (3). Defining additional 2 × 2 matrices and introducing the corre sponding notation for them, we can easily increase the number of onequbit logical gates in the program. In addition to the identical transformation, Had amard and Pauli X gates, and phase gate S, the data base in the QuantumCircuit program includes Pauli Y and Pauli Z gates, gate π/8 or T, gate Rk, and Hermi tian conjugate gates.
Vol. 36
No. 2
2010
114
GERDT, PROKOPENYA gateCR[ n–, kn–, kc–?ListQ, k– ] := Block[ {b1, u0, rules}, b1 = Table[ IntegerDigits[j, 2, n], {j, 0, 2n – 1} ]; rules = Table[ {j, j} If[Apply[Times, b1[[j, kc]] ] == 1, Exp[2π ∗ I ∗ b1[[j, kn]]/2k ], 1 ], {j, 2n} ]; u0 = SparseArray[ rules, {2n, 2n} ]; u0 ] Fig. 4. Function for calculating the unitary matrix corresponding to the nqubit controlled gate Rk.
gateSW[ n–, k1–, k2–] := Block[ {b1, u0, rules}, b1 = Table[ IntegerDigits[j, 2, n], {j, 0, 2n – 1} ]; rules = Table[ {FromDigits[ ReplacePart[b1[[j]], {k1 b1[[j, k2]], k2 b1[[j, k1]] } ], 2 ] + 1, j} 1, {j, 2n} ]; u0 = SparseArray[ rules, {2n, 2n} ]; u0 ] Fig. 5. Function for calculating the unitary matrix corresponding to the nqubit SWAP gate.
3. UNITARY MATRICES DETERMINED BY MULTIQUBIT ELEMENTS The simplest logical gate on several qubits is CNOT. It has two input qubits |a1〉 and |a2〉 and performs the transformation (7) |a 1〉, |a 2〉 |a 1〉, |a 2 ⊕ a 1〉, where qubits |a1〉 and |a2〉 are control and target qubits, respectively, and ⊕ denotes addition modulo 2. Clearly, the state of the control qubit is not changed under the action of CNOT, and the sate of the target qubit is changed only if the control qubit is in the state |1〉. This corresponds to the following transformation of the basis vectors: |00〉 |00〉, |01〉 |01〉, (8) |10〉 |11〉, |11〉 |10〉. Using the short notation |k〉2 (k = 0, 1, 2, 3) for them, we may conclude that the action of gate CNOT results in the permutation of the basis vectors |2〉2 and |3〉2. Since the basis vector |k〉2 can be represented as a col umn the (k + 1)th component of which is equal to one, the gate CNOT is associated with the matrix
U CN
⎛ ⎜ = ⎜ ⎜ ⎜ ⎝
1 0 0 0
0 1 0 0
0 0 0 1
0 0 1 0
⎞ ⎟ ⎟. ⎟ ⎟ ⎠
It is easy to see that the jth column of matrix UCN is a vector obtained from the basis vector |j – 1〉 (j = 1, 2, 3, 4) under the action of the gate CNOT. In the general case of an nqubit register, the matrix representation of the gate CNOT can be obtained sim ilar to the case of two qubits. To this end, it is required to successively look through all basis vectors |k〉n (k = 0, 1, …, 2n – 1), apply CNOT to each vector, and place the vector obtained into the (k + 1)th column of matrix UCN. The first step in the implementation of this algorithm is the transformation of the basis vector |k〉n
to the form |a1a2…an〉, where the set of digits (a1a2…an) is determined by the nbit representation of number k. In the Mathematica system, such a transformation can be performed by means of function IntegerDigits[k, 2, n]. Further, if the control qubit has number k1, and target qubit, number k2, then, in accordance with rule (8), the action of gate CNOT reduces to the substitu tion of ak2 ⊕ ak1 for number ak2 in the factorization (a1a2…an). Having performed such a substitution, it is required to pass from the binary representation of the number obtained to the decimal representation using function FromDigits[x, 2] and substituting a list of numbers of the form {a1, a2, …, an} for its first argu ment. If the result is number j, the desired vector is the column with the (j + 1)th element equal to one and all other elements equal to zero. In the QuantumCircuit program, the above described algorithm for calculating the matrix corre sponding to the logical gate CNOT in the case of an n qubit register, is implemented by function gateCN[n, kn, kc], where kn is the number of the target qubit and kc is the list of the controlling qubits (Fig. 3). Note that the definition of function gateCN allows use of several controlling qubits; therefore, its third argument kc is written as a list even if there is only one controlling qubit, for example, gateCN[3, 2, {1}]. In the case of two controlling qubits, function gateCN generates a matrix corresponding to the Toffoli gate. For example, the command gateCN [ 3, 3, { 1, 2 } ]//MatrixForm generates an 8 × 8 matrix corresponding to the stan dard Toffoli gate with the control qubits |a1〉 and |a2〉 and the target qubit |a3〉. Since function gateCN gener ates a sparse matrix, we apply function MatrixForm to represent it in the ordinary form (see, for example, [1]). It should be noted that the qubit state can be changed by means of the Pauli X gate which trans forms state |0〉 to |1〉 and vice versa (to verify this, it is sufficient to multiply matrix matX by vectors (2)).
PROGRAMMING AND COMPUTER SOFTWARE
Vol. 36
No. 2
2010
SOME ALGORITHMS FOR CALCULATING UNITARY MATRICES |a1〉
|b1〉
|a2〉
|b2〉
⎛ ⎜ ⎜ ⎜ ⎜ ⎝
1 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1
trolled gates Pauli Z, S, and T, respectively, which are also included in the database of multiqubit gates available in the QuantumCircuit program. Note that the multiqubit gate SWAP also inter changes states of two qubits, i.e., performs transforma tion of the form
⎞ ⎟ ⎟ ⎟ ⎟ ⎠
Fig. 6. The SWAP gate on two qubits and the correspond ing unitary matrix.
|a 1 …a j …a k …a n〉 |a 1 …a k …a j …a n〉.
Since, according to rule (8), the state of the target qubit is changed only if the control qubit is in the state |1〉, gate CNOT can be interpreted as follows. If the control qubit is in the state |0〉, then the target qubit is not changed. If the control qubit is in the state |1〉, then the Pauli X gate is applied to the target qubit. For this reason, CNOT gate is also referred to as the controlled Pauli X gate. By analogy with CNOT gate, other controlled gates can be defined by replacing the Pauli X gate by a one qubit gate U. As a result, we obtain a controlled gate U, which also has one or several control qubits and one target qubit, such that gate U is applied to a target qubit only if all control qubits are in the state |1〉. The corre sponding unitary matrices are obtained by means of functions that are similar to function gateCN. As an example, let us consider function gateCR[n, kn, kc, k], the first three arguments of which have the same meaning as those of function gateCN and the fourth argument k is a positive integer. This function generates a unitary matrix corresponding to the con trolled gate Rk, where the onequbit gate Rk has the following matrix representation: ⎛ 1 ⎞ 0 matR [ k – ] = ⎜ ⎟. k ⎝ 0 Exp [ 2πi/2 ] ⎠
(9)
According to (9), state |0〉 is not changed under action of element Rk, and state |1〉 receives the phase multi plier Exp[2πi/2k]. This means that exactly the same phase multiplier will be received by those basis states of the nqubit register for which all control qubits and the target qubit are in the state |1〉. Note that the number ing of the basis states is not changed in this case, and, therefore, the desired unitary matrix must be diagonal. In the Mathematica system, the function representing such a matrix in the form of a sparse array can be defined in the form shown in Fig. 4. Note that, for k = 1, 2, and 3, the controlled gate Rk reduces to the con
(10)
Since aj and ak take values 0 and 1, their permutation in (10) means the corresponding permutation of the basis elements in the set |k〉n (k = 0, 1, …, 2n – 1). The function generating the unitary matrix that performs such a permutation can be defined in the form shown in Fig. 5. The second and third arguments k1 and k2 of function gateSW specify numbers of the qubits of the nqubit register the sates of which are to be inter changed. The call of this function in the simplest case of a twoqubit register written as gateSW[2, 1, 2] results in the unitary matrix depicted in Fig. 6 (together with usual notation of the SWAP gate). In the QuantumCircuit program, the functions generating unitary matrices for onequbit and multi qubit logical gates are combined in one function gate Column[col], which generates unitary 2n × 2n matrix by a given list of symbols col. If matrix mat, in which information on the quantum circuit is coded, has m columns, then function gateColumn should be invoked m times, and the matrices generated by it are to be multiplied in the reverse order, since the initial register state is located in the quantum circuit on the left. These calculations are performed by means of func tion matrixU[mat] presented in Fig. 7. As an illustration, consider the quantum circuit depicted in Fig. 8. Clearly, matrix mat coding informa tion about this circuit should contain two rows of form {C, N, C} and {N, C, N}. The unitary matrix deter mined by this circuit is calculated by the command matrixU [ { { C, N, C }, { N, C, N } } ]//MatrixForm Note that the argument of function matrixU is written as a list of two lists, which corresponds to the standard representation of matrices in the Mathematica system. The unitary matrix obtained in this case is presented in Fig. 8, on the right, and coincides with the matrix shown in Fig. 6, which corresponds to the SWAP gate. This proves equivalence of the quantum circuit depicted in Fig. 8 to the logical gate SWAP.
matrixU[ mat–] := Block[ {matTransp, col, matU}, matTransp = Transpose[mat]; col = Length[ matTransp ]; matU = Table[ gateColumn[ matTransp[[col – j]] ], {j, 0, col – 1} ]; matU = Apply[ Dot, matU ]; matU ] Fig. 7. Function for calculating the unitary matrix corresponding to the quantum circuit determined by matrix mat. PROGRAMMING AND COMPUTER SOFTWARE
115
Vol. 36
No. 2
2010
116
GERDT, PROKOPENYA |a1〉
|b1〉
|a2〉
|b2〉
⎛ ⎜ ⎜ ⎜ ⎜ ⎝
1 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1
⎞ ⎟ ⎟ ⎟ ⎟ ⎠
the rules determined by the multiqubit gates. Since only nonzero elements of the unitary matrix are calcu lated, the result is also represented as a sparse array, which considerably speeds up the computation.
Fig. 8. Quantum circuit modeling the SWAP gate on two qubits and the corresponding unitary matrix.
ACKNOWLEDGMENTS This work was supported by the Russian Founda tion for Basic Research, project no. 100100200.
4. CONCLUSIONS
REFERENCES
In this paper, we considered algorithms for calcu lating unitary matrices used in the QuantumCircuit program [5] designed for modeling quantum circuits, which is written in the language of the computer alge bra system Mathematica. In the general case of a quan tum circuit on n qubits, to get the unitary matrix, the following calculations are required: (a) calculation of tensor product of n matrices of size 2 × 2; (b) calculation of 2n × 2n matrices determined by multiqubit logical elements; (c) matrix multiplication. Algorithms for performing calculations of the first and third types are based on using builtin linear alge bra functions of the Mathematica system. The compu tation efficiency is achieved owing to the use of sparse arrays for representing matrices and vectors. In the second case, the unitary matrices are obtained by directly transforming the basis vectors of the 2ndimensional Hilbert space in accordance with
1. Nielsen, M. and Chuang, I., Quantum Computation and Quantum Information, Cambridge: Cambridge Univ. Press, 2000. 2. Shor, P.W., Algorithms for Quantum Computation: Discrete Logarithms and Factoring, Proc. of the 35th Ann. Symp. on Foundations of Computer Science (Santa Fe, 1994), Los Alamitos: IEEE Comput. Soc., 1994, pp. 124–134. 3. Phoenix, S.J.D. and Townsend, P.D., Quantum Cryp tography: How to Beat the Code Breakers Using Quan tum Mechanics, Contemporary Phys., 1995, vol. 36, pp. 165–195. 4. Grover, L.K., From Schrodinger Equation to the Quantum Search Algorithm, Am. J. Phys., 2001, vol. 69, pp. 769–777. 5. Gerdt, V.P., Kragler, R., and Prokopenya, A.N., A Mathematica Package for Construction of Circuit Matrices in Quantum Computation, Comput. Algebra Differential Equations. Acta Academiae Aboensis, Ser. B, 2007, vol. 67, no. 2, pp. 28–38. 6. Wolfram, S., The Mathematica Book, Wolfram Media, Cambridge Univ. Press, 1999, 4th edition.
PROGRAMMING AND COMPUTER SOFTWARE
Vol. 36
No. 2
2010