First time studying this topic?

Launch the interactive walkthrough to discover each component, including the 3D Q-Sphere Lab & Python in Colab.

Topic 8.7

Variational Quantum Circuits (VQE & PQC): The Quantum Neural Network

Master Parameterized Quantum Circuits (Ansätze), classical optimization loops, and the Parameter Shift Rule for computing quantum gradients.

Learning Objectives

  • ✓Construct parameterized quantum circuits with rotational gates: Rx(theta), Ry(theta), Rz(theta)
  • ✓Implement the Parameter Shift Rule for exact hardware gradient evaluation
  • ✓Understand the Barren Plateau phenomenon (vanishing quantum gradients)

📖Let's Begin with a Story

In classical deep learning, neural networks learn by computing gradients via backpropagation. Now imagine you want to train a neural network on a real physical quantum computer. Can you use backpropagation on a quantum chip? NO! Backpropagation requires saving all intermediate layer activations in memory. But if you look at intermediate qubits on a quantum chip, you collapse the wavefunction and destroy the calculation! Furthermore, the No-Cloning Theorem forbids copying the states! How can we calculate gradients on a physical quantum chip without collapsing the state? In 2018, quantum researchers discovered the Parameter Shift Rule. By running the quantum circuit twice—once shifted forward by +90 degrees and once shifted backward by -90 degrees—the difference between the two measurements gives the EXACT analytical gradient! This mathematical breakthrough unlocked gradient descent on real quantum hardware!

Why Should You Learn Variational Quantum Circuits (VQE & PQC): The Quantum Neural Network?

🎯 Practical & Engineering Stakes

The foundational training mechanism of QML

The Parameter Shift Rule is the quantum equivalent of backpropagation. It allows frameworks like PennyLane and Qiskit Machine Learning to train quantum circuits using standard PyTorch optimizers.

What Is It?

Simple Explanation

A Variational Quantum Circuit is a quantum neural network: - It has layers of rotating gates with adjustable dials (angles θ\theta). - To find the slope (gradient) for a dial: 1. Turn the dial slightly forward (+90°) and measure. 2. Turn the dial slightly backward (-90°) and measure. 3. Subtract the two scores: (Forward - Backward) / 2! - That gives you the exact slope to adjust your dial down the loss mountain!

Technical Explanation

Parameterized Quantum Circuits (PQCs) implement Ansätze U(θ)=∏l=1LUl(θl)U(\boldsymbol{\theta}) = \prod_{l=1}^L U_l(\theta_l) where generators GlG_l have two unique eigenvalues ±r\pm r. The Parameter Shift Rule computes exact analytical partial derivatives on quantum hardware without finite-difference approximation: ∂⟨M⟩∂θl=r[⟨M⟩θl+π4r−⟨M⟩θl−π4r]\frac{\partial \langle M \rangle}{\partial \theta_l} = r \left[ \langle M \rangle_{\theta_l + \frac{\pi}{4r}} - \langle M \rangle_{\theta_l - \frac{\pi}{4r}} \right] For standard Pauli rotation generators with r=1/2r = 1/2, the shift is ±π/2\pm \pi/2.

📚 Key Terms

AnsatzAnsatz

Simple: The architectural layout of gates and wires in a variational quantum circuit.
Technical: A parameterized trial wavefunction structure U(θ)∣0⟩U(\boldsymbol{\theta})|0\rangle chosen for expressibility and hardware efficiency.

Parameter Shift Rule

Simple: A formula to compute exact quantum gradients by shifting gate angles by +90 and -90 degrees.
Technical: An exact analytical gradient evaluation methodology for quantum hardware bypassing numerical finite-difference instability.

Barren Plateau

Simple: When the quantum loss landscape becomes completely flat, causing learning to freeze.
Technical: Phenomenon where gradient variance decays exponentially in qubit count Var(∂L)∈O(2−n)\text{Var}(\partial \mathcal{L}) \in \mathcal{O}(2^{-n}), preventing optimization in deep random Ansätze.

Q-Sphere Lab: Variational Quantum Circuits (VQE & PQC): The Quantum Neural Network

IBM Q-Sphere Model

Interactive 3D state vector exploration, unitary transformation gates, and Born rule collapse for Topic 8.7

Azimuth: 45°Elevation: 25°
+Z |0⟩-Z |1⟩+X |+⟩+Y |+i⟩01|ψ⟩
Q-Sphere Phase Color:
0 π/2 π 3π/2

Quantum Statevector

dim(H) = 2
|ψ⟩ = (1.000 + 0.000i)|0⟩
     + (0.000 + 0.000i)|1⟩
Polar Angle (θ):0.0° (0.000π rad)
Phase Angle (ϕ):0.0° (0.000π rad)

Born Rule Probabilities: P(x) = |⟨x|ψ⟩|²Σ P = 1.00

P(|0⟩) = |α|²100.0%
P(|1⟩) = |β|²0.0%
Quantum Measurement

Apply Single-Qubit Unitary Gates

Presets:
Continuous Polar Angle θ (0° = |0⟩, 180° = |1⟩):0.0°
Continuous Phase Angle ϕ (0° to 360°):0.0°
Recent Operations Log:
#1Initial state: |0⟩

📐 Mathematical Foundation

LaTeX KaTeX
∂⟨M⟩∂θj=⟨M⟩θj+π2−⟨M⟩θj−π22,θt+1=θt−η∇θL\frac{\partial \langle M \rangle}{\partial \theta_j} = \frac{\langle M \rangle_{\theta_j + \frac{\pi}{2}} - \langle M \rangle_{\theta_j - \frac{\pi}{2}}}{2}, \quad \boldsymbol{\theta}_{t+1} = \boldsymbol{\theta}_t - \eta \nabla_{\boldsymbol{\theta}} \mathcal{L}

What does it mean?

The Parameter-Shift Rule for Parameterized Quantum Circuits (PQCs): Computes mathematically exact partial derivatives of quantum expectations on real quantum hardware without numerical finite-difference approximation errors. By evaluating the circuit at two macro-shifts (theta_j + pi/2 and theta_j - pi/2), the exact analytical gradient is obtained directly from hardware measurements, immune to small-step shot noise.

Understanding the Symbols

Symbol / TermMeaningBeginner Interpretation
∂⟨M⟩∂θj\frac{\partial \langle M \rangle}{\partial \theta_j}Exact analytical gradientDerivative of expectation value with respect to gate parameter hetajheta_j
⟨M⟩θ+π/2\langle M \rangle_{\theta + \pi/2}Forward shifted expectationMeasurement outcome with parameter shifted forward by +90 degrees
⟨M⟩θ−π/2\langle M \rangle_{\theta - \pi/2}Backward shifted expectationMeasurement outcome with parameter shifted backward by -90 degrees
η\etaOptimizer learning rateStep size taken along the negative gradient direction

📊 Numerical Example

Optimizing a variational gate parameter currently at theta = 0.70 radians with learning rate eta = 0.20 using the Parameter-Shift Rule:

Step 1: Execute Forward Shift (θ + π/2): θ_forward = 0.70 + 1.5708 = 2.2708 radians Measure expectation on QPU: ⟨M⟩_forward = -0.6420 Step 2: Execute Backward Shift (θ - π/2): θ_backward = 0.70 - 1.5708 = -0.8708 radians Measure expectation on QPU: ⟨M⟩_backward = +0.6420 Step 3: Compute Exact Analytical Gradient: ∂⟨M⟩/∂θ = [ ⟨M⟩_forward - ⟨M⟩_backward ] / 2 = [ -0.6420 - (+0.6420) ] / 2 = -1.2840 / 2 = -0.6420 Step 4: Update Parameter via Gradient Descent: θ_new = θ - η · (∂⟨M⟩/∂θ) = 0.70 - (0.20 × -0.6420) = 0.70 + 0.1284 = 0.8284 radians

Result: Parameter theta updates from 0.70 to 0.8284 radians, stepping directly toward the optimal minimum.

⚙️ How Does It Work?

1

Forward Evaluation

Run circuit at current parameters theta; evaluate loss.

2

Parameter Shift Forward

Execute circuit with theta_j + pi/2.

3

Parameter Shift Backward

Execute circuit with theta_j - pi/2.

4

Classical Step

Subtract evaluations to compute exact gradient; step theta via Adam optimizer.

🌍 Where Is It Used?

Variational Quantum Eigensolver (VQE)

Problem: Finding the ground-state molecular bonding energy of hydrogen (H2).

Solution: Parameter-shift gradients optimize circuit angles until energy expectation matches the true chemical ground state.

🎯 Try It Yourself

Why is the Parameter Shift Rule preferred over classical numerical finite-difference approximation ((f(x+h) - f(x)) / h) on quantum hardware?

Interactive activity component

🐍 Python Hands-On: Constructing Parameterized Ansatzes with RealAmplitudes in Qiskit

Construct hardware-efficient variational ansatz circuits in Qiskit with parameterized rotation and entanglement layers.

Open Colab

1Install Required Packages

For Google Colab or Jupyter notebooks, run this shell command to install the required dependencies:

Terminal / Notebook Install Commandbash
!pip install qiskit
Packages:
qiskit

2Import Libraries

Import Statementspython
1from qiskit.circuit.library import RealAmplitudes, TwoLocal

from qiskit.circuit.library import RealAmplitudes, TwoLocal → Standard Qiskit variational ansatzes

3Demonstration Code & Logic

Python Execution Codepython
1# 1. RealAmplitudes Ansatz: Alternating Ry rotations and CNOT entanglers
2ansatz_real = RealAmplitudes(num_qubits=3, reps=1, entanglement='linear')
3print('=== 1. RealAmplitudes Variational Ansatz ===')
4print(ansatz_real.decompose().draw('text'))
5print(f'Number of trainable parameters: {ansatz_real.num_parameters}')
6
7# 2. TwoLocal Ansatz: Flexible Ry-Rz rotations with full entanglement
8ansatz_twolocal = TwoLocal(num_qubits=3, rotation_blocks=['ry', 'rz'], entanglement_blocks='cz', reps=1)
9print('\n=== 2. TwoLocal Variational Ansatz (Ry + Rz + CZ) ===')
10print(ansatz_twolocal.decompose().draw('text'))
11print(f'Number of trainable parameters: {ansatz_twolocal.num_parameters}')
Step-by-Step Code Breakdown:

# 1. RealAmplitudes Ansatz: Alternating Ry rotations and CNOT entanglers

RealAmplitudes

ansatz_real = RealAmplitudes(num_qubits=3, reps=1, entanglement='linear')

3 qubits, 1 layer

print('=== 1. RealAmplitudes Variational Ansatz ===')

Header 1

print(ansatz_real.decompose().draw('text'))

Draw circuit

print(f'Number of trainable parameters: {ansatz_real.num_parameters}')

Count params

# 2. TwoLocal Ansatz: Flexible Ry-Rz rotations with full entanglement

TwoLocal

ansatz_twolocal = TwoLocal(num_qubits=3, rotation_blocks=['ry', 'rz'], entanglement_blocks='cz', reps=1)

TwoLocal

print('\n=== 2. TwoLocal Variational Ansatz (Ry + Rz + CZ) ===')

Header 2

print(ansatz_twolocal.decompose().draw('text'))

Draw circuit

print(f'Number of trainable parameters: {ansatz_twolocal.num_parameters}')

Count params

📋 Complete Script (All-in-One)

Click "Copy for Colab", open Google Colab, paste into a single cell, and run.

Google Colab Notebook Scriptpython
1# =========================================================
2# Constructing Parameterized Ansatzes with RealAmplitudes in Qiskit
3# Prepared for Google Colab (Run with Shift + Enter)
4# =========================================================
5
6# Step 1: Install required packages
7!pip install qiskit
8
9# Step 2: Import libraries
10from qiskit.circuit.library import RealAmplitudes, TwoLocal
11
12# Step 3: Run Demonstration
13# 1. RealAmplitudes Ansatz: Alternating Ry rotations and CNOT entanglers
14ansatz_real = RealAmplitudes(num_qubits=3, reps=1, entanglement='linear')
15print('=== 1. RealAmplitudes Variational Ansatz ===')
16print(ansatz_real.decompose().draw('text'))
17print(f'Number of trainable parameters: {ansatz_real.num_parameters}')
18
19# 2. TwoLocal Ansatz: Flexible Ry-Rz rotations with full entanglement
20ansatz_twolocal = TwoLocal(num_qubits=3, rotation_blocks=['ry', 'rz'], entanglement_blocks='cz', reps=1)
21print('\n=== 2. TwoLocal Variational Ansatz (Ry + Rz + CZ) ===')
22print(ansatz_twolocal.decompose().draw('text'))
23print(f'Number of trainable parameters: {ansatz_twolocal.num_parameters}')
⚡ How to Run in Google Colab (3 Quick Steps)
  1. Click "Copy for Google Colab" above.
  2. Click Open New Google Colab Notebook (opens in a new tab).
  3. Click on the first empty code cell, press Ctrl + V (or Cmd + V) to paste, then press Shift + Enter to execute!

🔄 Algorithmic Execution Flow

1
RealAmplitudes Architecture

Builds layers of real-valued single-qubit rotations followed by entangling CNOTs.

→
2
TwoLocal Architecture

Constructs expressive multi-rotation parameterized circuits with CZ entanglement.

→
3
Parameter Auditing

Inspect parameter counts and ensure circuit depth fits within hardware coherence limits.

📥 Input / Initial State
3-qubit variational circuit with linear and full entanglement topologies.
📤 Expected Output
RealAmplitudes: 3 wires with Ry layers and linear CNOTs (6 parameters).
TwoLocal: 3 wires with Ry, Rz layers and CZ gates (12 parameters).
💡 What Does This Demonstration Show?

Variational ansatzes are the quantum analogue of neural network layers. Their expressive power and parameter efficiency determine whether a hybrid quantum algorithm can learn without getting trapped in barren plateaus.

Course Learning Resource • Slide DeckLesson 1011 Slides

Lesson 10: Variational Quantum Classification

Feature maps, ansatz, COBYLA optimization, and complete VQC pipeline.

Author: Dr. Syed Muzamil Basha• FDP Approved Curriculum
Open in New Tab

📝 Learning Check

Test your understanding with these questions. Remember, this is for learning, not examination!

Question 1: What is the "Barren Plateau" problem in deep Variational Quantum Circuits?

Question 2: Placeholder question 2

Question 3: Placeholder question 3

Question 4: Placeholder question 4

Question 5: Placeholder question 5