Composing Gates
Quantum gates are linear maps, and like all linear maps they can be chained: the output of one gate becomes the input of the next. A sequence of gates is called a circuit, and reading it left to right tells you the order in which the operations are applied to the state.
Gate sequences as matrix products
Suppose you apply gate and then gate to a state . The result is
Notice the reversal: in matrix notation the rightmost factor is applied first. When you write code, however, you list the operations in the order they are applied — first , then — which matches the way you read a timeline.
A worked example: X then Z
Starting from , apply and then :
- — the NOT gate flips the bit.
- — Z multiplies the amplitude by .
So the final state is . The overall operation can be written as a single matrix product:
You can verify: , matching the step-by-step calculation.
Order matters
Swapping the order to first and then gives a different state:
Step by step:
- — Z leaves unchanged (it only introduces a sign on ).
- .
The final state is , which differs from only by a global phase of . Because and anti-commute (), every pair of states produced by the two orderings is related by this global phase of for any input — so the two orderings are never measurably distinguishable on their own. In contrast, gate pairs that neither commute nor anti-commute (such as and ) can produce states that differ by a relative phase, which does affect measurement statistics in a larger circuit.
Try it
Apply and then to qubit 0, starting from . The grader checks the full state vector — the sign of the amplitude matters here.
After running, you should see that all the probability sits on (as expected — the qubit is definitely ) but the amplitude is , not . That minus sign is the imprint of .
Sign in on the full site to ask questions and join the discussion.