From 7f4ee65d0712e34d89302e73e098ca75e1ee06e8 Mon Sep 17 00:00:00 2001 From: Matthew Harrigan Date: Mon, 18 Mar 2024 15:11:37 -0700 Subject: [PATCH] [autodoc] More arithmetic examples and sundry (#792) * Import qdatatype in autogenerated notebooks * simplify call graph in autogenerated notebooks * default import line for bloqs * Autodoc arithmetic bloqs * Autodoc arithmetic bloqs * Autogenerate arithmetic notebooks * Re-generate the rest of the notebooks --- dev_tools/autogenerate-bloqs-notebooks-v2.py | 8 + .../qualtran_dev_tools/jupyter_autogen.py | 1 + .../qualtran_dev_tools/jupyter_autogen_v2.py | 3 +- qualtran/_infra/bloq_example.py | 10 +- qualtran/bloqs/arithmetic/__init__.py | 8 +- qualtran/bloqs/arithmetic/addition.ipynb | 156 +++++- qualtran/bloqs/arithmetic/addition.py | 37 +- qualtran/bloqs/arithmetic/comparison.ipynb | 482 +++++++++++++++++- qualtran/bloqs/arithmetic/comparison.py | 141 +++-- qualtran/bloqs/arithmetic/conversions.ipynb | 11 +- qualtran/bloqs/arithmetic/conversions.py | 12 +- qualtran/bloqs/arithmetic/conversions_test.py | 8 + .../bloqs/arithmetic/multiplication.ipynb | 36 +- qualtran/bloqs/arithmetic/multiplication.py | 42 +- qualtran/bloqs/arithmetic/sorting.ipynb | 11 +- qualtran/bloqs/arithmetic/sorting.py | 12 +- qualtran/bloqs/basic_gates/s_gate.ipynb | 4 +- qualtran/bloqs/basic_gates/t_gate.ipynb | 4 +- qualtran/bloqs/basic_gates/toffoli.ipynb | 4 +- qualtran/bloqs/block_encoding.ipynb | 7 +- .../chemistry/df/double_factorization.ipynb | 7 +- .../first_quantization.ipynb | 19 +- .../projectile/projectile.ipynb | 7 +- .../chemistry/sf/single_factorization.ipynb | 7 +- qualtran/bloqs/chemistry/sparse/sparse.ipynb | 7 +- qualtran/bloqs/chemistry/thc/thc.ipynb | 10 +- qualtran/bloqs/data_loading/qrom.ipynb | 4 +- qualtran/bloqs/factoring/mod_exp.ipynb | 4 +- qualtran/bloqs/factoring/mod_mul.ipynb | 4 +- qualtran/bloqs/qft/two_bit_ffft.ipynb | 4 +- qualtran/bloqs/reflection.ipynb | 4 +- .../rotations/phasing_via_cost_function.ipynb | 4 +- .../rotations/quantum_variable_rotation.ipynb | 7 +- .../state_preparation_via_rotation.ipynb | 4 +- .../bloqs/swap_network/swap_network.ipynb | 13 +- 35 files changed, 918 insertions(+), 184 deletions(-) diff --git a/dev_tools/autogenerate-bloqs-notebooks-v2.py b/dev_tools/autogenerate-bloqs-notebooks-v2.py index 531a5d41c..e619e7eaa 100644 --- a/dev_tools/autogenerate-bloqs-notebooks-v2.py +++ b/dev_tools/autogenerate-bloqs-notebooks-v2.py @@ -257,6 +257,7 @@ bloq_specs=[ qualtran.bloqs.arithmetic.addition._ADD_DOC, qualtran.bloqs.arithmetic.addition._ADD_OOP_DOC, + qualtran.bloqs.arithmetic.addition._SIMPLE_ADD_K_DOC, qualtran.bloqs.arithmetic.addition._ADD_K_DOC, ], ), @@ -294,9 +295,13 @@ title='Comparison', module=qualtran.bloqs.arithmetic.comparison, bloq_specs=[ + qualtran.bloqs.arithmetic.comparison._LT_K_DOC, qualtran.bloqs.arithmetic.comparison._GREATER_THAN_DOC, qualtran.bloqs.arithmetic.comparison._GREATER_THAN_K_DOC, qualtran.bloqs.arithmetic.comparison._EQUALS_K_DOC, + qualtran.bloqs.arithmetic.comparison._BI_QUBITS_MIXER_DOC, + qualtran.bloqs.arithmetic.comparison._SQ_CMP_DOC, + qualtran.bloqs.arithmetic.comparison._LEQ_DOC, ], ), NotebookSpecV2( @@ -307,6 +312,9 @@ qualtran.bloqs.arithmetic.conversions._TO_CONTG_INDX, ], ), + # -------------------------------------------------------------------------- + # ----- Rotations ----------------------------------------------------- + # -------------------------------------------------------------------------- NotebookSpecV2( title='Quantum Variable Rotation', module=qualtran.bloqs.rotations.quantum_variable_rotation, diff --git a/dev_tools/qualtran_dev_tools/jupyter_autogen.py b/dev_tools/qualtran_dev_tools/jupyter_autogen.py index 4a4c7bfcf..df67f659f 100644 --- a/dev_tools/qualtran_dev_tools/jupyter_autogen.py +++ b/dev_tools/qualtran_dev_tools/jupyter_autogen.py @@ -185,6 +185,7 @@ def _get_lines_for_constructing_an_object(func: Callable): _IMPORTS = """\ from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register +from qualtran import QBit, QInt, QUInt, QAny from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma from typing import * import numpy as np diff --git a/dev_tools/qualtran_dev_tools/jupyter_autogen_v2.py b/dev_tools/qualtran_dev_tools/jupyter_autogen_v2.py index 166aaea14..1020c5493 100644 --- a/dev_tools/qualtran_dev_tools/jupyter_autogen_v2.py +++ b/dev_tools/qualtran_dev_tools/jupyter_autogen_v2.py @@ -197,7 +197,8 @@ def get_call_graph_cells(bloqdoc: BloqDocSpec, cid_prefix: str) -> List[_Cell]: sigmavar = f'{ex.name}_sigma' code = [ - f'{graphvar}, {sigmavar} = {ex.name}.call_graph()', + 'from qualtran.resource_counting.generalizers import ignore_split_join', + f'{graphvar}, {sigmavar} = {ex.name}.call_graph(max_depth=1, generalizer=ignore_split_join)', f'show_call_graph({graphvar})', f'show_counts_sigma({sigmavar})', ] diff --git a/qualtran/_infra/bloq_example.py b/qualtran/_infra/bloq_example.py index 1be8400d3..1ea51f18b 100644 --- a/qualtran/_infra/bloq_example.py +++ b/qualtran/_infra/bloq_example.py @@ -116,7 +116,7 @@ def _t(x: Iterable[T]) -> Tuple[T, ...]: return _t -@frozen +@frozen(kw_only=True) class BloqDocSpec: """A collection of bloq examples and specifications for documenting a bloq class. @@ -141,10 +141,16 @@ class BloqDocSpec: """ bloq_cls: Type[Bloq] - import_line: str examples: Sequence[BloqExample] = field(converter=_to_tuple(BloqExample), factory=tuple) + import_line: str = field() call_graph_example: Union[BloqExample, None] = field() + @import_line.default + def _import_line_default(self) -> str: + pkg = '.'.join(self.bloq_cls.__module__.split('.')[:-1]) + line = f'from {pkg} import {self.bloq_cls.__name__}' + return line + @call_graph_example.default def _call_graph_example_default(self) -> Union[BloqExample, None]: if not self.examples: diff --git a/qualtran/bloqs/arithmetic/__init__.py b/qualtran/bloqs/arithmetic/__init__.py index 38e444695..8934888ed 100644 --- a/qualtran/bloqs/arithmetic/__init__.py +++ b/qualtran/bloqs/arithmetic/__init__.py @@ -12,7 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -from qualtran.bloqs.arithmetic.addition import Add, AddConstantMod, OutOfPlaceAdder +from qualtran.bloqs.arithmetic.addition import ( + Add, + AddConstantMod, + OutOfPlaceAdder, + SimpleAddConstant, +) from qualtran.bloqs.arithmetic.comparison import ( BiQubitsMixer, EqualsAConstant, @@ -33,3 +38,4 @@ SquareRealNumber, SumOfSquares, ) +from qualtran.bloqs.arithmetic.sorting import BitonicSort, Comparator diff --git a/qualtran/bloqs/arithmetic/addition.ipynb b/qualtran/bloqs/arithmetic/addition.ipynb index 524e21372..6f21045cc 100644 --- a/qualtran/bloqs/arithmetic/addition.ipynb +++ b/qualtran/bloqs/arithmetic/addition.ipynb @@ -20,6 +20,7 @@ "outputs": [], "source": [ "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", + "from qualtran import QBit, QInt, QUInt, QAny\n", "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", "from typing import *\n", "import numpy as np\n", @@ -59,8 +60,7 @@ }, "outputs": [], "source": [ - "from qualtran import QInt, QUInt\n", - "from qualtran.bloqs.arithmetic.addition import Add" + "from qualtran.bloqs.arithmetic import Add" ] }, { @@ -153,7 +153,8 @@ }, "outputs": [], "source": [ - "add_symb_g, add_symb_sigma = add_symb.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "add_symb_g, add_symb_sigma = add_symb.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(add_symb_g)\n", "show_counts_sigma(add_symb_sigma)" ] @@ -192,7 +193,7 @@ }, "outputs": [], "source": [ - "from qualtran.bloqs.arithmetic.addition import OutOfPlaceAdder" + "from qualtran.bloqs.arithmetic import OutOfPlaceAdder" ] }, { @@ -285,7 +286,8 @@ }, "outputs": [], "source": [ - "add_oop_symb_g, add_oop_symb_sigma = add_oop_symb.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "add_oop_symb_g, add_oop_symb_sigma = add_oop_symb.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(add_oop_symb_g)\n", "show_counts_sigma(add_oop_symb_sigma)" ] @@ -321,7 +323,7 @@ }, "outputs": [], "source": [ - "from qualtran.bloqs.arithmetic.addition import AddConstantMod" + "from qualtran.bloqs.arithmetic import AddConstantMod" ] }, { @@ -414,10 +416,148 @@ }, "outputs": [], "source": [ - "add_k_symb_g, add_k_symb_sigma = add_k_symb.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "add_k_symb_g, add_k_symb_sigma = add_k_symb.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(add_k_symb_g)\n", "show_counts_sigma(add_k_symb_sigma)" ] + }, + { + "cell_type": "markdown", + "id": "9449e4e9", + "metadata": { + "cq.autogen": "SimpleAddConstant.bloq_doc.md" + }, + "source": [ + "## `SimpleAddConstant`\n", + "Takes |x> to |x + k> for a classical integer `k`.\n", + "\n", + "Applies addition to input register `|x>` given classical integer 'k'.\n", + "\n", + "This is the simple version of constant addition because it involves simply converting the\n", + "classical integer into a quantum parameter and using quantum-quantum addition as opposed to\n", + "designing a bespoke circuit for constant addition based on the classical parameter.\n", + "\n", + "#### Parameters\n", + " - `bitsize`: Number of bits used to represent each integer.\n", + " - `k`: The classical integer value to be added to x.\n", + " - `cvs`: A tuple of control values. Each entry specifies whether that control line is a \"positive\" control (`cv[i]=1`) or a \"negative\" control (`cv[i]=0`).\n", + " - `signed`: A boolean condition which controls whether the x register holds a value represented in 2's Complement or Unsigned. This affects the ability to add a negative constant. \n", + "\n", + "#### Registers\n", + " - `x`: A bitsize-sized input register (register x above). \n", + "\n", + "#### References\n", + "[Improved quantum circuits for elliptic curve discrete logarithms](https://arxiv.org/abs/2001.09580) Fig 2a\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cd255bf9", + "metadata": { + "cq.autogen": "SimpleAddConstant.bloq_doc.py" + }, + "outputs": [], + "source": [ + "from qualtran.bloqs.arithmetic import SimpleAddConstant" + ] + }, + { + "cell_type": "markdown", + "id": "7538f9a5", + "metadata": { + "cq.autogen": "SimpleAddConstant.example_instances.md" + }, + "source": [ + "### Example Instances" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4305289f", + "metadata": { + "cq.autogen": "SimpleAddConstant.simple_add_k_symb" + }, + "outputs": [], + "source": [ + "n, k = sympy.symbols('n k')\n", + "simple_add_k_symb = SimpleAddConstant(bitsize=n, k=k)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f6048819", + "metadata": { + "cq.autogen": "SimpleAddConstant.simple_add_k_small" + }, + "outputs": [], + "source": [ + "simple_add_k_small = SimpleAddConstant(bitsize=4, k=2, signed=False)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b67fd469", + "metadata": { + "cq.autogen": "SimpleAddConstant.simple_add_k_large" + }, + "outputs": [], + "source": [ + "simple_add_k_large = SimpleAddConstant(bitsize=64, k=-23, signed=True)" + ] + }, + { + "cell_type": "markdown", + "id": "b8b04228", + "metadata": { + "cq.autogen": "SimpleAddConstant.graphical_signature.md" + }, + "source": [ + "#### Graphical Signature" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e93e7f2e", + "metadata": { + "cq.autogen": "SimpleAddConstant.graphical_signature.py" + }, + "outputs": [], + "source": [ + "from qualtran.drawing import show_bloqs\n", + "show_bloqs([simple_add_k_small, simple_add_k_large],\n", + " ['`simple_add_k_small`', '`simple_add_k_large`'])" + ] + }, + { + "cell_type": "markdown", + "id": "13552795", + "metadata": { + "cq.autogen": "SimpleAddConstant.call_graph.md" + }, + "source": [ + "### Call Graph" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d8d6584e", + "metadata": { + "cq.autogen": "SimpleAddConstant.call_graph.py" + }, + "outputs": [], + "source": [ + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "simple_add_k_small_g, simple_add_k_small_sigma = simple_add_k_small.call_graph(max_depth=1, generalizer=ignore_split_join)\n", + "show_call_graph(simple_add_k_small_g)\n", + "show_counts_sigma(simple_add_k_small_sigma)" + ] } ], "metadata": { @@ -436,7 +576,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.9" + "version": "3.11.7" } }, "nbformat": 4, diff --git a/qualtran/bloqs/arithmetic/addition.py b/qualtran/bloqs/arithmetic/addition.py index 8567ffc49..9149fb331 100644 --- a/qualtran/bloqs/arithmetic/addition.py +++ b/qualtran/bloqs/arithmetic/addition.py @@ -210,11 +210,7 @@ def _add_large() -> Add: return add_large -_ADD_DOC = BloqDocSpec( - bloq_cls=Add, - import_line='from qualtran import QInt, QUInt\nfrom qualtran.bloqs.arithmetic.addition import Add', - examples=(_add_symb, _add_small, _add_large), -) +_ADD_DOC = BloqDocSpec(bloq_cls=Add, examples=[_add_symb, _add_small, _add_large]) @frozen @@ -322,9 +318,7 @@ def _add_oop_large() -> OutOfPlaceAdder: _ADD_OOP_DOC = BloqDocSpec( - bloq_cls=OutOfPlaceAdder, - import_line='from qualtran.bloqs.arithmetic.addition import OutOfPlaceAdder', - examples=(_add_oop_symb, _add_oop_small, _add_oop_large), + bloq_cls=OutOfPlaceAdder, examples=[_add_oop_symb, _add_oop_small, _add_oop_large] ) @@ -355,8 +349,10 @@ class SimpleAddConstant(Bloq): bitsize: int k: int - cvs: Tuple[int, ...] = field(converter=lambda v: (v,) if isinstance(v, int) else tuple(v)) - signed: bool + cvs: Tuple[int, ...] = field( + converter=lambda v: (v,) if isinstance(v, int) else tuple(v), default=() + ) + signed: bool = False @cached_property def signature(self) -> 'Signature': @@ -443,6 +439,23 @@ def short_name(self) -> str: return f'x += {self.k}' +@bloq_example +def _simple_add_k_small() -> SimpleAddConstant: + simple_add_k_small = SimpleAddConstant(bitsize=4, k=2, signed=False) + return simple_add_k_small + + +@bloq_example +def _simple_add_k_large() -> SimpleAddConstant: + simple_add_k_large = SimpleAddConstant(bitsize=64, k=-23, signed=True) + return simple_add_k_large + + +_SIMPLE_ADD_K_DOC = BloqDocSpec( + bloq_cls=SimpleAddConstant, examples=[_simple_add_k_small, _simple_add_k_large] +) + + @frozen(auto_attribs=True) class AddConstantMod(GateWithRegisters, cirq.ArithmeticGate): """Applies U(add, M)|x> = |(x + add) % M> if x < M else |x>. @@ -552,7 +565,5 @@ def _add_k_large() -> AddConstantMod: _ADD_K_DOC = BloqDocSpec( - bloq_cls=AddConstantMod, - import_line='from qualtran.bloqs.arithmetic.addition import AddConstantMod', - examples=(_add_k_symb, _add_k_small, _add_k_large), + bloq_cls=AddConstantMod, examples=[_add_k_symb, _add_k_small, _add_k_large] ) diff --git a/qualtran/bloqs/arithmetic/comparison.ipynb b/qualtran/bloqs/arithmetic/comparison.ipynb index 570cd7702..f190c1aca 100644 --- a/qualtran/bloqs/arithmetic/comparison.ipynb +++ b/qualtran/bloqs/arithmetic/comparison.ipynb @@ -20,6 +20,7 @@ "outputs": [], "source": [ "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", + "from qualtran import QBit, QInt, QUInt, QAny\n", "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", "from typing import *\n", "import numpy as np\n", @@ -64,7 +65,7 @@ }, "outputs": [], "source": [ - "from qualtran.bloqs.arithmetic.comparison import GreaterThan" + "from qualtran.bloqs.arithmetic import GreaterThan" ] }, { @@ -132,7 +133,8 @@ }, "outputs": [], "source": [ - "greater_than_g, greater_than_sigma = greater_than.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "greater_than_g, greater_than_sigma = greater_than.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(greater_than_g)\n", "show_counts_sigma(greater_than_sigma)" ] @@ -172,7 +174,7 @@ }, "outputs": [], "source": [ - "from qualtran.bloqs.arithmetic.comparison import GreaterThanConstant" + "from qualtran.bloqs.arithmetic import GreaterThanConstant" ] }, { @@ -240,7 +242,8 @@ }, "outputs": [], "source": [ - "gt_k_g, gt_k_sigma = gt_k.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "gt_k_g, gt_k_sigma = gt_k.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(gt_k_g)\n", "show_counts_sigma(gt_k_sigma)" ] @@ -276,7 +279,7 @@ }, "outputs": [], "source": [ - "from qualtran.bloqs.arithmetic.comparison import EqualsAConstant" + "from qualtran.bloqs.arithmetic import EqualsAConstant" ] }, { @@ -344,20 +347,483 @@ }, "outputs": [], "source": [ - "eq_k_g, eq_k_sigma = eq_k.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "eq_k_g, eq_k_sigma = eq_k.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(eq_k_g)\n", "show_counts_sigma(eq_k_sigma)" ] + }, + { + "cell_type": "markdown", + "id": "5d4a473d", + "metadata": { + "cq.autogen": "LessThanConstant.bloq_doc.md" + }, + "source": [ + "## `LessThanConstant`\n", + "Applies U_a|x>|z> = |x> |z ^ (x < a)>" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f978cd77", + "metadata": { + "cq.autogen": "LessThanConstant.bloq_doc.py" + }, + "outputs": [], + "source": [ + "from qualtran.bloqs.arithmetic import LessThanConstant" + ] + }, + { + "cell_type": "markdown", + "id": "1ff5855a", + "metadata": { + "cq.autogen": "LessThanConstant.example_instances.md" + }, + "source": [ + "### Example Instances" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f0e3705e", + "metadata": { + "cq.autogen": "LessThanConstant.lt_k_symb" + }, + "outputs": [], + "source": [ + "n, k = sympy.symbols(\"n k\")\n", + "lt_k_symb = LessThanConstant(bitsize=n, less_than_val=k)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "65845541", + "metadata": { + "cq.autogen": "LessThanConstant.lt_k" + }, + "outputs": [], + "source": [ + "lt_k = LessThanConstant(bitsize=8, less_than_val=5)" + ] + }, + { + "cell_type": "markdown", + "id": "697c19b4", + "metadata": { + "cq.autogen": "LessThanConstant.graphical_signature.md" + }, + "source": [ + "#### Graphical Signature" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c722a3c7", + "metadata": { + "cq.autogen": "LessThanConstant.graphical_signature.py" + }, + "outputs": [], + "source": [ + "from qualtran.drawing import show_bloqs\n", + "show_bloqs([lt_k, lt_k_symb],\n", + " ['`lt_k`', '`lt_k_symb`'])" + ] + }, + { + "cell_type": "markdown", + "id": "9ac764fa", + "metadata": { + "cq.autogen": "LessThanConstant.call_graph.md" + }, + "source": [ + "### Call Graph" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b8de0ffc", + "metadata": { + "cq.autogen": "LessThanConstant.call_graph.py" + }, + "outputs": [], + "source": [ + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "lt_k_g, lt_k_sigma = lt_k.call_graph(max_depth=1, generalizer=ignore_split_join)\n", + "show_call_graph(lt_k_g)\n", + "show_counts_sigma(lt_k_sigma)" + ] + }, + { + "cell_type": "markdown", + "id": "58d1a6a6", + "metadata": { + "cq.autogen": "BiQubitsMixer.bloq_doc.md" + }, + "source": [ + "## `BiQubitsMixer`\n", + "Implements the COMPARE2 subroutine from the reference (Fig. 1)\n", + "\n", + "This gates mixes the values in a way that preserves the result of comparison.\n", + "The signature being compared are 2-qubit signature where\n", + "\n", + " x = 2*x_msb + x_lsb\n", + " y = 2*y_msb + y_lsb\n", + "\n", + "The Gate mixes the 4 qubits so that sign(x - y) = sign(x_lsb' - y_lsb') where x_lsb' and y_lsb'\n", + "are the final values of x_lsb' and y_lsb'.\n", + "\n", + "Note that the ancilla qubits are used to reduce the T-count and the user\n", + "should clean the qubits at a later point in time with the adjoint gate.\n", + "See: https://github.com/quantumlib/Cirq/pull/6313 and\n", + "https://github.com/quantumlib/Qualtran/issues/389\n", + "\n", + "#### References\n", + "Supplementary Materials: Improved Techniques for Preparing Eigenstates of Fermionic Hamiltonians\n", + "https://static-content.springer.com/esm/art%3A10.1038%2Fs41534-018-0071-5/MediaObjects/41534_2018_71_MOESM1_ESM.pdf\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1603c8b5", + "metadata": { + "cq.autogen": "BiQubitsMixer.bloq_doc.py" + }, + "outputs": [], + "source": [ + "from qualtran.bloqs.arithmetic import BiQubitsMixer" + ] + }, + { + "cell_type": "markdown", + "id": "78cbdc47", + "metadata": { + "cq.autogen": "BiQubitsMixer.example_instances.md" + }, + "source": [ + "### Example Instances" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d69a00ae", + "metadata": { + "cq.autogen": "BiQubitsMixer.bi_qubits_mixer" + }, + "outputs": [], + "source": [ + "bi_qubits_mixer = BiQubitsMixer()" + ] + }, + { + "cell_type": "markdown", + "id": "1f0bfe08", + "metadata": { + "cq.autogen": "BiQubitsMixer.graphical_signature.md" + }, + "source": [ + "#### Graphical Signature" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7558ee56", + "metadata": { + "cq.autogen": "BiQubitsMixer.graphical_signature.py" + }, + "outputs": [], + "source": [ + "from qualtran.drawing import show_bloqs\n", + "show_bloqs([bi_qubits_mixer],\n", + " ['`bi_qubits_mixer`'])" + ] + }, + { + "cell_type": "markdown", + "id": "a5c34728", + "metadata": { + "cq.autogen": "BiQubitsMixer.call_graph.md" + }, + "source": [ + "### Call Graph" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0daf86df", + "metadata": { + "cq.autogen": "BiQubitsMixer.call_graph.py" + }, + "outputs": [], + "source": [ + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "bi_qubits_mixer_g, bi_qubits_mixer_sigma = bi_qubits_mixer.call_graph(max_depth=1, generalizer=ignore_split_join)\n", + "show_call_graph(bi_qubits_mixer_g)\n", + "show_counts_sigma(bi_qubits_mixer_sigma)" + ] + }, + { + "cell_type": "markdown", + "id": "19ccfb41", + "metadata": { + "cq.autogen": "SingleQubitCompare.bloq_doc.md" + }, + "source": [ + "## `SingleQubitCompare`\n", + "Applies U|a>|b>|0>|0> = |a> |a=b> |(a |(a>b)>\n", + "\n", + "#### References\n", + "Supplementary Materials: Improved Techniques for Preparing Eigenstates of Fermionic Hamiltonians.\n", + "Figure 3.\n", + "https://static-content.springer.com/esm/art%3A10.1038%2Fs41534-018-0071-5/MediaObjects/41534_2018_71_MOESM1_ESM.pdf\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ae8f37f5", + "metadata": { + "cq.autogen": "SingleQubitCompare.bloq_doc.py" + }, + "outputs": [], + "source": [ + "from qualtran.bloqs.arithmetic import SingleQubitCompare" + ] + }, + { + "cell_type": "markdown", + "id": "61ef1472", + "metadata": { + "cq.autogen": "SingleQubitCompare.example_instances.md" + }, + "source": [ + "### Example Instances" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "534c5c22", + "metadata": { + "cq.autogen": "SingleQubitCompare.sq_cmp" + }, + "outputs": [], + "source": [ + "sq_cmp = SingleQubitCompare()" + ] + }, + { + "cell_type": "markdown", + "id": "2843d328", + "metadata": { + "cq.autogen": "SingleQubitCompare.graphical_signature.md" + }, + "source": [ + "#### Graphical Signature" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d5dcf72e", + "metadata": { + "cq.autogen": "SingleQubitCompare.graphical_signature.py" + }, + "outputs": [], + "source": [ + "from qualtran.drawing import show_bloqs\n", + "show_bloqs([sq_cmp],\n", + " ['`sq_cmp`'])" + ] + }, + { + "cell_type": "markdown", + "id": "ee1cc222", + "metadata": { + "cq.autogen": "SingleQubitCompare.call_graph.md" + }, + "source": [ + "### Call Graph" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c96f20fc", + "metadata": { + "cq.autogen": "SingleQubitCompare.call_graph.py" + }, + "outputs": [], + "source": [ + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "sq_cmp_g, sq_cmp_sigma = sq_cmp.call_graph(max_depth=1, generalizer=ignore_split_join)\n", + "show_call_graph(sq_cmp_g)\n", + "show_counts_sigma(sq_cmp_sigma)" + ] + }, + { + "cell_type": "markdown", + "id": "50d83fdb", + "metadata": { + "cq.autogen": "LessThanEqual.bloq_doc.md" + }, + "source": [ + "## `LessThanEqual`\n", + "Applies U|x>|y>|z> = |x>|y> |z ^ (x <= y)>\n", + "\n", + "Decomposes the gate in a T-complexity optimal way.\n", + "\n", + "The construction can be broken in 4 parts:\n", + " 1. In case of differing bitsizes then a multicontrol And Gate\n", + " (Section III.A. of the first reference) is used to check whether\n", + " the extra prefix is equal to zero and the result is stored in the `prefix_equality` qubit.\n", + " 2. The tree structure (Fig. 2) of the second reference.\n", + " followed by a `SingleQubitCompare` to compute the result of comparison of\n", + " the suffixes of equal length. The result is stored in `less_than` and `greater_than` and\n", + " equality in `qubits[-2]`\n", + " 3. The results from the previous two steps are combined to update the target qubit.\n", + " 4. The adjoint of the previous operations is added to restore the input qubits\n", + " to their original state and clean the ancilla qubits.\n", + "\n", + "When both registers are of the same size the T complexity is\n", + "8n - 4 as in the second reference.\n", + "\n", + "When the registers differ in size and `n` is the size of the smaller one and\n", + "`d` is the difference in size, the T complexity is the sum of the tree\n", + "decomposition as before giving 8n + O(1); and the T complexity of an `And` gate\n", + "over `d` registers giving 4d + O(1). This totals 8n + 4d + O(1).\n", + "\n", + "#### References\n", + "Encoding Electronic Spectra in Quantum Circuits with Linear T Complexity. https://arxiv.org/abs/1805.03662.\n", + "\n", + "Supplementary Materials: Improved Techniques for Preparing Eigenstates of Fermionic Hamiltonians.\n", + "https://static-content.springer.com/esm/art%3A10.1038%2Fs41534-018-0071-5/MediaObjects/41534_2018_71_MOESM1_ESM.pdf\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "31d1717c", + "metadata": { + "cq.autogen": "LessThanEqual.bloq_doc.py" + }, + "outputs": [], + "source": [ + "from qualtran.bloqs.arithmetic import LessThanEqual" + ] + }, + { + "cell_type": "markdown", + "id": "bb520292", + "metadata": { + "cq.autogen": "LessThanEqual.example_instances.md" + }, + "source": [ + "### Example Instances" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8c60d014", + "metadata": { + "cq.autogen": "LessThanEqual.leq_symb" + }, + "outputs": [], + "source": [ + "n1, n2 = sympy.symbols('n1 n2')\n", + "leq_symb = LessThanEqual(x_bitsize=n1, y_bitsize=n2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "545b145c", + "metadata": { + "cq.autogen": "LessThanEqual.leq" + }, + "outputs": [], + "source": [ + "leq = LessThanEqual(x_bitsize=4, y_bitsize=8)" + ] + }, + { + "cell_type": "markdown", + "id": "40db2f9f", + "metadata": { + "cq.autogen": "LessThanEqual.graphical_signature.md" + }, + "source": [ + "#### Graphical Signature" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bc035e97", + "metadata": { + "cq.autogen": "LessThanEqual.graphical_signature.py" + }, + "outputs": [], + "source": [ + "from qualtran.drawing import show_bloqs\n", + "show_bloqs([leq, leq_symb],\n", + " ['`leq`', '`leq_symb`'])" + ] + }, + { + "cell_type": "markdown", + "id": "1fe92bed", + "metadata": { + "cq.autogen": "LessThanEqual.call_graph.md" + }, + "source": [ + "### Call Graph" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dcd53c93", + "metadata": { + "cq.autogen": "LessThanEqual.call_graph.py" + }, + "outputs": [], + "source": [ + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "leq_g, leq_sigma = leq.call_graph(max_depth=1, generalizer=ignore_split_join)\n", + "show_call_graph(leq_g)\n", + "show_counts_sigma(leq_sigma)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { - "name": "python" + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.7" } }, "nbformat": 4, diff --git a/qualtran/bloqs/arithmetic/comparison.py b/qualtran/bloqs/arithmetic/comparison.py index d0d9959c6..c18dac46d 100644 --- a/qualtran/bloqs/arithmetic/comparison.py +++ b/qualtran/bloqs/arithmetic/comparison.py @@ -18,6 +18,7 @@ import attrs import cirq import numpy as np +import sympy from attrs import frozen from numpy.typing import NDArray @@ -158,14 +159,34 @@ def _t_complexity_(self) -> TComplexity: return TComplexity(t=4 * n, clifford=15 * n + 3 * bin(self.less_than_val).count("1") + 2) +@bloq_example +def _lt_k() -> LessThanConstant: + lt_k = LessThanConstant(bitsize=8, less_than_val=5) + return lt_k + + +@bloq_example +def _lt_k_symb() -> LessThanConstant: + n, k = sympy.symbols("n k") + lt_k_symb = LessThanConstant(bitsize=n, less_than_val=k) + return lt_k_symb + + +_LT_K_DOC = BloqDocSpec( + bloq_cls=LessThanConstant, examples=[_lt_k, _lt_k_symb] # TODO: support symbolic call graph +) + + @frozen class BiQubitsMixer(GateWithRegisters): - """Implements the COMPARE2 (Fig. 1) https://static-content.springer.com/esm/art%3A10.1038%2Fs41534-018-0071-5/MediaObjects/41534_2018_71_MOESM1_ESM.pdf + """Implements the COMPARE2 subroutine from the reference (Fig. 1) This gates mixes the values in a way that preserves the result of comparison. The signature being compared are 2-qubit signature where + x = 2*x_msb + x_lsb y = 2*y_msb + y_lsb + The Gate mixes the 4 qubits so that sign(x - y) = sign(x_lsb' - y_lsb') where x_lsb' and y_lsb' are the final values of x_lsb' and y_lsb'. @@ -173,6 +194,10 @@ class BiQubitsMixer(GateWithRegisters): should clean the qubits at a later point in time with the adjoint gate. See: https://github.com/quantumlib/Cirq/pull/6313 and https://github.com/quantumlib/Qualtran/issues/389 + + References: + Supplementary Materials: Improved Techniques for Preparing Eigenstates of Fermionic Hamiltonians + https://static-content.springer.com/esm/art%3A10.1038%2Fs41534-018-0071-5/MediaObjects/41534_2018_71_MOESM1_ESM.pdf """ is_adjoint: bool = False @@ -246,12 +271,24 @@ def _has_unitary_(self): return not self.is_adjoint +@bloq_example +def _bi_qubits_mixer() -> BiQubitsMixer: + bi_qubits_mixer = BiQubitsMixer() + return bi_qubits_mixer + + +_BI_QUBITS_MIXER_DOC = BloqDocSpec(bloq_cls=BiQubitsMixer, examples=[_bi_qubits_mixer]) + + @frozen class SingleQubitCompare(GateWithRegisters): """Applies U|a>|b>|0>|0> = |a> |a=b> |(a |(a>b)> - Source: (FIG. 3) in https://static-content.springer.com/esm/art%3A10.1038%2Fs41534-018-0071-5/MediaObjects/41534_2018_71_MOESM1_ESM.pdf - """ # pylint: disable=line-too-long + References: + Supplementary Materials: Improved Techniques for Preparing Eigenstates of Fermionic Hamiltonians. + Figure 3. + https://static-content.springer.com/esm/art%3A10.1038%2Fs41534-018-0071-5/MediaObjects/41534_2018_71_MOESM1_ESM.pdf + """ is_adjoint: bool = False @@ -306,9 +343,19 @@ def _t_complexity_(self) -> TComplexity: return TComplexity(t=4, clifford=16) +@bloq_example +def _sq_cmp() -> SingleQubitCompare: + sq_cmp = SingleQubitCompare() + return sq_cmp + + +_SQ_CMP_DOC = BloqDocSpec(bloq_cls=SingleQubitCompare, examples=[_sq_cmp]) + + def _equality_with_zero( context: cirq.DecompositionContext, qubits: Sequence[cirq.Qid], z: cirq.Qid ) -> cirq.OP_TREE: + """Helper decomposition used in `LessThanEqual`""" if len(qubits) == 1: (q,) = qubits yield cirq.X(q) @@ -323,7 +370,36 @@ def _equality_with_zero( @frozen class LessThanEqual(GateWithRegisters, cirq.ArithmeticGate): - """Applies U|x>|y>|z> = |x>|y> |z ^ (x <= y)>""" + """Applies U|x>|y>|z> = |x>|y> |z ^ (x <= y)> + + Decomposes the gate in a T-complexity optimal way. + + The construction can be broken in 4 parts: + 1. In case of differing bitsizes then a multicontrol And Gate + (Section III.A. of the first reference) is used to check whether + the extra prefix is equal to zero and the result is stored in the `prefix_equality` qubit. + 2. The tree structure (Fig. 2) of the second reference. + followed by a `SingleQubitCompare` to compute the result of comparison of + the suffixes of equal length. The result is stored in `less_than` and `greater_than` and + equality in `qubits[-2]` + 3. The results from the previous two steps are combined to update the target qubit. + 4. The adjoint of the previous operations is added to restore the input qubits + to their original state and clean the ancilla qubits. + + When both registers are of the same size the T complexity is + 8n - 4 as in the second reference. + + When the registers differ in size and `n` is the size of the smaller one and + `d` is the difference in size, the T complexity is the sum of the tree + decomposition as before giving 8n + O(1); and the T complexity of an `And` gate + over `d` registers giving 4d + O(1). This totals 8n + 4d + O(1). + + References: + Encoding Electronic Spectra in Quantum Circuits with Linear T Complexity. https://arxiv.org/abs/1805.03662. + + Supplementary Materials: Improved Techniques for Preparing Eigenstates of Fermionic Hamiltonians. + https://static-content.springer.com/esm/art%3A10.1038%2Fs41534-018-0071-5/MediaObjects/41534_2018_71_MOESM1_ESM.pdf + """ x_bitsize: int y_bitsize: int @@ -364,10 +440,6 @@ def __pow__(self, power: int): def _decompose_via_tree( self, context: cirq.DecompositionContext, X: Sequence[cirq.Qid], Y: Sequence[cirq.Qid] ) -> cirq.OP_TREE: - """Returns comparison oracle from https://static-content.springer.com/esm/art%3A10.1038%2Fs41534-018-0071-5/MediaObjects/41534_2018_71_MOESM1_ESM.pdf - - This decomposition follows the tree structure of (FIG. 2) - """ # pylint: disable=line-too-long if len(X) == 1: return if len(X) == 2: @@ -384,21 +456,6 @@ def _decompose_via_tree( def decompose_from_registers( self, *, context: cirq.DecompositionContext, **quregs: NDArray[cirq.Qid] ) -> cirq.OP_TREE: - """Decomposes the gate in a T-complexity optimal way. - - The construction can be broken in 4 parts: - 1. In case of differing bitsizes then a multicontrol And Gate - - Section III.A. https://arxiv.org/abs/1805.03662) is used to check whether - the extra prefix is equal to zero: - - result stored in: `prefix_equality` qubit. - 2. The tree structure (FIG. 2) https://static-content.springer.com/esm/art%3A10.1038%2Fs41534-018-0071-5/MediaObjects/41534_2018_71_MOESM1_ESM.pdf - followed by a SingleQubitCompare to compute the result of comparison of - the suffixes of equal length: - - result stored in: `less_than` and `greater_than` with equality in qubits[-2] - 3. The results from the previous two steps are combined to update the target qubit. - 4. The adjoint of the previous operations is added to restore the input qubits - to their original state and clean the ancilla qubits. - """ # pylint: disable=line-too-long lhs, rhs, (target,) = quregs['x'], quregs['y'], quregs['target'] n = min(len(lhs), len(rhs)) @@ -462,7 +519,7 @@ def _t_complexity_(self) -> TComplexity: is_second_longer = self.y_bitsize > self.x_bitsize if d == 0: # When both registers are of the same size the T complexity is - # 8n - 4 same as in https://static-content.springer.com/esm/art%3A10.1038%2Fs41534-018-0071-5/MediaObjects/41534_2018_71_MOESM1_ESM.pdf. pylint: disable=line-too-long + # 8n - 4 same as in the second reference. return TComplexity(t=8 * n - 4, clifford=46 * n - 17) else: # When the registers differ in size and `n` is the size of the smaller one and @@ -481,6 +538,24 @@ def _has_unitary_(self): return True +@bloq_example +def _leq_symb() -> LessThanEqual: + n1, n2 = sympy.symbols('n1 n2') + leq_symb = LessThanEqual(x_bitsize=n1, y_bitsize=n2) + return leq_symb + + +@bloq_example +def _leq() -> LessThanEqual: + leq = LessThanEqual(x_bitsize=4, y_bitsize=8) + return leq + + +_LEQ_DOC = BloqDocSpec( + bloq_cls=LessThanEqual, examples=[_leq, _leq_symb] # TODO: support symbolic call graph +) + + @frozen class GreaterThan(Bloq): r"""Compare two integers. @@ -539,11 +614,7 @@ def _greater_than() -> GreaterThan: return greater_than -_GREATER_THAN_DOC = BloqDocSpec( - bloq_cls=GreaterThan, - import_line='from qualtran.bloqs.arithmetic.comparison import GreaterThan', - examples=[_greater_than], -) +_GREATER_THAN_DOC = BloqDocSpec(bloq_cls=GreaterThan, examples=[_greater_than]) @frozen @@ -767,11 +838,7 @@ def _gt_k() -> GreaterThanConstant: return gt_k -_GREATER_THAN_K_DOC = BloqDocSpec( - bloq_cls=GreaterThanConstant, - import_line='from qualtran.bloqs.arithmetic.comparison import GreaterThanConstant', - examples=[_gt_k], -) +_GREATER_THAN_K_DOC = BloqDocSpec(bloq_cls=GreaterThanConstant, examples=[_gt_k]) @frozen @@ -827,8 +894,4 @@ def _eq_k() -> EqualsAConstant: return eq_k -_EQUALS_K_DOC = BloqDocSpec( - bloq_cls=EqualsAConstant, - import_line='from qualtran.bloqs.arithmetic.comparison import EqualsAConstant', - examples=[_eq_k], -) +_EQUALS_K_DOC = BloqDocSpec(bloq_cls=EqualsAConstant, examples=[_eq_k]) diff --git a/qualtran/bloqs/arithmetic/conversions.ipynb b/qualtran/bloqs/arithmetic/conversions.ipynb index 6bbcc2453..ab7e8fe39 100644 --- a/qualtran/bloqs/arithmetic/conversions.ipynb +++ b/qualtran/bloqs/arithmetic/conversions.ipynb @@ -20,6 +20,7 @@ "outputs": [], "source": [ "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", + "from qualtran import QBit, QInt, QUInt, QAny\n", "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", "from typing import *\n", "import numpy as np\n", @@ -58,7 +59,7 @@ }, "outputs": [], "source": [ - "from qualtran.bloqs.arithmetic.conversions import SignedIntegerToTwosComplement" + "from qualtran.bloqs.arithmetic import SignedIntegerToTwosComplement" ] }, { @@ -126,7 +127,8 @@ }, "outputs": [], "source": [ - "signed_to_twos_g, signed_to_twos_sigma = signed_to_twos.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "signed_to_twos_g, signed_to_twos_sigma = signed_to_twos.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(signed_to_twos_g)\n", "show_counts_sigma(signed_to_twos_sigma)" ] @@ -171,7 +173,7 @@ }, "outputs": [], "source": [ - "from qualtran.bloqs.arithmetic.conversions import ToContiguousIndex" + "from qualtran.bloqs.arithmetic import ToContiguousIndex" ] }, { @@ -239,7 +241,8 @@ }, "outputs": [], "source": [ - "to_contg_index_g, to_contg_index_sigma = to_contg_index.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "to_contg_index_g, to_contg_index_sigma = to_contg_index.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(to_contg_index_g)\n", "show_counts_sigma(to_contg_index_sigma)" ] diff --git a/qualtran/bloqs/arithmetic/conversions.py b/qualtran/bloqs/arithmetic/conversions.py index 121ecfa2d..8f985a10a 100644 --- a/qualtran/bloqs/arithmetic/conversions.py +++ b/qualtran/bloqs/arithmetic/conversions.py @@ -109,11 +109,7 @@ def _to_contg_index() -> ToContiguousIndex: return to_contg_index -_TO_CONTG_INDX = BloqDocSpec( - bloq_cls=ToContiguousIndex, - import_line='from qualtran.bloqs.arithmetic.conversions import ToContiguousIndex', - examples=(_to_contg_index,), -) +_TO_CONTG_INDX = BloqDocSpec(bloq_cls=ToContiguousIndex, examples=[_to_contg_index]) @frozen @@ -155,8 +151,4 @@ def _signed_to_twos() -> SignedIntegerToTwosComplement: return signed_to_twos -_SIGNED_TO_TWOS = BloqDocSpec( - bloq_cls=SignedIntegerToTwosComplement, - import_line='from qualtran.bloqs.arithmetic.conversions import SignedIntegerToTwosComplement', - examples=(_signed_to_twos,), -) +_SIGNED_TO_TWOS = BloqDocSpec(bloq_cls=SignedIntegerToTwosComplement, examples=[_signed_to_twos]) diff --git a/qualtran/bloqs/arithmetic/conversions_test.py b/qualtran/bloqs/arithmetic/conversions_test.py index 2659d477e..8b2baff7b 100644 --- a/qualtran/bloqs/arithmetic/conversions_test.py +++ b/qualtran/bloqs/arithmetic/conversions_test.py @@ -12,6 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. +import pytest + +import qualtran.testing as qlt_testing from qualtran import BloqBuilder from qualtran.bloqs.arithmetic.conversions import ( _signed_to_twos, @@ -49,3 +52,8 @@ def test_signed_to_twos_complement_t_complexity(): cbloq = bb.finalize(x=q0) _, sigma = cbloq.call_graph() assert sigma[TGate()] == 4 * (5 - 2) + + +@pytest.mark.notebook +def test_notebook(): + qlt_testing.execute_notebook("conversions") diff --git a/qualtran/bloqs/arithmetic/multiplication.ipynb b/qualtran/bloqs/arithmetic/multiplication.ipynb index 7def60abe..2ba0fd059 100644 --- a/qualtran/bloqs/arithmetic/multiplication.ipynb +++ b/qualtran/bloqs/arithmetic/multiplication.ipynb @@ -20,6 +20,7 @@ "outputs": [], "source": [ "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", + "from qualtran import QBit, QInt, QUInt, QAny\n", "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", "from typing import *\n", "import numpy as np\n", @@ -47,7 +48,7 @@ }, "outputs": [], "source": [ - "from qualtran.bloqs.arithmetic.multiplication import PlusEqualProduct" + "from qualtran.bloqs.arithmetic import PlusEqualProduct" ] }, { @@ -102,7 +103,8 @@ }, "outputs": [], "source": [ - "plus_equal_product_g, plus_equal_product_sigma = plus_equal_product.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "plus_equal_product_g, plus_equal_product_sigma = plus_equal_product.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(plus_equal_product_g)\n", "show_counts_sigma(plus_equal_product_sigma)" ] @@ -144,7 +146,7 @@ }, "outputs": [], "source": [ - "from qualtran.bloqs.arithmetic.multiplication import Product" + "from qualtran.bloqs.arithmetic import Product" ] }, { @@ -212,7 +214,8 @@ }, "outputs": [], "source": [ - "product_g, product_sigma = product.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "product_g, product_sigma = product.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(product_g)\n", "show_counts_sigma(product_sigma)" ] @@ -250,7 +253,7 @@ }, "outputs": [], "source": [ - "from qualtran.bloqs.arithmetic.multiplication import Square" + "from qualtran.bloqs.arithmetic import Square" ] }, { @@ -318,7 +321,8 @@ }, "outputs": [], "source": [ - "square_g, square_sigma = square.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "square_g, square_sigma = square.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(square_g)\n", "show_counts_sigma(square_sigma)" ] @@ -362,7 +366,7 @@ }, "outputs": [], "source": [ - "from qualtran.bloqs.arithmetic.multiplication import SumOfSquares" + "from qualtran.bloqs.arithmetic import SumOfSquares" ] }, { @@ -430,7 +434,8 @@ }, "outputs": [], "source": [ - "sum_of_squares_g, sum_of_squares_sigma = sum_of_squares.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "sum_of_squares_g, sum_of_squares_sigma = sum_of_squares.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(sum_of_squares_g)\n", "show_counts_sigma(sum_of_squares_sigma)" ] @@ -476,7 +481,7 @@ }, "outputs": [], "source": [ - "from qualtran.bloqs.arithmetic.multiplication import ScaleIntByReal" + "from qualtran.bloqs.arithmetic import ScaleIntByReal" ] }, { @@ -544,7 +549,8 @@ }, "outputs": [], "source": [ - "scale_int_by_real_g, scale_int_by_real_sigma = scale_int_by_real.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "scale_int_by_real_g, scale_int_by_real_sigma = scale_int_by_real.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(scale_int_by_real_g)\n", "show_counts_sigma(scale_int_by_real_sigma)" ] @@ -589,7 +595,7 @@ }, "outputs": [], "source": [ - "from qualtran.bloqs.arithmetic.multiplication import MultiplyTwoReals" + "from qualtran.bloqs.arithmetic import MultiplyTwoReals" ] }, { @@ -657,7 +663,8 @@ }, "outputs": [], "source": [ - "multiply_two_reals_g, multiply_two_reals_sigma = multiply_two_reals.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "multiply_two_reals_g, multiply_two_reals_sigma = multiply_two_reals.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(multiply_two_reals_g)\n", "show_counts_sigma(multiply_two_reals_sigma)" ] @@ -702,7 +709,7 @@ }, "outputs": [], "source": [ - "from qualtran.bloqs.arithmetic.multiplication import SquareRealNumber" + "from qualtran.bloqs.arithmetic import SquareRealNumber" ] }, { @@ -770,7 +777,8 @@ }, "outputs": [], "source": [ - "square_real_number_g, square_real_number_sigma = square_real_number.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "square_real_number_g, square_real_number_sigma = square_real_number.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(square_real_number_g)\n", "show_counts_sigma(square_real_number_sigma)" ] diff --git a/qualtran/bloqs/arithmetic/multiplication.py b/qualtran/bloqs/arithmetic/multiplication.py index 55861a9fe..69a919946 100644 --- a/qualtran/bloqs/arithmetic/multiplication.py +++ b/qualtran/bloqs/arithmetic/multiplication.py @@ -115,11 +115,7 @@ def _plus_equal_product() -> PlusEqualProduct: return plus_equal_product -_PLUS_EQUALS_PRODUCT_DOC = BloqDocSpec( - bloq_cls=PlusEqualProduct, - import_line='from qualtran.bloqs.arithmetic.multiplication import PlusEqualProduct', - examples=(_plus_equal_product,), -) +_PLUS_EQUALS_PRODUCT_DOC = BloqDocSpec(bloq_cls=PlusEqualProduct, examples=[_plus_equal_product]) @frozen @@ -206,11 +202,7 @@ def _square() -> Square: return square -_SQUARE_DOC = BloqDocSpec( - bloq_cls=Square, - import_line='from qualtran.bloqs.arithmetic.multiplication import Square', - examples=(_square,), -) +_SQUARE_DOC = BloqDocSpec(bloq_cls=Square, examples=[_square]) @frozen @@ -278,11 +270,7 @@ def _sum_of_squares() -> SumOfSquares: return sum_of_squares -_SUM_OF_SQUARES_DOC = BloqDocSpec( - bloq_cls=SumOfSquares, - import_line='from qualtran.bloqs.arithmetic.multiplication import SumOfSquares', - examples=(_sum_of_squares,), -) +_SUM_OF_SQUARES_DOC = BloqDocSpec(bloq_cls=SumOfSquares, examples=[_sum_of_squares]) @frozen @@ -341,11 +329,7 @@ def _product() -> Product: return product -_PRODUCT_DOC = BloqDocSpec( - bloq_cls=Product, - import_line='from qualtran.bloqs.arithmetic.multiplication import Product', - examples=(_product,), -) +_PRODUCT_DOC = BloqDocSpec(bloq_cls=Product, examples=[_product]) @frozen @@ -412,11 +396,7 @@ def _scale_int_by_real() -> ScaleIntByReal: return scale_int_by_real -_SCALE_INT_BY_REAL_DOC = BloqDocSpec( - bloq_cls=ScaleIntByReal, - import_line='from qualtran.bloqs.arithmetic.multiplication import ScaleIntByReal', - examples=(_scale_int_by_real,), -) +_SCALE_INT_BY_REAL_DOC = BloqDocSpec(bloq_cls=ScaleIntByReal, examples=[_scale_int_by_real]) @frozen @@ -476,11 +456,7 @@ def _multiply_two_reals() -> MultiplyTwoReals: return multiply_two_reals -_MULTIPLY_TWO_REALS_DOC = BloqDocSpec( - bloq_cls=MultiplyTwoReals, - import_line='from qualtran.bloqs.arithmetic.multiplication import MultiplyTwoReals', - examples=(_multiply_two_reals,), -) +_MULTIPLY_TWO_REALS_DOC = BloqDocSpec(bloq_cls=MultiplyTwoReals, examples=[_multiply_two_reals]) @frozen @@ -543,8 +519,4 @@ def _square_real_number() -> SquareRealNumber: return square_real_number -_SQUARE_REAL_NUMBER_DOC = BloqDocSpec( - bloq_cls=SquareRealNumber, - import_line='from qualtran.bloqs.arithmetic.multiplication import SquareRealNumber', - examples=(_square_real_number,), -) +_SQUARE_REAL_NUMBER_DOC = BloqDocSpec(bloq_cls=SquareRealNumber, examples=[_square_real_number]) diff --git a/qualtran/bloqs/arithmetic/sorting.ipynb b/qualtran/bloqs/arithmetic/sorting.ipynb index d82003af6..c61ea33f7 100644 --- a/qualtran/bloqs/arithmetic/sorting.ipynb +++ b/qualtran/bloqs/arithmetic/sorting.ipynb @@ -20,6 +20,7 @@ "outputs": [], "source": [ "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", + "from qualtran import QBit, QInt, QUInt, QAny\n", "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", "from typing import *\n", "import numpy as np\n", @@ -65,7 +66,7 @@ }, "outputs": [], "source": [ - "from qualtran.bloqs.arithmetic.sorting import Comparator" + "from qualtran.bloqs.arithmetic import Comparator" ] }, { @@ -134,7 +135,8 @@ }, "outputs": [], "source": [ - "cmp_symb_g, cmp_symb_sigma = cmp_symb.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "cmp_symb_g, cmp_symb_sigma = cmp_symb.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(cmp_symb_g)\n", "show_counts_sigma(cmp_symb_sigma)" ] @@ -174,7 +176,7 @@ }, "outputs": [], "source": [ - "from qualtran.bloqs.arithmetic.sorting import BitonicSort" + "from qualtran.bloqs.arithmetic import BitonicSort" ] }, { @@ -243,7 +245,8 @@ }, "outputs": [], "source": [ - "bitonic_sort_g, bitonic_sort_sigma = bitonic_sort.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "bitonic_sort_g, bitonic_sort_sigma = bitonic_sort.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(bitonic_sort_g)\n", "show_counts_sigma(bitonic_sort_sigma)" ] diff --git a/qualtran/bloqs/arithmetic/sorting.py b/qualtran/bloqs/arithmetic/sorting.py index 0829acbf1..4fe41d220 100644 --- a/qualtran/bloqs/arithmetic/sorting.py +++ b/qualtran/bloqs/arithmetic/sorting.py @@ -75,11 +75,7 @@ def _cmp_symb() -> Comparator: return cmp_symb -_COMPARATOR_DOC = BloqDocSpec( - bloq_cls=Comparator, - import_line='from qualtran.bloqs.arithmetic.sorting import Comparator', - examples=(_cmp_symb,), -) +_COMPARATOR_DOC = BloqDocSpec(bloq_cls=Comparator, examples=[_cmp_symb]) @frozen @@ -131,8 +127,4 @@ def _bitonic_sort() -> BitonicSort: return bitonic_sort -_BITONIC_SORT_DOC = BloqDocSpec( - bloq_cls=BitonicSort, - import_line='from qualtran.bloqs.arithmetic.sorting import BitonicSort', - examples=(_bitonic_sort,), -) +_BITONIC_SORT_DOC = BloqDocSpec(bloq_cls=BitonicSort, examples=[_bitonic_sort]) diff --git a/qualtran/bloqs/basic_gates/s_gate.ipynb b/qualtran/bloqs/basic_gates/s_gate.ipynb index f804f0488..d6301a890 100644 --- a/qualtran/bloqs/basic_gates/s_gate.ipynb +++ b/qualtran/bloqs/basic_gates/s_gate.ipynb @@ -20,6 +20,7 @@ "outputs": [], "source": [ "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", + "from qualtran import QBit, QInt, QUInt, QAny\n", "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", "from typing import *\n", "import numpy as np\n", @@ -126,7 +127,8 @@ }, "outputs": [], "source": [ - "s_gate_g, s_gate_sigma = s_gate.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "s_gate_g, s_gate_sigma = s_gate.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(s_gate_g)\n", "show_counts_sigma(s_gate_sigma)" ] diff --git a/qualtran/bloqs/basic_gates/t_gate.ipynb b/qualtran/bloqs/basic_gates/t_gate.ipynb index c2d48ffd8..23902a095 100644 --- a/qualtran/bloqs/basic_gates/t_gate.ipynb +++ b/qualtran/bloqs/basic_gates/t_gate.ipynb @@ -20,6 +20,7 @@ "outputs": [], "source": [ "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", + "from qualtran import QBit, QInt, QUInt, QAny\n", "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", "from typing import *\n", "import numpy as np\n", @@ -144,7 +145,8 @@ }, "outputs": [], "source": [ - "t_gate_g, t_gate_sigma = t_gate.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "t_gate_g, t_gate_sigma = t_gate.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(t_gate_g)\n", "show_counts_sigma(t_gate_sigma)" ] diff --git a/qualtran/bloqs/basic_gates/toffoli.ipynb b/qualtran/bloqs/basic_gates/toffoli.ipynb index 06c73d769..bc2e4a9fd 100644 --- a/qualtran/bloqs/basic_gates/toffoli.ipynb +++ b/qualtran/bloqs/basic_gates/toffoli.ipynb @@ -20,6 +20,7 @@ "outputs": [], "source": [ "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", + "from qualtran import QBit, QInt, QUInt, QAny\n", "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", "from typing import *\n", "import numpy as np\n", @@ -135,7 +136,8 @@ }, "outputs": [], "source": [ - "toffoli_g, toffoli_sigma = toffoli.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "toffoli_g, toffoli_sigma = toffoli.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(toffoli_g)\n", "show_counts_sigma(toffoli_sigma)" ] diff --git a/qualtran/bloqs/block_encoding.ipynb b/qualtran/bloqs/block_encoding.ipynb index 774249fa5..5a42e1adc 100644 --- a/qualtran/bloqs/block_encoding.ipynb +++ b/qualtran/bloqs/block_encoding.ipynb @@ -39,6 +39,7 @@ "outputs": [], "source": [ "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", + "from qualtran import QBit, QInt, QUInt, QAny\n", "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", "from typing import *\n", "import numpy as np\n", @@ -162,7 +163,8 @@ }, "outputs": [], "source": [ - "black_box_block_bloq_g, black_box_block_bloq_sigma = black_box_block_bloq.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "black_box_block_bloq_g, black_box_block_bloq_sigma = black_box_block_bloq.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(black_box_block_bloq_g)\n", "show_counts_sigma(black_box_block_bloq_sigma)" ] @@ -284,7 +286,8 @@ }, "outputs": [], "source": [ - "chebyshev_poly_g, chebyshev_poly_sigma = chebyshev_poly.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "chebyshev_poly_g, chebyshev_poly_sigma = chebyshev_poly.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(chebyshev_poly_g)\n", "show_counts_sigma(chebyshev_poly_sigma)" ] diff --git a/qualtran/bloqs/chemistry/df/double_factorization.ipynb b/qualtran/bloqs/chemistry/df/double_factorization.ipynb index 64c130b58..1b46c50ef 100644 --- a/qualtran/bloqs/chemistry/df/double_factorization.ipynb +++ b/qualtran/bloqs/chemistry/df/double_factorization.ipynb @@ -39,6 +39,7 @@ "outputs": [], "source": [ "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", + "from qualtran import QBit, QInt, QUInt, QAny\n", "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", "from typing import *\n", "import numpy as np\n", @@ -175,7 +176,8 @@ }, "outputs": [], "source": [ - "df_one_body_g, df_one_body_sigma = df_one_body.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "df_one_body_g, df_one_body_sigma = df_one_body.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(df_one_body_g)\n", "show_counts_sigma(df_one_body_sigma)" ] @@ -321,7 +323,8 @@ }, "outputs": [], "source": [ - "df_block_encoding_g, df_block_encoding_sigma = df_block_encoding.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "df_block_encoding_g, df_block_encoding_sigma = df_block_encoding.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(df_block_encoding_g)\n", "show_counts_sigma(df_block_encoding_sigma)" ] diff --git a/qualtran/bloqs/chemistry/pbc/first_quantization/first_quantization.ipynb b/qualtran/bloqs/chemistry/pbc/first_quantization/first_quantization.ipynb index 2be21938d..32ffd5806 100644 --- a/qualtran/bloqs/chemistry/pbc/first_quantization/first_quantization.ipynb +++ b/qualtran/bloqs/chemistry/pbc/first_quantization/first_quantization.ipynb @@ -87,6 +87,7 @@ "outputs": [], "source": [ "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", + "from qualtran import QBit, QInt, QUInt, QAny\n", "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", "from typing import *\n", "import numpy as np\n", @@ -218,7 +219,8 @@ }, "outputs": [], "source": [ - "prep_first_quant_g, prep_first_quant_sigma = prep_first_quant.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "prep_first_quant_g, prep_first_quant_sigma = prep_first_quant.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(prep_first_quant_g)\n", "show_counts_sigma(prep_first_quant_sigma)" ] @@ -348,7 +350,8 @@ }, "outputs": [], "source": [ - "sel_first_quant_g, sel_first_quant_sigma = sel_first_quant.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "sel_first_quant_g, sel_first_quant_sigma = sel_first_quant.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(sel_first_quant_g)\n", "show_counts_sigma(sel_first_quant_sigma)" ] @@ -470,7 +473,8 @@ }, "outputs": [], "source": [ - "prepare_t_g, prepare_t_sigma = prepare_t.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "prepare_t_g, prepare_t_sigma = prepare_t.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(prepare_t_g)\n", "show_counts_sigma(prepare_t_sigma)" ] @@ -596,7 +600,8 @@ }, "outputs": [], "source": [ - "prepare_uv_g, prepare_uv_sigma = prepare_uv.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "prepare_uv_g, prepare_uv_sigma = prepare_uv.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(prepare_uv_g)\n", "show_counts_sigma(prepare_uv_sigma)" ] @@ -705,7 +710,8 @@ }, "outputs": [], "source": [ - "select_t_g, select_t_sigma = select_t.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "select_t_g, select_t_sigma = select_t.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(select_t_g)\n", "show_counts_sigma(select_t_sigma)" ] @@ -819,7 +825,8 @@ }, "outputs": [], "source": [ - "select_uv_g, select_uv_sigma = select_uv.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "select_uv_g, select_uv_sigma = select_uv.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(select_uv_g)\n", "show_counts_sigma(select_uv_sigma)" ] diff --git a/qualtran/bloqs/chemistry/pbc/first_quantization/projectile/projectile.ipynb b/qualtran/bloqs/chemistry/pbc/first_quantization/projectile/projectile.ipynb index b23151feb..9cf100436 100644 --- a/qualtran/bloqs/chemistry/pbc/first_quantization/projectile/projectile.ipynb +++ b/qualtran/bloqs/chemistry/pbc/first_quantization/projectile/projectile.ipynb @@ -78,6 +78,7 @@ "outputs": [], "source": [ "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", + "from qualtran import QBit, QInt, QUInt, QAny\n", "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", "from typing import *\n", "import numpy as np\n", @@ -215,7 +216,8 @@ }, "outputs": [], "source": [ - "prep_first_quant_g, prep_first_quant_sigma = prep_first_quant.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "prep_first_quant_g, prep_first_quant_sigma = prep_first_quant.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(prep_first_quant_g)\n", "show_counts_sigma(prep_first_quant_sigma)" ] @@ -348,7 +350,8 @@ }, "outputs": [], "source": [ - "sel_first_quant_g, sel_first_quant_sigma = sel_first_quant.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "sel_first_quant_g, sel_first_quant_sigma = sel_first_quant.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(sel_first_quant_g)\n", "show_counts_sigma(sel_first_quant_sigma)" ] diff --git a/qualtran/bloqs/chemistry/sf/single_factorization.ipynb b/qualtran/bloqs/chemistry/sf/single_factorization.ipynb index 8e40e4edb..58c70f48f 100644 --- a/qualtran/bloqs/chemistry/sf/single_factorization.ipynb +++ b/qualtran/bloqs/chemistry/sf/single_factorization.ipynb @@ -30,6 +30,7 @@ "outputs": [], "source": [ "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", + "from qualtran import QBit, QInt, QUInt, QAny\n", "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", "from typing import *\n", "import numpy as np\n", @@ -166,7 +167,8 @@ }, "outputs": [], "source": [ - "sf_one_body_g, sf_one_body_sigma = sf_one_body.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "sf_one_body_g, sf_one_body_sigma = sf_one_body.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(sf_one_body_g)\n", "show_counts_sigma(sf_one_body_sigma)" ] @@ -295,7 +297,8 @@ }, "outputs": [], "source": [ - "sf_block_encoding_g, sf_block_encoding_sigma = sf_block_encoding.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "sf_block_encoding_g, sf_block_encoding_sigma = sf_block_encoding.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(sf_block_encoding_g)\n", "show_counts_sigma(sf_block_encoding_sigma)" ] diff --git a/qualtran/bloqs/chemistry/sparse/sparse.ipynb b/qualtran/bloqs/chemistry/sparse/sparse.ipynb index 78f093b54..3b25c155a 100644 --- a/qualtran/bloqs/chemistry/sparse/sparse.ipynb +++ b/qualtran/bloqs/chemistry/sparse/sparse.ipynb @@ -64,6 +64,7 @@ "outputs": [], "source": [ "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", + "from qualtran import QBit, QInt, QUInt, QAny\n", "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", "from typing import *\n", "import numpy as np\n", @@ -210,7 +211,8 @@ }, "outputs": [], "source": [ - "prep_sparse_g, prep_sparse_sigma = prep_sparse.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "prep_sparse_g, prep_sparse_sigma = prep_sparse.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(prep_sparse_g)\n", "show_counts_sigma(prep_sparse_sigma)" ] @@ -323,7 +325,8 @@ }, "outputs": [], "source": [ - "sel_sparse_g, sel_sparse_sigma = sel_sparse.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "sel_sparse_g, sel_sparse_sigma = sel_sparse.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(sel_sparse_g)\n", "show_counts_sigma(sel_sparse_sigma)" ] diff --git a/qualtran/bloqs/chemistry/thc/thc.ipynb b/qualtran/bloqs/chemistry/thc/thc.ipynb index d202b15fe..4cca694fb 100644 --- a/qualtran/bloqs/chemistry/thc/thc.ipynb +++ b/qualtran/bloqs/chemistry/thc/thc.ipynb @@ -22,6 +22,7 @@ "outputs": [], "source": [ "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", + "from qualtran import QBit, QInt, QUInt, QAny\n", "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", "from typing import *\n", "import numpy as np\n", @@ -149,7 +150,8 @@ }, "outputs": [], "source": [ - "thc_uni_g, thc_uni_sigma = thc_uni.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "thc_uni_g, thc_uni_sigma = thc_uni.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(thc_uni_g)\n", "show_counts_sigma(thc_uni_sigma)" ] @@ -353,7 +355,8 @@ }, "outputs": [], "source": [ - "thc_prep_g, thc_prep_sigma = thc_prep.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "thc_prep_g, thc_prep_sigma = thc_prep.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(thc_prep_g)\n", "show_counts_sigma(thc_prep_sigma)" ] @@ -540,7 +543,8 @@ }, "outputs": [], "source": [ - "thc_sel_g, thc_sel_sigma = thc_sel.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "thc_sel_g, thc_sel_sigma = thc_sel.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(thc_sel_g)\n", "show_counts_sigma(thc_sel_sigma)" ] diff --git a/qualtran/bloqs/data_loading/qrom.ipynb b/qualtran/bloqs/data_loading/qrom.ipynb index f0bdea376..700d551ca 100644 --- a/qualtran/bloqs/data_loading/qrom.ipynb +++ b/qualtran/bloqs/data_loading/qrom.ipynb @@ -22,6 +22,7 @@ "outputs": [], "source": [ "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", + "from qualtran import QBit, QInt, QUInt, QAny\n", "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", "from typing import *\n", "import numpy as np\n", @@ -165,7 +166,8 @@ }, "outputs": [], "source": [ - "qrom_small_g, qrom_small_sigma = qrom_small.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "qrom_small_g, qrom_small_sigma = qrom_small.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(qrom_small_g)\n", "show_counts_sigma(qrom_small_sigma)" ] diff --git a/qualtran/bloqs/factoring/mod_exp.ipynb b/qualtran/bloqs/factoring/mod_exp.ipynb index 8b76c8604..613371d05 100644 --- a/qualtran/bloqs/factoring/mod_exp.ipynb +++ b/qualtran/bloqs/factoring/mod_exp.ipynb @@ -20,6 +20,7 @@ "outputs": [], "source": [ "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", + "from qualtran import QBit, QInt, QUInt, QAny\n", "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", "from typing import *\n", "import numpy as np\n", @@ -178,7 +179,8 @@ }, "outputs": [], "source": [ - "modexp_symb_g, modexp_symb_sigma = modexp_symb.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "modexp_symb_g, modexp_symb_sigma = modexp_symb.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(modexp_symb_g)\n", "show_counts_sigma(modexp_symb_sigma)" ] diff --git a/qualtran/bloqs/factoring/mod_mul.ipynb b/qualtran/bloqs/factoring/mod_mul.ipynb index b51049613..c2d870924 100644 --- a/qualtran/bloqs/factoring/mod_mul.ipynb +++ b/qualtran/bloqs/factoring/mod_mul.ipynb @@ -20,6 +20,7 @@ "outputs": [], "source": [ "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", + "from qualtran import QBit, QInt, QUInt, QAny\n", "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", "from typing import *\n", "import numpy as np\n", @@ -157,7 +158,8 @@ }, "outputs": [], "source": [ - "modmul_symb_g, modmul_symb_sigma = modmul_symb.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "modmul_symb_g, modmul_symb_sigma = modmul_symb.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(modmul_symb_g)\n", "show_counts_sigma(modmul_symb_sigma)" ] diff --git a/qualtran/bloqs/qft/two_bit_ffft.ipynb b/qualtran/bloqs/qft/two_bit_ffft.ipynb index 57e6ec8cf..93ae2b774 100644 --- a/qualtran/bloqs/qft/two_bit_ffft.ipynb +++ b/qualtran/bloqs/qft/two_bit_ffft.ipynb @@ -20,6 +20,7 @@ "outputs": [], "source": [ "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", + "from qualtran import QBit, QInt, QUInt, QAny\n", "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", "from typing import *\n", "import numpy as np\n", @@ -125,7 +126,8 @@ }, "outputs": [], "source": [ - "two_bit_ffft_g, two_bit_ffft_sigma = two_bit_ffft.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "two_bit_ffft_g, two_bit_ffft_sigma = two_bit_ffft.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(two_bit_ffft_g)\n", "show_counts_sigma(two_bit_ffft_sigma)" ] diff --git a/qualtran/bloqs/reflection.ipynb b/qualtran/bloqs/reflection.ipynb index 61e92f855..c4c0a4ba4 100644 --- a/qualtran/bloqs/reflection.ipynb +++ b/qualtran/bloqs/reflection.ipynb @@ -22,6 +22,7 @@ "outputs": [], "source": [ "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", + "from qualtran import QBit, QInt, QUInt, QAny\n", "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", "from typing import *\n", "import numpy as np\n", @@ -124,7 +125,8 @@ }, "outputs": [], "source": [ - "reflection_g, reflection_sigma = reflection.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "reflection_g, reflection_sigma = reflection.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(reflection_g)\n", "show_counts_sigma(reflection_sigma)" ] diff --git a/qualtran/bloqs/rotations/phasing_via_cost_function.ipynb b/qualtran/bloqs/rotations/phasing_via_cost_function.ipynb index 3534552ea..6bb4e0e20 100644 --- a/qualtran/bloqs/rotations/phasing_via_cost_function.ipynb +++ b/qualtran/bloqs/rotations/phasing_via_cost_function.ipynb @@ -20,6 +20,7 @@ "outputs": [], "source": [ "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", + "from qualtran import QBit, QInt, QUInt, QAny\n", "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", "from typing import *\n", "import numpy as np\n", @@ -182,7 +183,8 @@ }, "outputs": [], "source": [ - "square_via_phase_gradient_g, square_via_phase_gradient_sigma = square_via_phase_gradient.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "square_via_phase_gradient_g, square_via_phase_gradient_sigma = square_via_phase_gradient.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(square_via_phase_gradient_g)\n", "show_counts_sigma(square_via_phase_gradient_sigma)" ] diff --git a/qualtran/bloqs/rotations/quantum_variable_rotation.ipynb b/qualtran/bloqs/rotations/quantum_variable_rotation.ipynb index 9bde1dff1..c1305517a 100644 --- a/qualtran/bloqs/rotations/quantum_variable_rotation.ipynb +++ b/qualtran/bloqs/rotations/quantum_variable_rotation.ipynb @@ -61,6 +61,7 @@ "outputs": [], "source": [ "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", + "from qualtran import QBit, QInt, QUInt, QAny\n", "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", "from typing import *\n", "import numpy as np\n", @@ -171,7 +172,8 @@ }, "outputs": [], "source": [ - "qvr_zpow_g, qvr_zpow_sigma = qvr_zpow.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "qvr_zpow_g, qvr_zpow_sigma = qvr_zpow.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(qvr_zpow_g)\n", "show_counts_sigma(qvr_zpow_sigma)" ] @@ -374,7 +376,8 @@ }, "outputs": [], "source": [ - "qvr_phase_gradient_g, qvr_phase_gradient_sigma = qvr_phase_gradient.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "qvr_phase_gradient_g, qvr_phase_gradient_sigma = qvr_phase_gradient.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(qvr_phase_gradient_g)\n", "show_counts_sigma(qvr_phase_gradient_sigma)" ] diff --git a/qualtran/bloqs/state_preparation/state_preparation_via_rotation.ipynb b/qualtran/bloqs/state_preparation/state_preparation_via_rotation.ipynb index a9906b338..e90b29b77 100644 --- a/qualtran/bloqs/state_preparation/state_preparation_via_rotation.ipynb +++ b/qualtran/bloqs/state_preparation/state_preparation_via_rotation.ipynb @@ -79,6 +79,7 @@ "outputs": [], "source": [ "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", + "from qualtran import QBit, QInt, QUInt, QAny\n", "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", "from typing import *\n", "import numpy as np\n", @@ -205,7 +206,8 @@ }, "outputs": [], "source": [ - "state_prep_via_rotation_g, state_prep_via_rotation_sigma = state_prep_via_rotation.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "state_prep_via_rotation_g, state_prep_via_rotation_sigma = state_prep_via_rotation.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(state_prep_via_rotation_g)\n", "show_counts_sigma(state_prep_via_rotation_sigma)" ] diff --git a/qualtran/bloqs/swap_network/swap_network.ipynb b/qualtran/bloqs/swap_network/swap_network.ipynb index 93c05ce96..93a6b064f 100644 --- a/qualtran/bloqs/swap_network/swap_network.ipynb +++ b/qualtran/bloqs/swap_network/swap_network.ipynb @@ -22,6 +22,7 @@ "outputs": [], "source": [ "from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", + "from qualtran import QBit, QInt, QUInt, QAny\n", "from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", "from typing import *\n", "import numpy as np\n", @@ -158,7 +159,8 @@ }, "outputs": [], "source": [ - "cswap_symb_g, cswap_symb_sigma = cswap_symb.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "cswap_symb_g, cswap_symb_sigma = cswap_symb.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(cswap_symb_g)\n", "show_counts_sigma(cswap_symb_sigma)" ] @@ -298,7 +300,8 @@ }, "outputs": [], "source": [ - "approx_cswap_symb_g, approx_cswap_symb_sigma = approx_cswap_symb.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "approx_cswap_symb_g, approx_cswap_symb_sigma = approx_cswap_symb.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(approx_cswap_symb_g)\n", "show_counts_sigma(approx_cswap_symb_sigma)" ] @@ -413,7 +416,8 @@ }, "outputs": [], "source": [ - "swz_g, swz_sigma = swz.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "swz_g, swz_sigma = swz.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(swz_g)\n", "show_counts_sigma(swz_sigma)" ] @@ -540,7 +544,8 @@ }, "outputs": [], "source": [ - "multiplexed_cswap_g, multiplexed_cswap_sigma = multiplexed_cswap.call_graph()\n", + "from qualtran.resource_counting.generalizers import ignore_split_join\n", + "multiplexed_cswap_g, multiplexed_cswap_sigma = multiplexed_cswap.call_graph(max_depth=1, generalizer=ignore_split_join)\n", "show_call_graph(multiplexed_cswap_g)\n", "show_counts_sigma(multiplexed_cswap_sigma)" ]