IBM quantum circuit - order of tensor product for equivalent matrix

I'm trying to understand how to apply tensor products on a 3 qbit systems (or well at least 2 qbits). Let's take a basic example:

where $$\lvert \psi \rangle = \lvert q2q1q0\rangle$$ with q2 being the most signifiant bit and q0 the least significant (matching the above schematic for the circuit).

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

I understand that I need to use the identity matrix & tensor product in order to format the Hadamard matrix (and any other in the circuit) to be applied to 3 qbits. 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? – Martin Vesely Apr 21 '20 at 7:01
• I must have been drunk. – logicOnAbstractions Apr 21 '20 at 13:50
• And therefore, if my H-gate was being applied on q1, I would do I X H X I, right? – logicOnAbstractions Apr 21 '20 at 14:00

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