# IBM quantum circuit - order of tensor product for equivalent matrix

I'm trying to understand how to apply tensor products on 3-qubit systems (or well at least 2 qubits). Let's take a basic example: where $$\lvert \psi \rangle = \lvert q_2q_1q_0\rangle$$ with $$q_2$$ being the most significant bit and $$q_0$$ the least significant (matching the above schematic for the circuit).

H matrix (for a single qubit): $$H = \frac{1}{\sqrt{2}}\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix}$$

I understand that I need to use the identity matrix and tensor product in order to format the Hadamard matrix (and any other in the circuit) to be applied to 3 qubits. What I do not understand is how, generally, I determine the order of the tensor product? In order words, with the above description, should I do:

$$M=(H\otimes I)\otimes I = \frac{1}{\sqrt{2}} \begin{bmatrix} I & 0 & I & 0\\ 0 & I & 0 & I\\ I & 0 & -I & 0\\ 0 & I & 0 & -I\\ \end{bmatrix}$$

or

$$M=(I\otimes I)\otimes H = \begin{bmatrix} H & 0 & 0 & 0\\ 0 & H & 0 & 0\\ 0 & 0 & H & 0\\ 0 & 0 & 0 & H\\ \end{bmatrix}$$

More generally, how should I think about why the correct one is the right one? What is the logic I need to understand with regards to the "extension" of my gate's matrices and in which order I need to apply the Identity matrices in the tensor product?

• Hi and welcome to Quantum computing SE. The order od matrices in tensor product follows order of qubits from MSB to LSB, so the first matrix is right one. Just question, why are you asking about T gate in the title and there is nothing about T in the question? Apr 21, 2020 at 7:01
• I must have been drunk. Apr 21, 2020 at 13:50
• And therefore, if my H-gate was being applied on q1, I would do I X H X I, right? Apr 21, 2020 at 14:00

Borrowing from Lenny's Paperback, you can value a product state as follows: $$\begin{eqnarray} |\psi_1\rangle &=& a|0\rangle + b|1\rangle\\ |\psi_1\rangle &=& c|0\rangle + d|1\rangle\\ |\psi_3\rangle &=& |\psi_1\rangle \otimes \,|\psi_2\rangle \\ |\psi_3\rangle &=& ( a|0\rangle + b|1\rangle) \otimes ( c|0\rangle + d|1\rangle) \\ |\psi_3\rangle &=& ac |0\rangle \otimes |0\rangle + ad |0\rangle \otimes |1\rangle + bc |1\rangle \otimes |0\rangle + bd|1\rangle \otimes |1\rangle \end{eqnarray}$$
$$\begin{eqnarray} O &=& H \otimes I \otimes I \\ |\psi\rangle &=& |q_1\rangle \otimes |q_2\rangle \otimes|q_3\rangle \\ O|\psi\rangle &=& \Big (H \otimes I \otimes I \Big) |q_1\rangle \otimes |q_2\rangle \otimes|q_3\rangle \\ O|\psi\rangle &=& (H|q_1\rangle)\otimes ( I|q_2\rangle ) \otimes ( I|q_3\rangle ) \end{eqnarray}$$
And if you want the other operator: $$\begin{eqnarray} O' &=& I \otimes I \otimes H \\ |\psi\rangle &=& |q_1\rangle \otimes |q_2\rangle \otimes|q_3\rangle \\ O|\psi\rangle &=& \Big (I \otimes I \otimes H \Big) |q_1\rangle \otimes |q_2\rangle \otimes|q_3\rangle \\ O|\psi\rangle &=& (I |q_1\rangle)\otimes ( I|q_2\rangle ) \otimes ( H|q_3\rangle ) \end{eqnarray}$$