# How do I apply a controlled gate to specific qbits in the register?

Say, I have a specific scheme,

where I need to specify inputs for controlled R logical gate, which here is $$R(\theta)=\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & e^{\frac{2 \pi i}{2\theta}} \end{bmatrix}$$

But that is for the case when the first qbit is the control and the second is the operand.

I don't understand, how I can do it matrixwise, that is, making a matrix that'd use certain qbit of a register as control line and another as the resulting one, leaving everything else as is. I mean, it's okay, if I have a 2 lines where I just put the qregister in the gate and it works. But this is somewhat harder. How should I change the matrix represenation to specify the gate's inputs and outputs?

The action of any controlled gate is to do nothing (i.e. apply the identity operation) if the control qubit is in $$\vert 0\rangle$$ and apply an operation $$U$$ on the target when the control is in $$\vert 1\rangle$$. All other qubits in the system are also left untouched (i.e. apply the identity operation).

Use the subscripts $$c$$ and $$t$$ for the control qubit and target qubit. The gate looks like this

$$I_1\otimes I_2\otimes... \otimes\vert 0\rangle\langle 0\vert_c \otimes... \otimes\ I_t\ \otimes...\otimes I_n \ \ +\ \ I_1\otimes I_2\otimes... \otimes\vert 1\rangle\langle 1\vert_c \otimes... \otimes\ U_t\ \otimes...\otimes I_n$$

In matrix form, this is just

$$\begin{pmatrix}1&0\\ 0&1\end{pmatrix}_1\otimes \begin{pmatrix}1 & 0\\ 0&1 \end{pmatrix}_2 \otimes ... \otimes\begin{pmatrix}1 & 0\\ 0&0 \end{pmatrix}_c \otimes ... \otimes\begin{pmatrix}1 & 0\\ 0&1 \end{pmatrix}_t\otimes... \otimes \begin{pmatrix}1 & 0\\ 0&1 \end{pmatrix}_n \\ + \begin{pmatrix}1&0\\ 0&1\end{pmatrix}_1\otimes \begin{pmatrix}1 & 0\\ 0&1 \end{pmatrix}_2 \otimes ... \otimes\begin{pmatrix}0 & 0\\ 0 & 1 \end{pmatrix}_c \otimes ... \otimes\begin{pmatrix}u_{11} & u_{12}\\ u_{21}& u_{22} \end{pmatrix}_t\otimes... \otimes \begin{pmatrix}1 & 0\\ 0&1 \end{pmatrix}_n$$

A simple example to help is when you only have two qubits, $$t$$ corresponds to the first qubit and $$c$$ corresponds to the second qubit. Then you have

$$I\otimes \vert 0\rangle\langle 0\vert + U\otimes \vert 1\rangle\langle 1\vert.$$

The matrix form is

$$\begin{pmatrix} 1 & 0 & 0 & 0\\ 0 & u_{11} & 0 & u_{12}\\ 0& 0& 1 & 0\\ 0 & u_{21} & 0 & u_{22} \end{pmatrix}$$

• Mathematics is beautiful and wonderful, yes, full of both. Commented Dec 9, 2019 at 15:47
• Please check if you have written the matrix correctly. Assume CNOT, i.e. $U=X$. In this case the matrix has following form: $\begin{pmatrix} I & 0 \\ 0 & X \end{pmatrix}$. This does not correspond to your answer. Commented Dec 9, 2019 at 23:11
• @MartinVesely that form is when the first qubit is the control and the second is the target i.e. for $\vert 0\rangle\langle 0\vert\otimes I + \vert 1\rangle\langle 1\vert\otimes X$. In my example, the second qubit is the control and the first is the target and hence the matrix takes a different form.
– rnva
Commented Dec 10, 2019 at 11:55
• Sorry, did not sebe that. Thanks for explanation. Commented Dec 10, 2019 at 13:23