# How can you decompose Grover's diffusion operator into gates?

I know how Grover's diffusion operator works ($$U_s = 2|s\rangle\langle s|-I$$) with the inversion around the mean. However, I want to implement it in simpler gates, to use the algorithm. How can I do this ?

Grover's diffusion operator can be implemented with H, X and a controlled Z gate. I will show this mathematically. Since $$|s\rangle = |+\rangle^{\otimes n}$$ : $$U_s = 2|s\rangle\langle s|-I = H^{\otimes n}(2|0\rangle\langle0|-I)H^{\otimes n}$$ We know have the H gate which appeared. We now know that we will have to apply the Hadamard gate to every qubit at the beginning and the end of Grover's diffusion operator. We will only work with $$2|0\rangle\langle 0|-I$$. $$|0\rangle\langle0|$$ is the outer product of the first basic state. It is a matrix filled with 0s only (0, 0) is filled with a 1. $$I$$ Is the identity matrix. $$2|0\rangle\langle 0|-I = 2 \begin {bmatrix} 1 & 0 & \cdots & 0 \\ 0 & 0 & \cdots & 0 \\ \vdots & \vdots & \ddots & 0\\ 0 & 0 & 0 & 0 \end {bmatrix} - I = \begin {bmatrix} 2 & 0 & \cdots & 0 \\ 0 & 0 & \cdots & 0 \\ \vdots & \vdots & \ddots & 0\\ 0 & 0 & 0 & 0 \end {bmatrix} - \begin {bmatrix} 1 & 0 & \cdots & 0 \\ 0 & 1 & \cdots & 0 \\ \vdots & \vdots & \ddots & 0\\ 0 & 0 & 0 & 1 \end {bmatrix} = \begin {bmatrix} 1 & 0 & \cdots & 0 \\ 0 & -1 & \cdots & 0 \\ \vdots & \vdots & \ddots & 0\\ 0 & 0 & 0 & -1 \end {bmatrix}$$ We now have the matrix which represents $$2|0\rangle\langle 0|-I$$. It flips the phase of every state apart from $$|0\cdots0\rangle$$. If we apply a global phase of $$-1$$ (which can be ignored), we only have to flip the phase of $$|0\cdots0\rangle$$. $$\begin {bmatrix} 1 & 0 & \cdots & 0 \\ 0 & -1 & \cdots & 0 \\ \vdots & \vdots & \ddots & 0\\ 0 & 0 & 0 & -1 \end {bmatrix} = -1 \begin {bmatrix} -1 & 0 & \cdots & 0 \\ 0 & 1 & \cdots & 0 \\ \vdots & \vdots & \ddots & 0\\ 0 & 0 & 0 & 1 \end {bmatrix} = -1 X^{\otimes n} \begin {bmatrix} 1 & 0 & \cdots & 0 & 0 \\ 0 & 1 & \cdots & 0 & 0 \\ \vdots & \vdots & \ddots & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & -1 \\ \end {bmatrix} X^{\otimes n}$$ We can transform the matrix by applying the X gate to every qubit to only have to flip the phase of $$|1\cdots1\rangle$$ state. However, this matrix is only a controlled Z operation with all the qubits except the last one as control.