Constructing the matrix of a controlled quantum operation
For any gate $U_n$ on $n$-qubits, you can create a controlled version by taking

$$\text{c-}U_n = \left (\begin{matrix}
I_n &amp; 0 \\ 0 &amp; U_n
\end{matrix}\right)$$

where $I_n$ is the identity matrix (of the same dimension as $U_n$). Note that this gate works on $n+1$ qubits, where the first serves as the control bit.

This should work because the controlled gate is required to satisfy 


$\text{c-}U_n |0\rangle|x\rangle = |0\rangle|x\rangle $, and
$\text{c-}U_n |1\rangle|x\rangle = |1\rangle(U_n|x\rangle) $


where $|x\rangle$ is an $n$-qubit state. You should be able to check that the above matrix satisfies exactly this.