You are on page 1of 6

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/334615236

Decomposition of unitary matrix into quantum gates

Article · June 2019

CITATIONS READS
0 2,009

1 author:

Dmytro Fedoriaka
Google Inc.
3 PUBLICATIONS   0 CITATIONS   

SEE PROFILE

Some of the authors of this publication are also working on these related projects:

VisARTM View project

All content following this page was uploaded by Dmytro Fedoriaka on 22 July 2019.

The user has requested enhancement of the downloaded file.


Decomposition of unitary matrix into quantum gates

Dmytro Fedoriaka
(Dated: June 19, 2019)
Algorithm is proposed to convert arbitrary unitary matrix to a sequence of X gates and fully
controlled Ry , Rz and R1 gates. This algorithm is used to generate Q# implementation for arbitrary
unitary matrix. Some optimizations are considered and complexity of result is analyzed.

I. INTRODUCTION All notation is 0-indexed.


Quantum gate acting on one qubit is a unitary operator
In this paper I will solve a problem of implementing a acting on state space of this qubit. Similarly, quantum
unitary matrix with a sequence of quantum gates which gate acting on register of several qubits is a unitary op-
can be expressed using standard library of Q# language. erator acting on space which is tensor product of state
Q# is a domain-specific programming language used for spaces of those qubits.
expressing quantum algorithms, developed by Microsoft
[1]. Its standard library currently doesn’t support explic- Matrices. Complex-valued matrix A ∈ Cn×n is called
itly specifying unitary operation by a matrix. Instead, unitary if U † = U −1 . U(n) is set of all unitary matrices
programmer has to express it as a sequence of built-in of size n × n.
quantum gates. However, while designing some quantum Matrix is called special unitary, if it is unitary and has
algorithms it may be necessary to implement a unitary determinant 1. SU(n) is set of all special unitary matrices
operation which is given by a matrix and decomposition of size n × n.
of this matrix into standard gates is not obvious (for Two-level unitary matrix is a unitary matrix obtained
example, [2, problem B2]). from identity matrix by changing a 2 × 2 principal sub-
In this paper I will describe algorithm which can be matrix.
used to generate Q# code using only fully-controlled Rx , Any quantum gate on register of n qubits, being an
Ry and R1 gates and single-qubit X gates. Length of this unitary operator, can be completely defined by unitary
code (in terms of number of commands) will be O(4n ), matrix 2n × 2n . Indexing of matrix elements follows the
where n is number of qubits. same pattern as indexing of register states, i.e. Uij =
hi| U |ji, where i, j ∈ [0, 2n − 1].
I will start by giving some basic definitions. Then I will
describe proposed algorithm (which is based on [3, 4]). Controlled gates. Let’s consider a gate U acting on
Then I will analyze complexity of this algorithm. I will register t. Let’s add a new qubit c to this register and
conclude with discussion of several related topics. define new gate CU as follows. If c is in state |1i, this gate
applies U on t, but if c is in state |0i, this gate doesn’t
change the register’s state:
II. DEFINITIONS (
CU (|ai ⊗ |0i) = |ai ⊗ |0i ,
(2)
Qubits. Qubit is a quantum system which can be in CU (|ai ⊗ |1i) = (U |ai) ⊗ |1i .
superposition of two basis states |0i and |1i.
Register of n qubits is a quantum system which consists Such gate is called controlled, qubits in t are called
of n qubits and its state space is tensor product of state target qubits, and qubit c is called control qubit.
spaces of those qubits. Register’s state space is span of Example of a controlled gate is CNOT (controlled-X)
2n basis states, each of which is tensor product of qubits’ gate:
basis states (although not any register’s state is a tensor
product of qubits’ states). 1 0 0 0
We denote states in qubit register of n qubits by binary CN OT = 00 10 00 01 . (3)
0010
string consisting of n bits. The leftmost bit in string
corresponds to qubit 0, rightmost bit corresponds to qubit Similarly, let’s define controlled gate with mul-
n − 1. Also we denote state by an integer i ∈ 0, . . . 2n − 1, tiple control qubits. Gate CU acting on regis-
represented by that binary string in little-endian style (i.e. ter t0 , . . . , tT −1 , c0 , . . . , cC−1 is controlled by qubits
leftmost bit is least significant). For example, if n = 5 c0 , . . . , cC−1 , if
then register has 32 basis states, and state 25 is
(
(U |ai) ⊗ |bi if |bi = |1 . . . 1i ,
CU (|ai ⊗ |bi) = (4)
|25i = |10011i = |1i ⊗ |0i ⊗ |0i ⊗ |1i ⊗ |1i . (1) |ai ⊗ |bi otherwise,

If i is index of state, i[j] is j-th bit of this index (i.e. where |ai - basis state of target qubits t0 , . . . tT −1 , |bi -
j-th character in binary string representing i). basis state of control qubits c0 , . . . cC−1 , U ∈ U(2T ). In
2

other words, it applies U on target bits only if all control Let’s demand that tan θ is real and positive. Then:
qubits are set to |1i .  
Matrix of a controlled gate has special form. It’s an b
θ = arctan , (10)
identity matrix 2C+T ×2C+T , where lower right submatrix a
2T ×2T is replaced by U . If T = 1, this matrix is two-level
unitary.  
b
arg − exp(−i(λ + µ)) = π+arg(b)−arg(a)−λ−µ = 0
We will denote I — unitary matrix; X = ( 01 10 ) — Pauli a
X matrix, also known as X gate or NOT gate. (11)
Expression a ⊕ b means bitwise addition modulo 2 (also From (11) we can express µ:
known as XOR), e.g. 25 ⊕ 13 = 20.

µ = π + arg(b) − arg(a) − λ. (12)


III. ALGORITHM
Let’s find λ. For this, let’s express c from first equation
A. Two-level decomposition in (8), using (9):

First step is to decompose our unitary matrix A ∈ 


b2 e−2iµ

iλ −iµ iλ
U(d) into product of two-level unitary matrices (d = 2n ). c = cos θ(ae − b tan θe ) = cos θ ae +
ae−iµ
Following algorithm is based on algorithm in [4].
(13)
Let’s make elements in first column equal to zeroes
Let λ = − arg a. Then aeiλ = |a|ei arg a e−i arg a = |a|.
by multiplying matrix (from the right) by two-level uni-
From (12) we get µ = π + arg b, therefore be−iµ =
tary matrices. Assume that at current step elements
|b|ei arg b e−iπ e−i arg b = −|b|, so
A0,i+1 . . . A0,d−1 are already zeroes and we want to make
element A0,i zero as well, without affecting already elimi-
nated elements. This can be written as: |b|2
 
c = cos θ · |a| + . (14)
|a|
...
( ... a b 0 ... 0 ... c 0 0 ... 0
... ... ... ... ... ) U = ( ... ... ... ... ... ... ). (5) We have c real and positive, exactly what we wanted,
Suppose a 6= 0, b 6= 0. so matrix U 0 is given by formula (7) with
Matrix U can be chosen to be two-level unitary matrix
acting on elements (i, i + 1) with non-trivial unitary 2 × 2  
b
submatrix U 0 , where θ = arctan ; λ = − arg(a); µ = π + arg(b). (15)
a

(a b) U 0 = (c 0). (6) If b = 0, we can just skip this step, formally putting


U 0 = I. If a = 0, we can just replace columns by taking
Let’s show that we can always find such special unitary U 0 = X = ( 01 10 ) and proceed.
matrix U 0 which satisfies condition (6), and makes c real After we finished eliminating first row, all elements in it
positive number. except first will be zeroes and matrix still will be unitary.
Any special unitary matrix U 0 can be written in form First element then must have magnitude 1 (because norm
[3, §4.6]: of row in unitary matrix must be 1). As our construction
always makes c real and positive, it must have value 1.
  All other elements in first column must be zeroes, because
cos θeiλ sin θeiµ norm of first column must be 1. So, we get (d − 1)-level
U0 =  , (7) matrix, and we can apply the same algorithm to remaining
− sin θeiµ cos θe−iλ (d − 1) × (d − 1) submatrix and repeat it until only 2 × 2
non-trivial submatrix is left in A, which will make A
where θ, λ, µ ∈ R.
2-level unitary matrix.
Substituting (7) in (6), we get:
For example, for matrix 4 × 4 this process looks like
( this:
a cos θeiλ − b sin θeiµ = c,
(8)
a sin θeiµ + b cos θe−iλ = 0.      
∗ ∗ ∗ ∗ 1 0 0 0 1 0 0 0
     
From second equation we get:  ∗ ∗ ∗ ∗ 0 ∗ ∗ ∗  0 1 0 0
→ → (16)
     
 
 ∗ ∗ ∗ ∗ 0 ∗ ∗ ∗ 0 0 ∗ ∗
b
     
tan θ = − exp (−i(λ + µ)) . (9) ∗ ∗ ∗ ∗ 0 ∗ ∗ ∗ 0 0 ∗ ∗
a
3

Let’s denote U1 , U2 , . . . UD — all matrices which we other qubits in the register. This gate will act on certain
applied during this algorithm and Uf - final two-level basis state only if all bits in index of this state (except
matrix we got. Then the whole process can be written as maybe i-th) are set to one. For example, if n = 5, FC
gate U acting on bit 1 applies matrix U to states |10111i
and |11111i.
A · U1 · U2 . . . UD = Uf , (17)
By convention, FC gate acting on single qubit is just
from which follows simple one-qubit gate without control qubits.
FC gate applies a two-level unitary matrix. But also
† † any two-level unitary matrix (acting on states differing in
A = Uf · UD · UD−1 . . . U1† . (18) one bit) can be implemented with a fully controlled gate
Equation (18) gives desired decomposition of A into and possibly some single-qubit X gates. Let’s show how.
2-level unitary matrices. This decomposition has d(d−1) Let U be two-level unitary acting matrix on states
matrices. Indeed, each matrix in decomposition (includ-
2 (i, i ⊕ 2r ). Let J0 — set of all indices j, such that j-th
ing Uf ) corresponds to one eliminated element in upper bit of i is zero, J1 — set of all indices j, such that j-th
bit of i is one (both sets don’t include r). Then we need
triangular part of matrix A, and there are d(d−1)2 such to apply this two-level unitary only to pair of such states,
elements. whose indices have zeroes on positions J0 , and ones on
All matrices in decomposition are special unitary, with positions J1 . If J0 = ∅, this is simply fully-controlled gate
two exceptions. First, if we were swapping columns due to on qubit r.
a being zero, we will have two-level X matrices. Second,
But if there is some j ∈ J0 , then we have to just apply
if det(A) 6= 1, matrix Uf will not be special unitary.
X on j-th qubit, then apply U and then apply X on j-th
qubit again.
B. Gray codes This will work because X acting on j-th qubit swaps
state i with state i ⊕ 2j , so if U does something only with
states i where i[j] = 1, then XU X does the same thing
Now all two-level matrices in decomposition act on pair
with states i where i[j] = 0.
of states (i, i + 1). For our purposes we want them to act
on pairs of states differing only in one bit, i.e. (i, i ⊕ 2k ). So, to implement two-level unitary matrix we have to
Luckily, for any positive integer n exists such permuta- apply X gate to all qubits from J0 , then apply fully-
tion of numbers 0, 1, . . . , 2n − 1, that any two neighboring controlled gate on qubit r and then again apply X gate
numbers in it differ only in one bit. Such permutation to all qubits from J0 .
is called binary-reflected Gray code [5], and is given by Let’s consider an example. Let n = 5 and we want
formula to apply two-level unitary matrix with non-trivial 2 × 2
submatrix U acting on states |10100i and |10110i. Then
r = 3, J0 = {1, 4} and J1 = {0, 2}. So, we need to build
πi = i ⊕ bi/2c , (19) the following circuit:
where i = 0, 1, . . . , 2n − 1.
For example, Gray code for n = 3 is (0, 1, 3, 2, 6, 7, 5, 4). •
Let’s consider matrix P ∈ U(2n ), such that Pij = δi,πj . •
This is permutation matrix, i.e. its action on a vector •
is permuting elements of that vector with permutation U
π. Then expression P † A0 P simultaneously permutes rows •
and columns of matrix A0 with permutation π. If A0
was a two-level matrix acting on states (i, i + 1), then
A = P † A0 P will be two-level matrix acting on states
D. Implementing a single gate
(πi , πi+1 ) — exactly what we need.
So, we need to apply two-level decomposition algorithm
to matrix A0 = P AP Q †
and get decomposition A0 = i A0i .
Q
At this point we have sequence of X gates and FC gates
Then A = P † A0 P = i (P † A0i P ). So, we have decomposi- acting on single qubit, but each such gate is represented
tion of A into two-level unitary matrices acting on states by arbitrary U ∈ U(2). We have to decompose U into
differing in one bit. product of matrices, which can be implemented by gates.
Similar technique is used in [6]. In this paper I will consider how to decompose them into
R1 , Ry and Rz gates, which are defined as:

C. Fully controlled gates


 
1 0
Let’s call gate a fully controlled (FC) gate acting on R1 (α) =  , (20)
qubit i if this gate acts on qubit i and is controlled by all 0 eiα
4

bits, so there will be not more than two X gates between


  almost all FC gates after optimization. There is only
cos(α/2) sin(α/2) O(2n ) pairs of gates where this doesn’t work and we can
 
iασy
Ry (α) = exp = ,
have up to n X gates (this happens when we proceed to
2 − sin(α/2) cos(α/2)
next row in matrix). Overall, this guarantees that after
(21) optimization there will be O(n · 2n + 4n ) = O(4n ) X gates,
or O(1) X gates per one two-level matrix.
  Another easy optimization to make is to re-
eiα/2 0
 
iασz move all gates which are identity matrices, namely
Rz (α) = exp = . (22)
2 0 e−iα/2 R1 (2πk), Ry (4πk), Rz (4πk) for k ∈ Z.
One more optimization is when we convert matrix to
First step is to make U special unitary matrix, if it’s gates and matrix X occurs, don’t apply usual procedure
not such matrix already. Let φ = arg det U (recall that for not-special unitary matrix (which will result in de-
| det U | = 1). Then det(R1 (−φ) · U ) = e−iφ eiφ = 1. So, composition X = Ry (−π)R1 (π)), but just use FC gate X
takes place decomposition U = R1 (φ)U 0 , where instead. If we do that, we can guarantee that final circuit
will contain at most one R1 gate, and even this gate will
U 0 = R1 (−φ)U (23) be needed only if initial matrix was not special unitary.

and U 0 is special unitary.


Now all is left is to decompose special unitary matrix F. Implementation
U 0 into gates. As U 0 is special unitary, it can be written
in form [3, §4.6]:
I implemented a Python program which uses described
algorithm to transform arbitrary uniform matrix U ∈
U(2n ) into a Q# operation, which implements action of
 
cos θeiλ sin θeiµ
U0 =  , (24) this matrix on array of n qubits. This program is available
− sin θeiµ cos θe−iλ on GitHub [7].
This program performs all steps described above and
where then maps gates to standard Q# commands, namely
X, Controlled X, Controlled Ry, Controlled Rz,
0 0 0
θ = arccos(|U00 |), λ = arg(U00 ), µ = arg(U01 ). (25) Controlled R1.
It can be directly checked that then
IV. COMPLEXITY
0
U = Rz (λ + µ)Ry (2θ)Rz (λ − µ). (26)
One interesting question to consider is how many op-
So, action of single-qubit gate U can be implemented erations does it generally require to implement unitary
using four gates: matrix acting on n qubits.
Decomposition into two-level unitary matrices consisted
n n
of 2 (22 −1) = O(4n ) matrices. Each two-level matrix was
U = R1 (φ)Rz (λ + µ)Ry (2θ)Rz (λ − µ), (27) mapped to 3 or 4 fully-controlled gates and O(n) X gates,
where φ = arg det U and other parameters are given by but after optimization we expect to have O(1) X gates
(25). per two-level unitary matrix. Thus we expect number of
needed commands to be O(4n ).

E. Optimizations Let’s check it experimentally. I generated random


matrices U ∈ U(2n ) for n = 1 . . . 9, decomposed
Combining all previous steps, we can build sequence of them using described algorithm, and calculated how
single-qubit X gates and FC gates R1 , Ry and Rz which many gates of each type appears in the decomposition
implements given unitary matrix. (#(X), #(Ry ), #(Rz ), #(R1 )). Also I calculated total
Each of O(4n ) FC gates is surrounded by O(n) X-gates. number of gates G(n) and number of gates per matrix
It can happen that there are two X gates (after one FC element, which is number of gates divided by 4n . Results
gate and before next FC gate), which act on the same are shown in table I.
qubit. As X 2 = I, they both can be removed. As we can see from the data, indeed G(n) ∼ 2.00 · 4n =
This will eliminate significant amount of X gates. When O(4n ).
neighboring two-level matrices correspond to eliminating
elements of the same row, they act on states (i1 , i2 ) and Let’s denote Gmin (n) — minimal number of gates
(i2 , i3 ), so mask J0 for them can differ at most in two (X, FC-Ry (α), FC-Rz (α), FC-R1 (α)), needed to imple-
5

V. DISCUSSION
Table I. Number of gates in decomposition of random matrices
n #(X) #(Ry ) #(Rz ) #(R1 ) G(n) G(n)/4n A. Further decomposition
1 0 1 2 1 4 1.00
2 2 6 12 1 21 1.31 In this paper we consider fully controlled Ry , Rz and
R1 gates as primitives, because they are supported by Q#
3 28 28 56 1 113 1.77 language. However, two-level matrix can be decomposed
4 130 120 240 1 491 1.92 into sequence of simpler gates, namely Ry , Rz and R1
gates acting on single qubit and CNOT (controlled-X)
5 532 496 992 1 2021 1.97
gate (acting on two qubits), and decomposition of a single
6 2118 2016 4032 1 8167 1.99 two-level matrix will contain Θ(n2 ) such gates. How to
7 8392 8128 16256 1 32777 2.00
do that is shown in [3, §4.6].

8 33290 32640 65280 1 131211 2.00


9 132364 130816 261632 1 524813 2.00 B. Universality

Set of gates is universal if any quantum gate acting on


n n any number of qubits can be implemented by combining
ment arbitrary 2 × 2 unitary matrix. We showed that
Gmin (n) = O(4n ). gates from this set.
Unitary matrix 2n × 2n can be parametrized by 4n Universality is important property for quantum com-
independent real numbers [8, §IV.4]. So, to implement puters: if we have set of universal gates, we only need
arbitrary matrix using gates parametrized by a real num- to be able to implement them on a particular quantum
ber, we will need at least 4n such gates. So, we will have computer to be able to implement any irreversible com-
to use at least 4n FC R1 , Ry and Rz gates, which gives putation on it. Possibility of implementing universal set
us lower bound Gmin (n) = Ω(4n ). Therefore, proposed of gates is one of DiVincenzo criteria [3, §11], meaning it
algorithm gives asymptotically optimal result. is necessary requirement for any physical system to be a
However, in some special cases matrix can be repre- viable quantum computer.
sented by much fewer number of gates, for example, if This paper can be seen as a constructive proof that
it was built as product of a few single-qubit gates and set of single-qubit X gate and fully-controlled gates
CNOT gates. Generally, proposed algorithm will not rec- Rx (α), Ry (α), R1 (α) with any number of control bits (in-
ognize special structure of the matrix and will still return cluding 0) and with any parameters is an universal set of
Ω(4n ) gates. gates.
However, this set can be reduced to just set of CNOT
gate and single-qubit gates Ry (α), Rz (α), R1 (α) [3, §4.6].
Moreover, this set can be reduced to just one 2-qubit gate
[9].

[1] “The Q# programming language.” https: Patent 2,632,058.


//docs.microsoft.com/en-gb/quantum/language. [6] J. J. Vartiainen, M. Möttönen, and M. M. Salomaa,
[2] M. Mykhailova and M. Roetteler, “Microsoft Q# coding “Efficient decomposition of quantum gates,” Physical
contest - winter 2019 - main contest.” https://assets. review letters 92 (2004) no. 17, 177902.
codeforces.com/rounds/1116/contest-editorial.pdf. [7] https://github.com/fedimser/quantum_decomp.
[3] M. Nakahara and T. Ohmi, Quantum computing: from [8] A. Zee, Group theory in a nutshell for physicists.
linear algebra to physical realizations. CRC press, 2008. Princeton University Press, 2016.
[4] C.-K. Li, R. Roberts, and X. Yin, “Decomposition of [9] D. E. Deutsch, A. Barenco, and A. Ekert, “Universality in
unitary matrices and quantum gates,” International quantum computation,” Proceedings of the Royal Society
Journal of Quantum Information 11 (2013) no. 01, of London. Series A: Mathematical and Physical Sciences
1350015. 449 (1995) no. 1937, 669–677.
[5] F. Gray, “Pulse code communication,” Mar. 17, 1953. US

View publication stats

You might also like