Programming your first quantum circuitsLesson 4 of 6

Rotate a qubit by any angle

RY and RZ: a gate can be partial, the Bloch sphere is its picture, and a phase alone is invisible.

The Playground's Circuit panel: qubit q0 runs through an Ry(0.25π) gate into a measurement.

The X gate flipped a qubit all the way. The Hadamard spread it exactly in half. In what they do to a fresh qubit, both are stops on one dial: a gate that takes an angle and moves the qubit part of the way.

With it, a circuit stops being a list of yes-or-no operations and becomes something you tune. That is what a rotation gate is for, and it is how a circuit gets odds other than all-or-nothing.

A gate with a dial

ry(theta, 0) rotates qubit 0 by the angle theta, in radians. At 0 it does nothing and the qubit stays at 0. At pi it flips a fresh qubit all the way to 1, the same tally the X gate gave in lesson 1.

At pi / 2, halfway, it gives the same even split the Hadamard gave in lesson 2. In between, it sets the odds anywhere you like.

The rule is short. After ry(theta) on a fresh qubit, the share of shots that read 1 is sin²(θ/2): take the sine of half the angle and square it.

The foundations course derives it from the geometry of the qubit. Here it is the thing to check against real runs, in code, one press per angle.

A semicircle from reads 0 at the top to reads 1 at the bottom, marked at five angles with the share of 1 at each: 0 percent at 0, 14.6 at pi over 4, 50 at pi over 2, 85.4 at three pi over 4, 100 at pi. A bar chart shows the same five shares.
The dial and the rule. The share of 1 is not proportional to the angle: it climbs slowly near the ends and fast through the middle, which is what sin²(θ/2) looks like.
ry

Rotation around Y

The dial. ry(theta, qubit) rotates the qubit by an angle in radians: 0 leaves it at 0, pi flips it, pi / 2 is the even split. The share of 1 after it is sin²(θ/2), and two rotations in a row add their angles. The Y is the axis it turns around.

The circuit, then the code

One qubit, one rotation, one measurement. The angle sits at the top of the file so it can be changed without touching the circuit:

theta = pi / 4

qc = QuantumCircuit(1, 1)
qc.ry(theta, 0)               # rotate qubit 0 by theta
qc.measure(0, 0)

The plumbing gains two lines at the end. One pulls the measured share of 1 out of the dict, which was lesson 1's last assignment. The other computes what the rule says for this angle, so the console shows both side by side.

rotations.py Python · PlaygroundOpen in Playground ↗
# 'backend' already exists when this runs: it is whatever you pick in the Run Experiment dialog.
from math import pi, sin
from qiskit import QuantumCircuit
from qiskit.providers.jobstatus import JobStatus
import time
shots = 1000
1theta = pi / 4
qc = QuantumCircuit(1, 1)
2qc.ry(theta, 0)
qc.measure(0, 0)
print(qc)
job = backend.run(qc, shots=shots)
while job.status() not in (JobStatus.DONE, JobStatus.ERROR, JobStatus.CANCELLED):
    time.sleep(5)
counts = job.result().get_counts()
print(counts)
3measured = counts.get("1", 0) / shots
4expected = sin(theta / 2) ** 2
print(f"theta = {theta:.4f}   share of 1: measured {measured:.3f}   rule says {expected:.3f}")
  1. 1the dial: change this, press Run, compare. 0 is no rotation, pi is a full flip
  2. 2rotate qubit 0 by theta around the Y axis
  3. 3the share of shots that read 1, from lesson 1's last task
  4. 4the rule this lesson is about

Run the circuit, five times

The listing runs one angle per press. Predict the share of 1 for pi / 4 first, then open the project, press Run on Qollab, choose IonQ Aria 1, and press Run.

Then fork the project, as in lesson 1, and in your fork set theta to 0, pi / 2, 3 * pi / 4 and pi, one press each.

The run console after RY by pi over 4: the counts dict and the line theta = 0.7854, share of 1: measured, rule says 0.146
One press, one angle. The last line puts the measured share beside the rule's value for that angle.

Our five presses on 15 September 2026, on the Aria 1 noise model:

thetacountsmeasured share of 1rule says
0{'0': 1000}0.0000.000
pi / 4{'0': 851, '1': 149}0.1490.146
pi / 2{'0': 507, '1': 493}0.4930.500
3 * pi / 4{'0': 123, '1': 877}0.8770.854
pi{'0': 1, '1': 999}0.9991.000

The measured shares track the rule to within a few hundredths, which is lesson 2's scatter at 1,000 shots. At the two ends, where the ideal share is exactly 0 or 1, the noise model adds its small tail: one stray shot at pi, none at 0 this time.

Assignment: hit a quarter

  1. Invert the rule. You want sin²(θ/2) = 0.25, so sin(θ/2) = 0.5. The angle whose sine is one half is π/6, thirty degrees, and that angle is θ/2, not θ.
  2. Set theta to the angle that follows, written as a fraction of pi; from math import pi is already at the top of the file.
  3. Run and compare the measured share with the rule line.
Solution

sin(θ/2) = 0.5 when θ/2 = π/6, so θ = π/3.

theta = pi / 3

The rule line prints 0.250, and the measured share lands near it: within about 0.03 at 1,000 shots, by lesson 2's scatter.

Rotations add

Two rotations in a row rotate by the sum of their angles. ry(pi / 2) twice is ry(pi), a full flip, even though each half on its own would give an even split.

This is the same fact as lesson 1's two X gates, with a dial: gates compose, and the answer depends on the whole sequence.

It is also why the Hadamard and ry(pi / 2) give the same tally but are not the same gate. Two Hadamards in a row gave every shot 0 in lesson 2; two ry(pi / 2) in a row give every shot 1, in the assignment below. Same first step, different second step.

The share after one gate is not the whole state, only the part a measurement can see.

Assignment: two half turns

  1. Set theta back to pi / 2.
  2. Add a second qc.ry(theta, 0) directly under the first.
  3. Predict from the angle, not from two even splits: add the two rotations and find that stop on the dial. Then run. The console's rule line only knows about one theta, so it will keep saying 0.500; compare the measured share with the rule for the summed angle instead.
Solution

π/2 + π/2 = π, a full flip: every shot reads 1, and the measured share prints as 1.000 or a hair under it.

qc.ry(theta, 0)
qc.ry(theta, 0)

The console's rule line says 0.500, because it only knows about one theta. The rule applies to the total angle, not to each gate on its own, which is why the line and the tally disagree.

A rotation you cannot see

Picture the qubit as an arrow from the centre of a globe: fresh, it points at the north pole, 0, and the south pole is 1. That globe has a name, the Bloch sphere, and you can drag the arrow around one on Qollab.

RY tilts the arrow south, and the measurement reads how far it tilted.

RZ spins the arrow around the pole instead. The north-south position does not change, so the share of 1 does not change either. The spin changes only the arrow's position around the pole, and that position is the qubit's phase.

In lesson 2 a weight carried a sign, plus or minus. A phase is that sign made continuous: any angle rather than two. Two paths with the same phase add up, two with opposite phases cancel, and anything between does part of each. Lesson 5 turns that into a tally.

rz

Rotation around Z

The same dial, turned around the axis the measurement reads along. rz(phi, qubit) changes the qubit's phase and leaves the share of 1 exactly where it was.

On a fresh qubit that spin changes nothing at all, not even in principle: there is only one path, and turning it leaves nothing to compare it with.

Lesson 5 puts a Hadamard before the spin, so there are two paths and the spin becomes a difference between them. A Hadamard after it turns that difference into odds you can read.

Assignment: turn the dial where measurement cannot see

  1. Remove the second RY again, so the circuit is back to one rotation.
  2. Replace qc.ry(theta, 0) with qc.rz(theta, 0), with any theta you like.
  3. Predict with the globe: RY tips the arrow south, RZ spins it around the pole. Work out where the arrow is after a spin that starts at the pole, and what a measurement that only reports north or south reads.
  4. Run. The rule line computes sin²(θ/2) whatever gate is in the circuit, so it prints a value that does not apply here; the tally is the answer.
Solution

Every shot reads 0, for any angle. The arrow started at the pole, and spinning around the pole leaves it there; the share of 1 stays at 0.

qc.rz(theta, 0)

The rule is for RY. RZ has a different one, and its answer for the share of 1 is always the share you started with.

A spin around the pole showed nothing on a fresh qubit. The next lesson gives the qubit two paths first, and the same spin becomes something you can read, and then a search.

Stay in the loop.

Get the latest tutorials, demos, and project showcases straight to your inbox. No noise, just the good stuff.