Is there an algorithm explaining how to represent any gate in the matrix form? Suppose, the circuit is the following:
where $$ U := e^{iA\pi/4} = \begin{bmatrix} 0.35-0.85i & -0.35-0.15i \\ -0.35-0.15i & 0.35-0.85i \end{bmatrix} , $$
and the final $CU$ operator has the following matrix representation:
$$ \begin{bmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0.35-0.85i & 0 & 0 & 0 & -0.35-0.15i & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0.35-0.85i & 0 & 0 & 0 & -0.35-0.15i\\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & -0.35-0.15i & 0 & 0 & 0 & 0.35-0.85i & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & -0.35-0.15i & 0 & 0 & 0 & 0.35-0.85i \end{bmatrix} $$
The question is how to understand that the final matrix will be like this? And if to firstly apply the $H$ gate on the 0-th qubit, what it changes in matrix? Or to take another control qubit? In other words, I'm interested in the gate formation from the martrix point of view