Launch the interactive walkthrough to discover each component, including the 3D Q-Sphere Lab & Python in Colab.
Master Parameterized Quantum Circuits (Ansätze), classical optimization loops, and the Parameter Shift Rule for computing quantum gradients.
(Forward - Backward) / 2!
- That gives you the exact slope to adjust your dial down the loss mountain!Interactive 3D state vector exploration, unitary transformation gates, and Born rule collapse for Topic 8.7
| Symbol / Term | Meaning | Beginner Interpretation |
|---|---|---|
| Exact analytical gradient | Derivative of expectation value with respect to gate parameter | |
| Forward shifted expectation | Measurement outcome with parameter shifted forward by +90 degrees | |
| Backward shifted expectation | Measurement outcome with parameter shifted backward by -90 degrees | |
| Optimizer learning rate | Step size taken along the negative gradient direction |
Optimizing a variational gate parameter currently at theta = 0.70 radians with learning rate eta = 0.20 using the Parameter-Shift Rule:
Result: Parameter theta updates from 0.70 to 0.8284 radians, stepping directly toward the optimal minimum.
Run circuit at current parameters theta; evaluate loss.
Execute circuit with theta_j + pi/2.
Execute circuit with theta_j - pi/2.
Subtract evaluations to compute exact gradient; step theta via Adam optimizer.
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.
Interactive activity component
Construct hardware-efficient variational ansatz circuits in Qiskit with parameterized rotation and entanglement layers.
For Google Colab or Jupyter notebooks, run this shell command to install the required dependencies:
!pip install qiskit1 from qiskit.circuit.library import RealAmplitudes, TwoLocal
from qiskit.circuit.library import RealAmplitudes, TwoLocal → Standard Qiskit variational ansatzes
1 # 1. RealAmplitudes Ansatz: Alternating Ry rotations and CNOT entanglers 2 ansatz_real = RealAmplitudes(num_qubits=3, reps=1, entanglement='linear') 3 print('=== 1. RealAmplitudes Variational Ansatz ===') 4 print(ansatz_real.decompose().draw('text')) 5 print(f'Number of trainable parameters: {ansatz_real.num_parameters}') 6 7 # 2. TwoLocal Ansatz: Flexible Ry-Rz rotations with full entanglement 8 ansatz_twolocal = TwoLocal(num_qubits=3, rotation_blocks=['ry', 'rz'], entanglement_blocks='cz', reps=1) 9 print('\n=== 2. TwoLocal Variational Ansatz (Ry + Rz + CZ) ===') 10 print(ansatz_twolocal.decompose().draw('text')) 11 print(f'Number of trainable parameters: {ansatz_twolocal.num_parameters}')
# 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
Click "Copy for Colab", open Google Colab, paste into a single cell, and run.
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 10 from qiskit.circuit.library import RealAmplitudes, TwoLocal 11 12 # Step 3: Run Demonstration 13 # 1. RealAmplitudes Ansatz: Alternating Ry rotations and CNOT entanglers 14 ansatz_real = RealAmplitudes(num_qubits=3, reps=1, entanglement='linear') 15 print('=== 1. RealAmplitudes Variational Ansatz ===') 16 print(ansatz_real.decompose().draw('text')) 17 print(f'Number of trainable parameters: {ansatz_real.num_parameters}') 18 19 # 2. TwoLocal Ansatz: Flexible Ry-Rz rotations with full entanglement 20 ansatz_twolocal = TwoLocal(num_qubits=3, rotation_blocks=['ry', 'rz'], entanglement_blocks='cz', reps=1) 21 print('\n=== 2. TwoLocal Variational Ansatz (Ry + Rz + CZ) ===') 22 print(ansatz_twolocal.decompose().draw('text')) 23 print(f'Number of trainable parameters: {ansatz_twolocal.num_parameters}')
Builds layers of real-valued single-qubit rotations followed by entangling CNOTs.
Constructs expressive multi-rotation parameterized circuits with CZ entanglement.
Inspect parameter counts and ensure circuit depth fits within hardware coherence limits.
3-qubit variational circuit with linear and full entanglement topologies.
RealAmplitudes: 3 wires with Ry layers and linear CNOTs (6 parameters). TwoLocal: 3 wires with Ry, Rz layers and CZ gates (12 parameters).
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.
Feature maps, ansatz, COBYLA optimization, and complete VQC pipeline.
Test your understanding with these questions. Remember, this is for learning, not examination!