├── qualtran ├── bloqs │ ├── chemistry │ │ ├── hubbard_model │ │ │ ├── __init__.py │ │ │ └── qubitization │ │ │ │ ├── walk_operator.py │ │ │ │ └── prepare_hubbard_test.py │ │ ├── __init__.py │ │ ├── df │ │ │ └── __init__.py │ │ ├── pbc │ │ │ ├── __init__.py │ │ │ └── first_quantization │ │ │ │ ├── prepare_zeta_test.py │ │ │ │ ├── prepare_test.py │ │ │ │ ├── select_t_test.py │ │ │ │ ├── projectile │ │ │ │ └── select_t_test.py │ │ │ │ └── select_uv_test.py │ │ ├── sf │ │ │ ├── __init__.py │ │ │ └── select_bloq_test.py │ │ ├── trotter │ │ │ ├── __init__.py │ │ │ ├── grid_ham │ │ │ │ ├── __init__.py │ │ │ │ ├── trotter_test.py │ │ │ │ ├── qvr_test.py │ │ │ │ └── kinetic_test.py │ │ │ ├── ising │ │ │ │ └── __init__.py │ │ │ └── hubbard │ │ │ │ ├── trotter_step_test.py │ │ │ │ ├── interaction_test.py │ │ │ │ └── hopping_test.py │ │ ├── quad_fermion │ │ │ └── __init__.py │ │ ├── ising │ │ │ └── __init__.py │ │ ├── thc │ │ │ ├── select_bloq_test.py │ │ │ └── __init__.py │ │ ├── sparse │ │ │ └── select_bloq_test.py │ │ └── black_boxes_test.py │ ├── qsp │ │ └── __init__.py │ ├── cryptography │ │ ├── __init__.py │ │ ├── ecc │ │ │ ├── ec_phase_estimate_r_test.py │ │ │ └── find_ecc_private_key_test.py │ │ └── rsa │ │ │ └── rsa_phase_estimate_test.py │ ├── mean_estimation │ │ └── __init__.py │ ├── optimization │ │ ├── __init__.py │ │ └── k_xor_sat │ │ │ ├── __init__.py │ │ │ └── kxor_instance_test.py │ ├── reflections │ │ ├── __init__.py │ │ └── prepare_identity_test.py │ ├── __init__.py │ ├── qubitization │ │ └── __init__.py │ ├── hamiltonian_simulation │ │ ├── __init__.py │ │ └── guided_hamiltonian_test.py │ ├── arithmetic │ │ ├── lists │ │ │ └── __init__.py │ │ ├── trigonometric │ │ │ ├── __init__.py │ │ │ └── arcsin_test.py │ │ └── conversions │ │ │ ├── __init__.py │ │ │ ├── contiguous_index_test.py │ │ │ └── ones_complement_to_twos_complement_test.py │ ├── qft │ │ ├── __init__.py │ │ └── two_bit_ffft_test.py │ ├── block_encoding │ │ ├── block_encoding_base_test.py │ │ └── __init__.py │ ├── for_testing │ │ ├── casting_test.py │ │ ├── __init__.py │ │ ├── many_registers_test.py │ │ ├── costing_test.py │ │ ├── interior_alloc_test.py │ │ ├── qubit_count_many_alloc_test.py │ │ ├── with_decomposition_test.py │ │ ├── matrix_gate_test.py │ │ ├── with_call_graph.py │ │ ├── with_call_graph_test.py │ │ └── interior_alloc.py │ ├── multiplexers │ │ └── __init__.py │ ├── bookkeeping │ │ ├── arbitrary_clifford_test.py │ │ ├── always_test.py │ │ ├── __init__.py │ │ ├── free_test.py │ │ └── allocate_test.py │ ├── gf_poly_arithmetic │ │ └── __init__.py │ ├── data_loading │ │ └── __init__.py │ ├── mod_arithmetic │ │ └── __init__.py │ ├── swap_network │ │ └── __init__.py │ ├── mcmt │ │ ├── __init__.py │ │ └── classically_controlled_test.py │ ├── phase_estimation │ │ ├── kaiser_window_state_test.py │ │ ├── __init__.py │ │ └── qpe_window_state_test.py │ ├── state_preparation │ │ ├── __init__.py │ │ └── black_box_prepare_test.py │ ├── gf_arithmetic │ │ ├── __init__.py │ │ ├── gf_utils.py │ │ └── gf2_add_k_test.py │ └── rotations │ │ └── __init__.py ├── third_party │ └── flamegraph │ │ └── METADATA ├── protos │ ├── __init__.py │ ├── annotations.proto │ ├── ec_point.proto │ ├── ctrl_spec.proto │ ├── args.proto │ ├── registers.proto │ ├── annotations_pb2.py │ ├── ctrl_spec_pb2.py │ ├── ec_point_pb2.py │ ├── args_pb2.py │ └── annotations_pb2.pyi ├── linalg │ ├── polynomial │ │ ├── __init__.py │ │ ├── basic_test.py │ │ └── basic.py │ ├── __init__.py │ ├── matrix_test.py │ ├── testing.py │ └── permutation_test.py ├── _version.py ├── serialization │ ├── __init__.py │ ├── annotations_test.py │ ├── annotations.py │ ├── ec_point_test.py │ ├── ctrl_spec_test.py │ ├── ec_point.py │ ├── args_test.py │ └── ctrl_spec.py ├── rotation_synthesis │ ├── relative_norm │ │ └── __init__.py │ ├── channels │ │ └── __init__.py │ ├── rings │ │ ├── _test_utils.py │ │ └── __init__.py │ ├── __init__.py │ ├── lattice │ │ └── __init__.py │ ├── _typing.py │ ├── matrix │ │ └── __init__.py │ └── protocols │ │ └── __init__.py ├── surface_code │ ├── physical_cost_model_test.py │ ├── msft_resource_estimator_interop_test.py │ ├── physical_cost_summary.py │ └── data_block_test.py ├── simulation │ ├── __init__.py │ └── tensor │ │ ├── _flattening_test.py │ │ └── __init__.py ├── _infra │ ├── __init__.py │ └── bloq_example_test.py ├── exception │ └── __init__.py ├── symbolics │ ├── types_test.py │ └── __init__.py ├── cirq_interop │ ├── decompose_protocol_test.py │ ├── __init__.py │ └── _interop_qubit_manager_test.py ├── resource_counting │ ├── _success_prob_test.py │ ├── _generalization.py │ └── __init__.py ├── drawing │ ├── musical_score.html │ ├── musical_score_test.py │ ├── classical_sim_graph_test.py │ └── __init__.py ├── qref_interop │ └── __init__.py └── dtype │ └── __init__.py ├── MANIFEST.in ├── dev_tools ├── requirements │ ├── deps │ │ ├── pip-tools.txt │ │ ├── format.txt │ │ ├── mypy.txt │ │ ├── dev-tools.txt │ │ ├── docs.txt │ │ ├── pylint.txt │ │ ├── packaging.txt │ │ ├── pytest.txt │ │ └── runtime.txt │ ├── Dockerfile │ ├── envs │ │ └── pip-tools.env.txt │ └── re-pip-compile-in-docker.sh ├── templates │ ├── root.jinja │ ├── type_alias.jinja │ ├── function.jinja │ └── page.jinja ├── conf │ ├── .coveragerc │ ├── .pylintrc │ └── mypy.ini ├── pyproject.toml ├── qualtran_dev_tools │ ├── make_reference_docs │ │ ├── _pages │ │ │ └── __init__.py │ │ ├── _components │ │ │ ├── __init__.py │ │ │ └── aliases.py │ │ └── __init__.py │ ├── __init__.py │ ├── git_tools.py │ └── all_call_graph_test.py ├── build-reference-docs.py ├── build-reference-docs-2.py ├── repomix │ └── README.md ├── clean-notebooks.py ├── README.md ├── execute-notebooks.py └── check-incremental-coverage-annotations.py ├── docs ├── _static │ ├── favicon.png │ ├── qualtran-logo-sm.png │ └── fixes.css ├── reference │ └── index.rst ├── .gitignore └── _templates │ ├── qai_logo.html │ └── qualtran-sidebar.html ├── .github ├── report-vulnerability-button.png └── problem-matchers │ ├── black.json │ ├── mypy.json │ ├── pylint.json │ └── README.md ├── .gitattributes ├── .readthedocs.yaml ├── check ├── autogenerate-notebooks ├── mypy ├── pylint ├── pytest ├── mypy-dev-tools └── pytest-dev-tools ├── pyproject.toml ├── SUPPORT.md └── .yamllint.yaml /qualtran/bloqs/chemistry/hubbard_model/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include dev_tools/requirements/deps/runtime.txt -------------------------------------------------------------------------------- /dev_tools/requirements/deps/pip-tools.txt: -------------------------------------------------------------------------------- 1 | pip-tools 2 | -------------------------------------------------------------------------------- /dev_tools/templates/root.jinja: -------------------------------------------------------------------------------- 1 | {{- self.content() -}} 2 | -------------------------------------------------------------------------------- /dev_tools/requirements/deps/format.txt: -------------------------------------------------------------------------------- 1 | flynt~=0.60 2 | black~=24.8.0 3 | isort~=5.10.1 4 | -------------------------------------------------------------------------------- /docs/_static/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/Qualtran/HEAD/docs/_static/favicon.png -------------------------------------------------------------------------------- /dev_tools/requirements/deps/mypy.txt: -------------------------------------------------------------------------------- 1 | # the mypy dependency file 2 | mypy~=1.14 3 | mypy-protobuf 4 | sympy 5 | -------------------------------------------------------------------------------- /docs/_static/qualtran-logo-sm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/Qualtran/HEAD/docs/_static/qualtran-logo-sm.png -------------------------------------------------------------------------------- /.github/report-vulnerability-button.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/Qualtran/HEAD/.github/report-vulnerability-button.png -------------------------------------------------------------------------------- /dev_tools/requirements/deps/dev-tools.txt: -------------------------------------------------------------------------------- 1 | -r pip-tools.txt 2 | -r mypy.txt 3 | -r pytest.txt 4 | -r format.txt 5 | -r pylint.txt 6 | -r docs.txt 7 | -r packaging.txt 8 | 9 | notebook 10 | -------------------------------------------------------------------------------- /dev_tools/requirements/deps/docs.txt: -------------------------------------------------------------------------------- 1 | ipywidgets 2 | nbconvert 3 | nbformat 4 | sphinx 5 | sphinx-autobuild 6 | pydata-sphinx-theme 7 | myst-nb 8 | tensorflow-docs~=2023.5.24 9 | 10 | griffe 11 | -------------------------------------------------------------------------------- /dev_tools/conf/.coveragerc: -------------------------------------------------------------------------------- 1 | [run] 2 | # Omit files outside the current working directory. 3 | # Note: this means coverage must be run from the cirq repo root. 4 | # Failure to do so will result in false positives. 5 | include = ./* 6 | -------------------------------------------------------------------------------- /docs/reference/index.rst: -------------------------------------------------------------------------------- 1 | .. _reference: 2 | 3 | API Reference 4 | ============= 5 | 6 | This section of the docs provides an API reference for all symbols 7 | in ``qualtran``. 8 | 9 | 10 | .. include:: autotoc.rst.inc 11 | -------------------------------------------------------------------------------- /dev_tools/requirements/deps/pylint.txt: -------------------------------------------------------------------------------- 1 | pylint~=3.3.1 2 | 3 | # for checking _test.py files 4 | pytest 5 | openfermion[resources] 6 | 7 | # dev tools 8 | tensorflow-docs 9 | sphinx 10 | filelock 11 | griffe 12 | mdit-py-plugins 13 | -------------------------------------------------------------------------------- /docs/.gitignore: -------------------------------------------------------------------------------- 1 | # Sphinx (stage 3) generation 2 | _build/ 3 | 4 | # Generated reference toc 5 | reference/autotoc.rst.inc 6 | 7 | # All the generated reference pages 8 | **/*.md 9 | 10 | # All the generated notebooks 11 | **/*.ipynb 12 | -------------------------------------------------------------------------------- /dev_tools/requirements/deps/packaging.txt: -------------------------------------------------------------------------------- 1 | # for testing packaging in isolation 2 | virtualenv 3 | 4 | # for building protos 5 | grpcio-tools 6 | 7 | # for creating packages 8 | setuptools 9 | wheel 10 | 11 | # for uploading packages to pypi 12 | twine 13 | -------------------------------------------------------------------------------- /docs/_templates/qai_logo.html: -------------------------------------------------------------------------------- 1 | 2 | Google Quantum AI 4 | 5 | -------------------------------------------------------------------------------- /dev_tools/requirements/deps/pytest.txt: -------------------------------------------------------------------------------- 1 | # dependencies to run pytest 2 | pytest 3 | pytest-asyncio 4 | pytest-cov 5 | pytest-xdist 6 | 7 | # test executing notebooks 8 | ipykernel 9 | filelock 10 | 11 | # chemistry resource estimates 12 | openfermion[resources] 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Mark generated protobuf files to be collapsed by default in PRs 2 | # See: https://docs.github.com/en/repositories/working-with-files/managing-files/customizing-how-changed-files-appear-on-github 3 | *_pb2.py linguist-generated=true 4 | *_pb2.pyi linguist-generated=true 5 | -------------------------------------------------------------------------------- /.github/problem-matchers/black.json: -------------------------------------------------------------------------------- 1 | { 2 | "problemMatcher": [ 3 | { 4 | "owner": "black", 5 | "severity": "error", 6 | "pattern": [ 7 | { 8 | "regexp": "^(would reformat) (.+)$", 9 | "file": 2, 10 | "message": 1 11 | } 12 | ] 13 | } 14 | ] 15 | } 16 | -------------------------------------------------------------------------------- /dev_tools/pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = ["setuptools>=61.0"] 3 | build-backend = "setuptools.build_meta" 4 | 5 | [project] 6 | name = "qualtran-dev-tools" 7 | version = "0.1.0" 8 | description = "Local development tools for the Qualtran project." 9 | requires-python = ">=3.10" 10 | 11 | [tool.setuptools] 12 | packages = ["qualtran_dev_tools"] 13 | -------------------------------------------------------------------------------- /.github/problem-matchers/mypy.json: -------------------------------------------------------------------------------- 1 | { 2 | "problemMatcher": [ 3 | { 4 | "owner": "mypy", 5 | "pattern": [ 6 | { 7 | "regexp": "^(.+):(\\d+):\\s(error|warning):\\s(.+)$", 8 | "file": 1, 9 | "line": 2, 10 | "severity": 3, 11 | "message": 4 12 | } 13 | ] 14 | } 15 | ] 16 | } 17 | -------------------------------------------------------------------------------- /qualtran/third_party/flamegraph/METADATA: -------------------------------------------------------------------------------- 1 | name: "Flamegraph" 2 | description: 3 | "The script allows plotting flamegraphs, useful to visualize T-costs for Bloqs." 4 | 5 | third_party { 6 | url { 7 | type: HOMEPAGE 8 | value: "https://github.com/brendangregg/FlameGraph" 9 | } 10 | last_upgrade_date { year: 2023 month: 10 day: 07 } 11 | license_type: CDDL1.0 12 | } 13 | -------------------------------------------------------------------------------- /.readthedocs.yaml: -------------------------------------------------------------------------------- 1 | # .readthedocs.yaml 2 | # Read the Docs configuration file 3 | # See https://docs.readthedocs.io/en/stable/config-file/v2.html for details 4 | 5 | # Required 6 | version: 2 7 | 8 | # Set the OS, Python version and other tools you might need 9 | build: 10 | os: ubuntu-22.04 11 | tools: 12 | python: "3.10" 13 | 14 | # Build documentation in the "docs/" directory with Sphinx 15 | sphinx: 16 | configuration: docs/conf.py 17 | 18 | python: 19 | install: 20 | - requirements: dev_tools/requirements/deps/docs.txt 21 | -------------------------------------------------------------------------------- /docs/_static/fixes.css: -------------------------------------------------------------------------------- 1 | 2 | /* Args etc tables 3 | - increase width so it isn't centered in the middle of the page 4 | */ 5 | table.fixed.responsive { 6 | width: 100%; 7 | } 8 | 9 | /* "View on GitHub" 10 | - increase width so it is the only thing on this line 11 | - add some margin beneath it to let things breathe 12 | */ 13 | table.tfo-notebook-buttons.tfo-api { 14 | width: 100%; 15 | margin-bottom: 1.5em; 16 | } 17 | 18 | /* Adjust size of GitHub icon in the nav bar. 19 | */ 20 | html .pst-navbar-icon { 21 | font-size: 1.5rem; 22 | } 23 | -------------------------------------------------------------------------------- /check/autogenerate-notebooks: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ################################################################################ 4 | # Autogenerates the notebooks and reports whether there were any changes. 5 | # 6 | # Usage: 7 | # check/autogenerate-notebooks 8 | # 9 | ################################################################################ 10 | 11 | python dev_tools/autogenerate-bloqs-notebooks-v2.py || exit $? 12 | 13 | if output=$(git status --porcelain) && [ -z "$output" ]; then 14 | # Working directory clean 15 | exit 0 16 | else 17 | # Uncommitted changes 18 | echo $output 19 | exit 1 20 | fi 21 | -------------------------------------------------------------------------------- /qualtran/protos/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /qualtran/bloqs/qsp/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/df/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/pbc/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/sf/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /qualtran/bloqs/cryptography/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /qualtran/bloqs/mean_estimation/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /qualtran/bloqs/optimization/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2025 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /qualtran/bloqs/reflections/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /qualtran/linalg/polynomial/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/trotter/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/quad_fermion/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /docs/_templates/qualtran-sidebar.html: -------------------------------------------------------------------------------- 1 | {# Displays the TOC-subtree for pages nested under the currently active top-level TOCtree element. #} 2 | 17 | -------------------------------------------------------------------------------- /qualtran/_version.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | __version__ = "0.7.0.dev0" 16 | -------------------------------------------------------------------------------- /dev_tools/qualtran_dev_tools/make_reference_docs/_pages/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /dev_tools/qualtran_dev_tools/make_reference_docs/_components/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /qualtran/bloqs/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """The standard library of quantum subroutines.""" 16 | -------------------------------------------------------------------------------- /qualtran/linalg/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """Linear algebra routines for building bloqs.""" 16 | -------------------------------------------------------------------------------- /qualtran/serialization/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """Functions for protobuf serialization of bloqs.""" 16 | -------------------------------------------------------------------------------- /check/mypy: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ################################################################################ 4 | # Runs mypy on the repository using a preconfigured mypy.ini file. 5 | # 6 | # Usage: 7 | # check/mypy [--flags] 8 | ################################################################################ 9 | 10 | # Get the working directory to the repo root. 11 | thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? 12 | topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? 13 | cd "${topdir}" || exit $? 14 | 15 | CONFIG_FILE='mypy.ini' 16 | 17 | echo -e -n "\033[31m" 18 | mypy --config-file=dev_tools/conf/$CONFIG_FILE "$@" qualtran/ 19 | result=$? 20 | echo -e -n "\033[0m" 21 | 22 | exit ${result} 23 | -------------------------------------------------------------------------------- /dev_tools/qualtran_dev_tools/make_reference_docs/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from ._make import make_reference_docs 16 | -------------------------------------------------------------------------------- /qualtran/bloqs/qubitization/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from .qubitization_walk_operator import QubitizationWalkOperator 16 | -------------------------------------------------------------------------------- /dev_tools/templates/type_alias.jinja: -------------------------------------------------------------------------------- 1 | {% extends "templates/page.jinja" %} 2 | 3 | {#----------------------------------------------------------------------------#} 4 | 5 | {% block header %} 6 | # {{page_info.short_name}} 7 | 8 | {% endblock header%} 9 | 10 | {#----------------------------------------------------------------------------#} 11 | 12 | {% block metadata %} 13 | This symbol is a **type alias**. 14 | 15 | {{ page_info.doc.brief }} 16 | 17 | {% endblock metadata %} 18 | 19 | {#----------------------------------------------------------------------------#} 20 | 21 | {% block body %} 22 | {% if page_info.signature %} 23 | #### Source: 24 | 25 | {{ builder.build_signature() }} 26 | {% endif %} 27 | {{ super() -}} 28 | {% endblock %} 29 | -------------------------------------------------------------------------------- /check/pylint: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ################################################################################ 4 | # Runs pylint on the repository using a preconfigured .pylintrc file. 5 | # 6 | # Usage: 7 | # check/pylint [--flags for pylint] 8 | ################################################################################ 9 | 10 | # Get the working directory to the repo root. 11 | thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? 12 | topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? 13 | cd "${topdir}" || exit $? 14 | 15 | # Add dev_tools to $PYTHONPATH so that pylint can find custom checkers 16 | PYTHONPATH=dev_tools:dev_tools/qualtran_dev_tools pylint --jobs=0 --rcfile=dev_tools/conf/.pylintrc "$@" qualtran/ dev_tools/ 17 | -------------------------------------------------------------------------------- /qualtran/bloqs/hamiltonian_simulation/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from .guided_hamiltonian import GuidedHamiltonian, GuidedHamiltonianPhaseEstimation 15 | -------------------------------------------------------------------------------- /check/pytest: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ################################################################################ 4 | # Runs pytest on the repository. 5 | # 6 | # Usage: 7 | # check/pytest [--flags for pytest] 8 | # 9 | ################################################################################ 10 | 11 | # Get the working directory to the repo root. 12 | thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? 13 | topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? 14 | cd "${topdir}" || exit $? 15 | 16 | # Run in parallel by default. Pass the `-n0` option for a single-process run. 17 | # (the last `-n` option wins) 18 | PYTEST_ARGS=( "-n=auto" "$@" ) 19 | 20 | pytest "${PYTEST_ARGS[@]}" qualtran/ 21 | RESULT=$? 22 | 23 | exit "$RESULT" 24 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/ising/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from .hamiltonian import get_1d_ising_hamiltonian, get_1d_ising_lcu_coeffs, get_1d_ising_pauli_terms 16 | -------------------------------------------------------------------------------- /check/mypy-dev-tools: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ################################################################################ 4 | # Runs mypy on the dev_tools/ folder using a preconfigured mypy.ini file. 5 | # 6 | # Usage: 7 | # check/mypy-dev-tool [--flags] 8 | ################################################################################ 9 | 10 | # Get the working directory to the repo root. 11 | thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? 12 | topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? 13 | cd "${topdir}" || exit $? 14 | 15 | CONFIG_FILE='mypy.ini' 16 | 17 | echo -e -n "\033[31m" 18 | mypy --config-file=dev_tools/conf/$CONFIG_FILE --no-namespace-packages "$@" dev_tools/ 19 | result=$? 20 | echo -e -n "\033[0m" 21 | 22 | exit ${result} 23 | -------------------------------------------------------------------------------- /qualtran/rotation_synthesis/relative_norm/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2025 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from qualtran.rotation_synthesis.relative_norm._clifford_t import CliffordTRelativeNormSolver 16 | -------------------------------------------------------------------------------- /.github/problem-matchers/pylint.json: -------------------------------------------------------------------------------- 1 | { 2 | "problemMatcher": [ 3 | { 4 | "owner": "pylint-error", 5 | "severity": "error", 6 | "pattern": [ 7 | { 8 | "regexp": "^(.+):(\\d+):(\\d+):\\s(([EF]\\d{4}):\\s.+)$", 9 | "file": 1, 10 | "line": 2, 11 | "column": 3, 12 | "message": 4, 13 | "code": 5 14 | } 15 | ] 16 | }, 17 | { 18 | "owner": "pylint-warning", 19 | "severity": "warning", 20 | "pattern": [ 21 | { 22 | "regexp": "^(.+):(\\d+):(\\d+):\\s(([CRW]\\d{4}):\\s.+)$", 23 | "file": 1, 24 | "line": 2, 25 | "column": 3, 26 | "message": 4, 27 | "code": 5 28 | } 29 | ] 30 | } 31 | ] 32 | } 33 | -------------------------------------------------------------------------------- /check/pytest-dev-tools: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ################################################################################ 4 | # Runs pytest on the dev_tools/ folder. 5 | # 6 | # Usage: 7 | # check/pytest-dev-tools [--flags for pytest] 8 | # 9 | ################################################################################ 10 | 11 | # Get the working directory to the repo root. 12 | thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? 13 | topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? 14 | cd "${topdir}" || exit $? 15 | 16 | # Run in parallel by default. Pass the `-n0` option for a single-process run. 17 | # (the last `-n` option wins) 18 | PYTEST_ARGS=( "-n=auto" "$@" ) 19 | 20 | pytest "${PYTEST_ARGS[@]}" dev_tools/ 21 | RESULT=$? 22 | 23 | exit "$RESULT" 24 | -------------------------------------------------------------------------------- /dev_tools/build-reference-docs.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from qualtran_dev_tools.reference_docs import build_reference_docs 16 | 17 | if __name__ == '__main__': 18 | build_reference_docs() 19 | -------------------------------------------------------------------------------- /qualtran/bloqs/arithmetic/lists/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from .has_duplicates import HasDuplicates 15 | from .sort_in_place import SortInPlace 16 | from .symmetric_difference import SymmetricDifference 17 | -------------------------------------------------------------------------------- /qualtran/bloqs/arithmetic/trigonometric/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from qualtran.bloqs.arithmetic.trigonometric.arcsin import ArcSin 15 | from qualtran.bloqs.arithmetic.trigonometric.arctan import ArcTan 16 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/sf/select_bloq_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from qualtran.bloqs.chemistry.sf.select_bloq import _select 15 | 16 | 17 | def test_select(bloq_autotester): 18 | bloq_autotester(_select) 19 | -------------------------------------------------------------------------------- /qualtran/protos/annotations.proto: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2023 Google LLC 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | https://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | syntax = "proto3"; 18 | 19 | package qualtran; 20 | 21 | message TComplexity { 22 | int64 clifford = 1; 23 | int64 rotations = 2; 24 | int64 t = 3; 25 | } -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/thc/select_bloq_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from qualtran.bloqs.chemistry.thc.select_bloq import _thc_sel 16 | 17 | 18 | def test_thc_uniform_prep(bloq_autotester): 19 | bloq_autotester(_thc_sel) 20 | -------------------------------------------------------------------------------- /qualtran/bloqs/qft/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from .approximate_qft import ApproximateQFT 15 | from .qft_phase_gradient import QFTPhaseGradient 16 | from .qft_text_book import QFTTextBook 17 | from .two_bit_ffft import TwoBitFFFT 18 | -------------------------------------------------------------------------------- /qualtran/rotation_synthesis/channels/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2025 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from qualtran.rotation_synthesis.channels._channel import ( 16 | Channel, 17 | ProbabilisticChannel, 18 | ProjectiveChannel, 19 | UnitaryChannel, 20 | ) 21 | -------------------------------------------------------------------------------- /dev_tools/templates/function.jinja: -------------------------------------------------------------------------------- 1 | {% extends "templates/page.jinja" %} 2 | 3 | {#----------------------------------------------------------------------------#} 4 | 5 | {% block header %} 6 | # {{page_info.short_name}} 7 | 8 | {% endblock header%} 9 | 10 | {#----------------------------------------------------------------------------#} 11 | 12 | {% block metadata %} 13 | {{ builder.top_source_link() }} 14 | 15 | {{ page_info.doc.brief }} 16 | 17 | {% if page_info.header %} 18 | 19 | 20 | {{ page_info.header -}} 21 | {% endif %} 22 | 23 | {{ builder.build_collapsable_aliases() -}} 24 | {% endblock metadata %} 25 | 26 | {#----------------------------------------------------------------------------#} 27 | 28 | {% block body %} 29 | {% if page_info.signature %} 30 | {{ builder.build_signature() }} 31 | {% endif %} 32 | {{ super() -}} 33 | {% endblock %} 34 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/trotter/grid_ham/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Bloqs for Trotter simulation of the real space grid Hamiltonian.""" 15 | 16 | from .kinetic import KineticEnergy 17 | from .potential import PairPotential, PotentialEnergy 18 | -------------------------------------------------------------------------------- /qualtran/rotation_synthesis/rings/_test_utils.py: -------------------------------------------------------------------------------- 1 | # Copyright 2025 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # fmt: off 16 | PRIMES_LESS_THAN_100 = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,\ 17 | 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] 18 | # fmt: on 19 | -------------------------------------------------------------------------------- /qualtran/bloqs/arithmetic/conversions/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from .contiguous_index import ToContiguousIndex 15 | from .ones_complement_to_twos_complement import SignedIntegerToTwosComplement 16 | from .sign_extension import SignExtend, SignTruncate 17 | -------------------------------------------------------------------------------- /qualtran/surface_code/physical_cost_model_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import pytest 15 | 16 | import qualtran.testing as qlt_testing 17 | 18 | 19 | @pytest.mark.notebook 20 | def test_notebook(): 21 | qlt_testing.execute_notebook('physical_cost_model') 22 | -------------------------------------------------------------------------------- /dev_tools/requirements/deps/runtime.txt: -------------------------------------------------------------------------------- 1 | # core dependencies 2 | attrs>=23.2.0 3 | cachetools>=5.3 4 | networkx 5 | numpy>=1.26,<3.0 6 | sympy 7 | cirq-core~=1.4 8 | fxpmath 9 | galois 10 | 11 | # qualtran/testing.py 12 | nbconvert 13 | nbformat 14 | 15 | # drawing 16 | notebook 17 | pydot 18 | ipython 19 | matplotlib 20 | ipywidgets 21 | plotly 22 | 23 | # ui 24 | dash 25 | 26 | # quimb simulation 27 | quimb 28 | 29 | # qsharp resource estimator interop 30 | qsharp 31 | qsharp-widgets 32 | 33 | # qref bartiq interop 34 | qref==0.11.0 35 | bartiq==0.12.1 36 | 37 | # pyzx interop 38 | pyzx 39 | 40 | # Pennylane interop 41 | pennylane 42 | 43 | # serialization 44 | protobuf 45 | 46 | # typing 47 | typing_extensions>=4.10.0 48 | 49 | # rotation_synthesis 50 | mpmath 51 | 52 | # Note: use `pipreqs` to generate a list of dependencies based on the imports in our files. 53 | -------------------------------------------------------------------------------- /qualtran/bloqs/block_encoding/block_encoding_base_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import pytest 16 | 17 | from qualtran.testing import execute_notebook 18 | 19 | 20 | @pytest.mark.notebook 21 | def test_notebook(): 22 | execute_notebook('block_encoding') 23 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/pbc/first_quantization/prepare_zeta_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from qualtran.bloqs.chemistry.pbc.first_quantization.prepare_zeta import _prepare_zeta 15 | 16 | 17 | def test_prepare_zeta(bloq_autotester): 18 | bloq_autotester(_prepare_zeta) 19 | -------------------------------------------------------------------------------- /qualtran/rotation_synthesis/rings/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2025 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """A sub-package representing the rings used in rotation synthesis.""" 16 | 17 | from qualtran.rotation_synthesis.rings._zsqrt2 import ZSqrt2 18 | from qualtran.rotation_synthesis.rings._zw import ZW 19 | -------------------------------------------------------------------------------- /dev_tools/build-reference-docs-2.py: -------------------------------------------------------------------------------- 1 | # Copyright 2025 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from qualtran_dev_tools.git_tools import get_git_root 16 | from qualtran_dev_tools.make_reference_docs import make_reference_docs 17 | 18 | if __name__ == "__main__": 19 | make_reference_docs(get_git_root()) 20 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/thc/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """SELECT and PREPARE for the second quantized Tensor Hypercontracted (THC) chemistry Hamtiltonian. 15 | """ 16 | 17 | from .prepare import PrepareTHC, UniformSuperpositionTHC 18 | from .select_bloq import SelectTHC 19 | -------------------------------------------------------------------------------- /qualtran/protos/ec_point.proto: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2024 Google LLC 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | https://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | */ 13 | 14 | syntax = "proto3"; 15 | 16 | import "qualtran/protos/args.proto"; 17 | 18 | package qualtran; 19 | 20 | message ECPoint { 21 | IntOrSympy x = 1; 22 | IntOrSympy y = 2; 23 | IntOrSympy mod = 3; 24 | optional IntOrSympy curve_a = 4; 25 | } 26 | -------------------------------------------------------------------------------- /dev_tools/qualtran_dev_tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """Python functionality to support development of Qualtran. 16 | 17 | Modules in this package should provide library functionality (not scripts). Scripts 18 | can import this functionality from the parent (dev_tools/) directory. 19 | """ 20 | -------------------------------------------------------------------------------- /qualtran/bloqs/for_testing/casting_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import qualtran.testing as qlt_testing 16 | from qualtran.bloqs.for_testing.casting import TestCastToFrom 17 | 18 | 19 | def test_casting(): 20 | cast = TestCastToFrom() 21 | qlt_testing.assert_valid_bloq_decomposition(cast) 22 | -------------------------------------------------------------------------------- /qualtran/protos/ctrl_spec.proto: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2024 Google LLC 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | https://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | syntax = "proto3"; 18 | 19 | import "qualtran/protos/data_types.proto"; 20 | import "qualtran/protos/args.proto"; 21 | 22 | package qualtran; 23 | 24 | message CtrlSpec { 25 | repeated QDataType qdtypes = 1; 26 | repeated NDArray cvs = 2; 27 | } 28 | -------------------------------------------------------------------------------- /qualtran/simulation/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """Simulators for quantum programs. 16 | 17 | This module includes `qualtran.simulation.classical_sim` for basis state simulation of 18 | classical-reversible bloqs and `qualtran.simulation.tensor` for Quimb-based tensor network 19 | contraction. 20 | """ 21 | -------------------------------------------------------------------------------- /qualtran/surface_code/msft_resource_estimator_interop_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import pytest 15 | 16 | import qualtran.testing as qlt_testing 17 | 18 | 19 | @pytest.mark.notebook 20 | def test_msft_resource_estimator_interop_notebook(): 21 | qlt_testing.execute_notebook('msft_resource_estimator_interop') 22 | -------------------------------------------------------------------------------- /qualtran/bloqs/multiplexers/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | r"""Multiplexed Bloqs that can be used to apply a coherent "for-loop" via unary iteration. 16 | 17 | Unary iteration can be used to implement unitaries of the form 18 | 19 | $$ 20 | U = \sum_{l=0}^{2^n-1} U_{l}|l\rangle \langle l| 21 | $$ 22 | """ 23 | -------------------------------------------------------------------------------- /qualtran/bloqs/bookkeeping/arbitrary_clifford_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import subprocess 16 | 17 | import pytest 18 | 19 | 20 | @pytest.mark.slow 21 | def test_no_circular_import(): 22 | subprocess.check_call( 23 | ['python', '-c', 'from qualtran.bloqs.bookkeeping import arbitrary_clifford'] 24 | ) 25 | -------------------------------------------------------------------------------- /qualtran/bloqs/for_testing/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from .atom import TestAtom, TestTwoBitOp 16 | from .casting import TestCastToFrom 17 | from .many_registers import TestMultiRegister 18 | from .with_call_graph import TestBloqWithCallGraph 19 | from .with_decomposition import TestParallelCombo, TestSerialCombo 20 | -------------------------------------------------------------------------------- /qualtran/bloqs/gf_poly_arithmetic/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2025 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | 16 | from qualtran.bloqs.gf_poly_arithmetic.gf2_poly_add import GF2PolyAdd 17 | from qualtran.bloqs.gf_poly_arithmetic.gf2_poly_add_k import GF2PolyAddK 18 | from qualtran.bloqs.gf_poly_arithmetic.gf_poly_split_and_join import GFPolyJoin, GFPolySplit 19 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/pbc/first_quantization/prepare_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from qualtran.bloqs.chemistry.pbc.first_quantization.prepare import ( 15 | UniformSuperpostionIJFirstQuantization, 16 | ) 17 | 18 | 19 | def test_uniform_super_position_ij(): 20 | prep = UniformSuperpostionIJFirstQuantization(eta=10, num_bits_rot_aa=8) 21 | -------------------------------------------------------------------------------- /qualtran/bloqs/data_loading/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """Bloqs to load classical data in a quantum register""" 16 | 17 | from qualtran.bloqs.data_loading.qroam_clean import QROAMClean, QROAMCleanAdjoint 18 | from qualtran.bloqs.data_loading.qrom import QROM 19 | from qualtran.bloqs.data_loading.select_swap_qrom import SelectSwapQROM 20 | -------------------------------------------------------------------------------- /qualtran/bloqs/cryptography/ecc/ec_phase_estimate_r_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from qualtran.bloqs.cryptography.ecc.ec_phase_estimate_r import _ec_pe, _ec_pe_small 16 | 17 | 18 | def test_ec_pe(bloq_autotester): 19 | bloq_autotester(_ec_pe) 20 | 21 | 22 | def test_ec_pe_small(bloq_autotester): 23 | bloq_autotester(_ec_pe_small) 24 | -------------------------------------------------------------------------------- /qualtran/bloqs/mod_arithmetic/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from .mod_addition import CModAdd, CModAddK, CtrlScaleModAdd, ModAdd, ModAddK 16 | from .mod_division import KaliskiModInverse 17 | from .mod_multiplication import CModMulK, DirtyOutOfPlaceMontgomeryModMul, ModDbl 18 | from .mod_subtraction import CModNeg, CModSub, ModNeg, ModSub 19 | -------------------------------------------------------------------------------- /qualtran/bloqs/swap_network/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Functionality for moving data between registers (swapping).""" 15 | 16 | from qualtran.bloqs.swap_network.cswap_approx import CSwapApprox 17 | from qualtran.bloqs.swap_network.multiplexed_cswap import MultiplexedCSwap 18 | from qualtran.bloqs.swap_network.swap_with_zero import SwapWithZero 19 | -------------------------------------------------------------------------------- /qualtran/_infra/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # ------------------------------------------------------------------------------ 16 | # Note to intrepid developers: everything under _infra/ is re-exported in the 17 | # top-level qualtran/__init__.py 18 | # ------------------------------------------------------------------------------ 19 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [tool.black] 2 | line-length = 100 3 | target_version = ['py39', 'py310', 'py311', 'py312'] 4 | skip-string-normalization = true 5 | skip-magic-trailing-comma = true 6 | exclude = "qualtran/protos/*" 7 | 8 | [tool.isort] 9 | profile = 'black' 10 | order_by_type = false 11 | line_length = 100 12 | remove_redundant_aliases = true 13 | skip_glob = ["qualtran/protos/*"] 14 | 15 | [tool.pytest.ini_options] 16 | filterwarnings = [ 17 | 'ignore::DeprecationWarning:quimb.linalg.approx_spectral:', 18 | 'ignore:.*standard platformdirs.*:DeprecationWarning:jupyter_client.*' 19 | ] 20 | # we define classes like TestBloq etc. which pytest tries to collect, 21 | # so if you really want to collect a class as a test use '*TestSuite'. 22 | # (but please stick to the convention of test_* functions) 23 | python_classes = '*TestSuite' 24 | addopts = '--strict-markers' 25 | markers = [ 26 | "slow: slow tests.", 27 | "notebook: unit tests that execute a notebook." 28 | ] 29 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/trotter/grid_ham/trotter_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import pytest 15 | 16 | import qualtran.testing as qlt_testing 17 | 18 | 19 | @pytest.mark.notebook 20 | def test_costing_notebook(): 21 | qlt_testing.execute_notebook('trotter_costs') 22 | 23 | 24 | @pytest.mark.notebook 25 | def test_notebook(): 26 | qlt_testing.execute_notebook('trotter') 27 | -------------------------------------------------------------------------------- /dev_tools/requirements/Dockerfile: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | FROM python:3.10 16 | 17 | WORKDIR /pip-compile 18 | 19 | # Step 0: install pip-tools 20 | COPY envs/pip-tools.env.txt ./ 21 | RUN pip install -r pip-tools.env.txt 22 | 23 | # Step 1: compile a complete & consistent environment with all dependencies 24 | COPY deps/ ./deps/ 25 | COPY re-pip-compile.sh ./ 26 | RUN bash re-pip-compile.sh -------------------------------------------------------------------------------- /qualtran/simulation/tensor/_flattening_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from qualtran.bloqs.basic_gates import CNOT 15 | from qualtran.bloqs.mcmt.and_bloq import MultiAnd 16 | from qualtran.simulation.tensor import bloq_has_custom_tensors 17 | 18 | 19 | def test_bloq_has_custom_tensors(): 20 | assert bloq_has_custom_tensors(CNOT()) 21 | assert not bloq_has_custom_tensors(MultiAnd((1,) * 5)) 22 | -------------------------------------------------------------------------------- /qualtran/bloqs/mcmt/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from qualtran.bloqs.mcmt.and_bloq import And, MultiAnd 16 | from qualtran.bloqs.mcmt.controlled_via_and import ControlledViaAnd 17 | from qualtran.bloqs.mcmt.ctrl_spec_and import CtrlSpecAnd 18 | from qualtran.bloqs.mcmt.multi_control_pauli import MultiControlX, MultiControlZ 19 | from qualtran.bloqs.mcmt.multi_target_cnot import MultiTargetCNOT 20 | -------------------------------------------------------------------------------- /qualtran/bloqs/cryptography/ecc/find_ecc_private_key_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import pytest 15 | 16 | import qualtran.testing as qlt_testing 17 | from qualtran.bloqs.cryptography.ecc.find_ecc_private_key import _ecc 18 | 19 | 20 | def test_ecc(bloq_autotester): 21 | bloq_autotester(_ecc) 22 | 23 | 24 | @pytest.mark.notebook 25 | def test_notebook(): 26 | qlt_testing.execute_notebook('ecc') 27 | -------------------------------------------------------------------------------- /qualtran/bloqs/phase_estimation/kaiser_window_state_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from qualtran.bloqs.phase_estimation.kaiser_window_state import ( 16 | _kaiser_window_state_small, 17 | _kaiser_window_state_symbolic, 18 | ) 19 | 20 | 21 | def test_kaiser_window_state_auto(bloq_autotester): 22 | bloq_autotester(_kaiser_window_state_small) 23 | bloq_autotester(_kaiser_window_state_symbolic) 24 | -------------------------------------------------------------------------------- /dev_tools/requirements/envs/pip-tools.env.txt: -------------------------------------------------------------------------------- 1 | # 2 | # This file is autogenerated by pip-compile with Python 3.10 3 | # by the following command: 4 | # 5 | # pip-compile --constraint=envs/dev.env.txt --output-file=envs/pip-tools.env.txt deps/pip-tools.txt 6 | # 7 | build==1.3.0 8 | # via 9 | # -c envs/dev.env.txt 10 | # pip-tools 11 | click==8.3.0 12 | # via 13 | # -c envs/dev.env.txt 14 | # pip-tools 15 | packaging==25.0 16 | # via 17 | # -c envs/dev.env.txt 18 | # build 19 | pip-tools==7.5.1 20 | # via 21 | # -c envs/dev.env.txt 22 | # -r deps/pip-tools.txt 23 | pyproject-hooks==1.2.0 24 | # via 25 | # -c envs/dev.env.txt 26 | # build 27 | # pip-tools 28 | tomli==2.3.0 29 | # via 30 | # -c envs/dev.env.txt 31 | # build 32 | # pip-tools 33 | wheel==0.45.1 34 | # via 35 | # -c envs/dev.env.txt 36 | # pip-tools 37 | 38 | # The following packages are considered to be unsafe in a requirements file: 39 | # pip 40 | # setuptools 41 | -------------------------------------------------------------------------------- /qualtran/protos/args.proto: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2023 Google LLC 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | https://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | syntax = "proto3"; 18 | 19 | package qualtran; 20 | 21 | message IntOrSympy { 22 | oneof val { 23 | int64 int_val = 1; 24 | string sympy_expr = 2; 25 | } 26 | } 27 | 28 | message NDArray { 29 | // A Numpy array serialized as bytes using np.save() / np.load(). 30 | bytes ndarray = 1; 31 | } 32 | 33 | message Complex { 34 | float real = 1; 35 | float imag = 2; 36 | } -------------------------------------------------------------------------------- /qualtran/rotation_synthesis/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2025 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from qualtran.rotation_synthesis._math_config import NumpyConfig, with_dps 16 | from qualtran.rotation_synthesis.matrix import to_cirq, to_quirk, to_sequence 17 | from qualtran.rotation_synthesis.protocols import ( 18 | diagonal_unitary_approx, 19 | fallback_protocol, 20 | magnitude_approx, 21 | mixed_diagonal_protocol, 22 | mixed_fallback_protocol, 23 | ) 24 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/trotter/ising/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | r"""Bloqs implementing Trotter steps for the 1D Ising model under periodic boundary conditions. 15 | 16 | The Ising model is given as 17 | $$ 18 | H = -J\sum_{k=0}^{L-1}\sigma_{k}^{Z}\sigma_{(k+1)}^{Z} - \Gamma\sum_{k=0}^{L-1}\sigma_{k}^{X} 19 | $$ 20 | 21 | where $J$ and $\Gamma$ are coupling parameters. 22 | """ 23 | 24 | from .unitaries import IsingXUnitary, IsingZZUnitary 25 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/sparse/select_bloq_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from qualtran.bloqs.chemistry.sparse.select_bloq import _sel_sparse 16 | 17 | 18 | def test_prep_inner(bloq_autotester): 19 | bloq_autotester(_sel_sparse) 20 | 21 | 22 | def test_decompose_bloq_counts(): 23 | sel = _sel_sparse() 24 | cost_decomp = sel.decompose_bloq().call_graph()[1] 25 | cost_call = sel.call_graph()[1] 26 | assert cost_call == cost_decomp 27 | -------------------------------------------------------------------------------- /qualtran/exception/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """Qualtran exceptions that may be raised by the framework.""" 16 | 17 | from qualtran._infra.bloq import DecomposeNotImplementedError, DecomposeTypeError 18 | from qualtran._infra.composite_bloq import BloqError, DidNotFlattenAnythingError 19 | 20 | __all__ = [ 21 | 'DecomposeTypeError', 22 | 'DecomposeNotImplementedError', 23 | 'BloqError', 24 | 'DidNotFlattenAnythingError', 25 | ] 26 | -------------------------------------------------------------------------------- /qualtran/bloqs/for_testing/many_registers_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import qualtran.testing as qlt_testing 16 | from qualtran.bloqs.for_testing.many_registers import TestMultiRegister 17 | 18 | 19 | def test_test_multi_register(): 20 | bloq = TestMultiRegister() 21 | assert [r.name for r in bloq.signature] == ['xx', 'yy', 'zz'] 22 | assert sum(r.total_bits() for r in bloq.signature) == 12 23 | 24 | qlt_testing.assert_valid_bloq_decomposition(bloq) 25 | -------------------------------------------------------------------------------- /qualtran/bloqs/phase_estimation/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from qualtran.bloqs.phase_estimation.kaiser_window_state import KaiserWindowState 16 | from qualtran.bloqs.phase_estimation.lp_resource_state import LPResourceState 17 | from qualtran.bloqs.phase_estimation.qpe_window_state import RectangularWindowState 18 | from qualtran.bloqs.phase_estimation.qubitization_qpe import QubitizationQPE 19 | from qualtran.bloqs.phase_estimation.text_book_qpe import TextbookQPE 20 | -------------------------------------------------------------------------------- /qualtran/symbolics/types_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import pytest 16 | import sympy 17 | 18 | from qualtran.symbolics import is_symbolic, Shaped, slen 19 | 20 | 21 | @pytest.mark.parametrize( 22 | "shape", 23 | [(4,), (1, 2), (1, 2, 3), (sympy.Symbol('n'),), (sympy.Symbol('n'), sympy.Symbol('m'), 100)], 24 | ) 25 | def test_shaped(shape: tuple[int, ...]): 26 | shaped = Shaped(shape=shape) 27 | assert is_symbolic(shaped) 28 | assert slen(shaped) == shape[0] 29 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/trotter/grid_ham/qvr_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import pytest 15 | 16 | from qualtran.bloqs.chemistry.trotter.grid_ham.qvr import _qvr, QuantumVariableRotation 17 | 18 | 19 | def test_kinetic_energy(bloq_autotester): 20 | bloq_autotester(_qvr) 21 | 22 | 23 | @pytest.mark.parametrize('bitsize', [8, 16, 32]) 24 | def test_qvr_t_complexity(bitsize: int): 25 | bloq = QuantumVariableRotation(bitsize) 26 | assert bloq.t_complexity().rotations == bitsize 27 | -------------------------------------------------------------------------------- /qualtran/bloqs/cryptography/rsa/rsa_phase_estimate_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import pytest 16 | 17 | import qualtran.testing as qlt_testing 18 | from qualtran.bloqs.cryptography.rsa.rsa_phase_estimate import _rsa_pe, _rsa_pe_small 19 | 20 | 21 | @pytest.mark.parametrize('bloq', [_rsa_pe_small, _rsa_pe]) 22 | def test_rsa_pe(bloq_autotester, bloq): 23 | bloq_autotester(bloq) 24 | 25 | 26 | @pytest.mark.notebook 27 | def test_notebook(): 28 | qlt_testing.execute_notebook('rsa') 29 | -------------------------------------------------------------------------------- /qualtran/bloqs/state_preparation/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from qualtran.bloqs.state_preparation.prepare_uniform_superposition import ( 16 | PrepareUniformSuperposition, 17 | ) 18 | from qualtran.bloqs.state_preparation.state_preparation_alias_sampling import ( 19 | SparseStatePreparationAliasSampling, 20 | StatePreparationAliasSampling, 21 | ) 22 | from qualtran.bloqs.state_preparation.state_preparation_via_rotation import ( 23 | StatePreparationViaRotations, 24 | ) 25 | -------------------------------------------------------------------------------- /dev_tools/qualtran_dev_tools/git_tools.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import subprocess 16 | from pathlib import Path 17 | 18 | 19 | def get_git_root() -> Path: 20 | """Get the root git repository path.""" 21 | cp = subprocess.run( 22 | ['git', 'rev-parse', '--show-toplevel'], 23 | capture_output=True, 24 | universal_newlines=True, 25 | check=True, 26 | ) 27 | path = Path(cp.stdout.strip()).absolute() 28 | assert path.exists() 29 | return path 30 | -------------------------------------------------------------------------------- /qualtran/bloqs/arithmetic/trigonometric/arcsin_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import numpy as np 16 | 17 | from qualtran.bloqs.arithmetic.trigonometric.arcsin import _arcsin 18 | 19 | 20 | def test_arcsin(bloq_autotester): 21 | bloq_autotester(_arcsin) 22 | 23 | 24 | def test_arcsin_classical(): 25 | bloq = _arcsin() 26 | x = int(1 / np.sqrt(2) * 2**10) 27 | result_out = int(np.arcsin(1 / np.sqrt(2)) * 2**10) 28 | assert bloq.call_classically(x=x, result=0) == (x, result_out) 29 | -------------------------------------------------------------------------------- /qualtran/cirq_interop/decompose_protocol_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import cirq 16 | 17 | from qualtran.cirq_interop.decompose_protocol import _decompose_once_considering_known_decomposition 18 | 19 | 20 | def test_known_decomposition_empty_unitary(): 21 | class DecomposeEmptyList(cirq.testing.SingleQubitGate): 22 | def _decompose_(self, _): 23 | return [] 24 | 25 | gate = DecomposeEmptyList() 26 | assert _decompose_once_considering_known_decomposition(gate) == [] 27 | -------------------------------------------------------------------------------- /qualtran/bloqs/optimization/k_xor_sat/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from .kikuchi_adjacency_list import KikuchiNonZeroIndex 15 | from .kikuchi_adjacency_matrix import KikuchiMatrixEntry 16 | from .kikuchi_block_encoding import KikuchiHamiltonian, KikuchiMatrixEntry, KikuchiNonZeroIndex 17 | from .kikuchi_guiding_state import GuidingState, SimpleGuidingState 18 | from .kxor_instance import Constraint, KXorInstance 19 | from .load_kxor_instance import LoadConstraintScopes, LoadUniqueScopeIndex, PRGAUniqueConstraintRHS 20 | -------------------------------------------------------------------------------- /qualtran/serialization/annotations_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from qualtran.cirq_interop.t_complexity_protocol import TComplexity 16 | from qualtran.serialization import annotations 17 | 18 | 19 | def test_t_complexity_to_proto(): 20 | t_complexity = TComplexity(t=10, clifford=100, rotations=1000) 21 | proto = annotations.t_complexity_to_proto(t_complexity) 22 | assert (proto.t, proto.clifford, proto.rotations) == (10, 100, 1000) 23 | assert annotations.t_complexity_from_proto(proto) == t_complexity 24 | -------------------------------------------------------------------------------- /qualtran/cirq_interop/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # isort:skip_file 15 | 16 | """Bi-directional interop between Qualtran & Cirq.""" 17 | 18 | from ._cirq_to_bloq import ( 19 | CirqQuregT, 20 | CirqGateAsBloq, 21 | CirqGateAsBloqBase, 22 | cirq_optree_to_cbloq, 23 | cirq_gate_to_bloq, 24 | decompose_from_cirq_style_method, 25 | ) 26 | 27 | from ._bloq_to_cirq import BloqAsCirqGate 28 | 29 | from .._infra.gate_with_registers import get_named_qubits, merge_qubits 30 | 31 | assert [get_named_qubits, merge_qubits] 32 | -------------------------------------------------------------------------------- /qualtran/bloqs/bookkeeping/always_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2025 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import pytest 15 | 16 | from qualtran.bloqs.bookkeeping.always import _always_and, Always 17 | from qualtran.bloqs.for_testing import TestAtom 18 | from qualtran.testing import execute_notebook 19 | 20 | 21 | def test_example(bloq_autotester): 22 | bloq_autotester(_always_and) 23 | 24 | 25 | def test_always(): 26 | bloq = Always(TestAtom()) 27 | assert bloq.controlled() == bloq 28 | 29 | 30 | @pytest.mark.notebook 31 | def test_notebook(): 32 | execute_notebook('always') 33 | -------------------------------------------------------------------------------- /qualtran/bloqs/for_testing/costing_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from qualtran.bloqs.for_testing.costing import make_example_costing_bloqs 16 | from qualtran.resource_counting import format_call_graph_debug_text 17 | 18 | 19 | def test_costing_bloqs(): 20 | algo = make_example_costing_bloqs() 21 | g, _ = algo.call_graph() 22 | assert ( 23 | format_call_graph_debug_text(g) 24 | == """\ 25 | Algo -- 2 -> Func1 26 | Algo -- 1 -> Func2 27 | Func1 -- 10 -> H 28 | Func1 -- 10 -> T 29 | Func1 -- 10 -> T† 30 | Func2 -- 100 -> Toffoli""" 31 | ) 32 | -------------------------------------------------------------------------------- /qualtran/bloqs/reflections/prepare_identity_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from qualtran import Signature 15 | from qualtran.bloqs.basic_gates import Identity 16 | from qualtran.bloqs.reflections.prepare_identity import _prepare_identity, PrepareIdentity 17 | 18 | 19 | def test_prepare_identity(bloq_autotester): 20 | bloq_autotester(_prepare_identity) 21 | 22 | 23 | def test_prepare_identity_call_graph(): 24 | bloq = PrepareIdentity(tuple(Signature.build(a=4, b=4, c=5))) 25 | _, sigma = bloq.call_graph() 26 | assert sigma == {Identity(4): 2, Identity(5): 1} 27 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/black_boxes_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import pytest 15 | from openfermion.resource_estimates.utils import QI, QR 16 | 17 | from qualtran.bloqs.chemistry.black_boxes import get_qroam_cost_clean_ancilla 18 | 19 | 20 | @pytest.mark.parametrize("data_size, bitsize", ((100, 10), (100, 3), (1_000, 13), (1_000_000, 20))) 21 | def test_qroam_factors(data_size, bitsize): 22 | assert get_qroam_cost_clean_ancilla(data_size, bitsize) == QR(data_size, bitsize)[-1] 23 | assert get_qroam_cost_clean_ancilla(data_size, bitsize, adjoint=True) == QI(data_size)[-1] 24 | -------------------------------------------------------------------------------- /qualtran/bloqs/for_testing/interior_alloc_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import qualtran.testing as qlt_testing 15 | from qualtran import QAny 16 | from qualtran.bloqs.basic_gates.swap import Swap 17 | from qualtran.bloqs.bookkeeping import Allocate, Free 18 | from qualtran.bloqs.for_testing.interior_alloc import InteriorAlloc 19 | 20 | 21 | def test_interior_alloc(): 22 | ia = InteriorAlloc(10) 23 | qlt_testing.assert_valid_bloq_decomposition(ia) 24 | g, counts = ia.call_graph(max_depth=1) 25 | assert counts == {Allocate(QAny(10)): 1, Free(QAny(10)): 1, Swap(10): 2} 26 | -------------------------------------------------------------------------------- /qualtran/rotation_synthesis/lattice/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2025 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """A package that provides methods for integer point enumeration.""" 16 | 17 | from qualtran.rotation_synthesis.lattice._geometry import Ellipse, Range, Rectangle 18 | from qualtran.rotation_synthesis.lattice._grid_operators import GridOperator 19 | from qualtran.rotation_synthesis.lattice._ipe import ( 20 | enumerate_1d, 21 | enumerate_upright, 22 | get_overall_action, 23 | get_points_from_state, 24 | ) 25 | from qualtran.rotation_synthesis.lattice._state import GridOperatorAction, SelingerState 26 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/trotter/grid_ham/kinetic_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import pytest 15 | 16 | import qualtran.testing as qlt_testing 17 | from qualtran.bloqs.chemistry.trotter.grid_ham.kinetic import _kinetic_energy, KineticEnergy 18 | 19 | 20 | @pytest.mark.parametrize("nelec, nx", ((2, 10), (6, 8), (8, 12))) 21 | def test_kinetic_bloq(nelec, nx): 22 | ngrid_x = 2 * nx + 1 23 | ke = KineticEnergy(nelec, ngrid_x) 24 | qlt_testing.assert_valid_bloq_decomposition(ke) 25 | 26 | 27 | def test_kinetic_energy(bloq_autotester): 28 | bloq_autotester(_kinetic_energy) 29 | -------------------------------------------------------------------------------- /dev_tools/templates/page.jinja: -------------------------------------------------------------------------------- 1 | {%- extends 'templates/root.jinja' -%} 2 | 3 | {#----------------------------------------------------------------------------#} 4 | 5 | {% block content %} 6 | {{- self.header() -}} 7 | {{- self.metadata() -}} 8 | {{- self.body() -}} 9 | {% endblock content %}} 10 | 11 | {#----------------------------------------------------------------------------#} 12 | 13 | {% block header %} 14 | {{ builder.get_devsite_headers() }} 15 | # {{page_info.short_name}} 16 | 17 | 18 | 19 | {% endblock header%} 20 | 21 | {#----------------------------------------------------------------------------#} 22 | 23 | {% block metadata %} 24 | 25 | {{ page_info.doc.brief }} 26 | 27 | {{ builder.build_collapsable_aliases() -}} 28 | {% endblock metadata %} 29 | 30 | {#----------------------------------------------------------------------------#} 31 | 32 | {% block body %} 33 | 34 | {{ builder.top_compat() -}} 35 | 36 | {% for part in page_info.doc.docstring_parts %} 37 | {{- builder.format_docstring_part(part) -}} 38 | {% endfor %} 39 | {{ builder.bottom_compat() -}} 40 | {% endblock body %} 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /qualtran/bloqs/optimization/k_xor_sat/kxor_instance_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import numpy as np 15 | import pytest 16 | 17 | from qualtran.bloqs.optimization.k_xor_sat.kxor_instance import KXorInstance 18 | 19 | 20 | @pytest.mark.slow 21 | @pytest.mark.parametrize("rho", [0, 0.8, 0.9]) 22 | def test_max_rhs(rho: float): 23 | rng = np.random.default_rng(402) 24 | 25 | rhs = [] 26 | for i in range(100): 27 | inst = KXorInstance.random_instance(n=100, m=1000, k=4, planted_advantage=rho, rng=rng) 28 | rhs.append(inst.max_rhs) 29 | 30 | assert max(rhs) == 2 31 | -------------------------------------------------------------------------------- /qualtran/protos/registers.proto: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2023 Google LLC 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | https://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | syntax = "proto3"; 18 | 19 | import "qualtran/protos/args.proto"; 20 | import "qualtran/protos/data_types.proto"; 21 | 22 | package qualtran; 23 | 24 | message Register { 25 | // A quantum register. 26 | enum Side { 27 | UNKNOWN = 0; 28 | LEFT = 1; 29 | RIGHT = 2; 30 | THRU = 3; 31 | } 32 | string name = 1; 33 | QDataType dtype = 2; 34 | repeated IntOrSympy shape = 3; 35 | Side side = 4; 36 | } 37 | 38 | message Registers { 39 | // A collection of Registers. 40 | repeated Register registers = 1; 41 | } -------------------------------------------------------------------------------- /dev_tools/repomix/README.md: -------------------------------------------------------------------------------- 1 | # Qualtran Repomix 2 | 3 | We use the [repomix](https://github.com/yamadashy/repomix) tool to concatenate relevant 4 | context so LLMs can write Qualtran code. 5 | 6 | The repository is too large to naively include everything in the context window, so we're 7 | a little selective. The packed output contains: 8 | 9 | - Reference docs for core modules and subpackages, via markdown. You must first run 10 | `dev_tools/build-reference-docs-2.py`. 11 | - Documentation notebooks (bloq notebooks and concept notebooks), via markdown. You must 12 | first run `dev_tools/execute-notebooks.py --output-md`. 13 | - Selected `*.py` files in `qualtran/bloqs/**/*.py`. The list is enumerated in 14 | `repomix-driver.py`. 15 | 16 | After ensuring the docs are rendered in markdown format, run `python repomix-driver.py`. This 17 | will run the repomix program, which requires Node.js. 18 | 19 | 20 | -------------- 21 | 22 | You may want to run `git clean -ndX` (change `n` to `f`) in the `docs/` directory before 23 | running the markdown-export scripts to get a fresh build of the docs. `repomix-driver.py` will 24 | include anything with a `*.md` extension, including any vestigial files. -------------------------------------------------------------------------------- /qualtran/bloqs/gf_arithmetic/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from qualtran.bloqs.gf_arithmetic.gf2_add_k import GF2AddK 16 | from qualtran.bloqs.gf_arithmetic.gf2_addition import GF2Addition 17 | from qualtran.bloqs.gf_arithmetic.gf2_inverse import GF2Inverse 18 | from qualtran.bloqs.gf_arithmetic.gf2_multiplication import ( 19 | BinaryPolynomialMultiplication, 20 | GF2MulK, 21 | GF2Multiplication, 22 | GF2MulViaKaratsuba, 23 | GF2ShiftLeft, 24 | GF2ShiftRight, 25 | MultiplyPolyByOnePlusXk, 26 | ) 27 | from qualtran.bloqs.gf_arithmetic.gf2_square import GF2Square 28 | -------------------------------------------------------------------------------- /qualtran/linalg/matrix_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import numpy as np 15 | import pytest 16 | from cirq.testing import random_unitary 17 | 18 | from qualtran.linalg.matrix import unitary_distance_ignoring_global_phase 19 | 20 | 21 | @pytest.mark.parametrize("dim", [2, 3]) 22 | def test_unitary_distance_zero(dim: int): 23 | rs = np.random.RandomState(1234) 24 | for _ in range(3): 25 | U = random_unitary(dim, random_state=rs) 26 | V = np.exp(1j * rs.random()) * U 27 | d = unitary_distance_ignoring_global_phase(U, V) 28 | np.testing.assert_almost_equal(d, 0) 29 | -------------------------------------------------------------------------------- /qualtran/resource_counting/_success_prob_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from qualtran.bloqs.for_testing.costing import CostingBloq 15 | from qualtran.resource_counting import get_cost_cache, get_cost_value, SuccessProb 16 | 17 | 18 | def test_coin_flip(): 19 | flip = CostingBloq('CoinFlip', num_qubits=1, static_costs=[(SuccessProb(), 0.5)]) 20 | algo = CostingBloq('Algo', num_qubits=0, callees=[(flip, 4)]) 21 | 22 | p = get_cost_value(algo, SuccessProb()) 23 | assert p == 0.5**4 24 | 25 | costs = get_cost_cache(algo, SuccessProb()) 26 | assert costs == {algo: p, flip: 0.5} 27 | -------------------------------------------------------------------------------- /qualtran/rotation_synthesis/_typing.py: -------------------------------------------------------------------------------- 1 | # Copyright 2025 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from typing import Union 16 | 17 | import mpmath 18 | import numpy as np 19 | from typing_extensions import TypeIs 20 | 21 | # mypy has a bug where it doesn't understand numbers.* https://github.com/python/mypy/issues/3186 22 | # So we define our own types 23 | Real = Union[float, np.floating, mpmath.ctx_mp_python.mpf] 24 | Integral = Union[int, np.integer] 25 | Complex = Union[complex, np.complexfloating, mpmath.ctx_mp_python.mpc] 26 | 27 | 28 | def is_int(x) -> TypeIs[Integral]: 29 | return isinstance(x, (int, np.integer)) 30 | -------------------------------------------------------------------------------- /dev_tools/qualtran_dev_tools/all_call_graph_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import networkx as nx 16 | import pytest 17 | 18 | from .all_call_graph import get_all_call_graph 19 | from .bloq_finder import get_bloq_examples 20 | 21 | 22 | @pytest.mark.slow 23 | def test_get_all_call_graph(): 24 | # This test generates a union of the call graphs of every bloq example in the library. 25 | # This test makes sure that there aren't any bloq examples with broken call graphs. 26 | bes = get_bloq_examples() 27 | g = get_all_call_graph(bes) 28 | res = list(nx.simple_cycles(g)) 29 | assert res == [] 30 | -------------------------------------------------------------------------------- /qualtran/bloqs/gf_arithmetic/gf_utils.py: -------------------------------------------------------------------------------- 1 | # Copyright 2025 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from typing import Sequence, Union 15 | 16 | from galois import Poly 17 | 18 | from qualtran import QGF 19 | from qualtran.symbolics import is_symbolic, SymbolicInt 20 | 21 | 22 | def qgf_converter(x: Union[QGF, int, Poly, SymbolicInt, Sequence[int]]) -> QGF: 23 | if isinstance(x, QGF): 24 | return x 25 | if isinstance(x, int): 26 | return QGF(2, x) 27 | if is_symbolic(x): 28 | return QGF(2, x) 29 | if isinstance(x, Poly): 30 | return QGF(2, x.degree, x) 31 | p = Poly.Degrees(x) 32 | return QGF(2, p.degree, p) 33 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/pbc/first_quantization/select_t_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from qualtran.bloqs.chemistry.pbc.first_quantization.select_t import ( 15 | _select_t, 16 | SelectTFirstQuantization, 17 | ) 18 | from qualtran.resource_counting import get_cost_value, QECGatesCost 19 | 20 | 21 | def test_select_t(bloq_autotester): 22 | bloq_autotester(_select_t) 23 | 24 | 25 | def test_select_kinetic_t_counts(): 26 | num_bits_p = 6 27 | sel = SelectTFirstQuantization(num_bits_p, 10) 28 | toffolis = get_cost_value(sel, QECGatesCost()).total_toffoli_only() 29 | assert toffolis == 5 * (num_bits_p - 1) + 2 30 | -------------------------------------------------------------------------------- /dev_tools/clean-notebooks.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import argparse 15 | import sys 16 | 17 | from qualtran_dev_tools.clean_notebooks import clean_notebooks 18 | from qualtran_dev_tools.git_tools import get_git_root 19 | 20 | 21 | def parse_args(): 22 | p = argparse.ArgumentParser() 23 | p.add_argument('--apply', action=argparse.BooleanOptionalAction, default=False) 24 | args = p.parse_args() 25 | 26 | reporoot = get_git_root() 27 | sourceroot = reporoot / 'qualtran' 28 | n_bad = clean_notebooks(sourceroot, do_clean=args.apply) 29 | sys.exit(n_bad) 30 | 31 | 32 | if __name__ == '__main__': 33 | parse_args() 34 | -------------------------------------------------------------------------------- /qualtran/drawing/musical_score.html: -------------------------------------------------------------------------------- 1 | 17 | 18 | 19 | 20 | 21 | 22 | musical score 23 | 24 | 28 | 29 | 30 |
31 | 32 |
33 | 37 | 38 | 41 | 42 | -------------------------------------------------------------------------------- /qualtran/bloqs/for_testing/qubit_count_many_alloc_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2025 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import qualtran.testing as qlt_testing 15 | from qualtran.bloqs.for_testing.qubit_count_many_alloc import ( 16 | TestManyAllocAbstracted, 17 | TestManyAllocMany, 18 | TestManyAllocOnce, 19 | ) 20 | 21 | 22 | def test_many_alloc_validity(): 23 | qlt_testing.assert_valid_bloq_decomposition(TestManyAllocMany(10)) 24 | qlt_testing.assert_valid_bloq_decomposition(TestManyAllocOnce(10)) 25 | qlt_testing.assert_valid_bloq_decomposition(TestManyAllocAbstracted(10)) 26 | qlt_testing.assert_valid_cbloq(TestManyAllocAbstracted(10).as_composite_bloq().flatten()) 27 | -------------------------------------------------------------------------------- /qualtran/bloqs/for_testing/with_decomposition_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import qualtran.testing as qlt_testing 16 | from qualtran.bloqs.for_testing.with_decomposition import ( 17 | TestIndependentParallelCombo, 18 | TestParallelCombo, 19 | TestSerialCombo, 20 | ) 21 | 22 | 23 | def test_test_serial_combo(): 24 | qlt_testing.assert_valid_bloq_decomposition(TestSerialCombo()) 25 | 26 | 27 | def test_test_parallel_combo(): 28 | qlt_testing.assert_valid_bloq_decomposition(TestParallelCombo()) 29 | 30 | 31 | def test_test_indep_parallel_combo(): 32 | qlt_testing.assert_valid_bloq_decomposition(TestIndependentParallelCombo()) 33 | -------------------------------------------------------------------------------- /qualtran/bloqs/for_testing/matrix_gate_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import cirq 15 | import numpy as np 16 | import pytest 17 | 18 | from .matrix_gate import MatrixGate 19 | 20 | 21 | @pytest.mark.parametrize("bitsize", [1, 2, 3]) 22 | def test_create_and_unitary(bitsize: int): 23 | random_state = np.random.RandomState(42) 24 | for _ in range(5): 25 | gate = MatrixGate.random(bitsize, random_state=random_state) 26 | np.testing.assert_allclose(cirq.unitary(gate), gate.matrix) 27 | np.testing.assert_allclose(cirq.unitary(gate**-1), np.conj(gate.matrix).T) 28 | np.testing.assert_allclose(gate.tensor_contract(), gate.matrix) 29 | -------------------------------------------------------------------------------- /qualtran/rotation_synthesis/matrix/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2025 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | r"""A submodule for compiling $\mathbb{Z}[e^{i \pi/4}]$ matrices to Clifford+T as well as generating them.""" 16 | 17 | from qualtran.rotation_synthesis.matrix._analytical_decomposition import ( 18 | su_unitary_to_zxz_angles, 19 | unitary_from_zxz, 20 | ) 21 | from qualtran.rotation_synthesis.matrix._clifford_t_repr import to_cirq, to_quirk, to_sequence 22 | from qualtran.rotation_synthesis.matrix._generation import ( 23 | generate_cliffords, 24 | generate_rotations, 25 | generate_rotations_iter, 26 | ) 27 | from qualtran.rotation_synthesis.matrix._su2_ct import SU2CliffordT 28 | -------------------------------------------------------------------------------- /qualtran/serialization/annotations.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from qualtran.cirq_interop.t_complexity_protocol import TComplexity 16 | from qualtran.protos import annotations_pb2 17 | 18 | 19 | def t_complexity_to_proto(t_complexity: TComplexity) -> annotations_pb2.TComplexity: 20 | return annotations_pb2.TComplexity( 21 | clifford=t_complexity.clifford, rotations=t_complexity.rotations, t=t_complexity.t 22 | ) 23 | 24 | 25 | def t_complexity_from_proto(t_complexity: annotations_pb2.TComplexity) -> TComplexity: 26 | return TComplexity( 27 | clifford=t_complexity.clifford, t=t_complexity.t, rotations=t_complexity.rotations 28 | ) 29 | -------------------------------------------------------------------------------- /qualtran/symbolics/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """Utilities for simultaneous support for Sympy symbolic objects and concrete values.""" 16 | 17 | from qualtran.symbolics.math_funcs import ( 18 | acos, 19 | bit_length, 20 | ceil, 21 | floor, 22 | is_zero, 23 | ln, 24 | log2, 25 | pi, 26 | prod, 27 | sabs, 28 | sarg, 29 | sconj, 30 | sexp, 31 | smax, 32 | smin, 33 | ssqrt, 34 | ssum, 35 | ) 36 | from qualtran.symbolics.types import ( 37 | HasLength, 38 | is_symbolic, 39 | shape, 40 | Shaped, 41 | slen, 42 | SymbolicComplex, 43 | SymbolicFloat, 44 | SymbolicInt, 45 | ) 46 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/pbc/first_quantization/projectile/select_t_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from qualtran.bloqs.chemistry.pbc.first_quantization.projectile.select_t import ( 15 | _sel_t_proj, 16 | SelectTFirstQuantizationWithProj, 17 | ) 18 | from qualtran.resource_counting import get_cost_value, QECGatesCost 19 | 20 | 21 | def test_sel_t_proj(bloq_autotester): 22 | bloq_autotester(_sel_t_proj) 23 | 24 | 25 | def test_select_kinetic_t_counts(): 26 | num_bits_n = 6 27 | sel = SelectTFirstQuantizationWithProj(num_bits_n, 10) 28 | toffolis = get_cost_value(sel, QECGatesCost()).total_toffoli_only() 29 | assert toffolis == 5 * (num_bits_n - 1) + 2 + 1 30 | -------------------------------------------------------------------------------- /qualtran/bloqs/bookkeeping/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Bloqs for virtual operations and register reshaping.""" 15 | 16 | from qualtran.bloqs.bookkeeping.allocate import Allocate 17 | from qualtran.bloqs.bookkeeping.always import Always 18 | from qualtran.bloqs.bookkeeping.arbitrary_clifford import ArbitraryClifford 19 | from qualtran.bloqs.bookkeeping.auto_partition import AutoPartition 20 | from qualtran.bloqs.bookkeeping.cast import Cast 21 | from qualtran.bloqs.bookkeeping.free import Free 22 | from qualtran.bloqs.bookkeeping.join import Join 23 | from qualtran.bloqs.bookkeeping.partition import Join2, Partition, Split2 24 | from qualtran.bloqs.bookkeeping.split import Split 25 | -------------------------------------------------------------------------------- /qualtran/bloqs/phase_estimation/qpe_window_state_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import numpy as np 15 | 16 | from qualtran.bloqs.phase_estimation.qpe_window_state import ( 17 | _rectangular_window_state_small, 18 | _rectangular_window_state_symbolic, 19 | RectangularWindowState, 20 | ) 21 | 22 | 23 | def test_rectangular_window_state_tensor(): 24 | n = 4 25 | bloq = RectangularWindowState(n) 26 | np.testing.assert_allclose(bloq.tensor_contract(), np.zeros(2**n) + 1 / 2 ** (n / 2)) 27 | 28 | 29 | def test_rectangular_window_state(bloq_autotester): 30 | bloq_autotester(_rectangular_window_state_small) 31 | bloq_autotester(_rectangular_window_state_symbolic) 32 | -------------------------------------------------------------------------------- /qualtran/resource_counting/_generalization.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from typing import Callable, Optional, TYPE_CHECKING 15 | 16 | if TYPE_CHECKING: 17 | from qualtran import Bloq 18 | 19 | GeneralizerT = Callable[['Bloq'], Optional['Bloq']] 20 | 21 | 22 | def _make_composite_generalizer(*funcs: 'GeneralizerT') -> 'GeneralizerT': 23 | """Return a generalizer that calls each `*funcs` generalizers in order.""" 24 | 25 | def _composite_generalize(b: Optional['Bloq']) -> Optional['Bloq']: 26 | for func in funcs: 27 | if b is None: 28 | return None 29 | b = func(b) 30 | return b 31 | 32 | return _composite_generalize 33 | -------------------------------------------------------------------------------- /qualtran/serialization/ec_point_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import pytest 16 | 17 | from qualtran.bloqs.cryptography.ecc import ECPoint 18 | from qualtran.serialization.ec_point import ec_point_from_proto, ec_point_to_proto 19 | 20 | 21 | @pytest.mark.parametrize( 22 | "ec_point", 23 | [ 24 | ECPoint(x=15, y=13, mod=17, curve_a=0), 25 | ECPoint(x=0, y=2, mod=7, curve_a=3), 26 | ECPoint(x=0, y=2, mod=7), 27 | ], 28 | ) 29 | def test_ec_point_to_proto_roundtrip(ec_point: ECPoint): 30 | ec_point_proto = ec_point_to_proto(ec_point) 31 | ec_point_roundtrip = ec_point_from_proto(ec_point_proto) 32 | assert ec_point == ec_point_roundtrip 33 | -------------------------------------------------------------------------------- /qualtran/protos/annotations_pb2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Generated by the protocol buffer compiler. DO NOT EDIT! 3 | # source: qualtran/protos/annotations.proto 4 | # Protobuf Python Version: 5.26.1 5 | """Generated protocol buffer code.""" 6 | from google.protobuf import descriptor as _descriptor 7 | from google.protobuf import descriptor_pool as _descriptor_pool 8 | from google.protobuf import symbol_database as _symbol_database 9 | from google.protobuf.internal import builder as _builder 10 | # @@protoc_insertion_point(imports) 11 | 12 | _sym_db = _symbol_database.Default() 13 | 14 | 15 | 16 | 17 | DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n!qualtran/protos/annotations.proto\x12\x08qualtran\"=\n\x0bTComplexity\x12\x10\n\x08\x63lifford\x18\x01 \x01(\x03\x12\x11\n\trotations\x18\x02 \x01(\x03\x12\t\n\x01t\x18\x03 \x01(\x03\x62\x06proto3') 18 | 19 | _globals = globals() 20 | _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) 21 | _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'qualtran.protos.annotations_pb2', _globals) 22 | if not _descriptor._USE_C_DESCRIPTORS: 23 | DESCRIPTOR._loaded_options = None 24 | _globals['_TCOMPLEXITY']._serialized_start=47 25 | _globals['_TCOMPLEXITY']._serialized_end=108 26 | # @@protoc_insertion_point(module_scope) 27 | -------------------------------------------------------------------------------- /qualtran/qref_interop/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | # Licensed under the Apache License, Version 2.0 (the "License"); 16 | # you may not use this file except in compliance with the License. 17 | # You may obtain a copy of the License at 18 | # 19 | # https://www.apache.org/licenses/LICENSE-2.0 20 | # 21 | # Unless required by applicable law or agreed to in writing, software 22 | # distributed under the License is distributed on an "AS IS" BASIS, 23 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 24 | # See the License for the specific language governing permissions and 25 | # limitations under the License. 26 | 27 | from ._bloq_to_qref import bloq_to_qref 28 | -------------------------------------------------------------------------------- /qualtran/_infra/bloq_example_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from qualtran import bloq_example, BloqExample 16 | from qualtran.bloqs.for_testing import TestAtom 17 | 18 | 19 | def _tester_bloq_func() -> TestAtom: 20 | return TestAtom() 21 | 22 | 23 | @bloq_example 24 | def _tester_bloq() -> TestAtom: 25 | return TestAtom() 26 | 27 | 28 | def test_bloq_example_explicit(): 29 | be = BloqExample(func=_tester_bloq_func, name='tester_bloq', bloq_cls=TestAtom) 30 | assert be.name == 'tester_bloq' 31 | assert be.bloq_cls == TestAtom 32 | 33 | 34 | def test_bloq_example_decorator(): 35 | be = _tester_bloq 36 | assert be.name == 'tester_bloq' 37 | assert be.bloq_cls == TestAtom 38 | -------------------------------------------------------------------------------- /qualtran/rotation_synthesis/protocols/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2025 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from qualtran.rotation_synthesis.protocols._clifford_t_synthesis import ( 16 | diagonal_unitary_approx, 17 | fallback_protocol, 18 | magnitude_approx, 19 | mixed_diagonal_protocol, 20 | mixed_fallback_protocol, 21 | ) 22 | from qualtran.rotation_synthesis.protocols._diagonal import Diagonal 23 | from qualtran.rotation_synthesis.protocols._fallback import Fallback 24 | from qualtran.rotation_synthesis.protocols._mixed_diagonal import MixedDiagonal 25 | from qualtran.rotation_synthesis.protocols._protocol import ( 26 | ApproxProblem, 27 | PointCollector, 28 | SimplePointCollector, 29 | SplitRegionCollector, 30 | ) 31 | -------------------------------------------------------------------------------- /qualtran/surface_code/physical_cost_summary.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from attrs import frozen 16 | 17 | 18 | @frozen 19 | class PhysicalCostsSummary: 20 | failure_prob: float 21 | """Approximate probability of an error occurring during execution of the algorithm. 22 | 23 | This can be a bad CCZ being produced, a bad T state being produced, 24 | or a topological error occurring during the algorithm. 25 | """ 26 | 27 | footprint: int 28 | """Total physical qubits required to run algorithm.""" 29 | 30 | duration_hr: float 31 | """Total time in hours to run algorithm.""" 32 | 33 | @property 34 | def qubit_hours(self) -> float: 35 | return self.footprint * self.duration_hr 36 | -------------------------------------------------------------------------------- /dev_tools/README.md: -------------------------------------------------------------------------------- 1 | # Development tooling 2 | 3 | This directory contains scripts, modules, and configuration data used by developers. 4 | 5 | `conf/` contains configuration files (for typecheckers, formatters, ...) 6 | 7 | `requirements/` contain the sources-of-truth for the various dependencies of the project. 8 | Notably, `requirements/deps/runtime.txt` contains the requirements that `pip install qualtran` 9 | will try to satisfy. The `requirements/` directory contains additional developer dependencies 10 | as well as pinned-version environment specifications. 11 | 12 | `templates/` is used by the reference doc generation script. 13 | 14 | At the top level of `dev_tools/.`, there are scripts that do various things. These scripts 15 | should be run from the command line (if you know what you're doing). 16 | 17 | `qualtran_dev_tools/` contains re-usable library code that may be helpful for writing 18 | developer-oriented scripts that e.g. do meta-analysis on the codebase. If you do 19 | `pip install -e /path/to/Qualtran/dev_tools/`, it will install a package called 20 | `qualtran-dev-tools` which can be accessed from Python by `import qualtran_dev_tools.submodule`. 21 | This package need not be installed if you're just interested in writing or analyzing quantum 22 | algorithms and is really only useful for doing framework-scale meta-analysis. -------------------------------------------------------------------------------- /SUPPORT.md: -------------------------------------------------------------------------------- 1 | # Support 2 | 3 | Thank you for your interest in this project! If you are experiencing problems 4 | or have questions, the following are some suggestions for how to get help. 5 | 6 | > [!NOTE] 7 | > Before participating in our community, please read our [code of 8 | > conduct](CODE_OF_CONDUCT.md). By interacting with this repository, 9 | > organization, or community, you agree to abide by its terms. 10 | 11 | ## Report an issue or request a feature 12 | 13 | To report an issue or request a feature in Qualtran, please first search the 14 | [issue tracker on GitHub](https://github.com/quantumlib/Qualtran/issues) to 15 | check if there is already an open issue identical or similar to your bug 16 | report/feature request. If there is none, go ahead and file a new issue in the 17 | issue tracker. 18 | 19 | ## Attend the developer meetings 20 | 21 | _Qualtran Cynq_ is our biweekly virtual meeting of contributors to discuss 22 | everything from issues to ongoing efforts, as well as to ask questions. Join the 23 | [`qualtran-dev` Google 24 | Group](https://groups.google.com/forum/#!forum/qualtran-dev) to get an automatic 25 | meeting invitation. 26 | 27 | ## Contact the maintainers 28 | 29 | For any questions or concerns not addressed here, please email 30 | [quantum-oss-maintainers@google.com](mailto:quantum-oss-maintainers@google.com). 31 | -------------------------------------------------------------------------------- /qualtran/simulation/tensor/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """Functionality for the `Bloq.tensor_contract()` protocol.""" 16 | 17 | from ._dense import bloq_to_dense, quimb_to_dense 18 | from ._flattening import bloq_has_custom_tensors, flatten_for_tensor_contraction 19 | from ._quimb import cbloq_to_quimb, cbloq_to_superquimb, DiscardInd, initialize_from_zero 20 | from ._tensor_data_manipulation import ( 21 | active_space_for_ctrl_spec, 22 | eye_tensor_for_signature, 23 | tensor_data_from_unitary_and_signature, 24 | tensor_out_inp_shape_from_signature, 25 | tensor_shape_from_signature, 26 | ) 27 | from ._tensor_from_classical import ( 28 | bloq_to_dense_via_classical_action, 29 | my_tensors_from_classical_action, 30 | ) 31 | -------------------------------------------------------------------------------- /qualtran/bloqs/bookkeeping/free_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from qualtran import BloqBuilder, QAny, Soquet 16 | from qualtran.bloqs.bookkeeping import Allocate, Free, Join, Split 17 | from qualtran.bloqs.bookkeeping.free import _free 18 | 19 | 20 | def test_free(bloq_autotester): 21 | bloq_autotester(_free) 22 | 23 | 24 | def test_util_bloqs(): 25 | bb = BloqBuilder() 26 | qs1 = bb.add(Allocate(QAny(10))) 27 | assert isinstance(qs1, Soquet) 28 | qs2 = bb.add(Split(QAny(10)), reg=qs1) 29 | assert qs2.shape == (10,) 30 | qs3 = bb.add(Join(QAny(10)), reg=qs2) 31 | assert isinstance(qs3, Soquet) 32 | no_return = bb.add(Free(QAny(10)), reg=qs3) 33 | assert no_return is None 34 | assert bb.finalize().tensor_contract() == 1.0 35 | -------------------------------------------------------------------------------- /qualtran/serialization/ctrl_spec_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import pytest 16 | 17 | from qualtran import CtrlSpec, QBit, QUInt 18 | from qualtran.serialization.ctrl_spec import ctrl_spec_from_proto, ctrl_spec_to_proto 19 | 20 | 21 | @pytest.mark.parametrize( 22 | "ctrl_spec", 23 | [ 24 | CtrlSpec(), 25 | CtrlSpec(cvs=0), 26 | CtrlSpec(qdtypes=QUInt(4), cvs=0b0110), 27 | CtrlSpec(cvs=[0, 1, 1, 0]), 28 | CtrlSpec(qdtypes=[QBit(), QBit()], cvs=[[0, 1], [1, 0]]), 29 | ], 30 | ) 31 | def test_ctrl_spec_to_proto_roundtrip(ctrl_spec: CtrlSpec): 32 | ctrl_spec_proto = ctrl_spec_to_proto(ctrl_spec) 33 | ctrl_spec_roundtrip = ctrl_spec_from_proto(ctrl_spec_proto) 34 | assert ctrl_spec == ctrl_spec_roundtrip 35 | -------------------------------------------------------------------------------- /qualtran/bloqs/arithmetic/conversions/contiguous_index_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from qualtran import BloqBuilder 15 | from qualtran.bloqs.arithmetic.conversions.contiguous_index import ( 16 | _to_contg_index, 17 | ToContiguousIndex, 18 | ) 19 | 20 | 21 | def test_to_contigous_index(bloq_autotester): 22 | bloq_autotester(_to_contg_index) 23 | 24 | 25 | def test_to_contiguous_index_t_complexity(): 26 | bb = BloqBuilder() 27 | bitsize = 5 28 | q0 = bb.add_register('mu', bitsize) 29 | q1 = bb.add_register('nu', bitsize) 30 | out = bb.add_register('s', 2 * bitsize) 31 | q0, q1, out = bb.add(ToContiguousIndex(bitsize, 2 * bitsize), mu=q0, nu=q1, s=out) 32 | cbloq = bb.finalize(mu=q0, nu=q1, s=out) 33 | assert cbloq.t_complexity().t == 4 * 29 34 | -------------------------------------------------------------------------------- /dev_tools/qualtran_dev_tools/make_reference_docs/_components/aliases.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from typing import Dict 15 | 16 | 17 | def get_aliases_str(pref_dotpath: str, *, aliases_d: Dict[str, str]) -> str: 18 | """From a complete dictionary of aliases, return a formatted string with our aliases. 19 | 20 | This will return just `pref_dotpath` if there are none, 21 | otherwise '{pref} **Alias(es):** {...}' 22 | """ 23 | aliases = [k for k, v in aliases_d.items() if v == pref_dotpath and k != pref_dotpath] 24 | s = f'`{pref_dotpath}`' 25 | alias_str = ', '.join(f'`{a}`' for a in aliases) 26 | if len(aliases) > 1: 27 | s += f". **Aliases:** {alias_str}" 28 | elif len(aliases) == 1: 29 | s += f". **Alias:** {alias_str}" 30 | return s 31 | -------------------------------------------------------------------------------- /qualtran/bloqs/arithmetic/conversions/ones_complement_to_twos_complement_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from qualtran import BloqBuilder 15 | from qualtran.bloqs.arithmetic.conversions.ones_complement_to_twos_complement import ( 16 | _signed_to_twos, 17 | SignedIntegerToTwosComplement, 18 | ) 19 | from qualtran.bloqs.basic_gates import Toffoli 20 | 21 | 22 | def test_signed_to_twos(bloq_autotester): 23 | bloq_autotester(_signed_to_twos) 24 | 25 | 26 | def test_signed_to_twos_complement_toffoli_count(): 27 | bb = BloqBuilder() 28 | bitsize = 5 29 | q0 = bb.add_register('x', bitsize) 30 | q0 = bb.add(SignedIntegerToTwosComplement(bitsize), x=q0) 31 | cbloq = bb.finalize(x=q0) 32 | _, sigma = cbloq.call_graph() 33 | assert sigma[Toffoli()] == (5 - 2) 34 | -------------------------------------------------------------------------------- /qualtran/linalg/testing.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import numpy as np 15 | from numpy.typing import NDArray 16 | 17 | from qualtran.linalg.matrix import unitary_distance_ignoring_global_phase 18 | 19 | 20 | def assert_matrices_almost_equal(A: NDArray, B: NDArray, *, atol: float = 1e-5): 21 | r"""Asserts that two matrices are close to each other by bounding the matrix norm of their difference. 22 | 23 | Asserts that $\|A - B\| \le \mathrm{atol}$. 24 | """ 25 | assert A.shape == B.shape 26 | assert np.linalg.norm(A - B) <= atol 27 | 28 | 29 | def assert_unitaries_equivalent_upto_global_phase(U: NDArray, V: NDArray, *, atol: float = 1e-5): 30 | d = unitary_distance_ignoring_global_phase(U, V) 31 | assert d <= atol, f"unitaries are not equivalent: distance={d} ({atol=})" 32 | -------------------------------------------------------------------------------- /dev_tools/conf/.pylintrc: -------------------------------------------------------------------------------- 1 | [MASTER] 2 | load-plugins=pylint.extensions.docstyle,pylint.extensions.docparams,pylint_copyright_checker 3 | max-line-length=100 4 | ignore-patterns=.*_pb2\.py 5 | output-format=colorized 6 | score=no 7 | reports=no 8 | py-version=3.9 9 | disable= 10 | C, 11 | R, 12 | missing-raises-doc, 13 | unused-variable, 14 | arguments-differ, 15 | unused-argument, 16 | fixme, 17 | unspecified-encoding, 18 | abstract-class-instantiated, # buggy with attrs 19 | not-an-iterable, # buggy with attrs 20 | unsubscriptable-object, # buggy with attrs 21 | not-callable, # buggy with attrs 22 | no-member, # buggy with attrs 23 | reimported, # often relevant for 'autodoc' functionality 24 | import-self, # often relevant for 'autodoc' functionality 25 | redefined-outer-name, # triggered by re-importing 26 | abstract-method, # cirq's @value.alternative not respected 27 | protected-access, # a lot of usages in tests 28 | redundant-returns-doc 29 | enable= 30 | wrong-or-nonexistent-copyright-notice 31 | 32 | extension-pkg-whitelist=numpy 33 | 34 | # Ignore long lines containing urls or pylint directives. 35 | ignore-long-lines=^(.*#\w*pylint: disable.*|\s*(# )?[<\[\(]?https?://\S+[>\]\)]?)$ 36 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/hubbard_model/qubitization/walk_operator.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from qualtran.bloqs.block_encoding.lcu_block_encoding import SelectBlockEncoding 15 | from qualtran.bloqs.chemistry.hubbard_model.qubitization.prepare_hubbard import PrepareHubbard 16 | from qualtran.bloqs.chemistry.hubbard_model.qubitization.select_hubbard import SelectHubbard 17 | from qualtran.bloqs.qubitization.qubitization_walk_operator import QubitizationWalkOperator 18 | 19 | 20 | def get_walk_operator_for_hubbard_model( 21 | x_dim: int, y_dim: int, t: int, u: int 22 | ) -> 'QubitizationWalkOperator': 23 | select = SelectHubbard(x_dim, y_dim) 24 | prepare = PrepareHubbard(x_dim, y_dim, t, u) 25 | 26 | return QubitizationWalkOperator(SelectBlockEncoding(select=select, prepare=prepare)) 27 | -------------------------------------------------------------------------------- /qualtran/linalg/polynomial/basic_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import numpy as np 15 | 16 | from qualtran.bloqs.for_testing.matrix_gate import MatrixGate 17 | from qualtran.linalg.polynomial.basic import evaluate_polynomial_of_unitary_matrix 18 | 19 | 20 | def test_evaluate_polynomial_of_unitary_matrix(): 21 | U = np.array([[0, -1j], [1j, 0]]) 22 | 23 | V = evaluate_polynomial_of_unitary_matrix([0, 0, 1], U) 24 | np.testing.assert_allclose(V, np.eye(2)) 25 | 26 | V = evaluate_polynomial_of_unitary_matrix([1, 0, 1], U, offset=-1) 27 | np.testing.assert_allclose(V, 2 * U) 28 | 29 | U = np.array(MatrixGate.random(2, random_state=42).matrix) 30 | V = evaluate_polynomial_of_unitary_matrix([1, 2, 3], U) 31 | np.testing.assert_allclose(V, np.eye(4) + 2 * U + 3 * U @ U) 32 | -------------------------------------------------------------------------------- /qualtran/resource_counting/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # isort:skip_file 16 | """Analysis routines for computing costs and resource counts.""" 17 | 18 | from ._generalization import GeneralizerT 19 | 20 | from ._call_graph import ( 21 | BloqCountDictT, 22 | BloqCountT, 23 | big_O, 24 | MutableBloqCountDictT, 25 | SympySymbolAllocator, 26 | get_bloq_callee_counts, 27 | get_bloq_call_graph, 28 | build_cbloq_call_graph, 29 | format_call_graph_debug_text, 30 | ) 31 | 32 | from ._costing import GeneralizerT, get_cost_value, get_cost_cache, query_costs, CostKey, CostValT 33 | 34 | from ._success_prob import SuccessProb 35 | from ._qubit_counts import QubitCount 36 | from ._bloq_counts import BloqCount, QECGatesCost, GateCounts 37 | 38 | from . import generalizers 39 | -------------------------------------------------------------------------------- /qualtran/bloqs/hamiltonian_simulation/guided_hamiltonian_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2025 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import pytest 15 | 16 | import qualtran.testing as qlt_testing 17 | from qualtran.bloqs.hamiltonian_simulation.guided_hamiltonian import ( 18 | _guided_hamiltonian_symb, 19 | _guided_phase_estimate_symb, 20 | ) 21 | 22 | 23 | @pytest.mark.parametrize( 24 | "bloq_ex", 25 | [_guided_hamiltonian_symb, _guided_phase_estimate_symb], 26 | ids=lambda bloq_ex: bloq_ex.name, 27 | ) 28 | def test_examples(bloq_autotester, bloq_ex): 29 | bloq_autotester(bloq_ex) 30 | 31 | 32 | @pytest.mark.notebook 33 | def test_notebook(): 34 | qlt_testing.execute_notebook('guided_hamiltonian') 35 | 36 | 37 | @pytest.mark.notebook 38 | def test_tutorial(): 39 | qlt_testing.execute_notebook('tutorial_guided_hamiltonian') 40 | -------------------------------------------------------------------------------- /qualtran/bloqs/rotations/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from qualtran.bloqs.rotations.hamming_weight_phasing import ( 16 | HammingWeightPhasing, 17 | HammingWeightPhasingViaPhaseGradient, 18 | ) 19 | from qualtran.bloqs.rotations.phase_gradient import ( 20 | AddIntoPhaseGrad, 21 | AddScaledValIntoPhaseReg, 22 | PhaseGradientState, 23 | ) 24 | from qualtran.bloqs.rotations.phasing_via_cost_function import PhasingViaCostFunction 25 | from qualtran.bloqs.rotations.programmable_rotation_gate_array import ProgrammableRotationGateArray 26 | from qualtran.bloqs.rotations.quantum_variable_rotation import QvrPhaseGradient, QvrZPow 27 | from qualtran.bloqs.rotations.rz_via_phase_gradient import RzViaPhaseGradient 28 | from qualtran.bloqs.rotations.zpow_via_phase_gradient import ZPowConstViaPhaseGradient 29 | -------------------------------------------------------------------------------- /qualtran/drawing/musical_score_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import pytest 15 | 16 | from qualtran.bloqs.mcmt import MultiAnd 17 | from qualtran.drawing import dump_musical_score, get_musical_score_data, HLine 18 | from qualtran.testing import execute_notebook 19 | 20 | 21 | def test_dump_json(tmp_path): 22 | hline = HLine(y=10, seq_x_start=5, seq_x_end=6) 23 | assert hline.json_dict() == { 24 | 'y': 10, 25 | 'seq_x_start': 5, 26 | 'seq_x_end': 6, 27 | 'flavor': 'HLineFlavor.QUANTUM', 28 | } 29 | 30 | cbloq = MultiAnd((1, 1, 0, 1)).decompose_bloq() 31 | msd = get_musical_score_data(cbloq) 32 | dump_musical_score(msd, name=f'{tmp_path}/musical_score_example') 33 | 34 | 35 | @pytest.mark.notebook 36 | def test_notebook(): 37 | execute_notebook('musical_score') 38 | -------------------------------------------------------------------------------- /qualtran/surface_code/data_block_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | 16 | import pytest 17 | 18 | from qualtran.surface_code import FastDataBlock, LogicalErrorModel, QECScheme 19 | 20 | 21 | @pytest.mark.parametrize( 22 | ["logical_qubits", "logical_qubits_with_routing", "data_error"], 23 | [[100, 230, 0.69], [1318, 2740, 8.22], [12581, 25481, 76.443]], 24 | ) 25 | def test_fast_block(logical_qubits, logical_qubits_with_routing, data_error): 26 | assert FastDataBlock.get_n_tiles(n_algo_qubits=logical_qubits) == logical_qubits_with_routing 27 | err_model = LogicalErrorModel(qec_scheme=QECScheme.make_gidney_fowler(), physical_error=1e-3) 28 | assert FastDataBlock(3).data_error( 29 | n_algo_qubits=logical_qubits, n_cycles=3, logical_error_model=err_model 30 | ) == pytest.approx(data_error) 31 | -------------------------------------------------------------------------------- /dev_tools/requirements/re-pip-compile-in-docker.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Copyright 2023 Google LLC 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | set -ex 18 | 19 | # https://stackoverflow.com/q/59895 20 | SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)" 21 | readonly SCRIPT_DIR 22 | 23 | # We use docker to run the actual `pip-compile` command because its 24 | # behavior depends on the platform from which it is run. Please see 25 | # ./Dockerfile for the list of `pip-compile` commands that are run. 26 | docker build -t qualtran-pip-compile "$SCRIPT_DIR" 27 | 28 | # Create a container from the image so we can copy out the outputs 29 | container_id=$(docker create qualtran-pip-compile) 30 | 31 | # Copy out the files and organize them 32 | docker cp "$container_id:/pip-compile/envs" "$SCRIPT_DIR/" 33 | 34 | # Clean up 35 | docker rm -v "$container_id" 36 | -------------------------------------------------------------------------------- /dev_tools/conf/mypy.ini: -------------------------------------------------------------------------------- 1 | [mypy] 2 | show_error_codes = true 3 | plugins = duet.typing, numpy.typing.mypy_plugin 4 | allow_redefinition = true 5 | check_untyped_defs = true 6 | 7 | # Disabling function override checking 8 | # Qualtran has many places where kwargs are used 9 | # with the intention to override in subclasses in ways mypy does not like 10 | disable_error_code = override 11 | 12 | [mypy-__main__] 13 | follow_imports = silent 14 | ignore_missing_imports = true 15 | 16 | # 3rd-party libs for which we don't have stubs 17 | 18 | # Google 19 | [mypy-google.api_core.*,google.auth.*,google.colab.*,google.protobuf.text_format.*,google.cloud.*,google.*] 20 | follow_imports = silent 21 | ignore_missing_imports = true 22 | 23 | # Non-Google 24 | [mypy-sympy.*,matplotlib.*,proto.*,pandas.*,scipy.*,freezegun.*,mpl_toolkits.*,networkx.*,ply.*,astroid.*,pytest.*,_pytest.*,pylint.*,setuptools.*,qiskit.*,quimb.*,pylatex.*,filelock.*,sortedcontainers.*,tqdm.*,plotly.*,dash.*,tensorflow_docs.*,fxpmath.*,ipywidgets.*,cachetools.*,pydot.*,nbformat.*,nbconvert.*,openfermion.*,pennylane.*,mpmath.*] 25 | follow_imports = silent 26 | ignore_missing_imports = true 27 | 28 | [mypy-sympy.*] 29 | follow_untyped_imports = True 30 | 31 | # Treat symbols imported from Google's protobuf library as type Any. 32 | # This suppresses errors due to attributes not known to typeshed, 33 | # e.g. Descriptor._options. 34 | [mypy-google.protobuf.*] 35 | follow_imports = skip 36 | follow_imports_for_stubs = true 37 | -------------------------------------------------------------------------------- /qualtran/drawing/classical_sim_graph_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import re 15 | 16 | import numpy as np 17 | 18 | from qualtran.bloqs.mcmt.and_bloq import MultiAnd 19 | from qualtran.drawing import ClassicalSimGraphDrawer 20 | 21 | 22 | def test_classical_sim_graph(): 23 | gd = ClassicalSimGraphDrawer( 24 | bloq=MultiAnd((1, 1, 1, 1)).decompose_bloq(), vals=dict(ctrl=np.array([1, 1, 0, 1])) 25 | ) 26 | 27 | # The main test is in the drawing notebook, so please spot check that. 28 | # Here: we make sure the edge labels are "0" and "1" for MultiAnd. 29 | dot_lines = gd.get_graph().to_string().splitlines() 30 | edge_lines = [line for line in dot_lines if '->' in line] 31 | for line in edge_lines: 32 | ma = re.search(r'label=(\w+)', line) 33 | assert ma is not None, line 34 | i = int(ma.group(1)) 35 | assert i in [0, 1] 36 | -------------------------------------------------------------------------------- /dev_tools/execute-notebooks.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import argparse 16 | 17 | from qualtran_dev_tools.notebook_execution import execute_and_export_notebooks 18 | 19 | 20 | def parse_args(): 21 | p = argparse.ArgumentParser() 22 | p.add_argument('--output-nbs', action=argparse.BooleanOptionalAction, default=True) 23 | p.add_argument('--output-md', action=argparse.BooleanOptionalAction, default=False) 24 | p.add_argument('--only-out-of-date', action=argparse.BooleanOptionalAction, default=True) 25 | p.add_argument('--n-workers', type=int, default=None) 26 | args = p.parse_args() 27 | execute_and_export_notebooks( 28 | output_nbs=args.output_nbs, 29 | output_md=args.output_md, 30 | only_out_of_date=args.only_out_of_date, 31 | n_workers=args.n_workers, 32 | ) 33 | 34 | 35 | if __name__ == '__main__': 36 | parse_args() 37 | -------------------------------------------------------------------------------- /qualtran/serialization/ec_point.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from qualtran.bloqs.cryptography.ecc import ECPoint 16 | from qualtran.protos import ec_point_pb2 17 | from qualtran.serialization.args import int_or_sympy_from_proto, int_or_sympy_to_proto 18 | 19 | 20 | def ec_point_from_proto(point: ec_point_pb2.ECPoint) -> ECPoint: 21 | return ECPoint( 22 | x=int_or_sympy_from_proto(point.x), 23 | y=int_or_sympy_from_proto(point.y), 24 | mod=int_or_sympy_from_proto(point.mod), 25 | curve_a=int_or_sympy_from_proto(point.curve_a), 26 | ) 27 | 28 | 29 | def ec_point_to_proto(point: ECPoint) -> ec_point_pb2.ECPoint: 30 | return ec_point_pb2.ECPoint( 31 | x=int_or_sympy_to_proto(point.x), 32 | y=int_or_sympy_to_proto(point.y), 33 | mod=int_or_sympy_to_proto(point.mod), 34 | curve_a=int_or_sympy_to_proto(point.curve_a), 35 | ) 36 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/hubbard_model/qubitization/prepare_hubbard_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import pytest 16 | 17 | from qualtran.bloqs.chemistry.hubbard_model.qubitization.prepare_hubbard import ( 18 | _prep_hubb, 19 | PrepareHubbard, 20 | ) 21 | from qualtran.cirq_interop.t_complexity_protocol import t_complexity 22 | 23 | 24 | def test_prep_hubb_auto(bloq_autotester): 25 | bloq_autotester(_prep_hubb) 26 | 27 | 28 | @pytest.mark.parametrize('dim', [*range(3, 10)]) 29 | def test_prepare_t_complexity(dim): 30 | prepare = PrepareHubbard(x_dim=dim, y_dim=dim, t=2, u=8) 31 | cost = t_complexity(prepare) 32 | logN = 2 * (dim - 1).bit_length() + 1 33 | assert cost.t <= 32 * logN 34 | # TODO(#233): The rotation count should reduce to a constant once cost for Controlled-H 35 | # gates is recognized as $2$ T-gates instead of $2$ rotations. 36 | assert cost.rotations <= 2 * logN + 9 37 | -------------------------------------------------------------------------------- /qualtran/protos/ctrl_spec_pb2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Generated by the protocol buffer compiler. DO NOT EDIT! 3 | # source: qualtran/protos/ctrl_spec.proto 4 | # Protobuf Python Version: 5.26.1 5 | """Generated protocol buffer code.""" 6 | from google.protobuf import descriptor as _descriptor 7 | from google.protobuf import descriptor_pool as _descriptor_pool 8 | from google.protobuf import symbol_database as _symbol_database 9 | from google.protobuf.internal import builder as _builder 10 | # @@protoc_insertion_point(imports) 11 | 12 | _sym_db = _symbol_database.Default() 13 | 14 | 15 | from qualtran.protos import data_types_pb2 as qualtran_dot_protos_dot_data__types__pb2 16 | from qualtran.protos import args_pb2 as qualtran_dot_protos_dot_args__pb2 17 | 18 | 19 | DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x1fqualtran/protos/ctrl_spec.proto\x12\x08qualtran\x1a qualtran/protos/data_types.proto\x1a\x1aqualtran/protos/args.proto\"P\n\x08\x43trlSpec\x12$\n\x07qdtypes\x18\x01 \x03(\x0b\x32\x13.qualtran.QDataType\x12\x1e\n\x03\x63vs\x18\x02 \x03(\x0b\x32\x11.qualtran.NDArrayb\x06proto3') 20 | 21 | _globals = globals() 22 | _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) 23 | _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'qualtran.protos.ctrl_spec_pb2', _globals) 24 | if not _descriptor._USE_C_DESCRIPTORS: 25 | DESCRIPTOR._loaded_options = None 26 | _globals['_CTRLSPEC']._serialized_start=107 27 | _globals['_CTRLSPEC']._serialized_end=187 28 | # @@protoc_insertion_point(module_scope) 29 | -------------------------------------------------------------------------------- /qualtran/bloqs/for_testing/with_call_graph.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from functools import cached_property 16 | from typing import TYPE_CHECKING 17 | 18 | from attrs import frozen 19 | 20 | from qualtran import Bloq, Signature 21 | from qualtran.bloqs.for_testing.atom import TestAtom 22 | from qualtran.bloqs.for_testing.with_decomposition import TestParallelCombo, TestSerialCombo 23 | 24 | if TYPE_CHECKING: 25 | from qualtran.resource_counting import BloqCountDictT, SympySymbolAllocator 26 | 27 | 28 | @frozen 29 | class TestBloqWithCallGraph(Bloq): 30 | """A bloq that declares its callees (only).""" 31 | 32 | @cached_property 33 | def signature(self) -> Signature: 34 | return Signature.build() 35 | 36 | def build_call_graph(self, ssa: 'SympySymbolAllocator') -> 'BloqCountDictT': 37 | n = ssa.new_symbol('n') 38 | return {TestParallelCombo(): 1, TestSerialCombo(): 1, TestAtom(): n} 39 | -------------------------------------------------------------------------------- /qualtran/bloqs/mcmt/classically_controlled_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2025 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from qualtran import CBit, CtrlSpec 16 | from qualtran.bloqs.basic_gates import XGate 17 | from qualtran.bloqs.mcmt.classically_controlled import ClassicallyControlled 18 | 19 | 20 | def test_controlled_x_simulated() -> None: 21 | bloq = XGate() 22 | 23 | ctrl_bloq = ClassicallyControlled(bloq, CtrlSpec(CBit(), cvs=1)) 24 | assert len(ctrl_bloq.ctrl_reg_names) == 1 25 | inputs = {ctrl_bloq.ctrl_reg_names[0]: 0, 'q': 0} 26 | assert ctrl_bloq.call_classically(**inputs) == (0, 0) 27 | inputs = {ctrl_bloq.ctrl_reg_names[0]: 0, 'q': 1} 28 | assert ctrl_bloq.call_classically(**inputs) == (0, 1) 29 | inputs = {ctrl_bloq.ctrl_reg_names[0]: 1, 'q': 0} 30 | assert ctrl_bloq.call_classically(**inputs) == (1, 1) 31 | inputs = {ctrl_bloq.ctrl_reg_names[0]: 1, 'q': 1} 32 | assert ctrl_bloq.call_classically(**inputs) == (1, 0) 33 | -------------------------------------------------------------------------------- /qualtran/cirq_interop/_interop_qubit_manager_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import cirq 15 | import pytest 16 | 17 | from qualtran.cirq_interop._interop_qubit_manager import InteropQubitManager 18 | 19 | 20 | @pytest.mark.parametrize('base_qm', [cirq.GreedyQubitManager('anc'), cirq.SimpleQubitManager()]) 21 | def test_interop_qubit_manager(base_qm): 22 | qm = InteropQubitManager(base_qm) 23 | # qm delegates allocation / de-allocation requests to base_qm. 24 | q = qm.qalloc(1) 25 | qm.qfree(q) 26 | q = cirq.q('junk') 27 | # q is not managed and was not allocated by base_qm. 28 | with pytest.raises((ValueError, AssertionError)): 29 | qm.qfree([q]) 30 | # You can delegate qubits to be "managed" by the InteropQubitManager. 31 | qm.manage_qubits([q]) 32 | qm.qfree([q]) 33 | # q was already deallocated. 34 | with pytest.raises((ValueError, AssertionError)): 35 | qm.qfree([q]) 36 | -------------------------------------------------------------------------------- /qualtran/linalg/permutation_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from qualtran.linalg.permutation import ( 15 | decompose_permutation_into_cycles, 16 | decompose_permutation_map_into_cycles, 17 | ) 18 | 19 | 20 | def test_decompose_permutation_into_cycles(): 21 | assert list(decompose_permutation_into_cycles([0, 1, 2])) == [] 22 | assert list(decompose_permutation_into_cycles([1, 2, 0])) == [(0, 1, 2)] 23 | assert sorted(decompose_permutation_into_cycles([1, 0, 2, 4, 5, 3])) == [(0, 1), (3, 4, 5)] 24 | 25 | 26 | def test_decompose_sparse_prefix_permutation_into_cycles(): 27 | assert list(decompose_permutation_map_into_cycles({0: 1, 1: 20})) == [(0, 1, 20)] 28 | assert sorted(decompose_permutation_map_into_cycles({0: 30, 1: 50})) == [(0, 30), (1, 50)] 29 | assert list(decompose_permutation_map_into_cycles({0: 0})) == [] 30 | assert list(decompose_permutation_map_into_cycles({0: 1, 1: 0})) == [(0, 1)] 31 | -------------------------------------------------------------------------------- /qualtran/serialization/args_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import numpy as np 16 | import pytest 17 | import sympy 18 | 19 | from qualtran.serialization import args 20 | 21 | 22 | @pytest.mark.parametrize('arg', [1, sympy.Symbol('a') * sympy.Symbol('b') + sympy.Symbol('c') / 10]) 23 | def test_int_or_sympy_to_proto(arg): 24 | proto = args.int_or_sympy_to_proto(arg) 25 | arg_from_proto = args.int_or_sympy_from_proto(proto) 26 | assert arg_from_proto == arg 27 | 28 | 29 | def test_ndarray_to_proto(): 30 | x = np.random.random(100) 31 | proto = args.ndarray_to_proto(x) 32 | x_from_proto = args.ndarray_from_proto(proto) 33 | assert np.allclose(x, x_from_proto) 34 | 35 | 36 | @pytest.mark.parametrize('arg', [1j, np.complex64(1j)]) 37 | def test_complex_to_proto(arg): 38 | proto = args.complex_to_proto(arg) 39 | arg_from_proto = args.complex_from_proto(proto) 40 | assert arg_from_proto == arg 41 | -------------------------------------------------------------------------------- /.github/problem-matchers/README.md: -------------------------------------------------------------------------------- 1 | # Problem Matchers 2 | 3 | GitHub [Problem 4 | Matchers](https://github.com/actions/toolkit/blob/main/docs/problem-matchers.md) 5 | are a mechanism that enable workflow steps to scan the outputs of GitHub 6 | Actions for regex patterns and automatically write annotations in the workflow 7 | summary page. Using Problem Matchers allows information to be displayed more 8 | prominently in the GitHub user interface. 9 | 10 | This directory contains Problem Matchers used by the GitHub Actions workflows 11 | in the [`workflows`](./workflows) subdirectory. 12 | 13 | The following problem matcher JSON files found in this directory were copied 14 | from the [Home Assistant](https://github.com/home-assistant/core) project on 15 | GitHub. The Home Assistant project is licensed under the Apache 2.0 open-source 16 | license. The versions of the file at the time it was copied was 2025.1.2. 17 | 18 | - [`pylint.json`](https://github.com/home-assistant/core/blob/dev/.github/workflows/matchers/pylint.json) 19 | - [`mypy.json`](https://github.com/home-assistant/core/blob/dev/.github/workflows/matchers/pypy.json) 20 | 21 | The following problem matcher for Black came from a fork of the 22 | [MLflow](https://github.com/mlflow/mlflow) project by user Sumanth077 on 23 | GitHub. The MLflow project is licensed under the Apache 2.0 open-source 24 | license. The version of the file copied was dated 2022-05-29. 25 | 26 | - [`black.json`](https://github.com/Sumanth077/mlflow/blob/problem-matcher-for-black/.github/workflows/matchers/black.json) 27 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/pbc/first_quantization/select_uv_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from qualtran.bloqs.chemistry.pbc.first_quantization.select_uv import ( 15 | _select_uv, 16 | SelectUVFirstQuantization, 17 | ) 18 | from qualtran.resource_counting import get_cost_value, QECGatesCost 19 | 20 | 21 | def test_select_uv(bloq_autotester): 22 | bloq_autotester(_select_uv) 23 | 24 | 25 | def test_select_uv_toffoli_counts(): 26 | num_bits_p = 6 27 | eta = 10 28 | num_bits_nuc_pos = 8 29 | expected_cost = 24 * num_bits_p + 3 * ( 30 | 2 * num_bits_p * num_bits_nuc_pos - num_bits_p * (num_bits_p + 1) - 1 31 | ) 32 | sel = SelectUVFirstQuantization(num_bits_p, eta, eta, num_bits_nuc_pos) 33 | qual_cost = get_cost_value(sel, QECGatesCost()).total_toffoli_only() 34 | # + 6 as they cost additon as nbits not nbits - 1, there are 6 additions / subtractions. 35 | assert qual_cost + 6 == expected_cost 36 | -------------------------------------------------------------------------------- /qualtran/protos/ec_point_pb2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Generated by the protocol buffer compiler. DO NOT EDIT! 3 | # source: qualtran/protos/ec_point.proto 4 | # Protobuf Python Version: 4.25.3 5 | """Generated protocol buffer code.""" 6 | from google.protobuf import descriptor as _descriptor 7 | from google.protobuf import descriptor_pool as _descriptor_pool 8 | from google.protobuf import symbol_database as _symbol_database 9 | from google.protobuf.internal import builder as _builder 10 | # @@protoc_insertion_point(imports) 11 | 12 | _sym_db = _symbol_database.Default() 13 | 14 | 15 | from qualtran.protos import args_pb2 as qualtran_dot_protos_dot_args__pb2 16 | 17 | 18 | DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x1equaltran/protos/ec_point.proto\x12\x08qualtran\x1a\x1aqualtran/protos/args.proto\"\xa6\x01\n\x07\x45\x43Point\x12\x1f\n\x01x\x18\x01 \x01(\x0b\x32\x14.qualtran.IntOrSympy\x12\x1f\n\x01y\x18\x02 \x01(\x0b\x32\x14.qualtran.IntOrSympy\x12!\n\x03mod\x18\x03 \x01(\x0b\x32\x14.qualtran.IntOrSympy\x12*\n\x07\x63urve_a\x18\x04 \x01(\x0b\x32\x14.qualtran.IntOrSympyH\x00\x88\x01\x01\x42\n\n\x08_curve_ab\x06proto3') 19 | 20 | _globals = globals() 21 | _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) 22 | _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'qualtran.protos.ec_point_pb2', _globals) 23 | if _descriptor._USE_C_DESCRIPTORS == False: 24 | DESCRIPTOR._options = None 25 | _globals['_ECPOINT']._serialized_start=73 26 | _globals['_ECPOINT']._serialized_end=239 27 | # @@protoc_insertion_point(module_scope) 28 | -------------------------------------------------------------------------------- /qualtran/drawing/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # isort:skip_file 16 | 17 | """Draw and visualize bloqs.""" 18 | 19 | from .graphviz import GraphDrawer, PrettyGraphDrawer, TypedGraphDrawer 20 | from .musical_score import ( 21 | RegPosition, 22 | HLine, 23 | VLine, 24 | LineManager, 25 | WireSymbol, 26 | TextBox, 27 | Text, 28 | RarrowTextBox, 29 | LarrowTextBox, 30 | directional_text_box, 31 | Circle, 32 | ModPlus, 33 | MusicalScoreData, 34 | get_musical_score_data, 35 | draw_musical_score, 36 | dump_musical_score, 37 | ) 38 | 39 | from .classical_sim_graph import ClassicalSimGraphDrawer 40 | 41 | from .bloq_counts_graph import GraphvizCallGraph, format_counts_sigma, format_counts_graph_markdown 42 | 43 | from ._show_funcs import ( 44 | show_bloq, 45 | show_bloqs, 46 | show_call_graph, 47 | show_counts_sigma, 48 | show_flame_graph, 49 | show_bloq_via_qpic, 50 | ) 51 | -------------------------------------------------------------------------------- /qualtran/serialization/ctrl_spec.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from qualtran import CtrlSpec 15 | from qualtran.protos import ctrl_spec_pb2 16 | from qualtran.serialization import args, data_types 17 | from qualtran.symbolics import Shaped 18 | 19 | 20 | def ctrl_spec_from_proto(spec: ctrl_spec_pb2.CtrlSpec) -> CtrlSpec: 21 | return CtrlSpec( 22 | qdtypes=[data_types.data_type_from_proto(dtype) for dtype in spec.qdtypes], 23 | cvs=[args.ndarray_from_proto(cvs) for cvs in spec.cvs], 24 | ) 25 | 26 | 27 | def ctrl_spec_to_proto(spec: CtrlSpec) -> ctrl_spec_pb2.CtrlSpec: 28 | def cvs_to_proto(cvs): 29 | if isinstance(cvs, Shaped): 30 | raise ValueError("cannot serialize Shaped") 31 | return args.ndarray_to_proto(cvs) 32 | 33 | return ctrl_spec_pb2.CtrlSpec( 34 | qdtypes=[data_types.data_type_to_proto(dtype) for dtype in spec.qdtypes], 35 | cvs=[cvs_to_proto(cvs) for cvs in spec.cvs], 36 | ) 37 | -------------------------------------------------------------------------------- /qualtran/bloqs/bookkeeping/allocate_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import numpy as np 16 | import pytest 17 | 18 | from qualtran import BloqBuilder, QAny 19 | from qualtran.bloqs.bookkeeping import Allocate, Free 20 | from qualtran.bloqs.bookkeeping.allocate import _alloc 21 | from qualtran.testing import execute_notebook 22 | 23 | 24 | def test_alloc(bloq_autotester): 25 | bloq_autotester(_alloc) 26 | 27 | 28 | def test_free_nonzero_state_vector_leads_to_unnormalized_state(): 29 | from qualtran.bloqs.basic_gates.hadamard import Hadamard 30 | from qualtran.bloqs.basic_gates.on_each import OnEach 31 | 32 | bb = BloqBuilder() 33 | qs1 = bb.add(Allocate(QAny(10))) 34 | qs2 = bb.add(OnEach(10, Hadamard()), q=qs1) 35 | no_return = bb.add(Free(QAny(10)), reg=qs2) 36 | assert np.allclose(bb.finalize().tensor_contract(), np.sqrt(1 / 2**10)) 37 | 38 | 39 | @pytest.mark.notebook 40 | def test_notebook(): 41 | execute_notebook('allocate') 42 | -------------------------------------------------------------------------------- /.yamllint.yaml: -------------------------------------------------------------------------------- 1 | # Copyright 2025 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 16 | # Yamllint configuration to match project settings like line length. 17 | # See https://yamllint.readthedocs.io/ for info about configuration options. 18 | # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 19 | 20 | rules: 21 | line-length: 22 | # YAML files (especially GitHub Actions workflows) tend to end up with 23 | # long lines. The default of 80 is pretty limiting, and besides, in Python 24 | # code linting, we set line lengths to 100. May as well follow suit here. 25 | max: 100 26 | # Another common occurrence in YAML files is long URLs. The next two 27 | # settings are not specific to URLs, but help. It saves developer time by 28 | # not requiring comment directives to disable warnings at every occurrence. 29 | allow-non-breakable-words: true 30 | allow-non-breakable-inline-mappings: true 31 | -------------------------------------------------------------------------------- /qualtran/bloqs/block_encoding/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | r"""High level bloqs for defining bloq encodings and operations on block encodings.""" 15 | 16 | from qualtran.bloqs.block_encoding.block_encoding_base import BlockEncoding 17 | from qualtran.bloqs.block_encoding.chebyshev_polynomial import ( 18 | ChebyshevPolynomial, 19 | ScaledChebyshevPolynomial, 20 | ) 21 | from qualtran.bloqs.block_encoding.lcu_block_encoding import LCUBlockEncoding, SelectBlockEncoding 22 | from qualtran.bloqs.block_encoding.linear_combination import LinearCombination 23 | from qualtran.bloqs.block_encoding.phase import Phase 24 | from qualtran.bloqs.block_encoding.product import Product 25 | from qualtran.bloqs.block_encoding.sparse_matrix import SparseMatrix 26 | from qualtran.bloqs.block_encoding.sparse_matrix_hermitian import SparseMatrixHermitian 27 | from qualtran.bloqs.block_encoding.tensor_product import TensorProduct 28 | from qualtran.bloqs.block_encoding.unitary import Unitary 29 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/trotter/hubbard/trotter_step_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import pytest 15 | 16 | from qualtran.bloqs.chemistry.trotter.hubbard.trotter_step import ( 17 | build_plaq_unitary_second_order_suzuki, 18 | ) 19 | from qualtran.resource_counting import get_cost_value, QECGatesCost 20 | from qualtran.testing import execute_notebook 21 | 22 | 23 | def test_second_order_suzuki_costs(): 24 | length = 8 25 | u = 4 26 | dt = 0.1234 27 | unitary = build_plaq_unitary_second_order_suzuki(length, u, dt) 28 | costs = get_cost_value(unitary, QECGatesCost()) 29 | # there are 3 hopping unitaries contributing 8 Ts from from the F gate 30 | assert costs.total_t_count(ts_per_rotation=0) == (3 * length**2 // 2) * 8 31 | # 3 hopping unitaries and 2 interaction unitaries 32 | assert costs.rotation == (3 * length**2 + 2 * length**2) 33 | 34 | 35 | @pytest.mark.notebook 36 | def test_notebook(): 37 | execute_notebook('qpe_cost_optimization') 38 | -------------------------------------------------------------------------------- /qualtran/protos/args_pb2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Generated by the protocol buffer compiler. DO NOT EDIT! 3 | # source: qualtran/protos/args.proto 4 | # Protobuf Python Version: 5.26.1 5 | """Generated protocol buffer code.""" 6 | from google.protobuf import descriptor as _descriptor 7 | from google.protobuf import descriptor_pool as _descriptor_pool 8 | from google.protobuf import symbol_database as _symbol_database 9 | from google.protobuf.internal import builder as _builder 10 | # @@protoc_insertion_point(imports) 11 | 12 | _sym_db = _symbol_database.Default() 13 | 14 | 15 | 16 | 17 | DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x1aqualtran/protos/args.proto\x12\x08qualtran\"<\n\nIntOrSympy\x12\x11\n\x07int_val\x18\x01 \x01(\x03H\x00\x12\x14\n\nsympy_expr\x18\x02 \x01(\tH\x00\x42\x05\n\x03val\"\x1a\n\x07NDArray\x12\x0f\n\x07ndarray\x18\x01 \x01(\x0c\"%\n\x07\x43omplex\x12\x0c\n\x04real\x18\x01 \x01(\x02\x12\x0c\n\x04imag\x18\x02 \x01(\x02\x62\x06proto3') 18 | 19 | _globals = globals() 20 | _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) 21 | _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'qualtran.protos.args_pb2', _globals) 22 | if not _descriptor._USE_C_DESCRIPTORS: 23 | DESCRIPTOR._loaded_options = None 24 | _globals['_INTORSYMPY']._serialized_start=40 25 | _globals['_INTORSYMPY']._serialized_end=100 26 | _globals['_NDARRAY']._serialized_start=102 27 | _globals['_NDARRAY']._serialized_end=128 28 | _globals['_COMPLEX']._serialized_start=130 29 | _globals['_COMPLEX']._serialized_end=167 30 | # @@protoc_insertion_point(module_scope) 31 | -------------------------------------------------------------------------------- /qualtran/linalg/polynomial/basic.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from typing import Sequence 15 | 16 | import numpy as np 17 | from numpy.typing import NDArray 18 | 19 | 20 | def evaluate_polynomial_of_unitary_matrix( 21 | P: Sequence[complex], U: NDArray, *, offset: int = 0 22 | ) -> NDArray: 23 | r"""Computes $U^{\mathsf{offset}} P(U)$ for a polynomial P and unitary U. 24 | 25 | Args: 26 | P: Coefficients of a complex polynomial (P[i] is the coefficient of $x^i$) 27 | U: Unitary matrix 28 | offset: the power offset $U^\mathsf{offset}$ to multiply the result by. 29 | """ 30 | assert U.ndim == 2 and U.shape[0] == U.shape[1] 31 | 32 | if offset < 0: 33 | pow_U = np.linalg.matrix_power(U.conj().T, -offset) 34 | else: 35 | pow_U = np.linalg.matrix_power(U, offset) 36 | 37 | result = np.zeros(U.shape, dtype=U.dtype) 38 | 39 | for c in P: 40 | result += pow_U * c 41 | pow_U = pow_U @ U 42 | 43 | return result 44 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/trotter/hubbard/interaction_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from qualtran.bloqs.chemistry.trotter.hubbard.interaction import _interaction, _interaction_hwp 16 | from qualtran.resource_counting import get_cost_value, QECGatesCost 17 | 18 | 19 | def test_hopping_tile(bloq_autotester): 20 | bloq_autotester(_interaction) 21 | 22 | 23 | def test_interaction_hwp(bloq_autotester): 24 | bloq_autotester(_interaction_hwp) 25 | 26 | 27 | def test_interaction_hwp_bloq_counts(): 28 | bloq = _interaction_hwp() 29 | costs = get_cost_value(bloq, QECGatesCost()) 30 | n_rot_par = bloq.length**2 // 2 31 | assert costs.rotation == 2 * n_rot_par.bit_length() 32 | assert costs.total_t_count(ts_per_rotation=0) == 2 * 4 * (n_rot_par - n_rot_par.bit_count()) 33 | 34 | 35 | def test_interaction_bloq_counts(): 36 | bloq = _interaction() 37 | costs = get_cost_value(bloq, QECGatesCost()) 38 | n_rot = bloq.length**2 39 | assert costs.rotation == n_rot 40 | -------------------------------------------------------------------------------- /qualtran/bloqs/gf_arithmetic/gf2_add_k_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import pytest 16 | from galois import GF 17 | 18 | from qualtran.bloqs.gf_arithmetic.gf2_add_k import _gf2_add_k_symbolic, _gf16_add_k, GF2AddK 19 | from qualtran.testing import assert_consistent_classical_action 20 | 21 | 22 | def test_gf16_add_k(bloq_autotester): 23 | bloq_autotester(_gf16_add_k) 24 | 25 | 26 | def test_gf2_add_k_symbolic(bloq_autotester): 27 | bloq_autotester(_gf2_add_k_symbolic) 28 | 29 | 30 | def test_gf2_add_k_classical_sim_quick(): 31 | m = 2 32 | GFM = GF(2**m) 33 | for k in GFM.elements: 34 | bloq = GF2AddK(m, int(k)) 35 | assert_consistent_classical_action(bloq, x=GFM.elements) 36 | 37 | 38 | @pytest.mark.slow 39 | @pytest.mark.parametrize('m', [3, 4, 5]) 40 | def test_gf2_add_k_classical_sim(m): 41 | GFM = GF(2**m) 42 | for k in GFM.elements: 43 | bloq = GF2AddK(m, int(k)) 44 | assert_consistent_classical_action(bloq, x=GFM.elements) 45 | -------------------------------------------------------------------------------- /qualtran/bloqs/for_testing/with_call_graph_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from typing import Optional 15 | 16 | import attrs 17 | import pytest 18 | 19 | from qualtran import Bloq 20 | from qualtran.bloqs.for_testing import TestAtom, TestBloqWithCallGraph 21 | 22 | 23 | def test_test_bloq_with_call_graph(): 24 | bwcg = TestBloqWithCallGraph() 25 | 26 | def all_atoms_the_same(b: Bloq) -> Optional[Bloq]: 27 | if isinstance(b, TestAtom): 28 | return attrs.evolve(b, tag=None) 29 | return b 30 | 31 | g, sigma = bwcg.call_graph(generalizer=all_atoms_the_same) 32 | assert len(sigma) == 3 33 | assert g.number_of_edges() == (3 + 2 + 2) # level 1 + level 2 + split/join 34 | 35 | 36 | def test_no_decomp_classical_simulation(): 37 | bwcg = TestBloqWithCallGraph() 38 | with pytest.raises( 39 | NotImplementedError, 40 | match=r'.*has no decomposition and does not support classical simulation', 41 | ): 42 | bwcg.call_classically() 43 | -------------------------------------------------------------------------------- /qualtran/dtype/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2025 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # isort:skip_file 15 | 16 | """Data type objects for your quantum programs.""" 17 | 18 | from qualtran._infra.data_types import ( 19 | QCDType, 20 | CDType, 21 | QDType, 22 | QAny, 23 | QBit, 24 | CBit, 25 | QInt, 26 | CInt, 27 | QIntOnesComp, 28 | CIntOnesComp, 29 | QUInt, 30 | CUInt, 31 | BQUInt, 32 | BCUInt, 33 | QFxp, 34 | CFxp, 35 | QMontgomeryUInt, 36 | CMontgomeryUInt, 37 | QGF, 38 | CGF, 39 | QGFPoly, 40 | CGFPoly, 41 | ) 42 | 43 | __all__ = [ 44 | 'QCDType', 45 | 'CDType', 46 | 'QDType', 47 | 'QAny', 48 | 'QBit', 49 | 'CBit', 50 | 'QInt', 51 | 'CInt', 52 | 'QIntOnesComp', 53 | 'CIntOnesComp', 54 | 'QUInt', 55 | 'CUInt', 56 | 'BQUInt', 57 | 'BCUInt', 58 | 'QFxp', 59 | 'CFxp', 60 | 'QMontgomeryUInt', 61 | 'CMontgomeryUInt', 62 | 'QGF', 63 | 'CGF', 64 | 'QGFPoly', 65 | 'CGFPoly', 66 | ] 67 | -------------------------------------------------------------------------------- /dev_tools/check-incremental-coverage-annotations.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import os 16 | import sys 17 | 18 | from qualtran_dev_tools import shell_tools 19 | from qualtran_dev_tools.incremental_coverage import check_for_uncovered_lines 20 | from qualtran_dev_tools.prepared_env import PreparedEnv 21 | 22 | 23 | def main(): 24 | if len(sys.argv) < 2: 25 | print( 26 | shell_tools.highlight( 27 | 'Must specify a comparison branch (e.g. "origin/master" or "HEAD~1").', 28 | shell_tools.RED, 29 | ) 30 | ) 31 | sys.exit(1) 32 | comparison_branch = sys.argv[1] 33 | 34 | env = PreparedEnv( 35 | actual_commit_id=None, # local uncommitted files 36 | compare_commit_id=comparison_branch, 37 | destination_directory=os.getcwd(), 38 | ) 39 | 40 | uncovered_count = check_for_uncovered_lines(env) 41 | if uncovered_count: 42 | sys.exit(1) 43 | 44 | 45 | if __name__ == "__main__": 46 | main() 47 | -------------------------------------------------------------------------------- /qualtran/protos/annotations_pb2.pyi: -------------------------------------------------------------------------------- 1 | """ 2 | @generated by mypy-protobuf. Do not edit manually! 3 | isort:skip_file 4 | 5 | Copyright 2023 Google LLC 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | https://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | """ 19 | 20 | import builtins 21 | import google.protobuf.descriptor 22 | import google.protobuf.message 23 | import typing 24 | 25 | DESCRIPTOR: google.protobuf.descriptor.FileDescriptor 26 | 27 | @typing.final 28 | class TComplexity(google.protobuf.message.Message): 29 | DESCRIPTOR: google.protobuf.descriptor.Descriptor 30 | 31 | CLIFFORD_FIELD_NUMBER: builtins.int 32 | ROTATIONS_FIELD_NUMBER: builtins.int 33 | T_FIELD_NUMBER: builtins.int 34 | clifford: builtins.int 35 | rotations: builtins.int 36 | t: builtins.int 37 | def __init__( 38 | self, 39 | *, 40 | clifford: builtins.int = ..., 41 | rotations: builtins.int = ..., 42 | t: builtins.int = ..., 43 | ) -> None: ... 44 | def ClearField(self, field_name: typing.Literal["clifford", b"clifford", "rotations", b"rotations", "t", b"t"]) -> None: ... 45 | 46 | global___TComplexity = TComplexity 47 | -------------------------------------------------------------------------------- /qualtran/bloqs/state_preparation/black_box_prepare_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from typing import Tuple 16 | 17 | from attr import frozen 18 | 19 | from qualtran import QAny, QBit, Register 20 | from qualtran.bloqs.state_preparation.black_box_prepare import _black_box_prepare, BlackBoxPrepare 21 | from qualtran.bloqs.state_preparation.prepare_base import PrepareOracle 22 | 23 | 24 | def test_black_box_prepare(bloq_autotester): 25 | bloq_autotester(_black_box_prepare) 26 | 27 | 28 | @frozen 29 | class TestPrepareOracle(PrepareOracle): 30 | @property 31 | def selection_registers(self) -> Tuple[Register, ...]: 32 | return (Register('z', QBit()),) 33 | 34 | @property 35 | def junk_registers(self) -> Tuple[Register, ...]: 36 | return (Register('a', QAny(5)),) 37 | 38 | 39 | def test_prepare_oracle(): 40 | bloq = BlackBoxPrepare(TestPrepareOracle()) 41 | assert bloq.selection_registers == (Register('selection', QAny(1)),) 42 | assert bloq.junk_registers == (Register('junk', QAny(5)),) 43 | _ = bloq.decompose_bloq() 44 | -------------------------------------------------------------------------------- /qualtran/bloqs/qft/two_bit_ffft_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import itertools 15 | 16 | import cirq 17 | import pytest 18 | 19 | from qualtran import Bloq 20 | from qualtran.bloqs.basic_gates import TGate 21 | from qualtran.bloqs.qft.two_bit_ffft import _fkn_matrix, _two_bit_ffft, TwoBitFFFT 22 | 23 | 24 | def test_t_gate(bloq_autotester): 25 | bloq_autotester(_two_bit_ffft) 26 | 27 | 28 | def test_call_graph(): 29 | def all_t(bloq) -> Bloq: 30 | if isinstance(bloq, TGate): 31 | return TGate() 32 | return bloq 33 | 34 | assert ( 35 | TwoBitFFFT(2, 3).call_graph(generalizer=all_t)[1] 36 | == TwoBitFFFT(2, 3).decompose_bloq().call_graph(generalizer=all_t)[1] 37 | ) 38 | 39 | 40 | @pytest.mark.parametrize('k,n', itertools.product(range(0, 4), range(1, 4))) 41 | def test_tensors(k, n): 42 | # Eq. E11 in https://arxiv.org/pdf/2012.09238.pdf 43 | from_decomp = TwoBitFFFT(k, n).decompose_bloq().tensor_contract() 44 | cirq.testing.assert_allclose_up_to_global_phase(from_decomp, _fkn_matrix(k, n), atol=1e-12) 45 | -------------------------------------------------------------------------------- /qualtran/bloqs/for_testing/interior_alloc.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from functools import cached_property 15 | from typing import Dict, Union 16 | 17 | import sympy 18 | from attrs import frozen 19 | 20 | from qualtran import Bloq, BloqBuilder, Signature, Soquet, SoquetT 21 | from qualtran.bloqs.basic_gates.swap import Swap 22 | 23 | 24 | @frozen 25 | class InteriorAlloc(Bloq): 26 | """A bloq that performs an allocation and de-allocation on an interior wire. 27 | 28 | This means the maximum number of qubits used is larger than the sum of the register bitsizes. 29 | """ 30 | 31 | n: Union[int, sympy.Symbol] 32 | 33 | @cached_property 34 | def signature(self) -> 'Signature': 35 | return Signature.build(x=self.n, y=self.n) 36 | 37 | def build_composite_bloq(self, bb: 'BloqBuilder', x: Soquet, y: Soquet) -> Dict[str, 'SoquetT']: 38 | middle = bb.allocate(self.n) 39 | x, middle = bb.add(Swap(self.n), x=x, y=middle) 40 | middle, y = bb.add(Swap(self.n), x=middle, y=y) 41 | bb.free(middle) 42 | return {'x': x, 'y': y} 43 | -------------------------------------------------------------------------------- /qualtran/bloqs/chemistry/trotter/hubbard/hopping_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from qualtran.bloqs.chemistry.trotter.hubbard.hopping import ( 15 | _hopping_tile, 16 | _hopping_tile_hwp, 17 | _plaquette, 18 | ) 19 | from qualtran.resource_counting import get_cost_value, QECGatesCost 20 | 21 | 22 | def test_hopping_tile(bloq_autotester): 23 | bloq_autotester(_hopping_tile) 24 | 25 | 26 | def test_hopping_plaquette(bloq_autotester): 27 | bloq_autotester(_plaquette) 28 | 29 | 30 | def test_hopping_tile_t_counts(): 31 | bloq = _hopping_tile() 32 | costs = get_cost_value(bloq, QECGatesCost()) 33 | assert costs.t == 8 * bloq.length**2 // 2 34 | assert costs.rotation == 2 * bloq.length**2 // 2 35 | 36 | 37 | def test_hopping_tile_hwp_t_counts(): 38 | bloq = _hopping_tile_hwp() 39 | costs = get_cost_value(bloq, QECGatesCost()) 40 | n_rot_par = bloq.length**2 // 2 41 | assert costs.rotation == 2 * n_rot_par.bit_length() 42 | assert costs.total_t_count(ts_per_rotation=0) == 8 * bloq.length**2 // 2 + 2 * 4 * ( 43 | n_rot_par - n_rot_par.bit_count() 44 | ) 45 | --------------------------------------------------------------------------------