# How to translate the Hadamard gate matrix into Dirac notation?

$$\frac{1}{\sqrt 2}\begin{bmatrix}1 && 1 \\ 1 && -1\end{bmatrix}$$

The Dirac notation for it is:

$$\frac{|0\rangle+|1\rangle}{\sqrt 2}\langle0|+\frac{|0\rangle-|1\rangle}{\sqrt 2}\langle1|$$

I am unable to understand, how this gate matrix is translated into dirac notation?

First remember that each matrix element can be written as outer products in Dirac notation:

$$|0\rangle\langle 0| = \begin{bmatrix}1 & 0 \\ 0 & 0 \end{bmatrix},|1\rangle\langle 1| = \begin{bmatrix}0 & 0 \\ 0 & 1 \end{bmatrix},|1\rangle\langle 0| = \begin{bmatrix}0 & 1 \\ 0 & 0 \end{bmatrix}, |0\rangle\langle 1| = \begin{bmatrix}0 & 0 \\ 1 & 0 \end{bmatrix}\tag{1}.$$

This can be verified easily, for example:

zero=[0 ; 1], one=[1; 0]
one*zero'


gives:

ans =
0   1
0   0


So now let's write the matrix as a linear combination of outer products in Dirac notation:

$$\tag{2} \frac{1}{\sqrt{2}}|0\rangle\langle 0 | + \frac{1}{\sqrt{2}}|0\rangle\langle 1 | + \frac{1}{\sqrt{2}}|1\rangle\langle 0 | - \frac{1}{\sqrt{2}}|1\rangle\langle 1 |.$$

This is what you have in your question, especially if you factor out the $$|0\rangle$$ from two terms and $$|1\rangle$$ from the other two terms 😊