← Quantum Store · Guides · Lessons · Glossary · About · Türkçe
Qiskit course: real quantum programming · Lesson 5 / 6 · 10 min
Rotation gates and parameterised circuits
Meet the angle-based rx, ry and rz gates, build circuits whose angles are supplied later with Parameter and see the idea behind variational algorithms.
Turning by an angle: rx, ry, rz
Gates such as H and X always do the same thing. Rotation gates take an angle and turn the qubit by that much. For a |0⟩ qubit turned with ry(θ), the probability of measuring 1 is sin²(θ/2): θ = 0 does nothing, θ = π/2 gives 50–50 like H and θ = π gives a certain 1 like X.
rz(θ) and p(θ) only change the phase. Gates you already know are special angles of these: Z = p(π), S = p(π/2), T = p(π/4).
import math
from qiskit import QuantumCircuit
qc = QuantumCircuit(1)
qc.ry(math.pi / 3, 0) # probability of 1 is sin²(π/6) = 0.25
qc.measure_all()
Parameter: supply the angle later
When you want to try the same circuit with different angles you do not need to build a new circuit each time. Parameter leaves the angle as a name and assign_parameters gives it a value before running. The loop below gives always 0, 50–50 and always 1 for the angles 0, π/2 and π.
import math
from qiskit import QuantumCircuit
from qiskit.circuit import Parameter
from qiskit.primitives import StatevectorSampler
theta = Parameter('theta')
qc = QuantumCircuit(1)
qc.ry(theta, 0)
qc.measure_all()
sampler = StatevectorSampler()
for angle in [0, math.pi / 2, math.pi]:
circuit = qc.assign_parameters({theta: angle})
counts = sampler.run([circuit], shots=1000).result()[0].data.meas.get_counts()
print(round(angle, 2), counts)
Why it matters: variational algorithms
The methods most tried on today’s noisy devices rely on parameterised circuits. In variational algorithms such as VQE and QAOA the quantum computer runs a parameterised circuit and computes an energy or cost with the Estimator, while a classical optimiser adjusts the angles to make that value smaller. The loop repeats until the best angles are found.
“Quantum neural networks” in quantum machine learning follow the same idea: the weights being trained are the angles of rotation gates.
Try it yourself
The playground has no rotation gates, but you can see two special angles: ry(π/2) gives the same 50–50 result as H, and ry(π) the same certain 1 as X. Compare the two examples.
Open the circuit in your browserThe circuit playground is in Turkish. Button guide: “1000 kez ölç” = measure 1000 times, “Durum vektörünü göster” = show the state vector, “Bu devrenin Qiskit kodu” = this circuit’s Qiskit code, “Geri al” = undo, “Temizle” = clear. Click a gate in the circuit to remove it.
Key points
- For a |0⟩ qubit turned with ry(θ), the probability of measuring 1 is sin²(θ/2).
- rz and p only change the phase: Z = p(π), S = p(π/2), T = p(π/4).
- Parameter leaves an angle as a name; assign_parameters gives it a value before running.
- Variational algorithms tune the angles with a classical optimiser.
Test yourself
For a |0⟩ qubit, which θ in ry(θ) gives a 50% chance of measuring 1?
- π/4
- π/2
- π
Show the answer
π/2. The probability of 1 is sin²(θ/2). For θ = π/2 that is sin²(π/4) = 0.5. π/4 gives about 15% and π gives 100%.
← The statevector and expectation values · Onto a real quantum computer: transpile and IBM Quantum →