Launch the interactive walkthrough to discover each component, including the 3D Q-Sphere Lab & Python in Colab.
Design and integrate end-to-end architectures combining classical deep learning backbones with quantum variational processing units.
Interactive 3D state vector exploration, unitary transformation gates, and Born rule collapse for Topic 9.4
| Symbol / Term | Meaning | Beginner Interpretation |
|---|---|---|
| Task loss gradient vector | Direction and rate of fastest loss decrease with respect to quantum parameters | |
| Classical loss gradient | Gradient backpropagated through classical neural layers to quantum measurement head | |
| Unit basis vector | Indicates parameter shift applied specifically to the -th gate angle | |
| Adam moment vectors | Running averages of gradient and squared gradient smoothing out quantum shot noise |
Updating a quantum parameter with Adam optimizer given previous momentum m_{t-1} = 0.10, variance v_{t-1} = 0.04, learning rate eta = 0.05, and evaluated quantum gradient g_t = 0.40:
Result: The quantum gate angle is smoothly updated to 1.4676 radians, damped against shot-noise fluctuations by Adam momentum.
A classical ResNet or CNN processes raw input (e.g. 224x224 image) down to a compact 4- or 8-dimensional latent vector.
The latent vector is mapped into quantum angles on an 8-qubit register using parameterized rotations.
Parameterized CNOT and rotation gates create high-dimensional entangled representations.
Pauli Z expectation values are measured, passed to a final classical Softmax head, and end-to-end gradients update both networks.
Problem: Detecting subtle pre-cancerous cellular patterns in MRI scans that standard CNNs classify as noise.
Solution: CNN extracts tissue features, and a quantum circuit evaluates subtle non-linear correlations across multi-qubit entangled states.
Problem: Rapidly correlating asynchronous global market price movements under regime shifts.
Solution: Classical LSTM predicts short-term momentum; quantum layer searches for cross-asset entanglement correlations.
Interactive activity component
Define a PyTorch model containing a classical Linear layer followed by a PennyLane quantum variational layer.
For Google Colab or Jupyter notebooks, run this shell command to install the required dependencies:
!pip install pennylane torch1 import pennylane as qml 2 import torch 3 import torch.nn as nn
import pennylane as qml → Import PennyLane quantum ML library
import torch → Import PyTorch deep learning framework
import torch.nn as nn → Import neural network modules
1 dev = qml.device("default.qubit", wires=2) 2 @qml.qnode(dev, interface="torch") 3 def quantum_circuit(inputs, weights): 4 qml.AngleEmbedding(inputs, wires=[0, 1]) 5 qml.BasicEntanglerLayers(weights, wires=[0, 1]) 6 return [qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1))] 7 weight_shapes = {"weights": (2, 2)} 8 qlayer = qml.qnn.TorchLayer(quantum_circuit, weight_shapes) 9 model = nn.Sequential(nn.Linear(4, 2), nn.Tanh(), qlayer, nn.Linear(2, 1)) 10 dummy_x = torch.randn(5, 4) 11 output = model(dummy_x) 12 print("Hybrid Model Output Shape:", output.shape) 13 print("Sample Output:", output.detach().numpy())
dev = qml.device("default.qubit", wires=2)
Create 2-qubit simulator device
@qml.qnode(dev, interface="torch")
Decorate quantum function with PyTorch autograd bridge
def quantum_circuit(inputs, weights):
Define parameterized circuit
qml.AngleEmbedding(inputs, wires=[0, 1])
Encode classical inputs into rotation angles
qml.BasicEntanglerLayers(weights, wires=[0, 1])
Parameterized trainable rotation and CNOT layers
return [qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1))]
Measure Z expectations
weight_shapes = {"weights": (2, 2)}
Specify shapes for 2 layers across 2 qubits
qlayer = qml.qnn.TorchLayer(quantum_circuit, weight_shapes)
Convert PennyLane QNode into torch.nn.Module
model = nn.Sequential(nn.Linear(4, 2), nn.Tanh(), qlayer, nn.Linear(2, 1))
Construct full hybrid pipeline
dummy_x = torch.randn(5, 4)
Batch of 5 samples with 4 features
output = model(dummy_x)
Forward pass through classical and quantum layers
print("Hybrid Model Output Shape:", output.shape)
Check output tensor shape
print("Sample Output:", output.detach().numpy())
Display predictions
Click "Copy for Colab", open Google Colab, paste into a single cell, and run.
1 # ========================================================= 2 # Building a Hybrid PyTorch-PennyLane Neural Network Layer 3 # Prepared for Google Colab (Run with Shift + Enter) 4 # ========================================================= 5 6 # Step 1: Install required packages 7 !pip install pennylane torch 8 9 # Step 2: Import libraries 10 import pennylane as qml 11 import torch 12 import torch.nn as nn 13 14 # Step 3: Run Demonstration 15 dev = qml.device("default.qubit", wires=2) 16 @qml.qnode(dev, interface="torch") 17 def quantum_circuit(inputs, weights): 18 qml.AngleEmbedding(inputs, wires=[0, 1]) 19 qml.BasicEntanglerLayers(weights, wires=[0, 1]) 20 return [qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1))] 21 weight_shapes = {"weights": (2, 2)} 22 qlayer = qml.qnn.TorchLayer(quantum_circuit, weight_shapes) 23 model = nn.Sequential(nn.Linear(4, 2), nn.Tanh(), qlayer, nn.Linear(2, 1)) 24 dummy_x = torch.randn(5, 4) 25 output = model(dummy_x) 26 print("Hybrid Model Output Shape:", output.shape) 27 print("Sample Output:", output.detach().numpy())
Construct a 2-qubit circuit with angle embedding and entangling gates.
Convert the quantum circuit into a standard PyTorch module.
Chain classical dense layers before and after the quantum layer.
Batch of 5 classical vectors with 4 features each
Hybrid Model Output Shape: torch.Size([5, 1])
The input passes through a classical compression layer, through a 2-qubit entangled quantum circuit, and finally through a classical output layer, all end-to-end differentiable.
EstimatorQNN, TorchConnector, and hybrid training workflows.
Test your understanding with these questions. Remember, this is for learning, not examination!