diff --git a/.github/workflows/quality_checks.yaml b/.github/workflows/quality_checks.yaml index 7b0a773..8c7e0f1 100644 --- a/.github/workflows/quality_checks.yaml +++ b/.github/workflows/quality_checks.yaml @@ -82,6 +82,8 @@ jobs: - uses: actions/checkout@v2 - name: Setup poetry uses: abatilo/actions-poetry@v2 + - name: Setup Graphviz + uses: ts-graphviz/setup-graphviz@v2 - name: Install package and deps run: | poetry install -E jupyter diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9754708..0beab3f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -22,7 +22,7 @@ repos: rev: 5.13.2 hooks: - id: isort - args: ["--profile", "black", "--filter-files", "--check"] + args: ['--profile', "black", "--filter-files", "--check"] - repo: https://github.com/ambv/black rev: 24.4.1 @@ -40,4 +40,5 @@ repos: hooks: - id: conventional-pre-commit stages: [commit-msg] - args: [] \ No newline at end of file + args: [] + diff --git a/docs/tutorials/01_basic_example.ipynb b/docs/tutorials/01_basic_example.ipynb index f76f327..870f74e 100644 --- a/docs/tutorials/01_basic_example.ipynb +++ b/docs/tutorials/01_basic_example.ipynb @@ -66,10 +66,13 @@ }, { "cell_type": "code", - "execution_count": 1, "id": "6e2460c7-8ec3-4157-8b92-3d8ac5ff300f", - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T20:58:18.268334Z", + "start_time": "2024-06-24T20:58:18.259199Z" + } + }, "source": [ "my_algorithm = {\n", " \"name\": \"my_algorithm\",\n", @@ -79,7 +82,9 @@ " {\"name\": \"out\", \"direction\": \"output\", \"size\": None},\n", " ],\n", "}" - ] + ], + "outputs": [], + "execution_count": 31 }, { "cell_type": "markdown", @@ -99,10 +104,13 @@ }, { "cell_type": "code", - "execution_count": 2, "id": "591b4a88-af8c-41ec-bc07-ca0bbe576c70", - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T20:58:18.403637Z", + "start_time": "2024-06-24T20:58:18.391328Z" + } + }, "source": [ "routine_a = {\n", " \"name\": \"A\",\n", @@ -112,7 +120,9 @@ " {\"name\": \"out\", \"direction\": \"output\", \"size\": \"2*n_a\"},\n", " ],\n", "}" - ] + ], + "outputs": [], + "execution_count": 32 }, { "cell_type": "markdown", @@ -124,21 +134,26 @@ }, { "cell_type": "code", - "execution_count": 3, "id": "76a0ed1a-0acb-489e-b2fc-40ce956b8f49", - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T20:58:18.438587Z", + "start_time": "2024-06-24T20:58:18.425845Z" + } + }, "source": [ "routine_b = {\n", " \"name\": \"B\",\n", " \"type\": None,\n", " \"ports\": [\n", " {\"name\": \"in\", \"direction\": \"input\", \"size\": \"n_b\"},\n", - " # \"y\" will be defined in the next step\n", + " # \"y\" will be defined in the next step\n", " {\"name\": \"out\", \"direction\": \"output\", \"size\": \"n_b + y\"},\n", " ],\n", "}" - ] + ], + "outputs": [], + "execution_count": 33 }, { "cell_type": "markdown", @@ -159,23 +174,24 @@ }, { "cell_type": "code", - "execution_count": 4, "id": "697c5cdb-abb8-4437-97e8-d3a1e4de1ca2", - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T20:58:18.530086Z", + "start_time": "2024-06-24T20:58:18.521327Z" + } + }, "source": [ "# Define T-gate counts for routine a\n", "routine_a[\"input_params\"] = [\"x\"]\n", - "routine_a[\"resources\"] = [\n", - " {\"name\": \"T_gates\", \"type\": \"additive\", \"value\": \"2*n_a + x\"}\n", - "]\n", + "routine_a[\"resources\"] = [{\"name\": \"T_gates\", \"type\": \"additive\", \"value\": \"2*n_a + x\"}]\n", "\n", "# Define T-gate counts for routine b\n", "routine_b[\"input_params\"] = [\"y\"]\n", - "routine_b[\"resources\"] = [\n", - " {\"name\": \"T_gates\", \"type\": \"additive\", \"value\": \"n_b*ceil(log_2(n_b)) * y\"}\n", - "]" - ] + "routine_b[\"resources\"] = [{\"name\": \"T_gates\", \"type\": \"additive\", \"value\": \"n_b*ceil(log_2(n_b)) * y\"}]" + ], + "outputs": [], + "execution_count": 34 }, { "cell_type": "markdown", @@ -195,10 +211,13 @@ }, { "cell_type": "code", - "execution_count": 5, "id": "3cc1dcef-a151-401b-a818-3be783aa68f9", - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T20:58:18.603781Z", + "start_time": "2024-06-24T20:58:18.587847Z" + } + }, "source": [ "my_algorithm[\"children\"] = [routine_a, routine_b]\n", "my_algorithm[\"connections\"] = [\n", @@ -208,7 +227,9 @@ "]\n", "my_algorithm[\"input_params\"] = [\"z\"]\n", "my_algorithm[\"linked_params\"] = [{\"source\": \"z\", \"targets\": [\"A.x\", \"B.y\"]}]" - ] + ], + "outputs": [], + "execution_count": 35 }, { "cell_type": "markdown", @@ -231,12 +252,86 @@ }, { "cell_type": "code", - "execution_count": 6, "id": "ac4be2b0-f1be-46db-a42d-9f204727db7b", - "metadata": {}, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T20:58:18.643939Z", + "start_time": "2024-06-24T20:58:18.637009Z" + } + }, + "source": [ + "my_algorithm_qref = {\"version\": \"v1\", \"program\": my_algorithm}" + ], "outputs": [], + "execution_count": 36 + }, + { + "cell_type": "markdown", + "id": "6ef92aec51534839", + "metadata": {}, + "source": "So, is there an intuitive way to understand what my algorithm looks like and how the resources are used in each routine? You can use the visualization tool from [`QREF`](https://github.com/PsiQ/qref) to plot the hierarchical Directed Acyclic Graph (DAG) of the algorithm you wrote." + }, + { + "cell_type": "markdown", + "id": "c7d1b18a9c90420c", + "metadata": {}, + "source": [ + "
\n", + "

NOTE:

\n", + "

\n", + " To use the qref rendering tool in Jupyter Notebook, ensure the Graphviz software is installed on your OS and that its executables are included in your system variables. For installation instructions, please refer to the Graphviz download page.\n", + "

\n", + "
" + ] + }, + { + "cell_type": "markdown", + "id": "a4f5318f14af6421", + "metadata": {}, + "source": "Then, run:
" + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T20:58:19.585394Z", + "start_time": "2024-06-24T20:58:18.727647Z" + } + }, + "cell_type": "code", + "source": [ + "from qref.experimental.rendering import to_graphviz\n", + "\n", + "# Convert the qref format to Graphviz object\n", + "gv_object = to_graphviz(my_algorithm_qref)\n", + "\n", + "# Render the Graphviz object to a PNG file\n", + "gv_object.render(\"my_algorithm\", format=\"png\")\n", + "\n", + "# Render the Graphviz object in the notebook\n", + "gv_object" + ], + "id": "3464d3b49599fe1d", + "outputs": [ + { + "data": { + "image/svg+xml": "\n\n\n\n\n\n\n\ncluster_.my_algorithm\n\nmy_algorithm\n\n\n\n.my_algorithm.in\n\nin\n\n\n\n.my_algorithm.A\n\nin\n\nA\n\nout\n\n\n\n.my_algorithm.in->.my_algorithm.A:in\n\n\n\n\n\n.my_algorithm.out\n\nout\n\n\n\n.my_algorithm.B\n\nin\n\nB\n\nout\n\n\n\n.my_algorithm.A:out->.my_algorithm.B:in\n\n\n\n\n\n.my_algorithm.B:out->.my_algorithm.out\n\n\n\n\n\n", + "text/plain": [ + "" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "execution_count": 37 + }, + { + "cell_type": "markdown", + "id": "564ecc3e766e7453", + "metadata": {}, "source": [ - "my_algorithm_qref = {'version': 'v1', 'program': my_algorithm}" + "From this graph, you can find both subroutines from the original algorithm, along with their `name`, and`ports`. It provides a general idea of the connectivity between subroutines in the algorithm and shows how information is stored." ] }, { @@ -260,7 +355,7 @@ "id": "5d4c9a76-ab98-4eac-9233-2c5ad62bb770", "metadata": {}, "source": [ - "Below you can find depiction of `my_algorithm`.\n", + " Below you can find depiction of `my_algorithm`.\n", "![title](../images/basic_uncompiled.png)\n", "\n", "We can create `bartiq` `Routine` from `QREF` definition by simply running:" @@ -268,25 +363,20 @@ }, { "cell_type": "code", - "execution_count": 7, "id": "f3bfa36d-b208-4488-abd6-8b020ef6cffc", - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/mstechly/.pyenv/versions/3.9.19/envs/2024-06-04-18-validation/lib/python3.9/site-packages/pydantic/_internal/_generate_schema.py:386: UserWarning: typing_extensions.Self is not a Python type (it may be an instance of an object), Pydantic will allow any object with no validation since we cannot even enforce that the input is an instance of the given type. To get rid of this error wrap the type with `pydantic.SkipValidation`.\n", - " warn(\n", - "/Users/mstechly/.pyenv/versions/3.9.19/envs/2024-06-04-18-validation/lib/python3.9/site-packages/pydantic/_internal/_generate_schema.py:386: UserWarning: typing_extensions.Self is not a Python type (it may be an instance of an object), Pydantic will allow any object with no validation since we cannot even enforce that the input is an instance of the given type. To get rid of this error wrap the type with `pydantic.SkipValidation`.\n", - " warn(\n" - ] + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T20:58:19.598730Z", + "start_time": "2024-06-24T20:58:19.589741Z" } - ], + }, "source": [ "from bartiq.integrations import qref_to_bartiq\n", + "\n", "uncompiled_routine = qref_to_bartiq(my_algorithm_qref)" - ] + ], + "outputs": [], + "execution_count": 38 }, { "cell_type": "markdown", @@ -300,9 +390,16 @@ }, { "cell_type": "code", - "execution_count": 8, "id": "ef6e5a99-9537-4a9a-aea2-ddf4ec28c5df", - "metadata": {}, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T20:58:19.615088Z", + "start_time": "2024-06-24T20:58:19.601383Z" + } + }, + "source": [ + "uncompiled_routine.children[\"A\"].resources" + ], "outputs": [ { "data": { @@ -310,14 +407,12 @@ "{'T_gates': }" ] }, - "execution_count": 8, + "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], - "source": [ - "uncompiled_routine.children[\"A\"].resources" - ] + "execution_count": 39 }, { "cell_type": "markdown", @@ -331,9 +426,16 @@ }, { "cell_type": "code", - "execution_count": 9, "id": "71860050-361e-478e-b6c8-495c9b0bb6fe", - "metadata": {}, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T20:58:19.641159Z", + "start_time": "2024-06-24T20:58:19.625793Z" + } + }, + "source": [ + "uncompiled_routine.ports[\"out\"]" + ], "outputs": [ { "data": { @@ -341,14 +443,12 @@ "Port(my_algorithm.#out, size=None, output)" ] }, - "execution_count": 9, + "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], - "source": [ - "uncompiled_routine.ports[\"out\"]" - ] + "execution_count": 40 }, { "cell_type": "markdown", @@ -360,9 +460,16 @@ }, { "cell_type": "code", - "execution_count": 10, "id": "da6566d1-f14b-4531-8029-4134143f785f", - "metadata": {}, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T20:58:19.665379Z", + "start_time": "2024-06-24T20:58:19.652567Z" + } + }, + "source": [ + "uncompiled_routine.resources" + ], "outputs": [ { "data": { @@ -370,14 +477,12 @@ "{}" ] }, - "execution_count": 10, + "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], - "source": [ - "uncompiled_routine.resources" - ] + "execution_count": 41 }, { "cell_type": "markdown", @@ -394,23 +499,20 @@ }, { "cell_type": "code", - "execution_count": 11, "id": "0163c4b5-16a6-4510-9210-dc84f6711c61", - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/mstechly/Documents/code/2024-06-04-18-validation/bartiq/src/bartiq/compilation/_compile.py:117: UserWarning: Found the following issues with the provided routine after the compilation has finished: [\"Symbol n_b found in subroutine: my_algorithm.B, which is not among top level params: {'n', 'z'}.\"]\n", - " warnings.warn(\n" - ] + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T20:58:19.716932Z", + "start_time": "2024-06-24T20:58:19.667445Z" } - ], + }, "source": [ "from bartiq import compile_routine\n", + "\n", "compiled_routine = compile_routine(uncompiled_routine)" - ] + ], + "outputs": [], + "execution_count": 42 }, { "cell_type": "markdown", @@ -422,9 +524,18 @@ }, { "cell_type": "code", - "execution_count": 12, "id": "e017c894-5203-4958-9f0f-f85904a1ad0b", - "metadata": {}, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T20:58:19.733877Z", + "start_time": "2024-06-24T20:58:19.720524Z" + } + }, + "source": [ + "print(\"T gates for A:\", compiled_routine.children[\"A\"].resources[\"T_gates\"].value)\n", + "print(\"Output size:\", compiled_routine.ports[\"out\"].size)\n", + "print(\"Total T gates:\", compiled_routine.resources[\"T_gates\"].value)" + ], "outputs": [ { "name": "stdout", @@ -436,11 +547,7 @@ ] } ], - "source": [ - "print(\"T gates for A:\", compiled_routine.children[\"A\"].resources[\"T_gates\"].value)\n", - "print(\"Output size:\", compiled_routine.ports[\"out\"].size)\n", - "print(\"Total T gates:\", compiled_routine.resources[\"T_gates\"].value)" - ] + "execution_count": 43 }, { "cell_type": "markdown", @@ -468,9 +575,29 @@ }, { "cell_type": "code", - "execution_count": 13, "id": "4c232bc3-77bb-4bd8-a587-b6591d539c41", - "metadata": {}, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T20:58:19.846940Z", + "start_time": "2024-06-24T20:58:19.739023Z" + } + }, + "source": [ + "from bartiq import evaluate\n", + "\n", + "print(\"Different values of n:\")\n", + "for n in range(6, 16, 2):\n", + " assignments = [f\"n={n}\"]\n", + " evaluated_routine = evaluate(compiled_routine, assignments)\n", + " print(f\"n = {n}, total T gates:\", evaluated_routine.resources[\"T_gates\"].value)\n", + "\n", + "z = 5\n", + "assignments = [f\"n={n}\", f\"z={z}\"]\n", + "evaluated_routine = evaluate(compiled_routine, assignments)\n", + "print(f\"For n={n}, z={z}\")\n", + "\n", + "print(\"Total T gates:\", evaluated_routine.resources[\"T_gates\"].value)" + ], "outputs": [ { "name": "stdout", @@ -487,22 +614,7 @@ ] } ], - "source": [ - "from bartiq import evaluate\n", - "\n", - "print(\"Different values of n:\")\n", - "for n in range(6, 16, 2):\n", - " assignments = [f\"n={n}\"]\n", - " evaluated_routine = evaluate(compiled_routine, assignments)\n", - " print(f\"n = {n}, total T gates:\", evaluated_routine.resources[\"T_gates\"].value)\n", - "\n", - "z=5\n", - "assignments = [f\"n={n}\", f\"z={z}\"]\n", - "evaluated_routine = evaluate(compiled_routine, assignments)\n", - "print(f\"For n={n}, z={z}\")\n", - "\n", - "print(\"Total T gates:\", evaluated_routine.resources[\"T_gates\"].value)" - ] + "execution_count": 44 }, { "cell_type": "markdown", @@ -515,6 +627,7 @@ "- How to construct a simple algorithm in `bartiq`\n", "- How to compile an estimate\n", "- How to evaluate an estimate\n", + "- How to use the `qref` visualization tool to visualize an algorithm\n", "\n", "In the next tutorial we'll cover how to implement a more complex algorithm from a paper." ] @@ -536,7 +649,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.19" + "version": "3.12.3" } }, "nbformat": 4, diff --git a/docs/tutorials/02_alias_sampling_basic.ipynb b/docs/tutorials/02_alias_sampling_basic.ipynb index fb85bee..459c696 100644 --- a/docs/tutorials/02_alias_sampling_basic.ipynb +++ b/docs/tutorials/02_alias_sampling_basic.ipynb @@ -88,10 +88,13 @@ }, { "cell_type": "code", - "execution_count": 1, "id": "fa6d5b43-0eac-4f6f-84de-1df68b8d0d8e", - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T21:06:39.019531Z", + "start_time": "2024-06-24T21:06:39.004429Z" + } + }, "source": [ "usp_dict = {\n", " \"name\": \"usp\",\n", @@ -107,7 +110,9 @@ " \"input_params\": [\"L\"],\n", " \"local_variables\": [\"R=ceiling(log_2(L))\"],\n", "}" - ] + ], + "outputs": [], + "execution_count": 41 }, { "cell_type": "markdown", @@ -122,10 +127,13 @@ }, { "cell_type": "code", - "execution_count": 2, "id": "9cbe85ec-c435-4e60-8561-9b151de06b41", - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T21:06:39.245618Z", + "start_time": "2024-06-24T21:06:39.230480Z" + } + }, "source": [ "had_dict = {\n", " \"name\": \"had\",\n", @@ -135,14 +143,19 @@ " {\"name\": \"out\", \"direction\": \"output\", \"size\": \"N\"},\n", " ],\n", "}" - ] + ], + "outputs": [], + "execution_count": 42 }, { "cell_type": "code", - "execution_count": 3, "id": "ef43c139-304f-473f-b718-16883760658e", - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T21:06:39.372209Z", + "start_time": "2024-06-24T21:06:39.363485Z" + } + }, "source": [ "qrom_dict = {\n", " \"name\": \"qrom\",\n", @@ -159,14 +172,19 @@ " \"input_params\": [\"L\", \"mu\"],\n", " \"local_variables\": [\"R=ceiling(log_2(L))\"],\n", "}" - ] + ], + "outputs": [], + "execution_count": 43 }, { "cell_type": "code", - "execution_count": 4, "id": "698c0b16-2e5f-4c9f-bc81-89f0bbed9611", - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T21:06:39.450728Z", + "start_time": "2024-06-24T21:06:39.442880Z" + } + }, "source": [ "compare_dict = {\n", " \"name\": \"compare\",\n", @@ -181,15 +199,20 @@ " ],\n", " \"resources\": [{\"name\": \"T_gates\", \"type\": \"additive\", \"value\": \"4*mu-4\"}],\n", " \"input_params\": [\"mu\"],\n", - "}\n" - ] + "}" + ], + "outputs": [], + "execution_count": 44 }, { "cell_type": "code", - "execution_count": 5, "id": "da5c61bf-7fb2-47d1-a0f5-d15b32147673", - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T21:06:39.561170Z", + "start_time": "2024-06-24T21:06:39.553767Z" + } + }, "source": [ "swap_dict = {\n", " \"name\": \"swap\",\n", @@ -209,8 +232,10 @@ " {\"source\": \"In_target_1\", \"target\": \"out_target_1\"},\n", " ],\n", " \"input_params\": [\"X\"],\n", - "}\n" - ] + "}" + ], + "outputs": [], + "execution_count": 45 }, { "cell_type": "markdown", @@ -232,10 +257,13 @@ }, { "cell_type": "code", - "execution_count": 6, "id": "b066f011-96e1-4544-81bf-cdc7083b3f73", - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T21:06:39.705603Z", + "start_time": "2024-06-24T21:06:39.658696Z" + } + }, "source": [ "alias_sampling_dict = {\n", " \"name\": \"alias_sampling\",\n", @@ -278,48 +306,123 @@ " {\"source\": \"mu\", \"targets\": [\"qrom.mu\", \"compare.mu\"]},\n", " ],\n", "}" - ] + ], + "outputs": [], + "execution_count": 46 }, { "cell_type": "code", - "execution_count": 7, "id": "e3d17a61-4b9e-48e9-901a-231da501f41b", - "metadata": {}, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T21:06:39.803187Z", + "start_time": "2024-06-24T21:06:39.795127Z" + } + }, + "source": [ + "alias_sampling_qref = {\"version\": \"v1\", \"program\": alias_sampling_dict}" + ], "outputs": [], + "execution_count": 47 + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "Additionally, you can examine how the circuit is represented in `qref` format by visualizing it. ", + "id": "a97dfdd428b669cb" + }, + { + "metadata": {}, + "cell_type": "markdown", "source": [ - "alias_sampling_qref = {'version': 'v1', 'program': alias_sampling_dict}" - ] + "
\n", + "

NOTE:

\n", + "

\n", + " To use the qref rendering tool in Jupyter Notebook, ensure the Graphviz software is installed on your OS and that its executables are included in your system variables. For installation instructions, please refer to the Graphviz download page.\n", + "

\n", + "
" + ], + "id": "dfdf59f8a2ef16c5" }, { + "metadata": {}, + "cell_type": "markdown", + "source": "

Once installed, proceed to visualize the representation of the circuit.

", + "id": "ff432b4dac9921c1" + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T21:06:40.822960Z", + "start_time": "2024-06-24T21:06:39.876790Z" + } + }, "cell_type": "code", - "execution_count": 8, - "id": "5d0d2d69-4b77-4d62-9dd3-2405f4db21ed", + "source": [ + "from qref.experimental.rendering import to_graphviz\n", + "\n", + "# Convert the qref format to Graphviz object\n", + "gv_object = to_graphviz(alias_sampling_qref)\n", + "\n", + "# Render the Graphviz object to a PNG file\n", + "gv_object.render(\"alias_sampling\", format=\"png\")\n", + "gv_object" + ], + "id": "492ad64333e398fa", + "outputs": [ + { + "data": { + "image/svg+xml": "\n\n\n\n\n\n\n\ncluster_.alias_sampling\n\nalias_sampling\n\n\n\n.alias_sampling.In_0\n\nIn_0\n\n\n\n.alias_sampling.usp\n\nin\n\nusp\n\nout\n\n\n\n.alias_sampling.In_0->.alias_sampling.usp:in\n\n\n\n\n\n.alias_sampling.In_1\n\nIn_1\n\n\n\n.alias_sampling.had\n\nin\n\nhad\n\nout\n\n\n\n.alias_sampling.In_1->.alias_sampling.had:in\n\n\n\n\n\n.alias_sampling.In_2\n\nIn_2\n\n\n\n.alias_sampling.qrom\n\nIn_alt\n\nIn_keep\n\nIn_l\n\nqrom\n\nout_alt\n\nout_keep\n\nout_l\n\n\n\n.alias_sampling.In_2->.alias_sampling.qrom:In_alt\n\n\n\n\n\n.alias_sampling.In_3\n\nIn_3\n\n\n\n.alias_sampling.In_3->.alias_sampling.qrom:In_keep\n\n\n\n\n\n.alias_sampling.In_4\n\nIn_4\n\n\n\n.alias_sampling.compare\n\nIn_flag\n\nIn_keep\n\nIn_sigma\n\ncompare\n\nout_flag\n\nout_keep\n\nout_sigma\n\n\n\n.alias_sampling.In_4->.alias_sampling.compare:In_flag\n\n\n\n\n\n.alias_sampling.out_0\n\nout_0\n\n\n\n.alias_sampling.temp_0\n\ntemp_0\n\n\n\n.alias_sampling.temp_1\n\ntemp_1\n\n\n\n.alias_sampling.temp_2\n\ntemp_2\n\n\n\n.alias_sampling.temp_3\n\ntemp_3\n\n\n\n.alias_sampling.compare:out_sigma->.alias_sampling.temp_0\n\n\n\n\n\n.alias_sampling.compare:out_keep->.alias_sampling.temp_2\n\n\n\n\n\n.alias_sampling.swap\n\nIn_control\n\nIn_target_0\n\nIn_target_1\n\nswap\n\nout_control\n\nout_target_0\n\nout_target_1\n\n\n\n.alias_sampling.compare:out_flag->.alias_sampling.swap:In_control\n\n\n\n\n\n.alias_sampling.had:out->.alias_sampling.compare:In_sigma\n\n\n\n\n\n.alias_sampling.qrom:out_keep->.alias_sampling.compare:In_keep\n\n\n\n\n\n.alias_sampling.qrom:out_alt->.alias_sampling.swap:In_target_1\n\n\n\n\n\n.alias_sampling.qrom:out_l->.alias_sampling.swap:In_target_0\n\n\n\n\n\n.alias_sampling.swap:out_target_0->.alias_sampling.out_0\n\n\n\n\n\n.alias_sampling.swap:out_target_1->.alias_sampling.temp_1\n\n\n\n\n\n.alias_sampling.swap:out_control->.alias_sampling.temp_3\n\n\n\n\n\n.alias_sampling.usp:out->.alias_sampling.qrom:In_l\n\n\n\n\n\n", + "text/plain": [ + "" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "execution_count": 48 + }, + { "metadata": {}, - "outputs": [], + "cell_type": "markdown", + "source": "As we expected, the diagram displays five subroutines which are `usp`, `qrom`, `compare`, `had`, `swap`, and their hierarchical connections. Everything appears to be in order! Let's proceed with the compilation.\n", + "id": "2ee860fa5c451aad" + }, + { + "cell_type": "code", + "id": "5d0d2d69-4b77-4d62-9dd3-2405f4db21ed", + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T21:06:40.841356Z", + "start_time": "2024-06-24T21:06:40.830608Z" + } + }, "source": [ "from bartiq.integrations import qref_to_bartiq\n", + "\n", "uncompiled_routine = qref_to_bartiq(alias_sampling_qref)" - ] + ], + "outputs": [], + "execution_count": 49 }, { "cell_type": "code", - "execution_count": 9, "id": "d20ef4d2-9420-4090-a402-ee5f27034524", - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/mstechly/Documents/code/2024-06-04-18-validation/bartiq/src/bartiq/compilation/_compile.py:117: UserWarning: Found the following issues with the provided routine after the compilation has finished: [\"Symbol P_2 found in subroutine: alias_sampling.swap.passthrough_2, which is not among top level params: {'L', 'mu', 'R'}.\", \"Symbol P_1 found in subroutine: alias_sampling.swap.passthrough_1, which is not among top level params: {'L', 'mu', 'R'}.\", \"Symbol P_2 found in subroutine: alias_sampling.swap, which is not among top level params: {'L', 'mu', 'R'}.\"]\n", - " warnings.warn(\n" - ] + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T21:06:40.953312Z", + "start_time": "2024-06-24T21:06:40.846589Z" } - ], + }, "source": [ "from bartiq import compile_routine\n", + "\n", "compiled_routine = compile_routine(uncompiled_routine)" - ] + ], + "outputs": [], + "execution_count": 50 }, { "cell_type": "markdown", @@ -331,9 +434,17 @@ }, { "cell_type": "code", - "execution_count": 10, "id": "dad8df1e-9570-46c5-a422-4f29bba4d52c", - "metadata": {}, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T21:06:40.984789Z", + "start_time": "2024-06-24T21:06:40.967739Z" + } + }, + "source": [ + "for resource in compiled_routine.resources.values():\n", + " print(f\"{resource.name}: {resource.value}\")" + ], "outputs": [ { "name": "stdout", @@ -344,10 +455,7 @@ ] } ], - "source": [ - "for resource in compiled_routine.resources.values():\n", - " print(f\"{resource.name}: {resource.value}\")" - ] + "execution_count": 51 }, { "cell_type": "markdown", @@ -368,9 +476,21 @@ }, { "cell_type": "code", - "execution_count": 11, "id": "becb2091-9faf-4364-ac22-f10240072937", - "metadata": {}, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T21:06:41.110642Z", + "start_time": "2024-06-24T21:06:40.992467Z" + } + }, + "source": [ + "from bartiq import evaluate\n", + "\n", + "assignments = {\"L=120\", \"mu=8\"}\n", + "evaluated_routine = evaluate(compiled_routine, assignments)\n", + "for resource in evaluated_routine.resources.values():\n", + " print(f\"{resource.name}: {resource.value}\")" + ], "outputs": [ { "name": "stdout", @@ -381,13 +501,7 @@ ] } ], - "source": [ - "from bartiq import evaluate\n", - "assignments = {\"L=120\", \"mu=8\"}\n", - "evaluated_routine = evaluate(compiled_routine, assignments)\n", - "for resource in evaluated_routine.resources.values():\n", - " print(f\"{resource.name}: {resource.value}\")" - ] + "execution_count": 52 }, { "cell_type": "markdown", @@ -404,9 +518,18 @@ }, { "cell_type": "code", - "execution_count": 12, "id": "dbf7d3a4-5a58-4dcb-990e-eccd0d7cda9a", - "metadata": {}, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T21:06:41.217016Z", + "start_time": "2024-06-24T21:06:41.114669Z" + } + }, + "source": [ + "compiled_routine = compile_routine(uncompiled_routine, global_functions=[\"O\"])\n", + "for resource in compiled_routine.resources.values():\n", + " print(f\"{resource.name}: {resource.value}\")" + ], "outputs": [ { "name": "stdout", @@ -417,11 +540,7 @@ ] } ], - "source": [ - "compiled_routine = compile_routine(uncompiled_routine, global_functions=[\"O\"])\n", - "for resource in compiled_routine.resources.values():\n", - " print(f\"{resource.name}: {resource.value}\")" - ] + "execution_count": 53 }, { "cell_type": "markdown", @@ -433,30 +552,37 @@ }, { "cell_type": "code", - "execution_count": 13, "id": "2909fb37-6a15-44fc-a80f-46e562220fcb", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "T_gates: 831\n", - "rotations: 2\n" - ] + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T21:06:41.362809Z", + "start_time": "2024-06-24T21:06:41.222791Z" } - ], + }, "source": [ "import math\n", "\n", + "\n", "def big_O(x):\n", " return math.ceil(x)\n", "\n", + "\n", "functions_map = {\"O\": big_O}\n", "evaluated_routine = evaluate(compiled_routine, assignments, functions_map=functions_map)\n", "for resource in evaluated_routine.resources.values():\n", " print(f\"{resource.name}: {resource.value}\")" - ] + ], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "T_gates: 831\n", + "rotations: 2\n" + ] + } + ], + "execution_count": 54 }, { "cell_type": "markdown", @@ -485,30 +611,36 @@ }, { "cell_type": "code", - "execution_count": 14, "id": "1a346534-4f97-4855-8262-2c5df824eb27", - "metadata": {}, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-24T21:06:41.457553Z", + "start_time": "2024-06-24T21:06:41.372181Z" + } + }, + "source": [ + "from bartiq.integrations import explore_routine\n", + "\n", + "explore_routine(evaluated_routine)" + ], "outputs": [ { "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "6551d6b59d664fb3a126943b78445c3c", - "version_major": 2, - "version_minor": 0 - }, "text/plain": [ "HBox(children=(_RoutineTree(multiple_selection=False, nodes=(Node(name='alias_sampling', nodes=(Node(name='com…" - ] + ], + "application/vnd.jupyter.widget-view+json": { + "version_major": 2, + "version_minor": 0, + "model_id": "dbd90e9500c04d6894d2bff15b01a796" + } }, - "execution_count": 14, + "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], - "source": [ - "from bartiq.integrations import explore_routine\n", - "explore_routine(evaluated_routine)" - ] + "execution_count": 55 }, { "cell_type": "markdown", @@ -520,51 +652,28 @@ }, { "cell_type": "code", - "execution_count": 15, "id": "a925abe0-9026-4079-9c5a-8ef5e087a551", "metadata": {}, + "source": [ + "from bartiq.integrations import routine_to_latex\n", + "from IPython.display import Math\n", + "\n", + "Math(routine_to_latex(evaluated_routine))" + ], "outputs": [ { "data": { - "text/latex": [ - "$\\displaystyle \\begin{align}\n", - "&\\text{Routine \\textrm{(alias\\_sampling)}}\\newline\n", - "&\\underline{\\text{Input ports:}}\\\\\n", - "&\\text{In\\_0} = R\\\\\n", - "&\\text{In\\_1} = 8\\\\\n", - "&\\text{In\\_2} = R\\\\\n", - "&\\text{In\\_3} = 8\\\\\n", - "&\\text{In\\_4} = 1\\newline\n", - "&\\underline{\\text{Output ports:}}\\\\\n", - "&\\text{out\\_0} = R\\\\\n", - "&\\text{temp\\_0} = 8\\\\\n", - "&\\text{temp\\_1} = R\\\\\n", - "&\\text{temp\\_2} = 8\\\\\n", - "&\\text{temp\\_3} = 1\\newline\n", - "&\\underline{\\text{Resources:}}\\\\\n", - "&T_{\\text{gates}} = 831\\\\\n", - "&rotations = 2\\\\\n", - "&\\text{usp}.\\!T_{\\text{gates}} = 320\\\\\n", - "&\\text{usp}.\\!rotations = 2\\\\\n", - "&\\text{qrom}.\\!T_{\\text{gates}} = 476\\\\\n", - "&\\text{compare}.\\!T_{\\text{gates}} = 28\\\\\n", - "&\\text{swap}.\\!T_{\\text{gates}} = 7\n", - "\\end{align}$" - ], "text/plain": [ "" - ] + ], + "text/latex": "$\\displaystyle \\begin{align}\n&\\text{Routine \\textrm{(alias\\_sampling)}}\\newline\n&\\underline{\\text{Input ports:}}\\\\\n&\\text{In\\_0} = R\\\\\n&\\text{In\\_1} = 8\\\\\n&\\text{In\\_2} = R\\\\\n&\\text{In\\_3} = 8\\\\\n&\\text{In\\_4} = 1\\newline\n&\\underline{\\text{Output ports:}}\\\\\n&\\text{out\\_0} = R\\\\\n&\\text{temp\\_0} = 8\\\\\n&\\text{temp\\_1} = R\\\\\n&\\text{temp\\_2} = 8\\\\\n&\\text{temp\\_3} = 1\\newline\n&\\underline{\\text{Resources:}}\\\\\n&T_{\\text{gates}} = 831\\\\\n&rotations = 2\\\\\n&\\text{usp}.\\!T_{\\text{gates}} = 320\\\\\n&\\text{usp}.\\!rotations = 2\\\\\n&\\text{qrom}.\\!T_{\\text{gates}} = 476\\\\\n&\\text{compare}.\\!T_{\\text{gates}} = 28\\\\\n&\\text{swap}.\\!T_{\\text{gates}} = 7\n\\end{align}$" }, - "execution_count": 15, + "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], - "source": [ - "from bartiq.integrations import routine_to_latex\n", - "from IPython.display import Math\n", - "Math(routine_to_latex(evaluated_routine))" - ] + "execution_count": 56 }, { "cell_type": "markdown", @@ -578,14 +687,6 @@ "- How to create a routine with multiple resources, `local_variables` and custom functions\n", "- How to use `explore_routine` and latex integration to get most out of `bartiq`" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "5897ad78-b510-4cb8-ac37-7157e054e937", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": {