Skip to content

Commit

Permalink
Add more arithmetic bloqs.
Browse files Browse the repository at this point in the history
* Square real
* Scale int by real
* Multiply two reals

Fix bug with bitsize for sum of squares.
  • Loading branch information
fdmalone committed Oct 5, 2023
1 parent 9021b71 commit 005fa98
Show file tree
Hide file tree
Showing 4 changed files with 466 additions and 23 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_scale_int_by_real),
BloqNbSpec(qualtran.bloqs.arithmetic_test._make_multiply_two_reals),
BloqNbSpec(qualtran.bloqs.arithmetic_test._make_square_real_number),
],
directory=f'{SOURCE_DIR}/bloqs',
),
Expand Down
150 changes: 144 additions & 6 deletions qualtran/bloqs/arithmetic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
},
"source": [
"## `Product`\n",
"Compute the product of an `n` and `m` bit integer.\n",
"Compute the product of an `n` and `m` bit binary number.\n",
"\n",
"Implements $U|a\\rangle|b\\rangle|0\\rangle -\\rightarrow\n",
"|a\\rangle|b\\rangle|a\\times b\\rangle$ using $2nm-n$ Toffolis.\n",
Expand Down Expand Up @@ -112,9 +112,9 @@
},
"source": [
"## `Square`\n",
"Square an n-bit number.\n",
"Square an n-bit binary number.\n",
"\n",
"Implements $U|a\\rangle|0\\rangle \\rightarrow |a\\rangle|a^2\\rangle$ using $4n - 4 T$ gates.\n",
"Implements $U|a\\rangle|0\\rangle \\rightarrow |a\\rangle|a^2\\rangle$ using $n^2 - n$ Toffolis.\n",
"\n",
"#### Parameters\n",
" - `bitsize`: Number of bits used to represent the integer to be squared. The result is stored in a register of size 2*bitsize. \n",
Expand Down Expand Up @@ -150,19 +150,21 @@
},
"source": [
"## `SumOfSquares`\n",
"Compute the sum of squares of k n-bit numbers.\n",
"Compute the sum of squares of k n-bit binary numbers.\n",
"\n",
"Implements $U|a\\rangle|b\\rangle\\dots k\\rangle|0\\rangle \\rightarrow\n",
" |a\\rangle|b\\rangle\\dots|k\\rangle|a^2+b^2+\\dots k^2\\rangle$ using\n",
" $4 k n^2 T$ gates.\n",
"\n",
"The number of bits required by the output register is 2*bitsize + ceil(log2(k)).\n",
"\n",
"#### Parameters\n",
" - `bitsize`: Number of bits used to represent each of the k integers.\n",
" - `k`: The number of integers we want to square. \n",
"\n",
"#### Registers\n",
" - `input`: k n-bit registers.\n",
" - `result`: 2 * bitsize + 1 sized output register. \n",
" - `result`: 2 * bitsize + ceil(log2(k)) sized output register. \n",
"\n",
"#### References\n",
"[Fault-Tolerant Quantum Simulations of Chemistry in First Quantization](https://arxiv.org/abs/2105.12767) pg 80 give a Toffoli complexity for squaring.\n"
Expand Down Expand Up @@ -223,6 +225,142 @@
"bloq = GreaterThan(bitsize=4)\n",
"show_bloq(bloq)"
]
},
{
"cell_type": "markdown",
"id": "1c63d263",
"metadata": {
"cq.autogen": "_make_scale_int_by_real.md"
},
"source": [
"## `ScaleIntByReal`\n",
"Scale an integer by fixed-point representation of a real number.\n",
"\n",
"i.e.\n",
"\n",
"$$\n",
" |r\\rangle|i\\rangle|0\\rangle \\rightarrow |r\\rangle|i\\rangle|r \\times i\\rangle\n",
"$$\n",
"\n",
"The real number is assumed to be in the range [0, 1).\n",
"\n",
"#### Parameters\n",
" - `r_bitsize`: Number of bits used to represent the real number.\n",
" - `i_bitsize`: Number of bits used to represent the integer. \n",
"\n",
"#### Registers\n",
" - `- real_in`: r_bitsize-sized input register.\n",
" - `- int_in`: i_bitsize-sized input register.\n",
" - `- result`: r_bitsize output register \n",
"\n",
"#### References\n",
"[Compilation of Fault-Tolerant Quantum Heuristics for Combinatorial Optimization] (https://arxiv.org/pdf/2007.07391.pdf) pg 70.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4538d32b",
"metadata": {
"cq.autogen": "_make_scale_int_by_real.py"
},
"outputs": [],
"source": [
"from qualtran.bloqs.arithmetic import ScaleIntByReal\n",
"\n",
"bloq = ScaleIntByReal(r_bitsize=8, i_bitsize=12)\n",
"show_bloq(bloq)"
]
},
{
"cell_type": "markdown",
"id": "6247c0bd",
"metadata": {
"cq.autogen": "_make_multiply_two_reals.md"
},
"source": [
"## `MultiplyTwoReals`\n",
"Multiply two fixed-point representations of real numbers\n",
"\n",
"i.e.\n",
"\n",
"$$\n",
" |a\\rangle|b\\rangle|0\\rangle \\rightarrow |a\\rangle|b\\rangle|a \\times b\\rangle\n",
"$$\n",
"\n",
"The real numbers are assumed to be in the range [0, 1).\n",
"\n",
"#### Parameters\n",
" - `bitsize`: Number of bits used to represent the real number. \n",
"\n",
"#### Registers\n",
" - `- a`: bitsize-sized input register.\n",
" - `- b`: bitsize-sized input register.\n",
" - `- result`: bitsize output register \n",
"\n",
"#### References\n",
"[Compilation of Fault-Tolerant Quantum Heuristics for Combinatorial Optimization] (https://arxiv.org/pdf/2007.07391.pdf) pg 71.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5b8be106",
"metadata": {
"cq.autogen": "_make_multiply_two_reals.py"
},
"outputs": [],
"source": [
"from qualtran.bloqs.arithmetic import MultiplyTwoReals\n",
"\n",
"bloq = MultiplyTwoReals(bitsize=10)\n",
"show_bloq(bloq)"
]
},
{
"cell_type": "markdown",
"id": "7b0a73bb",
"metadata": {
"cq.autogen": "_make_square_real_number.md"
},
"source": [
"## `SquareRealNumber`\n",
"Square a fixed-point representation of a real number\n",
"\n",
"i.e.\n",
"\n",
"$$\n",
" |a\\rangle|0\\rangle \\rightarrow |a\\rangle|a^2\\rangle\n",
"$$\n",
"\n",
"The real numbers are assumed to be in the range [0, 1).\n",
"\n",
"#### Parameters\n",
" - `bitsize`: Number of bits used to represent the real number. \n",
"\n",
"#### Registers\n",
" - `- a`: bitsize-sized input register.\n",
" - `- b`: bitsize-sized input register.\n",
" - `- result`: bitsize output register \n",
"\n",
"#### References\n",
"[Compilation of Fault-Tolerant Quantum Heuristics for Combinatorial Optimization ](https://arxiv.org/pdf/2007.07391.pdf) pg 74.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ec6335d9",
"metadata": {
"cq.autogen": "_make_square_real_number.py"
},
"outputs": [],
"source": [
"from qualtran.bloqs.arithmetic import SquareRealNumber\n",
"\n",
"bloq = SquareRealNumber(bitsize=10)\n",
"show_bloq(bloq)"
]
}
],
"metadata": {
Expand All @@ -241,7 +379,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
"version": "3.9.6"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 005fa98

Please sign in to comment.