Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Quantum neural networks

Quantum neural networks (QNNs) are parameterized quantum circuits used as trainable models where quantum states, parameterized gates, and classical optimisation are combined to learn patterns — useful for hybrid quantum-classical algorithms and near-term quantum hardware.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

QNNs let engineers build trainable models that run partly on quantum hardware and partly on classical computers. They are used in hybrid algorithms for classification, regression, and feature extraction where near-term quantum devices (NISQ devices) can provide expressive functions not easy to emulate classically.

Key ideas

  • Ansatz — the fixed structure of the PQC (which gates and in what order). The ansatz determines the function class the QNN can represent.

  • Cost function — a real function C(θ) built from measured expectation values, e.g. C(θ) = 1/N Σi (yi − ⟨Oi⟩θ)^2 where ⟨Oi⟩θ = ⟨ψ0|U†(θ) Oi U(θ)|ψ0⟩. The optimizer seeks θ that minimize C(θ).

  • Data-encoding — how classical input x is encoded into the circuit: using parameterized gates Rx(x), Ry(x), phase kicks, or amplitude encoding. Good encoding is crucial.

  • Measurement / readout — the QNN output is one or more expectation values of observables (Pauli matrices commonly). Example: output = ⟨Z⟩θ where Z = [[1,0],[0,-1]].

  • Parameter-shift rule — an exact method to compute gradients of expectation values with respect to parameters when the generator of the parameterized gate has only two eigenvalues that differ by 2 (commonly ±1, as with Pauli rotations). Exact statement: If U(θ) = e^{-i θ G/2} with G^2 = I (so G has eigenvalues ±1), and f(θ) = ⟨ψ|U†(θ) O U(θ)|ψ⟩, then ∂f/∂θ = (f(θ + π/2) − f(θ − π/2))/2. Assumptions: generator G has eigenvalues ±1 (valid for single-qubit Pauli rotations Rx, Ry, Rz). This avoids finite-difference noise and works on quantum hardware.

  • Derivation universities expect (parameter-shift rule, step by step):

    1. Write U(θ) = cos(θ/2) I − i sin(θ/2) G because e^{-i θ G/2} = cos(θ/2) I − i sin(θ/2) G when G^2 = I.
    2. Compute f(θ) = ⟨ψ|U†(θ) O U(θ)|ψ⟩ and its derivative using product rule, simplify using G^2 = I and unitarity.
    3. Recognize trigonometric identities that give the shifted evaluations f(θ ± π/2). Solve algebraically to obtain ∂f/∂θ = (f(θ + π/2) − f(θ − π/2))/2.
  • On diagrams to draw: for a one-qubit QNN with input x and parameter θ: draw a horizontal wire labeled |0⟩ on the left; place a gate labeled Ry(x) (data-encoding) then a gate Ry(θ) (trainable) then a measurement box labeled ⟨Z⟩. Label time left-to-right and annotate that measurement returns expectation value of Pauli Z.

Formulas

|ψ(θ)⟩ = U(θ) |ψ0⟩ — |ψ(θ)⟩ is the state after the parameterized unitary U(θ); (unitless). Applies to pure states.

⟨O⟩θ = ⟨ψ0| U†(θ) O U(θ) |ψ0⟩ — expectation value of observable O under parameters θ; O is Hermitian; (unitless if O is dimensionless).

C(θ) = (1/N) Σ_i (y_i − ⟨O_i⟩θ)^2 — mean squared error cost function for supervised learning; y_i and ⟨O_i⟩θ are unitless labels/expectations.

U(θ) = e^{-i θ G/2} — parameterized gate generated by Hermitian G. θ is in radians (treated as dimensionless), and G is typically dimensionless (e.g., Pauli matrices), so the exponent is unitless.

If G^2 = I then U(θ) = cos(θ/2) I − i sin(θ/2) G — expansion by spectral decomposition (unitless).

∂f/∂θ = (f(θ + π/2) − f(θ − π/2)) / 2 — parameter-shift rule for generators with eigenvalues ±1; f(θ) = ⟨ψ|U†(θ) O U(θ)|ψ⟩. Units: f and gradients are unitless; θ in radians.

⟨Z⟩ for state Ry(φ)|0⟩ = cos(φ) — expectation of Pauli Z after a y-rotation by φ. Unitless; φ in radians.

Notes: Pauli matrices X,Y,Z are dimensionless operators; expectations are unitless numbers in [-1,1].

Worked examples

  1. Theory: expectation ⟨Z⟩ after a single Ry(φ) and a short derivation

    • Circuit: |0⟩ — Ry(φ) — measure Z. Draw the one-wire circuit, label gate Ry(φ) and measurement box ⟨Z⟩.
    • Ry(φ) on |0⟩ gives |ψ⟩ = cos(φ/2)|0⟩ + sin(φ/2)|1⟩.
    • Compute ⟨Z⟩ = |⟨0|ψ⟩|^2 − |⟨1|ψ⟩|^2 = cos^2(φ/2) − sin^2(φ/2) = cos φ.
    • Final boxed answer: ⟨Z⟩ = cos φ (unitless).
  2. Derivation: parameter-shift rule for G^2 = I (complete steps)

    • Let U(θ) = e^{-i θ G/2} and f(θ) = ⟨ψ|U†(θ) O U(θ)|ψ⟩.
    • Because G^2 = I, U(θ) = cos(θ/2) I − i sin(θ/2) G.
    • Compute f(θ + s) and f(θ − s) for s = π/2; use trig identities to eliminate derivative terms and solve for ∂f/∂θ.
    • Algebra (sketch): differentiate f(θ) directly or equivalently use that combinations f(θ + π/2) − f(θ − π/2) isolate sin and cos cross-terms to yield 2 ∂f/∂θ. Conclude ∂f/∂θ = [f(θ + π/2) − f(θ − π/2)]/2.
  3. Programming: compute expectation and parameter-shift gradient for a one-qubit Ry with φ = x + θ.

    • Code (plain Python, runs with standard library):
# QNN forward and parameter-shift gradient for one qubit, Ry rotation
import math

def expectation_Z(phi):
    # For Ry(phi)|0> expectation of Z is cos(phi)
    return math.cos(phi)

def param_shift_grad(x, theta):
    phi = x + theta
    f_plus = expectation_Z(phi + math.pi/2)
    f_minus = expectation_Z(phi - math.pi/2)
    grad = 0.5 * (f_plus - f_minus)
    return expectation_Z(phi), grad

# Sample input
x = math.pi/4    # 45 degrees
theta = math.pi/6  # 30 degrees
exp_val, gradient = param_shift_grad(x, theta)
print('Expectation <Z> =', exp_val)
print('Parameter-shift gradient d<Z>/dθ =', gradient)

Sample input (values used inside program): x = π/4, θ = π/6

Exact output (decimal values):

Expectation <Z> = 0.25881904510252074

Parameter-shift gradient d<Z>/dθ = -0.9659258262890683

Explanation: φ = x + θ = 5π/12 = 75°, cos75° ≈ 0.2588190451 and derivative is −sin75° ≈ −0.9659258263, matching parameter-shift.

Common mistakes

  • Not normalizing states after amplitude encoding or incorrect ordering of gates (data-encoding then ansatz vs ansatz then encoding matters).
  • Mixing radians and degrees for gate parameters — quantum gates expect radians.
  • Thinking QNNs automatically give quantum advantage; expressivity does not equal advantage and training issues (barren plateaus) are common.

For your exam

Kinds of questions

  • Definitions: define QNN, PQC, ansatz, parameter-shift rule.
  • Short notes: explain data-encoding methods and readout for QNNs.
  • Derivations: derive the parameter-shift rule for Pauli rotations; derive expectation ⟨Z⟩ after Ry(φ).
  • Numericals: compute expectation values for given rotations; use parameter-shift to compute gradients for small circuits.
  • Programs: write simple Python code to compute expectation and gradient for a one-qubit QNN (no external libraries) — include sample inputs/outputs.
  • Diagrams: draw and label a one- or two-qubit PQC with data-encoding and trainable layers.

What a full-marks answer includes

  • Clear definitions with assumptions (e.g., what generators are allowed), correct step-by-step derivations, labeled diagrams, units (radians), and careful handling of measurement vs expectation. For programming questions include runnable code and sample output.

Quick check

  1. What is the PQC output from an initial state |ψ0⟩?
  2. State the parameter-shift formula for a rotation generated by a Pauli operator.
  3. For Ry(φ)|0⟩, what is ⟨Z⟩?

Answers: 1) |ψ(θ)⟩ = U(θ)|ψ0⟩. 2) ∂f/∂θ = (f(θ + π/2) − f(θ − π/2))/2 when generator has eigenvalues ±1. 3) ⟨Z⟩ = cos φ.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?