# Controlled-U gate on IBMQ

I want to construct a following 16x16 matrix (system of four qubits)

$$U=\text{diag}(1,1,...,1,e^{-iV},e^{-iV})$$

where $$V$$ is a constant.

So this matrix describe these rotations:

$$|1110 \rangle \to e^{-iV}|1110\rangle$$

$$|1111 \rangle \to e^{-iV}|1111\rangle$$

No rotation is applied to other computational basis states.

Following the book Principles of quantum computation and information I can build this $$C^4$$-$$U$$ gate: (with ancillary qubits)

In my situation, I have to build a $$C^3$$-$$U$$ gate, but the procedure is the same.

My problem is when I have to apply this $$C$$-$$U$$ that appears in the middle of the $$C^3$$-$$U$$ gate.

I don't know how to construct the $$C$$-$$U$$ gate so that it performs ONLY the rotation $$e^{-iV}$$ on q[0], leaving the rest of thee qubits (q[1], q[2], q[3]) as they were.

I understand that if I know how to construct the $$C$$-$$U$$, I will have the $$C^3$$-$$U$$ gate which represents the matrix I want to implement.

• Hi @Álvaro and welcome to QCSE. The main question is how to construct a controlled version of the phase gate $U$, defined as $U(V) = \begin{pmatrix}1 & 0 \\ 0 & e^{i V} \end{pmatrix}$, am I right? Did I understand the question correctly? Apr 16, 2020 at 12:29
• Does chap 5.1 in this reference help you? Apr 16, 2020 at 13:29
• You should apply $U(V)$ proposed by Davit on qubit $q_1$ and controlled by qubits $q_2$ and $q_3$. This will prepare global phase gate controlled by qubits $q_1$, $q_2$ and $q_3$ acting on qubit $q_0$. Apr 16, 2020 at 13:46

You can implement your gate $$U$$ with following circuit (note that I denoted qubits differently, i.e. qubits $$q_0$$, $$q_1$$ and $$q_2$$ are controlling qubits and $$q_3$$ is target qubit):

I will start with description of gates acting on qubits $$q_2$$ and $$q_3$$. Gate $$U1$$ is defined as follows $$U1(\theta) = \begin{pmatrix} 1 & 0 \\ 0 & \mathrm{e}^{i \theta} \end{pmatrix}$$

When gate $$U1$$ acts on qubit $$q_2$$ and identity gate $$I$$ acts on qubit $$q_3$$, they together compose so-called controlled global phase gate denoted by matrix

$$U1 \otimes I = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & \mathrm{e}^{i \theta} & 0 \\ 0 & 0 & 0 & \mathrm{e}^{i \theta} \end{pmatrix}$$

This gate changes a phase of $$q_3$$ only in case $$q_2$$ is in state $$|1\rangle$$.

However, we want the gate $$U$$ to act only in case qubits $$q_0$$, $$q_1$$ and $$q_2$$ are all in state $$|1\rangle$$. This means that gate $$U1$$ has to be controlled by qubits $$q_0$$ and $$q_1$$ as well. Since there is only gate $$U1$$ controlled by one qubit, we have to use Toffoli gate (logical AND) to indicate whether both qubits $$q_0$$ and $$q_1$$ are in state $$|1\rangle$$. The result of Toffoli gate is stored in ancila qubit $$q_4$$ and then used for controlling gate $$U1$$. Finnaly ancila qubit is uncomputed again by Toffoli gate (because this gate is inverse to itself) back to state $$|0\rangle$$.

Eventuall, only in case qubits $$q_0$$, $$q_1$$ and $$q_2$$ are all in state $$|1\rangle$$ (as indicated by $$X$$ gates in the figure), the gate acts and change the phase as desired.

A matrix describing the circuits is this $$U = \begin{pmatrix} I_4 & O_4\\ O_4 & U_1 \otimes I \\ \end{pmatrix} = \text{diag}(1,1\ \cdots, \mathrm{e}^{i\theta},\mathrm{e}^{i\theta}),$$ where $$I_4$$ is 4x4 unit matrix and $$O_4$$ is 4x4 zero matrix.

Here I want to share an alternative way of implementing ccu1(t, q[0], q[1], q[2]) gate, where $$t$$ is some phase. It can be shown that ccu1 gate can be decomposed in the following way:

ccu1(t, q[0], q[1], q[2]) =

cu1(t/2, q[0], q[1])
cx(q[1], q[2])
cu1(-t/2, q[0], q[2])
cx(q[1], q[2])
cu1(t/2, q[0], q[2])


where:

cu1(t, q[0], q[1]) =

u1(t/2, q[0])
cx(q[0], q[1])
u1(-t/2, q[1])
cx(q[0], q[1])
u1(t/2, q[1])


As can be found here. So, this implementation of ccu1 gate will require 17 gates in Qiskit and no ancillary qubit. For the version with the ccx gates we will need 35 gates in Qiskit if we will use this decomposition for ccx.

EDIT (by Martin Vesely): For graphical illustration, here is the circuit implementing $$U$$ on IBM Q for $$t = \pi$$:

• Nice solution with no ancilas (+1). Just note that $CU1$ gate is available in QASM and I would expect that in Qiskit as well. So, you can use it directly without further decomposition to simpler gates. Apr 17, 2020 at 13:34
• Thanks, @MartinVesely :). Your proposed solution with the $ccu1$ gate was great. But is the $cu1$ gate implemented at the hardware level? At the hardware level, isn't there just $u1$, $u2$, $u3$ and $cx$ gates for most of IBM's available hardwares? Apr 17, 2020 at 13:57
• But yes, we can use $cu1$ and let the transpiler to do the decomposition part. Apr 17, 2020 at 14:04
• Yes, you are right, $CU1$ is not implemented on HW level but transpiler will replace it by code you posted. Apr 17, 2020 at 14:24