Skip to content

Commit

Permalink
Improve support for CAdd (#1277)
Browse files Browse the repository at this point in the history
  • Loading branch information
NoureldinYosri authored Aug 13, 2024
1 parent 28487f6 commit ec85731
Show file tree
Hide file tree
Showing 7 changed files with 303 additions and 163 deletions.
6 changes: 6 additions & 0 deletions dev_tools/autogenerate-bloqs-notebooks-v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import qualtran.bloqs.arithmetic.bitwise
import qualtran.bloqs.arithmetic.comparison
import qualtran.bloqs.arithmetic.controlled_add_or_subtract
import qualtran.bloqs.arithmetic.controlled_addition
import qualtran.bloqs.arithmetic.conversions
import qualtran.bloqs.arithmetic.multiplication
import qualtran.bloqs.arithmetic.negate
Expand Down Expand Up @@ -387,6 +388,11 @@
qualtran.bloqs.arithmetic.addition._ADD_K_DOC,
],
),
NotebookSpecV2(
title='Controlled Addition',
module=qualtran.bloqs.arithmetic.controlled_addition,
bloq_specs=[qualtran.bloqs.arithmetic.controlled_addition._CADD_DOC],
),
NotebookSpecV2(
title='Negation',
module=qualtran.bloqs.arithmetic.negate,
Expand Down
1 change: 1 addition & 0 deletions docs/bloqs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Bloqs Library
:caption: Arithmetic:

arithmetic/addition.ipynb
arithmetic/controlled_addition.ipynb
arithmetic/negate.ipynb
arithmetic/subtraction.ipynb
arithmetic/controlled_add_or_subtract.ipynb
Expand Down
1 change: 1 addition & 0 deletions qualtran/bloqs/arithmetic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
LessThanEqual,
SingleQubitCompare,
)
from qualtran.bloqs.arithmetic.controlled_addition import CAdd
from qualtran.bloqs.arithmetic.conversions import (
SignedIntegerToTwosComplement,
SignExtend,
Expand Down
163 changes: 163 additions & 0 deletions qualtran/bloqs/arithmetic/controlled_addition.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "064ac84d",
"metadata": {
"cq.autogen": "title_cell"
},
"source": [
"# Controlled Addition"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a283d3fb",
"metadata": {
"cq.autogen": "top_imports"
},
"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",
"import sympy\n",
"import cirq"
]
},
{
"cell_type": "markdown",
"id": "d85b6f41",
"metadata": {
"cq.autogen": "CAdd.bloq_doc.md"
},
"source": [
"## `CAdd`\n",
"An n-bit controlled-addition gate.\n",
"\n",
"#### Parameters\n",
" - `a_dtype`: Quantum datatype used to represent the integer a.\n",
" - `b_dtype`: Quantum datatype used to represent the integer b. Must be large enough to hold the result in the output register of a + b, or else it simply drops the most significant bits. If not specified, b_dtype is set to a_dtype.\n",
" - `cv`: When controlled=0, this bloq is active when the ctrl register is 0. When controlled=1, this bloq is active when the ctrl register is 1. \n",
"\n",
"#### Registers\n",
" - `ctrl`: the control bit for the addition\n",
" - `a`: A a_dtype.bitsize-sized input register (register a above).\n",
" - `b`: A b_dtype.bitsize-sized input/output register (register b above). \n",
"\n",
"#### References\n",
" - [Halving the cost of quantum addition](https://arxiv.org/abs/1709.06648). \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1e441ac2",
"metadata": {
"cq.autogen": "CAdd.bloq_doc.py"
},
"outputs": [],
"source": [
"from qualtran.bloqs.arithmetic import CAdd"
]
},
{
"cell_type": "markdown",
"id": "801cf1e1",
"metadata": {
"cq.autogen": "CAdd.example_instances.md"
},
"source": [
"### Example Instances"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "854ba572",
"metadata": {
"cq.autogen": "CAdd.cadd_small"
},
"outputs": [],
"source": [
"cadd_small = CAdd(QUInt(3))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0a4d89b2",
"metadata": {
"cq.autogen": "CAdd.cadd_large"
},
"outputs": [],
"source": [
"cadd_large = CAdd(QUInt(1000), QUInt(1000))"
]
},
{
"cell_type": "markdown",
"id": "25c1ed97",
"metadata": {
"cq.autogen": "CAdd.graphical_signature.md"
},
"source": [
"#### Graphical Signature"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "03b6c6a6",
"metadata": {
"cq.autogen": "CAdd.graphical_signature.py"
},
"outputs": [],
"source": [
"from qualtran.drawing import show_bloqs\n",
"show_bloqs([cadd_small, cadd_large],\n",
" ['`cadd_small`', '`cadd_large`'])"
]
},
{
"cell_type": "markdown",
"id": "3bd7842e",
"metadata": {
"cq.autogen": "CAdd.call_graph.md"
},
"source": [
"### Call Graph"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "80c50e8b",
"metadata": {
"cq.autogen": "CAdd.call_graph.py"
},
"outputs": [],
"source": [
"from qualtran.resource_counting.generalizers import ignore_split_join\n",
"cadd_small_g, cadd_small_sigma = cadd_small.call_graph(max_depth=1, generalizer=ignore_split_join)\n",
"show_call_graph(cadd_small_g)\n",
"show_counts_sigma(cadd_small_sigma)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit ec85731

Please sign in to comment.