smartyfy.mrindigi.com

Quantum Computing Master Course — 011. Matrix Multiplication and Order

Lesson 011: Matrix Multiplication and Order

Smartyfy Quantum Computing Master Course

Starting point and objectives

You can already multiply a matrix by a column vector. Now imagine passing the result through a second transformation. Matrix multiplication packages those two steps into one matrix. By the end, you should be able to check product dimensions, compose transformations, calculate a product using rows and columns, and demonstrate that changing the order can change the result.

Throughout this lesson, [a, b] describes a column vector when it is called a vector, while [[a, b], [c, d]] describes a matrix whose listed groups are rows. We keep the computational basis ordered as zero, then one. Numbers inside a matrix are amplitudes or transformation coefficients, not automatically probabilities.

Composition explains the rule

Suppose B acts first on a vector v, producing Bv. If A acts next, the final vector is A(Bv). We define the product AB so that (AB)v equals A(Bv) for every compatible v. The rightmost matrix touches the vector first. Reading the written product from left to right as a timeline would reverse the experiment.

This convention is not a special quantum rule. It follows from ordinary function composition with column vectors. The MIT lecture on matrix multiplication provides further approaches to viewing a product, including through its columns. When software represents a sequence differently, translate its convention before comparing formulas.

Why do rows meet columns? Consider the jth basis vector e_j. Multiplying B by e_j selects the jth column of B. Applying A to that column gives the jth column of AB. The ith output coordinate is the sum of the ith row of A times the corresponding entries of that column. Thus the entry in row i, column j of AB is the sum of A_ik B_kj over the shared index k.

Do not conjugate either factor in this calculation. Matrix multiplication is different from the conjugate inner product introduced earlier. Complex entries multiply using ordinary complex arithmetic; conjugation appears only when an operation explicitly requests it.

Dimensions are a first error check

If A has m rows and n columns, while B has n rows and p columns, AB has m rows and p columns. The inner dimensions n must agree. B accepts a p-coordinate vector and produces an n-coordinate vector; A then accepts those n coordinates and produces m coordinates. This input-output explanation is often easier to remember than a shape mnemonic.

For example, a two-by-three matrix can multiply a three-by-four matrix to produce a two-by-four matrix. Reversing those particular factors is impossible: a three-by-four matrix cannot accept the two-coordinate outputs of a two-by-three matrix. Even when both factors are square and both orders exist, their answers can differ.

Worked example: build and use a product

Let A = [[1, 2], [0, 1]] and B = [[2, 0], [1, 1]]. Calculate each entry separately:

AB = [[1·2 + 2·1, 1·0 + 2·1], [0·2 + 1·1, 0·0 + 1·1]] = [[4, 2], [1, 1]].

Test the composition on v = [1, −1]. First, Bv = [2, 0]. Then A(Bv) = [2, 0]. Using the combined matrix gives (AB)v = [4 − 2, 1 − 1] = [2, 0], as required. The agreement illustrates the definition; the row-column derivation explains why it holds for every input.

Now reverse the factors. BA = [[2, 4], [1, 3]], so (BA)v = [−2, −2]. A followed by B is a different transformation. These example matrices are useful algebra exercises, but they are not assumed to be physically allowed standalone quantum gates. We will study that additional constraint in Lesson 013.

Worked example: order changes measurement predictions

Define X = [[0, 1], [1, 0]], which swaps two coordinates, and H = [[1, 1], [1, −1]]/sqrt(2). Start with |0⟩ = [1, 0]. Apply X first: X|0⟩ = [0, 1]. Then H produces [1, −1]/sqrt(2), the minus state. This sequence is HX|0⟩.

Reverse the sequence. H|0⟩ = [1, 1]/sqrt(2), and swapping these equal entries leaves the plus state unchanged. Thus XH|0⟩ is the plus state. Both resulting states have computational-basis probabilities one half and one half. Identical probabilities in this one measurement do not establish identical state vectors.

A final H makes the difference visible: H acting on the minus state yields |1⟩, while H acting on the plus state yields |0⟩. Order can affect later interference even when an immediate measurement hides the distinction. IBM’s single-system treatment gives the standard X and H definitions used here.

Top circuit applies X then H to zero, giving the minus state, and a final H gives one with certainty. Bottom circuit applies H then X, giving the plus state, and a final H gives zero with certainty. Both intermediate states have computational-basis probabilities one half each.
HX means X acts first on the input column. Reversing the order produces a different state, even when an immediate computational-basis measurement has the same distribution. Open full-size diagram

Rules that remain valid

Matrix multiplication is associative: (AB)C = A(BC) whenever the shapes fit. You may change parentheses to organize the calculation without changing factor order. It is distributive: A(B + C) = AB + AC. The identity matrix I leaves compatible vectors and matrices unchanged, so AI = IA = A for square A.

Commutative would mean AB = BA. That is not a general rule, although particular pairs do commute. The identity commutes with every square matrix of its size; diagonal matrices of the same size commute with one another. A single counterexample disproves a claim that all matrices commute. Conversely, agreement on just one input does not prove two transformations identical; agreement on every vector of a basis does.

Practice exercises with worked solutions

  1. Exercise 1. For A = [[1, 2], [0, 1]] and D = [[3, 0], [0, −1]], calculate AD and DA.

    Solution. Row-column multiplication gives AD = [[3, −2], [0, −1]]. For DA, the first row becomes three times A’s first row and the second becomes negative A’s second row, giving [[3, 6], [0, −1]]. Their upper-right entries differ, so they do not commute.

  2. Exercise 2. B maps four coordinates to three; A maps three coordinates to two. What are their shapes, and what is the shape of AB?

    Solution. B is three-by-four and A is two-by-three. Their shared intermediate size is three, so AB is two-by-four. It maps the original four-coordinate input directly to the final two-coordinate output.

  3. Exercise 3. Apply S = [[1, 0], [0, i]] first and X second to v = [1, 2]. Write the product and calculate its action.

    Solution. The product is XS = [[0, i], [1, 0]]. Stepwise, Sv = [1, 2i], followed by X(Sv) = [2i, 1]. Direct multiplication by XS gives the same vector. No normalization was requested; v is an algebra input.

  4. Exercise 4. Two two-by-two matrices send both [1, 0] and [0, 1] to the same outputs. Must they be equal?

    Solution. Yes. Those two outputs are their first and second columns. Equal corresponding columns give equal matrices. Equivalently, every vector is a linear combination of these basis vectors, and linearity makes the outputs agree for every input.

Knowledge check

  1. 1. Which product applies A, then B? A. AB. B. BA. C. A + B. D. Either product always.

    Answer: B. With column vectors, B(Av) = (BA)v; the earliest action sits nearest the input.

  2. 2. A is two-by-three and B is three-by-five. AB is: A. Three-by-three. B. Five-by-two. C. Two-by-five. D. Undefined.

    Answer: C. The shared size three is summed over, leaving two output rows and five input columns.

  3. 3. Which identity always holds when defined? A. AB = BA. B. (AB)C = A(BC). C. AB = A + B. D. AB = I.

    Answer: B. Associativity permits regrouping; it does not permit swapping factors.

  4. 4. One vector satisfies ABv = BAv. What follows? A. A = B. B. AB = BA. C. Both are identity. D. They agree on that input.

    Answer: D. Other inputs may reveal different transformations.

  5. 5. Does ordinary matrix multiplication conjugate complex entries? A. Always. B. Only the left matrix. C. Only diagonal entries. D. No.

    Answer: D. Conjugation belongs to separately specified operations such as the adjoint.

Pitfalls and summary

Check dimensions before arithmetic, preserve the written order, and resist multiplying entries position by position. Use a basis-vector test when you suspect a mistake. Composition is captured by ABv = A(Bv); associativity changes grouping, while commutation changes order and needs justification. Next, we introduce the adjoint, which reverses product order for a different reason.

Primary sources and further study

Definitions are cross-checked against the sources below. Explanations, numerical examples, exercises, and solutions were written for this course. No code, account, installation, or hardware is required for this lesson.