Skip to content

Commit

Permalink
Add more arithmetic gates. (#381)
Browse files Browse the repository at this point in the history
* Add more arithmetic gates.

* Fix formatting.

* Fix registers formatting.

* Use cirq_ft's t_complexity for other comparators bloq_counts / t_complexity.

* Fix t_complexity calls.

* Address review comments.

* Fix unit test.
  • Loading branch information
fdmalone authored Oct 10, 2023
1 parent bb4af36 commit cf5222b
Show file tree
Hide file tree
Showing 5 changed files with 349 additions and 39 deletions.
3 changes: 3 additions & 0 deletions dev_tools/autogenerate-bloqs-notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@
BloqNbSpec(qualtran.bloqs.arithmetic_test._make_square),
BloqNbSpec(qualtran.bloqs.arithmetic_test._make_sum_of_squares),
BloqNbSpec(qualtran.bloqs.arithmetic_test._make_greater_than),
BloqNbSpec(qualtran.bloqs.arithmetic_test._make_greater_than_constant),
BloqNbSpec(qualtran.bloqs.arithmetic_test._make_equals_a_constant),
BloqNbSpec(qualtran.bloqs.arithmetic_test._make_to_contiguous_index),
BloqNbSpec(qualtran.bloqs.arithmetic_test._make_scale_int_by_real),
BloqNbSpec(qualtran.bloqs.arithmetic_test._make_multiply_two_reals),
BloqNbSpec(qualtran.bloqs.arithmetic_test._make_square_real_number),
Expand Down
171 changes: 148 additions & 23 deletions qualtran/bloqs/arithmetic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "40368a5b",
"id": "bd6ce2e3",
"metadata": {
"cq.autogen": "title_cell"
},
Expand All @@ -13,7 +13,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "410926b9",
"id": "8133a7f0",
"metadata": {
"cq.autogen": "top_imports"
},
Expand All @@ -27,7 +27,7 @@
},
{
"cell_type": "markdown",
"id": "b92171ba",
"id": "ae9dfdf0",
"metadata": {
"cq.autogen": "_make_add.md"
},
Expand All @@ -51,7 +51,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "f930d4ff",
"id": "543903f4",
"metadata": {
"cq.autogen": "_make_add.py"
},
Expand All @@ -65,7 +65,7 @@
},
{
"cell_type": "markdown",
"id": "3b20c805",
"id": "dd75387c",
"metadata": {
"cq.autogen": "_make_product.md"
},
Expand All @@ -92,7 +92,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "877b583b",
"id": "38185fa9",
"metadata": {
"cq.autogen": "_make_product.py"
},
Expand All @@ -106,7 +106,7 @@
},
{
"cell_type": "markdown",
"id": "f50caf16",
"id": "4bdb27f5",
"metadata": {
"cq.autogen": "_make_square.md"
},
Expand All @@ -130,7 +130,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "633eb3ba",
"id": "49618c06",
"metadata": {
"cq.autogen": "_make_square.py"
},
Expand All @@ -144,7 +144,7 @@
},
{
"cell_type": "markdown",
"id": "18e2cca7",
"id": "e9028de9",
"metadata": {
"cq.autogen": "_make_sum_of_squares.md"
},
Expand Down Expand Up @@ -173,7 +173,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "95d0069d",
"id": "a5cf7d8b",
"metadata": {
"cq.autogen": "_make_sum_of_squares.py"
},
Expand All @@ -187,7 +187,7 @@
},
{
"cell_type": "markdown",
"id": "20639a7a",
"id": "190b0a0f",
"metadata": {
"cq.autogen": "_make_greater_than.md"
},
Expand All @@ -198,23 +198,25 @@
"Implements $U|a\\rangle|b\\rangle|0\\rangle \\rightarrow\n",
"|a\\rangle|b\\rangle|a > b\\rangle$ using $8n T$ gates.\n",
"\n",
"The bloq_counts and t_complexity are derived from equivalent cirq_ft gates\n",
"assuming a clean decomposition which should yield identical costs.\n",
"\n",
"See: https://github.com/quantumlib/Qualtran/pull/381 and\n",
"https://qualtran.readthedocs.io/en/latest/bloqs/comparison_gates.html\n",
"\n",
"#### Parameters\n",
" - `bitsize`: Number of bits used to represent the two integers a and b. \n",
"\n",
"#### Registers\n",
" - `a`: n-bit-sized input registers.\n",
" - `b`: n-bit-sized input registers.\n",
" - `result`: A single bit output register to store the result of A > B. \n",
"\n",
"#### References\n",
"[Improved techniques for preparing eigenstates of fermionic Hamiltonians](https://www.nature.com/articles/s41534-018-0071-5#additional-information), Comparison Oracle from SI: Appendix 2B (pg 3)\n"
" - `target`: A single bit output register to store the result of A > B.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "af66690c",
"id": "0ea3e1aa",
"metadata": {
"cq.autogen": "_make_greater_than.py"
},
Expand All @@ -228,7 +230,130 @@
},
{
"cell_type": "markdown",
"id": "1c63d263",
"id": "895d5f80",
"metadata": {
"cq.autogen": "_make_greater_than_constant.md"
},
"source": [
"## `GreaterThanConstant`\n",
"Implements $U_a|x\\rangle = U_a|x\\rangle|z\\rangle = |x\\rangle |z \\land (x > a)\\rangle$\n",
"\n",
"The bloq_counts and t_complexity are derived from equivalent cirq_ft gates\n",
"assuming a clean decomposition which should yield identical costs.\n",
"\n",
"See: https://github.com/quantumlib/Qualtran/pull/381 and\n",
"https://qualtran.readthedocs.io/en/latest/bloqs/comparison_gates.html\n",
"\n",
"\n",
"#### Parameters\n",
" - `bitsize`: bitsize of x register.\n",
" - `val`: integer to compare x against (a above.) \n",
"\n",
"#### Registers\n",
" - `x`: Register to compare against val.\n",
" - `target`: Register to hold result of comparison.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "066a16fe",
"metadata": {
"cq.autogen": "_make_greater_than_constant.py"
},
"outputs": [],
"source": [
"from qualtran.bloqs.arithmetic import GreaterThanConstant\n",
"\n",
"bloq = GreaterThanConstant(bitsize=4, val=13)\n",
"show_bloq(bloq)"
]
},
{
"cell_type": "markdown",
"id": "0158ca13",
"metadata": {
"cq.autogen": "_make_equals_a_constant.md"
},
"source": [
"## `EqualsAConstant`\n",
"Implements $U_a|x\\rangle = U_a|x\\rangle|z\\rangle = |x\\rangle |z \\land (x = a)\\rangle$\n",
"\n",
"The bloq_counts and t_complexity are derived from:\n",
"https://qualtran.readthedocs.io/en/latest/bloqs/comparison_gates.html#equality-as-a-special-case\n",
"\n",
"#### Parameters\n",
" - `bitsize`: bitsize of x register.\n",
" - `val`: integer to compare x against (a above.) \n",
"\n",
"#### Registers\n",
" - `x`: Register to compare against val.\n",
" - `target`: Register to hold result of comparison.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aa8d3095",
"metadata": {
"cq.autogen": "_make_equals_a_constant.py"
},
"outputs": [],
"source": [
"from qualtran.bloqs.arithmetic import EqualsAConstant\n",
"\n",
"bloq = EqualsAConstant(bitsize=4, val=13)\n",
"show_bloq(bloq)"
]
},
{
"cell_type": "markdown",
"id": "ced59882",
"metadata": {
"cq.autogen": "_make_to_contiguous_index.md"
},
"source": [
"## `ToContiguousIndex`\n",
"Build a contiguous register s from mu and nu.\n",
"\n",
"$$\n",
" s = \\nu (\\nu + 1) / 2 + \\mu\n",
"$$\n",
"\n",
"Assuming nu is zero indexed (in contrast to the THC paper which assumes 1,\n",
"hence the slightly different formula).\n",
"\n",
"#### Parameters\n",
" - `bitsize`: number of bits for mu and nu registers.\n",
" - `s_bitsize`: Number of bits for contiguous register. \n",
"\n",
"#### Registers\n",
" - `mu`: input register\n",
" - `nu`: input register\n",
" - `s`: output contiguous register \n",
"\n",
"#### References\n",
"(Even more efficient quantum computations of chemistry through tensor hypercontraction)[https://arxiv.org/pdf/2011.03494.pdf] Eq. 29.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b0581d0a",
"metadata": {
"cq.autogen": "_make_to_contiguous_index.py"
},
"outputs": [],
"source": [
"from qualtran.bloqs.arithmetic import ToContiguousIndex\n",
"\n",
"bloq = ToContiguousIndex(bitsize=4, s_bitsize=8)\n",
"show_bloq(bloq)"
]
},
{
"cell_type": "markdown",
"id": "12a1747d",
"metadata": {
"cq.autogen": "_make_scale_int_by_real.md"
},
Expand Down Expand Up @@ -260,7 +385,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "4538d32b",
"id": "e82868fe",
"metadata": {
"cq.autogen": "_make_scale_int_by_real.py"
},
Expand All @@ -274,7 +399,7 @@
},
{
"cell_type": "markdown",
"id": "6247c0bd",
"id": "20c439a4",
"metadata": {
"cq.autogen": "_make_multiply_two_reals.md"
},
Expand Down Expand Up @@ -305,7 +430,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "5b8be106",
"id": "6d1dffcc",
"metadata": {
"cq.autogen": "_make_multiply_two_reals.py"
},
Expand All @@ -319,7 +444,7 @@
},
{
"cell_type": "markdown",
"id": "7b0a73bb",
"id": "c1e1cf01",
"metadata": {
"cq.autogen": "_make_square_real_number.md"
},
Expand Down Expand Up @@ -350,7 +475,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "ec6335d9",
"id": "12b00bbd",
"metadata": {
"cq.autogen": "_make_square_real_number.py"
},
Expand Down Expand Up @@ -379,7 +504,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit cf5222b

Please sign in to comment.