├── third_party ├── BUILD └── cuquantum │ ├── BUILD │ └── BUILD.tpl ├── .bazelversion ├── docs ├── tutorials │ ├── multinode │ │ ├── out │ │ │ └── placeholder │ │ ├── noise.sub │ │ ├── noiseless.sub │ │ ├── noiseless3.py │ │ └── noise3.py │ ├── gcp_before_you_begin.md │ └── amd_gpu.md ├── images │ ├── choose_hw.png │ ├── colab_remote.png │ ├── colab_connect.png │ ├── colab_connected.png │ ├── qsimcirq_gcp │ │ ├── image1.png │ │ ├── image10.png │ │ ├── image11.png │ │ ├── image12.png │ │ ├── image13.png │ │ ├── image2.png │ │ ├── image3.png │ │ ├── image4.png │ │ ├── image5.png │ │ ├── image6.png │ │ ├── image7.png │ │ ├── image8.png │ │ ├── image9.png │ │ ├── container.png │ │ ├── colab_local.png │ │ ├── connection.png │ │ ├── q32d14_grid.png │ │ ├── colab_settings.png │ │ └── colab_success.png │ ├── qsim_runtime_comparison_noisy.png │ ├── qsim_runtime_comparison_noiseless.png │ └── quantum-ai-vertical.svg ├── release.md ├── testing.md ├── _scripts │ └── build_api_docs.py ├── docker.md ├── overview.md ├── _book.yaml └── install_qsimcirq.md ├── .github ├── ISSUE_TEMPLATE │ ├── config.yml │ ├── 4-task.yml │ ├── 3-question.yml │ ├── 2-feature-request.yml │ └── 1-bug-report.yml ├── problem-matchers │ ├── black.json │ ├── pytest.json │ ├── buildifier.json │ ├── hadolint.json │ ├── markdownlint.json │ ├── mypy.json │ ├── actionlint.json │ ├── yamllint.json │ ├── shellcheck.json │ └── pylint.json ├── workflows │ ├── README.md │ ├── cirq_compatibility.yml │ └── pr-labeler.yaml ├── actionlint.yaml ├── scorecard.yaml ├── dependabot.yaml └── SECURITY.md ├── requirements.txt ├── qsimcirq ├── py.typed ├── _version.py ├── __init__.py └── qsimh_simulator.py ├── .gitmodules ├── MANIFEST.in ├── dev_tools ├── ci │ └── README.md └── test_libs.sh ├── jupyter ├── README.md └── Dockerfile ├── .gitignore ├── dev-requirements.txt ├── .git-blame-ignore-revs ├── qsimcirq_tests └── __init__.py ├── .dockerignore ├── circuits └── BUILD ├── pybind_interface ├── sse │ ├── pybind_main_sse.h │ ├── CMakeLists.txt │ └── pybind_main_sse.cpp ├── avx2 │ ├── pybind_main_avx2.h │ ├── CMakeLists.txt │ └── pybind_main_avx2.cpp ├── hip │ ├── pybind_main_hip.h │ ├── CMakeLists.txt │ └── pybind_main_hip.cpp ├── avx512 │ ├── pybind_main_avx512.h │ ├── CMakeLists.txt │ └── pybind_main_avx512.cpp ├── basic │ ├── pybind_main_basic.h │ ├── CMakeLists.txt │ └── pybind_main_basic.cpp ├── cuda │ ├── pybind_main_cuda.h │ ├── pybind_main_cuda.cpp │ └── CMakeLists.txt ├── custatevec │ ├── pybind_main_custatevec.h │ ├── CMakeLists.txt │ └── pybind_main_custatevec.cpp ├── Dockerfile ├── GetCUDAARCHS.cmake ├── GetPybind11.cmake └── decide │ └── CMakeLists.txt ├── tests ├── Dockerfile ├── unitaryspace_avx_test.cc ├── unitaryspace_sse_test.cc ├── unitaryspace_basic_test.cc ├── unitaryspace_avx512_test.cc ├── hybrid_avx_test.cc ├── hybrid_cuda_test.cu ├── hybrid_custatevec_test.cu ├── unitary_calculator_avx_test.cc ├── unitary_calculator_sse_test.cc ├── unitary_calculator_avx512_test.cc ├── unitary_calculator_basic_test.cc ├── bitstring_test.cc ├── unitaryspace_testfixture.h ├── simulator_avx_test.cc ├── simulator_sse_test.cc ├── simulator_basic_test.cc ├── simulator_avx512_test.cc ├── vectorspace_test.cc └── qtrajectory_avx_test.cc ├── SUPPORT.md ├── lib ├── formux.h ├── simmux_gpu.h ├── io.h ├── simmux.h ├── util_cpu.h ├── util_custatevec.h ├── umux.h ├── io_file.h ├── unitaryspace.h ├── circuit.h ├── seqfor.h ├── util.h ├── bits.h └── cuda2hip.h ├── .gemini └── config.yaml ├── .hadolint.yaml ├── .yamlfmt.yaml ├── apps ├── Makefile ├── BUILD └── make.sh ├── install └── tests │ ├── docker-compose.yml │ └── Dockerfile ├── .jsonlintrc.yaml ├── .yamllint.yaml ├── .clang-format ├── CONTRIBUTING.md ├── docker-compose.yml ├── .editorconfig ├── pyproject.toml ├── Dockerfile ├── .pylintrc └── WORKSPACE /third_party/BUILD: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.bazelversion: -------------------------------------------------------------------------------- 1 | 6.5.0 2 | -------------------------------------------------------------------------------- /third_party/cuquantum/BUILD: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/tutorials/multinode/out/placeholder: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: true 2 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | absl-py 2 | cirq-core~=1.0 3 | numpy>=1.26.0 4 | -------------------------------------------------------------------------------- /qsimcirq/py.typed: -------------------------------------------------------------------------------- 1 | # Marker file for PEP 561. This package uses inline types. 2 | -------------------------------------------------------------------------------- /docs/images/choose_hw.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/qsim/HEAD/docs/images/choose_hw.png -------------------------------------------------------------------------------- /docs/images/colab_remote.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/qsim/HEAD/docs/images/colab_remote.png -------------------------------------------------------------------------------- /docs/images/colab_connect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/qsim/HEAD/docs/images/colab_connect.png -------------------------------------------------------------------------------- /docs/images/colab_connected.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/qsim/HEAD/docs/images/colab_connected.png -------------------------------------------------------------------------------- /docs/images/qsimcirq_gcp/image1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/qsim/HEAD/docs/images/qsimcirq_gcp/image1.png -------------------------------------------------------------------------------- /docs/images/qsimcirq_gcp/image10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/qsim/HEAD/docs/images/qsimcirq_gcp/image10.png -------------------------------------------------------------------------------- /docs/images/qsimcirq_gcp/image11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/qsim/HEAD/docs/images/qsimcirq_gcp/image11.png -------------------------------------------------------------------------------- /docs/images/qsimcirq_gcp/image12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/qsim/HEAD/docs/images/qsimcirq_gcp/image12.png -------------------------------------------------------------------------------- /docs/images/qsimcirq_gcp/image13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/qsim/HEAD/docs/images/qsimcirq_gcp/image13.png -------------------------------------------------------------------------------- /docs/images/qsimcirq_gcp/image2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/qsim/HEAD/docs/images/qsimcirq_gcp/image2.png -------------------------------------------------------------------------------- /docs/images/qsimcirq_gcp/image3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/qsim/HEAD/docs/images/qsimcirq_gcp/image3.png -------------------------------------------------------------------------------- /docs/images/qsimcirq_gcp/image4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/qsim/HEAD/docs/images/qsimcirq_gcp/image4.png -------------------------------------------------------------------------------- /docs/images/qsimcirq_gcp/image5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/qsim/HEAD/docs/images/qsimcirq_gcp/image5.png -------------------------------------------------------------------------------- /docs/images/qsimcirq_gcp/image6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/qsim/HEAD/docs/images/qsimcirq_gcp/image6.png -------------------------------------------------------------------------------- /docs/images/qsimcirq_gcp/image7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/qsim/HEAD/docs/images/qsimcirq_gcp/image7.png -------------------------------------------------------------------------------- /docs/images/qsimcirq_gcp/image8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/qsim/HEAD/docs/images/qsimcirq_gcp/image8.png -------------------------------------------------------------------------------- /docs/images/qsimcirq_gcp/image9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/qsim/HEAD/docs/images/qsimcirq_gcp/image9.png -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "tests/googletest"] 2 | path = tests/googletest 3 | url = https://github.com/google/googletest.git 4 | -------------------------------------------------------------------------------- /docs/images/qsimcirq_gcp/container.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/qsim/HEAD/docs/images/qsimcirq_gcp/container.png -------------------------------------------------------------------------------- /docs/images/qsimcirq_gcp/colab_local.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/qsim/HEAD/docs/images/qsimcirq_gcp/colab_local.png -------------------------------------------------------------------------------- /docs/images/qsimcirq_gcp/connection.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/qsim/HEAD/docs/images/qsimcirq_gcp/connection.png -------------------------------------------------------------------------------- /docs/images/qsimcirq_gcp/q32d14_grid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/qsim/HEAD/docs/images/qsimcirq_gcp/q32d14_grid.png -------------------------------------------------------------------------------- /docs/images/qsimcirq_gcp/colab_settings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/qsim/HEAD/docs/images/qsimcirq_gcp/colab_settings.png -------------------------------------------------------------------------------- /docs/images/qsimcirq_gcp/colab_success.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/qsim/HEAD/docs/images/qsimcirq_gcp/colab_success.png -------------------------------------------------------------------------------- /docs/images/qsim_runtime_comparison_noisy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/qsim/HEAD/docs/images/qsim_runtime_comparison_noisy.png -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include requirements.txt 2 | include dev-requirements.txt 3 | include CMakeLists.txt 4 | 5 | graft pybind_interface 6 | graft lib 7 | -------------------------------------------------------------------------------- /docs/images/qsim_runtime_comparison_noiseless.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/quantumlib/qsim/HEAD/docs/images/qsim_runtime_comparison_noiseless.png -------------------------------------------------------------------------------- /dev_tools/ci/README.md: -------------------------------------------------------------------------------- 1 | # Continuous integration scripts 2 | 3 | The scripts in this directory are used by the workflows in 4 | [`../../.github/workflows/`](../../.github/workflows/). 5 | -------------------------------------------------------------------------------- /jupyter/README.md: -------------------------------------------------------------------------------- 1 | # Docker container for Jupyter with _Cirq_ and _qsim_ 2 | 3 | You can build this container with the standard build commmand. 4 | 5 | More details on how to use this container can be found [here](../docs/tutorials/qsimcirq_gcp.md). -------------------------------------------------------------------------------- /.github/problem-matchers/black.json: -------------------------------------------------------------------------------- 1 | { 2 | "problemMatcher": [ 3 | { 4 | "owner": "black", 5 | "severity": "warning", 6 | "pattern": [ 7 | { 8 | "regexp": "^(would reformat\\s(.*))$", 9 | "message": 1, 10 | "file": 2 11 | } 12 | ] 13 | } 14 | ] 15 | } 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Generated files 2 | *.o 3 | *.a 4 | *.so 5 | *.x 6 | *.mod 7 | 8 | # Python cache 9 | __pycache__ 10 | .ipynb_checkpoints 11 | 12 | # Build 13 | /build 14 | *.egg-info 15 | 16 | # Bazel output 17 | bazel-* 18 | 19 | .idea/* 20 | 21 | # Eigen library 22 | eigen 23 | 24 | # vscode 25 | .vscode/* 26 | 27 | # Bazel files 28 | /bazel-* -------------------------------------------------------------------------------- /.github/problem-matchers/pytest.json: -------------------------------------------------------------------------------- 1 | { 2 | "problemMatcher": [ 3 | { 4 | "owner": "pytest", 5 | "severity": "error", 6 | "pattern": [ 7 | { 8 | "regexp": "^(\\S+):(\\d+): (.*)$", 9 | "file": 1, 10 | "line": 2, 11 | "message": 3 12 | } 13 | ] 14 | } 15 | ] 16 | } 17 | -------------------------------------------------------------------------------- /.github/problem-matchers/buildifier.json: -------------------------------------------------------------------------------- 1 | { 2 | "problemMatcher": [ 3 | { 4 | "owner": "buildifier", 5 | "pattern": [ 6 | { 7 | "regexp": "^(.+?):(\\d+):(\\d+): (.+)$", 8 | "file": 1, 9 | "line": 2, 10 | "column": 3, 11 | "message": 4 12 | } 13 | ] 14 | } 15 | ] 16 | } 17 | -------------------------------------------------------------------------------- /dev-requirements.txt: -------------------------------------------------------------------------------- 1 | cmake~=3.28.1 2 | black~=25.9.0 3 | flynt~=1.0 4 | isort[colors]~=6.0.1 5 | # The global option to pybind11 makes it include CMake files in a location where 6 | # CMake will find them. It makes a crucial difference in some environments. 7 | pybind11[global] 8 | pylint~=4.0.2 9 | pytest 10 | pytest-xdist 11 | py-cpuinfo 12 | setuptools>=78.1.1 13 | -------------------------------------------------------------------------------- /.github/problem-matchers/hadolint.json: -------------------------------------------------------------------------------- 1 | { 2 | "problemMatcher": [ 3 | { 4 | "owner": "hadolint", 5 | "pattern": [ 6 | { 7 | "regexp": "^(.*?):(\\d+) (DL\\d+|SC\\d+) (warning|error|info|style): (.*)$", 8 | "file": 1, 9 | "line": 2, 10 | "code": 3, 11 | "severity": 4, 12 | "message": 5 13 | } 14 | ] 15 | } 16 | ] 17 | } 18 | -------------------------------------------------------------------------------- /.github/problem-matchers/markdownlint.json: -------------------------------------------------------------------------------- 1 | { 2 | "problemMatcher": [ 3 | { 4 | "owner": "markdownlint", 5 | "severity": "warning", 6 | "pattern": [ 7 | { 8 | "regexp": "^([^:]*):\\s*(\\d+):?(\\d+)?\\s+([\\w-\\/]*)\\s+(.*)$", 9 | "file": 1, 10 | "line": 2, 11 | "column": 3, 12 | "code": 4, 13 | "message": 5 14 | } 15 | ] 16 | } 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /.github/problem-matchers/mypy.json: -------------------------------------------------------------------------------- 1 | { 2 | "problemMatcher": [ 3 | { 4 | "owner": "mypy", 5 | "severity": "error", 6 | "pattern": [ 7 | { 8 | "regexp": "^([^:]*):(\\d+):(?:(\\d+):)?\\s(error|warning): (.*?)(?: \\[(\\S+)\\])?$", 9 | "file": 1, 10 | "line": 2, 11 | "column": 3, 12 | "severity": 4, 13 | "message": 5, 14 | "code": 6 15 | } 16 | ] 17 | } 18 | ] 19 | } 20 | -------------------------------------------------------------------------------- /.git-blame-ignore-revs: -------------------------------------------------------------------------------- 1 | # git-blame can be instructed to ignore certain commits, which is useful to do 2 | # for commits that only involve large refactoring and reformatting operations. 3 | # The format of this file is one full 40-character commit hash (SHA-1) per line. 4 | # Blank lines and comments (such as this) are allowed. 5 | # 6 | # Usage: run the following command to configure git to use this file as a 7 | # default ignore file for git blame: 8 | # 9 | # git config blame.ignoreRevsFile .git-blame-ignore-revs 10 | # 11 | -------------------------------------------------------------------------------- /.github/problem-matchers/actionlint.json: -------------------------------------------------------------------------------- 1 | { 2 | "problemMatcher": [ 3 | { 4 | "owner": "actionlint", 5 | "pattern": [ 6 | { 7 | "regexp": "^(?:\\x1b\\[\\d+m)?(.+?)(?:\\x1b\\[\\d+m)*:(?:\\x1b\\[\\d+m)*(\\d+)(?:\\x1b\\[\\d+m)*:(?:\\x1b\\[\\d+m)*(\\d+)(?:\\x1b\\[\\d+m)*: (?:\\x1b\\[\\d+m)*(.+?)(?:\\x1b\\[\\d+m)* \\[(.+?)\\]$", 8 | "file": 1, 9 | "line": 2, 10 | "column": 3, 11 | "message": 4, 12 | "code": 5 13 | } 14 | ] 15 | } 16 | ] 17 | } 18 | -------------------------------------------------------------------------------- /docs/tutorials/multinode/noise.sub: -------------------------------------------------------------------------------- 1 | universe = docker 2 | docker_image = gcr.io/quantum-builds/github.com/quantumlib/jupyter_qsim:latest 3 | arguments = python3 noise3.py 4 | should_transfer_files = YES 5 | transfer_input_files = noise3.py 6 | when_to_transfer_output = ON_EXIT 7 | output = out/out.$(Cluster)-$(Process) 8 | error = out/err.$(Cluster)-$(Process) 9 | log = out/log.$(Cluster)-$(Process) 10 | request_memory = 10GB 11 | queue 50 -------------------------------------------------------------------------------- /.github/problem-matchers/yamllint.json: -------------------------------------------------------------------------------- 1 | { 2 | "problemMatcher": [ 3 | { 4 | "owner": "yamllint", 5 | "pattern": [ 6 | { 7 | "regexp": "^(.*\\.ya?ml)$", 8 | "file": 1 9 | }, 10 | { 11 | "regexp": "^\\s{2}(\\d+):(\\d+)\\s+(error|warning)\\s+(.*?)\\s+\\((.*)\\)$", 12 | "line": 1, 13 | "column": 2, 14 | "severity": 3, 15 | "message": 4, 16 | "code": 5, 17 | "loop": true 18 | } 19 | ] 20 | } 21 | ] 22 | } 23 | -------------------------------------------------------------------------------- /docs/tutorials/multinode/noiseless.sub: -------------------------------------------------------------------------------- 1 | universe = docker 2 | docker_image = gcr.io/quantum-builds/github.com/quantumlib/jupyter_qsim:latest 3 | arguments = python3 noiseless3.py 4 | should_transfer_files = YES 5 | transfer_input_files = noiseless3.py 6 | when_to_transfer_output = ON_EXIT 7 | output = out/out.$(Cluster)-$(Process) 8 | error = out/err.$(Cluster)-$(Process) 9 | log = out/log.$(Cluster)-$(Process) 10 | request_memory = 10GB 11 | queue 1 -------------------------------------------------------------------------------- /.github/problem-matchers/shellcheck.json: -------------------------------------------------------------------------------- 1 | { 2 | "problemMatcher": [ 3 | { 4 | "owner": "shellcheck", 5 | "pattern": [ 6 | { 7 | "regexp": "^In\\s(.+)\\sline\\s(\\d+):$", 8 | "file": 1, 9 | "line": 2 10 | }, 11 | { 12 | "regexp": ".*" 13 | }, 14 | { 15 | "regexp": "SC(\\d+)(\\s\\((note|warning|error)\\))?:\\s(.+)$", 16 | "code": 1, 17 | "severity": 3, 18 | "message": 4, 19 | "loop": true 20 | } 21 | ] 22 | } 23 | ] 24 | } 25 | -------------------------------------------------------------------------------- /.github/workflows/README.md: -------------------------------------------------------------------------------- 1 | # GitHub Actions workflows 2 | 3 | Herea are some notes about the workflows in this directory: 4 | 5 | Some workflows are reusable and are meant to called from other workflows 6 | only, not invoked directly. As of mid-2025, GitHub's user interface for 7 | Actions does not provide a way to hide, group, or otherwise distinguish these 8 | kinds of reusable modular workflows from the main workflows. To make these 9 | workflows more apparent in the user interface, we use the convention of 10 | naming these workflows with a leading tilde (`~`) character; this makes them 11 | appear last in the list of workflows on GitHub. 12 | -------------------------------------------------------------------------------- /qsimcirq_tests/__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 | """Root module of test package""" 16 | -------------------------------------------------------------------------------- /.dockerignore: -------------------------------------------------------------------------------- 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 | # Ignore this in case the user previously did a "pip install -e ." 16 | qsimcirq.egg-info 17 | -------------------------------------------------------------------------------- /circuits/BUILD: -------------------------------------------------------------------------------- 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 | exports_files([ 16 | "circuit_q24", 17 | "circuit_q30", 18 | "circuit_q40", 19 | ]) 20 | -------------------------------------------------------------------------------- /qsimcirq/_version.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 | """The version number defined here is read automatically in setup.py.""" 16 | 17 | __version__ = "0.23.0.dev0" 18 | -------------------------------------------------------------------------------- /pybind_interface/sse/pybind_main_sse.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "../pybind_main.h" 16 | 17 | PYBIND11_MODULE(qsim_sse, m) { MODULE_BINDINGS } 18 | -------------------------------------------------------------------------------- /pybind_interface/avx2/pybind_main_avx2.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "../pybind_main.h" 16 | 17 | PYBIND11_MODULE(qsim_avx2, m) { MODULE_BINDINGS } 18 | -------------------------------------------------------------------------------- /pybind_interface/hip/pybind_main_hip.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "../pybind_main.h" 16 | 17 | PYBIND11_MODULE(qsim_hip, m) { GPU_MODULE_BINDINGS } 18 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /pybind_interface/avx512/pybind_main_avx512.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "../pybind_main.h" 16 | 17 | PYBIND11_MODULE(qsim_avx512, m) { MODULE_BINDINGS } 18 | -------------------------------------------------------------------------------- /pybind_interface/basic/pybind_main_basic.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "../pybind_main.h" 16 | 17 | PYBIND11_MODULE(qsim_basic, m) { MODULE_BINDINGS } 18 | -------------------------------------------------------------------------------- /pybind_interface/cuda/pybind_main_cuda.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "../pybind_main.h" 16 | 17 | PYBIND11_MODULE(qsim_cuda, m) { GPU_MODULE_BINDINGS } 18 | -------------------------------------------------------------------------------- /pybind_interface/custatevec/pybind_main_custatevec.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "../pybind_main.h" 16 | 17 | PYBIND11_MODULE(qsim_custatevec, m) { GPU_MODULE_BINDINGS } 18 | -------------------------------------------------------------------------------- /tests/Dockerfile: -------------------------------------------------------------------------------- 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 | # Base OS 16 | # hadolint ignore=DL3006 17 | FROM qsim-base AS qsim-cxx-tests 18 | 19 | # Copy relevant files 20 | COPY ./tests/ /qsim/tests/ 21 | 22 | WORKDIR /qsim/ 23 | 24 | # Compile and run qsim tests 25 | ENTRYPOINT ["make", "-j", "-C", "/qsim/", "run-cxx-tests"] 26 | -------------------------------------------------------------------------------- /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, please first search the [issue tracker 14 | on GitHub](https://github.com/quantumlib/Qsim/issues) to check if there 15 | is already an open issue identical or similar to your bug report/feature 16 | request. If there is none, go ahead and file a new issue in the issue tracker. 17 | 18 | ## Contact the maintainers 19 | 20 | For any questions or concerns not addressed here, please email 21 | [quantum-oss-maintainers@google.com](mailto:quantum-oss-maintainers@google.com). 22 | -------------------------------------------------------------------------------- /lib/formux.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #ifndef FORMUX_H_ 16 | #define FORMUX_H_ 17 | 18 | #ifdef _OPENMP 19 | # include "parfor.h" 20 | namespace qsim { 21 | using For = ParallelFor; 22 | } 23 | #else 24 | # include "seqfor.h" 25 | namespace qsim { 26 | using For = SequentialFor; 27 | } 28 | #endif 29 | 30 | #endif // FORMUX_H_ 31 | -------------------------------------------------------------------------------- /lib/simmux_gpu.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC. All Rights Reserved. 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 | #ifndef SIMMUX_GPU_H_ 16 | #define SIMMUX_GPU_H_ 17 | 18 | #ifdef __CUSTATEVEC__ 19 | # include "simulator_custatevec.h" 20 | namespace qsim { 21 | using SimulatorGpu = SimulatorCuStateVec<>; 22 | } 23 | #else 24 | # include "simulator_cuda.h" 25 | namespace qsim { 26 | using SimulatorGpu = SimulatorCUDA<>; 27 | } 28 | #endif 29 | 30 | #endif // SIMMUX_GPU_H_ 31 | -------------------------------------------------------------------------------- /.github/actionlint.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 | # Summary: config actionlint (https://github.com/rhysd/actionlint) for certain 16 | # things we use. 17 | 18 | self-hosted-runner: 19 | # We don't have self-hosted runners, but we do use some of the "partner" 20 | # runner images from https://github.com/actions/partner-runner-images 21 | # and some runners that actionlint doesn't currently recognize. 22 | labels: 23 | - ubuntu-24.04-arm 24 | - ubuntu-slim 25 | -------------------------------------------------------------------------------- /.gemini/config.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 | # Summary: configure Gemini Code Assist (https://codeassist.google/). 16 | # See https://github.com/marketplace/gemini-code-assist for more info. 17 | 18 | have_fun: false 19 | code_review: 20 | disable: false 21 | comment_severity_threshold: HIGH 22 | max_review_comments: -1 23 | pull_request_opened: 24 | help: false 25 | summary: false 26 | code_review: true 27 | include_drafts: false 28 | ignore_patterns: [] 29 | -------------------------------------------------------------------------------- /.hadolint.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 | # See https://github.com/hadolint/hadolint/ for info about config options. 16 | # IMPORTANT: if you change values here, update .editorconfig to match. 17 | 18 | format: tty 19 | no-color: false 20 | no-fail: false 21 | failure-threshold: error 22 | 23 | # Rule/error numbers are described at https://github.com/hadolint/hadolint/wiki 24 | ignored: 25 | # Multiple consecutive RUN stmts are usually deliberate. Don't flag that. 26 | - DL3059 27 | -------------------------------------------------------------------------------- /.yamlfmt.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 | # See https://github.com/google/yamlfmt/ for info about configuration options. 16 | # IMPORTANT: if you change values here, update .editorconfig to match. 17 | 18 | # Read .gitignore for paths to exclude. 19 | gitignore_excludes: true 20 | 21 | formatter: 22 | type: basic 23 | eof_newline: true 24 | # Note: if you change the line length here, update .editorconfig too. 25 | max_line_length: 80 26 | retain_line_breaks: true 27 | trim_trailing_whitespace: true 28 | -------------------------------------------------------------------------------- /pybind_interface/Dockerfile: -------------------------------------------------------------------------------- 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 | # Base OS 16 | # hadolint ignore=DL3006 17 | FROM qsim-base AS qsim-py-tests 18 | 19 | # Copy relevant files 20 | COPY ./pybind_interface/ /qsim/pybind_interface/ 21 | COPY ./qsimcirq/ /qsim/qsimcirq/ 22 | COPY ./qsimcirq_tests/ /qsim/qsimcirq_tests/ 23 | 24 | WORKDIR /qsim/ 25 | 26 | # Build pybind code early to cache the results 27 | RUN make -j -C /qsim/ pybind 28 | 29 | # Compile and run qsim tests 30 | ENTRYPOINT ["make", "-C", "/qsim/", "run-py-tests"] 31 | -------------------------------------------------------------------------------- /apps/Makefile: -------------------------------------------------------------------------------- 1 | CXX_TARGETS = $(shell find . -maxdepth 1 -name '*.cc') 2 | CXX_TARGETS := $(CXX_TARGETS:%.cc=%.x) 3 | 4 | CUDA_TARGETS = $(shell find . -maxdepth 1 -name '*cuda.cu') 5 | CUDA_TARGETS := $(CUDA_TARGETS:%cuda.cu=%cuda.x) 6 | 7 | CUSTATEVEC_TARGETS = $(shell find . -maxdepth 1 -name "*custatevec.cu") 8 | CUSTATEVEC_TARGETS := $(CUSTATEVEC_TARGETS:%custatevec.cu=%custatevec.x) 9 | 10 | HIP_TARGETS = $(shell find . -maxdepth 1 -name '*cuda.cu') 11 | HIP_TARGETS := $(HIP_TARGETS:%cuda.cu=%hip.x) 12 | 13 | .PHONY: qsim 14 | qsim: $(CXX_TARGETS) 15 | 16 | .PHONY: qsim-cuda 17 | qsim-cuda: $(CUDA_TARGETS) 18 | 19 | .PHONY: qsim-custatevec 20 | qsim-custatevec: $(CUSTATEVEC_TARGETS) 21 | 22 | .PHONY: qsim-hip 23 | qsim-hip: $(HIP_TARGETS) 24 | 25 | %.x: %.cc 26 | $(CXX) -o ./$@ $< $(CXXFLAGS) $(ARCHFLAGS) 27 | 28 | %cuda.x: %cuda.cu 29 | $(NVCC) -o ./$@ $< $(NVCCFLAGS) 30 | 31 | %custatevec.x: %custatevec.cu 32 | $(NVCC) -o ./$@ $< $(NVCCFLAGS) $(CUSTATEVECFLAGS) 33 | 34 | %hip.x: %cuda.cu 35 | $(HIPCC) -o ./$@ $< $(HIPCCFLAGS) 36 | 37 | .PHONY: clean 38 | clean: 39 | -rm -f ./*.x ./*.a ./*.so ./*.mod 40 | -------------------------------------------------------------------------------- /docs/tutorials/multinode/noiseless3.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 | import cirq 16 | 17 | import qsimcirq 18 | 19 | # Create a Bell state, |00) + |11) 20 | q0, q1 = cirq.LineQubit.range(2) 21 | circuit = cirq.Circuit(cirq.H(q0), cirq.CNOT(q0, q1), cirq.measure(q0, q1, key="m")) 22 | 23 | sim = qsimcirq.QSimSimulator() 24 | result = sim.run(circuit, repetitions=1000) 25 | # Outputs a histogram dict of result:count pairs. 26 | # Expected result is a bunch of 0s and 3s, with no 1s or 2s. 27 | print(result.histogram(key="m")) 28 | -------------------------------------------------------------------------------- /install/tests/docker-compose.yml: -------------------------------------------------------------------------------- 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 | services: 16 | # Verifies that installing from the `qsimcirq` PyPI package works correctly. 17 | # To ensure that users do not need to clone the qsim repository in order to 18 | # install successfully, this image should NOT depend on the `qsim` image. 19 | qsim-install: 20 | image: qsim-install 21 | container_name: qsim-install 22 | build: 23 | context: ../../ 24 | dockerfile: ./install/tests/Dockerfile 25 | -------------------------------------------------------------------------------- /.jsonlintrc.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 | # The variant of `jsonlint` we use is (https://github.com/prantlf/jsonlint). 16 | # IMPORTANT: if you change values here, update .editorconfig to match. 17 | 18 | comments: false 19 | compact: true 20 | continue: true 21 | endOfLine: lf 22 | # Note: if you change indentation or line length, update .editorconfig too. 23 | indent: 2 24 | log-files: false 25 | no-duplicate-keys: true 26 | patterns: 27 | - '**/*.json' 28 | - '!**/node_modules' 29 | singleQuote: false 30 | trailing-commas: false 31 | -------------------------------------------------------------------------------- /.github/scorecard.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 | # Summary: configure OSSF Scorecard for some nuances of this project. 16 | # For more info about Scorecard options, see https://scorecard.dev/. 17 | 18 | checks: 19 | - name: Pinned-Dependencies 20 | reason: >- 21 | Unpinned dependencies are allowed in test environments for ease of 22 | maintenance in this project. 23 | ignored-paths: 24 | - "**/*test*/**" 25 | # TODO: remove after fixing https://github.com/quantumlib/qsim/issues/951 26 | - ".github/workflows/**" 27 | -------------------------------------------------------------------------------- /third_party/cuquantum/BUILD.tpl: -------------------------------------------------------------------------------- 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 | package(default_visibility = ["//visibility:public"]) 16 | 17 | cc_library( 18 | name = "cuquantum_headers", 19 | linkstatic = 1, 20 | srcs = [":cuquantum_header_include"], 21 | includes = ["include"], 22 | visibility = ["//visibility:public"], 23 | ) 24 | 25 | cc_library( 26 | name = "libcuquantum", 27 | srcs = [ 28 | ":libcustatevec.so", 29 | ], 30 | visibility = ["//visibility:public"], 31 | ) 32 | 33 | %{CUQUANTUM_HEADER_GENRULE} 34 | %{CUSTATEVEC_SHARED_LIBRARY_GENRULE} 35 | -------------------------------------------------------------------------------- /docs/release.md: -------------------------------------------------------------------------------- 1 | # Release process 2 | 3 | The qsimcirq release is maintained by Google contributors through a dedicated 4 | Kokoro build (a Google-internal variant of Jenkins). If you are a Google 5 | contributor and need to cut a release, please see `RELEASE.md` in the 6 | Google-internal fork of qsim. 7 | 8 | 9 | 10 | ## Version semantics 11 | 12 | Version numbering follows the semantic versioning guidelines at 13 | [semver.org](https://semver.org/), whose summary is copied below: 14 | 15 | > Given a version number MAJOR.MINOR.PATCH, increment the: 16 | > 17 | > 1. MAJOR version when you make incompatible API changes, 18 | > 2. MINOR version when you add functionality in a backwards compatible manner, and 19 | > 3. PATCH version when you make backwards compatible bug fixes. 20 | > 21 | > Additional labels for pre-release and build metadata are available 22 | > as extensions to the MAJOR.MINOR.PATCH format. 23 | 24 | Note that this behavior is altered for MAJOR version zero (0.y.z): 25 | 26 | > Major version zero (0.y.z) is for initial development. Anything MAY change at 27 | > any time. The public API SHOULD NOT be considered stable. 28 | -------------------------------------------------------------------------------- /pybind_interface/GetCUDAARCHS.cmake: -------------------------------------------------------------------------------- 1 | # Copyright 2025 Google LLC. All Rights Reserved. 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 | # Check whether the user has provided info about the GPU(s) installed 17 | # on their system. If not, try to determine what it is automaticaly. 18 | if(CMAKE_CUDA_ARCHITECTURES) 19 | # CMake 3.18+ sets this variable from $CUDAARCHS automatically. 20 | message(STATUS "qsim: using CUDA architectures " 21 | "${CMAKE_CUDA_ARCHITECTURES}") 22 | else() 23 | # Compile for all supported major and minor real architectures, and the 24 | # highest major virtual architecture. 25 | set(CMAKE_CUDA_ARCHITECTURES native) 26 | endif() 27 | -------------------------------------------------------------------------------- /docs/testing.md: -------------------------------------------------------------------------------- 1 | # Testing qsim 2 | 3 | Unit and integration tests are provided for qsim and its Cirq interface. To run 4 | all tests, simply run the command: 5 | ``` 6 | make run-tests 7 | ``` 8 | 9 | **NOTE:** This command (and all others specified on this page) runs all tests in 10 | sequence. If any test fails, execution will halt and no further tests will run. 11 | 12 | ## C++ tests 13 | 14 | If you only want to run tests for the core C++ libraries, use this command: 15 | ``` 16 | make run-cxx-tests 17 | ``` 18 | 19 | To build tests without running them, instead use: 20 | ``` 21 | make cxx-tests 22 | ``` 23 | 24 | ## Cirq interface tests 25 | 26 | Similarly, tests specific to the Python Cirq interface can be run with: 27 | ``` 28 | make run-py-tests 29 | ``` 30 | 31 | **NOTE:** Due to how Python handles imports, this will fail if run from any 32 | directory except the top-level `qsim` directory. Similarly, attempting to run 33 | tests using an installed version of qsimcirq (e.g. with `pip3 install qsimcirq`) 34 | can misbehave if there is a `qsimcirq/` directory _anywhere_ in the current 35 | working directory. For more information, see the 36 | [Python module documentation](https://docs.python.org/3/tutorial/modules.html#the-module-search-path). 37 | -------------------------------------------------------------------------------- /.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 | # See https://yamllint.readthedocs.io/ for info about configuration options. 16 | # IMPORTANT: if you change values here, update .editorconfig to match. 17 | 18 | rules: 19 | line-length: 20 | # Note: if you change the line length here, update .editorconfig too. 21 | max: 80 22 | # When the last item on a line can't be broken (e.g., a URL as a value), 23 | # it's rarely useful to flag it. The next 2 settings save developer time by 24 | # not requiring comment directives to disable warnings at every occurrence. 25 | allow-non-breakable-words: true 26 | allow-non-breakable-inline-mappings: true 27 | -------------------------------------------------------------------------------- /lib/io.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #ifndef IO_H_ 16 | #define IO_H_ 17 | 18 | #include 19 | #include 20 | 21 | namespace qsim { 22 | 23 | /** 24 | * Controller for output logs. 25 | */ 26 | struct IO { 27 | static void errorf(const char* format, ...) { 28 | va_list args; 29 | va_start(args, format); 30 | vfprintf(stderr, format, args); 31 | va_end(args); 32 | } 33 | 34 | static void messagef(const char* format, ...) { 35 | va_list args; 36 | va_start(args, format); 37 | vprintf(format, args); 38 | va_end(args); 39 | } 40 | }; 41 | 42 | } // namespace qsim 43 | 44 | #endif // IO_H_ 45 | -------------------------------------------------------------------------------- /.clang-format: -------------------------------------------------------------------------------- 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 | # Summary: configure clang-format for this project. 16 | # See https://clang.llvm.org/docs/ClangFormatStyleOptions.html for more info. 17 | 18 | # Note: if you change line length or indentation here, update .editorconfig too. 19 | --- 20 | Language: Cpp 21 | BasedOnStyle: Google 22 | ColumnLimit: 80 23 | AlignAfterOpenBracket: AlwaysBreak 24 | IncludeCategories: 25 | - Regex: '^<.*>$' 26 | Priority: 1 27 | - Regex: '^"gtest/.*"$' 28 | Priority: 2 29 | - Regex: '^".*"$' 30 | Priority: 3 31 | IncludeIsMainRegex: '([.](test|benchmark))?$' 32 | --- 33 | Language: Proto 34 | BasedOnStyle: Google 35 | ... 36 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to Contribute 2 | 3 | We'd love to accept your patches and contributions to this project. There are 4 | just a few small guidelines you need to follow, but don't worry about 5 | (or expect to) get everything right the first time! Create a pull request and 6 | we'll nudge you in the right direction. 7 | 8 | ## Contributor License Agreement 9 | 10 | Contributions to this project must be accompanied by a Contributor License 11 | Agreement. You (or your employer) retain the copyright to your contribution; 12 | this simply gives us permission to use and redistribute your contributions as 13 | part of the project. Head over to to see 14 | your current agreements on file or to sign a new one. 15 | 16 | You generally only need to submit a CLA once, so if you've already submitted one 17 | (even if it was for a different project), you probably don't need to do it 18 | again. 19 | 20 | ## Code reviews 21 | 22 | All submissions, including submissions by project members, require review. We 23 | use GitHub pull requests for this purpose. Consult 24 | [GitHub Help](https://help.github.com/articles/about-pull-requests/) for more 25 | information on using pull requests. 26 | 27 | ## Community Guidelines 28 | 29 | This project follows 30 | [Google's Open Source Community Guidelines](https://opensource.google/conduct/). 31 | -------------------------------------------------------------------------------- /pybind_interface/GetPybind11.cmake: -------------------------------------------------------------------------------- 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 | include(FetchContent) 16 | 17 | set(MIN_PYBIND_VERSION "2.13.6") 18 | 19 | # Suppress warning "Compatibility with CMake < 3.10 will be removed ..." coming 20 | # from Pybind11. Not ideal, but avoids wasting time trying to find the cause. 21 | # TODO(mhucka): remove the settings when pybind11 updates its CMake files 22 | set(CMAKE_WARN_DEPRECATED OFF CACHE BOOL "Disable CMake deprecation warnings" FORCE) 23 | 24 | FetchContent_Declare( 25 | pybind11 26 | GIT_REPOSITORY https://github.com/pybind/pybind11 27 | GIT_TAG "v${MIN_PYBIND_VERSION}" 28 | OVERRIDE_FIND_PACKAGE 29 | ) 30 | FetchContent_MakeAvailable(pybind11) 31 | 32 | set(CMAKE_WARN_DEPRECATED ON CACHE BOOL "Reenable CMake deprecation warnings" FORCE) 33 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 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 | services: 16 | qsim-base-image: 17 | image: qsim-base 18 | platform: linux/amd64 19 | build: 20 | context: ./ 21 | dockerfile: Dockerfile 22 | target: qsim-base 23 | 24 | qsim-cxx-tests-image: 25 | image: qsim-cxx-tests 26 | build: 27 | context: ./ 28 | dockerfile: tests/Dockerfile 29 | target: qsim-cxx-tests 30 | depends_on: 31 | - qsim-base-image 32 | 33 | qsim-py-tests-image: 34 | image: qsim-py-tests 35 | build: 36 | context: ./ 37 | dockerfile: pybind_interface/Dockerfile 38 | target: qsim-py-tests 39 | depends_on: 40 | - qsim-base-image 41 | -------------------------------------------------------------------------------- /lib/simmux.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #ifndef SIMMUX_H_ 16 | #define SIMMUX_H_ 17 | 18 | #ifdef __AVX512F__ 19 | # include "simulator_avx512.h" 20 | namespace qsim { 21 | template 22 | using Simulator = SimulatorAVX512; 23 | } 24 | #elif __AVX2__ 25 | # include "simulator_avx.h" 26 | namespace qsim { 27 | template 28 | using Simulator = SimulatorAVX; 29 | } 30 | #elif __SSE4_1__ 31 | # include "simulator_sse.h" 32 | namespace qsim { 33 | template 34 | using Simulator = SimulatorSSE; 35 | } 36 | #else 37 | # include "simulator_basic.h" 38 | namespace qsim { 39 | template 40 | using Simulator = SimulatorBasic; 41 | } 42 | #endif 43 | 44 | #endif // SIMMUX_H_ 45 | -------------------------------------------------------------------------------- /tests/unitaryspace_avx_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "unitaryspace_testfixture.h" 16 | 17 | #include "gtest/gtest.h" 18 | 19 | #include "../lib/formux.h" 20 | #include "../lib/unitaryspace_avx.h" 21 | 22 | namespace qsim { 23 | 24 | namespace unitary { 25 | namespace { 26 | 27 | TEST(UnitarySpaceAVXTest, SetZero) { 28 | TestSetZeros>(); 29 | } 30 | 31 | TEST(UnitarySpaceAVXTest, SetIdentity) { 32 | TestSetIdentity>(); 33 | } 34 | 35 | TEST(UnitarySpaceAVXTest, GetEntry) { 36 | TestSetEntry>(); 37 | } 38 | 39 | } // namspace 40 | } // namespace unitary 41 | } // namespace qsim 42 | 43 | int main(int argc, char** argv) { 44 | ::testing::InitGoogleTest(&argc, argv); 45 | return RUN_ALL_TESTS(); 46 | } 47 | -------------------------------------------------------------------------------- /tests/unitaryspace_sse_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "unitaryspace_testfixture.h" 16 | 17 | #include "gtest/gtest.h" 18 | 19 | #include "../lib/formux.h" 20 | #include "../lib/unitaryspace_sse.h" 21 | 22 | namespace qsim { 23 | 24 | namespace unitary { 25 | namespace { 26 | 27 | TEST(UnitarySpaceSSETest, SetZero) { 28 | TestSetZeros>(); 29 | } 30 | 31 | TEST(UnitarySpaceSSETest, SetIdentity) { 32 | TestSetIdentity>(); 33 | } 34 | 35 | TEST(UnitarySpaceSSETest, GetEntry) { 36 | TestSetEntry>(); 37 | } 38 | 39 | } // namspace 40 | } // namespace unitary 41 | } // namespace qsim 42 | 43 | int main(int argc, char** argv) { 44 | ::testing::InitGoogleTest(&argc, argv); 45 | return RUN_ALL_TESTS(); 46 | } 47 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/4-task.yml: -------------------------------------------------------------------------------- 1 | name: Task 2 | description: Describe a task that needs to be done 3 | type: Task 4 | body: 5 | - type: markdown 6 | attributes: 7 | value: >- 8 | Thank you for your interest in qsim! Before continuing, it's worth 9 | [searching through the existing issues in this 10 | repository](https://github.com/quantumlib/qsim/issues?q=is%3Aissue) in 11 | case the same or similar task already exists. 12 | 13 | - type: textarea 14 | attributes: 15 | label: What is the task? 16 | placeholder: >- 17 | Describe the task here 18 | validations: 19 | required: true 20 | 21 | - type: textarea 22 | attributes: 23 | label: (Optional) Do you have ideas or preferences for the approach? 24 | placeholder: >- 25 | Describe a suggested or preferred approach here 26 | validations: 27 | required: false 28 | 29 | - type: dropdown 30 | attributes: 31 | label: How urgent is this for you? 32 | description: >- 33 | Please choose from among the following options. If the lack of this 34 | feature is blocking important work, please choose from among P0–P2. 35 | options: 36 | - P0 – needed within two weeks 37 | - P1 – needed by the next release 38 | - P2 – needed within two quarters 39 | - P3 – not blocked; it's an idea 40 | validations: 41 | required: false 42 | -------------------------------------------------------------------------------- /tests/unitaryspace_basic_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "unitaryspace_testfixture.h" 16 | 17 | #include "gtest/gtest.h" 18 | 19 | #include "../lib/formux.h" 20 | #include "../lib/unitaryspace_basic.h" 21 | 22 | namespace qsim { 23 | 24 | namespace unitary { 25 | namespace { 26 | 27 | TEST(UnitarySpaceBasicTest, SetZero) { 28 | TestSetZeros>(); 29 | } 30 | 31 | TEST(UnitarySpaceBasicTest, SetIdentity) { 32 | TestSetIdentity>(); 33 | } 34 | 35 | TEST(UnitarySpaceBasicTest, GetEntry) { 36 | TestSetEntry>(); 37 | } 38 | 39 | } // namspace 40 | } // namespace unitary 41 | } // namespace qsim 42 | 43 | int main(int argc, char** argv) { 44 | ::testing::InitGoogleTest(&argc, argv); 45 | return RUN_ALL_TESTS(); 46 | } 47 | -------------------------------------------------------------------------------- /lib/util_cpu.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #ifndef UTIL_CPU_H_ 16 | #define UTIL_CPU_H_ 17 | 18 | #ifdef __SSE2__ 19 | # include 20 | #endif 21 | 22 | namespace qsim { 23 | 24 | // This function sets flush-to-zero and denormals-are-zeros MXCSR control 25 | // flags. This prevents rare cases of performance slowdown potentially at 26 | // the cost of a tiny precision loss. 27 | inline void SetFlushToZeroAndDenormalsAreZeros() { 28 | #ifdef __SSE2__ 29 | _mm_setcsr(_mm_getcsr() | 0x8040); 30 | #endif 31 | } 32 | 33 | // This function clears flush-to-zero and denormals-are-zeros MXCSR control 34 | // flags. 35 | inline void ClearFlushToZeroAndDenormalsAreZeros() { 36 | #ifdef __SSE2__ 37 | _mm_setcsr(_mm_getcsr() & ~unsigned{0x8040}); 38 | #endif 39 | } 40 | 41 | } // namespace qsim 42 | 43 | #endif // UTIL_CPU_H_ 44 | -------------------------------------------------------------------------------- /lib/util_custatevec.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #ifndef UTIL_CUSTATEVEC_H_ 16 | #define UTIL_CUSTATEVEC_H_ 17 | 18 | #include 19 | #include 20 | 21 | #include "io.h" 22 | #include "util_cuda.h" 23 | 24 | namespace qsim { 25 | 26 | inline void ErrorAssert(cublasStatus_t code, const char* file, unsigned line) { 27 | if (code != CUBLAS_STATUS_SUCCESS) { 28 | IO::errorf("cuBLAS error %i: %s %d\n", code, file, line); 29 | exit(code); 30 | } 31 | } 32 | 33 | inline void ErrorAssert( 34 | custatevecStatus_t code, const char* file, unsigned line) { 35 | if (code != CUSTATEVEC_STATUS_SUCCESS) { 36 | IO::errorf("custatevec error: %s %s %d\n", 37 | custatevecGetErrorString(code), file, line); 38 | exit(code); 39 | } 40 | } 41 | 42 | } // namespace qsim 43 | 44 | #endif // UTIL_CUSTATEVEC_H_ 45 | -------------------------------------------------------------------------------- /docs/tutorials/multinode/noise3.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 | import cirq 16 | 17 | import qsimcirq 18 | 19 | # Create a Bell state, |00) + |11) 20 | q0, q1 = cirq.LineQubit.range(2) 21 | circuit = cirq.Circuit(cirq.H(q0), cirq.CNOT(q0, q1), cirq.measure(q0, q1, key="m")) 22 | 23 | # Constructs a noise model that adds depolarizing noise after each gate. 24 | noise = cirq.NoiseModel.from_noise_model_like(cirq.depolarize(p=0.05)) 25 | 26 | # Use the noise model to create a noisy circuit. 27 | noisy_circuit = cirq.Circuit(noise.noisy_moments(circuit, system_qubits=[q0, q1])) 28 | 29 | sim = qsimcirq.QSimSimulator() 30 | result = sim.run(noisy_circuit, repetitions=1000) 31 | # Outputs a histogram dict of result:count pairs. 32 | # Expected result is a bunch of 0s and 3s, with fewer 1s and 2s. 33 | # (For comparison, the noiseless circuit will only have 0s and 3s) 34 | print(result.histogram(key="m")) 35 | -------------------------------------------------------------------------------- /tests/unitaryspace_avx512_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "unitaryspace_testfixture.h" 16 | 17 | #include "gtest/gtest.h" 18 | 19 | #if defined(__AVX512F__) && !defined(_WIN32) 20 | 21 | #include "../lib/formux.h" 22 | #include "../lib/unitaryspace_avx512.h" 23 | 24 | namespace qsim { 25 | namespace unitary { 26 | namespace { 27 | 28 | TEST(UnitarySpaceAVX512Test, SetZero) { 29 | TestSetZeros>(); 30 | } 31 | 32 | TEST(UnitarySpaceAVX512Test, SetIdentity) { 33 | TestSetIdentity>(); 34 | } 35 | 36 | TEST(UnitarySpaceAVX512Test, GetEntry) { 37 | TestSetEntry>(); 38 | } 39 | 40 | } // namspace 41 | } // namespace unitary 42 | } // namespace qsim 43 | 44 | #endif // __AVX512F__ && !_WIN32 45 | 46 | int main(int argc, char** argv) { 47 | ::testing::InitGoogleTest(&argc, argv); 48 | return RUN_ALL_TESTS(); 49 | } 50 | -------------------------------------------------------------------------------- /.github/dependabot.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 | version: 2 16 | updates: 17 | - package-ecosystem: "docker" 18 | directories: 19 | - ./ 20 | - install/tests/ 21 | - jupyter/ 22 | - pybind_interface/ 23 | - tests/ 24 | schedule: 25 | interval: "monthly" 26 | labels: 27 | - "area/devops" 28 | - "area/health" 29 | 30 | - package-ecosystem: "github-actions" 31 | # The "github-actions" code explicitly looks in /.github/workflows if the 32 | # value "/" is given for the directory attribute. Yes, that's confusing. 33 | directory: "/" 34 | schedule: 35 | interval: "monthly" 36 | labels: 37 | - "area/devops" 38 | - "area/health" 39 | 40 | - package-ecosystem: "pip" 41 | directory: "/" 42 | schedule: 43 | interval: "monthly" 44 | versioning-strategy: "increase-if-necessary" 45 | labels: 46 | - "area/dependencies" 47 | - "area/python" 48 | - "area/health" 49 | -------------------------------------------------------------------------------- /tests/hybrid_avx_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "hybrid_testfixture.h" 16 | 17 | #include "gtest/gtest.h" 18 | 19 | #include "../lib/seqfor.h" 20 | #include "../lib/simulator_avx.h" 21 | 22 | namespace qsim { 23 | 24 | template 25 | struct Factory { 26 | using Simulator = qsim::SimulatorAVX; 27 | using StateSpace = typename Simulator::StateSpace; 28 | using fp_type = typename StateSpace::fp_type; 29 | 30 | StateSpace CreateStateSpace() const { 31 | return StateSpace(1); 32 | } 33 | 34 | Simulator CreateSimulator() const { 35 | return Simulator(1); 36 | } 37 | }; 38 | 39 | TEST(HybridAVXTest, Hybrid2) { 40 | TestHybrid2(qsim::Factory()); 41 | } 42 | 43 | TEST(HybridAVXTest, Hybrid4) { 44 | TestHybrid4(qsim::Factory()); 45 | } 46 | 47 | } // namespace qsim 48 | 49 | int main(int argc, char** argv) { 50 | testing::InitGoogleTest(&argc, argv); 51 | return RUN_ALL_TESTS(); 52 | } 53 | -------------------------------------------------------------------------------- /pybind_interface/basic/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC. All Rights Reserved. 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 | cmake_minimum_required(VERSION 3.28) 16 | project(qsim) 17 | 18 | if(WIN32) 19 | add_compile_options(/openmp) 20 | # Add /O2 to any configuration that is NOT Debug. 21 | # This prevents a conflict with /RTC1 in DEBUG builds. 22 | add_compile_options($<$>:/O2>) 23 | else() 24 | add_compile_options(-O3 -flto=auto) 25 | endif() 26 | 27 | if(APPLE) 28 | include_directories( 29 | "/usr/local/include" 30 | "/usr/local/opt/llvm/include" 31 | "/opt/homebrew/include" 32 | "/opt/homebrew/opt/llvm@19/include" 33 | ) 34 | link_directories( 35 | "/usr/local/lib" 36 | "/usr/local/opt/llvm/lib" 37 | "/opt/homebrew/lib" 38 | "/opt/homebrew/opt/llvm@19/lib" 39 | ) 40 | endif() 41 | 42 | include(../GetPybind11.cmake) 43 | pybind11_add_module(qsim_basic pybind_main_basic.cpp) 44 | 45 | target_link_libraries(qsim_basic PUBLIC OpenMP::OpenMP_CXX) 46 | -------------------------------------------------------------------------------- /pybind_interface/sse/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC. All Rights Reserved. 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 | cmake_minimum_required(VERSION 3.28) 16 | project(qsim) 17 | 18 | if(WIN32) 19 | add_compile_options(/openmp) 20 | # Add /O2 to any configuration that is NOT Debug. 21 | # This prevents a conflict with /RTC1 in DEBUG builds. 22 | add_compile_options($<$>:/O2>) 23 | else() 24 | add_compile_options(-msse4.1 -O3 -flto=auto) 25 | endif() 26 | 27 | if(APPLE) 28 | include_directories( 29 | "/usr/local/include" 30 | "/usr/local/opt/llvm/include" 31 | "/opt/homebrew/include" 32 | "/opt/homebrew/opt/llvm@19/include" 33 | ) 34 | link_directories( 35 | "/usr/local/lib" 36 | "/usr/local/opt/llvm/lib" 37 | "/opt/homebrew/lib" 38 | "/opt/homebrew/opt/llvm@19/lib" 39 | ) 40 | endif() 41 | 42 | include(../GetPybind11.cmake) 43 | pybind11_add_module(qsim_sse pybind_main_sse.cpp) 44 | 45 | target_link_libraries(qsim_sse PUBLIC OpenMP::OpenMP_CXX) 46 | -------------------------------------------------------------------------------- /pybind_interface/avx2/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC. All Rights Reserved. 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 | cmake_minimum_required(VERSION 3.28) 16 | project(qsim) 17 | 18 | if(WIN32) 19 | add_compile_options(/arch:AVX2 /openmp) 20 | # Add /O2 to any configuration that is NOT Debug. 21 | # This prevents a conflict with /RTC1 in DEBUG builds. 22 | add_compile_options($<$>:/O2>) 23 | else() 24 | add_compile_options(-mavx2 -mfma -O3 -flto=auto) 25 | endif() 26 | 27 | if(APPLE) 28 | include_directories( 29 | "/usr/local/include" 30 | "/usr/local/opt/llvm/include" 31 | "/opt/homebrew/include" 32 | "/opt/homebrew/opt/llvm@19/include" 33 | ) 34 | link_directories( 35 | "/usr/local/lib" 36 | "/usr/local/opt/llvm/lib" 37 | "/opt/homebrew/lib" 38 | "/opt/homebrew/opt/llvm@19/lib" 39 | ) 40 | endif() 41 | 42 | include(../GetPybind11.cmake) 43 | pybind11_add_module(qsim_avx2 pybind_main_avx2.cpp) 44 | 45 | target_link_libraries(qsim_avx2 PUBLIC OpenMP::OpenMP_CXX) 46 | -------------------------------------------------------------------------------- /lib/umux.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #ifndef UMUX_H_ 16 | #define UMUX_H_ 17 | 18 | #ifdef __AVX512F__ 19 | # include "unitary_calculator_avx512.h" 20 | namespace qsim { 21 | namespace unitary { 22 | template 23 | using UnitaryCalculator = UnitaryCalculatorAVX512; 24 | } 25 | } 26 | #elif __AVX2__ 27 | # include "unitary_calculator_avx.h" 28 | namespace qsim { 29 | namespace unitary { 30 | template 31 | using UnitaryCalculator = UnitaryCalculatorAVX; 32 | } 33 | } 34 | #elif __SSE4_1__ 35 | # include "unitary_calculator_sse.h" 36 | namespace qsim { 37 | namespace unitary { 38 | template 39 | using UnitaryCalculator = UnitaryCalculatorSSE; 40 | } 41 | } 42 | #else 43 | # include "unitary_calculator_basic.h" 44 | namespace qsim { 45 | namespace unitary { 46 | template 47 | using UnitaryCalculator = UnitaryCalculatorBasic; 48 | } 49 | } 50 | #endif 51 | 52 | #endif // UMUX_H_ 53 | -------------------------------------------------------------------------------- /pybind_interface/avx512/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC. All Rights Reserved. 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 | cmake_minimum_required(VERSION 3.28) 16 | project(qsim) 17 | 18 | if(WIN32) 19 | add_compile_options(/arch:AVX512 /openmp) 20 | # Add /O2 to any configuration that is NOT Debug. 21 | # This prevents a conflict with /RTC1 in DEBUG builds. 22 | add_compile_options($<$>:/O2>) 23 | else() 24 | add_compile_options(-mavx512f -mbmi2 -O3 -flto=auto) 25 | endif() 26 | 27 | if(APPLE) 28 | include_directories( 29 | "/usr/local/include" 30 | "/usr/local/opt/llvm/include" 31 | "/opt/homebrew/include" 32 | "/opt/homebrew/opt/llvm@19/include" 33 | ) 34 | link_directories( 35 | "/usr/local/lib" 36 | "/usr/local/opt/llvm/lib" 37 | "/opt/homebrew/lib" 38 | "/opt/homebrew/opt/llvm@19/lib" 39 | ) 40 | endif() 41 | 42 | include(../GetPybind11.cmake) 43 | pybind11_add_module(qsim_avx512 pybind_main_avx512.cpp) 44 | 45 | target_link_libraries(qsim_avx512 PUBLIC OpenMP::OpenMP_CXX) 46 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/3-question.yml: -------------------------------------------------------------------------------- 1 | name: Question 2 | description: Ask a question 3 | type: Question 4 | body: 5 | - type: markdown 6 | attributes: 7 | value: >- 8 | Thank you for your interest in qsim! Before continuing, it's worth 9 | [searching through the existing issues in this 10 | repository](https://github.com/quantumlib/qsim/issues?q=is%3Aissue) in 11 | case the same question has already been asked and possibly answered. 12 | 13 | - type: textarea 14 | attributes: 15 | label: What is the question? 16 | description: >- 17 | Please provide enough details and contextual information so that the 18 | question is answerable. 19 | placeholder: >- 20 | Write your question here 21 | validations: 22 | required: true 23 | 24 | - type: input 25 | attributes: 26 | label: Does this concern a specific version of qsim? If so, what version? 27 | placeholder: >- 28 | Write the qsim version number here 29 | validations: 30 | required: false 31 | 32 | - type: input 33 | attributes: 34 | label: Is it specific to a computing environment? If so, which one? 35 | description: >- 36 | If the platform and operating system version may be relevant, please 37 | tell us those details as well. It may matter, for example, whether you 38 | are using Linux, macOS, Windows, or Google Colab, as well as whether 39 | you are trying to use CUDA (and what version), and other details. 40 | placeholder: >- 41 | Describe the computing environment here 42 | validations: 43 | required: false 44 | -------------------------------------------------------------------------------- /.github/SECURITY.md: -------------------------------------------------------------------------------- 1 | # Reporting security issues 2 | 3 | This project's developers and community are committed to addressing security 4 | bugs promptly and effectively. We appreciate your efforts to disclose your 5 | findings responsibly, and will make every effort to acknowledge your 6 | contributions. 7 | 8 | Please **do not** use GitHub issues to report security vulnerabilities; GitHub 9 | issues are public, and doing so could allow someone to exploit the information 10 | before the problem can be addressed. Instead, please use the *Report a 11 | vulnerability* button from the *Security* tab at the top of this GitHub 12 | repository page. 13 | 14 | Please report security issues in third-party modules to the person or team 15 | maintaining the module rather than this project's stewards, unless you believe 16 | that some action needs to be taken specifically with this project in order to 17 | guard against the effects of a security vulnerability in third-party software. 18 | 19 | ## Responses to security reports 20 | 21 | The project stewards at Google Quantum AI will send a response indicating the 22 | next steps in handling your report. After the initial reply to your report, the 23 | project stewards will keep you informed of the progress towards a fix and full 24 | announcement, and may ask for additional information or guidance. 25 | 26 | ## Additional points of contact 27 | 28 | Please contact the project stewards at Google Quantum AI via email at 29 | quantum-oss-maintainers@google.com if you have questions or other concerns. If 30 | for any reason you are uncomfortable reaching out to the project stewards, 31 | please email opensource@google.com instead. 32 | -------------------------------------------------------------------------------- /pybind_interface/hip/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC. All Rights Reserved. 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 | cmake_minimum_required(VERSION 3.28) 16 | project(qsim LANGUAGES CXX HIP) 17 | 18 | if(WIN32) 19 | add_compile_options(/openmp) 20 | # Add /O2 to any configuration that is NOT Debug. 21 | # This prevents a conflict with /RTC1 in DEBUG builds. 22 | add_compile_options($<$>:/O2>) 23 | else() 24 | add_compile_options(-O3 -flto=auto) 25 | endif() 26 | 27 | include(../GetPybind11.cmake) 28 | find_package(PythonLibs 3.10 REQUIRED) 29 | 30 | list(APPEND CMAKE_MODULE_PATH "/opt/rocm/lib/cmake/hip") 31 | find_package(HIP REQUIRED) 32 | 33 | include_directories(${PYTHON_INCLUDE_DIRS} ${pybind11_SOURCE_DIR}/include) 34 | 35 | hip_add_library(qsim_hip MODULE pybind_main_hip.cpp) 36 | 37 | set_target_properties(qsim_hip PROPERTIES 38 | PREFIX "${PYTHON_MODULE_PREFIX}" 39 | SUFFIX "${PYTHON_MODULE_EXTENSION}" 40 | ) 41 | set_source_files_properties(pybind_main_hip.cpp PROPERTIES LANGUAGE HIP) 42 | 43 | target_link_libraries(qsim_hip PUBLIC OpenMP::OpenMP_CXX) 44 | -------------------------------------------------------------------------------- /apps/BUILD: -------------------------------------------------------------------------------- 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 | # TODO: remove reliance on getopt (unistd.h) to allow apps to run on Windows. 16 | 17 | load("@rules_cc//cc:defs.bzl", "cc_binary") 18 | 19 | cc_binary( 20 | name = "qsim_base", 21 | srcs = ["qsim_base.cc"], 22 | data = ["//circuits:circuit_q24"], 23 | deps = [ 24 | "//lib:run_qsim_lib", 25 | ], 26 | ) 27 | 28 | cc_binary( 29 | name = "qsim_von_neumann", 30 | srcs = ["qsim_von_neumann.cc"], 31 | deps = [ 32 | "//lib:run_qsim_lib", 33 | ], 34 | ) 35 | 36 | cc_binary( 37 | name = "qsim_amplitudes", 38 | srcs = ["qsim_amplitudes.cc"], 39 | deps = [ 40 | "//lib:bitstring", 41 | "//lib:run_qsim_lib", 42 | ], 43 | ) 44 | 45 | cc_binary( 46 | name = "qsimh_base", 47 | srcs = ["qsimh_base.cc"], 48 | deps = [ 49 | "//lib:bitstring", 50 | "//lib:run_qsimh_lib", 51 | ], 52 | ) 53 | 54 | cc_binary( 55 | name = "qsimh_amplitudes", 56 | srcs = ["qsimh_amplitudes.cc"], 57 | deps = [ 58 | "//lib:bitstring", 59 | "//lib:run_qsimh_lib", 60 | ], 61 | ) 62 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 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 | root = true 16 | 17 | # We mostly follow Google style guides (https://google.github.io/styleguide/) 18 | # with only a few deviations for line length and indentation in some files. 19 | 20 | # IMPORTANT: some of the other config files (.clang-format, etc.) also have 21 | # the same settings and need to be updated if changes are made to this file. 22 | [*] 23 | charset = utf-8 24 | indent_style = space 25 | insert_final_newline = true 26 | spelling_language = en-US 27 | trim_trailing_whitespace = true 28 | max_line_length = 80 29 | 30 | [{BUILD,*.BUILD,*.bzl,*.bazel,.bazelrc,WORKSPACE}] 31 | indent_size = 4 32 | 33 | [{CMakeLists.txt,*.cmake}] 34 | indent_size = 4 35 | 36 | [Dockerfile] 37 | indent_size = 4 38 | 39 | [{Makefile,makefile,*.mk}] 40 | indent_style = tab 41 | indent_size = 4 42 | 43 | [{*.cc,*.h}] 44 | indent_size = 2 45 | 46 | [*.json] 47 | indent_size = 2 48 | 49 | [*.py] 50 | indent_size = 4 51 | # This is the Black default line length. 52 | max_line_length = 88 53 | 54 | [*.sh] 55 | indent_size = 4 56 | 57 | [*.toml] 58 | indent_size = 2 59 | 60 | [{*.yaml,*.yml,*.cff}] 61 | indent_size = 2 62 | -------------------------------------------------------------------------------- /tests/hybrid_cuda_test.cu: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "hybrid_testfixture.h" 16 | 17 | #include "gtest/gtest.h" 18 | 19 | #include "../lib/simulator_cuda.h" 20 | 21 | namespace qsim { 22 | 23 | template 24 | struct Factory { 25 | using fp_type = FP; 26 | using Simulator = qsim::SimulatorCUDA; 27 | using StateSpace = typename Simulator::StateSpace; 28 | 29 | Factory(const typename StateSpace::Parameter& param) : param(param) {} 30 | 31 | StateSpace CreateStateSpace() const { 32 | return StateSpace(param); 33 | } 34 | 35 | Simulator CreateSimulator() const { 36 | return Simulator(); 37 | } 38 | 39 | typename StateSpace::Parameter param; 40 | }; 41 | 42 | TEST(HybridCUDATest, Hybrid2) { 43 | using Factory = qsim::Factory; 44 | Factory::StateSpace::Parameter param; 45 | Factory factory(param); 46 | TestHybrid2(factory); 47 | } 48 | 49 | TEST(HybridCUDATest, Hybrid4) { 50 | using Factory = qsim::Factory; 51 | Factory::StateSpace::Parameter param; 52 | Factory factory(param); 53 | TestHybrid4(factory); 54 | } 55 | 56 | } // namespace qsim 57 | 58 | int main(int argc, char** argv) { 59 | ::testing::InitGoogleTest(&argc, argv); 60 | return RUN_ALL_TESTS(); 61 | } 62 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 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 | [build-system] 16 | requires = [ 17 | "packaging", 18 | "setuptools>=78.1.1", 19 | "pybind11[global]", 20 | # "pip install" from sources needs to build Pybind, which needs CMake too. 21 | "cmake~=3.28.1", 22 | ] 23 | build-backend = "setuptools.build_meta" 24 | 25 | [tool.cibuildwheel] 26 | test-extras = "dev" 27 | dependency-versions = "latest" 28 | enable = ["cpython-prerelease"] 29 | environment.PIP_PREFER_BINARY = "1" 30 | # Due to package & module name conflict, temporarily move it away to run tests: 31 | before-test = "mv {package}/qsimcirq /tmp" 32 | test-command = "pytest -s -v {package}/qsimcirq_tests/qsimcirq_test.py && mv /tmp/qsimcirq {package}" 33 | 34 | [tool.cibuildwheel.macos] 35 | before-build = "brew install -q libomp llvm@19 && brew unlink libomp && brew unlink llvm@19 && brew link --force libomp && brew link --force llvm@19" 36 | repair-wheel-command = "delocate-listdeps {wheel} && delocate-wheel --verbose --require-archs {delocate_archs} -w {dest_dir} {wheel}" 37 | 38 | [tool.cibuildwheel.linux] 39 | manylinux-x86_64-image = "manylinux2014" 40 | manylinux-i686-image = "manylinux2014" 41 | skip = "*musllinux*" 42 | 43 | [tool.black] 44 | target-version = ['py310', 'py311', 'py312', 'py313'] 45 | extend-exclude = 'third_party' 46 | -------------------------------------------------------------------------------- /jupyter/Dockerfile: -------------------------------------------------------------------------------- 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 ubuntu:24.04 AS qsimcirq 16 | 17 | # hadolint ignore=DL3002 18 | USER root 19 | 20 | # hadolint ignore=DL3008 21 | RUN apt-get update && \ 22 | apt-get install -y --no-install-recommends python3-pip python3-dev git \ 23 | && rm -rf /var/lib/apt/lists/* 24 | 25 | # Newer Ubuntus adopted PEP 668, which blocks pip from installing into system 26 | # locations, but Docker is already an isolated environment, so we don't mind. 27 | ENV PIP_BREAK_SYSTEM_PACKAGES=1 28 | 29 | RUN python3 -m pip install --no-cache-dir \ 30 | cirq~=1.0 \ 31 | cirq[contrib]~=1.0 \ 32 | qsimcirq~=0.1 \ 33 | notebook==6.4.8 \ 34 | jupyterlab==4.4.10 \ 35 | jupyter-contrib-nbextensions==0.7.0 \ 36 | jupyter-nbextensions-configurator==0.6.4 \ 37 | jupyter-http-over-ws==0.0.8 38 | 39 | WORKDIR / 40 | 41 | RUN git clone https://github.com/quantumlib/qsim.git 42 | 43 | RUN jupyter contrib nbextension install --user && \ 44 | jupyter nbextension enable varInspector/main && \ 45 | jupyter serverextension enable --py jupyter_http_over_ws 46 | 47 | CMD ["jupyter-notebook", "--port=8888", "--no-browser",\ 48 | "--ip=0.0.0.0", "--allow-root", \ 49 | "--NotebookApp.allow_origin='*'", \ 50 | "--NotebookApp.port_retries=0", \ 51 | "--NotebookApp.token=''"] 52 | -------------------------------------------------------------------------------- /apps/make.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Copyright 2019 Google LLC. All Rights Reserved. 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 | # This file provides an alternate method for building apps in this directory. 17 | # Prefer using the Makefile (e.g. `make -C apps/`) if possible. 18 | 19 | g++ -O3 -march=native -fopenmp -o qsim_base.x qsim_base.cc 20 | g++ -O3 -march=native -fopenmp -o qsim_von_neumann.x qsim_von_neumann.cc 21 | g++ -O3 -march=native -fopenmp -o qsim_amplitudes.x qsim_amplitudes.cc 22 | g++ -O3 -march=native -fopenmp -o qsimh_base.x qsimh_base.cc 23 | g++ -O3 -march=native -fopenmp -o qsimh_amplitudes.x qsimh_amplitudes.cc 24 | 25 | if command -v nvcc &>/dev/null; then 26 | nvcc -O3 -o qsim_base_cuda.x qsim_base_cuda.cu 27 | nvcc -O3 -o qsim_qtrajectory_cuda.x qsim_qtrajectory_cuda.cu 28 | 29 | if [ -n "$CUQUANTUM_ROOT" ]; then 30 | declare -a CUSTATEVECFLAGS 31 | CUSTATEVECFLAGS=( 32 | "-I${CUQUANTUM_ROOT}/include" 33 | "-L${CUQUANTUM_ROOT}/lib" 34 | "-L${CUQUANTUM_ROOT}/lib64" 35 | "-lcustatevec" 36 | "-lcublas" 37 | ) 38 | nvcc -O3 "${CUSTATEVECFLAGS[@]}" \ 39 | -o qsim_base_custatevec.x qsim_base_custatevec.cu 40 | 41 | fi 42 | elif command -v hipcc &>/dev/null; then 43 | hipcc -O3 -o qsim_base_hip.x qsim_base_cuda.cu 44 | hipcc -O3 -o qsim_qtrajectory_hip.x qsim_qtrajectory_cuda.cu 45 | fi 46 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/2-feature-request.yml: -------------------------------------------------------------------------------- 1 | name: Feature request 2 | description: Request a feature or suggest an enhancement 3 | type: Enhancement 4 | body: 5 | - type: markdown 6 | attributes: 7 | value: >- 8 | Thank you for your interest in qsim! Before continuing, it's worth 9 | [searching through the existing issues in this 10 | repository](https://github.com/quantumlib/qsim/issues?q=is%3Aissue) in 11 | case the same feature has already been requested. 12 | 13 | - type: textarea 14 | attributes: 15 | label: What is your request or suggestion? 16 | description: >- 17 | Please describe the request and its context in detail. Is the feature 18 | something that would help you do something that you currently cannot due 19 | to limitations in qsim, or is it an idea for an enhancement or new 20 | functionality, or something else? 21 | placeholder: >- 22 | Describe the feature request here 23 | validations: 24 | required: true 25 | 26 | - type: textarea 27 | attributes: 28 | label: What solution or approach do you envision? 29 | description: >- 30 | If you have ideas or preferences for the solution, you can let us know 31 | here. If you are aware of other similar or related work, please let us 32 | know about it here. 33 | placeholder: >- 34 | Describe your proposed solution here 35 | validations: 36 | required: false 37 | 38 | - type: dropdown 39 | attributes: 40 | label: How urgent is this for you? 41 | description: >- 42 | Please choose from among the following options. If the lack of this 43 | feature is blocking important work, please choose from among P0–P2. 44 | options: 45 | - P0 – needed within two weeks 46 | - P1 – needed by the next release 47 | - P2 – needed within two quarters 48 | - P3 – not blocked; it's an idea 49 | validations: 50 | required: false 51 | -------------------------------------------------------------------------------- /tests/hybrid_custatevec_test.cu: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "hybrid_testfixture.h" 16 | 17 | #include 18 | #include 19 | 20 | #include "gtest/gtest.h" 21 | 22 | #include "../lib/simulator_custatevec.h" 23 | 24 | namespace qsim { 25 | 26 | template 27 | struct Factory { 28 | using fp_type = FP; 29 | using Simulator = qsim::SimulatorCuStateVec; 30 | using StateSpace = typename Simulator::StateSpace; 31 | 32 | Factory() { 33 | ErrorCheck(cublasCreate(&cublas_handle)); 34 | ErrorCheck(custatevecCreate(&custatevec_handle)); 35 | } 36 | 37 | ~Factory() { 38 | ErrorCheck(cublasDestroy(cublas_handle)); 39 | ErrorCheck(custatevecDestroy(custatevec_handle)); 40 | } 41 | 42 | StateSpace CreateStateSpace() const { 43 | return StateSpace(cublas_handle, custatevec_handle); 44 | } 45 | 46 | Simulator CreateSimulator() const { 47 | return Simulator(cublas_handle, custatevec_handle); 48 | } 49 | 50 | cublasHandle_t cublas_handle; 51 | custatevecHandle_t custatevec_handle; 52 | }; 53 | 54 | TEST(HybridCuStateVecTest, Hybrid2) { 55 | TestHybrid2(qsim::Factory()); 56 | } 57 | 58 | TEST(HybridCuStateVecTest, Hybrid4) { 59 | TestHybrid4(qsim::Factory()); 60 | } 61 | 62 | } // namespace qsim 63 | 64 | int main(int argc, char** argv) { 65 | ::testing::InitGoogleTest(&argc, argv); 66 | return RUN_ALL_TESTS(); 67 | } 68 | -------------------------------------------------------------------------------- /lib/io_file.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #ifndef IO_FILE_H_ 16 | #define IO_FILE_H_ 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | #include "io.h" 23 | 24 | namespace qsim { 25 | 26 | /** 27 | * Controller for output logs with methods for writing to file. 28 | */ 29 | struct IOFile : public IO { 30 | static std::ifstream StreamFromFile(const std::string& file) { 31 | std::ifstream fs; 32 | fs.open(file); 33 | if (!fs) { 34 | errorf("cannot open %s for reading.\n", file.c_str()); 35 | } 36 | return fs; 37 | } 38 | 39 | static void CloseStream(std::ifstream& fs) { 40 | fs.close(); 41 | } 42 | 43 | static bool WriteToFile( 44 | const std::string& file, const std::string& content) { 45 | return WriteToFile(file, content.data(), content.size()); 46 | } 47 | 48 | static bool WriteToFile( 49 | const std::string& file, const void* data, uint64_t size) { 50 | auto fs = std::fstream(file, std::ios::out | std::ios::binary); 51 | 52 | if (!fs) { 53 | errorf("cannot open %s for writing.\n", file.c_str()); 54 | return false; 55 | } else { 56 | fs.write((const char*) data, size); 57 | if (!fs) { 58 | errorf("cannot write to %s.\n", file.c_str()); 59 | return false; 60 | } 61 | 62 | fs.close(); 63 | } 64 | 65 | return true; 66 | } 67 | }; 68 | 69 | } // namespace qsim 70 | 71 | #endif // IO_FILE_H_ 72 | -------------------------------------------------------------------------------- /lib/unitaryspace.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #ifndef UNITARYSPACE_H_ 16 | #define UNITARYSPACE_H_ 17 | 18 | #include 19 | 20 | namespace qsim { 21 | 22 | namespace unitary { 23 | 24 | /** 25 | * Abstract class containing routines for general unitary matrix manipulations. 26 | * "AVX", "AVX512", "Basic", and "SSE" implementations are provided. 27 | */ 28 | template class VectorSpace, typename... VSTypeParams> 30 | class UnitarySpace : public VectorSpace { 31 | private: 32 | using Base = VectorSpace; 33 | 34 | public: 35 | using fp_type = typename Base::fp_type; 36 | using Unitary = typename Base::Vector; 37 | 38 | template 39 | UnitarySpace(ForArgs&&... args) : Base(args...) {} 40 | 41 | static Unitary CreateUnitary(unsigned num_qubits) { 42 | return Base::Create(num_qubits); 43 | } 44 | 45 | static Unitary CreateUnitary(fp_type* p, unsigned num_qubits) { 46 | return Base::Create(p, num_qubits); 47 | } 48 | 49 | static Unitary NullUnitary() { 50 | return Base::Null(); 51 | } 52 | 53 | static uint64_t Size(unsigned num_qubits) { 54 | return uint64_t{1} << num_qubits; 55 | }; 56 | 57 | void CopyUnitary(const Unitary& src, Unitary& dest) const { 58 | Base::Copy(src, dest); 59 | } 60 | }; 61 | 62 | } // namespace unitary 63 | } // namespace qsim 64 | 65 | #endif // UNITARYSPACE_H_ 66 | -------------------------------------------------------------------------------- /tests/unitary_calculator_avx_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "unitary_calculator_testfixture.h" 16 | 17 | #include "gtest/gtest.h" 18 | 19 | #include "../lib/formux.h" 20 | #include "../lib/unitary_calculator_avx.h" 21 | 22 | namespace qsim { 23 | namespace unitary { 24 | namespace { 25 | 26 | TEST(UnitaryCalculatorAVXTest, ApplyGate1) { 27 | TestApplyGate1>(); 28 | } 29 | 30 | TEST(UnitaryCalculatorAVXTest, ApplyControlledGate1) { 31 | TestApplyControlledGate1>(); 32 | } 33 | 34 | TEST(UnitaryCalculatorAVXTest, ApplyGate2) { 35 | TestApplyGate2>(); 36 | } 37 | 38 | TEST(UnitaryCalculatorAVXTest, ApplyControlledGate2) { 39 | TestApplyControlledGate2>(); 40 | } 41 | 42 | TEST(UnitaryCalculatorAVXTest, ApplyFusedGate) { 43 | TestApplyFusedGate>(); 44 | } 45 | 46 | TEST(UnitaryCalculatorAVXTest, ApplyGates) { 47 | TestApplyGates>(false); 48 | } 49 | 50 | TEST(UnitaryCalculatorAVXTest, ApplyControlledGates) { 51 | TestApplyControlledGates>(false); 52 | } 53 | 54 | TEST(UnitaryCalculatorAVXTest, SmallCircuits) { 55 | TestSmallCircuits>(); 56 | } 57 | 58 | } // namespace 59 | } // namespace unitary 60 | } // namespace qsim 61 | 62 | int main(int argc, char** argv) { 63 | ::testing::InitGoogleTest(&argc, argv); 64 | return RUN_ALL_TESTS(); 65 | } 66 | -------------------------------------------------------------------------------- /tests/unitary_calculator_sse_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "unitary_calculator_testfixture.h" 16 | 17 | #include "gtest/gtest.h" 18 | 19 | #include "../lib/formux.h" 20 | #include "../lib/unitary_calculator_sse.h" 21 | 22 | namespace qsim { 23 | namespace unitary { 24 | namespace { 25 | 26 | TEST(UnitaryCalculatorSSETest, ApplyGate1) { 27 | TestApplyGate1>(); 28 | } 29 | 30 | TEST(UnitaryCalculatorSSETest, ApplyControlledGate1) { 31 | TestApplyControlledGate1>(); 32 | } 33 | 34 | TEST(UnitaryCalculatorSSETest, ApplyGate2) { 35 | TestApplyGate2>(); 36 | } 37 | 38 | TEST(UnitaryCalculatorSSETest, ApplyControlledGate2) { 39 | TestApplyControlledGate2>(); 40 | } 41 | 42 | TEST(UnitaryCalculatorSSETest, ApplyFusedGate) { 43 | TestApplyFusedGate>(); 44 | } 45 | 46 | TEST(UnitaryCalculatorSSETest, ApplyGates) { 47 | TestApplyGates>(false); 48 | } 49 | 50 | TEST(UnitaryCalculatorSSETest, ApplyControlledGates) { 51 | TestApplyControlledGates>(false); 52 | } 53 | 54 | TEST(UnitaryCalculatorSSETest, SmallCircuits) { 55 | TestSmallCircuits>(); 56 | } 57 | 58 | } // namespace 59 | } // namespace unitary 60 | } // namespace qsim 61 | 62 | int main(int argc, char** argv) { 63 | ::testing::InitGoogleTest(&argc, argv); 64 | return RUN_ALL_TESTS(); 65 | } 66 | -------------------------------------------------------------------------------- /lib/circuit.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #ifndef CIRCUIT_H_ 16 | #define CIRCUIT_H_ 17 | 18 | #include 19 | 20 | namespace qsim { 21 | 22 | /** 23 | * A collection of gates. This object is consumed by `QSim[h]Runner.Run()`. 24 | */ 25 | template 26 | struct Circuit { 27 | unsigned num_qubits; 28 | /** 29 | * The set of gates to be run. Gate times should be ordered. 30 | */ 31 | std::vector gates; 32 | }; 33 | 34 | namespace detail { 35 | 36 | /** 37 | * An adapter for vectors of gates. 38 | */ 39 | template 40 | struct Gates; 41 | 42 | template 43 | struct Gates> { 44 | static const std::vector& get(const qsim::Circuit& circuit) { 45 | return circuit.gates; 46 | } 47 | 48 | static const Gate& gate(const Gate& g) { 49 | return g; 50 | } 51 | }; 52 | 53 | template 54 | struct Gates> { 55 | static const std::vector& get(const std::vector& gates) { 56 | return gates; 57 | } 58 | 59 | static const Gate& gate(const Gate& g) { 60 | return g; 61 | } 62 | }; 63 | 64 | template 65 | struct Gates> { 66 | static const std::vector& get(const std::vector& gates) { 67 | return gates; 68 | } 69 | 70 | static const Gate& gate(const Gate* g) { 71 | return *g; 72 | } 73 | }; 74 | 75 | } // namespace detail 76 | 77 | } // namespace qsim 78 | 79 | #endif // CIRCUIT_H_ 80 | -------------------------------------------------------------------------------- /pybind_interface/hip/pybind_main_hip.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "pybind_main_hip.h" 16 | 17 | #include "../../lib/fuser_mqubit.h" 18 | #include "../../lib/gates_cirq.h" 19 | #include "../../lib/io.h" 20 | #include "../../lib/run_qsim.h" 21 | #include "../../lib/simulator_cuda.h" 22 | 23 | namespace qsim { 24 | using Simulator = SimulatorCUDA; 25 | 26 | struct Factory { 27 | Factory( 28 | unsigned num_sim_threads, 29 | unsigned num_state_threads, 30 | unsigned num_dblocks 31 | ) : ss_params{num_state_threads, num_dblocks} {} 32 | 33 | using Simulator = qsim::Simulator; 34 | using StateSpace = Simulator::StateSpace; 35 | 36 | using Gate = Cirq::GateCirq; 37 | using Runner = QSimRunner, Factory>; 38 | using RunnerQT = 39 | QSimRunner, Factory>; 40 | using RunnerParameter = Runner::Parameter; 41 | using NoisyRunner = qsim::QuantumTrajectorySimulator; 42 | using NoisyRunnerParameter = NoisyRunner::Parameter; 43 | 44 | StateSpace CreateStateSpace() const { 45 | return StateSpace(ss_params); 46 | } 47 | 48 | Simulator CreateSimulator() const { 49 | return Simulator(); 50 | } 51 | 52 | StateSpace::Parameter ss_params; 53 | }; 54 | 55 | inline void SetFlushToZeroAndDenormalsAreZeros() {} 56 | inline void ClearFlushToZeroAndDenormalsAreZeros() {} 57 | } 58 | 59 | #include "../pybind_main.cpp" 60 | -------------------------------------------------------------------------------- /docs/tutorials/gcp_before_you_begin.md: -------------------------------------------------------------------------------- 1 | # Before you begin 2 | 3 | The following tutorials demonstrate how to configure the Google Cloud Platform 4 | to run quantum simulations with qsim. 5 | 6 | You can use Google Cloud to run high-performance CPU-based simulations or 7 | GPU-based simulations, depending on your requirements. For more information 8 | about making a choice between CPU- and GPU-based simulations, see 9 | [Choosing hardware for your qsim simulation](../choose_hw). 10 | 11 | This tutorial depends on resources provided by the Google Cloud Platform. 12 | 13 | * **Ensure that you have a Google Cloud Platform project.** You can reuse an 14 | existing project, or create a new one, from your 15 | [project dashboard](https://console.cloud.google.com/projectselector2/home/dashboard). 16 | * For more information about Google Cloud projects, see 17 | [Creating and managing projects](https://cloud.google.com/resource-manager/docs/creating-managing-projects) 18 | in the Google Cloud documentation. 19 | * **Ensure that billing is enabled for your project.** 20 | * For more information about billing, see 21 | [Enable, disable, or change billing for a project](https://cloud.google.com/billing/docs/how-to/modify-project#enable-billing) 22 | in the Google Cloud documentation. 23 | * **Estimate costs for your project** Use the 24 | [Google Cloud Pricing Calculator](https://cloud.google.com/products/calculator) 25 | to estimate the scale of the costs you might incur, based on your projected 26 | usage. The resources that you use to simulate a quantum circuit on the 27 | Google Cloud platform are billable. 28 | * **Enable the Compute Engine API for your project.** You can enable APIs from 29 | the [API Library Console](https://console.cloud.google.com/apis/library). On 30 | the console, in the search box, enter "compute engine api" to find the API 31 | and click through to Enable it. 32 | * For more information about enabling the Compute Engine API, see 33 | [Getting Started](https://cloud.google.com/apis/docs/getting-started) in 34 | the Google Cloud documentation. 35 | -------------------------------------------------------------------------------- /pybind_interface/custatevec/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC. All Rights Reserved. 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 | cmake_minimum_required(VERSION 3.28) 16 | project(qsim LANGUAGES CXX CUDA) 17 | 18 | if(WIN32) 19 | add_compile_options(/openmp) 20 | # Add /O2 to any configuration that is NOT Debug. 21 | # This prevents a conflict with /RTC1 in DEBUG builds. 22 | add_compile_options($<$>:/O2>) 23 | else() 24 | add_compile_options(-O3 -flto=auto) 25 | endif() 26 | 27 | if(APPLE) 28 | include_directories( 29 | "/usr/local/include" 30 | "/usr/local/opt/llvm/include" 31 | "/opt/homebrew/include" 32 | "/opt/homebrew/opt/llvm@19/include" 33 | ) 34 | link_directories( 35 | "/usr/local/lib" 36 | "/usr/local/opt/llvm/lib" 37 | "/opt/homebrew/lib" 38 | "/opt/homebrew/opt/llvm@19/lib" 39 | ) 40 | endif() 41 | 42 | include(../GetPybind11.cmake) 43 | find_package(Python3 3.10 REQUIRED) 44 | 45 | include_directories(${pybind11_INCLUDE_DIRS}) 46 | 47 | include_directories($ENV{CUQUANTUM_ROOT}/include) 48 | link_directories($ENV{CUQUANTUM_ROOT}/lib $ENV{CUQUANTUM_ROOT}/lib64) 49 | 50 | add_library(qsim_custatevec MODULE pybind_main_custatevec.cpp) 51 | target_link_libraries(qsim_custatevec -lcustatevec -lcublas) 52 | 53 | set_target_properties(qsim_custatevec PROPERTIES 54 | PREFIX "${PYTHON_MODULE_PREFIX}" 55 | SUFFIX "${PYTHON_MODULE_EXTENSION}" 56 | ) 57 | set_source_files_properties(pybind_main_custatevec.cpp PROPERTIES LANGUAGE CUDA) 58 | 59 | target_link_libraries(qsim_custatevec OpenMP::OpenMP_CXX) 60 | -------------------------------------------------------------------------------- /pybind_interface/cuda/pybind_main_cuda.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "pybind_main_cuda.h" 16 | 17 | #include "../../lib/fuser_mqubit.h" 18 | #include "../../lib/gates_cirq.h" 19 | #include "../../lib/io.h" 20 | #include "../../lib/run_qsim.h" 21 | #include "../../lib/simulator_cuda.h" 22 | 23 | namespace qsim { 24 | using Simulator = SimulatorCUDA; 25 | 26 | struct Factory { 27 | Factory( 28 | unsigned num_sim_threads, 29 | unsigned num_state_threads, 30 | unsigned num_dblocks 31 | ) { 32 | ss_params.num_threads = num_state_threads; 33 | ss_params.num_dblocks = num_dblocks; 34 | } 35 | 36 | using Simulator = qsim::Simulator; 37 | using StateSpace = Simulator::StateSpace; 38 | 39 | using Gate = Cirq::GateCirq; 40 | using Runner = QSimRunner, Factory>; 41 | using RunnerQT = 42 | QSimRunner, Factory>; 43 | using RunnerParameter = Runner::Parameter; 44 | using NoisyRunner = qsim::QuantumTrajectorySimulator; 45 | using NoisyRunnerParameter = NoisyRunner::Parameter; 46 | 47 | StateSpace CreateStateSpace() const { 48 | return StateSpace(ss_params); 49 | } 50 | 51 | Simulator CreateSimulator() const { 52 | return Simulator(); 53 | } 54 | 55 | StateSpace::Parameter ss_params; 56 | }; 57 | 58 | inline void SetFlushToZeroAndDenormalsAreZeros() {} 59 | inline void ClearFlushToZeroAndDenormalsAreZeros() {} 60 | } 61 | 62 | #include "../pybind_main.cpp" 63 | -------------------------------------------------------------------------------- /pybind_interface/sse/pybind_main_sse.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "pybind_main_sse.h" 16 | 17 | #include "../../lib/formux.h" 18 | #include "../../lib/fuser_mqubit.h" 19 | #include "../../lib/gates_cirq.h" 20 | #include "../../lib/io.h" 21 | #include "../../lib/run_qsim.h" 22 | #include "../../lib/simulator_sse.h" 23 | #include "../../lib/util_cpu.h" 24 | 25 | namespace qsim { 26 | template 27 | using Simulator = SimulatorSSE; 28 | 29 | struct Factory { 30 | // num_state_threads and num_dblocks are unused, but kept for consistency 31 | // with the GPU Factory. 32 | Factory( 33 | unsigned num_sim_threads, 34 | unsigned num_state_threads, 35 | unsigned num_dblocks) : num_threads(num_sim_threads) {} 36 | 37 | using Simulator = qsim::Simulator; 38 | using StateSpace = Simulator::StateSpace; 39 | 40 | using Gate = Cirq::GateCirq; 41 | using Runner = QSimRunner, Factory>; 42 | using RunnerQT = 43 | QSimRunner, Factory>; 44 | using RunnerParameter = Runner::Parameter; 45 | using NoisyRunner = qsim::QuantumTrajectorySimulator; 46 | using NoisyRunnerParameter = NoisyRunner::Parameter; 47 | 48 | StateSpace CreateStateSpace() const { 49 | return StateSpace(num_threads); 50 | } 51 | 52 | Simulator CreateSimulator() const { 53 | return Simulator(num_threads); 54 | } 55 | 56 | unsigned num_threads; 57 | }; 58 | } 59 | 60 | #include "../pybind_main.cpp" 61 | -------------------------------------------------------------------------------- /pybind_interface/avx2/pybind_main_avx2.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "pybind_main_avx2.h" 16 | 17 | #include "../../lib/formux.h" 18 | #include "../../lib/fuser_mqubit.h" 19 | #include "../../lib/gates_cirq.h" 20 | #include "../../lib/io.h" 21 | #include "../../lib/run_qsim.h" 22 | #include "../../lib/simulator_avx.h" 23 | #include "../../lib/util_cpu.h" 24 | 25 | namespace qsim { 26 | template 27 | using Simulator = SimulatorAVX; 28 | 29 | struct Factory { 30 | // num_state_threads and num_dblocks are unused, but kept for consistency 31 | // with the GPU Factory. 32 | Factory( 33 | unsigned num_sim_threads, 34 | unsigned num_state_threads, 35 | unsigned num_dblocks) : num_threads(num_sim_threads) {} 36 | 37 | using Simulator = qsim::Simulator; 38 | using StateSpace = Simulator::StateSpace; 39 | 40 | using Gate = Cirq::GateCirq; 41 | using Runner = QSimRunner, Factory>; 42 | using RunnerQT = 43 | QSimRunner, Factory>; 44 | using RunnerParameter = Runner::Parameter; 45 | using NoisyRunner = qsim::QuantumTrajectorySimulator; 46 | using NoisyRunnerParameter = NoisyRunner::Parameter; 47 | 48 | StateSpace CreateStateSpace() const { 49 | return StateSpace(num_threads); 50 | } 51 | 52 | Simulator CreateSimulator() const { 53 | return Simulator(num_threads); 54 | } 55 | 56 | unsigned num_threads; 57 | }; 58 | } 59 | 60 | #include "../pybind_main.cpp" 61 | -------------------------------------------------------------------------------- /pybind_interface/basic/pybind_main_basic.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "pybind_main_basic.h" 16 | 17 | #include "../../lib/formux.h" 18 | #include "../../lib/fuser_mqubit.h" 19 | #include "../../lib/gates_cirq.h" 20 | #include "../../lib/io.h" 21 | #include "../../lib/run_qsim.h" 22 | #include "../../lib/simulator_basic.h" 23 | #include "../../lib/util_cpu.h" 24 | 25 | namespace qsim { 26 | template 27 | using Simulator = SimulatorBasic; 28 | 29 | struct Factory { 30 | // num_state_threads and num_dblocks are unused, but kept for consistency 31 | // with the GPU Factory. 32 | Factory( 33 | unsigned num_sim_threads, 34 | unsigned num_state_threads, 35 | unsigned num_dblocks) : num_threads(num_sim_threads) {} 36 | 37 | using Simulator = qsim::Simulator; 38 | using StateSpace = Simulator::StateSpace; 39 | 40 | using Gate = Cirq::GateCirq; 41 | using Runner = QSimRunner, Factory>; 42 | using RunnerQT = 43 | QSimRunner, Factory>; 44 | using RunnerParameter = Runner::Parameter; 45 | using NoisyRunner = qsim::QuantumTrajectorySimulator; 46 | using NoisyRunnerParameter = NoisyRunner::Parameter; 47 | 48 | StateSpace CreateStateSpace() const { 49 | return StateSpace(num_threads); 50 | } 51 | 52 | Simulator CreateSimulator() const { 53 | return Simulator(num_threads); 54 | } 55 | 56 | unsigned num_threads; 57 | }; 58 | } 59 | 60 | #include "../pybind_main.cpp" 61 | -------------------------------------------------------------------------------- /pybind_interface/avx512/pybind_main_avx512.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "pybind_main_avx512.h" 16 | 17 | #include "../../lib/formux.h" 18 | #include "../../lib/fuser_mqubit.h" 19 | #include "../../lib/gates_cirq.h" 20 | #include "../../lib/io.h" 21 | #include "../../lib/run_qsim.h" 22 | #include "../../lib/simulator_avx512.h" 23 | #include "../../lib/util_cpu.h" 24 | 25 | namespace qsim { 26 | template 27 | using Simulator = SimulatorAVX512; 28 | 29 | struct Factory { 30 | // num_state_threads and num_dblocks are unused, but kept for consistency 31 | // with the GPU Factory. 32 | Factory( 33 | unsigned num_sim_threads, 34 | unsigned num_state_threads, 35 | unsigned num_dblocks) : num_threads(num_sim_threads) {} 36 | 37 | using Simulator = qsim::Simulator; 38 | using StateSpace = Simulator::StateSpace; 39 | 40 | using Gate = Cirq::GateCirq; 41 | using Runner = QSimRunner, Factory>; 42 | using RunnerQT = 43 | QSimRunner, Factory>; 44 | using RunnerParameter = Runner::Parameter; 45 | using NoisyRunner = qsim::QuantumTrajectorySimulator; 46 | using NoisyRunnerParameter = NoisyRunner::Parameter; 47 | 48 | StateSpace CreateStateSpace() const { 49 | return StateSpace(num_threads); 50 | } 51 | 52 | Simulator CreateSimulator() const { 53 | return Simulator(num_threads); 54 | } 55 | 56 | unsigned num_threads; 57 | }; 58 | } 59 | 60 | #include "../pybind_main.cpp" 61 | -------------------------------------------------------------------------------- /install/tests/Dockerfile: -------------------------------------------------------------------------------- 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 | # Base OS 16 | FROM ubuntu:24.04 17 | 18 | # Allow passing this variable in from the outside. 19 | ARG CUDA_PATH 20 | ENV PATH="$CUDA_PATH/bin:$PATH" 21 | 22 | # Update package list & install some basic tools we'll need. 23 | # hadolint ignore=DL3009,DL3008 24 | RUN apt-get update && \ 25 | apt-get install -y make g++ wget git --no-install-recommends && \ 26 | apt-get install -y python3-dev python3-pip python3-venv --no-install-recommends 27 | 28 | # Ubuntu 24's version of CMake is 3.28. We need a newer version. 29 | RUN apt-get remove --purge --auto-remove cmake 30 | RUN wget -q https://github.com/Kitware/CMake/releases/download/v3.28.1/cmake-3.28.1-linux-x86_64.sh && \ 31 | sh cmake-3.28.1-linux-x86_64.sh --prefix=/usr/local --skip-license 32 | 33 | # Copy qsim files from the outside-Docker location to an inside-Docker location. 34 | COPY ./ /qsim/ 35 | 36 | # Switch to that location. 37 | WORKDIR /qsim/ 38 | 39 | # Create venv to avoid collision between system packages and what we install. 40 | RUN python3 -m venv --upgrade-deps /qsim/test_env 41 | 42 | # Activate the venv. 43 | ENV PATH="/qsim/test_env/bin:$PATH" 44 | 45 | # Install qsim from sources. 46 | # Note: use pip3 here, not python3 -m pip. We need to make sure to get the pip 47 | # installed inside the venv, because that one has the correct sys.path. 48 | # hadolint ignore=DL3042 49 | RUN pip3 install -v /qsim/ 50 | 51 | # Copy the tests to a non-qsim directory. 52 | COPY ./qsimcirq_tests/ /test-install/ 53 | 54 | # Run the tests from that location. 55 | WORKDIR /test-install/ 56 | ENTRYPOINT ["python3", "-m", "pytest", "./"] 57 | -------------------------------------------------------------------------------- /tests/unitary_calculator_avx512_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "unitary_calculator_testfixture.h" 16 | 17 | #include "gtest/gtest.h" 18 | 19 | #if defined(__AVX512F__) && !defined(_WIN32) 20 | 21 | #include "../lib/formux.h" 22 | #include "../lib/unitary_calculator_avx512.h" 23 | 24 | namespace qsim { 25 | namespace unitary { 26 | namespace { 27 | 28 | TEST(UnitaryCalculatorAVX512Test, ApplyGate1) { 29 | TestApplyGate1>(); 30 | } 31 | 32 | TEST(UnitaryCalculatorAVX512Test, ApplyControlledGate1) { 33 | TestApplyControlledGate1>(); 34 | } 35 | 36 | TEST(UnitaryCalculatorAVX512Test, ApplyGate2) { 37 | TestApplyGate2>(); 38 | } 39 | 40 | TEST(UnitaryCalculatorAVX512Test, ApplyControlledGate2) { 41 | TestApplyControlledGate2>(); 42 | } 43 | 44 | TEST(UnitaryCalculatorAVX512Test, ApplyFusedGate) { 45 | TestApplyFusedGate>(); 46 | } 47 | 48 | TEST(UnitaryCalculatorAVX512Test, ApplyGates) { 49 | TestApplyGates>(false); 50 | } 51 | 52 | TEST(UnitaryCalculatorAVX512Test, ApplyControlledGates) { 53 | TestApplyControlledGates>(false); 54 | } 55 | 56 | TEST(UnitaryCalculatorAVX512Test, SmallCircuits) { 57 | TestSmallCircuits>(); 58 | } 59 | 60 | } // namespace 61 | } // namespace unitary 62 | } // namespace qsim 63 | 64 | #endif // __AVX512F__ && !_WIN32 65 | 66 | int main(int argc, char** argv) { 67 | ::testing::InitGoogleTest(&argc, argv); 68 | return RUN_ALL_TESTS(); 69 | } 70 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 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 | # Base OS 16 | FROM ubuntu:24.04 AS qsim-base 17 | 18 | # Allow passing this variable in from the outside. 19 | ARG CUDA_PATH 20 | ENV PATH="$CUDA_PATH/bin:$PATH" 21 | 22 | # Update package list & install some basic tools we'll need. 23 | # hadolint ignore=DL3009,DL3008 24 | RUN apt-get update && \ 25 | apt-get install -y make g++ wget git --no-install-recommends && \ 26 | apt-get install -y python3-dev python3-pip python3-venv --no-install-recommends 27 | 28 | # Ubuntu 24's version of CMake is 3.28. We need a newer version. 29 | RUN apt-get remove --purge --auto-remove cmake 30 | RUN wget -q https://github.com/Kitware/CMake/releases/download/v3.28.1/cmake-3.28.1-linux-x86_64.sh && \ 31 | sh cmake-3.28.1-linux-x86_64.sh --prefix=/usr/local --skip-license 32 | 33 | # Copy relevant files for simulation. 34 | COPY ./Makefile /qsim/Makefile 35 | COPY ./apps/ /qsim/apps/ 36 | COPY ./circuits/ /qsim/circuits/ 37 | COPY ./lib/ /qsim/lib/ 38 | COPY ./pybind_interface/ /qsim/lib/ 39 | COPY ./qsimcirq_tests/ /qsim/qsimcirq_tests/ 40 | COPY ./requirements.txt /qsim/requirements.txt 41 | COPY ./dev-requirements.txt /qsim/dev-requirements.txt 42 | 43 | # Create venv to avoid collision between system packages and what we install. 44 | RUN python3 -m venv --upgrade-deps test_env 45 | 46 | # Activate venv. 47 | ENV PATH="/test_env/bin:$PATH" 48 | 49 | # Install qsim requirements. 50 | # hadolint ignore=DL3042 51 | RUN python3 -m pip install -r /qsim/requirements.txt && \ 52 | python3 -m pip install -r /qsim/dev-requirements.txt 53 | 54 | # Compile qsim. 55 | WORKDIR /qsim/ 56 | RUN make -j qsim 57 | 58 | ENTRYPOINT ["/qsim/apps/qsim_base.x"] 59 | -------------------------------------------------------------------------------- /qsimcirq/__init__.py: -------------------------------------------------------------------------------- 1 | # pylint: disable=wrong-import-position 2 | # Copyright 2019 Google LLC. All Rights Reserved. 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 | import importlib 17 | 18 | from qsimcirq import qsim_decide 19 | 20 | 21 | def _load_simd_qsim(): 22 | instr = qsim_decide.detect_instructions() 23 | if instr == 0: 24 | qsim = importlib.import_module("qsimcirq.qsim_avx512") 25 | elif instr == 1: 26 | qsim = importlib.import_module("qsimcirq.qsim_avx2") 27 | elif instr == 2: 28 | qsim = importlib.import_module("qsimcirq.qsim_sse") 29 | else: 30 | qsim = importlib.import_module("qsimcirq.qsim_basic") 31 | return qsim 32 | 33 | 34 | def _load_qsim_gpu(): 35 | instr = qsim_decide.detect_gpu() 36 | if instr == 0: 37 | qsim_gpu = importlib.import_module("qsimcirq.qsim_cuda") 38 | elif instr == 2: 39 | qsim_gpu = importlib.import_module("qsimcirq.qsim_hip") 40 | else: 41 | qsim_gpu = None 42 | return qsim_gpu 43 | 44 | 45 | def _load_qsim_custatevec(): 46 | instr = qsim_decide.detect_custatevec() 47 | if instr == 1: 48 | qsim_custatevec = importlib.import_module("qsimcirq.qsim_custatevec") 49 | else: 50 | qsim_custatevec = None 51 | return qsim_custatevec 52 | 53 | 54 | qsim = _load_simd_qsim() 55 | qsim_gpu = _load_qsim_gpu() 56 | qsim_custatevec = _load_qsim_custatevec() 57 | 58 | # Note: the following imports must remain at the bottom of this file. 59 | 60 | from qsimcirq._version import __version__ 61 | 62 | from .qsim_circuit import QSimCircuit, add_op_to_circuit, add_op_to_opstring 63 | from .qsim_simulator import QSimOptions, QSimSimulator 64 | from .qsimh_simulator import QSimhSimulator 65 | -------------------------------------------------------------------------------- /pybind_interface/cuda/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC. All Rights Reserved. 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 | cmake_minimum_required(VERSION 3.28) 16 | project(qsim LANGUAGES CXX CUDA) 17 | 18 | if(WIN32) 19 | # Always apply AVX2 and openmp on Windows 20 | add_compile_options(/openmp) 21 | # Add /O2 to any configuration that is NOT Debug. 22 | # This prevents a conflict with /RTC1 in DEBUG builds. 23 | add_compile_options($<$>:/O2>) 24 | else() 25 | add_compile_options(-O3 -flto=auto) 26 | endif() 27 | 28 | if(APPLE) 29 | include_directories( 30 | "/usr/local/include" 31 | "/usr/local/opt/llvm/include" 32 | "/opt/homebrew/include" 33 | "/opt/homebrew/opt/llvm@19/include" 34 | ) 35 | link_directories( 36 | "/usr/local/lib" 37 | "/usr/local/opt/llvm/lib" 38 | "/opt/homebrew/lib" 39 | "/opt/homebrew/opt/llvm@19/lib" 40 | ) 41 | endif() 42 | 43 | include(../GetPybind11.cmake) 44 | include(../GetCUDAARCHS.cmake) 45 | 46 | find_package(Python 3.10 REQUIRED) 47 | 48 | include_directories(${PYTHON_INCLUDE_DIRS}) 49 | if(pybind11_FOUND) 50 | include_directories(${pybind11_INCLUDE_DIRS}) 51 | else() # means pybind11 has been fetched in GetPybind11.cmake 52 | include_directories(${pybind11_SOURCE_DIR}/include) 53 | endif() 54 | 55 | add_library(qsim_cuda MODULE pybind_main_cuda.cpp) 56 | set_target_properties(qsim_cuda PROPERTIES 57 | CUDA_ARCHITECTURES "${CMAKE_CUDA_ARCHITECTURES}" 58 | PREFIX "${PYTHON_MODULE_PREFIX}" 59 | SUFFIX "${PYTHON_MODULE_EXTENSION}" 60 | ) 61 | set_source_files_properties(pybind_main_cuda.cpp PROPERTIES LANGUAGE CUDA) 62 | 63 | target_link_libraries(qsim_cuda OpenMP::OpenMP_CXX) 64 | -------------------------------------------------------------------------------- /lib/seqfor.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #ifndef SEQFOR_H_ 16 | #define SEQFOR_H_ 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | namespace qsim { 23 | 24 | /** 25 | * Helper struct for executing for loops in series. 26 | */ 27 | struct SequentialFor { 28 | explicit SequentialFor(unsigned num_threads) {} 29 | 30 | // SequentialFor does not have any state. So all its methods can be static. 31 | 32 | static uint64_t GetIndex0(uint64_t size, unsigned thread_id) { 33 | return 0; 34 | } 35 | 36 | static uint64_t GetIndex1(uint64_t size, unsigned thread_id) { 37 | return size; 38 | } 39 | 40 | template 41 | static void Run(uint64_t size, Function&& func, Args&&... args) { 42 | for (uint64_t i = 0; i < size; ++i) { 43 | func(1, 0, i, args...); 44 | } 45 | } 46 | 47 | template 48 | static std::vector RunReduceP( 49 | uint64_t size, Function&& func, Op&& op, Args&&... args) { 50 | typename Op::result_type result = 0; 51 | 52 | for (uint64_t i = 0; i < size; ++i) { 53 | result = op(result, func(1, 0, i, args...)); 54 | } 55 | 56 | return std::vector(1, result); 57 | } 58 | 59 | template 60 | static typename Op::result_type RunReduce(uint64_t size, Function&& func, 61 | Op&& op, Args&&... args) { 62 | return RunReduceP(size, func, std::move(op), args...)[0]; 63 | } 64 | }; 65 | 66 | } // namespace qsim 67 | 68 | #endif // SEQFOR_H_ 69 | -------------------------------------------------------------------------------- /docs/_scripts/build_api_docs.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 The qsim Authors. All Rights Reserved. 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 | # http://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 | """Tool to generate external api_docs for qsim shameless copy from TFQ.""" 16 | import os 17 | 18 | from absl import app, flags 19 | from tensorflow_docs.api_generator import generate_lib, public_api 20 | 21 | import qsimcirq as qs 22 | 23 | flags.DEFINE_string("output_dir", "/tmp/qsim_api", "Where to output the docs") 24 | 25 | flags.DEFINE_string( 26 | "code_url_prefix", 27 | ("https://github.com/quantumlib/qsim/tree/main/" "qsimcirq"), 28 | "The url prefix for links to code.", 29 | ) 30 | 31 | flags.DEFINE_bool( 32 | "search_hints", True, "Include metadata search hints in the generated files" 33 | ) 34 | 35 | flags.DEFINE_string( 36 | "site_path", "/quark/qsim/api_docs/python", "Path prefix in the _toc.yaml" 37 | ) 38 | 39 | FLAGS = flags.FLAGS 40 | 41 | 42 | def main(unused_argv): 43 | 44 | doc_generator = generate_lib.DocGenerator( 45 | root_title="qsim", 46 | py_modules=[("qsimcirq", qs)], 47 | base_dir=os.path.dirname(qs.__file__), 48 | code_url_prefix=FLAGS.code_url_prefix, 49 | search_hints=FLAGS.search_hints, 50 | site_path=FLAGS.site_path, 51 | callbacks=[public_api.local_definitions_filter], 52 | private_map={ 53 | "qsimcirq": [ 54 | "qsim_circuit", 55 | "qsimc", 56 | "qsim_simulator", 57 | "qsimh_simulator", 58 | ] 59 | }, 60 | ) 61 | 62 | doc_generator.build(output_dir=FLAGS.output_dir) 63 | 64 | 65 | if __name__ == "__main__": 66 | app.run(main) 67 | -------------------------------------------------------------------------------- /.pylintrc: -------------------------------------------------------------------------------- 1 | [MAIN] 2 | load-plugins=pylint.extensions.docstyle,pylint.extensions.docparams 3 | max-line-length=88 4 | ignore-paths=eigen/*,tests/googletest/* 5 | disable=all 6 | output-format=colorized 7 | score=no 8 | reports=no 9 | enable= 10 | abstract-class-instantiated, 11 | anomalous-backslash-in-string, 12 | assert-on-tuple, 13 | assignment-from-no-return, 14 | bad-chained-comparison, 15 | bad-indentation, 16 | bad-mcs-classmethod-argument, 17 | bad-mcs-method-argument, 18 | bad-option-value, 19 | bad-reversed-sequence, 20 | bad-super-call, 21 | consider-merging-isinstance, 22 | consider-using-f-string, 23 | continue-in-finally, 24 | dangerous-default-value, 25 | deprecated-decorator, 26 | docstyle, 27 | duplicate-argument-name, 28 | expression-not-assigned, 29 | f-string-without-interpolation, 30 | function-redefined, 31 | inconsistent-mro, 32 | init-is-generator, 33 | line-too-long, 34 | lost-exception, 35 | method-hidden, 36 | missing-kwoa, 37 | missing-param-doc, 38 | missing-raises-doc, 39 | mixed-line-endings, 40 | no-value-for-parameter, 41 | nonexistent-operator, 42 | not-in-loop, 43 | pointless-statement, 44 | raising-bad-type, 45 | raising-format-tuple, 46 | redefined-builtin, 47 | redefined-slots-in-subclass, 48 | return-arg-in-generator, 49 | return-in-init, 50 | return-outside-function, 51 | self-cls-assignment, 52 | shadowed-import, 53 | simplifiable-condition, 54 | simplifiable-if-statement, 55 | singleton-comparison, 56 | too-many-function-args, 57 | trailing-whitespace, 58 | undefined-variable, 59 | unexpected-keyword-arg, 60 | unhashable-dict-key, 61 | unnecessary-pass, 62 | unnecessary-semicolon, 63 | unneeded-not, 64 | unreachable, 65 | unrecognized-inline-option, 66 | unused-import, 67 | unused-variable, 68 | unused-wildcard-import, 69 | use-maxsplit-arg, 70 | useless-suppression, 71 | using-constant-test, 72 | wildcard-import, 73 | wrong-import-order, 74 | wrong-import-position, 75 | yield-outside-function 76 | 77 | # Ignore long lines containing urls or pylint directives. 78 | ignore-long-lines=^(.*#\w*pylint: disable.*|\s*(# )?[<\[\(]?https?://\S+[>\]\)]?)$ 79 | -------------------------------------------------------------------------------- /tests/unitary_calculator_basic_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "unitary_calculator_testfixture.h" 16 | 17 | #include "gtest/gtest.h" 18 | 19 | #include "../lib/formux.h" 20 | #include "../lib/unitary_calculator_basic.h" 21 | 22 | namespace qsim { 23 | namespace unitary { 24 | namespace { 25 | 26 | TEST(UnitaryCalculatorBasicTest, ApplyGate1) { 27 | TestApplyGate1>(); 28 | } 29 | 30 | TEST(UnitaryCalculatorBasicTest, ApplyControlledGate1) { 31 | TestApplyControlledGate1>(); 32 | } 33 | 34 | TEST(UnitaryCalculatorBasicTest, ApplyGate2) { 35 | TestApplyGate2>(); 36 | } 37 | 38 | TEST(UnitaryCalculatorBasicTest, ApplyControlledGate2) { 39 | TestApplyControlledGate2>(); 40 | } 41 | 42 | TEST(UnitaryCalculatorBasicTest, ApplyFusedGate) { 43 | TestApplyFusedGate>(); 44 | } 45 | 46 | TEST(UnitaryCalculatorBasicTest, ApplyGates) { 47 | TestApplyGates>(false); 48 | TestApplyGates>(true); 49 | } 50 | 51 | TEST(UnitaryCalculatorBasicTest, ApplyControlledGates) { 52 | TestApplyControlledGates>(false); 53 | TestApplyControlledGates>(true); 54 | } 55 | 56 | TEST(UnitaryCalculatorBasicTest, SmallCircuits) { 57 | TestSmallCircuits>(); 58 | } 59 | 60 | } // namespace 61 | } // namespace unitary 62 | } // namespace qsim 63 | 64 | int main(int argc, char** argv) { 65 | ::testing::InitGoogleTest(&argc, argv); 66 | return RUN_ALL_TESTS(); 67 | } 68 | -------------------------------------------------------------------------------- /.github/workflows/cirq_compatibility.yml: -------------------------------------------------------------------------------- 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 | name: 'Cirq dev compatibility test' 16 | run-name: Regularly test code against latest Cirq dev release 17 | 18 | on: 19 | schedule: 20 | - cron: "10 7 * * *" 21 | 22 | workflow_dispatch: 23 | inputs: 24 | debug: 25 | description: 'Run with debugging options' 26 | type: boolean 27 | default: true 28 | 29 | permissions: read-all 30 | 31 | concurrency: 32 | # Cancel any previously-started but still active runs on the same branch. 33 | cancel-in-progress: true 34 | group: ${{github.workflow}}-${{github.event.pull_request.number||github.ref}} 35 | 36 | jobs: 37 | test-compatibility: 38 | name: Test Cirq compatibility 39 | runs-on: ubuntu-24.04 40 | timeout-minutes: 30 41 | steps: 42 | - name: Check out a copy of the git repository 43 | uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0 44 | with: 45 | fetch-depth: 1 46 | submodules: recursive 47 | 48 | - name: Set up Python with caching of pip dependencies 49 | uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v5 50 | with: 51 | python-version: '3.12' 52 | cache: pip 53 | cache-dependency-path: | 54 | requirements.txt 55 | dev-requirements.txt 56 | 57 | - name: Install latest dev version of Cirq 58 | run: pip install --upgrade cirq~=1.0.dev 59 | 60 | - name: Install qsim dev requirements 61 | run: | 62 | pip install -r requirements.txt 63 | pip install -r dev-requirements.txt 64 | 65 | - name: Run Python tests 66 | env: 67 | PYTEST_ADDOPTS: ${{inputs.debug && '-v' || '' }} 68 | run: make -j run-py-tests 69 | -------------------------------------------------------------------------------- /docs/tutorials/amd_gpu.md: -------------------------------------------------------------------------------- 1 | # Support for AMD Instinct™ MI Series Accelerators 2 | 3 | qsim provides support for AMD Instinct accelerators. 4 | The implementation covers the native GPU support in qsim 5 | by utilizing [AMD HIP SDK](https://rocm.docs.amd.com/projects/HIP) 6 | (Heterogeneous-Compute Interface for Portability). 7 | The cuQuantum implementation is currently not covered. 8 | 9 | ## Building 10 | 11 | Building qsim with support for AMD Instinct accelerators requires installation of 12 | [AMD ROCm™ Open Software Platform](https://www.amd.com/en/developer/resources/rocm-hub.html). 13 | Instructions for installing ROCm are available at https://rocm.docs.amd.com/. 14 | 15 | To enable support for AMD GPUs, qsim needs to be built from sources. 16 | This can be done as follows: 17 | 18 | ``` 19 | conda env list 20 | conda create -y -n CirqDevEnv python=3 21 | conda activate CirqDevEnv 22 | pip install pybind11 23 | 24 | git clone https://github.com/quantumlib/qsim.git 25 | cd qsim 26 | 27 | make -j qsim # to build CPU qsim 28 | make -j qsim-hip # to build HIP qsim 29 | make -j pybind # to build Python bindings 30 | make -j cxx-tests # to build CPU tests 31 | make -j hip-tests # to build HIP tests 32 | 33 | pip install . 34 | ``` 35 | 36 | Note: To avoid problems when building qsim with support for AMD GPUs, 37 | make sure to use the latest version of CMake. 38 | 39 | ## Testing 40 | 41 | ### Simulator 42 | 43 | To test the qsim simulator: 44 | 45 | ``` 46 | make run-cxx-tests # to run CPU tests 47 | make run-hip-tests # to run HIP tests 48 | ``` 49 | 50 | or 51 | 52 | ``` 53 | cd tests 54 | for file in *.x; do ./"$file"; done # to run all tests 55 | for file in *_hip_test.x; do ./"$file"; done # to run HIP tests only 56 | ``` 57 | 58 | ### Python Bindings 59 | 60 | To test the Python bindings: 61 | 62 | ``` 63 | make run-py-tests 64 | ``` 65 | 66 | or 67 | 68 | ``` 69 | cd qsimcirq_tests 70 | python3 -m pytest -v qsimcirq_test.py 71 | ``` 72 | 73 | ## Using 74 | 75 | Using qsim on AMD Instinct GPUs is identical to using it on NVIDIA GPUs. 76 | I.e., it is done by passing `use_gpu=True` and `gpu_mode=0` as `qsimcirq.QSimOptions`: 77 | 78 | ``` 79 | simulator = qsimcirq.QSimSimulator(qsim_options=qsimcirq.QSimOptions( 80 | use_gpu=True, 81 | gpu_mode=0, 82 | ... 83 | )) 84 | ``` 85 | 86 | Note: `gpu_mode` has to be set to zero for AMD GPUs, as cuStateVec is not supported. 87 | -------------------------------------------------------------------------------- /dev_tools/test_libs.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Copyright 2025 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 | set -eo pipefail -o errtrace 17 | 18 | declare -r usage="Usage: ${0##*/} [-h | --help | help] [bazel options ...] 19 | Run the programs in tests/, and on Linux, also build the programs in apps/. 20 | 21 | If the first option on the command line is -h, --help, or help, this help text 22 | will be printed and the program will exit. Any other options on the command 23 | line are passed directly to Bazel." 24 | 25 | # Exit early if the user requested help. 26 | if [[ "$1" == "-h" || "$1" == "--help" || "$1" == "help" ]]; then 27 | echo "$usage" 28 | exit 0 29 | fi 30 | 31 | # Look for AVX and SSE in the processor's feature flags. 32 | declare features="" 33 | declare filters="" 34 | declare -a configs=() 35 | features="$(python -c 'import cpuinfo; print(" ".join(cpuinfo.get_cpu_info().get("flags", [])))')" 36 | if [[ "$features" == *avx2* ]]; then 37 | filters+=",avx" 38 | configs+=( "--config=avx" ) 39 | fi 40 | if [[ "$features" == *sse4* ]]; then 41 | filters+=",sse" 42 | configs+=( "--config=sse" ) 43 | fi 44 | filters="${filters#,}" 45 | 46 | # If none of the optimization configs were added, use the basic config. 47 | if [[ ${#configs[@]} -eq 0 ]]; then 48 | configs=( "--config=basic" ) 49 | fi 50 | 51 | declare -a build_filters=() 52 | declare -a test_filters=() 53 | if [[ -n "$filters" ]]; then 54 | build_filters=( "--build_tag_filters=$filters" ) 55 | test_filters=( "--test_tag_filters=$filters" ) 56 | fi 57 | 58 | # The apps are sample programs and are only meant to run on Linux. 59 | if [[ "$OSTYPE" == "linux-gnu"* ]]; then 60 | bazel build "${configs[@]}" "${build_filters[@]}" "$@" apps:all 61 | fi 62 | 63 | # Run all basic tests. This should work on all platforms. 64 | bazel test "${configs[@]}" "${test_filters[@]}" "$@" tests:all 65 | -------------------------------------------------------------------------------- /docs/docker.md: -------------------------------------------------------------------------------- 1 | # Docker 2 | 3 | [Docker](https://docker.com) can be used to run qsim tests in a self-contained 4 | environment, regardless of your local operating system. 5 | 6 | Note that Docker is _not_ usually preinstalled on popular operating systems 7 | such as MacOS, Windows, or Linux, so you will most likely have to install 8 | Docker components yourself. Also, depending on the system, installing Docker 9 | may require root access on your computer (e.g., using `sudo` on Linux). 10 | 11 | The following packages are needed for the steps described on the rest of this 12 | page: 13 | 14 | * `docker` 15 | * `docker-buildx` 16 | * `docker-compose` 17 | 18 | ## Build Docker Images 19 | 20 | Prior to building with Docker, make sure that your local copy of the qsim 21 | source code repository is clean and all submodules are up-to-date. The 22 | following commands should accomplish this: 23 | 24 | ``` 25 | make clean 26 | git submodule update --init --recursive 27 | ``` 28 | 29 | To build qsim and run all the tests: 30 | 31 | ``` 32 | docker compose up --build 33 | ``` 34 | 35 | `docker compose` will create the `qsim`, `qsim-cxx-tests`, and `qsim-py-tests` 36 | images and automatically run all the tests for those targets. A successful run 37 | should have the following messages somewhere in the logs: 38 | 39 | ``` 40 | qsim-cxx-tests exited with code 0 41 | qsim-py-tests exited with code 0 42 | ``` 43 | 44 | To build the Docker image without running tests, simply run: 45 | 46 | ``` 47 | docker compose build 48 | ``` 49 | 50 | Note that currently (qsim version 0.23.0), the Docker build does not make use 51 | of CUDA or GPUs. 52 | 53 | ## Run Simulations 54 | 55 | Once the `qsim` Docker image is created, it can be used to run the 56 | `qsim_base.x` simulation binary with the following command: 57 | 58 | ``` 59 | docker run -ti --rm -v $PWD/circuits:/qsim/circuits:ro \ 60 | qsim:latest -c /qsim/circuits/circuit_q24 61 | ``` 62 | 63 | The flag `-v [orig]:[dest]:[attr]` is required to allow access to the host 64 | folders from within the `qsim` image. This can be omitted only if the circuit 65 | to be simulated has not been modified since the image was created. 66 | 67 | ## Run C++ Tests 68 | 69 | Once the `qsim-cxx-tests` image is created, use the following command to run 70 | all C++ tests: 71 | 72 | ``` 73 | docker run -ti --rm qsim-cxx-tests 74 | ``` 75 | 76 | ## Run Python Tests 77 | 78 | Once the `qsim-py-tests` image is created, use the following command to run 79 | all Python tests: 80 | 81 | ``` 82 | docker run -ti --rm qsim-py-tests 83 | ``` 84 | -------------------------------------------------------------------------------- /tests/bitstring_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include 16 | 17 | #include "gtest/gtest.h" 18 | 19 | #include "../lib/bitstring.h" 20 | 21 | namespace qsim { 22 | 23 | struct IO { 24 | static void errorf(const char* format, ...) {} 25 | static void messagef(const char* format, ...) {} 26 | }; 27 | 28 | constexpr char provider[] = "bitstring_test"; 29 | 30 | TEST(BitstringTest, ValidBitstrings) { 31 | constexpr char valid_bitstrings[] = 32 | R"(1000000 33 | 0100000 34 | 0100100 35 | 1001010 36 | 1000101 37 | 1110000 38 | )"; 39 | 40 | std::stringstream ss(valid_bitstrings); 41 | std::vector bitstrings; 42 | 43 | EXPECT_EQ(BitstringsFromStream(7, provider, ss, bitstrings), true); 44 | EXPECT_EQ(bitstrings.size(), 6); 45 | EXPECT_EQ(bitstrings[0], 1); 46 | EXPECT_EQ(bitstrings[1], 2); 47 | EXPECT_EQ(bitstrings[2], 18); 48 | EXPECT_EQ(bitstrings[3], 41); 49 | EXPECT_EQ(bitstrings[4], 81); 50 | EXPECT_EQ(bitstrings[5], 7); 51 | } 52 | 53 | TEST(BitstringTest, InValidBitstrings1) { 54 | constexpr char invalid_bitstrings[] = 55 | R"(1000000 56 | 0100000 57 | 010010 58 | 1001010 59 | 1000101 60 | 1110000 61 | )"; 62 | 63 | std::stringstream ss(invalid_bitstrings); 64 | std::vector bitstrings; 65 | 66 | EXPECT_EQ(BitstringsFromStream(7, provider, ss, bitstrings), false); 67 | EXPECT_EQ(bitstrings.size(), 0); 68 | } 69 | 70 | TEST(BitstringTest, InValidBitstrings2) { 71 | constexpr char invalid_bitstrings[] = 72 | R"(1000000 73 | 0100000 74 | 0100100 75 | 1001010 76 | 10001011 77 | 1110000 78 | )"; 79 | 80 | std::stringstream ss(invalid_bitstrings); 81 | std::vector bitstrings; 82 | 83 | EXPECT_EQ(BitstringsFromStream(7, provider, ss, bitstrings), false); 84 | EXPECT_EQ(bitstrings.size(), 0); 85 | } 86 | 87 | } // namespace qsim 88 | 89 | int main(int argc, char** argv) { 90 | testing::InitGoogleTest(&argc, argv); 91 | return RUN_ALL_TESTS(); 92 | } 93 | -------------------------------------------------------------------------------- /pybind_interface/custatevec/pybind_main_custatevec.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include 16 | #include 17 | 18 | #include "pybind_main_custatevec.h" 19 | 20 | #include "../../lib/fuser_mqubit.h" 21 | #include "../../lib/gates_cirq.h" 22 | #include "../../lib/io.h" 23 | #include "../../lib/run_qsim.h" 24 | #include "../../lib/simulator_custatevec.h" 25 | 26 | namespace qsim { 27 | using Simulator = SimulatorCuStateVec; 28 | 29 | struct Factory { 30 | // num_sim_threads, num_state_threads and num_dblocks are unused, but kept 31 | // for consistency with other factories. 32 | Factory(unsigned num_sim_threads, 33 | unsigned num_state_threads, 34 | unsigned num_dblocks) { 35 | ErrorCheck(cublasCreate(&cublas_handle)); 36 | ErrorCheck(custatevecCreate(&custatevec_handle)); 37 | } 38 | 39 | ~Factory() { 40 | ErrorCheck(cublasDestroy(cublas_handle)); 41 | ErrorCheck(custatevecDestroy(custatevec_handle)); 42 | } 43 | 44 | using Simulator = qsim::Simulator; 45 | using StateSpace = Simulator::StateSpace; 46 | 47 | using Gate = Cirq::GateCirq; 48 | using Runner = QSimRunner, Factory>; 49 | using RunnerQT = 50 | QSimRunner, Factory>; 51 | using RunnerParameter = Runner::Parameter; 52 | using NoisyRunner = qsim::QuantumTrajectorySimulator; 53 | using NoisyRunnerParameter = NoisyRunner::Parameter; 54 | 55 | StateSpace CreateStateSpace() const { 56 | return StateSpace(cublas_handle, custatevec_handle); 57 | } 58 | 59 | Simulator CreateSimulator() const { 60 | return Simulator(cublas_handle, custatevec_handle); 61 | } 62 | 63 | cublasHandle_t cublas_handle; 64 | custatevecHandle_t custatevec_handle; 65 | }; 66 | 67 | inline void SetFlushToZeroAndDenormalsAreZeros() {} 68 | inline void ClearFlushToZeroAndDenormalsAreZeros() {} 69 | } 70 | 71 | #include "../pybind_main.cpp" 72 | -------------------------------------------------------------------------------- /lib/util.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #ifndef UTIL_H_ 16 | #define UTIL_H_ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | namespace qsim { 27 | 28 | template 29 | inline void SplitString( 30 | const std::string& str, char delim, Container& words) { 31 | words.resize(0); 32 | 33 | std::string word; 34 | std::stringstream ss(str); 35 | 36 | while (std::getline(ss, word, delim)) { 37 | words.push_back(std::move(word)); 38 | } 39 | } 40 | 41 | template 42 | inline void SplitString( 43 | const std::string& str, char delim, Op op, Container& words) { 44 | words.resize(0); 45 | 46 | std::string word; 47 | std::stringstream ss(str); 48 | 49 | while (std::getline(ss, word, delim)) { 50 | words.push_back(op(word)); 51 | } 52 | } 53 | 54 | inline double GetTime() { 55 | using namespace std::chrono; 56 | steady_clock::duration since_epoch = steady_clock::now().time_since_epoch(); 57 | return double(since_epoch.count() * steady_clock::period::num) 58 | / steady_clock::period::den; 59 | } 60 | 61 | template 62 | inline DistrRealType RandomValue(RGen& rgen, DistrRealType max_value) { 63 | std::uniform_real_distribution distr(0.0, max_value); 64 | return distr(rgen); 65 | } 66 | 67 | template 68 | inline std::vector GenerateRandomValues( 69 | uint64_t num_samples, unsigned seed, DistrRealType max_value) { 70 | std::vector rs; 71 | rs.reserve(num_samples + 1); 72 | 73 | std::mt19937 rgen(seed); 74 | std::uniform_real_distribution distr(0.0, max_value); 75 | 76 | for (uint64_t i = 0; i < num_samples; ++i) { 77 | rs.emplace_back(distr(rgen)); 78 | } 79 | 80 | std::sort(rs.begin(), rs.end()); 81 | // Populate the final element to prevent sanitizer errors. 82 | rs.emplace_back(max_value); 83 | 84 | return rs; 85 | } 86 | 87 | } // namespace qsim 88 | 89 | #endif // UTIL_H_ 90 | -------------------------------------------------------------------------------- /lib/bits.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #ifndef BITS_H_ 16 | #define BITS_H_ 17 | 18 | #include 19 | 20 | #ifdef __BMI2__ 21 | 22 | #include 23 | 24 | #include 25 | 26 | namespace qsim { 27 | namespace bits { 28 | 29 | inline uint32_t ExpandBits(uint32_t bits, unsigned n, uint32_t mask) { 30 | return _pdep_u32(bits, mask); 31 | } 32 | 33 | inline uint64_t ExpandBits(uint64_t bits, unsigned n, uint64_t mask) { 34 | return _pdep_u64(bits, mask); 35 | } 36 | 37 | inline uint32_t CompressBits(uint32_t bits, unsigned n, uint32_t mask) { 38 | return _pext_u32(bits, mask); 39 | } 40 | 41 | inline uint64_t CompressBits(uint64_t bits, unsigned n, uint64_t mask) { 42 | return _pext_u64(bits, mask); 43 | } 44 | 45 | } // namespace bits 46 | } // namespace qsim 47 | 48 | #else // __BMI2__ 49 | 50 | namespace qsim { 51 | namespace bits { 52 | 53 | template 54 | inline Integer ExpandBits(Integer bits, unsigned n, Integer mask) { 55 | Integer ebits = 0; 56 | unsigned k = 0; 57 | 58 | for (unsigned i = 0; i < n; ++i) { 59 | if ((mask >> i) & 1) { 60 | ebits |= ((bits >> k) & 1) << i; 61 | ++k; 62 | } 63 | } 64 | 65 | return ebits; 66 | } 67 | 68 | template 69 | inline Integer CompressBits(Integer bits, unsigned n, Integer mask) { 70 | Integer sbits = 0; 71 | unsigned k = 0; 72 | 73 | for (unsigned i = 0; i < n; ++i) { 74 | if ((mask >> i) & 1) { 75 | sbits |= ((bits >> i) & 1) << k; 76 | ++k; 77 | } 78 | } 79 | 80 | return sbits; 81 | } 82 | 83 | } // namespace bits 84 | } // namespace qsim 85 | 86 | #endif // __BMI2__ 87 | 88 | namespace qsim { 89 | namespace bits { 90 | 91 | template 92 | inline Integer PermuteBits( 93 | Integer bits, unsigned n, const std::vector& perm) { 94 | Integer pbits = 0; 95 | 96 | for (unsigned i = 0; i < n; ++i) { 97 | pbits |= ((bits >> i) & 1) << perm[i]; 98 | } 99 | 100 | return pbits; 101 | } 102 | 103 | } // namespace bits 104 | } // namespace qsim 105 | 106 | #endif // BITS_H_ 107 | -------------------------------------------------------------------------------- /lib/cuda2hip.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Advanced Micro Devices, Inc. 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 | // http://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 | #ifndef SIMULATOR_CUDA2HIP_H_ 16 | #define SIMULATOR_CUDA2HIP_H_ 17 | 18 | #define cublasCaxpy hipblasCaxpy 19 | #define cublasCdotc hipblasCdotc 20 | #define cublasCreate hipblasCreate 21 | #define cublasCscal hipblasCscal 22 | #define cublasCsscal hipblasCsscal 23 | #define cublasDestroy hipblasDestroy 24 | #define cublasDznrm2 hipblasDznrm2 25 | #define cublasHandle_t hipblasHandle_t 26 | #define cublasScnrm2 hipblasScnrm2 27 | #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS 28 | #define cublasStatus_t hipblasStatus_t 29 | #define cublasZaxpy hipblasZaxpy 30 | #define cublasZdotc hipblasZdotc 31 | #define cublasZdscal hipblasZdscal 32 | #define cublasZscal hipblasZscal 33 | #define cuCimagf hipCimagf 34 | #define cuCimag hipCimag 35 | #define cuComplex hipComplex 36 | #define cuCrealf hipCrealf 37 | #define cuCreal hipCreal 38 | #define CUDA_C_32F HIPBLAS_C_32F 39 | #define CUDA_C_64F HIPBLAS_C_64F 40 | #define cudaDeviceSynchronize hipDeviceSynchronize 41 | #define cudaError_t hipError_t 42 | #define cudaFree hipFree 43 | #define cudaGetErrorString hipGetErrorString 44 | #define cudaMalloc hipMalloc 45 | #define cudaMemcpyAsync hipMemcpyAsync 46 | #define cudaMemcpyDeviceToDevice hipMemcpyDeviceToDevice 47 | #define cudaMemcpyDeviceToHost hipMemcpyDeviceToHost 48 | #define cudaMemcpy hipMemcpy 49 | #define cudaMemcpyHostToDevice hipMemcpyHostToDevice 50 | #define cudaMemset hipMemset 51 | #define cudaPeekAtLastError hipPeekAtLastError 52 | #define cudaSuccess hipSuccess 53 | #define cuDoubleComplex hipDoubleComplex 54 | 55 | template 56 | __device__ __forceinline__ T __shfl_down_sync( 57 | unsigned mask, T var, unsigned int delta, int width = warpSize) { 58 | return __shfl_down(var, delta, width); 59 | } 60 | 61 | #endif // SIMULATOR_CUDA2HIP_H_ 62 | -------------------------------------------------------------------------------- /.github/workflows/pr-labeler.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 | name: Pull request labeler 16 | run-name: >- 17 | Label pull request ${{github.event.pull_request.number}} by ${{github.actor}} 18 | 19 | # This workflow is designed NOT to fail if labeling actions encounter errors; 20 | # instead, it notes errors as annotations on the workflow's run summary page. If 21 | # labels don't seem to be getting applied, check there for errors. 22 | 23 | on: 24 | # Note: do not copy-paste this workflow with `pull_request_target` left as-is. 25 | # Its use here is a special case where security implications are understood. 26 | # Workflows should normally use `pull_request` instead. 27 | pull_request_target: 28 | types: 29 | - opened 30 | - synchronize 31 | 32 | # Allow manual invocation. 33 | workflow_dispatch: 34 | inputs: 35 | pr-number: 36 | description: 'The PR number of the PR to label:' 37 | type: string 38 | required: true 39 | debug: 40 | description: 'Run with debugging options' 41 | type: boolean 42 | default: true 43 | 44 | # Declare default workflow permissions as read only. 45 | permissions: read-all 46 | 47 | jobs: 48 | label-pr-size: 49 | if: github.repository_owner == 'quantumlib' 50 | name: Update PR size labels 51 | runs-on: ubuntu-24.04 52 | timeout-minutes: 5 53 | permissions: 54 | contents: read 55 | issues: write 56 | pull-requests: write 57 | env: 58 | # Environment variable PR_NUMBER is needed by size-labeler.sh. 59 | PR_NUMBER: ${{inputs.pr-number || github.event.pull_request.number}} 60 | # Add xtrace to SHELLOPTS for all Bash scripts when doing debug runs. 61 | SHELLOPTS: ${{inputs.debug && 'xtrace' || '' }} 62 | steps: 63 | - name: Check out a copy of the git repository 64 | uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0 65 | with: 66 | sparse-checkout: | 67 | ./dev_tools/ci/size-labeler.sh 68 | 69 | - name: Label the PR with a size label 70 | continue-on-error: true 71 | env: 72 | GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} 73 | run: ./dev_tools/ci/size-labeler.sh 74 | -------------------------------------------------------------------------------- /docs/overview.md: -------------------------------------------------------------------------------- 1 | # qsim and qsimh 2 | 3 | qsim and qsimh are a collection of C++ libraries for quantum circuit 4 | simulation. These libraries provide powerful, low-cost tools for 5 | researchers to test quantum algorithms before running on quantum hardware. 6 | 7 | qsim makes use of AVX/FMA vector operations, OpenMP multithreading, and 8 | gate fusion [[1]](https://arxiv.org/abs/1601.07195) 9 | [[2]](https://arxiv.org/abs/1704.01127) 10 | to accelerate simulations. This performance is best demonstrated by the use 11 | of qsim in cross-entropy benchmarks 12 | [[3]](https://www.nature.com/articles/s41586-019-1666-5). 13 | 14 | Integration with [Cirq](https://github.com/quantumlib/Cirq) makes getting 15 | started with qsim easy! Check out the 16 | [installation guide](https://github.com/quantumlib/qsim/blob/main/docs/install_qsimcirq.md) 17 | or try the runnable 18 | [notebook tutorial](https://github.com/quantumlib/qsim/blob/main/docs/tutorials/qsimcirq.ipynb). 19 | 20 | ## Design 21 | 22 | The git repository for qsim includes two top-level libraries for simulation: 23 | 24 | - **qsim** is a Schrödinger state-vector simulator designed to run on a 25 | single machine. It produces the full state vector as output which, 26 | for instance, allows users to sample repeatedly from a single execution. 27 | - **qsimh** is a hybrid Schrödinger-Feynman simulator built for parallel 28 | execution on a cluster of machines. It produces amplitudes for 29 | user-specified output bitstrings. Compared to qsim, by limiting what it 30 | returns, qsimh can simulate more qubits. 31 | 32 | These libraries can be invoked either directly or through the qsim-Cirq 33 | interface to perform the following operations: 34 | 35 | - Determine the final state vector of a circuit (qsim only). 36 | - Sample results from a circuit. Multiple samples can be generated with 37 | minimal additional cost for circuits with no intermediate measurements 38 | (qsim only). 39 | - Calculate amplitudes for user-specified result bitstrings. With qsimh, 40 | this is trivially parallelizable across several machines. 41 | 42 | Circuits of up to 30 qubits can be simulated in qsim with ~16GB of RAM; 43 | each additional qubit doubles the RAM requirement. In contrast, careful 44 | use of qsimh can support 50 qubits or more. 45 | 46 | 47 | [[1]](https://arxiv.org/abs/1601.07195) M. Smelyanskiy, N. P. Sawaya, 48 | A. Aspuru-Guzik, "qHiPSTER: The Quantum High Performance Software Testing 49 | Environment", arXiv:1601.07195 (2016). 50 | 51 | [[2]](https://arxiv.org/abs/1704.01127) T. Häner, D. S. Steiger, 52 | "0.5 Petabyte Simulation of a 45-Qubit Quantum Circuit", arXiv:1704.01127 53 | (2017). 54 | 55 | [[3]](https://www.nature.com/articles/s41586-019-1666-5), F. Arute et al, 56 | "Quantum Supremacy Using a Programmable Superconducting Processor", 57 | Nature 574, 505, (2019). 58 | -------------------------------------------------------------------------------- /docs/_book.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 | upper_tabs: 16 | # Dropdown menu 17 | - name: "Software" 18 | path: /software 19 | is_default: true 20 | menu: 21 | - include: /_book/menu_software.yaml 22 | lower_tabs: 23 | # Subsite tabs 24 | other: 25 | - name: "Tutorials" 26 | contents: 27 | - title: "Get started with qsimcirq" 28 | path: /qsim/tutorials/qsimcirq 29 | - heading: "qsim on Google Cloud" 30 | - title: "Before you begin" 31 | path: /qsim/tutorials/gcp_before_you_begin 32 | - title: "CPU-based simulation" 33 | path: /qsim/tutorials/gcp_cpu 34 | - title: "GPU-based simulation" 35 | path: /qsim/tutorials/gcp_gpu 36 | - title: "Multinode simulation" 37 | path: /qsim/tutorials/multinode 38 | - heading: "Other tutorials" 39 | - title: "Simulate a large circuit" 40 | path: /qsim/tutorials/q32d14 41 | - title: "Simulate noise" 42 | path: /qsim/tutorials/noisy_qsimcirq 43 | - title: "AMD GPU support" 44 | path: /qsim/tutorials/amd_gpu 45 | 46 | - name: "Guides" 47 | contents: 48 | - title: "Introduction" 49 | path: /qsim/overview 50 | - title: "Choosing hardware" 51 | path: /qsim/choose_hw 52 | - title: "Command line reference" 53 | path: /qsim/usage 54 | - title: "Python interface" 55 | path: /qsim/cirq_interface 56 | - title: "C++ templates" 57 | path: /qsim/type_reference 58 | - title: "C++ builds with Bazel" 59 | path: /qsim/bazel 60 | - title: "Docker builds" 61 | path: /qsim/docker 62 | - title: "Testing qsim" 63 | path: /qsim/testing 64 | - title: "Release process" 65 | path: /qsim/release 66 | 67 | - name: "Python Reference" 68 | skip_translation: true 69 | contents: 70 | - title: "All Symbols" 71 | path: /reference/python/qsimcirq/all_symbols 72 | - include: /reference/python/qsimcirq/_toc.yaml 73 | 74 | - name: "C++ Reference" 75 | skip_translation: true 76 | contents: 77 | - title: "All Symbols" 78 | path: /reference/cc/qsim 79 | - include: /reference/cc/qsim/_doxygen.yaml 80 | 81 | - include: /_book/upper_tabs_right.yaml 82 | -------------------------------------------------------------------------------- /tests/unitaryspace_testfixture.h: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #ifndef UNITARYSPACE_TESTFIXTURE_H_ 16 | #define UNITARYSPACE_TESTFIXTURE_H_ 17 | 18 | #include 19 | 20 | #include "gtest/gtest.h" 21 | 22 | namespace qsim { 23 | 24 | namespace unitary { 25 | 26 | template 27 | void TestSetZeros() { 28 | using Unitary = typename UnitarySpace::Unitary; 29 | 30 | for (unsigned nq = 1; nq <= 5; ++nq) { 31 | UnitarySpace us(1); 32 | Unitary u = us.CreateUnitary(nq); 33 | 34 | us.SetAllZeros(u); 35 | 36 | unsigned size = 1 << nq; 37 | for (unsigned i = 0; i < size; ++i) { 38 | for (unsigned j = 0; j < size; ++j) { 39 | EXPECT_EQ(us.GetEntry(u, i, j), std::complex(0, 0)); 40 | } 41 | } 42 | } 43 | } 44 | 45 | template 46 | void TestSetIdentity() { 47 | using Unitary = typename UnitarySpace::Unitary; 48 | 49 | for (unsigned nq = 1; nq <= 5; ++nq) { 50 | UnitarySpace us(1); 51 | Unitary u = us.CreateUnitary(nq); 52 | 53 | us.SetIdentity(u); 54 | 55 | unsigned size = 1 << nq; 56 | for (unsigned i = 0; i < size; ++i) { 57 | for (unsigned j = 0; j < size; ++j) { 58 | if (i == j) { 59 | EXPECT_EQ(us.GetEntry(u, i, j), std::complex(1, 0)); 60 | } else { 61 | EXPECT_EQ(us.GetEntry(u, i, j), std::complex(0, 0)); 62 | } 63 | } 64 | } 65 | } 66 | } 67 | 68 | template 69 | void TestSetEntry() { 70 | using Unitary = typename UnitarySpace::Unitary; 71 | 72 | for (unsigned nq = 1; nq <= 5; ++nq) { 73 | UnitarySpace us(1); 74 | Unitary u = us.CreateUnitary(nq); 75 | 76 | unsigned size = 1 << nq; 77 | 78 | for (unsigned i = 0; i < size; ++i) { 79 | for (unsigned j = 0; j < size; ++j) { 80 | unsigned val = i * size + j; 81 | us.SetEntry(u, i, j, 2 * val, 2 * val + 1); 82 | } 83 | } 84 | 85 | for (unsigned i = 0; i < size; ++i) { 86 | for (unsigned j = 0; j < size; ++j) { 87 | unsigned val = i * size + j; 88 | EXPECT_EQ( 89 | us.GetEntry(u, i, j), std::complex(2 * val, 2 * val + 1)); 90 | } 91 | } 92 | } 93 | } 94 | 95 | } // namespace unitary 96 | } // namespace qsim 97 | 98 | #endif // UNITARYSPACE_TESTFIXTURE_H_ 99 | -------------------------------------------------------------------------------- /docs/images/quantum-ai-vertical.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /WORKSPACE: -------------------------------------------------------------------------------- 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 | load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") 16 | 17 | http_archive( 18 | name = "platforms", 19 | sha256 = "29742e87275809b5e598dc2f04d86960cc7a55b3067d97221c9abbc9926bff0f", 20 | urls = [ 21 | "https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.11/platforms-0.0.11.tar.gz", 22 | "https://github.com/bazelbuild/platforms/releases/download/0.0.11/platforms-0.0.11.tar.gz", 23 | ], 24 | ) 25 | 26 | http_archive( 27 | name = "com_google_googletest", 28 | sha256 = "40d4ec942217dcc84a9ebe2a68584ada7d4a33a8ee958755763278ea1c5e18ff", 29 | strip_prefix = "googletest-1.17.0", 30 | url = "https://github.com/google/googletest/archive/refs/tags/v1.17.0.zip", 31 | ) 32 | 33 | # Required for testing compatibility with TF Quantum: 34 | # https://github.com/tensorflow/quantum 35 | http_archive( 36 | name = "org_tensorflow", 37 | sha256 = "447cdb65c80c86d6c6cf1388684f157612392723eaea832e6392d219098b49de", 38 | strip_prefix = "tensorflow-2.13.0", 39 | url = "https://github.com/tensorflow/tensorflow/archive/refs/tags/v2.13.0.zip", 40 | ) 41 | 42 | load("@org_tensorflow//tensorflow:workspace3.bzl", "tf_workspace3") 43 | 44 | tf_workspace3() 45 | 46 | load("@org_tensorflow//tensorflow:workspace2.bzl", "tf_workspace2") 47 | 48 | tf_workspace2() 49 | 50 | load("@org_tensorflow//tensorflow:workspace1.bzl", "tf_workspace1") 51 | 52 | tf_workspace1() 53 | 54 | load("@org_tensorflow//tensorflow:workspace0.bzl", "tf_workspace0") 55 | 56 | tf_workspace0() 57 | 58 | EIGEN_COMMIT = "d71c30c47858effcbd39967097a2d99ee48db464" # 3.4.1 59 | 60 | EIGEN_SHA256 = "f1d28c2205d015490a685b1e5a171c434da87f757746724de3cb85e69621dec2" 61 | 62 | http_archive( 63 | name = "eigen", 64 | build_file_content = """ 65 | cc_library( 66 | name = "eigen3", 67 | textual_hdrs = glob(["Eigen/**", "unsupported/**"]), 68 | visibility = ["//visibility:public"], 69 | ) 70 | """, 71 | sha256 = EIGEN_SHA256, 72 | strip_prefix = "eigen-{commit}".format(commit = EIGEN_COMMIT), 73 | urls = [ 74 | "https://gitlab.com/libeigen/eigen/-/archive/{commit}/eigen-{commit}.tar.gz".format(commit = EIGEN_COMMIT), 75 | ], 76 | ) 77 | 78 | load("//third_party/cuquantum:cuquantum_configure.bzl", "cuquantum_configure") 79 | 80 | cuquantum_configure(name = "local_config_cuquantum") 81 | -------------------------------------------------------------------------------- /tests/simulator_avx_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "simulator_testfixture.h" 16 | 17 | #include "gtest/gtest.h" 18 | 19 | #ifdef _OPENMP 20 | #include "../lib/parfor.h" 21 | #endif 22 | #include "../lib/seqfor.h" 23 | #include "../lib/simulator_avx.h" 24 | 25 | namespace qsim { 26 | 27 | template 28 | class SimulatorAVXTest : public testing::Test {}; 29 | 30 | using ::testing::Types; 31 | #ifdef _OPENMP 32 | typedef Types for_impl; 33 | #else 34 | typedef Types for_impl; 35 | #endif 36 | 37 | template 38 | struct Factory { 39 | using Simulator = SimulatorAVX; 40 | using StateSpace = typename Simulator::StateSpace; 41 | 42 | static StateSpace CreateStateSpace() { 43 | return StateSpace(2); 44 | } 45 | 46 | static Simulator CreateSimulator() { 47 | return Simulator(2); 48 | } 49 | }; 50 | 51 | TYPED_TEST_SUITE(SimulatorAVXTest, for_impl); 52 | 53 | TYPED_TEST(SimulatorAVXTest, ApplyGate1) { 54 | TestApplyGate1(Factory()); 55 | } 56 | 57 | TYPED_TEST(SimulatorAVXTest, ApplyGate2) { 58 | TestApplyGate2(Factory()); 59 | } 60 | 61 | TYPED_TEST(SimulatorAVXTest, ApplyGate3) { 62 | TestApplyGate3(Factory()); 63 | } 64 | 65 | TYPED_TEST(SimulatorAVXTest, ApplyGate5) { 66 | TestApplyGate5(Factory()); 67 | } 68 | 69 | TYPED_TEST(SimulatorAVXTest, CircuitWithControlledGates) { 70 | TestCircuitWithControlledGates(Factory()); 71 | } 72 | 73 | TYPED_TEST(SimulatorAVXTest, CircuitWithControlledGatesDagger) { 74 | TestCircuitWithControlledGatesDagger(Factory()); 75 | } 76 | 77 | TYPED_TEST(SimulatorAVXTest, MultiQubitGates) { 78 | TestMultiQubitGates(Factory()); 79 | } 80 | 81 | TYPED_TEST(SimulatorAVXTest, ControlledGates) { 82 | TestControlledGates(Factory(), false); 83 | } 84 | 85 | TYPED_TEST(SimulatorAVXTest, GlobalPhaseGate) { 86 | TestGlobalPhaseGate(Factory()); 87 | } 88 | 89 | TYPED_TEST(SimulatorAVXTest, ExpectationValue1) { 90 | TestExpectationValue1(Factory()); 91 | } 92 | 93 | TYPED_TEST(SimulatorAVXTest, ExpectationValue2) { 94 | TestExpectationValue2(Factory()); 95 | } 96 | 97 | } // namespace qsim 98 | 99 | int main(int argc, char** argv) { 100 | ::testing::InitGoogleTest(&argc, argv); 101 | return RUN_ALL_TESTS(); 102 | } 103 | -------------------------------------------------------------------------------- /tests/simulator_sse_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "simulator_testfixture.h" 16 | 17 | #include "gtest/gtest.h" 18 | 19 | #ifdef _OPENMP 20 | #include "../lib/parfor.h" 21 | #endif 22 | #include "../lib/seqfor.h" 23 | #include "../lib/simulator_sse.h" 24 | 25 | namespace qsim { 26 | 27 | template 28 | class SimulatorSSETest : public testing::Test {}; 29 | 30 | using ::testing::Types; 31 | #ifdef _OPENMP 32 | typedef Types for_impl; 33 | #else 34 | typedef Types for_impl; 35 | #endif 36 | 37 | template 38 | struct Factory { 39 | using Simulator = SimulatorSSE; 40 | using StateSpace = typename Simulator::StateSpace; 41 | 42 | static StateSpace CreateStateSpace() { 43 | return StateSpace(2); 44 | } 45 | 46 | static Simulator CreateSimulator() { 47 | return Simulator(2); 48 | } 49 | }; 50 | 51 | TYPED_TEST_SUITE(SimulatorSSETest, for_impl); 52 | 53 | TYPED_TEST(SimulatorSSETest, ApplyGate1) { 54 | TestApplyGate1(Factory()); 55 | } 56 | 57 | TYPED_TEST(SimulatorSSETest, ApplyGate2) { 58 | TestApplyGate2(Factory()); 59 | } 60 | 61 | TYPED_TEST(SimulatorSSETest, ApplyGate3) { 62 | TestApplyGate3(Factory()); 63 | } 64 | 65 | TYPED_TEST(SimulatorSSETest, ApplyGate5) { 66 | TestApplyGate5(Factory()); 67 | } 68 | 69 | TYPED_TEST(SimulatorSSETest, CircuitWithControlledGates) { 70 | TestCircuitWithControlledGates(Factory()); 71 | } 72 | 73 | TYPED_TEST(SimulatorSSETest, CircuitWithControlledGatesDagger) { 74 | TestCircuitWithControlledGatesDagger(Factory()); 75 | } 76 | 77 | TYPED_TEST(SimulatorSSETest, MultiQubitGates) { 78 | TestMultiQubitGates(Factory()); 79 | } 80 | 81 | TYPED_TEST(SimulatorSSETest, ControlledGates) { 82 | TestControlledGates(Factory(), false); 83 | } 84 | 85 | TYPED_TEST(SimulatorSSETest, GlobalPhaseGate) { 86 | TestGlobalPhaseGate(Factory()); 87 | } 88 | 89 | TYPED_TEST(SimulatorSSETest, ExpectationValue1) { 90 | TestExpectationValue1(Factory()); 91 | } 92 | 93 | TYPED_TEST(SimulatorSSETest, ExpectationValue2) { 94 | TestExpectationValue2(Factory()); 95 | } 96 | 97 | } // namespace qsim 98 | 99 | int main(int argc, char** argv) { 100 | ::testing::InitGoogleTest(&argc, argv); 101 | return RUN_ALL_TESTS(); 102 | } 103 | -------------------------------------------------------------------------------- /docs/install_qsimcirq.md: -------------------------------------------------------------------------------- 1 | # Installing qsimcirq 2 | 3 | The qsim-Cirq Python interface is available as a PyPI package for Linux, MacOS and Windows users. 4 | For all others, Dockerfiles are provided to install qsim in a containerized 5 | environment. 6 | 7 | **Note:** The core qsim library (under 8 | [lib/](https://github.com/quantumlib/qsim/blob/main/lib)) can be included 9 | directly in C++ code without building and installing the qsimcirq interface. 10 | 11 | ## Before installation 12 | 13 | Prior to installation, consider opening a 14 | [virtual environment](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/). 15 | 16 | Prerequisites are included in the 17 | [`requirements.txt`](https://github.com/quantumlib/qsim/blob/main/requirements.txt) 18 | file, and will be automatically installed along with qsimcirq. 19 | 20 | If you'd like to develop qsimcirq, a separate set of dependencies are includes 21 | in the 22 | [`dev-requirements.txt`](https://github.com/quantumlib/qsim/blob/main/dev-requirements.txt) 23 | file. You can install them with `pip3 install -r dev-requirements.txt` or 24 | `pip3 install qsimcirq[dev]`. 25 | 26 | ## Linux installation 27 | 28 | We provide `qsimcirq` Python wheels on 64-bit `x86` architectures with 29 | `Python 3.{10,11,12,13}`. The installation process will automatically check for 30 | CUDA and GPUs on your computer if they exist and attempt to build a version of 31 | qsim that can make use of the GPU(s). (Note that this is presently an 32 | installation-time action and will take several minutes to finish.) 33 | 34 | Simply run `pip3 install qsimcirq`. 35 | 36 | ## MacOS installation 37 | 38 | We provide `qsimcirq` Python wheels on `x86` and Apple Silicon architectures 39 | with `Python 3.{10,11,12,13}`. 40 | 41 | Simply run `pip3 install qsimcirq`. 42 | 43 | Note that, due to architectural differences, CUDA support is not available on 44 | MacOS. The version of `qsimcirq` on MacOS will only use the CPU, without GPU 45 | acceleration. 46 | 47 | ## Windows installation 48 | 49 | We provide `qsimcirq` Python wheels on 64-bit `x86` and `amd64` architectures 50 | with `Python 3.{10,11,12,13}`. 51 | 52 | Simply run `pip3 install qsimcirq`. 53 | 54 | ## Help! There's no compatible wheel for my machine! 55 | 56 | If existing wheels do no meet your needs, please open an issue with your 57 | machine configuration (i.e., CPU architecture, Python version) and consider 58 | using the [Docker config](./docker.md) provided in the qsim GitHub repository. 59 | 60 | ## Testing 61 | 62 | After installing `qsimcirq` on your machine, you can test the installation by 63 | copying [qsimcirq_tests/qsimcirq_test.py](qsimcirq_tests/qsimcirq_test.py) 64 | to your machine and running `python3 -m pytest qsimcirq_test.py`. 65 | 66 | The file `qsimcirq_test.py` also has examples of how to use qsimcirq. 67 | 68 | **Note:** Because of how Python searches for modules, the test file cannot 69 | be run from inside a clone of the qsim repository, or from any parent 70 | directory of such a repository. Failure to meet this criteria may result 71 | in misbehaving tests (e.g., false positives after a failed installation). 72 | -------------------------------------------------------------------------------- /tests/simulator_basic_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "simulator_testfixture.h" 16 | 17 | #include "gtest/gtest.h" 18 | 19 | #ifdef _OPENMP 20 | #include "../lib/parfor.h" 21 | #endif 22 | #include "../lib/seqfor.h" 23 | #include "../lib/simulator_basic.h" 24 | 25 | namespace qsim { 26 | 27 | template 28 | class SimulatorBasicTest : public testing::Test {}; 29 | 30 | using ::testing::Types; 31 | #ifdef _OPENMP 32 | typedef Types for_impl; 33 | #else 34 | typedef Types for_impl; 35 | #endif 36 | 37 | template 38 | struct Factory { 39 | using Simulator = SimulatorBasic; 40 | using StateSpace = typename Simulator::StateSpace; 41 | 42 | static StateSpace CreateStateSpace() { 43 | return StateSpace(2); 44 | } 45 | 46 | static Simulator CreateSimulator() { 47 | return Simulator(2); 48 | } 49 | }; 50 | 51 | TYPED_TEST_SUITE(SimulatorBasicTest, for_impl); 52 | 53 | TYPED_TEST(SimulatorBasicTest, ApplyGate1) { 54 | TestApplyGate1(Factory()); 55 | } 56 | 57 | TYPED_TEST(SimulatorBasicTest, ApplyGate2) { 58 | TestApplyGate2(Factory()); 59 | } 60 | 61 | TYPED_TEST(SimulatorBasicTest, ApplyGate3) { 62 | TestApplyGate3(Factory()); 63 | } 64 | 65 | TYPED_TEST(SimulatorBasicTest, ApplyGate5) { 66 | TestApplyGate5(Factory()); 67 | } 68 | 69 | TYPED_TEST(SimulatorBasicTest, CircuitWithControlledGates) { 70 | TestCircuitWithControlledGates(Factory()); 71 | } 72 | 73 | TYPED_TEST(SimulatorBasicTest, CircuitWithControlledGatesDagger) { 74 | TestCircuitWithControlledGatesDagger(Factory()); 75 | } 76 | 77 | TYPED_TEST(SimulatorBasicTest, MultiQubitGates) { 78 | TestMultiQubitGates(Factory()); 79 | } 80 | 81 | TYPED_TEST(SimulatorBasicTest, ControlledGates) { 82 | TestControlledGates(Factory(), true); 83 | } 84 | 85 | TYPED_TEST(SimulatorBasicTest, GlobalPhaseGate) { 86 | TestGlobalPhaseGate(Factory()); 87 | } 88 | 89 | TYPED_TEST(SimulatorBasicTest, ExpectationValue1) { 90 | TestExpectationValue1(Factory()); 91 | } 92 | 93 | TYPED_TEST(SimulatorBasicTest, ExpectationValue2) { 94 | TestExpectationValue2(Factory()); 95 | } 96 | 97 | } // namespace qsim 98 | 99 | int main(int argc, char** argv) { 100 | ::testing::InitGoogleTest(&argc, argv); 101 | return RUN_ALL_TESTS(); 102 | } 103 | -------------------------------------------------------------------------------- /pybind_interface/decide/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC. All Rights Reserved. 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 | cmake_minimum_required(VERSION 3.28) 16 | project(qsim LANGUAGES CXX) 17 | 18 | include(CheckLanguage) 19 | check_language(CUDA) 20 | 21 | if(WIN32) 22 | add_compile_options(/openmp) 23 | # Add /O2 to any configuration that is NOT Debug. 24 | # This prevents a conflict with /RTC1 in DEBUG builds. 25 | add_compile_options($<$>:/O2>) 26 | else() 27 | add_compile_options(-O3 -flto=auto) 28 | endif() 29 | 30 | if(APPLE) 31 | include_directories( 32 | "/usr/local/include" 33 | "/usr/local/opt/llvm/include" 34 | "/opt/homebrew/include" 35 | "/opt/homebrew/opt/llvm@19/include" 36 | ) 37 | link_directories( 38 | "/usr/local/lib" 39 | "/usr/local/opt/llvm/lib" 40 | "/opt/homebrew/lib" 41 | "/opt/homebrew/opt/llvm@19/lib" 42 | ) 43 | endif() 44 | 45 | include(../GetPybind11.cmake) 46 | 47 | # Configure based on the detected platform 48 | if(CMAKE_CUDA_COMPILER) 49 | include(../GetCUDAARCHS.cmake) 50 | add_library(qsim_decide MODULE decide.cpp) 51 | if(DEFINED ENV{CUQUANTUM_ROOT}) 52 | target_compile_options(qsim_decide PRIVATE 53 | $<$:-D__CUSTATEVEC__> 54 | ) 55 | endif() 56 | find_package(Python3 3.10 REQUIRED COMPONENTS Interpreter Development) 57 | include_directories(${PYTHON_INCLUDE_DIRS} ${pybind11_SOURCE_DIR}/include) 58 | set_target_properties(qsim_decide PROPERTIES 59 | CUDA_ARCHITECTURES "${CMAKE_CUDA_ARCHITECTURES}" 60 | PREFIX "${PYTHON_MODULE_PREFIX}" 61 | SUFFIX "${PYTHON_MODULE_EXTENSION}" 62 | ) 63 | set_source_files_properties(decide.cpp PROPERTIES LANGUAGE CUDA) 64 | target_link_libraries(qsim_decide OpenMP::OpenMP_CXX) 65 | elseif(has_hipcc) 66 | list(APPEND CMAKE_MODULE_PATH "/opt/rocm/lib/cmake/hip") 67 | find_package(HIP REQUIRED) 68 | hip_add_library(qsim_decide MODULE decide.cpp) 69 | set_source_files_properties(decide.cpp PROPERTIES LANGUAGE HIP) 70 | find_package(Python3 3.10 REQUIRED COMPONENTS Interpreter Development) 71 | include_directories(${PYTHON_INCLUDE_DIRS} ${pybind11_SOURCE_DIR}/include) 72 | set_target_properties(qsim_decide PROPERTIES 73 | PREFIX "${PYTHON_MODULE_PREFIX}" 74 | SUFFIX "${PYTHON_MODULE_EXTENSION}" 75 | ) 76 | target_link_libraries(qsim_decide PUBLIC OpenMP::OpenMP_CXX) 77 | else() 78 | pybind11_add_module(qsim_decide decide.cpp) 79 | target_link_libraries(qsim_decide PUBLIC OpenMP::OpenMP_CXX) 80 | endif() 81 | -------------------------------------------------------------------------------- /qsimcirq/qsimh_simulator.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC. All Rights Reserved. 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 Sequence 16 | 17 | import cirq 18 | 19 | import qsimcirq.qsim_circuit as qsimc 20 | 21 | from . import qsim 22 | 23 | 24 | class QSimhSimulator(cirq.SimulatesAmplitudes): 25 | def __init__(self, qsimh_options: dict = None): 26 | if qsimh_options is None: 27 | qsimh_options = {} 28 | """Creates a new QSimhSimulator using the given options. 29 | 30 | Args: 31 | qsim_options: A map of circuit options for the simulator. These will be 32 | applied to all circuits run using this simulator. Accepted keys and 33 | their behavior are as follows: 34 | - 'k': Comma-separated list of ints. Indices of "part 1" qubits. 35 | - 'p': int (>= 0). Number of "prefix" gates. 36 | - 'r': int (>= 0). Number of "root" gates. 37 | - 't': int (> 0). Number of threads to run on. Default: 1. 38 | - 'v': int (>= 0). Log verbosity. Default: 0. 39 | - 'w': int (>= 0). Prefix value. 40 | See qsim/docs/usage.md for more details on these options. 41 | """ 42 | self.qsimh_options = {"t": 1, "f": 2, "v": 0} 43 | self.qsimh_options.update(qsimh_options) 44 | 45 | def compute_amplitudes_sweep( 46 | self, 47 | program: cirq.Circuit, 48 | bitstrings: Sequence[int], 49 | params: cirq.Sweepable, 50 | qubit_order: cirq.QubitOrderOrList = cirq.QubitOrder.DEFAULT, 51 | ) -> Sequence[Sequence[complex]]: 52 | 53 | if not isinstance(program, qsimc.QSimCircuit): 54 | program = qsimc.QSimCircuit(program) 55 | 56 | n_qubits = len(program.all_qubits()) 57 | # qsim numbers qubits in reverse order from cirq 58 | bitstrings = [ 59 | format(bitstring, "b").zfill(n_qubits)[::-1] for bitstring in bitstrings 60 | ] 61 | 62 | options = {"i": "\n".join(bitstrings)} 63 | options.update(self.qsimh_options) 64 | param_resolvers = cirq.to_resolvers(params) 65 | 66 | trials_results = [] 67 | for prs in param_resolvers: 68 | 69 | solved_circuit = cirq.resolve_parameters(program, prs) 70 | 71 | options["c"], _ = solved_circuit.translate_cirq_to_qsim(qubit_order) 72 | 73 | options.update(self.qsimh_options) 74 | amplitudes = qsim.qsimh_simulate(options) 75 | trials_results.append(amplitudes) 76 | 77 | return trials_results 78 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/1-bug-report.yml: -------------------------------------------------------------------------------- 1 | name: Bug report 2 | description: Report a problem with qsim or this project 3 | type: Bug 4 | body: 5 | - type: markdown 6 | attributes: 7 | value: >- 8 | Thank you for taking the time to open an issue report. Before 9 | continuing, it's worth [searching through the existing issues in this 10 | repository](https://github.com/quantumlib/qsim/issues?q=is%3Aissue) in 11 | case the same topic has already been reported. 12 | 13 | - type: textarea 14 | attributes: 15 | label: Describe the issue 16 | description: >- 17 | Please explain clearly and in detail what the issue is. What were you 18 | trying to do? What happened? What was unexpected about what happened? 19 | placeholder: >- 20 | Describe the issue here 21 | validations: 22 | required: true 23 | 24 | - type: textarea 25 | attributes: 26 | label: Tell us how to reproduce the issue 27 | description: >- 28 | Explain in a step-by-step fashion what someone else will need to 29 | do in order to reproduce the issue. If possible, include literal 30 | examples of commands or code using [Markdown fenced code 31 | blocks](https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks). 32 | For long output, paste the text into [collapsed 33 | sections](https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/organizing-information-with-collapsed-sections) 34 | or attach text files. 35 | placeholder: >- 36 | Write a step-by-step explanation of how to reproduce the issue here 37 | validations: 38 | required: false 39 | 40 | - type: input 41 | attributes: 42 | label: Tell us the version of qsim or qsimcirq (if relevant) 43 | description: |- 44 | For qsim software problems, we need to know the exact version you are 45 | using. If you are using the Python package and are running within a 46 | terminal emulator, then typing the following command in the shell will 47 | print the version number:
        48 | ```python3 -c 'import qsimcirq; print(qsimcirq.__version__)'```
49 | If you are using a Google Colab or Jupyter notebook environment, 50 | then running the following command in a notebook cell will print 51 | the version number:
        52 | `import qsimcirq; print(qsimcirq.__version__)` 53 | placeholder: >- 54 | Write the qsim version number here 55 | validations: 56 | required: false 57 | 58 | - type: input 59 | attributes: 60 | label: Tell us the computing environment (if relevant) 61 | description: >- 62 | Please tell us if you are using Linux, macOS, Windows, Google Colab, or 63 | something else, and its version. If you are trying to use a GPU and 64 | CUDA, please tell us the model of GPU and version of CUDA. 65 | placeholder: >- 66 | Describe the computing environment and its version here 67 | validations: 68 | required: false 69 | -------------------------------------------------------------------------------- /tests/simulator_avx512_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "simulator_testfixture.h" 16 | 17 | #include "gtest/gtest.h" 18 | 19 | #if defined(__AVX512F__) && !defined(_WIN32) 20 | 21 | #ifdef _OPENMP 22 | #include "../lib/parfor.h" 23 | #endif 24 | #include "../lib/seqfor.h" 25 | #include "../lib/simulator_avx512.h" 26 | 27 | namespace qsim { 28 | 29 | template 30 | class SimulatorAVX512Test : public testing::Test {}; 31 | 32 | using ::testing::Types; 33 | #ifdef _OPENMP 34 | typedef Types for_impl; 35 | #else 36 | typedef Types for_impl; 37 | #endif 38 | 39 | template 40 | struct Factory { 41 | using Simulator = SimulatorAVX512; 42 | using StateSpace = typename Simulator::StateSpace; 43 | 44 | static StateSpace CreateStateSpace() { 45 | return StateSpace(2); 46 | } 47 | 48 | static Simulator CreateSimulator() { 49 | return Simulator(2); 50 | } 51 | }; 52 | 53 | TYPED_TEST_SUITE(SimulatorAVX512Test, for_impl); 54 | 55 | TYPED_TEST(SimulatorAVX512Test, ApplyGate1) { 56 | TestApplyGate1(Factory()); 57 | } 58 | 59 | TYPED_TEST(SimulatorAVX512Test, ApplyGate2) { 60 | TestApplyGate2(Factory()); 61 | } 62 | 63 | TYPED_TEST(SimulatorAVX512Test, ApplyGate3) { 64 | TestApplyGate3(Factory()); 65 | } 66 | 67 | TYPED_TEST(SimulatorAVX512Test, ApplyGate5) { 68 | TestApplyGate5(Factory()); 69 | } 70 | 71 | TYPED_TEST(SimulatorAVX512Test, CircuitWithControlledGates) { 72 | TestCircuitWithControlledGates(Factory()); 73 | } 74 | 75 | TYPED_TEST(SimulatorAVX512Test, CircuitWithControlledGatesDagger) { 76 | TestCircuitWithControlledGatesDagger(Factory()); 77 | } 78 | 79 | TYPED_TEST(SimulatorAVX512Test, MultiQubitGates) { 80 | TestMultiQubitGates(Factory()); 81 | } 82 | 83 | TYPED_TEST(SimulatorAVX512Test, ControlledGates) { 84 | TestControlledGates(Factory(), false); 85 | } 86 | 87 | TYPED_TEST(SimulatorAVX512Test, GlobalPhaseGate) { 88 | TestGlobalPhaseGate(Factory()); 89 | } 90 | 91 | TYPED_TEST(SimulatorAVX512Test, ExpectationValue1) { 92 | TestExpectationValue1(Factory()); 93 | } 94 | 95 | TYPED_TEST(SimulatorAVX512Test, ExpectationValue2) { 96 | TestExpectationValue2(Factory()); 97 | } 98 | 99 | } // namespace qsim 100 | 101 | #endif // defined(__AVX512F__) && !defined(_WIN32) 102 | 103 | int main(int argc, char** argv) { 104 | ::testing::InitGoogleTest(&argc, argv); 105 | return RUN_ALL_TESTS(); 106 | } 107 | -------------------------------------------------------------------------------- /tests/vectorspace_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include 16 | #include 17 | #include 18 | 19 | #include "gtest/gtest.h" 20 | 21 | #include "../lib/formux.h" 22 | #include "../lib/vectorspace.h" 23 | 24 | namespace qsim { 25 | 26 | TEST(VectorSpaceTest, BasicTest) { 27 | struct DummyImplementation { 28 | static uint64_t MinSize(unsigned num_qubits) { 29 | return uint64_t{1} << num_qubits; 30 | } 31 | }; 32 | 33 | unsigned num_qubits = 4; 34 | uint64_t size = uint64_t{1} << num_qubits; 35 | 36 | VectorSpace vector_space(1); 37 | 38 | auto vector1 = vector_space.Create(num_qubits); 39 | 40 | EXPECT_FALSE(vector_space.IsNull(vector1)); 41 | EXPECT_NE(vector1.get(), nullptr); 42 | EXPECT_EQ(uint64_t(vector1.get()) % 64, 0); 43 | EXPECT_EQ(vector1.num_qubits(), num_qubits); 44 | 45 | std::vector buf(size, 0); 46 | 47 | auto vector2 = vector_space.Create(buf.data(), num_qubits); 48 | EXPECT_FALSE(vector_space.IsNull(vector2)); 49 | EXPECT_EQ(vector2.get(), buf.data()); 50 | EXPECT_EQ(vector2.num_qubits(), num_qubits); 51 | 52 | for (uint64_t i = 0; i < size; ++i) { 53 | vector1.get()[i] = i + 1; 54 | } 55 | 56 | vector_space.Copy(vector1, vector2); 57 | 58 | for (uint64_t i = 0; i < size; ++i) { 59 | EXPECT_FLOAT_EQ(vector2.get()[i], i + 1); 60 | } 61 | 62 | auto vector3 = vector_space.Null(); 63 | EXPECT_TRUE(vector_space.IsNull(vector3)); 64 | EXPECT_EQ(vector3.get(), nullptr); 65 | EXPECT_EQ(vector3.num_qubits(), 0); 66 | 67 | auto p1 = vector1.get(); 68 | 69 | std::swap(vector1, vector3); 70 | 71 | EXPECT_TRUE(vector_space.IsNull(vector1)); 72 | EXPECT_EQ(vector1.get(), nullptr); 73 | 74 | EXPECT_FALSE(vector_space.IsNull(vector3)); 75 | EXPECT_EQ(vector3.get(), p1); 76 | EXPECT_EQ(vector3.num_qubits(), num_qubits); 77 | 78 | for (uint64_t i = 0; i < size; ++i) { 79 | EXPECT_FLOAT_EQ(vector3.get()[i], i + 1); 80 | } 81 | 82 | auto p2 = vector2.release(); 83 | 84 | EXPECT_TRUE(vector_space.IsNull(vector2)); 85 | EXPECT_EQ(vector2.get(), nullptr); 86 | EXPECT_EQ(vector2.num_qubits(), 0); 87 | EXPECT_EQ(p2, buf.data()); 88 | 89 | auto p3 = vector3.release(); 90 | 91 | EXPECT_TRUE(vector_space.IsNull(vector3)); 92 | EXPECT_EQ(vector3.get(), nullptr); 93 | EXPECT_EQ(vector3.num_qubits(), 0); 94 | EXPECT_EQ(p3, p1); 95 | 96 | vector_space.Free(p3); 97 | } 98 | 99 | } // namespace qsim 100 | 101 | int main(int argc, char** argv) { 102 | ::testing::InitGoogleTest(&argc, argv); 103 | return RUN_ALL_TESTS(); 104 | } 105 | -------------------------------------------------------------------------------- /tests/qtrajectory_avx_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google LLC. All Rights Reserved. 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 | #include "qtrajectory_testfixture.h" 16 | 17 | #include "gtest/gtest.h" 18 | 19 | #include "../lib/fuser_mqubit.h" 20 | #include "../lib/gates_cirq.h" 21 | #include "../lib/io.h" 22 | #include "../lib/run_qsim.h" 23 | #include "../lib/seqfor.h" 24 | #include "../lib/simulator_avx.h" 25 | 26 | namespace qsim { 27 | 28 | template 29 | struct Factory { 30 | using Simulator = qsim::SimulatorAVX; 31 | using StateSpace = typename Simulator::StateSpace; 32 | using fp_type = typename StateSpace::fp_type; 33 | 34 | StateSpace CreateStateSpace() const { 35 | return StateSpace(1); 36 | } 37 | 38 | Simulator CreateSimulator() const { 39 | return Simulator(1); 40 | } 41 | }; 42 | 43 | TEST(QTrajectoryAVXTest, BitFlip) { 44 | using Fuser = MultiQubitGateFuser*>; 45 | using Runner = QSimRunner>; 46 | TestBitFlip(Factory()); 47 | } 48 | 49 | TEST(QTrajectoryAVXTest, GenDump) { 50 | using Fuser = MultiQubitGateFuser*>; 51 | using Runner = QSimRunner>; 52 | TestGenDump(Factory()); 53 | } 54 | 55 | TEST(QTrajectoryAVXTest, ReusingResults) { 56 | using Fuser = MultiQubitGateFuser*>; 57 | using Runner = QSimRunner>; 58 | TestReusingResults(Factory()); 59 | } 60 | 61 | TEST(QTrajectoryAVXTest, CollectKopStat) { 62 | using Fuser = MultiQubitGateFuser*>; 63 | using Runner = QSimRunner>; 64 | TestCollectKopStat(Factory()); 65 | } 66 | 67 | TEST(QTrajectoryAVXTest, CleanCircuit) { 68 | using Fuser = MultiQubitGateFuser*>; 69 | using Runner = QSimRunner>; 70 | TestCleanCircuit(Factory()); 71 | } 72 | 73 | TEST(QTrajectoryAVXTest, InitialState) { 74 | using Fuser = MultiQubitGateFuser*>; 75 | using Runner = QSimRunner>; 76 | TestInitialState(Factory()); 77 | } 78 | 79 | TEST(QTrajectoryAVXTest, UncomputeFinalState) { 80 | using Fuser = MultiQubitGateFuser*>; 81 | using Runner = QSimRunner>; 82 | TestUncomputeFinalState(Factory()); 83 | } 84 | 85 | } // namespace qsim 86 | 87 | int main(int argc, char** argv) { 88 | testing::InitGoogleTest(&argc, argv); 89 | return RUN_ALL_TESTS(); 90 | } 91 | --------------------------------------------------------------------------------