# Quantum circuit to implement matrix exponential

I want to build a circuit which will implement $$e^{iAt}$$, where $$A= \begin{pmatrix} 1.5 & 0.5\\ 0.5 & 1.5\\ \end{pmatrix}$$ and $$t= \pi/2$$. We see that $$A$$ can be written as, $$A=1.5I+0.5X$$. Since $$I$$ and $$X$$ commute, $$e^{iAt}=e^{i(1.5I)t}e^{i(0.5X)t}$$.

Evaluating manually, I get $$e^{iAt}=1/2\begin{pmatrix} e^{2it}+e^{it} & e^{2it}-e^{it}\\ e^{2it}-e^{it} & e^{2it}+e^{it}\\ \end{pmatrix}.$$

Question

How can I decompose the matrix $$"1/2\begin{pmatrix} e^{2it}+e^{it} & e^{2it}-e^{it}\\ e^{2it}-e^{it} & e^{2it}+e^{it}\\ \end{pmatrix}"$$ into elementary quantum gates

I think this is enough $$e^{iAt}= e^{i(1.5I)t} e^{i(0.5X)t}$$ for constructing the circuit. From rx and u3: $$R_x(-t) = e^{i(0.5X)t} \qquad R_x(\theta) = u3(\theta, -\pi/2, \pi/2)$$ The $$e^{i(1.5I)t}$$ is a global phase gate that can be implemented via the following circuit for q[0] qubit. Here is the whole circuit for the $$e^{iAt}$$:

# Rx part
circuit.u3(-t, -pi/2, pi/2)

# Global phase part
circuit.u1(1.5t, q[0])
circuit.x(q[0])
circuit.u1(1.5t, q[0])
circuit.x(q[0])


The more general approach can be found in this paper (especially 4.1 Trotter decomposition).

• after first u1 gate we will have $a|0\rangle + be^{i1.5t}|1\rangle$, after X gate $a|1\rangle + be^{i1.5t}|0\rangle$, second u1: $ae^{i1.5t}|1\rangle + be^{i1.5t}|0\rangle$, and the last X: $ae^{i1.5t}|0\rangle + be^{i1.5t}|1\rangle$. So we have the same phase for both $|0\rangle$ and $|1\rangle$ states. Is this correct? If yes then in both places we should have 1.5t :) – Davit Khachatryan Mar 29 '20 at 13:24
• I see, thanks for explanation. – Martin Vesely Mar 29 '20 at 17:26
• Just one question. If we have simple one qubit gate, I think that we do not have to bother with global phase, right? In case matrix exponentiation is used in controlled form (e.g. in HHL algorithm), controlled global phase is implemented by gate $U1 \otimes I$. Or do I miss anything? – Martin Vesely Mar 29 '20 at 21:33
• I think we should keep the global phase when we are doing Hamiltonian simulation for HHL(or PEA) algorithm because in the final circuit we will not have the global phase gate, but the controlled version of it. Specifically for one qubit, I think we still should keep it. – Davit Khachatryan Mar 29 '20 at 22:48
• I checked that $U1 \otimes I = |0\rangle\langle0|\otimes I + |1\rangle\langle1|\otimes GP$ (i.e. tensor product have to be switched to arrive at $U1 \otimes I$). However, according to Nielsen and Chuang $U1\otimes I$ is a controlled global phase. It is a little bit confusing. – Martin Vesely Mar 30 '20 at 5:54

There is actually a nice way to do this in Qiskit, since it has decompositions for single-qubit unitaries built in. The QuantumCircuit.squ method takes a unitary 2x2 matrix $$U$$ and a qubit and computes the decomposition

$$U = R_Z(\alpha) R_Y(\beta) R_Z(\gamma)$$

This is a common decomposition, you can find a proof here https://arxiv.org/pdf/quant-ph/9503016.pdf in Lemma 4.1.

Here's how to do it in Qiskit:

import numpy as np
from scipy.linalg import expm
from qiskit import QuantumCircuit, QuantumRegister

A = np.array([[1.5, 0.5],
[0.5, 1.5]])
t = np.pi / 2

# expm is a matrix exponential
U = expm(1j * t * A)

# create a 1 qubit circuit
q = QuantumRegister(1, name='q')
circuit = QuantumCircuit(q)

# apply a single-qubit unitary gate, this will do the decomposition
circuit.squ(U, q[0])

# print the circuit components
print(circuit.decompose().decompose().draw())


This will print

        ┌──────────┐┌───────────┐┌───────────┐
q_0: |0>┤ Rz(pi/2) ├┤ Ry(-pi/2) ├┤ Rz(-pi/2) ├
└──────────┘└───────────┘└───────────┘


So your decomposition would be $$e^{iAt} = R_Z\left(\frac{\pi}{2}\right) R_Y\left(-\frac{\pi}{2}\right) R_Z\left(\frac{-\pi}{2}\right)$$

with

$$R_Y(\theta) = \begin{pmatrix} \cos(\theta / 2) & -\sin(\theta / 2) \\ \sin(\theta / 2) & \cos(\theta / 2) \end{pmatrix}$$ and $$R_Z(\lambda) = \begin{pmatrix} 1 & 0 \\ 0 & e^{i\lambda} \end{pmatrix}$$

You can also drop the last RZ gate if you don't care about the global phase. That's also supported in Qiskit using the up_to_diagonal argument:

# apply a single-qubit unitary gate, this will do the decomposition
circuit.squ(U, q[0], up_to_diagonal=True)

# print the circuit components
print(circuit.decompose().decompose().draw())


which produces

        ┌──────────┐┌───────────┐
q_0: |0>┤ Rz(pi/2) ├┤ Ry(-pi/2) ├
└──────────┘└───────────┘


Here's the implementation in Qiskit: https://qiskit.org/documentation/_modules/qiskit/extensions/quantum_initializer/squ.html.

• Good answer! But is the matrix presented here for $R_z$ the right one? Here I find a different representation of it github.com/Qiskit/qiskit-terra/blob/master/qiskit/extensions/…. The presented matrix is actually the matrix for $u1$ gate. – Davit Khachatryan Mar 29 '20 at 12:49
• But the matrix in the paper you referred decomposes it as $e^{i\alpha}R_y(\beta)R_Z(\gamma)R_Z(\beta)$ which actually gives $e^{iAt}$ for $t=\pi/2, \beta=\pi/2, \gamma=-\pi/2$. The expression you gave i.e. $R_Z(\pi/2)R_Y(\pi/2)R_Z(-\pi/2)$ does not give $e^{iA\pi/2}$ – Upstart Nov 18 '20 at 13:30

You want to implement $$e^{i3\pi/4}e^{iX\pi/4}.$$ I would rewrite this as $$e^{i3\pi/4}He^{iZ\pi/4}H.$$ This is the same as $$-HS^\dagger H$$ in standard gate terminology.

If you're only implementing the gate $$e^{iAt}$$, then you can neglect the global phase and just implement $$HS^\dagger H$$. Both of these gates are readily implemented in qiskit as sdg and h. OK, you can probably implement the basic rotation directly using rx(pi/2,), but this construction is far more relevant in the following case (keeping the global phase a little nicer):

If you're wanting to use this inside something like the HHL algorithm where you actually need controlled-$$e^{iAt}$$, then you implement controlled-$$HS^\dagger H$$, and follow it up with a $$Z$$ gate on the control qubit in order to get the no-longer-global phase correct. Furthermore, note that something like HHL required controlled-$$e^{i2^kAt}$$ for various integers $$k$$. Well, we've done the $$k=0$$ case. But from the way I've written things, I think it makes it more obvious that for $$k=1$$, you're just doing controlled-not ($$HS^\dagger HHS^\dagger H=HZH=X$$). And you don't have to do anything at all for larger $$k$$ ($$X^{2^{k-1}}=I$$ for $$k\geq 2$$).