\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",
+ "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",
+ "