|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "id": "a3f4079e-8dab-4a6a-b326-53c84cde572c", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "import numpy as np\n", |
| 11 | + "import matplotlib.pyplot as plt\n", |
| 12 | + "# import matplotlib.colors as mcolors\n", |
| 13 | + "import pickle\n", |
| 14 | + "import tikzplotlib\n", |
| 15 | + "from collections import defaultdict\n", |
| 16 | + "def tikzplotlib_fix_ncols(obj):\n", |
| 17 | + " \"\"\"\n", |
| 18 | + " workaround for matplotlib 3.6 renamed legend's _ncol to _ncols, which breaks tikzplotlib\n", |
| 19 | + " \"\"\"\n", |
| 20 | + " if hasattr(obj, \"_ncols\"):\n", |
| 21 | + " obj._ncol = obj._ncols\n", |
| 22 | + " for child in obj.get_children():\n", |
| 23 | + " tikzplotlib_fix_ncols(child)\n", |
| 24 | + "casename=\"henon\"" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "code", |
| 29 | + "execution_count": 2, |
| 30 | + "id": "f95a6de7-6e84-43a0-86d6-d2bd23633036", |
| 31 | + "metadata": {}, |
| 32 | + "outputs": [], |
| 33 | + "source": [ |
| 34 | + "if casename==\"logistic\":\n", |
| 35 | + " from reservoirpy.datasets import logistic_map\n", |
| 36 | + " ts=logistic_map(100000, r=3.9, x0=0.5).flatten()\n", |
| 37 | + "elif casename==\"henon\":\n", |
| 38 | + " from run_reservoir import henon1d \n", |
| 39 | + " ts = henon1d(10000)" |
| 40 | + ] |
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "code", |
| 44 | + "execution_count": null, |
| 45 | + "id": "2cac7dd9", |
| 46 | + "metadata": {}, |
| 47 | + "outputs": [], |
| 48 | + "source": [ |
| 49 | + "num_qubits=6\n", |
| 50 | + "num_meas=3\n", |
| 51 | + "degree=2" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "code", |
| 56 | + "execution_count": null, |
| 57 | + "id": "4dec95e5-58ea-4932-8bc0-88e3a2285a25", |
| 58 | + "metadata": {}, |
| 59 | + "outputs": [], |
| 60 | + "source": [ |
| 61 | + "def get_data(ep, num_qubits, num_meas, method, noise=\"None\", decode=True):\n", |
| 62 | + " degree=num_meas\n", |
| 63 | + " num_reservoirs=20\n", |
| 64 | + " timeplex=1\n", |
| 65 | + " string_identifier=str(ep)+\"_casename\"+str(casename)+\"_num_qubits\"+str(num_qubits)+\"_num_meas\"+str(num_meas)\n", |
| 66 | + " string_identifier+=\"_degree\"+str(degree)+\"_num_reservoirs\"+str(num_reservoirs)+\"_timeplex\"+str(timeplex)\n", |
| 67 | + " string_identifier+=\"_method\"+str(method)+\"_noise\"+str(noise)\n", |
| 68 | + " if not decode:\n", |
| 69 | + " string_identifier+=\"_decodeFalse\"\n", |
| 70 | + " string_identifier+=\".pickle\"\n", |
| 71 | + " name=\"X_train\"+string_identifier\n", |
| 72 | + " # print(\"name=\",name)\n", |
| 73 | + " with open(name,\"rb\") as f:\n", |
| 74 | + " X_train = pickle.load(f)\n", |
| 75 | + " \n", |
| 76 | + " name=\"X_test\"+string_identifier\n", |
| 77 | + " with open(name,\"rb\") as f:\n", |
| 78 | + " X_test = pickle.load(f)\n", |
| 79 | + " \n", |
| 80 | + " name=\"score\"+string_identifier\n", |
| 81 | + " with open(name,\"rb\") as f:\n", |
| 82 | + " score = pickle.load(f)\n", |
| 83 | + " \n", |
| 84 | + " name=\"prediction\"+string_identifier\n", |
| 85 | + " with open(name,\"rb\") as f:\n", |
| 86 | + " prediction = pickle.load(f)\n", |
| 87 | + " \n", |
| 88 | + " name=\"state\"+string_identifier\n", |
| 89 | + " with open(name,\"rb\") as f:\n", |
| 90 | + " state = pickle.load(f)\n", |
| 91 | + " return X_train, X_test, score, prediction, state, string_identifier" |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "code", |
| 96 | + "execution_count": 4, |
| 97 | + "id": "43ea3fa2-780c-4d55-b69b-7e1730bce1a0", |
| 98 | + "metadata": {}, |
| 99 | + "outputs": [], |
| 100 | + "source": [ |
| 101 | + "# X_train, X_test, score, prediction, state, basen = get_data(ep, 7, 6, \"classical\", \"None\")\n", |
| 102 | + "# _=plt.plot(state)" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "cell_type": "code", |
| 107 | + "execution_count": null, |
| 108 | + "id": "9fdee019", |
| 109 | + "metadata": {}, |
| 110 | + "outputs": [], |
| 111 | + "source": [ |
| 112 | + "for ep in [0,4]:\n", |
| 113 | + " X_train, X_test, score, prediction, states, basen = get_data(ep, num_qubits, num_meas, \"classical\", \"None\")\n", |
| 114 | + " data=states\n", |
| 115 | + " groups = defaultdict(list)\n", |
| 116 | + " for n, m, _ , stats in data:\n", |
| 117 | + " mean, var = stats\n", |
| 118 | + " x = n\n", |
| 119 | + " groups[n].append((x, float(mean), float(var)))\n", |
| 120 | + "\n", |
| 121 | + " # Plot each group\n", |
| 122 | + " plt.figure(figsize=(8,6))\n", |
| 123 | + "\n", |
| 124 | + " for n, values in groups.items():\n", |
| 125 | + " values.sort(key=lambda v: v[0]) # sort by x = n/m\n", |
| 126 | + " xs = [v[0] for v in values]\n", |
| 127 | + " ys = [v[1] for v in values]\n", |
| 128 | + " errs = [v[2] for v in values]\n", |
| 129 | + " plt.errorbar(xs, ys, yerr=errs, label=f\"n = {n}\", marker='o', capsize=4)\n", |
| 130 | + "\n", |
| 131 | + " plt.xlabel(\"degree\")\n", |
| 132 | + " plt.ylabel(\"Mean ± Variance\")\n", |
| 133 | + " plt.legend()\n", |
| 134 | + " plt.grid(True)\n", |
| 135 | + " plt.show()" |
| 136 | + ] |
| 137 | + } |
| 138 | + ], |
| 139 | + "metadata": { |
| 140 | + "kernelspec": { |
| 141 | + "display_name": "Python 3 (ipykernel)", |
| 142 | + "language": "python", |
| 143 | + "name": "python3" |
| 144 | + }, |
| 145 | + "language_info": { |
| 146 | + "codemirror_mode": { |
| 147 | + "name": "ipython", |
| 148 | + "version": 3 |
| 149 | + }, |
| 150 | + "file_extension": ".py", |
| 151 | + "mimetype": "text/x-python", |
| 152 | + "name": "python", |
| 153 | + "nbconvert_exporter": "python", |
| 154 | + "pygments_lexer": "ipython3", |
| 155 | + "version": "3.8.12" |
| 156 | + } |
| 157 | + }, |
| 158 | + "nbformat": 4, |
| 159 | + "nbformat_minor": 5 |
| 160 | +} |
0 commit comments