# How to represent controlled quantum circuits as matrices?

I'm trying to implement (actually, simulate) the quantum algorithm by Harrow, Hassidim and Lloyd - http://arxiv.org/abs/0811.3171 - to solve linear systems of equations using a simple $2\times2$ case in Scilab. By entering the matrix $A$ and a vector $\vec{b}$, the algorithm should output the vector $\vec{x}$.

The simplified circuit for this case is described by Cao et all. - http://arxiv.org/abs/1110.2232 - (sorry, I have no permission to post more links), and uses controlled gates for Hamiltonian Simulation and rotation over the eigenvalues of $A$.

I was able to implement the controlled HAM-SIM gates, represented by

$\hskip2in$

using matrices like $\begin{bmatrix}\mathbb{I}&\cdots&0\\\vdots&\ddots&\vdots\\0&\cdots&U\end{bmatrix}$ where $U = e^{iA\frac{t_0}{denom}}$, but I'm having trouble representing the rotation matrices:

$\hskip2in$

Since they will be applied at the first qubit and controlled by other qubits, I could not figure out the structure of the matrix required to apply the expected transformation.

For example, the second controlled-rotation matrix would apply the transformation $\left|000\right\rangle \rightarrow \left|000\right\rangle$; $\left|001\right\rangle \rightarrow R_y(\frac{2\pi}{2^{m-1}})\left|0\right\rangle \otimes \left|01\right\rangle$; and so on.