# Representing Quantum Gates in Tensor Product Space

I want to write the matrix form of a single or two qubit gate in the tensor product vector space of a many qubit system. Ill outline a simple example:

Both qubits, $$q_0$$ and $$q_1$$ start in the ground state, $$|0 \rangle =\begin{pmatrix}1 \\ 0 \end{pmatrix}$$. Then we apply the Hadamard gate, $$\begin{pmatrix} 1 & 1 \\ -1 & 1 \end{pmatrix}$$ on the $$q_0$$.

Here is my understanding:

The Hadamard gate on two qubit system only operates on $$q_0$$

$$\hat{H}_0(q_0 \otimes q_1) = \hat{H}_0q_0 \otimes q_1$$

$$\hat{H}_0(|0 \rangle \otimes |0 \rangle) = \hat{H}_0|0\rangle \otimes |0\rangle$$

$$\hat{H}_0 (\begin{pmatrix} 1 \\ 0 \end{pmatrix} \otimes \begin{pmatrix} 1 \\ 0 \end{pmatrix} )= \hat{H}_0 \begin{pmatrix} 1 \\ 0 \end{pmatrix} \otimes \begin{pmatrix} 1 \\ 0 \end{pmatrix}$$

$$\hat{H}_0 \begin{pmatrix} 1 \\ 0 \\ 0 \\0 \end{pmatrix} = \frac{1}{\sqrt{2}} ( \begin{pmatrix} 1 \\ 1 \end{pmatrix} \otimes \begin{pmatrix} 1 \\ 0 \end{pmatrix})$$

$$\hat{H}_0 \begin{pmatrix} 1 \\ 0 \\ 0 \\0 \end{pmatrix} = \frac{1}{\sqrt{2}} \begin{pmatrix} 1 \\ 0 \\ 1 \\ 0 \end{pmatrix}$$

I don't know exactly how to solve this but can give a guess.

$$\hat{H}_0 = \begin{pmatrix} 1 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 \\1 & 0 & -1 & 0 \\ 0 & 0 & 0 & 0 \end{pmatrix}$$

How is $$\hat{H}_0$$ written in the tensor product space? Are there any resources that explain this well? Any help is appreciated!!

First, let me fix a couple of misprints in your Hadamard gate definition, namely the absence of the factor $$1/\sqrt{2}$$ and the minus sign wrong position (take a look at this article): $$H_0 = \frac{1}{\sqrt{2}} \begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix}$$ $$H_0$$ without $$1/\sqrt{2}$$ won't be a unitary matrix (a matrix that conserves a wavefunction norm). And the minus sign before the right bottom element guarantees not only antisymmetry of $$H_0 |0\rangle$$state, but also the Hermitian property $$H_0^{\dagger} = H_0$$. Thus, the conventional Hadamard gate used twice doesn't modify a given state: $$H_0^2 = I$$, where $$I$$ is a $$2\times2$$ identity matrix.

Now, for construction of the desired two-qubit gate, you need the same tensor product operation as you used for the vectors (see this):

$$H_1 \equiv H_0\otimes I = \frac{1}{\sqrt{2}} \begin{pmatrix} 1 \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix} & 1 \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}\\ 1 \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix} & -1 \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix} \end{pmatrix} = \frac{1}{\sqrt{2}} \begin{pmatrix} 1 & 0 & 1 & 0 \\ 0 & 1 & 0 & 1 \\ 1 & 0 & -1 & 0 \\ 0 & 1 & 0 & -1 \end{pmatrix}$$

where $$H_1$$ is a one-qubit Hadamard gate in the two-qubit space. The sense of the formula above is simple: applying $$H_1$$ you mix up the first qubit states and keep the second qubit state unchanged.

Indeed:

$$H_1 \left(|0\rangle\otimes|0\rangle\right) = H_1 \begin{pmatrix} 1 \\ 0 \\ 0 \\ 0 \end{pmatrix} = \frac{1}{\sqrt{2}} \begin{pmatrix} 1 \\ 0 \\ 1 \\ 0 \end{pmatrix} = \frac{1}{\sqrt{2}}\left(|0\rangle\otimes|0\rangle + |1\rangle\otimes|0\rangle\right)$$

If you wish to swap the gate action, i.e. change only the second qubit, you can write it in the same manner:

$$H_2 \equiv I \otimes H_0 = \frac{1}{\sqrt{2}} \begin{pmatrix} 1 \begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix} & 0 \begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix}\\ 0 \begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix} & 1 \begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix} \end{pmatrix} = \frac{1}{\sqrt{2}} \begin{pmatrix} 1 & 1 & 0 & 0 \\ 1 & -1 & 0 & 0 \\ 0 & 0 & 1 & 1 \\ 0 & 0 & 1 & -1 \end{pmatrix}$$

and

$$H_2 \left(|0\rangle\otimes|0\rangle\right) = H_2 \begin{pmatrix} 1 \\ 0 \\ 0 \\ 0 \end{pmatrix} = \frac{1}{\sqrt{2}} \begin{pmatrix} 1 \\ 1 \\ 0 \\ 0 \end{pmatrix} = \frac{1}{\sqrt{2}}\left(|0\rangle\otimes|0\rangle + |0\rangle\otimes|1\rangle\right)$$

If you want the Hadamard gate to act on only one qubit, say only the first qubit, then the composite operator acting on the two-qubit state is given by $$\hat{H} \otimes \mathbf{I}$$ so that

$$(\hat{H} \otimes \mathbf{I})(|q_{0}\rangle \otimes |q_{1}\rangle=\hat{H}|q_{0}\rangle \otimes \mathbf{I}|q_{1}\rangle= \hat{H}|q_{0}\rangle \otimes |q_{1}\rangle$$

Here $$\mathbf{I}$$ is the unit operator or identity operator.

If you want the Hadamard gate to act on both the qubits, then the composite operator acting on both the qubits is given by $$\hat{H} \otimes \hat{H}$$ so that

$$(\hat{H} \otimes \hat{H})(|q_{0}\rangle \otimes |q_{1}\rangle=\hat{H}|q_{0}\rangle \otimes \hat{H}|q_{1}\rangle$$

The operators $$\hat{H} \otimes \mathbf{I}$$ and $$\hat{H} \otimes \hat{H}$$ can be expressed in the matrix form by using the rules of tensor product. See https://en.wikipedia.org/wiki/Tensor_product

To give you a general answer, everytime you have "no gate" on a qubit, this means that there is a identity operator described by unit matrix 2x2 (denote $$I$$). For example, you have $$n$$ qubits and want to apply a gate $$U$$ on $$i$$ th qubit. Then matrix form of this operation is $$I_0 \otimes I_1 \otimes \dots\otimes I_{i-1} \otimes U_i \otimes I_{i+1} \dots \otimes I_{n-2} \otimes I_{n-1}.$$

Note that subscripts are indices of qubits going from 0 to $$n-1$$.