Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
tanujkhattar authored Nov 25, 2024
2 parents 6970437 + 6c8e772 commit a8e4ccb
Show file tree
Hide file tree
Showing 117 changed files with 7,771 additions and 1,373 deletions.
31 changes: 26 additions & 5 deletions dev_tools/qualtran_dev_tools/notebook_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import qualtran.bloqs.block_encoding.phase
import qualtran.bloqs.block_encoding.product
import qualtran.bloqs.block_encoding.sparse_matrix
import qualtran.bloqs.block_encoding.sparse_matrix_hermitian
import qualtran.bloqs.block_encoding.tensor_product
import qualtran.bloqs.block_encoding.unitary
import qualtran.bloqs.bookkeeping
Expand Down Expand Up @@ -83,7 +84,7 @@
import qualtran.bloqs.data_loading.qrom_base
import qualtran.bloqs.data_loading.select_swap_qrom
import qualtran.bloqs.factoring.ecc
import qualtran.bloqs.factoring.mod_exp
import qualtran.bloqs.factoring.rsa
import qualtran.bloqs.gf_arithmetic.gf2_add_k
import qualtran.bloqs.gf_arithmetic.gf2_addition
import qualtran.bloqs.gf_arithmetic.gf2_inverse
Expand Down Expand Up @@ -435,6 +436,10 @@
qualtran.bloqs.arithmetic.comparison._SQ_CMP_DOC,
qualtran.bloqs.arithmetic.comparison._LEQ_DOC,
qualtran.bloqs.arithmetic.comparison._CLinearDepthGreaterThan_DOC,
qualtran.bloqs.arithmetic.comparison._LINEAR_DEPTH_HALF_GREATERTHAN_DOC,
qualtran.bloqs.arithmetic.comparison._LINEAR_DEPTH_HALF_GREATERTHANEQUAL_DOC,
qualtran.bloqs.arithmetic.comparison._LINEAR_DEPTH_HALF_LESSTHAN_DOC,
qualtran.bloqs.arithmetic.comparison._LINEAR_DEPTH_HALF_LESSTHANEQUAL_DOC,
],
),
NotebookSpecV2(
Expand Down Expand Up @@ -493,6 +498,8 @@
qualtran.bloqs.mod_arithmetic.mod_addition._MOD_ADD_DOC,
qualtran.bloqs.mod_arithmetic.mod_addition._MOD_ADD_K_DOC,
qualtran.bloqs.mod_arithmetic.mod_addition._C_MOD_ADD_DOC,
qualtran.bloqs.mod_arithmetic.mod_addition._C_MOD_ADD_K_DOC,
qualtran.bloqs.mod_arithmetic.mod_addition._CTRL_SCALE_MOD_ADD_DOC,
],
),
NotebookSpecV2(
Expand All @@ -515,10 +522,17 @@
],
),
NotebookSpecV2(
title='Modular Exponentiation',
module=qualtran.bloqs.factoring.mod_exp,
bloq_specs=[qualtran.bloqs.factoring.mod_exp._MODEXP_DOC],
directory=f'{SOURCE_DIR}/bloqs/factoring',
title='Modular Divison',
module=qualtran.bloqs.mod_arithmetic.mod_division,
bloq_specs=[qualtran.bloqs.mod_arithmetic.mod_division._KALISKI_MOD_INVERSE_DOC],
),
NotebookSpecV2(
title='Factoring RSA',
module=qualtran.bloqs.factoring.rsa,
bloq_specs=[
qualtran.bloqs.factoring.rsa.rsa_phase_estimate._RSA_PE_BLOQ_DOC,
qualtran.bloqs.factoring.rsa.rsa_mod_exp._RSA_MODEXP_DOC,
],
),
NotebookSpecV2(
title='Elliptic Curve Addition',
Expand Down Expand Up @@ -727,6 +741,13 @@
module=qualtran.bloqs.block_encoding.sparse_matrix,
bloq_specs=[qualtran.bloqs.block_encoding.sparse_matrix._SPARSE_MATRIX_DOC],
),
NotebookSpecV2(
title='Sparse Matrix Hermitian',
module=qualtran.bloqs.block_encoding.sparse_matrix_hermitian,
bloq_specs=[
qualtran.bloqs.block_encoding.sparse_matrix_hermitian._SPARSE_MATRIX_HERMITIAN_DOC
],
),
NotebookSpecV2(
title='Chebyshev Polynomial',
module=qualtran.bloqs.block_encoding.chebyshev_polynomial,
Expand Down
2 changes: 1 addition & 1 deletion dev_tools/qualtran_dev_tools/reference_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def apply_fixups(reporoot: Path):
output_dir = reporoot / 'docs/reference'
remove_extra_files(output_dir)

page_paths = output_dir.glob('qualtran/**/*.md')
page_paths = output_dir.glob('**/*.md')
for path in page_paths:
if fixup_all_symbols_page(path):
continue
Expand Down
102 changes: 73 additions & 29 deletions dev_tools/ui-export.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -45,28 +45,45 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import attrs\n",
"import hashlib\n",
"import json\n",
"\n",
"from qualtran import CompositeBloq\n",
"from qualtran import Adjoint, CompositeBloq, Controlled\n",
"from qualtran.bloqs.rotations.programmable_rotation_gate_array import ProgrammableRotationGateArray\n",
"\n",
"def bloq_attrs(bloq):\n",
" if isinstance(bloq, CompositeBloq):\n",
" return {}\n",
" if isinstance(bloq, ProgrammableRotationGateArray):\n",
" return {}\n",
" if isinstance(bloq, (Adjoint, Controlled)):\n",
" return bloq_attrs(bloq.subbloq)\n",
"\n",
" return attrs.asdict(bloq)\n",
"\n",
"def bloq_filename(bloq):\n",
" unhashed = json.dumps(bloq_attrs(bloq), cls=BloqEncoder)\n",
"\n",
" return hashlib.md5(unhashed.encode(), usedforsecurity=False).hexdigest() + '.json'"
" attrs_dict = bloq_attrs(bloq)\n",
" attrs_keys = list(attrs_dict.keys())\n",
" attrs_keys.sort()\n",
"\n",
" prefix = ''\n",
" if isinstance(bloq, Adjoint):\n",
" prefix = 'Adjoint_'\n",
" if isinstance(bloq, Controlled):\n",
" prefix = 'Controlled_'\n",
"\n",
" attrs_list = [\n",
" [key, attrs_dict[key]]\n",
" for key in attrs_keys\n",
" ] if attrs_keys else []\n",
" unhashed = json.dumps(attrs_list, cls=BloqEncoder)\n",
"\n",
" return prefix + hashlib.md5(unhashed.encode(), usedforsecurity=False).hexdigest() + '.json'"
]
},
{
Expand Down Expand Up @@ -108,60 +125,87 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import os\n",
"from pathlib import Path\n",
"\n",
"from qualtran_dev_tools.notebook_specs import NB_BY_SECTION\n",
"from qualtran_dev_tools.parse_docstrings import get_markdown_docstring\n",
"\n",
"for section in NB_BY_SECTION:\n",
" for notebook_spec in section[1]:\n",
" for bloq_spec in notebook_spec.bloq_specs:\n",
" Path(f'ui_export/{bloq_spec.bloq_cls.__name__}').mkdir(parents=True, exist_ok=True)\n",
"\n",
" doc_name = f'ui_export/{bloq_spec.bloq_cls.__name__}/docs.txt'\n",
" if not os.path.isfile(doc_name):\n",
" with open(doc_name, 'w') as doc_file:\n",
" doc_file.write('\\n'.join(get_markdown_docstring(bloq_spec.bloq_cls)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import os\n",
"from pathlib import Path\n",
"\n",
"from qualtran_dev_tools.all_call_graph import get_all_call_graph\n",
"from qualtran_dev_tools.notebook_specs import NB_BY_SECTION\n",
"from qualtran import Adjoint, Controlled\n",
"from qualtran.drawing.musical_score import get_musical_score_data\n",
"\n",
"examples = [\n",
" example\n",
" for section in NB_BY_SECTION\n",
" for notebook_spec in section[1]\n",
" for bloq_spec in notebook_spec.bloq_specs\n",
" for example in bloq_spec.examples\n",
"]\n",
"\n",
"call_graph = get_all_call_graph(examples)\n",
"\n",
"def bloq_score(bloq):\n",
" try:\n",
" return get_musical_score_data(bloq.decompose_bloq())\n",
" except:\n",
" return None\n",
"\n",
"def write_example(bloq):\n",
" call_graph, _ = bloq.call_graph(max_depth=1)\n",
"\n",
" for child_bloq, _ in call_graph.succ[bloq].items():\n",
" write_example(child_bloq)\n",
"def bloq_name(bloq):\n",
" if (isinstance(bloq, (Adjoint, Controlled))):\n",
" return bloq_name(bloq.subbloq)\n",
"\n",
" Path(f'ui_export/{bloq.__class__.__name__}').mkdir(parents=True, exist_ok=True)\n",
" return bloq.__class__.__name__\n",
"\n",
" doc_name = f'ui_export/{bloq.__class__.__name__}/docs.txt'\n",
" if not os.path.isfile(doc_name):\n",
" with open(doc_name, 'w') as doc_file:\n",
" doc_file.write('\\n'.join(get_markdown_docstring(bloq.__class__)))\n",
"\n",
" file_name = f'ui_export/{bloq.__class__.__name__}/{bloq_filename(bloq)}'\n",
"def write_example(bloq):\n",
" file_name = f'ui_export/{bloq_name(bloq)}/{bloq_filename(bloq)}'\n",
" if not os.path.isfile(file_name):\n",
" bloq_dict = {\n",
" 'name': bloq.__class__.__name__,\n",
" 'name': str(bloq),\n",
" 'attrs': bloq_attrs(bloq),\n",
" 'score': bloq_score(bloq),\n",
" 'msd': bloq_score(bloq),\n",
" 'callees': list(\n",
" {\n",
" 'name': child_bloq.__class__.__name__,\n",
" 'name': bloq_name(child_bloq),\n",
" 'filename': bloq_filename(child_bloq)\n",
" }\n",
" for child_bloq, _ in call_graph.succ[bloq].items()\n",
" for child_bloq in call_graph.neighbors(bloq)\n",
" )\n",
" }\n",
"\n",
" Path(f'ui_export/{bloq_name(bloq)}').mkdir(parents=True, exist_ok=True)\n",
"\n",
" with open(file_name, 'w') as f:\n",
" json.dump(bloq_dict, f, indent=2, cls=BloqEncoder)\n",
"\n",
"for section in NB_BY_SECTION:\n",
" for notebook_spec in section[1]:\n",
" for bloq_spec in notebook_spec.bloq_specs:\n",
" for example in bloq_spec.examples:\n",
" write_example(example.make())"
"for bloq, _ in call_graph.nodes.items():\n",
" write_example(bloq)"
]
}
],
Expand Down
4 changes: 3 additions & 1 deletion docs/bloqs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ Bloqs Library
mod_arithmetic/mod_addition.ipynb
mod_arithmetic/mod_subtraction.ipynb
mod_arithmetic/mod_multiplication.ipynb
factoring/mod_exp.ipynb
mod_arithmetic/mod_division.ipynb
factoring/rsa/rsa.ipynb
factoring/ecc/ec_add.ipynb
factoring/ecc/ecc.ipynb

Expand Down Expand Up @@ -129,6 +130,7 @@ Bloqs Library
block_encoding/phase.ipynb
block_encoding/linear_combination.ipynb
block_encoding/sparse_matrix.ipynb
block_encoding/sparse_matrix_hermitian.ipynb
block_encoding/chebyshev_polynomial.ipynb
block_encoding/lcu_block_encoding.ipynb

Expand Down
2 changes: 1 addition & 1 deletion docs/reference/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This section of the docs provides an API reference for all symbols
in ``qualtran``.

.. toctree::
:hidden:
:maxdepth: 1

qualtran.md

Expand Down
11 changes: 9 additions & 2 deletions qualtran/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# isort:skip_file

"""qualtran
"""The top-level Qualtran module.
isort:skip_file
Many fundamental objects for expressing quantum programs can be imported from this
top-level namespace like `qualtran.Bloq`, `qualtran.Register`, and the various quantum
data types (`qualtran.QBit`, `qualtran.QUInt`, ...).
The standard library of quantum algorithms must be imported from the `qualtran.bloqs` submodule.
A variety of analysis protocols are available in submodules as well like
`qualtran.resource_counting`, `qualtran.drawing`, `qualtran.simulation`, and others.
"""

# --------------------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion qualtran/_infra/Bloqs-Tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@
"metadata": {},
"outputs": [],
"source": [
"from qualtran.bloqs.factoring import ModExp\n",
"from qualtran.bloqs.factoring.rsa import ModExp\n",
"\n",
"mod_exp = ModExp(base=8, mod=13*17, exp_bitsize=3, x_bitsize=1024)\n",
"show_bloq(mod_exp)"
Expand Down
7 changes: 6 additions & 1 deletion qualtran/_infra/bloq.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,12 @@ def _my_add_controlled(
add_controlled: A function with the signature documented above that the system
can use to automatically wire up the new control registers.
"""
from qualtran import Controlled
from qualtran import Controlled, CtrlSpec
from qualtran.bloqs.mcmt.controlled_via_and import ControlledViaAnd

if ctrl_spec != CtrlSpec():
# reduce controls to a single qubit
return ControlledViaAnd.make_ctrl_system(self, ctrl_spec=ctrl_spec)

return Controlled.make_ctrl_system(self, ctrl_spec=ctrl_spec)

Expand Down
Loading

0 comments on commit a8e4ccb

Please sign in to comment.