3690236902 "\n",
3690336903 "\n",
3690436904 "def maps2str(maps):\n",
36905- " return \"||\".join(\"|\".join(str(i) for i in reg) for reg in maps) "
36905+ " return \"||\".join(\"|\".join(str(i) for i in reg) for reg in maps)"
3690636906 ]
3690736907 },
3690836908 {
@@ -36922,24 +36922,24 @@
3692236922 " result_path_base = rf\"result-{circuit.name}-{backend.name}-{shots}\"\n",
3692336923 " maps_str = maps2str(maps) if maps is not None else None\n",
3692436924 " if use_cached is not False:\n",
36925- " cached_results = [] \n",
36925+ " cached_results = []\n",
3692636926 " path_pattern = re.compile(re.escape(result_path_base) + \"-([\\d|]+)-?([a-z\\d]*).json\")\n",
3692736927 " for result_path in assets_path.iterdir():\n",
3692836928 " m = path_pattern.match(result_path.name)\n",
3692936929 " if m is None:\n",
3693036930 " continue\n",
3693136931 " if maps is None or m.group(1) == maps_str:\n",
36932- " result = qiskit.result.Result.from_dict(json.loads(result_path.read_text())) \n",
36932+ " result = qiskit.result.Result.from_dict(json.loads(result_path.read_text()))\n",
3693336933 " print(f\"Load result from '{os.path.relpath(result_path, Path.cwd())}'!\")\n",
3693436934 " if use_cached is True or use_cached == \"any\":\n",
36935- " return result \n",
36935+ " return result\n",
3693636936 " elif use_cached == \"all\":\n",
3693736937 " cached_results.append(result)\n",
3693836938 " else:\n",
3693936939 " raise ValueError(f\"wrong type of cache rule: {use_cached}\")\n",
3694036940 " if len(cached_results) > 0:\n",
3694136941 " return cached_results\n",
36942- " \n",
36942+ "\n",
3694336943 " circuit_transpiled = transpile(circuit, backend, maps=maps)\n",
3694436944 " if maps is None:\n",
3694536945 " maps = maps_from_layout(circuit_transpiled.layout)\n",
@@ -39402,23 +39402,31 @@
3940239402 " errs = [fit_limit(x, y, hdist, neighbour=n, a_tol=1e-3) for n in range(1, n_data_qubits // 2 + 1)]\n",
3940339403 " return errs\n",
3940439404 "\n",
39405+ "\n",
3940539406 "def argminmeans(lst):\n",
3940639407 " return np.argmin([np.mean([e for _, e in item]) for item in lst])\n",
39407- " \n",
39408+ "\n",
39409+ "\n",
3940839410 "def neighbors_errors(backend, circuit, n_data_qubits, shots, maps=None, use_cached=True, choose=None):\n",
3940939411 " result = run_on_backend(backend, circuit, shots, maps=maps, use_cached=use_cached)\n",
3941039412 " if not isinstance(result, (tuple, list)):\n",
3941139413 " return neighbors_errors_from_result(result, n_data_qubits)\n",
39412- " \n",
39414+ "\n",
3941339415 " choose = argminmeans if choose is None else choose\n",
3941439416 " errss = [neighbors_errors_from_result(r, n_data_qubits) for r in result]\n",
3941539417 " print(errss)\n",
3941639418 " errs_indx = choose(errss)\n",
3941739419 " print(f\"Choose {errs_indx} result.\")\n",
3941839420 " return errss[errs_indx]\n",
3941939421 "\n",
39420- "def neighbors_errors_over_shots(backend, circuit, n_data_qubits, shots_arr, maps=None, use_cached=True, choose=None) -> list:\n",
39421- " errs = [neighbors_errors(backend, circuit, n_data_qubits, s, maps=maps, choose=choose, use_cached=use_cached) for s in shots_arr]\n",
39422+ "\n",
39423+ "def neighbors_errors_over_shots(\n",
39424+ " backend, circuit, n_data_qubits, shots_arr, maps=None, use_cached=True, choose=None\n",
39425+ ") -> list:\n",
39426+ " errs = [\n",
39427+ " neighbors_errors(backend, circuit, n_data_qubits, s, maps=maps, choose=choose, use_cached=use_cached)\n",
39428+ " for s in shots_arr\n",
39429+ " ]\n",
3942239430 " errs = [list(zip(*limits_and_errors)) for limits_and_errors in list(zip(*errs))]\n",
3942339431 " return errs"
3942439432 ]
3943639444 "\n",
3943739445 "def line(x, a, b):\n",
3943839446 " return a * x + b\n",
39439- " \n",
39440- "def fit_exp(shots_arr, errs_arr): \n",
39441- " errs_arr_argmin = np.argmin(np.hstack((errs_arr, [0]))) # can not fit at zero \n",
39447+ "\n",
39448+ "\n",
39449+ "def fit_exp(shots_arr, errs_arr):\n",
39450+ " errs_arr_argmin = np.argmin(np.hstack((errs_arr, [0]))) # can not fit at zero\n",
3944239451 " errs_arr_up_to_min, shots_arr_ = errs_arr[:errs_arr_argmin], shots_arr[:errs_arr_argmin]\n",
3944339452 " params, params_covariance = curve_fit(line, shots_arr_, np.log(errs_arr_up_to_min), p0=(1e-3, 1))\n",
3944439453 " r2 = r2_score(errs_arr_up_to_min, np.exp(line(shots_arr_, *params)))\n",
3947339482 },
3947439483 "outputs": [],
3947539484 "source": [
39476- "shots_arr = np.linspace(500, 1.7e4, 34).astype(int)# ; shots_arr"
39485+ "shots_arr = np.linspace(500, 1.7e4, 34).astype(int) # ; shots_arr"
3947739486 ]
3947839487 },
3947939488 {
3957839587 "import re\n",
3957939588 "\n",
3958039589 "pattern = rf\"result-{circuit.name}-{ibm_backend.name}-(\\d+)-.*.json\"\n",
39581- "ibm_shots_arr = sorted({int(m.group(1)) for item in (Path.cwd().parent / \"assets\").iterdir() if (m := re.match(pattern, str(item.name)))}\n",
39582- " )# ibm_shots_arr = np.linspace(2e3, 1.7e4, 16).astype(int)\n",
39590+ "ibm_shots_arr = sorted(\n",
39591+ " {\n",
39592+ " int(m.group(1))\n",
39593+ " for item in (Path.cwd().parent / \"assets\").iterdir()\n",
39594+ " if (m := re.match(pattern, str(item.name)))\n",
39595+ " }\n",
39596+ ") # ibm_shots_arr = np.linspace(2e3, 1.7e4, 16).astype(int)\n",
3958339597 "ibm_shots_arr"
3958439598 ]
3958539599 },
3967239686 }
3967339687 ],
3967439688 "source": [
39675- "ibm_limits_and_errors = neighbors_errors_over_shots(ibm_backend, circuit, n_data_qubits, ibm_shots_arr, use_cached=\"all\")"
39689+ "ibm_limits_and_errors = neighbors_errors_over_shots(\n",
39690+ " ibm_backend, circuit, n_data_qubits, ibm_shots_arr, use_cached=\"all\"\n",
39691+ ")"
3967639692 ]
3967739693 },
3967839694 {
@@ -42014,12 +42030,14 @@
4201442030 }
4201542031 ],
4201642032 "source": [
42017- "fig, (axy, axx) = plt.subplots(2, 1, figsize=(6, 7), gridspec_kw={' height_ratios' : [10, 1]})\n",
42033+ "fig, (axy, axx) = plt.subplots(2, 1, figsize=(6, 7), gridspec_kw={\" height_ratios\" : [10, 1]})\n",
4201842034 "ibm_errs_arrs = list(zip(*ibm_limits_and_errors))[1]\n",
4201942035 "for i, errs_arr in enumerate(list(zip(*limits_and_errors))[1], 1):\n",
4202042036 " color = (\"r\", \"b\")[i - 1]\n",
4202142037 " marker = (\"o\", \".\")[i - 1]\n",
42022- " axy.scatter(shots_arr, errs_arr, marker=marker, c=color, label=rf\"Noise simulator ($d^\\mathrm{{max}} = {i}$)\")\n",
42038+ " axy.scatter(\n",
42039+ " shots_arr, errs_arr, marker=marker, c=color, label=rf\"Noise simulator ($d^\\mathrm{{max}} = {i}$)\"\n",
42040+ " )\n",
4202342041 " axx.scatter(shots_arr, errs_arr, marker=marker, c=color)\n",
4202442042 "\n",
4202542043 " params, r2 = fit_exp(shots_arr, errs_arr)\n",
@@ -42030,15 +42048,20 @@
4203042048 " f\"{linestyle}{color}\",\n",
4203142049 " label=rf\"Noise simulator $R^2={r2:.2}$ ($d^\\mathrm{{max}} = {i}$)\",\n",
4203242050 " )\n",
42033- " \n",
42051+ "\n",
4203442052 " ibm_marker = (\"^\", \"v\")[i - 1]\n",
4203542053 " ibm_color = (\"m\", \"c\")[i - 1]\n",
4203642054 " ibm_shots_arr_, ibm_errs_arr_ = np.array(ibm_shots_arr), np.array(ibm_errs_arrs[i - 1])\n",
4203742055 " ibm_shots_arr_, ibm_errs_arr_ = ibm_shots_arr_, ibm_errs_arr_\n",
42038- " axy.scatter(ibm_shots_arr_, ibm_errs_arr_, marker=ibm_marker, c=ibm_color, label=rf\"IBM ($d^\\mathrm{{max}} = {i}$)\")\n",
42056+ " axy.scatter(\n",
42057+ " ibm_shots_arr_,\n",
42058+ " ibm_errs_arr_,\n",
42059+ " marker=ibm_marker,\n",
42060+ " c=ibm_color,\n",
42061+ " label=rf\"IBM ($d^\\mathrm{{max}} = {i}$)\",\n",
42062+ " )\n",
4203942063 " axx.scatter(ibm_shots_arr_, ibm_errs_arr_, marker=ibm_marker, c=ibm_color)\n",
42040- " \n",
42041- " \n",
42064+ "\n",
4204242065 "\n",
4204342066 "axy.set_ylabel(r\"$N_\\mathrm{e}$, number of wrong neighbours\")\n",
4204442067 "axy.set_yscale(\"log\", base=2)\n",
@@ -42049,13 +42072,13 @@
4204942072 "axx.set_yticklabels([0])\n",
4205042073 "axx.set_ylim(-0.5, 0.5)\n",
4205142074 "\n",
42052- "axy.spines[' bottom' ].set_visible(False)\n",
42075+ "axy.spines[\" bottom\" ].set_visible(False)\n",
4205342076 "axy.set_xticks(np.linspace(0, 20e3, 21))\n",
4205442077 "axy.set_xticklabels([])\n",
4205542078 "axy.tick_params(axis=\"x\", width=0)\n",
4205642079 "axy.set_xlim(0, 2**14 + 1e3)\n",
4205742080 "\n",
42058- "axx.spines[' top' ].set_visible(False)\n",
42081+ "axx.spines[\" top\" ].set_visible(False)\n",
4205942082 "axx.set_xlabel(\"$N_\\mathrm{s}$, number of shots\")\n",
4206042083 "axx.set_xticks(np.linspace(0, 20e3, 21))\n",
4206142084 "axx.set_xticklabels([0] + [f\"{i}k\" for i in np.linspace(1, 20, 20).astype(int)])\n",
4207042093 "axx.set_axisbelow(True)\n",
4207142094 "\n",
4207242095 "\n",
42073- "\n",
42074- "\n",
4207542096 "fig.tight_layout()\n",
4207642097 "fig.savefig(f\"./ne-over-shots-n{n_data_qubits}.pdf\")\n",
4207742098 "fig.show()"
4208742108 "ERRORS_OVER_SHOTS = {\n",
4208842109 " \"IBM\": {\n",
4208942110 " \"4\": (ibm_shots_arr, ibm_limits_and_errors),\n",
42090- " }, \n",
42111+ " },\n",
4209142112 " \"NOISE\": {\n",
4209242113 " \"4\": (shots_arr, limits_and_errors),\n",
4209342114 " },\n",
4222742248 "import re\n",
4222842249 "\n",
4222942250 "pattern = rf\"result-{circuit.name}-{ibm_backend.name}-(\\d+)-.*.json\"\n",
42230- "ibm_shots_arr = sorted({int(m.group(1)) for item in (Path.cwd().parent / \"assets\").iterdir() if (m := re.match(pattern, str(item.name)))})\n",
42251+ "ibm_shots_arr = sorted(\n",
42252+ " {\n",
42253+ " int(m.group(1))\n",
42254+ " for item in (Path.cwd().parent / \"assets\").iterdir()\n",
42255+ " if (m := re.match(pattern, str(item.name)))\n",
42256+ " }\n",
42257+ ")\n",
4223142258 "# ibm_shots_arr = np.linspace(2e3, 1.7e4, 16).astype(int)\n",
4223242259 "print(ibm_shots_arr)"
4223342260 ]
4455944586 }
4456044587 ],
4456144588 "source": [
44562- "fig, (axy, axx) = plt.subplots(2, 1, figsize=(6, 5), gridspec_kw={' height_ratios' : [10, 1]})\n",
44589+ "fig, (axy, axx) = plt.subplots(2, 1, figsize=(6, 5), gridspec_kw={\" height_ratios\" : [10, 1]})\n",
4456344590 "ibm_errs_arrs = list(zip(*ibm_limits_and_errors))[1]\n",
4456444591 "for i, errs_arr in enumerate(list(zip(*limits_and_errors))[1], 1):\n",
4456544592 " color = (\"r\", \"b\")[i - 1]\n",
@@ -44569,17 +44596,19 @@
4456944596 "\n",
4457044597 " ibm_marker = (\"^\", \"v\")[i - 1]\n",
4457144598 " print(len(ibm_shots_arr), len(ibm_errs_arrs[i - 1]))\n",
44572- " axy.scatter(ibm_shots_arr, ibm_errs_arrs[i - 1], marker=ibm_marker, c=color, label=f\"(IBM) Up to {i} neighbors\")\n",
44599+ " axy.scatter(\n",
44600+ " ibm_shots_arr, ibm_errs_arrs[i - 1], marker=ibm_marker, c=color, label=f\"(IBM) Up to {i} neighbors\"\n",
44601+ " )\n",
4457344602 " axx.scatter(ibm_shots_arr, ibm_errs_arrs[i - 1], marker=ibm_marker, c=color)\n",
44574- " \n",
44603+ "\n",
4457544604 " params, r2 = fit_exp(shots_arr, errs_arr)\n",
4457644605 " axy.plot(\n",
4457744606 " shots_arr,\n",
4457844607 " np.exp(line(shots_arr, *params)),\n",
4457944608 " f\"{color}\",\n",
4458044609 " label=f\"Up to {i} neighbors R2={r2:.2}\",\n",
4458144610 " )\n",
44582- " \n",
44611+ "\n",
4458344612 "\n",
4458444613 "axy.set_ylabel(r\"$N_\\mathrm{e}$, number of wrong neighbours\")\n",
4458544614 "axy.set_yscale(\"log\", base=2)\n",
@@ -44590,13 +44619,13 @@
4459044619 "axx.set_yticklabels([0])\n",
4459144620 "axx.set_ylim(-0.5, 0.5)\n",
4459244621 "\n",
44593- "axy.spines[' bottom' ].set_visible(False)\n",
44622+ "axy.spines[\" bottom\" ].set_visible(False)\n",
4459444623 "axy.set_xticks(np.linspace(1e4, 13e4, 13))\n",
4459544624 "axy.set_xticklabels([])\n",
4459644625 "axy.tick_params(axis=\"x\", width=0)\n",
4459744626 "axy.set_xlim(2**12, 13e4 + 5e3)\n",
4459844627 "\n",
44599- "axx.spines[' top' ].set_visible(False)\n",
44628+ "axx.spines[\" top\" ].set_visible(False)\n",
4460044629 "axx.set_xlabel(\"$N_\\mathrm{s}$, number of shots\")\n",
4460144630 "axx.set_xticks(np.linspace(1e4, 13e4, 13))\n",
4460244631 "axx.set_xticklabels([f\"{i}0k\" for i in np.linspace(1, 13, 13).astype(int)])\n",
4460844637 "axx.grid()\n",
4460944638 "\n",
4461044639 "\n",
44611- "\n",
4461244640 "fig.tight_layout()\n",
4461344641 "fig.savefig(f\"./ne-over-shots-n{n_data_qubits}.pdf\")\n",
4461444642 "fig.show()"
4720747235 "(shots_arr, limits_and_errors) = ERRORS_OVER_SHOTS[\"NOISE\"][\"4\"]\n",
4720847236 "(ibm_shots_arr, ibm_limits_and_errors) = ERRORS_OVER_SHOTS[\"IBM\"][\"4\"]\n",
4720947237 "\n",
47210- "fig, ((axy, axy_), (axx, axx_)) = plt.subplots(2, 2, figsize=(9, 5), gridspec_kw={' height_ratios' : [10, 1]})\n",
47238+ "fig, ((axy, axy_), (axx, axx_)) = plt.subplots(2, 2, figsize=(9, 5), gridspec_kw={\" height_ratios\" : [10, 1]})\n",
4721147239 "ibm_errs_arrs = list(zip(*ibm_limits_and_errors))[1]\n",
4721247240 "for i, errs_arr in enumerate(list(zip(*limits_and_errors))[1], 1):\n",
4721347241 " color = (\"r\", \"b\")[i - 1]\n",
4722547253 " # label=rf\"Noise simulator $R^2={r2:.2}$ ($d^\\mathrm{{max}} = {i}$)\",\n",
4722647254 " label=rf\"Noise simulator $R^2={r2:.2}$\",\n",
4722747255 " )\n",
47228- " \n",
47256+ "\n",
4722947257 " ibm_marker = (\"^\", \"v\")[i - 1]\n",
4723047258 " ibm_color = (\"m\", \"c\")[i - 1]\n",
4723147259 " ibm_shots_arr_, ibm_errs_arr_ = np.array(ibm_shots_arr), np.array(ibm_errs_arrs[i - 1])\n",
4723447262 " # axy.scatter(ibm_shots_arr_, ibm_errs_arr_, marker=ibm_marker, c=ibm_color, label=rf\"IBM ($d^\\mathrm{{max}} = {i}$)\")\n",
4723547263 " axy.scatter(ibm_shots_arr_, ibm_errs_arr_, marker=ibm_marker, c=ibm_color, label=rf\"IBM Sherbrooke\")\n",
4723647264 " axx.scatter(ibm_shots_arr_, ibm_errs_arr_, marker=ibm_marker, c=ibm_color)\n",
47237- " \n",
47265+ "\n",
4723847266 " break\n",
4723947267 "\n",
4724047268 "axy.set_ylabel(r\"$N_\\mathrm{e}$, number of wrong neighbours\")\n",
@@ -47246,13 +47274,13 @@
4724647274 "axx.set_yticklabels([0])\n",
4724747275 "axx.set_ylim(-0.5, 0.5)\n",
4724847276 "\n",
47249- "axy.spines[' bottom' ].set_visible(False)\n",
47277+ "axy.spines[\" bottom\" ].set_visible(False)\n",
4725047278 "axy.set_xticks(np.linspace(0, 18e3, 10))\n",
4725147279 "axy.set_xticklabels([])\n",
4725247280 "axy.tick_params(axis=\"x\", width=0)\n",
4725347281 "axy.set_xlim(0, 18e3)\n",
4725447282 "\n",
47255- "axx.spines[' top' ].set_visible(False)\n",
47283+ "axx.spines[\" top\" ].set_visible(False)\n",
4725647284 "axx.set_xlabel(\"$N_\\mathrm{s}$, number of shots\")\n",
4725747285 "axx.set_xticks(np.linspace(0, 18e3, 10))\n",
4725847286 "axx.set_xticklabels([0 if i == 0 else f\"{i}k\" for i in np.linspace(0, 18, 10).astype(int)])\n",
4726947297 "axx.set_axisbelow(True)\n",
4727047298 "\n",
4727147299 "\n",
47272- "axx.text(-2000, -2, \"a\", color=\"k\", fontsize='14' , fontweight=\"bold\")\n",
47300+ "axx.text(-2000, -2, \"a\", color=\"k\", fontsize=\"14\" , fontweight=\"bold\")\n",
4727347301 "\n",
4727447302 "\n",
4727547303 "(shots_arr, limits_and_errors) = ERRORS_OVER_SHOTS[\"NOISE\"][\"5\"]\n",
4729447322 " # label=rf\"Noise simulator $R^2={r2:.2}$ ($d^\\mathrm{{max}} = {i}$)\",\n",
4729547323 " label=rf\"Noise simulator $R^2={r2:.2}$\",\n",
4729647324 " )\n",
47297- " \n",
47325+ "\n",
4729847326 " ibm_marker = (\"^\", \"v\")[i - 1]\n",
4729947327 " ibm_color = (\"m\", \"c\")[i - 1]\n",
4730047328 " ibm_shots_arr_, ibm_errs_arr_ = np.array(ibm_shots_arr), np.array(ibm_errs_arrs[i - 1])\n",
4730347331 " # axy.scatter(ibm_shots_arr_, ibm_errs_arr_, marker=ibm_marker, c=ibm_color, label=rf\"IBM ($d^\\mathrm{{max}} = {i}$)\")\n",
4730447332 " axy.scatter(ibm_shots_arr_, ibm_errs_arr_, marker=ibm_marker, c=ibm_color, label=rf\"IBM Sherbrooke\")\n",
4730547333 " axx.scatter(ibm_shots_arr_, ibm_errs_arr_, marker=ibm_marker, c=ibm_color)\n",
47306- " \n",
47334+ "\n",
4730747335 " break\n",
4730847336 "\n",
4730947337 "# axy.set_ylabel(r\"$N_\\mathrm{e}$, number of wrong neighbours\")\n",
@@ -47315,13 +47343,13 @@
4731547343 "axx.set_yticklabels([0])\n",
4731647344 "axx.set_ylim(-0.5, 0.5)\n",
4731747345 "\n",
47318- "axy.spines[' bottom' ].set_visible(False)\n",
47346+ "axy.spines[\" bottom\" ].set_visible(False)\n",
4731947347 "axy.set_xticks(np.linspace(0, 14e4, 8))\n",
4732047348 "axy.set_xticklabels([])\n",
4732147349 "axy.tick_params(axis=\"x\", width=0)\n",
4732247350 "axy.set_xlim(0, 14e4)\n",
4732347351 "\n",
47324- "axx.spines[' top' ].set_visible(False)\n",
47352+ "axx.spines[\" top\" ].set_visible(False)\n",
4732547353 "axx.set_xlabel(\"$N_\\mathrm{s}$, number of shots\")\n",
4732647354 "axx.set_xticks(np.linspace(0, 14e4, 8))\n",
4732747355 "axx.set_xticklabels([0 if i == 0 else f\"{i}0k\" for i in np.linspace(0, 14, 8).astype(int)])\n",
4733447362 "# axy.set_title(f\"{min_qubits_for_data_encoding(x)} data qubits\")\n",
4733547363 "axx.grid()\n",
4733647364 "\n",
47337- "axx.text(-15500, -2, \"b\", color=\"k\", fontsize='14' , fontweight=\"bold\")\n",
47365+ "axx.text(-15500, -2, \"b\", color=\"k\", fontsize=\"14\" , fontweight=\"bold\")\n",
4733847366 "\n",
4733947367 "\n",
4734047368 "fig.tight_layout()\n",
4742247450 "\n",
4742347451 "# 99|101|117|119||98|102|116|120||100|110|118-cw89p5r9ezk00080zv40\n",
4742447452 "\n",
47425- "results = run_on_backend(ibm_backend, circuit, shots=shots, use_cached=\"all\") # \"cw89p5r9ezk00080zv40\""
47453+ "results = run_on_backend(ibm_backend, circuit, shots=shots, use_cached=\"all\") # \"cw89p5r9ezk00080zv40\""
4742647454 ]
4742747455 },
4742847456 {
5598156009 "# maps = [[109, 95, 91, 101, 110], [114, 94, 79, 102, 118], [96, 97, 98, 99, 100]]\n",
5598256010 "# maps = [[123, 121, 103, 93, 107], [124, 120, 102, 87, 108], [122, 111, 104, 105, 106]]\n",
5598356011 "# maps = [[46, 35, 50, 69, 67], [45, 28, 51, 70, 66], [47, 48, 49, 55, 68]]\n",
55984- "results = run_on_backend(ibm_backend, circuit, shots=shots, use_cached=\"all\") # cxe6nf5ky7rg0083pew0"
56012+ "results = run_on_backend(ibm_backend, circuit, shots=shots, use_cached=\"all\") # cxe6nf5ky7rg0083pew0"
5598556013 ]
5598656014 },
5598756015 {
0 commit comments