Matrix Multiplication - RPIQuantumComputing/QuantumCircuits GitHub Wiki

How to Multiply Matrices?

Matrix multiplication is essential to determine the states of qubits. Although matrix multiplication seems intimidating at first glance, following these three steps can quickly compute it.

  1. Check the dimension of the matrices.
  2. Guess the dimension of the outcome matrix.
  3. Dot products & fill in.

Check the Dimension of the Matrices

Let's say you are given two different matrices: $A$ and $B$, and you must operate multiplication. Unfortunately, you cannot always find the product of two matrices. There is a requirement to make multiplication happen: the number of columns in the first matrix (matrix $A$) and the number of rows in the second matrix (matrix $B$) must match. That is, if you got matrices $A_{a \times b}$ and $B_{c \times d}$, and you find $b = c$, you can find their product. Else, the product of these matrices is not defined.

$$ AB = \begin{bmatrix} a_{11}&a_{12}&a_{13} \ a_{21}&a_{22}&a_{23} \end{bmatrix} \begin{bmatrix} b_{11}&b_{12}&b_{13} \ b_{21}&b_{22}&b_{23} \ b_{31}&b_{32}&b_{33} \end{bmatrix} $$

In this example, $A$ is a $2 \times 3$ matrix, and $B$ is a $3 \times 3$ matrix. You can immediately know finding $AB$ is possible because the number of columns in matrix $A$ (3) matches the number of rows in matrix $B$ (3).

$$ CD = \begin{bmatrix} c_{11}&c_{12}&c_{13} \ c_{21}&c_{22}&c_{23} \end{bmatrix} \begin{bmatrix} d_{11}&d_{12} \ d_{21}&d_{22} \ d_{31}&d_{32} \ d_{41}&d_{42} \end{bmatrix} $$

This time, finding $CD$ is impossible. The number of columns in matrix $C$, the first matrix, is 3, but the number of rows in matrix $D$, the second matrix, is 4. Since the numbers do not match, it is impossible to compute this matrix multiplication. As we will mention later, it is why the commutative property in matrix multiplication does not work (i.e., $AB \neq BA$).

Guess the Dimension of the Outcome Matrix

In step 1, we verified if the matrix multiplication is possible by checking whether the number of columns in the first matrix equals the number of rows in the second matrix. In easier terms, you were checking $n$ in $A_{m \times n} B_{n \times l}$, the numbers that are inside. In this step, we are going to predict the dimension of the outcome matrix. The outcome matrix must have the number of rows in the first matrix and the number of columns in the second matrix. Let's say you are given $A_{m \times n} B_{n \times l}$ again. We know the outcome matrix $C$ must be $C_{m \times l}$ because $m$ is the number of rows in matrix $A$, and $n$ is the number of columns in $B$.

$$ AB = \begin{bmatrix} 1&3&8 \ -7&2&4 \end{bmatrix} \begin{bmatrix} 1&2&-3 \ -5&0&-2 \ 4&9&7 \end{bmatrix} = \begin{bmatrix} \square&\square&\square \ \square&\square&\square \end{bmatrix} $$

In this example. matrix $A$ has 2 rows and 3 columns, and matrix $B$ has 3 rows and 3 columns. The outcome matrix of this multiplication must have 2 rows and 3 columns because $A$ has 2 rows and $B$ has 3 columns. Now, we will have a deeper look at how to fill in those $\square$ in the next section.

Dot Products & Fill In

In this final step, as the subtitle name explains, we will fill the outcome matrix with the result of dot products. Let's say we want to fill the first-row first-column entry. We choose the first row of the first matrix and the first column of the second matrix. Then, we find the dot product of these to fill the entry. Let's continue with the example above. Recall that you multiply each entry and find the sum to compute a dot product.

$$ \begin{bmatrix} 1&3&8 \end{bmatrix} \begin{bmatrix} 1 \ -5 \ 4 \end{bmatrix} = (1)(1) + (3)(-5) + (8)(4) = 18 $$

So, we found the first row first column entry of $AB$.

$$ AB = \begin{bmatrix} 18&\square&\square \ \square&\square&\square \end{bmatrix} $$

More generally speaking, if you want to find the $i$ th row and $j$ th column of the outcome matrix, you will choose the $i$ th row of the first matrix and the $j$ th column from the second matrix and calculate their dot product. Let's find the second row second column of our outcome matrix. We choose the second row of the first matrix and the second column.

$$ \begin{bmatrix} -7&2&4 \end{bmatrix} \begin{bmatrix} 2 \ 0 \ 9 \end{bmatrix} = (-7)(2) + (2)(0) + (4)(9) = 22 $$

Now we know the second row second column entry of the outcome matrix.

$$ AB = \begin{bmatrix} 18&\square&\square \ \square&22&\square \end{bmatrix} $$

If you continue this process to fill out the rest of the entries, you will get something like this:

$$ AB = \begin{bmatrix} 18&74&47 \ -1&22&45 \end{bmatrix} $$

Useful Matrix Multiplication Properties

  • $AB \neq BA$ (Commutative property does not hold.)
  • $A(B+C) = AB + AC$, $(B+C)A = BA + CA$ (Distributive Property)
  • $(AB)C = A(BC)$ (Associative Property)
  • $IA = A$, $AI = A$ (Identity Property)
  • $[0]A = [0]$, $A[0] = [0]$ (Zero Property)