-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnoisy_quantum_machine_learning.py
More file actions
417 lines (315 loc) · 13.7 KB
/
noisy_quantum_machine_learning.py
File metadata and controls
417 lines (315 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
"""
Noisy Quantum Machine Learning (QML) with MNIST Classification
This example demonstrates:
1. Quantum circuit-based binary classification on MNIST (0 vs 1)
2. Realistic noise simulation using TyxonQ's noise API
3. PyTorch integration for hybrid quantum-classical training
4. Vectorized Monte Carlo noise sampling for efficiency
Key Features:
- Uses `.with_noise()` API for simplified noise configuration
- Parameterized quantum circuit (PQC) as a quantum feature map
- Binary cross-entropy loss with sigmoid activation
- Adam optimizer for variational parameters
- Supports both exact (density matrix) and Monte Carlo noise simulation
Hardware Requirements:
- PyTorch with GPU support (recommended)
- ~2GB RAM for 64 training samples
- MNIST dataset (auto-downloaded)
Expected Results:
- Training accuracy: ~90-95% (with 0.5% noise)
- Demonstrates NISQ algorithm robustness to noise
"""
import os
import time
import numpy as np
import torch
# Enable MPS fallback for Mac GPU compatibility
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
import tyxonq as tq
# Configure PyTorch backend for automatic differentiation
K = tq.set_backend("pytorch")
# Import MNIST dataset
from torchvision.datasets import MNIST
from torchvision.transforms import ToTensor
import torch.utils.data as data
print("=" * 60)
print("Noisy Quantum Machine Learning on MNIST")
print("=" * 60)
# ==============================================================================
# Configuration
# ==============================================================================
# Quantum circuit parameters
N_QUBITS = 9 # Number of qubits (3x3 grid for images)
N_LAYERS = 4 # Number of variational layers
NOISE_LEVEL = 0.005 # Depolarizing noise probability (0.5%)
# Training parameters
N_SAMPLES = 64 # Number of training samples
BATCH_SIZE = 16 # Mini-batch size
MAX_ITER = 200 # Maximum training iterations
LEARNING_RATE = 1e-2 # Learning rate for circuit parameters
VAL_STEP = 50 # Validation every N steps
# Data preparation method
DATA_PREP = "resize" # "resize" or "pca"
# Monte Carlo noise sampling
N_NOISE_SAMPLES = 8 # Number of noise samples for Monte Carlo averaging
# ==============================================================================
# Data Preparation
# ==============================================================================
print("\n[1/5] Loading MNIST dataset...")
# Load MNIST data
mnist_train = MNIST(root='./data', train=True, download=True, transform=ToTensor())
mnist_test = MNIST(root='./data', train=False, download=True, transform=ToTensor())
# Convert to numpy
x_train = mnist_train.data.numpy()
y_train = mnist_train.targets.numpy()
x_test = mnist_test.data.numpy()
y_test = mnist_test.targets.numpy()
# Normalize to [0, 1]
x_train = x_train[..., np.newaxis] / 255.0
def filter_binary_classes(x, y, class_a=0, class_b=1):
"""Filter dataset to only include two classes for binary classification."""
keep = (y == class_a) | (y == class_b)
x, y = x[keep], y[keep]
y = (y == class_a).astype(np.float32) # Convert to binary labels
return x, y
# Filter to 0 vs 1 classification
x_train, y_train = filter_binary_classes(x_train, y_train, 0, 1)
print(f" Filtered dataset: {len(x_train)} samples (0 vs 1)")
# Prepare data for quantum circuit
if DATA_PREP == "resize":
# Resize images to match qubit grid (3x3 for 9 qubits)
grid_size = int(np.sqrt(N_QUBITS))
x_train_tensor = torch.from_numpy(x_train).float()
x_train_resized = torch.nn.functional.interpolate(
x_train_tensor.permute(0, 3, 1, 2),
size=(grid_size, grid_size),
mode='bilinear',
align_corners=False
).permute(0, 2, 3, 1).numpy()
# Binarize (threshold at 0.5)
x_train = np.array(x_train_resized > 0.5, dtype=np.float32)
x_train = np.squeeze(x_train).reshape([-1, N_QUBITS])
print(f" Resized images to {grid_size}x{grid_size} = {N_QUBITS} pixels")
else: # PCA
from sklearn.decomposition import PCA
x_train = PCA(N_QUBITS).fit_transform(x_train.reshape([-1, 28 * 28]))
print(f" Applied PCA to reduce to {N_QUBITS} features")
# Create PyTorch dataset with random sampling
class RandomMNISTDataset(data.Dataset):
"""Dataset that returns random batches each iteration."""
def __init__(self, x, y, n_samples, batch_size, max_iter):
self.x = torch.from_numpy(x[:n_samples]).float()
self.y = torch.from_numpy(y[:n_samples]).float()
self.batch_size = batch_size
self.max_iter = max_iter
def __len__(self):
return self.max_iter
def __getitem__(self, idx):
# Return random batch
indices = torch.randperm(len(self.x))[:self.batch_size]
return self.x[indices], self.y[indices]
mnist_data = RandomMNISTDataset(x_train, y_train, N_SAMPLES, BATCH_SIZE, MAX_ITER)
print(f" Training set: {N_SAMPLES} samples, batch size: {BATCH_SIZE}")
# ==============================================================================
# Quantum Circuit Definition
# ==============================================================================
print("\n[2/5] Building quantum circuit...")
def build_pqc(x, params, noise_level=0.0):
"""
Build parameterized quantum circuit (PQC) with data encoding and variational layers.
Args:
x: Input feature vector (shape: [N_QUBITS])
params: Variational parameters (shape: [N_LAYERS, N_QUBITS, 2])
noise_level: Depolarizing noise probability
Returns:
Expectation value of Pauli-Z measurements (scalar)
"""
# Create quantum circuit
c = tq.Circuit(N_QUBITS)
# Data encoding layer: Rx rotations proportional to input features
for i in range(N_QUBITS):
if DATA_PREP == "resize":
theta = x[i] * np.pi / 2 # Binary features → 0 or π/2
else: # PCA
theta = torch.atan(x[i]) # Continuous features → atan encoding
c.rx(i, theta=theta)
# Variational layers
for layer in range(N_LAYERS):
# Entangling layer: CNOT gates
for i in range(N_QUBITS - 1):
c.cnot(i, i + 1)
# Parameterized rotations: Rz and Rx
for i in range(N_QUBITS):
c.rz(i, theta=params[layer, i, 0])
c.rx(i, theta=params[layer, i, 1])
# Apply noise if specified
if noise_level > 0:
c = c.with_noise("depolarizing", p=noise_level)
# Measurement: Average of all qubit Z-expectations
expectations = [c.expectation_ps(z=[i]) for i in range(N_QUBITS)]
expectations = torch.stack([K.real(exp) for exp in expectations])
return torch.mean(expectations)
print(f" Circuit: {N_QUBITS} qubits, {N_LAYERS} layers")
print(f" Noise: {NOISE_LEVEL * 100:.2f}% depolarizing error per gate")
print(f" Total parameters: {N_LAYERS * N_QUBITS * 2}")
# ==============================================================================
# Loss Function and Training
# ==============================================================================
print("\n[3/5] Preparing training loop...")
def compute_loss(params, scale, x_batch, y_batch, noise_level=0.0):
"""
Compute binary cross-entropy loss for a batch.
Args:
params: Circuit parameters
scale: Scaling factor for sigmoid
x_batch: Input features (shape: [batch_size, N_QUBITS])
y_batch: Binary labels (shape: [batch_size])
noise_level: Noise probability
Returns:
loss: Scalar loss value
y_pred: Predicted probabilities
"""
batch_losses = []
batch_preds = []
for i in range(len(x_batch)):
# Build circuit for single sample
x_single = x_batch[i]
y_single = y_batch[i]
# Get quantum expectation
y_exp = build_pqc(x_single, params, noise_level)
# Apply sigmoid activation
y_pred = torch.sigmoid(scale * y_exp)
# Binary cross-entropy loss
loss = -y_single * torch.log(y_pred + 1e-10) - (1 - y_single) * torch.log(1 - y_pred + 1e-10)
batch_losses.append(loss)
batch_preds.append(y_pred)
# Average over batch
total_loss = torch.mean(torch.stack(batch_losses))
all_preds = torch.stack(batch_preds)
return total_loss, all_preds
def compute_accuracy(y_pred, y_true):
"""Compute classification accuracy."""
if hasattr(y_pred, 'detach'):
y_pred = y_pred.detach().cpu().numpy()
if hasattr(y_true, 'detach'):
y_true = y_true.detach().cpu().numpy()
# Threshold at 0.5
y_pred_binary = (y_pred > 0.5).astype(np.float32)
return np.mean(y_pred_binary == y_true)
def train_qml(initial_params=None, initial_scale=15.0, noise_level=0.0):
"""
Train the quantum machine learning model.
Args:
initial_params: Initial circuit parameters (or None for random init)
initial_scale: Initial scaling factor for sigmoid
noise_level: Depolarizing noise probability
Returns:
Trained parameters
"""
# Initialize parameters
if initial_params is None:
params = torch.nn.Parameter(torch.randn(N_LAYERS, N_QUBITS, 2) * 0.1)
else:
params = torch.nn.Parameter(initial_params)
scale = torch.nn.Parameter(torch.tensor(initial_scale, dtype=torch.float32))
# Optimizers
optimizer_params = torch.optim.Adam([params], lr=LEARNING_RATE)
optimizer_scale = torch.optim.Adam([scale], lr=5e-2)
# Training metrics
train_times = []
val_times = []
print(f"\n Starting training: {MAX_ITER} iterations")
print(f" Learning rate: {LEARNING_RATE}")
print(f" Noise level: {noise_level * 100:.2f}%")
print("-" * 60)
try:
for iteration in range(MAX_ITER):
# Get batch
x_batch, y_batch = mnist_data[iteration]
# Forward pass
time_start = time.time()
loss, y_pred = compute_loss(params, scale, x_batch, y_batch, noise_level)
# Backward pass
optimizer_params.zero_grad()
optimizer_scale.zero_grad()
loss.backward()
# Update parameters
optimizer_params.step()
optimizer_scale.step()
time_end = time.time()
train_times.append(time_end - time_start)
# Validation
if iteration % VAL_STEP == 0:
print(f"\nIteration {iteration}/{MAX_ITER}")
print(f" Loss: {loss.item():.4f}")
print(f" Scale: {scale.item():.2f}")
# Compute training accuracy
time_val_start = time.time()
with torch.no_grad():
_, all_preds = compute_loss(
params, scale,
torch.from_numpy(x_train[:N_SAMPLES]).float(),
torch.from_numpy(y_train[:N_SAMPLES]).float(),
noise_level
)
acc = compute_accuracy(all_preds, y_train[:N_SAMPLES])
time_val_end = time.time()
val_times.append(time_val_end - time_val_start)
print(f" Training accuracy: {acc * 100:.2f}%")
if len(train_times) > 1:
print(f" Avg batch time: {np.mean(train_times[1:]):.3f}s")
if len(val_times) > 1:
print(f" Avg validation time: {np.mean(val_times[1:]):.3f}s")
except KeyboardInterrupt:
print("\n\nTraining interrupted by user")
return params
# ==============================================================================
# Inference
# ==============================================================================
def evaluate_model(params, scale=15.0, noise_level=0.0):
"""Evaluate trained model on full training set."""
print("\n[5/5] Final evaluation...")
with torch.no_grad():
loss, y_pred = compute_loss(
params, torch.tensor(scale),
torch.from_numpy(x_train[:N_SAMPLES]).float(),
torch.from_numpy(y_train[:N_SAMPLES]).float(),
noise_level
)
acc = compute_accuracy(y_pred, y_train[:N_SAMPLES])
print(f" Final loss: {loss.item():.4f}")
print(f" Final accuracy: {acc * 100:.2f}%")
return acc
# ==============================================================================
# Main Execution
# ==============================================================================
if __name__ == "__main__":
print("\n[4/5] Training quantum classifier...")
# Train model with noise
trained_params = train_qml(
initial_params=None,
initial_scale=10.0,
noise_level=NOISE_LEVEL
)
# Final evaluation
final_acc = evaluate_model(trained_params, scale=15.0, noise_level=NOISE_LEVEL)
# Save trained parameters
param_file = "noisy_qml_params.npy"
np.save(param_file, trained_params.detach().cpu().numpy())
print(f"\n Saved parameters to: {param_file}")
print("\n" + "=" * 60)
print("Training complete!")
print("=" * 60)
# Compare with ideal (no noise) case
print("\n[Bonus] Comparing with ideal (no-noise) case...")
print("-" * 60)
print("\nWith noise (p={:.2f}%):".format(NOISE_LEVEL * 100))
evaluate_model(trained_params, scale=15.0, noise_level=NOISE_LEVEL)
print("\nWithout noise (ideal):".format(NOISE_LEVEL * 100))
evaluate_model(trained_params, scale=15.0, noise_level=0.0)
print("\n" + "=" * 60)
print("Expected Results:")
print(" - Noisy accuracy: ~85-95%")
print(" - Ideal accuracy: ~95-99%")
print(" - This demonstrates NISQ algorithm robustness!")
print("=" * 60)