8 min read
Matrices as Linear Operators
How a matrix transforms a vector, why gate order matters, and a first look at the Pauli-X matrix in action.
Suggested before this lesson
Every quantum gate you'll ever apply in Qiskit, h, x, cx, all of them, is a matrix. Applying a gate to a state is nothing more than multiplying that matrix by the state's vector. This lesson is about what that multiplication means and how matrices compose.
A matrix acting on a vector in :
Each output entry is a row of the matrix, dotted against the input vector. This is a linear map: scaling the input scales the output by the same factor, and the map distributes over sums, . Every quantum gate has to be linear for exactly this reason: it has to act consistently on a superposition, transforming each amplitude's contribution independently, not "pick one branch and act on that."
The Pauli-X matrix is . Apply it to :
That's the bit-flip you already know from Qiskit's qc.x(0), now visible as ordinary matrix arithmetic: swaps the two rows of whatever vector it's given, which swaps the / amplitudes exactly.
Applying gate and then gate to a state means computing , which turns out to equal : a single matrix, , computed by multiplying the two gate matrices together (note the order: happens first but ends up on the right). Matrix multiplication is defined so that this always works, for any sequence of linear maps.
Crucially, in general, matrix multiplication doesn't commute. This is the formal reason gate order matters in a circuit: qc.x(0); qc.h(0) and qc.h(0); qc.x(0) are different matrices, and (unless you happen to pick two gates that do commute) different final states.
Different matrices (), confirming then isn't the same circuit as then .
Pick a gate and sweep the input vector around the circle; the solid arrow shows where that gate actually sends it.
Vector transform
20°
(0.94, 0.34)
(0.34, 0.94)
1.000
Drag the slider to sweep around the unit circle and watch how reshapes it into .