The Single-Qubit Toolbox
Most early QCoder problems are really asking one question:
βCan you control one qubit well?β
That sounds small, but one-qubit fluency carries a lot of the book.
The first gates to master
x: swaps|0>and|1>z: flips the sign of|1>h: changes between the computational basis and the plus/minus basisrx,ry,rz: continuous rotationsp: adds a phase to|1>
You do not need every gate in the library yet. You do need to know what these do without hesitation.
ry as a state-preparation tool
For real amplitudes, ry(theta) is the cleanest gate to learn first:
\[ R_y(\theta)|0\rangle = \cos(\theta/2)|0\rangle + \sin(\theta/2)|1\rangle \]
This equation matters because it lets you design a target probability instead of guessing.
For example, if you want probability 3/4 on |1>, then you want:
\[ \sin^2(\theta/2) = 3/4 \]
so one valid choice is theta = 2 * asin(sqrt(3) / 2) = 2pi/3.
from math import pi
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector
qc = QuantumCircuit(1)
qc.ry(2 * pi / 3, 0)
print(Statevector.from_instruction(qc))
Why z feels useless until it does not
If your qubit is definitely |0>, then z appears to do nothing.
But after a Hadamard, z changes relative phase:
\[ Z\frac{|0\rangle + |1\rangle}{\sqrt{2}} = \frac{|0\rangle - |1\rangle}{\sqrt{2}} \]
That is enough to change later interference.
The plus/minus basis is not optional
The states
\[ |+\rangle = \frac{|0\rangle + |1\rangle}{\sqrt{2}}, \qquad |-\rangle = \frac{|0\rangle - |1\rangle}{\sqrt{2}} \]
show up constantly:
|+>is whathcreates from|0>|->is whathcreates from|1>|->is the standard ancilla state for phase kickback tricks
You do not need to worship this notation. You do need to recognize it instantly.
Learn one tiny identity well
\[ H Z H = X \]
This is not just algebra. It is the smallest example of a powerful idea:
- phase information exists in one basis
- change basis
- the same action looks like a bit flip
You can check it directly:
from qiskit import QuantumCircuit
from qiskit.quantum_info import Operator
lhs_circuit = QuantumCircuit(1)
lhs_circuit.h(0)
lhs_circuit.z(0)
lhs_circuit.h(0)
rhs_circuit = QuantumCircuit(1)
rhs_circuit.x(0)
lhs = Operator(lhs_circuit)
rhs = Operator(rhs_circuit)
print(lhs == rhs)
If you prefer not to rely on operator equality, test both circuits on |0> and |1> with Statevector.
Relative phase versus global phase
This distinction becomes important very early.
- multiplying the entire state by
-1does not change physical behavior - changing only one branch by
-1does change interference
So -|1> and |1> are physically equivalent, but (|0> + |1>)/sqrt(2) and (|0> - |1>)/sqrt(2) are not.
Checkpoint Exercises
- Prepare the minus state.
- Prepare a state with
P(1)=3/4. - Verify
H Z H = Xon|0>and|1>. - Build three different circuits for
-|1>and confirm they differ only by global phase.