|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "1c2b1422-5a3b-401c-88fb-0c0b723c6633", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Timing Cost Computation\n", |
| 9 | + "\n", |
| 10 | + "This notebook goes through each bloq example and calls `report_on_cost_timings`, which currently times how long it takes to do the `QubitCount` cost key. This uses the `ExecuteWithTimeout` fixture." |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": null, |
| 16 | + "id": "9ae691c8-a5f8-4b66-a99c-7d82ad8ffc2e", |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [], |
| 19 | + "source": [ |
| 20 | + "from qualtran_dev_tools.execute_with_timeout import ExecuteWithTimeout\n", |
| 21 | + "from qualtran_dev_tools.bloq_report_card import report_on_cost_timings\n", |
| 22 | + "from qualtran_dev_tools.bloq_finder import get_bloq_examples" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "code", |
| 27 | + "execution_count": null, |
| 28 | + "id": "3fcfb561-6cb9-4891-8157-547dbfa5502b", |
| 29 | + "metadata": {}, |
| 30 | + "outputs": [], |
| 31 | + "source": [ |
| 32 | + "bes = get_bloq_examples()\n", |
| 33 | + "\n", |
| 34 | + "# Imports to exclude certain bloqs, see following comment\n", |
| 35 | + "from qualtran.bloqs.multiplexers.apply_gate_to_lth_target import ApplyGateToLthQubit" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "code", |
| 40 | + "execution_count": null, |
| 41 | + "id": "cea586d9-55b3-4ee8-b255-ea88bdff75f4", |
| 42 | + "metadata": {}, |
| 43 | + "outputs": [], |
| 44 | + "source": [ |
| 45 | + "exec = ExecuteWithTimeout(timeout=20., max_workers=4)\n", |
| 46 | + "for i, be in enumerate(bes):\n", |
| 47 | + "\n", |
| 48 | + " if be.bloq_cls == ApplyGateToLthQubit:\n", |
| 49 | + " # This bloq uses a lambda function as one of its attributes, which\n", |
| 50 | + " # can't be pickled and used with multiprocessing.\n", |
| 51 | + " continue\n", |
| 52 | + " \n", |
| 53 | + " exec.submit(report_on_cost_timings, kwargs=dict(name=be.name, cls_name=be.bloq_cls.__name__, bloq=be.make()))\n", |
| 54 | + "\n", |
| 55 | + "records = []\n", |
| 56 | + "while exec.work_to_be_done:\n", |
| 57 | + " kwargs, record = exec.next_result()\n", |
| 58 | + " print('\\r', f'{exec.work_to_be_done:5d} remaining', end='', flush=True)\n", |
| 59 | + " \n", |
| 60 | + " if record is None:\n", |
| 61 | + " records.append({\n", |
| 62 | + " 'name': kwargs['name'],\n", |
| 63 | + " 'cls': kwargs['cls_name'],\n", |
| 64 | + " 'err': 'Timeout',\n", |
| 65 | + " })\n", |
| 66 | + " else:\n", |
| 67 | + " records.append(record)\n", |
| 68 | + "\n", |
| 69 | + "import pandas as pd\n", |
| 70 | + "df = pd.DataFrame(records)" |
| 71 | + ] |
| 72 | + }, |
| 73 | + { |
| 74 | + "cell_type": "markdown", |
| 75 | + "id": "abf0d3f1-bc5f-48a6-9d9b-b9fa1bbeb8d1", |
| 76 | + "metadata": {}, |
| 77 | + "source": [ |
| 78 | + "## Slowest\n", |
| 79 | + "\n", |
| 80 | + "This prints the total number of bloq examples considered and then summarizes the 5 slowest-to-compute bloq examples." |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "code", |
| 85 | + "execution_count": null, |
| 86 | + "id": "86f259b0-ec70-4860-b76c-0df993ca7934", |
| 87 | + "metadata": {}, |
| 88 | + "outputs": [], |
| 89 | + "source": [ |
| 90 | + "print(len(df))\n", |
| 91 | + "df.sort_values(by='qubitcount_dur', ascending=False).head()" |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "markdown", |
| 96 | + "id": "a30190dd-550d-45d4-a572-94fdaa4a2f10", |
| 97 | + "metadata": {}, |
| 98 | + "source": [ |
| 99 | + "## Errors and timeouts\n", |
| 100 | + "\n", |
| 101 | + "These bloq examples either time-out or encounter errors in the qubit computation." |
| 102 | + ] |
| 103 | + }, |
| 104 | + { |
| 105 | + "cell_type": "code", |
| 106 | + "execution_count": null, |
| 107 | + "id": "513a3421-8f80-43ae-9449-89c8063da242", |
| 108 | + "metadata": {}, |
| 109 | + "outputs": [], |
| 110 | + "source": [ |
| 111 | + "df[df['qubitcount_dur'].isna()]" |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "code", |
| 116 | + "execution_count": null, |
| 117 | + "id": "605e8869-e77e-40b3-8455-07204b7b872c", |
| 118 | + "metadata": {}, |
| 119 | + "outputs": [], |
| 120 | + "source": [ |
| 121 | + "for i, row in df[df['qubitcount_dur'].isna()].iterrows():\n", |
| 122 | + " print(\"### `{}`\".format(row['name']))\n", |
| 123 | + " print(\"{}\\n\".format(row[\"err\"]))" |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "markdown", |
| 128 | + "id": "bceb65d1-d107-4c0d-8662-8ea80c00de16", |
| 129 | + "metadata": {}, |
| 130 | + "source": [ |
| 131 | + "## Timeouts\n", |
| 132 | + "\n", |
| 133 | + "These examples specifically time out. " |
| 134 | + ] |
| 135 | + }, |
| 136 | + { |
| 137 | + "cell_type": "code", |
| 138 | + "execution_count": null, |
| 139 | + "id": "043c3d7a-f4d4-486c-aa99-eb871e23df3b", |
| 140 | + "metadata": {}, |
| 141 | + "outputs": [], |
| 142 | + "source": [ |
| 143 | + "df[df['err'] == 'Timeout']" |
| 144 | + ] |
| 145 | + }, |
| 146 | + { |
| 147 | + "cell_type": "markdown", |
| 148 | + "id": "c7b87a8d-c4eb-4b7b-abce-66dfa31a0699", |
| 149 | + "metadata": {}, |
| 150 | + "source": [ |
| 151 | + "## Investigation\n", |
| 152 | + "\n", |
| 153 | + "Individual bloq examples can be investigated. Strangely, hubbard_time_evolution_by_gqsp times out when run through the fixture but appears reasonably quick when run directly." |
| 154 | + ] |
| 155 | + }, |
| 156 | + { |
| 157 | + "cell_type": "code", |
| 158 | + "execution_count": null, |
| 159 | + "id": "d0f486e8-8cbf-4907-a84d-9ff43cfcd43e", |
| 160 | + "metadata": {}, |
| 161 | + "outputs": [], |
| 162 | + "source": [ |
| 163 | + "def get_bloq_example(name):\n", |
| 164 | + " results = [be for be in bes if be.name == name]\n", |
| 165 | + " \n", |
| 166 | + " if len(results) == 1:\n", |
| 167 | + " return results[0]\n", |
| 168 | + " if len(results) > 1:\n", |
| 169 | + " raise ValueError(\"Found more than one result for the query\")\n", |
| 170 | + " if len(results) == 0:\n", |
| 171 | + " raise KeyError(f\"The bloq example {name} was not found\") " |
| 172 | + ] |
| 173 | + }, |
| 174 | + { |
| 175 | + "cell_type": "code", |
| 176 | + "execution_count": null, |
| 177 | + "id": "0d01d939-fa0d-4934-8a56-24f1087cd232", |
| 178 | + "metadata": {}, |
| 179 | + "outputs": [], |
| 180 | + "source": [ |
| 181 | + "from qualtran.drawing import show_call_graph\n", |
| 182 | + "be = get_bloq_example('hubbard_time_evolution_by_gqsp')\n", |
| 183 | + "bloq = be.make()\n", |
| 184 | + "show_call_graph(bloq, max_depth=1)" |
| 185 | + ] |
| 186 | + }, |
| 187 | + { |
| 188 | + "cell_type": "code", |
| 189 | + "execution_count": null, |
| 190 | + "id": "e402cd50-8015-4f14-9c6f-1a7755bf4936", |
| 191 | + "metadata": {}, |
| 192 | + "outputs": [], |
| 193 | + "source": [ |
| 194 | + "%%timeit\n", |
| 195 | + "from qualtran.resource_counting import get_cost_value, QubitCount\n", |
| 196 | + "\n", |
| 197 | + "get_cost_value(bloq, QubitCount())" |
| 198 | + ] |
| 199 | + }, |
| 200 | + { |
| 201 | + "cell_type": "code", |
| 202 | + "execution_count": null, |
| 203 | + "id": "56391b2c-9381-480e-b204-e881fe0828ae", |
| 204 | + "metadata": {}, |
| 205 | + "outputs": [], |
| 206 | + "source": [] |
| 207 | + } |
| 208 | + ], |
| 209 | + "metadata": { |
| 210 | + "kernelspec": { |
| 211 | + "display_name": "Python 3 (ipykernel)", |
| 212 | + "language": "python", |
| 213 | + "name": "python3" |
| 214 | + }, |
| 215 | + "language_info": { |
| 216 | + "codemirror_mode": { |
| 217 | + "name": "ipython", |
| 218 | + "version": 3 |
| 219 | + }, |
| 220 | + "file_extension": ".py", |
| 221 | + "mimetype": "text/x-python", |
| 222 | + "name": "python", |
| 223 | + "nbconvert_exporter": "python", |
| 224 | + "pygments_lexer": "ipython3", |
| 225 | + "version": "3.11.8" |
| 226 | + } |
| 227 | + }, |
| 228 | + "nbformat": 4, |
| 229 | + "nbformat_minor": 5 |
| 230 | +} |
0 commit comments