|
| 1 | +""" |
| 2 | +quantum part in tensorflow or jax, neural part in torch, both on GPU, |
| 3 | +fantastic hybrid pipeline |
| 4 | +""" |
| 5 | + |
| 6 | +import os |
| 7 | + |
| 8 | +os.environ["TF_FORCE_GPU_ALLOW_GROWTH"] = "true" |
| 9 | +import time |
| 10 | +import numpy as np |
| 11 | +import tensorflow as tf |
| 12 | +import torch |
| 13 | +import tensorcircuit as tc |
| 14 | + |
| 15 | +K = tc.set_backend("tensorflow") |
| 16 | + |
| 17 | +if torch.cuda.is_available(): |
| 18 | + device = torch.device("cuda") |
| 19 | +else: |
| 20 | + device = torch.device("cpu") |
| 21 | + |
| 22 | + |
| 23 | +print(device) |
| 24 | + |
| 25 | +# dataset preparation |
| 26 | + |
| 27 | +(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data() |
| 28 | +x_train = x_train[..., np.newaxis] / 255.0 |
| 29 | + |
| 30 | + |
| 31 | +def filter_pair(x, y, a, b): |
| 32 | + keep = (y == a) | (y == b) |
| 33 | + x, y = x[keep], y[keep] |
| 34 | + y = y == a |
| 35 | + return x, y |
| 36 | + |
| 37 | + |
| 38 | +x_train, y_train = filter_pair(x_train, y_train, 1, 5) |
| 39 | +x_train_small = tf.image.resize(x_train, (3, 3)).numpy() |
| 40 | +x_train_bin = np.array(x_train_small > 0.5, dtype=np.float32) |
| 41 | +x_train_bin = np.squeeze(x_train_bin).reshape([-1, 9]) |
| 42 | +y_train_torch = torch.tensor(y_train, dtype=torch.float32) |
| 43 | +x_train_torch = torch.tensor(x_train_bin) |
| 44 | +x_train_torch = x_train_torch.to(device=device) |
| 45 | +y_train_torch = y_train_torch.to(device=device) |
| 46 | + |
| 47 | +n = 9 |
| 48 | +nlayers = 3 |
| 49 | + |
| 50 | +# We define the quantum function, |
| 51 | +# note how this function is running on tensorflow |
| 52 | + |
| 53 | + |
| 54 | +def qpreds(x, weights): |
| 55 | + c = tc.Circuit(n) |
| 56 | + for i in range(n): |
| 57 | + c.rx(i, theta=x[i]) |
| 58 | + for j in range(nlayers): |
| 59 | + for i in range(n - 1): |
| 60 | + c.cnot(i, i + 1) |
| 61 | + for i in range(n): |
| 62 | + c.rx(i, theta=weights[2 * j, i]) |
| 63 | + c.ry(i, theta=weights[2 * j + 1, i]) |
| 64 | + |
| 65 | + return K.stack([K.real(c.expectation_ps(z=[i])) for i in range(n)]) |
| 66 | + |
| 67 | + |
| 68 | +# qpreds_vmap = K.vmap(qpreds, vectorized_argnums=0) |
| 69 | +# qpreds_batch = tc.interfaces.torch_interface(qpreds_vmap, jit=True, enable_dlpack=True) |
| 70 | + |
| 71 | +quantumnet = tc.TorchLayer( |
| 72 | + qpreds, |
| 73 | + weights_shape=[2 * nlayers, n], |
| 74 | + use_vmap=True, |
| 75 | + use_interface=True, |
| 76 | + use_jit=True, |
| 77 | + enable_dlpack=True, |
| 78 | +) |
| 79 | + |
| 80 | + |
| 81 | +model = torch.nn.Sequential(quantumnet, torch.nn.Linear(9, 1), torch.nn.Sigmoid()) |
| 82 | +model = model.to(device=device) |
| 83 | + |
| 84 | + |
| 85 | +criterion = torch.nn.BCELoss() |
| 86 | +opt = torch.optim.Adam(model.parameters(), lr=1e-2) |
| 87 | +nepochs = 300 |
| 88 | +nbatch = 32 |
| 89 | +times = [] |
| 90 | +for epoch in range(nepochs): |
| 91 | + index = np.random.randint(low=0, high=100, size=nbatch) |
| 92 | + # index = np.arange(nbatch) |
| 93 | + inputs, labels = x_train_torch[index], y_train_torch[index] |
| 94 | + opt.zero_grad() |
| 95 | + |
| 96 | + with torch.set_grad_enabled(True): |
| 97 | + time0 = time.time() |
| 98 | + yps = model(inputs) |
| 99 | + loss = criterion( |
| 100 | + torch.reshape(yps, [nbatch, 1]), torch.reshape(labels, [nbatch, 1]) |
| 101 | + ) |
| 102 | + loss.backward() |
| 103 | + if epoch % 100 == 0: |
| 104 | + print(loss) |
| 105 | + opt.step() |
| 106 | + time1 = time.time() |
| 107 | + times.append(time1 - time0) |
| 108 | +print("training time per step: ", np.mean(times[1:])) |
0 commit comments