diff --git a/docs/data/alias_sampling_basic.json b/docs/data/alias_sampling_basic.json index 549948c..6278106 100644 --- a/docs/data/alias_sampling_basic.json +++ b/docs/data/alias_sampling_basic.json @@ -1,627 +1,358 @@ { - "name": "", - "type": null, - "ports": { - "in_0": { - "name": "in_0", - "direction": "input", - "size": { - "type": "str", - "value": "R" - }, - "meta": {} - }, - "in_1": { - "name": "in_1", - "direction": "input", - "size": { - "type": "str", - "value": "mu" - }, - "meta": {} - }, - "in_2": { - "name": "in_2", - "direction": "input", - "size": { - "type": "str", - "value": "mu" - }, - "meta": {} - }, - "in_3": { - "name": "in_3", - "direction": "input", - "size": { - "type": "str", - "value": "mu" - }, - "meta": {} - }, - "in_4": { - "name": "in_4", - "direction": "input", - "size": { - "type": "str", - "value": "1" - }, - "meta": {} - }, - "out_0": { - "name": "out_0", - "direction": "output", - "size": null, - "meta": {} - }, - "temp_0": { - "name": "temp_0", - "direction": "output", - "size": null, - "meta": {} - }, - "temp_1": { - "name": "temp_1", - "direction": "output", - "size": null, - "meta": {} - }, - "temp_2": { - "name": "temp_2", - "direction": "output", - "size": null, - "meta": {} - }, - "temp_3": { - "name": "temp_3", - "direction": "output", - "size": null, - "meta": {} - } - }, - "children": { - "usp": { - "name": "usp", - "type": null, - "ports": { - "in": { - "name": "in", - "direction": "input", - "size": { - "type": "str", - "value": "R" - }, - "meta": {} - }, - "out": { - "name": "out", - "direction": "output", - "size": { - "type": "str", - "value": "R" + "version": "v1", + "program": { + "name": "alias_sampling", + "children": [ + { + "name": "usp", + "type": null, + "ports": [ + { + "name": "in", + "direction": "input", + "size": "R" }, - "meta": {} - } - }, - "children": {}, - "connections": [], - "resources": { - "T_gates": { - "name": "T_gates", - "type": "additive", - "value": { - "type": "str", - "value": "8*ceiling(log_2(N))" + { + "name": "out", + "direction": "output", + "size": "R" } - }, - "rotations": { - "name": "rotations", - "type": "additive", - "value": { - "type": "str", + ], + "resources": [ + { + "name": "T_gates", + "type": "additive", + "value": "8*L/multiplicity(2,L)" + }, + { + "name": "rotations", + "type": "additive", "value": "2" } - } + ], + "input_params": [ + "L" + ], + "local_variables": [ + "R=ceiling(log_2(L))" + ] }, - "input_params": [ - "L" - ], - "local_variables": [ - "N=L/multiplicity(2,L)", - "R=ceiling(log_2(L))" - ], - "linked_params": {}, - "meta": {} - }, - "had": { - "name": "had", - "type": null, - "ports": { - "in": { - "name": "in", - "direction": "input", - "size": { - "type": "str", - "value": "N" - }, - "meta": {} - }, - "out": { - "name": "out", - "direction": "output", - "size": { - "type": "str", - "value": "N" + { + "name": "had", + "type": null, + "ports": [ + { + "name": "in", + "direction": "input", + "size": "N" }, - "meta": {} - } + { + "name": "out", + "direction": "output", + "size": "N" + } + ] }, - "children": {}, - "connections": [], - "resources": {}, - "input_params": [], - "local_variables": [], - "linked_params": {}, - "meta": {} - }, - "qrom": { - "name": "qrom", - "type": null, - "ports": { - "in_l": { - "name": "in_l", - "direction": "input", - "size": { - "type": "str", - "value": "R" - }, - "meta": {} - }, - "in_alt": { - "name": "in_alt", - "direction": "input", - "size": { - "type": "str", - "value": "R" + { + "name": "qrom", + "type": null, + "ports": [ + { + "name": "in_l", + "direction": "input", + "size": "R" }, - "meta": {} - }, - "in_keep": { - "name": "in_keep", - "direction": "input", - "size": { - "type": "str", - "value": "mu" + { + "name": "in_alt", + "direction": "input", + "size": "R" }, - "meta": {} - }, - "out_l": { - "name": "out_l", - "direction": "output", - "size": { - "type": "str", - "value": "R" + { + "name": "in_keep", + "direction": "input", + "size": "mu" }, - "meta": {} - }, - "out_alt": { - "name": "out_alt", - "direction": "output", - "size": { - "type": "str", - "value": "R" + { + "name": "out_l", + "direction": "output", + "size": "R" }, - "meta": {} - }, - "out_keep": { - "name": "out_keep", - "direction": "output", - "size": { - "type": "str", - "value": "mu" + { + "name": "out_alt", + "direction": "output", + "size": "R" }, - "meta": {} - } - }, - "children": {}, - "connections": [], - "resources": { - "T_gates": { - "name": "T_gates", - "type": "additive", - "value": { - "type": "str", + { + "name": "out_keep", + "direction": "output", + "size": "mu" + } + ], + "resources": [ + { + "name": "T_gates", + "type": "additive", "value": "4*L-4" } - } + ], + "input_params": [ + "L", + "mu" + ], + "local_variables": [ + "R=ceiling(log_2(L))" + ] }, - "input_params": [ - "L", - "mu" - ], - "local_variables": [ - "R=ceiling(log_2(L))" - ], - "linked_params": {}, - "meta": {} - }, - "compare": { - "name": "compare", - "type": null, - "ports": { - "in_sigma": { - "name": "in_sigma", - "direction": "input", - "size": { - "type": "str", - "value": "mu" - }, - "meta": {} - }, - "in_keep": { - "name": "in_keep", - "direction": "input", - "size": { - "type": "str", - "value": "mu" + { + "name": "compare", + "type": null, + "ports": [ + { + "name": "in_sigma", + "direction": "input", + "size": "mu" }, - "meta": {} - }, - "in_flag": { - "name": "in_flag", - "direction": "input", - "size": { - "type": "str", - "value": "1" + { + "name": "in_keep", + "direction": "input", + "size": "mu" }, - "meta": {} - }, - "out_sigma": { - "name": "out_sigma", - "direction": "output", - "size": { - "type": "str", - "value": "mu" + { + "name": "in_flag", + "direction": "input", + "size": "1" }, - "meta": {} - }, - "out_keep": { - "name": "out_keep", - "direction": "output", - "size": { - "type": "str", - "value": "mu" + { + "name": "out_sigma", + "direction": "output", + "size": "mu" }, - "meta": {} - }, - "out_flag": { - "name": "out_flag", - "direction": "output", - "size": { - "type": "str", - "value": "1" + { + "name": "out_keep", + "direction": "output", + "size": "mu" }, - "meta": {} - } - }, - "children": {}, - "connections": [], - "resources": { - "T_gates": { - "name": "T_gates", - "type": "additive", - "value": { - "type": "str", + { + "name": "out_flag", + "direction": "output", + "size": "1" + } + ], + "resources": [ + { + "name": "T_gates", + "type": "additive", "value": "4*mu-4" } - } + ], + "input_params": [ + "mu" + ] }, - "input_params": [ - "mu" - ], - "local_variables": [], - "linked_params": {}, - "meta": {} - }, - "swap": { - "name": "swap", - "type": null, - "ports": { - "in_control": { - "name": "in_control", - "direction": "input", - "size": null, - "meta": {} - }, - "in_target_0": { - "name": "in_target_0", - "direction": "input", - "size": null, - "meta": {} - }, - "in_target_1": { - "name": "in_target_1", - "direction": "input", - "size": null, - "meta": {} - }, - "out_control": { - "name": "out_control", - "direction": "output", - "size": null, - "meta": {} - }, - "out_target_0": { - "name": "out_target_0", - "direction": "output", - "size": null, - "meta": {} - }, - "out_target_1": { - "name": "out_target_1", - "direction": "output", - "size": null, - "meta": {} - } - }, - "children": { - "passthrough_0": { - "name": "passthrough_0", - "type": "passthrough", - "ports": { - "in_0": { - "name": "in_0", - "direction": "input", - "size": { - "type": "str", - "value": "P_0" - }, - "meta": {} - }, - "out_0": { - "name": "out_0", - "direction": "output", - "size": { - "type": "str", - "value": "P_0" - }, - "meta": {} - } + { + "name": "swap", + "type": null, + "ports": [ + { + "name": "in_control", + "direction": "input", + "size": "1" }, - "children": {}, - "connections": [], - "resources": {}, - "input_params": [], - "local_variables": [], - "linked_params": {}, - "meta": {} - }, - "passthrough_1": { - "name": "passthrough_1", - "type": "passthrough", - "ports": { - "in_0": { - "name": "in_0", - "direction": "input", - "size": { - "type": "str", - "value": "P_1" - }, - "meta": {} - }, - "out_0": { - "name": "out_0", - "direction": "output", - "size": { - "type": "str", - "value": "P_1" - }, - "meta": {} - } + { + "name": "in_target_0", + "direction": "input", + "size": "X" }, - "children": {}, - "connections": [], - "resources": {}, - "input_params": [], - "local_variables": [], - "linked_params": {}, - "meta": {} - }, - "passthrough_2": { - "name": "passthrough_2", - "type": "passthrough", - "ports": { - "in_0": { - "name": "in_0", - "direction": "input", - "size": { - "type": "str", - "value": "P_2" - }, - "meta": {} - }, - "out_0": { - "name": "out_0", - "direction": "output", - "size": { - "type": "str", - "value": "P_2" - }, - "meta": {} - } + { + "name": "in_target_1", + "direction": "input", + "size": "X" }, - "children": {}, - "connections": [], - "resources": {}, - "input_params": [], - "local_variables": [], - "linked_params": {}, - "meta": {} - } - }, - "connections": [ - { - "source": "in_control", - "target": "passthrough_0.in_0" - }, - { - "source": "in_target_0", - "target": "passthrough_1.in_0" - }, - { - "source": "in_target_1", - "target": "passthrough_2.in_0" - }, - { - "source": "passthrough_0.out_0", - "target": "out_control" - }, - { - "source": "passthrough_1.out_0", - "target": "out_target_0" - }, - { - "source": "passthrough_2.out_0", - "target": "out_target_1" - } - ], - "resources": { - "T_gates": { - "name": "T_gates", - "type": "additive", - "value": { - "type": "str", + { + "name": "out_control", + "direction": "output", + "size": null + }, + { + "name": "out_target_0", + "direction": "output", + "size": null + }, + { + "name": "out_target_1", + "direction": "output", + "size": null + } + ], + "resources": [ + { + "name": "T_gates", + "type": "additive", "value": "O(log_2(X))" } - } + ], + "connections": [ + { + "source": "in_control", + "target": "out_control" + }, + { + "source": "in_target_0", + "target": "out_target_0" + }, + { + "source": "in_target_1", + "target": "out_target_1" + } + ], + "input_params": [ + "X" + ] + } + ], + "type": null, + "ports": [ + { + "name": "in_0", + "direction": "input", + "size": "R" + }, + { + "name": "in_1", + "direction": "input", + "size": "mu" }, - "input_params": [ - "X" - ], - "local_variables": [], - "linked_params": {}, - "meta": {} - } - }, - "connections": [ - { - "source": "compare.out_flag", - "target": "swap.in_control" - }, - { - "source": "compare.out_keep", - "target": "temp_2" - }, - { - "source": "compare.out_sigma", - "target": "temp_0" - }, - { - "source": "had.out", - "target": "compare.in_sigma" - }, - { - "source": "in_0", - "target": "usp.in" - }, - { - "source": "in_1", - "target": "had.in" - }, - { - "source": "in_2", - "target": "qrom.in_alt" - }, - { - "source": "in_3", - "target": "qrom.in_keep" - }, - { - "source": "in_4", - "target": "compare.in_flag" - }, - { - "source": "qrom.out_alt", - "target": "swap.in_target_1" - }, - { - "source": "qrom.out_keep", - "target": "compare.in_keep" - }, - { - "source": "qrom.out_l", - "target": "swap.in_target_0" - }, - { - "source": "swap.out_control", - "target": "temp_3" - }, - { - "source": "swap.out_target_0", - "target": "out_0" - }, - { - "source": "swap.out_target_1", - "target": "temp_1" - }, - { - "source": "usp.out", - "target": "qrom.in_l" - } - ], - "resources": { - "T_gates": { - "name": "T_gates", - "type": "additive", - "value": { - "type": "str", - "value": "sum(usp.T_gates,qrom.T_gates,compare.T_gates,swap.T_gates)" + { + "name": "in_2", + "direction": "input", + "size": "R" + }, + { + "name": "in_3", + "direction": "input", + "size": "mu" + }, + { + "name": "in_4", + "direction": "input", + "size": "1" + }, + { + "name": "out_0", + "direction": "output", + "size": null + }, + { + "name": "temp_0", + "direction": "output", + "size": null + }, + { + "name": "temp_1", + "direction": "output", + "size": null + }, + { + "name": "temp_2", + "direction": "output", + "size": null + }, + { + "name": "temp_3", + "direction": "output", + "size": null } - }, - "rotations": { - "name": "rotations", - "type": "additive", - "value": { - "type": "str", - "value": "sum(usp.rotations)" + ], + "connections": [ + { + "source": "in_0", + "target": "usp.in" + }, + { + "source": "in_1", + "target": "had.in" + }, + { + "source": "in_2", + "target": "qrom.in_alt" + }, + { + "source": "in_3", + "target": "qrom.in_keep" + }, + { + "source": "in_4", + "target": "compare.in_flag" + }, + { + "source": "usp.out", + "target": "qrom.in_l" + }, + { + "source": "had.out", + "target": "compare.in_sigma" + }, + { + "source": "qrom.out_l", + "target": "swap.in_target_0" + }, + { + "source": "qrom.out_alt", + "target": "swap.in_target_1" + }, + { + "source": "qrom.out_keep", + "target": "compare.in_keep" + }, + { + "source": "compare.out_flag", + "target": "swap.in_control" + }, + { + "source": "swap.out_target_0", + "target": "out_0" + }, + { + "source": "compare.out_sigma", + "target": "temp_0" + }, + { + "source": "swap.out_target_1", + "target": "temp_1" + }, + { + "source": "compare.out_keep", + "target": "temp_2" + }, + { + "source": "swap.out_control", + "target": "temp_3" } - } - }, - "input_params": [ - "L", - "mu" - ], - "local_variables": [ - "R=ceiling(log_2(L))" - ], - "linked_params": { - "L": [ - [ - "usp", - "L" - ], - [ - "qrom", - "L" - ], - [ - "swap", - "X" - ] ], - "mu": [ - [ - "had", - "mu" - ], - [ - "qrom", - "mu" - ], - [ - "compare", - "mu" - ] + "input_params": [ + "mu", + "L" + ], + "local_variables": [ + "R=ceiling(log_2(L))" + ], + "linked_params": [ + { + "source": "L", + "targets": [ + "usp.L", + "qrom.L", + "swap.X" + ] + }, + { + "source": "mu", + "targets": [ + "had.mu", + "qrom.mu", + "compare.mu" + ] + } ] - }, - "meta": {} + } } \ No newline at end of file diff --git a/docs/data/basic_example.json b/docs/data/basic_example.json index 4aa8958..4a190d2 100644 --- a/docs/data/basic_example.json +++ b/docs/data/basic_example.json @@ -1,146 +1,96 @@ { - "name": "", - "type": null, - "ports": { - "in": { - "name": "in", - "direction": "input", - "size": { - "type": "str", - "value": "N" + "version": "v1", + "program": { + "name": "my_algorithm", + "type": null, + "ports": [ + { + "name": "in", + "direction": "input", + "size": "N" }, - "meta": {} - }, - "out": { - "name": "out", - "direction": "output", - "size": null, - "meta": {} - } - }, - "children": { - "a": { - "name": "a", - "type": null, - "ports": { - "in": { - "name": "in", - "direction": "input", - "size": { - "type": "str", - "value": "N_a" - }, - "meta": {} - }, - "out": { - "name": "out", - "direction": "output", - "size": { - "type": "str", - "value": "N_a" + { + "name": "out", + "direction": "output", + "size": null + } + ], + "children": [ + { + "name": "a", + "type": null, + "ports": [ + { + "name": "in", + "direction": "input", + "size": "N_a" }, - "meta": {} - } - }, - "children": {}, - "connections": [], - "resources": { - "T_gates": { - "name": "T_gates", - "type": "additive", - "value": { - "type": "str", + { + "name": "out", + "direction": "output", + "size": "N_a" + } + ], + "resources": [ + { + "name": "T_gates", + "type": "additive", "value": "2*N_a" } - } + ], + "input_params": [ + "N_a" + ] }, - "input_params": [ - "N_a" - ], - "local_variables": [], - "linked_params": {}, - "meta": {} - }, - "b": { - "name": "b", - "type": null, - "ports": { - "in": { - "name": "in", - "direction": "input", - "size": { - "type": "str", - "value": "N_b" - }, - "meta": {} - }, - "out": { - "name": "out", - "direction": "output", - "size": { - "type": "str", - "value": "N_b" + { + "name": "b", + "type": null, + "ports": [ + { + "name": "in", + "direction": "input", + "size": "N_b" }, - "meta": {} - } - }, - "children": {}, - "connections": [], - "resources": { - "T_gates": { - "name": "T_gates", - "type": "additive", - "value": { - "type": "str", + { + "name": "out", + "direction": "output", + "size": "N_b" + } + ], + "resources": [ + { + "name": "T_gates", + "type": "additive", "value": "N_b*ceil(log_2(N_b))" } - } + ], + "input_params": [ + "N_b" + ] + } + ], + "connections": [ + { + "source": "in", + "target": "a.in" }, - "input_params": [ - "N_b" - ], - "local_variables": [], - "linked_params": {}, - "meta": {} - } - }, - "connections": [ - { - "source": "a.out", - "target": "b.in" - }, - { - "source": "b.out", - "target": "out" - }, - { - "source": "in", - "target": "a.in" - } - ], - "resources": { - "T_gates": { - "name": "T_gates", - "type": "additive", - "value": { - "type": "str", - "value": "sum(a.T_gates,b.T_gates)" + { + "source": "a.out", + "target": "b.in" + }, + { + "source": "b.out", + "target": "out" + } + ], + "linked_params": [ + { + "source": "N", + "targets": [ + "a.N_a", + "b.N_b" + ] } - } - }, - "input_params": [], - "local_variables": [], - "linked_params": { - "N": [ - [ - "a", - "N_a" - ], - [ - "b", - "N_b" - ] ] - }, - "meta": {} + } } \ No newline at end of file diff --git a/docs/images/basic_compiled.png b/docs/images/basic_compiled.png index d079686..1c59ee5 100644 Binary files a/docs/images/basic_compiled.png and b/docs/images/basic_compiled.png differ diff --git a/docs/images/basic_example.png b/docs/images/basic_example.png new file mode 100644 index 0000000..f5785a1 Binary files /dev/null and b/docs/images/basic_example.png differ diff --git a/docs/images/basic_uncompiled.png b/docs/images/basic_uncompiled.png index da883a6..a2d0a23 100644 Binary files a/docs/images/basic_uncompiled.png and b/docs/images/basic_uncompiled.png differ diff --git a/docs/tutorials/01_basic_example.ipynb b/docs/tutorials/01_basic_example.ipynb index ffffc18..4968dcf 100644 --- a/docs/tutorials/01_basic_example.ipynb +++ b/docs/tutorials/01_basic_example.ipynb @@ -45,7 +45,7 @@ "id": "7f36d3b3-e451-4c44-8a89-bb2250f54f51", "metadata": {}, "source": [ - "TODO: picture from QAHLO" + "![title](../images/basic_example.png)" ] }, { @@ -61,7 +61,7 @@ "- It takes in a register of size \"N\".\n", "\n", "How to express it in `bartiq`? \n", - "We do that using `QREF` format (TODO: link) – it's a format for expressing algorithms that we developed with running QREs in mind. So let's write our first routine:" + "We do that using [`QREF` format](https://github.com/PsiQ/qref) – it's a format for expressing algorithms that we developed with running QREs in mind. So let's write our first routine:" ] }, { @@ -74,18 +74,10 @@ "my_algorithm = {\n", " \"name\": \"my_algorithm\",\n", " \"type\": None,\n", - " \"ports\": {\n", - " \"in\": {\n", - " \"name\": \"in\",\n", - " \"direction\": \"input\",\n", - " \"size\": \"N\"\n", - " },\n", - " \"out\": {\n", - " \"name\": \"out\",\n", - " \"direction\": \"output\",\n", - " \"size\": None\n", - " }\n", - " },\n", + " \"ports\": [\n", + " {\"name\": \"in\", \"direction\": \"input\", \"size\": \"N\"},\n", + " {\"name\": \"out\", \"direction\": \"output\", \"size\": None},\n", + " ],\n", "}" ] }, @@ -112,21 +104,13 @@ "outputs": [], "source": [ "routine_a = {\n", - " \"name\": \"a\",\n", - " \"type\": None,\n", - " \"ports\": {\n", - " \"in\": {\n", - " \"name\": \"in\",\n", - " \"direction\": \"input\",\n", - " \"size\": \"N_a\"\n", - " },\n", - " \"out\": {\n", - " \"name\": \"out\",\n", - " \"direction\": \"output\",\n", - " \"size\": \"N_a\"\n", - " }\n", - " },\n", - " }" + " \"name\": \"a\",\n", + " \"type\": None,\n", + " \"ports\": [\n", + " {\"name\": \"in\", \"direction\": \"input\", \"size\": \"N_a\"},\n", + " {\"name\": \"out\", \"direction\": \"output\", \"size\": \"N_a\"},\n", + " ],\n", + "}" ] }, { @@ -137,21 +121,13 @@ "outputs": [], "source": [ "routine_b = {\n", - " \"name\": \"b\",\n", - " \"type\": None,\n", - " \"ports\": {\n", - " \"in\": {\n", - " \"name\": \"in\",\n", - " \"direction\": \"input\",\n", - " \"size\": \"N_b\"\n", - " },\n", - " \"out\": {\n", - " \"name\": \"out\",\n", - " \"direction\": \"output\",\n", - " \"size\": \"N_b\"\n", - " }\n", - " },\n", - " }" + " \"name\": \"b\",\n", + " \"type\": None,\n", + " \"ports\": [\n", + " {\"name\": \"in\", \"direction\": \"input\", \"size\": \"N_b\"},\n", + " {\"name\": \"out\", \"direction\": \"output\", \"size\": \"N_b\"},\n", + " ],\n", + "}" ] }, { @@ -170,24 +146,12 @@ "metadata": {}, "outputs": [], "source": [ - "routine_a[\"resources\"] = {\n", - " \"T_gates\": {\n", - " \"name\": \"T_gates\",\n", - " \"type\": \"additive\",\n", - " \"value\": \"2*N_a\",\n", - " }\n", - " }\n", + "routine_a[\"resources\"] = [{\"name\": \"T_gates\", \"type\": \"additive\", \"value\": \"2*N_a\"}]\n", "\n", - "routine_b[\"resources\"] = {\n", - " \"T_gates\": {\n", - " \"name\": \"T_gates\",\n", - " \"type\": \"additive\",\n", - " \"value\": \"N_b*ceil(log_2(N_b))\"\n", - " }\n", - " }\n", + "routine_b[\"resources\"] = [{\"name\": \"T_gates\", \"type\": \"additive\", \"value\": \"N_b*ceil(log_2(N_b))\"}]\n", "\n", "routine_a[\"input_params\"] = [\"N_a\"]\n", - "routine_b[\"input_params\"] = [\"N_b\"]" + "routine_b[\"input_params\"] = [\"N_b\"]\n" ] }, { @@ -214,15 +178,13 @@ "metadata": {}, "outputs": [], "source": [ - "my_algorithm[\"children\"] = {\"a\": routine_a, \"b\": routine_b}\n", + "my_algorithm[\"children\"] = [routine_a, routine_b]\n", "my_algorithm[\"connections\"] = [\n", - " {\"source\": \"in\",\"target\": \"a.in\"},\n", - " {\"source\": \"a.out\",\"target\": \"b.in\"},\n", - " {\"source\": \"b.out\",\"target\": \"out\"}\n", - " ]\n", - "my_algorithm[\"linked_params\"] = {\n", - " \"N\": [(\"a\",\"N_a\"), (\"b\",\"N_b\")]\n", - " }" + " {\"source\": \"in\", \"target\": \"a.in\"},\n", + " {\"source\": \"a.out\", \"target\": \"b.in\"},\n", + " {\"source\": \"b.out\", \"target\": \"out\"},\n", + "]\n", + "my_algorithm[\"linked_params\"] = [{\"source\": \"N\", \"targets\": [\"a.N_a\", \"b.N_b\"]}]" ] }, { @@ -234,9 +196,33 @@ "We got:\n", "- `connections` – how ports are connected. Each connection has source and target.\n", "- `children`: specifies what subroutines does a particular routine consist of.\n", - "- `linked_params`: tells us how the parameters of the parent are linked to the parameters of children. In this case, it tells us that parameter `N` from this routine translates to `N_a` in child `a` and `N_b` in child `b`.\n", - "\n", - "Now we can translate our algorithm into a proper `bartiq` routine and see what's the total cost of `my_algorithm:" + "- `linked_params`: tells us how the parameters of the parent are linked to the parameters of children. In this case, it tells us that parameter `N` from this routine translates to `N_a` in child `a` and `N_b` in child `b`." + ] + }, + { + "cell_type": "markdown", + "id": "cd07e6b5-3b66-400f-8388-649a3129588b", + "metadata": {}, + "source": [ + "The last step is just a formality to indicate which version of QREF schema we use:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "ac4be2b0-f1be-46db-a42d-9f204727db7b", + "metadata": {}, + "outputs": [], + "source": [ + "my_algorithm_qref = {'version': 'v1', 'program': my_algorithm}" + ] + }, + { + "cell_type": "markdown", + "id": "5fe2e5a6-0487-44d1-8071-3f692ee845ba", + "metadata": {}, + "source": [ + "Now we can translate our algorithm into a proper `bartiq` routine and see what's the total cost of `my_algorithm`." ] }, { @@ -268,18 +254,18 @@ "Below you can find depiction of `my_algorithm`.\n", "![title](../images/basic_uncompiled.png)\n", "\n", - "We can create `bartiq` `Routine` out of it by simply running:" + "We can create `bartiq` `Routine` from `QREF` definition by simply running:" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "id": "f3bfa36d-b208-4488-abd6-8b020ef6cffc", "metadata": {}, "outputs": [], "source": [ - "from bartiq import Routine\n", - "uncompiled_routine = Routine(**my_algorithm)" + "from bartiq.integrations import qref_to_bartiq\n", + "uncompiled_routine = qref_to_bartiq(my_algorithm_qref)" ] }, { @@ -294,7 +280,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "id": "ef6e5a99-9537-4a9a-aea2-ddf4ec28c5df", "metadata": {}, "outputs": [ @@ -304,7 +290,7 @@ "{'T_gates': }" ] }, - "execution_count": 7, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -325,7 +311,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "id": "71860050-361e-478e-b6c8-495c9b0bb6fe", "metadata": {}, "outputs": [ @@ -335,7 +321,7 @@ "Port(name='out', parent=, direction='output', size=None, meta={})" ] }, - "execution_count": 8, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -354,7 +340,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 10, "id": "da6566d1-f14b-4531-8029-4134143f785f", "metadata": {}, "outputs": [ @@ -364,7 +350,7 @@ "{}" ] }, - "execution_count": 9, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -386,12 +372,12 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 11, "id": "0163c4b5-16a6-4510-9210-dc84f6711c61", "metadata": {}, "outputs": [], "source": [ - "from bartiq import Routine, compile_routine\n", + "from bartiq import compile_routine\n", "compiled_routine = compile_routine(uncompiled_routine)" ] }, @@ -405,7 +391,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 12, "id": "e017c894-5203-4958-9f0f-f85904a1ad0b", "metadata": {}, "outputs": [ @@ -443,7 +429,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 13, "id": "4c232bc3-77bb-4bd8-a587-b6591d539c41", "metadata": {}, "outputs": [ @@ -468,18 +454,6 @@ " print(f\"N = {N}, total T gates:\", evaluated_routine.resources[\"T_gates\"].value)" ] }, - { - "cell_type": "markdown", - "id": "d12048df-2964-4306-b50f-c067ec351c8a", - "metadata": {}, - "source": [ - "

NOTE:

\n", - "\n", - "- You might wonder – if we have `Routine` class, why do we define everything as dictionaries and do `Routine(**my_dict)` at the very end? We cover that in more details in TODO, but the short answer is, that created `Routine` objects by hand might lead to some very hard to trace bugs, so it's our recommendation to always define them as dictionaries.\n", - "\n", - "
" - ] - }, { "cell_type": "markdown", "id": "c892d377-fc47-4796-bc0a-fcc28133b8c9", @@ -492,7 +466,7 @@ "- How to perform compile an estimate\n", "- How to evaluate an estimate\n", "\n", - "In the next tutorial we'll cover how to implement a more complex algorithm from a paper – you can find the link [here](TODO)." + "In the next tutorial we'll cover how to implement a more complex algorithm from a paper – you can find the link [here](02_alias_sampling_basic.ipynb)." ] } ], @@ -512,7 +486,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.10" + "version": "3.9.19" } }, "nbformat": 4, diff --git a/docs/tutorials/02_alias_sampling_basic.ipynb b/docs/tutorials/02_alias_sampling_basic.ipynb index f609532..b3176a6 100644 --- a/docs/tutorials/02_alias_sampling_basic.ipynb +++ b/docs/tutorials/02_alias_sampling_basic.ipynb @@ -42,11 +42,10 @@ "- $L$ – number of states we want to load.\n", "- $\\mu$ – bits of precision for coefficients we load.\n", "- $k$ is the greatest common factor of $L$. This is not a widely mathematical operation, but fortunately in `bartiq` we have a shorthand for that, and we can say that `k = multiplicity(2, L)`. You can check how [it's defined in sympy](https://docs.sympy.org/latest/modules/ntheory.html#sympy.ntheory.factor_.multiplicity).\n", - "- $N = \\frac{L}{2^k}$. This is one of the confusing places as the $L$ used in Fig. 11 and Fig. 12 are two different things... Let's don't worry too much about the interpretation of this number and just call it $N$.\n", "\n", "Given all that – what are the costs of our routines?\n", "\n", - "- Uniform: $8log_2(N)$ T gates and a 2 rotation gates. It comes Fig 12 and its caption. We omit the $O(log(1/\\epsilon)$ term, because it corresponds to those two rotations that we count separately.\n", + "- Uniform: $8log_2(\\frac{L}{k})$ T gates and a 2 rotation gates. It comes Fig 12 and its caption. We omit the $O(log(1/\\epsilon)$ term, because it corresponds to those two rotations that we count separately.\n", "- Hadamards: It doesn't require any T gates.\n", "- QROM: $4L-4$ – this comes from Fig. 10.\n", "- compare: $4\\mu-4$ – this is not stated explicitly in the text, but comes from a careful analysis of the caption of Fig. 11.\n", @@ -80,35 +79,19 @@ "outputs": [], "source": [ "usp_dict = {\n", - " \"name\": \"usp\",\n", - " \"type\": None,\n", - " \"ports\": {\n", - " \"in\": {\n", - " \"name\": \"in\",\n", - " \"direction\": \"input\",\n", - " \"size\": \"R\" \n", - " },\n", - " \"out\": {\n", - " \"name\": \"out\",\n", - " \"direction\": \"output\",\n", - " \"size\": \"R\"\n", - " }\n", - " },\n", - " \"input_params\": [\"L\"],\n", - " \"local_variables\": [\"N=L/multiplicity(2,L)\", \"R=ceiling(log_2(L))\"],\n", - " \"resources\":{\n", - " \"T_gates\": {\n", - " \"name\": \"T_gates\",\n", - " \"type\": \"additive\",\n", - " \"value\": \"8*ceiling(log_2(N))\",\n", - " },\n", - " \"rotations\": {\n", - " \"name\": \"rotations\",\n", - " \"type\": \"additive\",\n", - " \"value\": \"2\",\n", - " }\n", - " }\n", - " }" + " \"name\": \"usp\",\n", + " \"type\": None,\n", + " \"ports\": [\n", + " {\"name\": \"in\", \"direction\": \"input\", \"size\": \"R\"},\n", + " {\"name\": \"out\", \"direction\": \"output\", \"size\": \"R\"},\n", + " ],\n", + " \"resources\": [\n", + " {\"name\": \"T_gates\", \"type\": \"additive\", \"value\": \"8*L/multiplicity(2,L)\"},\n", + " {\"name\": \"rotations\", \"type\": \"additive\", \"value\": \"2\"},\n", + " ],\n", + " \"input_params\": [\"L\"],\n", + " \"local_variables\": [\"R=ceiling(log_2(L))\"],\n", + "}" ] }, { @@ -130,21 +113,13 @@ "outputs": [], "source": [ "had_dict = {\n", - " \"name\": \"had\",\n", - " \"type\": None,\n", - " \"ports\": {\n", - " \"in\": {\n", - " \"name\": \"in\",\n", - " \"direction\": \"input\",\n", - " \"size\": \"N\"\n", - " },\n", - " \"out\": {\n", - " \"name\": \"out\",\n", - " \"direction\": \"output\",\n", - " \"size\": \"N\"\n", - " }\n", - " },\n", - " }" + " \"name\": \"had\",\n", + " \"type\": None,\n", + " \"ports\": [\n", + " {\"name\": \"in\", \"direction\": \"input\", \"size\": \"N\"},\n", + " {\"name\": \"out\", \"direction\": \"output\", \"size\": \"N\"},\n", + " ],\n", + "}" ] }, { @@ -155,51 +130,20 @@ "outputs": [], "source": [ "qrom_dict = {\n", - " \"name\": \"qrom\",\n", - " \"type\": None,\n", - " \"ports\": {\n", - " \"in_l\": {\n", - " \"name\": \"in_l\",\n", - " \"direction\": \"input\",\n", - " \"size\": \"R\"\n", - " },\n", - " \"in_alt\": {\n", - " \"name\": \"in_alt\",\n", - " \"direction\": \"input\",\n", - " \"size\": \"R\"\n", - " },\n", - " \"in_keep\": {\n", - " \"name\": \"in_keep\",\n", - " \"direction\": \"input\",\n", - " \"size\": \"mu\"\n", - " },\n", - "\n", - " \"out_l\": {\n", - " \"name\": \"out_l\",\n", - " \"direction\": \"output\",\n", - " \"size\": \"R\"\n", - " },\n", - " \"out_alt\": {\n", - " \"name\": \"out_alt\",\n", - " \"direction\": \"output\",\n", - " \"size\": \"R\"\n", - " },\n", - " \"out_keep\": {\n", - " \"name\": \"out_keep\",\n", - " \"direction\": \"output\",\n", - " \"size\": \"mu\"\n", - " }\n", - " },\n", - " \"input_params\": [\"L\", \"mu\"],\n", - " \"local_variables\": [\"R=ceiling(log_2(L))\"],\n", - " \"resources\":{\n", - " \"T_gates\": {\n", - " \"name\": \"T_gates\",\n", - " \"type\": \"additive\",\n", - " \"value\": \"4*L-4\",\n", - " },\n", - " }\n", - " }" + " \"name\": \"qrom\",\n", + " \"type\": None,\n", + " \"ports\": [\n", + " {\"name\": \"in_l\", \"direction\": \"input\", \"size\": \"R\"},\n", + " {\"name\": \"in_alt\", \"direction\": \"input\", \"size\": \"R\"},\n", + " {\"name\": \"in_keep\", \"direction\": \"input\", \"size\": \"mu\"},\n", + " {\"name\": \"out_l\", \"direction\": \"output\", \"size\": \"R\"},\n", + " {\"name\": \"out_alt\", \"direction\": \"output\", \"size\": \"R\"},\n", + " {\"name\": \"out_keep\", \"direction\": \"output\", \"size\": \"mu\"},\n", + " ],\n", + " \"resources\": [{\"name\": \"T_gates\", \"type\": \"additive\", \"value\": \"4*L-4\"}],\n", + " \"input_params\": [\"L\", \"mu\"],\n", + " \"local_variables\": [\"R=ceiling(log_2(L))\"],\n", + "}" ] }, { @@ -210,51 +154,19 @@ "outputs": [], "source": [ "compare_dict = {\n", - " \"name\": \"compare\",\n", - " \"type\": None,\n", - " \"ports\": {\n", - " \"in_sigma\": {\n", - " \"name\": \"in_sigma\",\n", - " \"direction\": \"input\",\n", - " \"size\": \"mu\"\n", - " },\n", - " \"in_keep\": {\n", - " \"name\": \"in_keep\",\n", - " \"direction\": \"input\",\n", - " \"size\": \"mu\"\n", - " },\n", - " \"in_flag\": {\n", - " \"name\": \"in_flag\",\n", - " \"direction\": \"input\",\n", - " \"size\": \"1\"\n", - " },\n", - "\n", - " \"out_sigma\": {\n", - " \"name\": \"out_sigma\",\n", - " \"direction\": \"output\",\n", - " \"size\": \"mu\"\n", - " },\n", - " \"out_keep\": {\n", - " \"name\": \"out_keep\",\n", - " \"direction\": \"output\",\n", - " \"size\": \"mu\"\n", - " },\n", - " \"out_flag\": {\n", - " \"name\": \"out_flag\",\n", - " \"direction\": \"output\",\n", - " \"size\": \"1\"\n", - " },\n", - "\n", - " },\n", - " \"input_params\": [\"mu\"],\n", - " \"resources\":{\n", - " \"T_gates\": {\n", - " \"name\": \"T_gates\",\n", - " \"type\": \"additive\",\n", - " \"value\": \"4*mu-4\",\n", - " },\n", - " }\n", - " }" + " \"name\": \"compare\",\n", + " \"type\": None,\n", + " \"ports\": [\n", + " {\"name\": \"in_sigma\", \"direction\": \"input\", \"size\": \"mu\"},\n", + " {\"name\": \"in_keep\", \"direction\": \"input\", \"size\": \"mu\"},\n", + " {\"name\": \"in_flag\", \"direction\": \"input\", \"size\": \"1\"},\n", + " {\"name\": \"out_sigma\", \"direction\": \"output\", \"size\": \"mu\"},\n", + " {\"name\": \"out_keep\", \"direction\": \"output\", \"size\": \"mu\"},\n", + " {\"name\": \"out_flag\", \"direction\": \"output\", \"size\": \"1\"},\n", + " ],\n", + " \"resources\": [{\"name\": \"T_gates\", \"type\": \"additive\", \"value\": \"4*mu-4\"}],\n", + " \"input_params\": [\"mu\"],\n", + "}\n" ] }, { @@ -265,65 +177,24 @@ "outputs": [], "source": [ "swap_dict = {\n", - " \"name\": \"swap\",\n", - " \"type\": None,\n", - " \"ports\": {\n", - " \"in_control\": {\n", - " \"name\": \"in_control\",\n", - " \"direction\": \"input\",\n", - " \"size\": \"1\"\n", - " },\n", - " \"in_target_0\": {\n", - " \"name\": \"in_target_0\",\n", - " \"direction\": \"input\",\n", - " \"size\": \"X\"\n", - " },\n", - " \"in_target_1\": {\n", - " \"name\": \"in_target_1\",\n", - " \"direction\": \"input\",\n", - " \"size\": \"X\"\n", - " },\n", - " \"out_control\": {\n", - " \"name\": \"out_control\",\n", - " \"direction\": \"output\",\n", - " \"size\": None\n", - " },\n", - " \"out_target_0\": {\n", - " \"name\": \"out_target_0\",\n", - " \"direction\": \"output\",\n", - " \"size\": None\n", - " },\n", - " \"out_target_1\": {\n", - " \"name\": \"out_target_1\",\n", - " \"direction\": \"output\",\n", - " \"size\": None\n", - " },\n", - "\n", - " },\n", - " \"connections\": [\n", - " {\n", - " \"source\": \"in_control\",\n", - " \"target\": \"out_control\"\n", - " },\n", - " {\n", - " \"source\": \"in_target_0\",\n", - " \"target\": \"out_target_0\"\n", - " },\n", - " {\n", - " \"source\": \"in_target_1\",\n", - " \"target\": \"out_target_1\"\n", - " },\n", - "\n", - " ],\n", - " \"input_params\": [\"X\"],\n", - " \"resources\":{\n", - " \"T_gates\": {\n", - " \"name\": \"T_gates\",\n", - " \"type\": \"additive\",\n", - " \"value\": \"O(log_2(X))\",\n", - " },\n", - " }\n", - " }" + " \"name\": \"swap\",\n", + " \"type\": None,\n", + " \"ports\": [\n", + " {\"name\": \"in_control\", \"direction\": \"input\", \"size\": \"1\"},\n", + " {\"name\": \"in_target_0\", \"direction\": \"input\", \"size\": \"X\"},\n", + " {\"name\": \"in_target_1\", \"direction\": \"input\", \"size\": \"X\"},\n", + " {\"name\": \"out_control\", \"direction\": \"output\", \"size\": None},\n", + " {\"name\": \"out_target_0\", \"direction\": \"output\", \"size\": None},\n", + " {\"name\": \"out_target_1\", \"direction\": \"output\", \"size\": None},\n", + " ],\n", + " \"resources\": [{\"name\": \"T_gates\", \"type\": \"additive\", \"value\": \"O(log_2(X))\"}],\n", + " \"connections\": [\n", + " {\"source\": \"in_control\", \"target\": \"out_control\"},\n", + " {\"source\": \"in_target_0\", \"target\": \"out_target_0\"},\n", + " {\"source\": \"in_target_1\", \"target\": \"out_target_1\"},\n", + " ],\n", + " \"input_params\": [\"X\"],\n", + "}\n" ] }, { @@ -352,153 +223,77 @@ "outputs": [], "source": [ "alias_sampling_dict = {\n", - " \"name\": \"alias_sampling\",\n", - " \"type\": None,\n", - " \"ports\": { \n", - " \"in_0\": {\n", - " \"name\": \"in_0\",\n", - " \"direction\": \"input\",\n", - " \"size\": \"R\"\n", - " },\n", - " \"in_1\": {\n", - " \"name\": \"in_1\",\n", - " \"direction\": \"input\",\n", - " \"size\": \"mu\"\n", - " },\n", - " \"in_2\": {\n", - " \"name\": \"in_2\",\n", - " \"direction\": \"input\",\n", - " \"size\": \"mu\"\n", - " },\n", - " \"in_3\": {\n", - " \"name\": \"in_3\",\n", - " \"direction\": \"input\",\n", - " \"size\": \"mu\"\n", - " },\n", - " \"in_4\": {\n", - " \"name\": \"in_4\",\n", - " \"direction\": \"input\",\n", - " \"size\": \"1\"\n", - " },\n", - " \"out_0\": {\n", - " \"name\": \"out_0\",\n", - " \"direction\": \"output\",\n", - " \"size\": None\n", - " },\n", - " \"temp_0\": {\n", - " \"name\": \"temp_0\",\n", - " \"direction\": \"output\",\n", - " \"size\": None\n", - " },\n", - " \"temp_1\": {\n", - " \"name\": \"temp_1\",\n", - " \"direction\": \"output\",\n", - " \"size\": None\n", - " },\n", - " \"temp_2\": {\n", - " \"name\": \"temp_2\",\n", - " \"direction\": \"output\",\n", - " \"size\": None\n", - " },\n", - " \"temp_3\": {\n", - " \"name\": \"temp_3\",\n", - " \"direction\": \"output\",\n", - " \"size\": None\n", - " },\n", - "},\n", - " \"children\": {\n", - " \"usp\": usp_dict,\n", - " \"had\": had_dict,\n", - " \"qrom\": qrom_dict,\n", - " \"compare\": compare_dict,\n", - " \"swap\": swap_dict,\n", - " },\n", - " \"input_params\": [\"mu\", \"L\"],\n", - " \"linked_params\": {\n", - " \"L\": [(\"usp\",\"L\"), (\"qrom\",\"L\"), (\"swap\", \"X\")],\n", - " \"mu\": [(\"had\",\"mu\"), (\"qrom\",\"mu\"), (\"compare\", \"mu\")],\n", - " },\n", - " \"local_variables\": [\"R=ceiling(log_2(L))\"],\n", - " \"connections\": [\n", - " {\n", - " \"source\": \"in_0\",\n", - " \"target\": \"usp.in\"\n", - " },\n", - " {\n", - " \"source\": \"in_1\",\n", - " \"target\": \"had.in\"\n", - " },\n", - " {\n", - " \"source\": \"in_2\",\n", - " \"target\": \"qrom.in_alt\"\n", - " },\n", - " {\n", - " \"source\": \"in_3\",\n", - " \"target\": \"qrom.in_keep\"\n", - " },\n", - " {\n", - " \"source\": \"in_4\",\n", - " \"target\": \"compare.in_flag\"\n", - " },\n", - " {\n", - " \"source\": \"usp.out\",\n", - " \"target\": \"qrom.in_l\"\n", - " },\n", - " {\n", - " \"source\": \"had.out\",\n", - " \"target\": \"compare.in_sigma\"\n", - " },\n", - " {\n", - " \"source\": \"qrom.out_l\",\n", - " \"target\": \"swap.in_target_0\"\n", - " },\n", - " {\n", - " \"source\": \"qrom.out_alt\",\n", - " \"target\": \"swap.in_target_1\"\n", - " },\n", - " {\n", - " \"source\": \"qrom.out_keep\",\n", - " \"target\": \"compare.in_keep\"\n", - " },\n", - " {\n", - " \"source\": \"compare.out_flag\",\n", - " \"target\": \"swap.in_control\"\n", - " },\n", - " {\n", - " \"source\": \"swap.out_target_0\",\n", - " \"target\": \"out_0\"\n", - " },\n", - " {\n", - " \"source\": \"compare.out_sigma\",\n", - " \"target\": \"temp_0\"\n", - " },\n", - " {\n", - " \"source\": \"swap.out_target_1\",\n", - " \"target\": \"temp_1\"\n", - " },\n", - " {\n", - " \"source\": \"compare.out_keep\",\n", - " \"target\": \"temp_2\"\n", - " },\n", - " {\n", - " \"source\": \"swap.out_control\",\n", - " \"target\": \"temp_3\"\n", - " },\n", - "\n", - " ]\n", - "\n", + " \"name\": \"alias_sampling\",\n", + " \"children\": [usp_dict, had_dict, qrom_dict, compare_dict, swap_dict],\n", + " \"type\": None,\n", + " \"ports\": [\n", + " {\"name\": \"in_0\", \"direction\": \"input\", \"size\": \"R\"},\n", + " {\"name\": \"in_1\", \"direction\": \"input\", \"size\": \"mu\"},\n", + " {\"name\": \"in_2\", \"direction\": \"input\", \"size\": \"R\"},\n", + " {\"name\": \"in_3\", \"direction\": \"input\", \"size\": \"mu\"},\n", + " {\"name\": \"in_4\", \"direction\": \"input\", \"size\": \"1\"},\n", + " {\"name\": \"out_0\", \"direction\": \"output\", \"size\": None},\n", + " {\"name\": \"temp_0\", \"direction\": \"output\", \"size\": None},\n", + " {\"name\": \"temp_1\", \"direction\": \"output\", \"size\": None},\n", + " {\"name\": \"temp_2\", \"direction\": \"output\", \"size\": None},\n", + " {\"name\": \"temp_3\", \"direction\": \"output\", \"size\": None},\n", + " ],\n", + " \"connections\": [\n", + " {\"source\": \"in_0\", \"target\": \"usp.in\"},\n", + " {\"source\": \"in_1\", \"target\": \"had.in\"},\n", + " {\"source\": \"in_2\", \"target\": \"qrom.in_alt\"},\n", + " {\"source\": \"in_3\", \"target\": \"qrom.in_keep\"},\n", + " {\"source\": \"in_4\", \"target\": \"compare.in_flag\"},\n", + " {\"source\": \"usp.out\", \"target\": \"qrom.in_l\"},\n", + " {\"source\": \"had.out\", \"target\": \"compare.in_sigma\"},\n", + " {\"source\": \"qrom.out_l\", \"target\": \"swap.in_target_0\"},\n", + " {\"source\": \"qrom.out_alt\", \"target\": \"swap.in_target_1\"},\n", + " {\"source\": \"qrom.out_keep\", \"target\": \"compare.in_keep\"},\n", + " {\"source\": \"compare.out_flag\", \"target\": \"swap.in_control\"},\n", + " {\"source\": \"swap.out_target_0\", \"target\": \"out_0\"},\n", + " {\"source\": \"compare.out_sigma\", \"target\": \"temp_0\"},\n", + " {\"source\": \"swap.out_target_1\", \"target\": \"temp_1\"},\n", + " {\"source\": \"compare.out_keep\", \"target\": \"temp_2\"},\n", + " {\"source\": \"swap.out_control\", \"target\": \"temp_3\"},\n", + " ],\n", + " \"input_params\": [\"mu\", \"L\"],\n", + " \"local_variables\": [\"R=ceiling(log_2(L))\"],\n", + " \"linked_params\": [\n", + " {\"source\": \"L\", \"targets\": [\"usp.L\", \"qrom.L\", \"swap.X\"]},\n", + " {\"source\": \"mu\", \"targets\": [\"had.mu\", \"qrom.mu\", \"compare.mu\"]},\n", + " ],\n", "}" ] }, { "cell_type": "code", "execution_count": 7, + "id": "e3d17a61-4b9e-48e9-901a-231da501f41b", + "metadata": {}, + "outputs": [], + "source": [ + "alias_sampling_qref = {'version': 'v1', 'program': alias_sampling_dict}" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "5d0d2d69-4b77-4d62-9dd3-2405f4db21ed", + "metadata": {}, + "outputs": [], + "source": [ + "from bartiq.integrations import qref_to_bartiq\n", + "uncompiled_routine = qref_to_bartiq(alias_sampling_qref)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, "id": "d20ef4d2-9420-4090-a402-ee5f27034524", "metadata": {}, "outputs": [], "source": [ - "from bartiq import Routine, compile_routine, evaluate\n", - "uncompiled_routine = Routine(**alias_sampling_dict)\n", + "from bartiq import compile_routine\n", "compiled_routine = compile_routine(uncompiled_routine)" ] }, @@ -512,7 +307,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 10, "id": "dad8df1e-9570-46c5-a422-4f29bba4d52c", "metadata": {}, "outputs": [ @@ -520,8 +315,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "rotations: 2\n", - "T_gates: 4*L + 4*mu + swap.O(log2(L)) + 8*ceiling(log2(L/multiplicity(2, L))) - 8\n" + "T_gates: 4*L + 8*L/multiplicity(2, L) + 4*mu + swap.O(log2(L)) - 8\n", + "rotations: 2\n" ] } ], @@ -539,7 +334,9 @@ "\n", "- We have the $4(L + \\mu)$ in both versions. \n", "- We have a `swap.O(log_2(L))` which corresponds to `O(log_2(L))`\n", - "- The following term is also `O(log_2(L))` (we can incorporate $-8$ into it as well)\n", + "- The `8*L/multiplicity(2, L)` term is also `O(log_2(L))`\n", + "- Constant factor of `-8` can also be incorporated in `O(log_2(L))`\n", + "\n", "\n", "\n", "Let's see how it looks like for some concrete values:" @@ -547,7 +344,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 11, "id": "becb2091-9faf-4364-ac22-f10240072937", "metadata": {}, "outputs": [ @@ -555,12 +352,13 @@ "name": "stdout", "output_type": "stream", "text": [ - "rotations: 2\n", - "T_gates: swap.O(log2(120)) + 552\n" + "T_gates: swap.O(log2(120)) + 824\n", + "rotations: 2\n" ] } ], "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", @@ -582,7 +380,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 12, "id": "dbf7d3a4-5a58-4dcb-990e-eccd0d7cda9a", "metadata": {}, "outputs": [ @@ -590,8 +388,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "rotations: 2\n", - "T_gates: 4*L + 4*mu + O(log2(L)) + 8*ceiling(log2(L/multiplicity(2, L))) - 8\n" + "T_gates: 4*L + 8*L/multiplicity(2, L) + 4*mu + O(log2(L)) - 8\n", + "rotations: 2\n" ] } ], @@ -611,7 +409,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 13, "id": "2909fb37-6a15-44fc-a80f-46e562220fcb", "metadata": {}, "outputs": [ @@ -619,8 +417,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "rotations: 2\n", - "T_gates: log2(120) + 552\n" + "T_gates: log2(120) + 824\n", + "rotations: 2\n" ] } ], @@ -671,7 +469,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.10" + "version": "3.9.19" } }, "nbformat": 4,