# Controlled-U operation in IBMQ

Suppose we have any matrix in $\mathrm U(2)$ and we need to construct a Controlled-$U$ operation with the $H,T,S$ gates. Then I am aware of the construction given in N&C of decomposing $U$ into $e^{ia}AXBXC$. My question is how to implement gates $A,B,C$ using the gates from library set $\left\lbrace H,T,S,X,Y,Z\right\rbrace$.

Also, any sequence of the gates induces some phase to the matrix (while $A,B,C \in \mathrm{SU}(2)$), so how is that phase removed from the circuit?

In general, it is not possible to implement every single-qubit operation exactly. However, it is possible to construct a quantum circuit whose action is approximately equal to this single-qubit operation. This can be done in many different ways, but I'm not sure if it is known which one is most efficient. I do not know of any efficient way to construct such a circuit, however, there is a pretty inefficient method that is well-known. I'll leave it to the OP to decide if it is efficient enough for his/her purposes.

Nielsen and Chuang explain one construction using just the $H$ and $T$ gates. This is done in chapter 4, especially in section 4.5.3. This section builds on earlier results of chapter 4, one of them being exercise 4.11. It is important to note that this exercise is wrong in some editions (have a look at the errata if this is true for your edition). Another source that provides this construction is this, section 4.3, where the $\pi/8$-gate is just a different notation for the $T$-gate.

The construction is difficult to present concisely in this answer. The main idea is to consider the following two gate sequences $HTHT$ and $THTH$. These two gate sequences can be visualized as rotations in the Bloch sphere, where the angle of rotation is an irrational multiple of $\pi$ for both of these gate sequences. Consecutive applications of these two rotations are enough to implement any rotation, and hence the gate sequences $HTHT$ and $THTH$ can be used to construct any single-qubit gate.

You asked for a decomposition using the $\left\lbrace H,T,S,X,Y,Z\right\rbrace$. I assume that's because you think that's all that IBMQ offers. However, it is also possible to do rotations around the $x$, $y$ and $z$ axes, which make things a lot easier.

Any single qubit unitary matrix may be decomposed as a sequence of rotations around two, non-parallel axes. For example

$$U = e^{i\alpha} \,\, R_z(\beta) \,\, R_y(\gamma) \,\, R_z(\delta)$$

Here the global phase $\alpha$ has no physical effects, and can be ignored. The $\beta$ and $\delta$ are angles of rotation around the $z$ axis, and $\gamma$ is the angle for the $y$ axis.

The $z$ axes rotation is called U1 on the IBM Q Experience and in QISKit. It takes a single parameter as an argument, which is the angle of rotation expressed in radians.

The $y$ axis rotation can be done using U3. This takes three arguments. The first of which is the angle in radians for the $y$ rotation, and the other two should be set to zero.

So if you want to do a rotation with $\beta=0.1$, $\gamma=0.2$ and $\delta=0.3$, for example, this could be done using the QASM editor of the IBM Q Experience with

u1(0.3) q[0];
u3(0.2,0,0) q[0];
u1(0.1) q[0];


It can also be done using the composer. You just need to tick the 'advanced' checkbox to see these gates.