|q⟩ Bad Qubits

beginner · Programming · Rotation Gates & Universal Single-Qubit Ops

Checkpoint: State Preparation

State preparation is the first step in almost every quantum algorithm: you must load the data or the initial conditions before any computation can take place. In module 8 you have built up two families of single-qubit tools — the RYR_Y gate for tuning amplitude magnitudes, and the phase gate PP for adding relative phases. This checkpoint asks you to combine both.

The general single-qubit state

Any normalized single-qubit state can be written as

ψ=α0+β1,α2+β2=1.|\psi\rangle = \alpha\,|0\rangle + \beta\,|1\rangle, \qquad |\alpha|^2 + |\beta|^2 = 1.

If α\alpha and β\beta are both real and non-negative, a single RYR_Y rotation suffices:

RY(θ)0=cos ⁣θ20+sin ⁣θ21.R_Y(\theta)|0\rangle = \cos\!\tfrac{\theta}{2}\,|0\rangle + \sin\!\tfrac{\theta}{2}\,|1\rangle.

When β\beta carries a non-trivial complex phase, you need a second gate.

Setting magnitudes and phases independently

A clean two-step recipe exploits the fact that RYR_Y leaves all amplitudes real, so any remaining phase can be added afterward with the phase gate P(λ)P(\lambda):

P(λ)=(100eiλ).P(\lambda) = \begin{pmatrix} 1 & 0 \\ 0 & e^{i\lambda} \end{pmatrix}.

P(λ)P(\lambda) leaves the 0|0\rangle amplitude unchanged and multiplies the 1|1\rangle amplitude by eiλe^{i\lambda}. The recipe is therefore:

  1. Choose θ=2arccosα\theta = 2\arccos|\alpha| and apply RY(θ)R_Y(\theta) to set the magnitude ratio.
  2. Choose λ=arg(β)arg(α)\lambda = \arg(\beta) - \arg(\alpha) and apply P(λ)P(\lambda) to install the relative phase.

For most real applications α\alpha is real and positive (so argα=0\arg\alpha = 0), which simplifies the phase choice to λ=arg(β)\lambda = \arg(\beta).

A worked example

Suppose the target is

ψ=320+i21.|\psi\rangle = \frac{\sqrt{3}}{2}\,|0\rangle + \frac{i}{2}\,|1\rangle.

The magnitudes are α=3/2|\alpha| = \sqrt{3}/2 and β=1/2|\beta| = 1/2.

Step 1 — set the magnitudes. Solve cos(θ/2)=3/2\cos(\theta/2) = \sqrt{3}/2:

θ2=π6θ=π3.\frac{\theta}{2} = \frac{\pi}{6} \quad\Longrightarrow\quad \theta = \frac{\pi}{3}.

Applying RY(π/3)R_Y(\pi/3) gives 320+121\frac{\sqrt{3}}{2}|0\rangle + \frac{1}{2}|1\rangle.

Step 2 — add the phase. The target phase on 1|1\rangle is i=eiπ/2i = e^{i\pi/2}, so λ=π/2\lambda = \pi/2. Applying P(π/2)P(\pi/2) multiplies the 1|1\rangle amplitude by eiπ/2=ie^{i\pi/2} = i:

P(π/2) ⁣(320+121)=320+i21.P(\pi/2)\!\left(\frac{\sqrt{3}}{2}|0\rangle + \frac{1}{2}|1\rangle\right) = \frac{\sqrt{3}}{2}|0\rangle + \frac{i}{2}|1\rangle. \checkmark

You can verify normalization: (3/2)2+(1/2)2=3/4+1/4=1(\sqrt{3}/2)^2 + (1/2)^2 = 3/4 + 1/4 = 1.

Try it

Prepare the state 320+i21\frac{\sqrt{3}}{2}|0\rangle + \frac{i}{2}|1\rangle using two gates. The grader checks the full statevector (up to global phase), so both the amplitude magnitudes and the relative phase must be correct.

Run your code to see the quantum state.

After pressing Run, inspect the statevector readout: the 0|0\rangle amplitude should be 0.866\approx 0.866 (real) and the 1|1\rangle amplitude should be 0.500i\approx 0.500i (purely imaginary).

Sign in on the full site to ask questions and join the discussion.