# Circuit for controlled rotations conditioned on a $d$ bit precision estimate of $\theta$ for $O(d)$ gates

Suppose I have a $$d$$-bit estimate of $$\theta \in (0, 2 \pi]$$, say a register of qubits $$|\tilde{\theta} \rangle = |z_1 \rangle \ldots |z_d \rangle$$ for $$|z_i \rangle \in \{|0 \rangle, |1 \rangle \}$$ where $$\theta \approx \sum_{i=1}^{d} z_i \pi /2^{i}$$, and I wish to implement a transformation $$|\tilde{\theta} \rangle |0 \rangle \mapsto |\tilde{\theta} \rangle (cos(\theta) |0 \rangle + sin(\theta)|1 \rangle)$$ using a quantum circuit consisting of two-qubit gates which can be represented by the controlled unitary transformation

$$U_{c_i} = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & cos(\pi / 2^i) & -sin(\pi / 2^i) \\ 0 & 0 & sin(\pi / 2^i) & cos(\pi / 2^i) \\ \end{bmatrix}$$

I am trying to come up with a formula for how a sequence of these controlled operations would be applied to the ancilla qubit $$|0 \rangle$$ (maybe more than a single qubit?) to result in an overall rotation by $$\approx \theta$$ but I am not sure how to proceed. Any insights appreciated.

Edited the matrix as in the answer.

$$U_{c_i}$$ should not have $$z_i$$ in it. That information is on the input. It is not in the gate.
$$U_{c_i} = \begin{pmatrix} 1&0&0&0\\ 0&1&0&0\\ 0&0&\cos(\pi/2^i)&-\sin(\pi/2^i)\\ 0&0&\sin(\pi/2^i)&\cos(\pi/2^i)\\ \end{pmatrix}$$
If this gate acts on the $$i$$'th qubit of $$| \tilde{\theta} \rangle$$ and the target, this will give the appropriate controlled rotation.
To do all of $$\tilde{\theta}$$ just do all these $$U_{c_i}$$ on the appropriate pairs as described above. This is thanks to the fact that all of these operations are rotating along the same axis. You are just building up
$$\prod_{i=1}^d R_z (\frac{z_i}{2^i}\pi) = R_z (\sum_{i=1}^d \frac{z_i \pi}{2^i}) = R_z (\tilde{\theta})$$