|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "id": "32980464", |
| 7 | + "metadata": { |
| 8 | + "ExecuteTime": { |
| 9 | + "end_time": "2024-11-18T15:53:19.909394Z", |
| 10 | + "start_time": "2024-11-18T15:53:18.779242Z" |
| 11 | + } |
| 12 | + }, |
| 13 | + "outputs": [], |
| 14 | + "source": [ |
| 15 | + "#!sudo /opt/conda/bin/conda-develop -n QML-QPF PATH /workspaces/QML-QPF/mosaiQue" |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "code", |
| 20 | + "execution_count": null, |
| 21 | + "id": "cd08081e", |
| 22 | + "metadata": {}, |
| 23 | + "outputs": [], |
| 24 | + "source": [ |
| 25 | + "\n", |
| 26 | + "#import tensorflow as tf\n", |
| 27 | + "#from tensorflow import keras\n", |
| 28 | + "import mosaique as mq\n", |
| 29 | + "from concurrent.futures import ProcessPoolExecutor, as_completed\n", |
| 30 | + "import itertools\n", |
| 31 | + "import numpy as np\n", |
| 32 | + "import pennylane as qml\n", |
| 33 | + "import os\n", |
| 34 | + "import time\n", |
| 35 | + "import datetime\n", |
| 36 | + "from tensorflow import keras\n", |
| 37 | + "from mosaique.models.operation import OperationLayer\n", |
| 38 | + "os.environ['TF_GPU_ALLOCATOR'] = 'cuda_malloc_async'\n", |
| 39 | + "\n", |
| 40 | + "fashionmnist_dataset = keras.datasets.fashion_mnist\n", |
| 41 | + "train_layer = mq.ConvolutionLayer4x4(\"fashionmnist_train\")\n", |
| 42 | + "test_layer = mq.ConvolutionLayer4x4(\"fashionmnist_test\")\n", |
| 43 | + "(train_images, train_labels), (test_images, test_labels) = fashionmnist_dataset.load_data()\n", |
| 44 | + "train_layer.fit(train_images)\n", |
| 45 | + "test_layer.fit(test_images)\n", |
| 46 | + "train_images = train_layer.transform(train_images)\n", |
| 47 | + "test_images = test_layer.transform(test_images)\n", |
| 48 | + "train_images = train_layer.post_transform(train_images.transpose((0,2,1)))\n", |
| 49 | + "test_images = test_layer.post_transform(test_images.transpose((0,2,1)))" |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "code", |
| 54 | + "execution_count": null, |
| 55 | + "id": "a62fb6d5", |
| 56 | + "metadata": {}, |
| 57 | + "outputs": [], |
| 58 | + "source": [ |
| 59 | + "def run(tr_images, te_images, label):\n", |
| 60 | + " log_dir = train_layer.name + \"/run8/\" + label\n", |
| 61 | + " tensorboard_callback = keras.callbacks.TensorBoard(\n", |
| 62 | + " log_dir=log_dir,\n", |
| 63 | + " histogram_freq=1,\n", |
| 64 | + " write_graph=True,\n", |
| 65 | + " write_images=True,\n", |
| 66 | + " write_steps_per_second=True,\n", |
| 67 | + " update_freq='batch',\n", |
| 68 | + " profile_batch=1,\n", |
| 69 | + " embeddings_freq=1,\n", |
| 70 | + " embeddings_metadata=None\n", |
| 71 | + " )\n", |
| 72 | + " q_model = keras.models.Sequential([\n", |
| 73 | + " keras.layers.Rescaling(scale=-1. / 127.5, offset=1),\n", |
| 74 | + " keras.layers.Flatten(),\n", |
| 75 | + " keras.layers.Dense(10, activation=\"softmax\")\n", |
| 76 | + " ])\n", |
| 77 | + " q_model.compile(\n", |
| 78 | + " optimizer='adam',\n", |
| 79 | + " loss=\"sparse_categorical_crossentropy\",\n", |
| 80 | + " metrics=[\"accuracy\"],\n", |
| 81 | + " )\n", |
| 82 | + "\n", |
| 83 | + " q_history = q_model.fit(\n", |
| 84 | + " tr_images,\n", |
| 85 | + " train_labels,\n", |
| 86 | + " validation_data=(te_images, test_labels),\n", |
| 87 | + " batch_size=128,\n", |
| 88 | + " epochs=30,\n", |
| 89 | + " verbose=2,\n", |
| 90 | + " callbacks=[tensorboard_callback]\n", |
| 91 | + " )\n", |
| 92 | + "\n", |
| 93 | + "def model(variant, tr_layer, te_layer):\n", |
| 94 | + " tr_images = tr_layer.open(variant)\n", |
| 95 | + " te_images = te_layer.open(variant)\n", |
| 96 | + "\n", |
| 97 | + " label = ''.join(map(str,variant))\n", |
| 98 | + "\n", |
| 99 | + " run(tr_images, te_images, label)" |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "cell_type": "code", |
| 104 | + "execution_count": null, |
| 105 | + "id": "ab5a65bf", |
| 106 | + "metadata": {}, |
| 107 | + "outputs": [], |
| 108 | + "source": [ |
| 109 | + "permutations = np.asarray(list(itertools.permutations(range(4))))\n", |
| 110 | + "\n", |
| 111 | + "#[model(variant = p, tr_layer = train_layer, te_layer= test_layer) for p in permutations[:1]]" |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "code", |
| 116 | + "execution_count": null, |
| 117 | + "id": "80a45f19", |
| 118 | + "metadata": {}, |
| 119 | + "outputs": [], |
| 120 | + "source": [ |
| 121 | + "from matplotlib import pyplot as plt\n", |
| 122 | + "\n", |
| 123 | + "print(train_layer.open([0,1,2,3]).shape)\n", |
| 124 | + "\n", |
| 125 | + "post = train_layer.open([0,1,2,3])\n", |
| 126 | + "\n", |
| 127 | + "_min, _max = np.amin(post), np.amax(post)\n", |
| 128 | + "fig, axes = plt.subplots(1, 4, figsize=(16, 4))\n", |
| 129 | + "\n", |
| 130 | + "# Plot all output channels for quantum cnot\n", |
| 131 | + "for c in range(4):\n", |
| 132 | + " axes[c].imshow(post[0,:,:,c],vmin = _min, vmax = _max)" |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "code", |
| 137 | + "execution_count": null, |
| 138 | + "id": "031c0127", |
| 139 | + "metadata": {}, |
| 140 | + "outputs": [], |
| 141 | + "source": [ |
| 142 | + "plt.imshow((train_layer.channel_merge(post))[0,:,:],vmin = _min, vmax = _max)" |
| 143 | + ] |
| 144 | + }, |
| 145 | + { |
| 146 | + "cell_type": "code", |
| 147 | + "execution_count": null, |
| 148 | + "id": "beb165f6", |
| 149 | + "metadata": {}, |
| 150 | + "outputs": [], |
| 151 | + "source": [ |
| 152 | + "\n", |
| 153 | + "\n", |
| 154 | + "for j in range(3):\n", |
| 155 | + " with ProcessPoolExecutor(8) as executor:\n", |
| 156 | + " runner = {\n", |
| 157 | + " executor.submit(model,variant = p, tr_layer = train_layer, te_layer= test_layer): p for p in permutations[8*j:8*(j+1)]\n", |
| 158 | + " }\n", |
| 159 | + " for future in as_completed(runner):\n", |
| 160 | + " runner.pop(future)\n", |
| 161 | + "# 1 min 8 sec" |
| 162 | + ] |
| 163 | + }, |
| 164 | + { |
| 165 | + "cell_type": "code", |
| 166 | + "execution_count": null, |
| 167 | + "id": "12b2c91c", |
| 168 | + "metadata": {}, |
| 169 | + "outputs": [], |
| 170 | + "source": [ |
| 171 | + "run(train_images,test_images,\"NO-FILTER\")" |
| 172 | + ] |
| 173 | + } |
| 174 | + ], |
| 175 | + "metadata": { |
| 176 | + "kernelspec": { |
| 177 | + "display_name": "QML-QPF", |
| 178 | + "language": "python", |
| 179 | + "name": "python3" |
| 180 | + }, |
| 181 | + "language_info": { |
| 182 | + "codemirror_mode": { |
| 183 | + "name": "ipython", |
| 184 | + "version": 3 |
| 185 | + }, |
| 186 | + "file_extension": ".py", |
| 187 | + "mimetype": "text/x-python", |
| 188 | + "name": "python", |
| 189 | + "nbconvert_exporter": "python", |
| 190 | + "pygments_lexer": "ipython3", |
| 191 | + "version": "3.10.13" |
| 192 | + } |
| 193 | + }, |
| 194 | + "nbformat": 4, |
| 195 | + "nbformat_minor": 5 |
| 196 | +} |
0 commit comments