diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index ab7eab8070..c49d3d6591 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -9,10 +9,16 @@ ### Improvements +* Update C++ and Python GitHub actions names to include the matrix info. + [(#717)](https://github.com/PennyLaneAI/pennylane-lightning/pull/717) + +* Remove `CPhase` in favour of `CPhaseShift` in Lightning devices. + [(#717)](https://github.com/PennyLaneAI/pennylane-lightning/pull/717) + * The various OpenMP configurations of Lightning-Qubit are tested in parallel on different Github Actions runners. [(#712)](https://github.com/PennyLaneAI/pennylane-lightning/pull/712) -* Update Linux wheels to use manylinux_2_28 images. +* Update Linux wheels to use `manylinux_2_28` images. [(#667)](https://github.com/PennyLaneAI/pennylane-lightning/pull/667) * Add support for `qml.expval` and `qml.var` in the `lightning.tensor` device for the `quimb` interface and the MPS method. @@ -29,7 +35,7 @@ This release contains contributions from (in alphabetical order): -Amintor Dusko, Pietropaolo Frisoni, Vincent Michaud-Rioux, Mudit Pandey +Ali Asadi, Amintor Dusko, Pietropaolo Frisoni, Vincent Michaud-Rioux, Mudit Pandey --- diff --git a/.github/workflows/dev_version_script.py b/.github/workflows/dev_version_script.py index 5093cf6534..4539701500 100644 --- a/.github/workflows/dev_version_script.py +++ b/.github/workflows/dev_version_script.py @@ -12,14 +12,16 @@ # See the License for the specific language governing permissions and # limitations under the License. -import re import argparse +import re from pathlib import Path try: from semver import Version except ImportError as exc: - raise ImportError("Unable to import semver. Install semver by running `pip install semver`") from exc + raise ImportError( + "Unable to import semver. Install semver by running `pip install semver`" + ) from exc DEV_PRERELEASE_TAG_PREFIX = "dev" DEV_PRERELEASE_TAG_START = "dev0" @@ -45,10 +47,14 @@ def extract_version(repo_root_path: Path) -> Version: if line.startswith("__version__"): if (m := rgx_ver.match(line.strip())) is not None: if not m.groups(): - raise ValueError(f"Unable to find valid semver for __version__. Got: '{line}'") + raise ValueError( + f"Unable to find valid semver for __version__. Got: '{line}'" + ) parsed_semver = m.group(1) if not Version.is_valid(parsed_semver): - raise ValueError(f"Invalid semver for __version__. Got: '{parsed_semver}' from line '{line}'") + raise ValueError( + f"Invalid semver for __version__. Got: '{parsed_semver}' from line '{line}'" + ) return Version.parse(parsed_semver) raise ValueError(f"Unable to find valid semver for __version__. Got: '{line}'") raise ValueError("Cannot parse version") @@ -67,10 +73,7 @@ def update_prerelease_version(repo_root_path: Path, version: Version): raise FileNotFoundError(f"Unable to find version file at location {version_file_path}") with version_file_path.open() as f: - lines = [ - rgx_ver.sub(f"__version__ = \"{str(version)}\"", line) - for line in f - ] + lines = [rgx_ver.sub(f'__version__ = "{str(version)}"', line) for line in f] with version_file_path.open("w") as f: f.write("".join(lines)) @@ -98,8 +101,14 @@ def update_prerelease_version(repo_root_path: Path, version: Version): # If a PR is of a higher version AND the prerelease tag is reset, then do nothing # This captures the case during release where we might bump the release version # within a PR and reset tag back to dev0 - if pr_version > master_version and pr_version.prerelease and pr_version.prerelease == DEV_PRERELEASE_TAG_START: - print("This Pull Request is upgrading the package version to next release ... skipping bumping!") + if ( + pr_version > master_version + and pr_version.prerelease + and pr_version.prerelease == DEV_PRERELEASE_TAG_START + ): + print( + "This Pull Request is upgrading the package version to next release ... skipping bumping!" + ) print("If this is happening in error, please report it to the PennyLane team!") elif pr_version.prerelease and pr_version.prerelease.startswith(DEV_PRERELEASE_TAG_PREFIX): # If master branch does not have a prerelease (for any reason) OR does not have an ending number @@ -109,7 +118,10 @@ def update_prerelease_version(repo_root_path: Path, version: Version): else: # If master branch does not have a prerelease (for any reason) OR does not have an ending number # Then default to the starting tag - if not master_version.prerelease or master_version.prerelease == DEV_PRERELEASE_TAG_PREFIX: + if ( + not master_version.prerelease + or master_version.prerelease == DEV_PRERELEASE_TAG_PREFIX + ): next_prerelease_version = DEV_PRERELEASE_TAG_START else: # Generate the next prerelease version (eg: dev1 -> dev2). Sourcing from master version. diff --git a/.github/workflows/tests_lgpu_cpp.yml b/.github/workflows/tests_lgpu_cpp.yml index 9cdc46c389..45c209bfa9 100644 --- a/.github/workflows/tests_lgpu_cpp.yml +++ b/.github/workflows/tests_lgpu_cpp.yml @@ -63,7 +63,7 @@ jobs: pl_backend: ["lightning_gpu"] cuda_version: ["12"] - name: C++ tests (Lightning-GPU) + name: C++ Tests (${{ matrix.pl_backend }}, cuda-${{ matrix.cuda_version }}) runs-on: - ubuntu-22.04 - self-hosted diff --git a/.github/workflows/tests_lgpu_python.yml b/.github/workflows/tests_lgpu_python.yml index 5ab6975fd0..d6a3bc0fc2 100644 --- a/.github/workflows/tests_lgpu_python.yml +++ b/.github/workflows/tests_lgpu_python.yml @@ -66,7 +66,7 @@ jobs: default_backend: ["lightning_qubit"] cuda_version: ["12"] - name: Python tests with LGPU + name: Python Tests (${{ matrix.pl_backend }}, cuda-${{ matrix.cuda_version }}) runs-on: - ubuntu-22.04 - self-hosted @@ -133,7 +133,7 @@ jobs: py_path=$(which python) echo "Python Interpreter Path => $py_path" echo "python=$py_path" >> $GITHUB_OUTPUT - + pip_path=$(which python) echo "PIP Path => $pip_path" echo "pip=$pip_path" >> $GITHUB_OUTPUT @@ -166,7 +166,7 @@ jobs: python -m pip uninstall -y pennylane && python -m pip install -U pennylane - name: Build and install package - env: + env: CUQUANTUM_SDK: $(python -c "import site; print( f'{site.getsitepackages()[0]}/cuquantum')") run: | cd main @@ -181,20 +181,20 @@ jobs: - name: Run PennyLane-Lightning-GPU unit tests if: ${{ matrix.pl_backend != 'all'}} - env: + env: OMP_NUM_THREADS: 1 OMP_PROC_BIND: false run: | cd main/ DEVICENAME=`echo ${{ matrix.pl_backend }} | sed "s/_/./g"` - pl-device-test --device ${DEVICENAME} --skip-ops --shots=20000 $COVERAGE_FLAGS --cov-append + pl-device-test --device ${DEVICENAME} --skip-ops --shots=20000 $COVERAGE_FLAGS --cov-append pl-device-test --device ${DEVICENAME} --shots=None --skip-ops $COVERAGE_FLAGS --cov-append - PL_DEVICE=${DEVICENAME} python -m pytest tests/ $COVERAGE_FLAGS + PL_DEVICE=${DEVICENAME} python -m pytest tests/ $COVERAGE_FLAGS mv coverage.xml coverage-${{ github.job }}-${{ matrix.pl_backend }}.xml - name: Install all backend devices if: ${{ matrix.pl_backend == 'all' }} - env: + env: CUQUANTUM_SDK: $(python -c "import site; print( f'{site.getsitepackages()[0]}/cuquantum')") run: | cd main @@ -202,13 +202,13 @@ jobs: CMAKE_ARGS="-DPL_BACKEND=${{matrix.default_backend}} -DENABLE_PYTHON=ON -DCMAKE_CXX_COMPILER=$(which g++-$GCC_VERSION)" \ python -m pip install . -vv rm -rf build - + CMAKE_ARGS="-DPL_BACKEND=${{ matrix.pl_backend }} -DENABLE_PYTHON=ON -DCMAKE_CXX_COMPILER=$(which g++-$GCC_VERSION)" \ python -m pip install . -vv - name: Run PennyLane-Lightning unit tests for lightning.qubit with all devices installed if: ${{ matrix.pl_backend == 'all' }} - env: + env: OMP_NUM_THREADS: 1 OMP_PROC_BIND: false run: | diff --git a/.github/workflows/tests_lgpumpi_cpp.yml b/.github/workflows/tests_lgpumpi_cpp.yml index 549cb88245..be87b0fa3a 100644 --- a/.github/workflows/tests_lgpumpi_cpp.yml +++ b/.github/workflows/tests_lgpumpi_cpp.yml @@ -184,7 +184,7 @@ jobs: fail_ci_if_error: true verbose: true token: ${{ secrets.CODECOV_TOKEN }} - + - name: Cleanup if: always() run: | diff --git a/.github/workflows/tests_linux_cpp.yml b/.github/workflows/tests_linux_cpp.yml index b66d8edec1..fb4461461c 100644 --- a/.github/workflows/tests_linux_cpp.yml +++ b/.github/workflows/tests_linux_cpp.yml @@ -51,7 +51,7 @@ jobs: - enable_kernel_omp: OFF enable_kernel_avx_stream: ON timeout-minutes: 60 - name: C++ tests + name: C++ Tests (${{ matrix.pl_backend }}, ENABLE_KERNEL_OMP=${{ matrix.enable_kernel_omp }}, ENABLE_KERNEL_AVX_STREAM=${{ matrix.enable_kernel_avx_stream }}) runs-on: ${{ needs.determine_runner.outputs.runner_group }} steps: @@ -113,7 +113,7 @@ jobs: matrix: pl_backend: ["lightning_qubit"] timeout-minutes: 60 - name: C++ tests (OpenBLAS) + name: C++ Tests (${{ matrix.pl_backend }}, blas-ON) runs-on: ${{ needs.determine_runner.outputs.runner_group }} steps: @@ -181,7 +181,7 @@ jobs: exec_model: ${{ fromJson(needs.build_and_cache_Kokkos.outputs.exec_model) }} kokkos_version: ${{ fromJson(needs.build_and_cache_Kokkos.outputs.kokkos_version) }} timeout-minutes: 60 - name: C++ tests (Kokkos) + name: C++ Tests (${{ matrix.pl_backend }}, kokkos-${{ matrix.kokkos_version }}, model-${{ matrix.exec_model }}) runs-on: ${{ needs.determine_runner.outputs.runner_group }} steps: @@ -275,7 +275,7 @@ jobs: exec_model: ${{ fromJson(needs.build_and_cache_Kokkos.outputs.exec_model) }} kokkos_version: ${{ fromJson(needs.build_and_cache_Kokkos.outputs.kokkos_version) }} timeout-minutes: 60 - name: C++ tests (multiple backends) + name: C++ Tests (multiple-backends, kokkos-${{ matrix.kokkos_version }}, model-${{ matrix.exec_model }}) runs-on: ${{ needs.determine_runner.outputs.runner_group }} steps: diff --git a/.github/workflows/tests_lkcpu_python.yml b/.github/workflows/tests_lkcpu_python.yml index 340e4e5c93..3ecec03276 100644 --- a/.github/workflows/tests_lkcpu_python.yml +++ b/.github/workflows/tests_lkcpu_python.yml @@ -58,7 +58,7 @@ jobs: exec_model: ${{ fromJson(needs.build_and_cache_Kokkos.outputs.exec_model) }} kokkos_version: ${{ fromJson(needs.build_and_cache_Kokkos.outputs.kokkos_version) }} timeout-minutes: 60 - name: Python tests with Kokkos + name: Build (${{ matrix.pl_backend }}, kokkos-${{ matrix.kokkos_version }}, model-${{ matrix.exec_model }}) runs-on: ${{ needs.determine_runner.outputs.runner_group }} steps: @@ -153,7 +153,7 @@ jobs: exec_model: ${{ fromJson(needs.build_and_cache_Kokkos.outputs.exec_model) }} kokkos_version: ${{ fromJson(needs.build_and_cache_Kokkos.outputs.kokkos_version) }} timeout-minutes: 60 - name: Python tests with Kokkos + name: Python Tests (${{ matrix.pl_backend }}, kokkos-${{ matrix.kokkos_version }}, model-${{ matrix.exec_model }}, test-group-${{ matrix.group }}) runs-on: ${{ needs.determine_runner.outputs.runner_group }} steps: diff --git a/.github/workflows/tests_lkcuda_cpp.yml b/.github/workflows/tests_lkcuda_cpp.yml index bca4dc79da..77a25896d1 100644 --- a/.github/workflows/tests_lkcuda_cpp.yml +++ b/.github/workflows/tests_lkcuda_cpp.yml @@ -111,7 +111,7 @@ jobs: exec_model: ["CUDA"] kokkos_version: ["4.2.00"] - name: C++ tests (Kokkos) + name: C++ Tests (${{ matrix.pl_backend }}, kokkos-${{ matrix.kokkos_version }}, model-${{ matrix.exec_model }}) runs-on: - ${{ matrix.os }} - self-hosted diff --git a/.github/workflows/tests_lkcuda_python.yml b/.github/workflows/tests_lkcuda_python.yml index 878a1051e5..52884ff9b9 100644 --- a/.github/workflows/tests_lkcuda_python.yml +++ b/.github/workflows/tests_lkcuda_python.yml @@ -112,7 +112,7 @@ jobs: exec_model: ["CUDA"] kokkos_version: ["4.2.00"] - name: Python tests with Kokkos + name: Python Tests (${{ matrix.pl_backend }}, kokkos-${{ matrix.kokkos_version }}, model-${{ matrix.exec_model }}) runs-on: - ${{ matrix.os }} - self-hosted diff --git a/.github/workflows/tests_lqcpu_python.yml b/.github/workflows/tests_lqcpu_python.yml index 7124b3f0d5..01e2f77670 100644 --- a/.github/workflows/tests_lqcpu_python.yml +++ b/.github/workflows/tests_lqcpu_python.yml @@ -49,7 +49,7 @@ jobs: pl_backend: ["lightning_qubit"] blas: ["OFF", "ON"] timeout-minutes: 60 - name: Python tests + name: Build (${{ matrix.pl_backend }}, blas-${{ matrix.blas }}) runs-on: ${{ needs.determine_runner.outputs.runner_group }} steps: @@ -126,7 +126,7 @@ jobs: blas: ["OFF", "ON"] group: [1, 2, 3, 4] timeout-minutes: 60 - name: Python tests + name: Python Tests (${{ matrix.pl_backend }}, blas-${{ matrix.blas }}, test-group-${{ matrix.group }}) runs-on: ${{ needs.determine_runner.outputs.runner_group }} steps: diff --git a/.github/workflows/tests_windows_cpp.yml b/.github/workflows/tests_windows_cpp.yml index 83d981c2a3..2a212d21a3 100644 --- a/.github/workflows/tests_windows_cpp.yml +++ b/.github/workflows/tests_windows_cpp.yml @@ -53,7 +53,7 @@ jobs: exec_model: ${{ fromJson(needs.win-set-matrix-x86.outputs.exec_model) }} kokkos_version: ${{ fromJson(needs.win-set-matrix-x86.outputs.kokkos_version) }} timeout-minutes: 30 - name: Kokkos core (${{ matrix.exec_model }}) + name: Build Kokkos core (kokkos-${{ matrix.kokkos_version }}, model-${{ matrix.exec_model }}) runs-on: ${{ matrix.os }} steps: @@ -100,12 +100,13 @@ jobs: cpptests: if: ${{ !contains(fromJSON('["schedule", "workflow_dispatch"]'), github.event_name) }} timeout-minutes: 30 - name: C++ tests (Windows) runs-on: ${{ matrix.os }} strategy: matrix: os: [windows-latest] pl_backend: ["lightning_qubit"] + name: C++ Tests (${{ matrix.pl_backend }}, on-${{ matrix.os }}) + steps: - uses: actions/setup-python@v4 name: Install Python @@ -114,7 +115,7 @@ jobs: - name: Checkout PennyLane-Lightning uses: actions/checkout@v3 - + - name: Install dependencies run: | python -m pip install cmake build ninja scipy @@ -155,7 +156,7 @@ jobs: name: windows-test-report-${{ github.job }}-${{ matrix.pl_backend }} path: .\Build\tests\results\ if-no-files-found: error - + - name: Upload coverage results uses: actions/upload-artifact@v3 with: @@ -174,7 +175,7 @@ jobs: kokkos_version: ${{ fromJson(needs.win-set-matrix-x86.outputs.kokkos_version) }} timeout-minutes: 30 - name: C++ tests (Windows, Kokkos) + name: C++ Tests (${{matrix.pl_backend}}, on-${{ matrix.os }}, kokkos-${{ matrix.kokkos_version }}, model-${{ matrix.exec_model }}) runs-on: ${{ matrix.os }} steps: diff --git a/.github/workflows/tests_without_binary.yml b/.github/workflows/tests_without_binary.yml index e0cd338255..9950a525a0 100644 --- a/.github/workflows/tests_without_binary.yml +++ b/.github/workflows/tests_without_binary.yml @@ -34,12 +34,13 @@ jobs: pythontests: needs: [determine_runner] timeout-minutes: 30 - name: Python tests runs-on: ${{ needs.determine_runner.outputs.runner_group }} strategy: matrix: pl_backend: ["lightning_qubit", "lightning_kokkos", "lightning_gpu"] + name: Python Tests without Binary (${{ matrix.pl_backend }}) + steps: - name: Checkout PennyLane-Lightning uses: actions/checkout@v4 diff --git a/.github/workflows/vb_script.py b/.github/workflows/vb_script.py index 2595bfd850..f50a41fc12 100644 --- a/.github/workflows/vb_script.py +++ b/.github/workflows/vb_script.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import argparse + import pennylane as qml pl_version = '"' + qml.version() + '"' diff --git a/doc/docker.rst b/doc/dev/docker.rst similarity index 79% rename from doc/docker.rst rename to doc/dev/docker.rst index 85ae81ba73..40528eba14 100644 --- a/doc/docker.rst +++ b/doc/dev/docker.rst @@ -1,3 +1,3 @@ -.. include:: ../README.rst +.. include:: ../../README.rst :start-after: docker-start-inclusion-marker-do-not-remove :end-before: docker-end-inclusion-marker-do-not-remove diff --git a/doc/installation.rst b/doc/dev/installation.rst similarity index 71% rename from doc/installation.rst rename to doc/dev/installation.rst index c0b056f5c0..639792896d 100644 --- a/doc/installation.rst +++ b/doc/dev/installation.rst @@ -6,17 +6,17 @@ Each device in the Lightning ecosystem is a separate Python package. Select the .. title-card:: :name: Lightning Qubit :description: Guidelines to installing and testing the Lightning Qubit device. - :link: ./lightning_qubit/installation.html + :link: ../lightning_qubit/installation.html .. title-card:: :name: Lightning GPU :description: Guidelines to installing and testing the Lightning GPU device - :link: ./lightning_gpu/installation.html + :link: ../lightning_gpu/installation.html .. title-card:: :name: Lightning Kokkos :description: Guidelines to installing and testing the Lightning Kokkos device - :link: ./lightning_kokkos/installation.html + :link: ../lightning_kokkos/installation.html .. raw:: html @@ -26,6 +26,6 @@ Each device in the Lightning ecosystem is a separate Python package. Select the .. toctree:: :hidden: - lightning_qubit/installation - lightning_gpu/installation - lightning_kokkos/installation + ../lightning_qubit/installation + ../lightning_gpu/installation + ../lightning_kokkos/installation diff --git a/doc/support.rst b/doc/dev/support.rst similarity index 79% rename from doc/support.rst rename to doc/dev/support.rst index 734dc40eff..b3929172b5 100644 --- a/doc/support.rst +++ b/doc/dev/support.rst @@ -1,3 +1,3 @@ -.. include:: ../README.rst +.. include:: ../../README.rst :start-after: support-start-inclusion-marker-do-not-remove :end-before: support-end-inclusion-marker-do-not-remove diff --git a/doc/index.rst b/doc/index.rst index f48d86c567..fae9442113 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -43,9 +43,9 @@ The Lightning ecosystem provides the following devices: :titlesonly: :hidden: - installation - docker - support + dev/installation + dev/docker + dev/support .. toctree:: :maxdepth: 2 diff --git a/doc/lightning_gpu/device.rst b/doc/lightning_gpu/device.rst index 6ac52272ce..1f0d6d92d6 100644 --- a/doc/lightning_gpu/device.rst +++ b/doc/lightning_gpu/device.rst @@ -39,7 +39,6 @@ Supported operations and observables ~pennylane.CNOT ~pennylane.ControlledPhaseShift ~pennylane.ControlledQubitUnitary - ~pennylane.CPhase ~pennylane.CRot ~pennylane.CRX ~pennylane.CRY diff --git a/doc/lightning_kokkos/device.rst b/doc/lightning_kokkos/device.rst index 3c13fe41aa..2016d27c31 100644 --- a/doc/lightning_kokkos/device.rst +++ b/doc/lightning_kokkos/device.rst @@ -37,7 +37,6 @@ Supported operations and observables ~pennylane.CNOT ~pennylane.ControlledPhaseShift ~pennylane.ControlledQubitUnitary - ~pennylane.CPhase ~pennylane.CRot ~pennylane.CRX ~pennylane.CRY diff --git a/doc/lightning_qubit/device.rst b/doc/lightning_qubit/device.rst index 53a1e5d9a2..4a3e3535e2 100644 --- a/doc/lightning_qubit/device.rst +++ b/doc/lightning_qubit/device.rst @@ -29,7 +29,6 @@ Supported operations and observables ~pennylane.CNOT ~pennylane.ControlledPhaseShift ~pennylane.ControlledQubitUnitary - ~pennylane.CPhase ~pennylane.CRot ~pennylane.CRX ~pennylane.CRY diff --git a/pennylane_lightning/core/_version.py b/pennylane_lightning/core/_version.py index 2ce6abc8cd..b1bdc4a571 100644 --- a/pennylane_lightning/core/_version.py +++ b/pennylane_lightning/core/_version.py @@ -16,4 +16,4 @@ Version number (major.minor.patch[-label]) """ -__version__ = "0.37.0-dev6" +__version__ = "0.37.0-dev7" diff --git a/pennylane_lightning/core/src/algorithms/AdjointJacobianBase.hpp b/pennylane_lightning/core/src/algorithms/AdjointJacobianBase.hpp index c8bd1474df..b96c7b41e0 100644 --- a/pennylane_lightning/core/src/algorithms/AdjointJacobianBase.hpp +++ b/pennylane_lightning/core/src/algorithms/AdjointJacobianBase.hpp @@ -53,7 +53,7 @@ template class AdjointJacobianBase { inline void applyOperations(UpdatedStateVectorT &state, const OpsData &operations, bool adj = false) { - for (size_t op_idx = 0; op_idx < operations.getOpsName().size(); + for (std::size_t op_idx = 0; op_idx < operations.getOpsName().size(); op_idx++) { if (operations.getOpsControlledWires()[op_idx].empty()) { state.applyOperation(operations.getOpsName()[op_idx], @@ -86,7 +86,7 @@ template class AdjointJacobianBase { template inline void applyOperationAdj(UpdatedStateVectorT &state, const OpsData &operations, - size_t op_idx) { + std::size_t op_idx) { if (operations.getOpsControlledWires()[op_idx].empty()) { state.applyOperation(operations.getOpsName()[op_idx], operations.getOpsWires()[op_idx], @@ -115,7 +115,7 @@ template class AdjointJacobianBase { */ inline void applyOperationsAdj(std::vector &states, const OpsData &operations, - size_t op_idx) { + std::size_t op_idx) { for (auto &state : states) { applyOperationAdj(state, operations, op_idx); } @@ -132,8 +132,8 @@ template class AdjointJacobianBase { * @return PrecisionT Generator scaling coefficient. */ inline auto applyGenerator(StateVectorT &sv, const std::string &op_name, - const std::vector &wires, const bool adj) - -> PrecisionT { + const std::vector &wires, + const bool adj) -> PrecisionT { return sv.applyGenerator(op_name, wires, adj); } @@ -150,10 +150,10 @@ template class AdjointJacobianBase { * @return PrecisionT Generator scaling coefficient. */ inline auto applyGenerator(StateVectorT &sv, const std::string &op_name, - const std::vector &controlled_wires, + const std::vector &controlled_wires, const std::vector &controlled_values, - const std::vector &wires, const bool adj) - -> PrecisionT { + const std::vector &wires, + const bool adj) -> PrecisionT { return sv.applyGenerator(op_name, controlled_wires, controlled_values, wires, adj); } @@ -182,8 +182,8 @@ template class AdjointJacobianBase { std::vector &states, const StateVectorT &reference_state, const std::vector>> &observables) { - size_t num_observables = observables.size(); - for (size_t i = 0; i < num_observables; i++) { + std::size_t num_observables = observables.size(); + for (std::size_t i = 0; i < num_observables; i++) { states[i].updateData(reference_state); applyObservable(states[i], *observables[i]); } diff --git a/pennylane_lightning/core/src/algorithms/JacobianData.hpp b/pennylane_lightning/core/src/algorithms/JacobianData.hpp index 01db01d2cf..c408167dbe 100644 --- a/pennylane_lightning/core/src/algorithms/JacobianData.hpp +++ b/pennylane_lightning/core/src/algorithms/JacobianData.hpp @@ -42,14 +42,14 @@ template class OpsData { using PrecisionT = typename StateVectorT::PrecisionT; using ComplexT = typename StateVectorT::ComplexT; - size_t num_par_ops_; - size_t num_nonpar_ops_; + std::size_t num_par_ops_; + std::size_t num_nonpar_ops_; const std::vector ops_name_; const std::vector> ops_params_; - const std::vector> ops_wires_; + const std::vector> ops_wires_; const std::vector ops_inverses_; const std::vector> ops_matrices_; - const std::vector> ops_controlled_wires_; + const std::vector> ops_controlled_wires_; const std::vector> ops_controlled_values_; public: @@ -69,10 +69,10 @@ template class OpsData { */ OpsData(std::vector ops_name, const std::vector> &ops_params, - std::vector> ops_wires, + std::vector> ops_wires, std::vector ops_inverses, std::vector> ops_matrices, - std::vector> ops_controlled_wires, + std::vector> ops_controlled_wires, std::vector> ops_controlled_values) : num_par_ops_{0}, ops_name_{std::move(ops_name)}, ops_params_{ops_params}, ops_wires_{std::move(ops_wires)}, @@ -81,7 +81,7 @@ template class OpsData { ops_controlled_wires_{std::move(ops_controlled_wires)}, ops_controlled_values_{std::move(ops_controlled_values)} { for (const auto &p : ops_params) { - num_par_ops_ += static_cast(!p.empty()); + num_par_ops_ += static_cast(!p.empty()); } num_nonpar_ops_ = ops_params.size() - num_par_ops_; }; @@ -100,7 +100,7 @@ template class OpsData { */ OpsData(std::vector ops_name, const std::vector> &ops_params, - std::vector> ops_wires, + std::vector> ops_wires, std::vector ops_inverses, std::vector> ops_matrices) : num_par_ops_{0}, ops_name_{std::move(ops_name)}, @@ -110,7 +110,7 @@ template class OpsData { ops_controlled_wires_(ops_name.size()), ops_controlled_values_(ops_name.size()) { for (const auto &p : ops_params) { - num_par_ops_ += static_cast(!p.empty()); + num_par_ops_ += static_cast(!p.empty()); } num_nonpar_ops_ = ops_params.size() - num_par_ops_; }; @@ -121,13 +121,13 @@ template class OpsData { * * @see OpsData(const std::vector &ops_name, const std::vector> &ops_params, - const std::vector> &ops_wires, + const std::vector> &ops_wires, const std::vector &ops_inverses, const std::vector> &ops_matrices) */ OpsData(const std::vector &ops_name, const std::vector> &ops_params, - std::vector> ops_wires, + std::vector> ops_wires, std::vector ops_inverses) : num_par_ops_{0}, ops_name_{ops_name}, ops_params_{ops_params}, ops_wires_{std::move(ops_wires)}, @@ -136,7 +136,7 @@ template class OpsData { ops_controlled_wires_(ops_name.size()), ops_controlled_values_(ops_name.size()) { for (const auto &p : ops_params) { - num_par_ops_ += static_cast(!p.empty()); + num_par_ops_ += static_cast(!p.empty()); } num_nonpar_ops_ = ops_params.size() - num_par_ops_; }; @@ -144,9 +144,11 @@ template class OpsData { /** * @brief Get the number of operations to be applied. * - * @return size_t Number of operations. + * @return std::size_t Number of operations. */ - [[nodiscard]] auto getSize() const -> size_t { return ops_name_.size(); } + [[nodiscard]] auto getSize() const -> std::size_t { + return ops_name_.size(); + } /** * @brief Get the names of the operations to be applied. @@ -169,25 +171,25 @@ template class OpsData { /** * @brief Get the wires for each operation. * - * @return const std::vector>& + * @return const std::vector>& */ [[nodiscard]] auto getOpsWires() const - -> const std::vector> & { + -> const std::vector> & { return ops_wires_; } /** * @brief Get the controlled wires for each operation. * - * @return const std::vector>& + * @return const std::vector>& */ [[nodiscard]] auto getOpsControlledWires() const - -> const std::vector> & { + -> const std::vector> & { return ops_controlled_wires_; } /** * @brief Get the controlled values for each operation. * - * @return const std::vector>& + * @return const std::vector>& */ [[nodiscard]] auto getOpsControlledValues() const -> const std::vector> & { @@ -219,32 +221,34 @@ template class OpsData { * @return true Gate is parametric (has parameters). * @return false Gate in non-parametric. */ - [[nodiscard]] inline auto hasParams(size_t index) const -> bool { + [[nodiscard]] inline auto hasParams(std::size_t index) const -> bool { return !ops_params_[index].empty(); } /** * @brief Get the number of parametric operations. * - * @return size_t + * @return std::size_t */ - [[nodiscard]] auto getNumParOps() const -> size_t { return num_par_ops_; } + [[nodiscard]] auto getNumParOps() const -> std::size_t { + return num_par_ops_; + } /** * @brief Get the number of non-parametric ops. * - * @return size_t + * @return std::size_t */ - [[nodiscard]] auto getNumNonParOps() const -> size_t { + [[nodiscard]] auto getNumNonParOps() const -> std::size_t { return num_nonpar_ops_; } /** * @brief Get total number of parameters. */ - [[nodiscard]] auto getTotalNumParams() const -> size_t { + [[nodiscard]] auto getTotalNumParams() const -> std::size_t { return std::accumulate( - ops_params_.begin(), ops_params_.end(), size_t{0U}, + ops_params_.begin(), ops_params_.end(), std::size_t{0U}, [](size_t acc, auto ¶ms) { return acc + params.size(); }); } }; @@ -257,9 +261,9 @@ template class OpsData { template class JacobianData { private: using CFP_t = typename StateVectorT::CFP_t; - size_t num_parameters; /**< Number of parameters in the tape */ - size_t num_elements; /**< Length of the statevector data */ - const CFP_t *psi; /**< Pointer to the statevector data */ + std::size_t num_parameters; /**< Number of parameters in the tape */ + std::size_t num_elements; /**< Length of the statevector data */ + const CFP_t *psi; /**< Pointer to the statevector data */ /** * @var observables @@ -274,7 +278,7 @@ template class JacobianData { const OpsData operations; /* @var trainableParams */ - const std::vector trainableParams; + const std::vector trainableParams; public: JacobianData(const JacobianData &) = default; @@ -302,9 +306,9 @@ template class JacobianData { * (e.g. StatePrep) or Hamiltonian coefficients. * @endrst */ - JacobianData(size_t num_params, size_t num_elem, const CFP_t *sv_ptr, + JacobianData(size_t num_params, std::size_t num_elem, const CFP_t *sv_ptr, std::vector>> obs, - OpsData ops, std::vector trainP) + OpsData ops, std::vector trainP) : num_parameters(num_params), num_elements(num_elem), psi(sv_ptr), observables(std::move(obs)), operations(std::move(ops)), trainableParams(std::move(trainP)) { @@ -315,16 +319,18 @@ template class JacobianData { /** * @brief Get Number of parameters in the Tape. * - * @return size_t + * @return std::size_t */ - [[nodiscard]] auto getNumParams() const -> size_t { return num_parameters; } + [[nodiscard]] auto getNumParams() const -> std::size_t { + return num_parameters; + } /** * @brief Get the length of the statevector data. * - * @return size_t + * @return std::size_t */ - [[nodiscard]] auto getSizeStateVec() const -> size_t { + [[nodiscard]] auto getSizeStateVec() const -> std::size_t { return num_elements; } @@ -349,9 +355,9 @@ template class JacobianData { * @brief Get the number of observables for which to calculate * Jacobian. * - * @return size_t + * @return std::size_t */ - [[nodiscard]] auto getNumObservables() const -> size_t { + [[nodiscard]] auto getNumObservables() const -> std::size_t { return observables.size(); } @@ -368,10 +374,10 @@ template class JacobianData { * @brief Get list of parameters participating in Jacobian * calculation. * - * @return std::vector& + * @return std::vector& */ [[nodiscard]] auto getTrainableParams() const - -> const std::vector & { + -> const std::vector & { return trainableParams; } diff --git a/pennylane_lightning/core/src/algorithms/tests/Test_AdjointJacobian.cpp b/pennylane_lightning/core/src/algorithms/tests/Test_AdjointJacobian.cpp index b0f0441a15..0f56ca388c 100644 --- a/pennylane_lightning/core/src/algorithms/tests/Test_AdjointJacobian.cpp +++ b/pennylane_lightning/core/src/algorithms/tests/Test_AdjointJacobian.cpp @@ -90,12 +90,12 @@ template void testAdjointJacobian() { DYNAMIC_SECTION("Throws an exception when size mismatches - " << StateVectorToName::name) { - const std::vector tp{0, 1}; - const size_t num_qubits = 1; - const size_t num_params = 3; - const size_t num_obs = 1; + const std::vector tp{0, 1}; + const std::size_t num_qubits = 1; + const std::size_t num_params = 3; + const std::size_t num_obs = 1; const auto obs = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); std::vector jacobian(num_obs * tp.size() - 1, 0); auto ops = OpsData({"RX"}, {{0.742}}, {{0}}, {false}); @@ -115,12 +115,12 @@ template void testAdjointJacobian() { DYNAMIC_SECTION("No trainable params - " << StateVectorToName::name) { - const std::vector tp{}; - const size_t num_qubits = 1; - const size_t num_params = 3; - const size_t num_obs = 1; + const std::vector tp{}; + const std::size_t num_qubits = 1; + const std::size_t num_params = 3; + const std::size_t num_obs = 1; const auto obs = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); std::vector jacobian(num_obs * tp.size(), 0); for (const auto &p : param) { @@ -141,18 +141,18 @@ template void testAdjointJacobian() { DYNAMIC_SECTION("Op=PhaseShift, Obs=Y - " << StateVectorToName::name) { if (SUPPORTS_CTRL) { - const std::vector tp{0}; - const size_t num_qubits = GENERATE(2, 3, 4); + const std::vector tp{0}; + const std::size_t num_qubits = GENERATE(2, 3, 4); - const size_t num_params = 3; - const size_t num_obs = 1; + const std::size_t num_params = 3; + const std::size_t num_obs = 1; const auto obs = std::make_shared>( - "PauliY", std::vector{num_qubits - 1}); + "PauliY", std::vector{num_qubits - 1}); std::vector jacobian(num_obs * tp.size(), 0); for (const auto &p : param) { - std::vector> controls{ - std::vector(num_qubits - 1)}; + std::vector> controls{ + std::vector(num_qubits - 1)}; std::iota(controls[0].begin(), controls[0].end(), 0); std::vector> control_values{ std::vector(num_qubits - 1, true)}; @@ -180,13 +180,13 @@ template void testAdjointJacobian() { DYNAMIC_SECTION("Op=RX, Obs=Z - " << StateVectorToName::name) { - const std::vector tp{0}; + const std::vector tp{0}; - const size_t num_qubits = 1; - const size_t num_params = 3; - const size_t num_obs = 1; + const std::size_t num_qubits = 1; + const std::size_t num_params = 3; + const std::size_t num_obs = 1; const auto obs = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); std::vector jacobian(num_obs * tp.size(), 0); for (const auto &p : param) { @@ -208,13 +208,13 @@ template void testAdjointJacobian() { DYNAMIC_SECTION("Op=RY, Obs=X - " << StateVectorToName::name) { - std::vector tp{0}; - const size_t num_qubits = 1; - const size_t num_params = 3; - const size_t num_obs = 1; + std::vector tp{0}; + const std::size_t num_qubits = 1; + const std::size_t num_params = 3; + const std::size_t num_obs = 1; const auto obs = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); std::vector jacobian(num_obs * tp.size(), 0); for (const auto &p : param) { @@ -236,10 +236,10 @@ template void testAdjointJacobian() { DYNAMIC_SECTION("Op=RX, Obs=[Z,Z] - " << StateVectorToName::name) { - std::vector tp{0}; - const size_t num_qubits = 2; - const size_t num_params = 1; - const size_t num_obs = 2; + std::vector tp{0}; + const std::size_t num_qubits = 2; + const std::size_t num_params = 1; + const std::size_t num_obs = 2; std::vector jacobian(num_obs * tp.size(), 0); std::vector cdata(1U << num_qubits); @@ -248,9 +248,9 @@ template void testAdjointJacobian() { StateVectorT psi(cdata.data(), cdata.size()); const auto obs1 = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); const auto obs2 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto ops = OpsData({"RX"}, {{param[0]}}, {{0}}, {false}); @@ -267,10 +267,10 @@ template void testAdjointJacobian() { DYNAMIC_SECTION("Op=[RX,RX,RX], Obs=[Z,Z,Z] - " << StateVectorToName::name) { - std::vector tp{0, 1, 2}; - const size_t num_qubits = 3; - const size_t num_params = 3; - const size_t num_obs = 3; + std::vector tp{0, 1, 2}; + const std::size_t num_qubits = 3; + const std::size_t num_params = 3; + const std::size_t num_obs = 3; std::vector jacobian(num_obs * tp.size(), 0); std::vector cdata(1U << num_qubits); @@ -279,11 +279,11 @@ template void testAdjointJacobian() { StateVectorT psi(cdata.data(), cdata.size()); const auto obs1 = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); const auto obs2 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); const auto obs3 = std::make_shared>( - "PauliZ", std::vector{2}); + "PauliZ", std::vector{2}); auto ops = OpsData( {"RX", "RX", "RX"}, {{param[0]}, {param[1]}, {param[2]}}, @@ -305,10 +305,10 @@ template void testAdjointJacobian() { DYNAMIC_SECTION("Op=[RX,RX,RX], Obs=[Z,Z,Z], TParams=[0,2] - " << StateVectorToName::name) { std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector t_params{0, 2}; - const size_t num_qubits = 3; - const size_t num_params = 3; - const size_t num_obs = 3; + std::vector t_params{0, 2}; + const std::size_t num_qubits = 3; + const std::size_t num_params = 3; + const std::size_t num_obs = 3; std::vector jacobian(num_obs * t_params.size(), 0); std::vector cdata(1U << num_qubits); @@ -316,11 +316,11 @@ template void testAdjointJacobian() { StateVectorT psi(cdata.data(), cdata.size()); const auto obs1 = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); const auto obs2 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); const auto obs3 = std::make_shared>( - "PauliZ", std::vector{2}); + "PauliZ", std::vector{2}); auto ops = OpsData( {"RX", "RX", "RX"}, {{param[0]}, {param[1]}, {param[2]}}, @@ -342,10 +342,10 @@ template void testAdjointJacobian() { DYNAMIC_SECTION("Op=[RX,RX,RX], Obs=[ZZZ] - " << StateVectorToName::name) { std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector tp{0, 1, 2}; - const size_t num_qubits = 3; - const size_t num_params = 3; - const size_t num_obs = 1; + std::vector tp{0, 1, 2}; + const std::size_t num_qubits = 3; + const std::size_t num_params = 3; + const std::size_t num_obs = 1; std::vector jacobian(num_obs * tp.size(), 0); std::vector cdata(1U << num_qubits); @@ -354,11 +354,11 @@ template void testAdjointJacobian() { const auto obs = std::make_shared>( std::make_shared>( - "PauliZ", std::vector{0}), + "PauliZ", std::vector{0}), std::make_shared>( - "PauliZ", std::vector{1}), + "PauliZ", std::vector{1}), std::make_shared>( - "PauliZ", std::vector{2})); + "PauliZ", std::vector{2})); auto ops = OpsData( {"RX", "RX", "RX"}, {{param[0]}, {param[1]}, {param[2]}}, {{0}, {1}, {2}}, {false, false, false}); @@ -379,10 +379,10 @@ template void testAdjointJacobian() { DYNAMIC_SECTION("Op=Mixed, Obs=[XXX] - " << StateVectorToName::name) { std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector tp{0, 1, 2, 3, 4, 5}; - const size_t num_qubits = 3; - const size_t num_params = 6; - const size_t num_obs = 1; + std::vector tp{0, 1, 2, 3, 4, 5}; + const std::size_t num_qubits = 3; + const std::size_t num_params = 6; + const std::size_t num_obs = 1; std::vector jacobian(num_obs * tp.size(), 0); std::vector cdata(1U << num_qubits); @@ -391,11 +391,11 @@ template void testAdjointJacobian() { const auto obs = std::make_shared>( std::make_shared>( - "PauliX", std::vector{0}), + "PauliX", std::vector{0}), std::make_shared>( - "PauliX", std::vector{1}), + "PauliX", std::vector{1}), std::make_shared>( - "PauliX", std::vector{2})); + "PauliX", std::vector{2})); std::vector cnot{1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0}; auto ops = OpsData( @@ -412,7 +412,7 @@ template void testAdjointJacobian() { {false, false, false, false, false, false, false, false}, std::vector>{ {}, {}, {}, cnot, {}, {}, {}, {}}, - std::vector>{ + std::vector>{ {}, {}, {}, {}, {}, {}, {}, {}}, std::vector>{{}, {}, {}, {}, {}, {}, {}, {}}); @@ -434,9 +434,9 @@ template void testAdjointJacobian() { DYNAMIC_SECTION("Decomposed Rot gate, non computational basis state - " << StateVectorToName::name) { - const std::vector tp{0, 1, 2}; - const size_t num_params = 3; - const size_t num_obs = 1; + const std::vector tp{0, 1, 2}; + const std::size_t num_params = 3; + const std::size_t num_obs = 1; PrecisionT limit = 2 * M_PI; const std::vector thetas = linspace(-limit, limit, 7); @@ -451,7 +451,7 @@ template void testAdjointJacobian() { {0, -9.90819511e-01, 0}}; const auto obs = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); for (size_t i = 0; i < thetas.size(); i++) { const PrecisionT theta = thetas[i]; @@ -487,8 +487,8 @@ template void testAdjointJacobian() { DYNAMIC_SECTION("Mixed Ops, Obs and TParams - " << StateVectorToName::name) { std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - const std::vector t_params{1, 2, 3}; - const size_t num_obs = 1; + const std::vector t_params{1, 2, 3}; + const std::size_t num_obs = 1; PrecisionT limit = 2 * M_PI; const std::vector thetas = linspace(-limit, limit, 8); @@ -505,9 +505,9 @@ template void testAdjointJacobian() { const auto obs = std::make_shared>( std::make_shared>( - "PauliX", std::vector{0}), + "PauliX", std::vector{0}), std::make_shared>( - "PauliZ", std::vector{1})); + "PauliZ", std::vector{1})); auto ops = OpsData( {"Hadamard", "RX", "CNOT", "RZ", "RY", "RZ", "RZ", "RY", "RZ", @@ -555,10 +555,10 @@ template void testAdjointJacobian() { DYNAMIC_SECTION("Op=RX, Obs=Ham[Z0+Z1] - " << StateVectorToName::name) { std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector tp{0}; - const size_t num_qubits = 2; - const size_t num_params = 1; - const size_t num_obs = 1; + std::vector tp{0}; + const std::size_t num_qubits = 2; + const std::size_t num_params = 1; + const std::size_t num_obs = 1; std::vector jacobian(num_obs * tp.size(), 0); std::vector cdata(1U << num_qubits); @@ -566,9 +566,9 @@ template void testAdjointJacobian() { StateVectorT psi(cdata.data(), cdata.size()); const auto obs1 = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); const auto obs2 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto ham = Hamiltonian::create({0.3, 0.7}, {obs1, obs2}); @@ -588,10 +588,10 @@ template void testAdjointJacobian() { DYNAMIC_SECTION("Op=[RX,RX,RX], Obs=Ham[Z0+Z1+Z2], TParams=[0,2] - " << StateVectorToName::name) { std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector t_params{0, 2}; - const size_t num_qubits = 3; - const size_t num_params = 3; - const size_t num_obs = 1; + std::vector t_params{0, 2}; + const std::size_t num_qubits = 3; + const std::size_t num_params = 3; + const std::size_t num_obs = 1; std::vector jacobian(num_obs * t_params.size(), 0); std::vector cdata(1U << num_qubits); @@ -599,11 +599,11 @@ template void testAdjointJacobian() { StateVectorT psi(cdata.data(), cdata.size()); auto obs1 = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); auto obs2 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto obs3 = std::make_shared>( - "PauliZ", std::vector{2}); + "PauliZ", std::vector{2}); auto ham = Hamiltonian::create({0.47, 0.32, 0.96}, {obs1, obs2, obs3}); @@ -625,10 +625,10 @@ template void testAdjointJacobian() { DYNAMIC_SECTION("HermitianObs - " << StateVectorToName::name) { std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector t_params{0, 2}; - const size_t num_qubits = 3; - const size_t num_params = 3; - const size_t num_obs = 1; + std::vector t_params{0, 2}; + const std::size_t num_qubits = 3; + const std::size_t num_params = 3; + const std::size_t num_obs = 1; std::vector jacobian1(num_obs * t_params.size(), 0); std::vector jacobian2(num_obs * t_params.size(), 0); @@ -638,13 +638,13 @@ template void testAdjointJacobian() { auto obs1 = std::make_shared>( std::make_shared>( - "PauliZ", std::vector{0}), + "PauliZ", std::vector{0}), std::make_shared>( - "PauliZ", std::vector{1})); + "PauliZ", std::vector{1})); auto obs2 = std::make_shared>( std::vector{1, 0, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1}, - std::vector{0, 1}); + std::vector{0, 1}); auto ops = OpsData( {"RX", "RX", "RX"}, {{param[0]}, {param[1]}, {param[2]}}, diff --git a/pennylane_lightning/core/src/algorithms/tests/mpi/Test_AdjointJacobianMPI.cpp b/pennylane_lightning/core/src/algorithms/tests/mpi/Test_AdjointJacobianMPI.cpp index 8619866c3a..b4e617eec4 100644 --- a/pennylane_lightning/core/src/algorithms/tests/mpi/Test_AdjointJacobianMPI.cpp +++ b/pennylane_lightning/core/src/algorithms/tests/mpi/Test_AdjointJacobianMPI.cpp @@ -64,13 +64,13 @@ template void testAdjointJacobian() { DYNAMIC_SECTION("Op=[RX,RX,RX], Obs=[Z,Z,Z] - " << StateVectorMPIToName::name) { - std::vector tp{0, 1, 2}; - const size_t num_qubits = 3; - const size_t num_params = 3; - const size_t num_obs = 3; + std::vector tp{0, 1, 2}; + const std::size_t num_qubits = 3; + const std::size_t num_params = 3; + const std::size_t num_obs = 3; std::vector jacobian(num_obs * tp.size(), 0); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; int nGlobalIndexBits = std::bit_width(static_cast( mpi_manager.getSize())) - @@ -90,11 +90,11 @@ template void testAdjointJacobian() { psi.initSV(); const auto obs1 = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); const auto obs2 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); const auto obs3 = std::make_shared>( - "PauliZ", std::vector{2}); + "PauliZ", std::vector{2}); auto ops = OpsData( {"RX", "RX", "RX"}, {{param[0]}, {param[1]}, {param[2]}}, @@ -115,13 +115,13 @@ template void testAdjointJacobian() { DYNAMIC_SECTION("Op=[RX,RX,RX], Obs=[Z,Z,Z], TParams=[0,2] - " << StateVectorMPIToName::name) { std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector t_params{0, 2}; - const size_t num_qubits = 3; - const size_t num_params = 3; - const size_t num_obs = 3; + std::vector t_params{0, 2}; + const std::size_t num_qubits = 3; + const std::size_t num_params = 3; + const std::size_t num_obs = 3; std::vector jacobian(num_obs * t_params.size(), 0); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; int nGlobalIndexBits = std::bit_width(static_cast( mpi_manager.getSize())) - @@ -141,11 +141,11 @@ template void testAdjointJacobian() { psi.initSV(); const auto obs1 = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); const auto obs2 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); const auto obs3 = std::make_shared>( - "PauliZ", std::vector{2}); + "PauliZ", std::vector{2}); auto ops = OpsData( {"RX", "RX", "RX"}, {{param[0]}, {param[1]}, {param[2]}}, @@ -166,13 +166,13 @@ template void testAdjointJacobian() { DYNAMIC_SECTION("Op=[RX,RX,RX], Obs=[ZZZ] - " << StateVectorMPIToName::name) { std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector tp{0, 1, 2}; - const size_t num_qubits = 3; - const size_t num_params = 3; - const size_t num_obs = 1; + std::vector tp{0, 1, 2}; + const std::size_t num_qubits = 3; + const std::size_t num_params = 3; + const std::size_t num_obs = 1; std::vector jacobian(num_obs * tp.size(), 0); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; int nGlobalIndexBits = std::bit_width(static_cast( mpi_manager.getSize())) - @@ -193,11 +193,11 @@ template void testAdjointJacobian() { const auto obs = std::make_shared>( std::make_shared>( - "PauliZ", std::vector{0}), + "PauliZ", std::vector{0}), std::make_shared>( - "PauliZ", std::vector{1}), + "PauliZ", std::vector{1}), std::make_shared>( - "PauliZ", std::vector{2})); + "PauliZ", std::vector{2})); auto ops = OpsData( {"RX", "RX", "RX"}, {{param[0]}, {param[1]}, {param[2]}}, {{0}, {1}, {2}}, {false, false, false}); @@ -217,13 +217,13 @@ template void testAdjointJacobian() { DYNAMIC_SECTION("Op=Mixed, Obs=[XXX] - " << StateVectorMPIToName::name) { std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector tp{0, 1, 2, 3, 4, 5}; - const size_t num_qubits = 3; - const size_t num_params = 6; - const size_t num_obs = 1; + std::vector tp{0, 1, 2, 3, 4, 5}; + const std::size_t num_qubits = 3; + const std::size_t num_params = 6; + const std::size_t num_obs = 1; std::vector jacobian(num_obs * tp.size(), 0); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; int nGlobalIndexBits = std::bit_width(static_cast( mpi_manager.getSize())) - @@ -244,11 +244,11 @@ template void testAdjointJacobian() { const auto obs = std::make_shared>( std::make_shared>( - "PauliX", std::vector{0}), + "PauliX", std::vector{0}), std::make_shared>( - "PauliX", std::vector{1}), + "PauliX", std::vector{1}), std::make_shared>( - "PauliX", std::vector{2})); + "PauliX", std::vector{2})); std::vector cnot{1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0}; auto ops = OpsData( @@ -265,7 +265,7 @@ template void testAdjointJacobian() { {false, false, false, false, false, false, false, false}, std::vector>{ {}, {}, {}, cnot, {}, {}, {}, {}}, - std::vector>{ + std::vector>{ {}, {}, {}, {}, {}, {}, {}, {}}, std::vector>{{}, {}, {}, {}, {}, {}, {}, {}}); @@ -287,13 +287,13 @@ template void testAdjointJacobian() { DYNAMIC_SECTION("Op=[RX,RX,RX], Obs=Ham[Z0+Z1+Z2], TParams=[0,2] - " << StateVectorMPIToName::name) { std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector t_params{0, 2}; - const size_t num_qubits = 3; - const size_t num_params = 3; - const size_t num_obs = 1; + std::vector t_params{0, 2}; + const std::size_t num_qubits = 3; + const std::size_t num_params = 3; + const std::size_t num_obs = 1; std::vector jacobian(num_obs * t_params.size(), 0); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; int nGlobalIndexBits = std::bit_width(static_cast( mpi_manager.getSize())) - @@ -313,11 +313,11 @@ template void testAdjointJacobian() { psi.initSV(); auto obs1 = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); auto obs2 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto obs3 = std::make_shared>( - "PauliZ", std::vector{2}); + "PauliZ", std::vector{2}); auto ham = HamiltonianMPI::create({0.47, 0.32, 0.96}, {obs1, obs2, obs3}); @@ -338,14 +338,14 @@ template void testAdjointJacobian() { DYNAMIC_SECTION("HermitianObs - " << StateVectorMPIToName::name) { std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector t_params{0, 2}; - const size_t num_qubits = 3; - const size_t num_params = 3; - const size_t num_obs = 1; + std::vector t_params{0, 2}; + const std::size_t num_qubits = 3; + const std::size_t num_params = 3; + const std::size_t num_obs = 1; std::vector jacobian1(num_obs * t_params.size(), 0); std::vector jacobian2(num_obs * t_params.size(), 0); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; int nGlobalIndexBits = std::bit_width(static_cast( mpi_manager.getSize())) - @@ -366,13 +366,13 @@ template void testAdjointJacobian() { auto obs1 = std::make_shared>( std::make_shared>( - "PauliZ", std::vector{0}), + "PauliZ", std::vector{0}), std::make_shared>( - "PauliZ", std::vector{1})); + "PauliZ", std::vector{1})); auto obs2 = std::make_shared>( std::vector{1, 0, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1}, - std::vector{0, 1}); + std::vector{0, 1}); auto ops = OpsData( {"RX", "RX", "RX"}, {{param[0]}, {param[1]}, {param[2]}}, diff --git a/pennylane_lightning/core/src/bindings/Bindings.hpp b/pennylane_lightning/core/src/bindings/Bindings.hpp index 39170e4af2..f5aa305828 100644 --- a/pennylane_lightning/core/src/bindings/Bindings.hpp +++ b/pennylane_lightning/core/src/bindings/Bindings.hpp @@ -126,7 +126,7 @@ auto createStateVectorFromNumpyData( } auto *data_ptr = static_cast(numpyArrayInfo.ptr); return StateVectorT( - {data_ptr, static_cast(numpyArrayInfo.shape[0])}); + {data_ptr, static_cast(numpyArrayInfo.shape[0])}); } /** @@ -149,7 +149,7 @@ auto getNumpyArrayAlignment(const py::array &numpyArray) -> CPUMemoryModel { * @return Numpy array */ template -auto alignedNumpyArray(CPUMemoryModel memory_model, size_t size, +auto alignedNumpyArray(CPUMemoryModel memory_model, std::size_t size, bool zeroInit = false) -> py::array { using Pennylane::Util::alignedAlloc; if (getAlignment(memory_model) > alignof(std::max_align_t)) { @@ -318,7 +318,7 @@ void registerBackendAgnosticObservables(py::module_ &m) { Observable>(m, class_name.c_str(), py::module_local()) .def(py::init( - [](const std::string &name, const std::vector &wires) { + [](const std::string &name, const std::vector &wires) { return NamedObs(name, wires); })) .def("__repr__", &NamedObs::getObsName) @@ -341,7 +341,7 @@ void registerBackendAgnosticObservables(py::module_ &m) { Observable>(m, class_name.c_str(), py::module_local()) .def(py::init( - [](const np_arr_c &matrix, const std::vector &wires) { + [](const np_arr_c &matrix, const std::vector &wires) { auto buffer = matrix.request(); const auto *ptr = static_cast(buffer.ptr); return HermitianObs( @@ -438,7 +438,7 @@ void registerBackendAgnosticMeasurements(PyClass &pyclass) { pyclass .def("probs", [](Measurements &M, - const std::vector &wires) { + const std::vector &wires) { return py::array_t(py::cast(M.probs(wires))); }) .def("probs", @@ -460,17 +460,18 @@ void registerBackendAgnosticMeasurements(PyClass &pyclass) { }, "Variance of an observable object.") .def("generate_samples", [](Measurements &M, - size_t num_wires, size_t num_shots) { + std::size_t num_wires, + std::size_t num_shots) { auto &&result = M.generate_samples(num_shots); - const size_t ndim = 2; - const std::vector shape{num_shots, num_wires}; + const std::size_t ndim = 2; + const std::vector shape{num_shots, num_wires}; constexpr auto sz = sizeof(size_t); - const std::vector strides{sz * num_wires, sz}; + const std::vector strides{sz * num_wires, sz}; // return 2-D NumPy array return py::array(py::buffer_info( result.data(), /* data as contiguous array */ sz, /* size of one scalar */ - py::format_descriptor::format(), /* data type */ + py::format_descriptor::format(), /* data type */ ndim, /* number of dimensions */ shape, /* shape of the matrix */ strides /* strides for each axis */ @@ -486,7 +487,7 @@ auto registerAdjointJacobian( AdjointJacobian &adjoint_jacobian, const StateVectorT &sv, const std::vector>> &observables, const OpsData &operations, - const std::vector &trainableParams) + const std::vector &trainableParams) -> py::array_t { using PrecisionT = typename StateVectorT::PrecisionT; std::vector jac(observables.size() * trainableParams.size(), @@ -530,15 +531,15 @@ void registerBackendAgnosticAlgorithms(py::module_ &m) { py::class_>(m, class_name.c_str(), py::module_local()) .def(py::init &, const std::vector> &, - const std::vector> &, + const std::vector> &, const std::vector &, const std::vector> &>()) .def(py::init &, const std::vector> &, - const std::vector> &, + const std::vector> &, const std::vector &, const std::vector> &, - const std::vector> &, + const std::vector> &, const std::vector> &>()) .def("__repr__", [](const OpsData &ops) { using namespace Pennylane::Util; @@ -568,10 +569,10 @@ void registerBackendAgnosticAlgorithms(py::module_ &m) { function_name.c_str(), [](const std::vector &ops_name, const std::vector> &ops_params, - const std::vector> &ops_wires, + const std::vector> &ops_wires, const std::vector &ops_inverses, const std::vector &ops_matrices, - const std::vector> &ops_controlled_wires, + const std::vector> &ops_controlled_wires, const std::vector> &ops_controlled_values) { std::vector> conv_matrices( ops_matrices.size()); @@ -609,7 +610,7 @@ void registerBackendAgnosticAlgorithms(py::module_ &m) { const std::vector>> &observables, const OpsData &operations, - const std::vector &trainableParams) { + const std::vector &trainableParams) { using PrecisionT = typename StateVectorT::PrecisionT; std::vector jac(observables.size() * trainableParams.size(), diff --git a/pennylane_lightning/core/src/bindings/BindingsBase.hpp b/pennylane_lightning/core/src/bindings/BindingsBase.hpp index 65b3589d21..996917105d 100644 --- a/pennylane_lightning/core/src/bindings/BindingsBase.hpp +++ b/pennylane_lightning/core/src/bindings/BindingsBase.hpp @@ -36,7 +36,7 @@ void registerMatrix( StateVectorT &st, const py::array_t, py::array::c_style | py::array::forcecast> &matrix, - const std::vector &wires, bool inverse = false) { + const std::vector &wires, bool inverse = false) { using ComplexT = typename StateVectorT::ComplexT; st.applyMatrix(static_cast(matrix.request().ptr), wires, inverse); @@ -69,7 +69,7 @@ void registerGatesForStateVector(PyClass &pyclass) { std::string(lookup(Constant::gate_names, gate_op)); const std::string doc = "Apply the " + gate_name + " gate."; auto func = [gate_name = gate_name]( - StateVectorT &sv, const std::vector &wires, + StateVectorT &sv, const std::vector &wires, bool inverse, const std::vector ¶ms) { sv.applyOperation(gate_name, wires, inverse, params); }; diff --git a/pennylane_lightning/core/src/bindings/BindingsMPI.hpp b/pennylane_lightning/core/src/bindings/BindingsMPI.hpp index 1d40fd5107..bad6e9e1da 100644 --- a/pennylane_lightning/core/src/bindings/BindingsMPI.hpp +++ b/pennylane_lightning/core/src/bindings/BindingsMPI.hpp @@ -100,7 +100,7 @@ template void registerObservablesMPI(py::module_ &m) { Observable>(m, class_name.c_str(), py::module_local()) .def(py::init( - [](const std::string &name, const std::vector &wires) { + [](const std::string &name, const std::vector &wires) { return NamedObsMPI(name, wires); })) .def("__repr__", &NamedObsMPI::getObsName) @@ -124,7 +124,7 @@ template void registerObservablesMPI(py::module_ &m) { Observable>(m, class_name.c_str(), py::module_local()) .def(py::init( - [](const np_arr_c &matrix, const std::vector &wires) { + [](const np_arr_c &matrix, const std::vector &wires) { auto buffer = matrix.request(); const auto *ptr = static_cast(buffer.ptr); return HermitianObsMPI( @@ -255,7 +255,7 @@ void registerBackendAgnosticMeasurementsMPI(PyClass &pyclass) { pyclass .def("probs", [](MeasurementsMPI &M, - const std::vector &wires) { + const std::vector &wires) { return py::array_t(py::cast(M.probs(wires))); }) .def("probs", @@ -277,17 +277,18 @@ void registerBackendAgnosticMeasurementsMPI(PyClass &pyclass) { }, "Variance of an observable object.") .def("generate_samples", [](MeasurementsMPI &M, - size_t num_wires, size_t num_shots) { + std::size_t num_wires, + std::size_t num_shots) { auto &&result = M.generate_samples(num_shots); - const size_t ndim = 2; - const std::vector shape{num_shots, num_wires}; + const std::size_t ndim = 2; + const std::vector shape{num_shots, num_wires}; constexpr auto sz = sizeof(size_t); - const std::vector strides{sz * num_wires, sz}; + const std::vector strides{sz * num_wires, sz}; // return 2-D NumPy array return py::array(py::buffer_info( result.data(), /* data as contiguous array */ sz, /* size of one scalar */ - py::format_descriptor::format(), /* data type */ + py::format_descriptor::format(), /* data type */ ndim, /* number of dimensions */ shape, /* shape of the matrix */ strides /* strides for each axis */ @@ -303,7 +304,7 @@ auto registerAdjointJacobianMPI( AdjointJacobianMPI &adjoint_jacobian, const StateVectorT &sv, const std::vector>> &observables, const OpsData &operations, - const std::vector &trainableParams) + const std::vector &trainableParams) -> py::array_t { using PrecisionT = typename StateVectorT::PrecisionT; std::vector jac(observables.size() * trainableParams.size(), @@ -344,7 +345,7 @@ void registerBackendAgnosticAlgorithmsMPI(py::module_ &m) { py::class_>(m, class_name.c_str(), py::module_local()) .def(py::init &, const std::vector> &, - const std::vector> &, + const std::vector> &, const std::vector &, const std::vector> &>()) .def("__repr__", [](const OpsData &ops) { @@ -370,10 +371,10 @@ void registerBackendAgnosticAlgorithmsMPI(py::module_ &m) { function_name.c_str(), [](const std::vector &ops_name, const std::vector> &ops_params, - const std::vector> &ops_wires, + const std::vector> &ops_wires, const std::vector &ops_inverses, const std::vector &ops_matrices, - const std::vector> &ops_controlled_wires, + const std::vector> &ops_controlled_wires, const std::vector> &ops_controlled_values) { std::vector> conv_matrices( ops_matrices.size()); @@ -410,7 +411,7 @@ void registerBackendAgnosticAlgorithmsMPI(py::module_ &m) { const std::vector>> &observables, const OpsData &operations, - const std::vector &trainableParams) { + const std::vector &trainableParams) { using PrecisionT = typename StateVectorT::PrecisionT; std::vector jac(observables.size() * trainableParams.size(), diff --git a/pennylane_lightning/core/src/gates/Constant.hpp b/pennylane_lightning/core/src/gates/Constant.hpp index b84159d80a..8ec99b477b 100644 --- a/pennylane_lightning/core/src/gates/Constant.hpp +++ b/pennylane_lightning/core/src/gates/Constant.hpp @@ -199,7 +199,7 @@ using CMatrixView = /** * @brief Number of wires for gates besides multi-qubit gates */ -using GateNWires = typename std::pair; +using GateNWires = typename std::pair; [[maybe_unused]] constexpr std::array gate_wires = { GateNWires{GateOperation::Identity, 1}, GateNWires{GateOperation::PauliX, 1}, @@ -237,7 +237,7 @@ using GateNWires = typename std::pair; GateNWires{GateOperation::GlobalPhase, 1}, }; -using CGateNWires = typename std::pair; +using CGateNWires = typename std::pair; [[maybe_unused]] constexpr std::array controlled_gate_wires = { CGateNWires{ControlledGateOperation::PauliX, 1}, CGateNWires{ControlledGateOperation::PauliY, 1}, @@ -267,7 +267,7 @@ using CGateNWires = typename std::pair; /** * @brief Number of wires for generators besides multi-qubit gates */ -using GeneratorNWires = typename std::pair; +using GeneratorNWires = typename std::pair; [[maybe_unused]] constexpr std::array generator_wires = { GeneratorNWires{GeneratorOperation::PhaseShift, 1}, GeneratorNWires{GeneratorOperation::RX, 1}, @@ -291,7 +291,7 @@ using GeneratorNWires = typename std::pair; }; using CGeneratorNWires = - typename std::pair; + typename std::pair; [[maybe_unused]] constexpr std::array controlled_generator_wires = { CGeneratorNWires{ControlledGeneratorOperation::PhaseShift, 1}, CGeneratorNWires{ControlledGeneratorOperation::RX, 1}, @@ -313,7 +313,7 @@ using CGeneratorNWires = /** * @brief Number of parameters for gates */ -using GateNParams = typename std::pair; +using GateNParams = typename std::pair; [[maybe_unused]] constexpr std::array gate_num_params = { GateNParams{GateOperation::Identity, 0}, GateNParams{GateOperation::PauliX, 0}, @@ -355,7 +355,7 @@ using GateNParams = typename std::pair; /** * @brief Number of parameters for gates */ -using CGateNParams = typename std::pair; +using CGateNParams = typename std::pair; [[maybe_unused]] constexpr std::array controlled_gate_num_params = { CGateNParams{ControlledGateOperation::PauliX, 0}, CGateNParams{ControlledGateOperation::PauliY, 0}, diff --git a/pennylane_lightning/core/src/measurements/MeasurementsBase.hpp b/pennylane_lightning/core/src/measurements/MeasurementsBase.hpp index ded6192d17..167e8c75ae 100644 --- a/pennylane_lightning/core/src/measurements/MeasurementsBase.hpp +++ b/pennylane_lightning/core/src/measurements/MeasurementsBase.hpp @@ -72,7 +72,7 @@ template class MeasurementsBase { * * @param seed Seed */ - void setSeed(const size_t seed) { rng.seed(seed); } + void setSeed(const std::size_t seed) { rng.seed(seed); } /** * @brief Randomly set the seed of the internal random generator @@ -122,7 +122,8 @@ template class MeasurementsBase { * @return Floating point std::vector with probabilities. * The basis columns are rearranged according to wires. */ - auto probs(const std::vector &wires) -> std::vector { + auto probs(const std::vector &wires) + -> std::vector { return static_cast(this)->probs(wires); }; @@ -133,7 +134,7 @@ template class MeasurementsBase { * @return 1-D vector of samples in binary with each sample * separated by a stride equal to the number of qubits. */ - auto generate_samples(size_t num_samples) -> std::vector { + auto generate_samples(size_t num_samples) -> std::vector { return static_cast(this)->generate_samples(num_samples); }; @@ -147,8 +148,9 @@ template class MeasurementsBase { * * @return Expectation value with respect to the given observable. */ - auto expval(const Observable &obs, const size_t &num_shots, - const std::vector &shot_range = {}) -> PrecisionT { + auto expval(const Observable &obs, + const std::size_t &num_shots, + const std::vector &shot_range = {}) -> PrecisionT { PrecisionT result{0.0}; if (obs.getObsName().find("SparseHamiltonian") != std::string::npos) { @@ -183,17 +185,18 @@ template class MeasurementsBase { * @return Expectation value with respect to the given observable. */ auto measure_with_samples(const Observable &obs, - const size_t &num_shots, - const std::vector &shot_range) + const std::size_t &num_shots, + const std::vector &shot_range) -> std::vector { - const size_t num_qubits = _statevector.getTotalNumQubits(); - std::vector obs_wires; + const std::size_t num_qubits = _statevector.getTotalNumQubits(); + std::vector obs_wires; std::vector> eigenValues; auto sub_samples = _sample_state(obs, num_shots, shot_range, obs_wires, eigenValues); - size_t num_samples = shot_range.empty() ? num_shots : shot_range.size(); + std::size_t num_samples = + shot_range.empty() ? num_shots : shot_range.size(); std::vector obs_samples(num_samples, 0); @@ -204,8 +207,8 @@ template class MeasurementsBase { } for (size_t i = 0; i < num_samples; i++) { - size_t idx = 0; - size_t wire_idx = 0; + std::size_t idx = 0; + std::size_t wire_idx = 0; for (auto &obs_wire : obs_wires) { idx += sub_samples[i * num_qubits + obs_wire] << (obs_wires.size() - 1 - wire_idx); @@ -225,7 +228,7 @@ template class MeasurementsBase { * * @return Variance of the given observable. */ - auto var(const Observable &obs, const size_t &num_shots) + auto var(const Observable &obs, const std::size_t &num_shots) -> PrecisionT { PrecisionT result{0.0}; if (obs.getObsName().find("SparseHamiltonian") != std::string::npos) { @@ -237,7 +240,7 @@ template class MeasurementsBase { auto coeffs = obs.getCoeffs(); auto obs_terms = obs.getObs(); - size_t obs_term_idx = 0; + std::size_t obs_term_idx = 0; for (const auto &coeff : coeffs) { result += coeff * coeff * var(*obs_terms[obs_term_idx], num_shots); @@ -269,12 +272,12 @@ template class MeasurementsBase { * @return Floating point std::vector with probabilities. * The basis columns are rearranged according to wires. */ - auto probs(const Observable &obs, size_t num_shots = 0) + auto probs(const Observable &obs, std::size_t num_shots = 0) -> std::vector { PL_ABORT_IF( obs.getObsName().find("Hamiltonian") != std::string::npos, "Hamiltonian and Sparse Hamiltonian do not support samples()."); - std::vector obs_wires; + std::vector obs_wires; std::vector> eigenvalues; if constexpr (std::is_same_v< typename StateVectorT::MemoryStorageT, @@ -286,7 +289,7 @@ template class MeasurementsBase { sv.updateData(data_storage.data(), data_storage.size()); obs.applyInPlaceShots(sv, eigenvalues, obs_wires); Derived measure(sv); - if (num_shots > size_t{0}) { + if (num_shots > std::size_t{0}) { return measure.probs(obs_wires, num_shots); } return measure.probs(obs_wires); @@ -294,7 +297,7 @@ template class MeasurementsBase { StateVectorT sv(_statevector); obs.applyInPlaceShots(sv, eigenvalues, obs_wires); Derived measure(sv); - if (num_shots > size_t{0}) { + if (num_shots > std::size_t{0}) { return measure.probs(obs_wires, num_shots); } return measure.probs(obs_wires); @@ -310,22 +313,23 @@ template class MeasurementsBase { * * @return Floating point std::vector with probabilities. */ - auto probs(const std::vector &wires, size_t num_shots) + auto probs(const std::vector &wires, std::size_t num_shots) -> std::vector { auto counts_map = counts(num_shots); - size_t num_wires = _statevector.getTotalNumQubits(); + std::size_t num_wires = _statevector.getTotalNumQubits(); std::vector prob_shots(size_t{1} << wires.size(), 0.0); for (auto &it : counts_map) { - size_t bitVal = 0; + std::size_t bitVal = 0; for (size_t bit = 0; bit < wires.size(); bit++) { // Mapping the value of wires[bit]th bit to local [bit]th bit of // the output - bitVal += ((it.first >> (num_wires - size_t{1} - wires[bit])) & - size_t{1}) - << (wires.size() - size_t{1} - bit); + bitVal += + ((it.first >> (num_wires - std::size_t{1} - wires[bit])) & + std::size_t{1}) + << (wires.size() - std::size_t{1} - bit); } prob_shots[bitVal] += @@ -345,7 +349,7 @@ template class MeasurementsBase { auto probs(size_t num_shots) -> std::vector { auto counts_map = counts(num_shots); - size_t num_wires = _statevector.getTotalNumQubits(); + std::size_t num_wires = _statevector.getTotalNumQubits(); std::vector prob_shots(size_t{1} << num_wires, 0.0); @@ -365,13 +369,13 @@ template class MeasurementsBase { * * @return Samples of eigenvalues of the observable */ - auto sample(const Observable &obs, const size_t &num_shots) - -> std::vector { + auto sample(const Observable &obs, + const std::size_t &num_shots) -> std::vector { PL_ABORT_IF( obs.getObsName().find("Hamiltonian") != std::string::npos, "Hamiltonian and Sparse Hamiltonian do not support samples()."); - std::vector obs_wires; - std::vector shot_range = {}; + std::vector obs_wires; + std::vector shot_range = {}; return measure_with_samples(obs, num_shots, shot_range); } @@ -383,7 +387,7 @@ template class MeasurementsBase { * * @return Raw basis state samples */ - auto sample(const size_t &num_shots) -> std::vector { + auto sample(const std::size_t &num_shots) -> std::vector { Derived measure(_statevector); return measure.generate_samples(num_shots); } @@ -395,12 +399,13 @@ template class MeasurementsBase { * @param obs The observable to sample * @param num_shots Number of wires the sampled observable was performed on * - * @return std::unordered_map with format + * @return std::unordered_map with format * ``{'EigenValue': num_occurences}`` */ - auto counts(const Observable &obs, const size_t &num_shots) - -> std::unordered_map { - std::unordered_map outcome_map; + auto counts(const Observable &obs, + const std::size_t &num_shots) + -> std::unordered_map { + std::unordered_map outcome_map; auto sample_data = sample(obs, num_shots); for (size_t i = 0; i < num_shots; i++) { auto key = sample_data[i]; @@ -420,16 +425,17 @@ template class MeasurementsBase { * * @param num_shots Number of wires the sampled observable was performed on * - * @return std::unordered_map with format ``{'outcome': + * @return std::unordered_map with format ``{'outcome': * num_occurences}`` */ - auto counts(const size_t &num_shots) -> std::unordered_map { - std::unordered_map outcome_map; + auto counts(const std::size_t &num_shots) + -> std::unordered_map { + std::unordered_map outcome_map; auto sample_data = sample(num_shots); - size_t num_wires = _statevector.getTotalNumQubits(); + std::size_t num_wires = _statevector.getTotalNumQubits(); for (size_t i = 0; i < num_shots; i++) { - size_t key = 0; + std::size_t key = 0; for (size_t j = 0; j < num_wires; j++) { key += sample_data[i * num_wires + j] << (num_wires - 1 - j); } @@ -455,16 +461,16 @@ template class MeasurementsBase { * @param obs_wires Observable wires. * @param eigenValues eigenvalues of the observable. * - * @return std::vector samples in std::vector + * @return std::vector samples in std::vector */ auto _sample_state(const Observable &obs, - const size_t &num_shots, - const std::vector &shot_range, - std::vector &obs_wires, + const std::size_t &num_shots, + const std::vector &shot_range, + std::vector &obs_wires, std::vector> &eigenValues) - -> std::vector { - const size_t num_qubits = _statevector.getTotalNumQubits(); - std::vector samples; + -> std::vector { + const std::size_t num_qubits = _statevector.getTotalNumQubits(); + std::vector samples; if constexpr (std::is_same_v< typename StateVectorT::MemoryStorageT, Pennylane::Util::MemoryStorageLocation::External>) { @@ -483,9 +489,10 @@ template class MeasurementsBase { } if (!shot_range.empty()) { - std::vector sub_samples(shot_range.size() * num_qubits); + std::vector sub_samples(shot_range.size() * + num_qubits); // Get a slice of samples based on the shot_range vector - size_t shot_idx = 0; + std::size_t shot_idx = 0; for (const auto &i : shot_range) { for (size_t j = i * num_qubits; j < (i + 1) * num_qubits; j++) { // TODO some extra work to make it cache-friendly diff --git a/pennylane_lightning/core/src/measurements/tests/Test_MeasurementsBase.cpp b/pennylane_lightning/core/src/measurements/tests/Test_MeasurementsBase.cpp index a4ca85d556..0f54df968b 100644 --- a/pennylane_lightning/core/src/measurements/tests/Test_MeasurementsBase.cpp +++ b/pennylane_lightning/core/src/measurements/tests/Test_MeasurementsBase.cpp @@ -82,7 +82,8 @@ template void testProbabilities() { using PrecisionT = typename StateVectorT::PrecisionT; // Expected results calculated with Pennylane default.qubit: - std::vector, std::vector>> + std::vector< + std::pair, std::vector>> input = { #if defined(_ENABLE_PLGPU) // Bit index reodering conducted in the python layer @@ -161,7 +162,8 @@ template void testProbabilitiesShots() { using PrecisionT = typename StateVectorT::PrecisionT; // Expected results calculated with Pennylane default.qubit: - std::vector, std::vector>> + std::vector< + std::pair, std::vector>> input = {// prob shots only support in-order target wires for now {{0, 1, 2}, {0.67078706, 0.03062806, 0.0870997, 0.00397696, @@ -186,7 +188,7 @@ template void testProbabilitiesShots() { DYNAMIC_SECTION( "Looping over different wire configurations - shots- fullsystem" << StateVectorToName::name) { - size_t num_shots = 10000; + std::size_t num_shots = 10000; probabilities = Measurer.probs(num_shots); REQUIRE_THAT(input[0].second, Catch::Approx(probabilities).margin(5e-2)); @@ -196,7 +198,7 @@ template void testProbabilitiesShots() { "Looping over different wire configurations - shots- sub system" << StateVectorToName::name) { for (const auto &term : input) { - size_t num_shots = 10000; + std::size_t num_shots = 10000; probabilities = Measurer.probs(term.first, num_shots); REQUIRE_THAT(term.second, Catch::Approx(probabilities).margin(5e-2)); @@ -218,7 +220,7 @@ template void testProbabilitiesObs() { using StateVectorT = typename TypeList::Type; using PrecisionT = typename StateVectorT::PrecisionT; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; // Defining the Statevector that will be measured. auto statevector_data = createNonTrivialState(); @@ -240,7 +242,7 @@ template void testProbabilitiesObs() { Measurements Measurer(sv); auto prob_obs = Measurer_obs.probs(obs); - auto prob = Measurer.probs(std::vector({i})); + auto prob = Measurer.probs(std::vector({i})); REQUIRE_THAT(prob_obs, Catch::Approx(prob).margin(1e-6)); } @@ -258,7 +260,7 @@ template void testProbabilitiesObs() { Measurements Measurer(sv); auto prob_obs = Measurer_obs.probs(obs); - auto prob = Measurer.probs(std::vector({i})); + auto prob = Measurer.probs(std::vector({i})); REQUIRE_THAT(prob_obs, Catch::Approx(prob).margin(1e-6)); } @@ -273,7 +275,7 @@ template void testProbabilitiesObs() { Measurements Measurer(sv); auto prob_obs = Measurer_obs.probs(obs); - auto prob = Measurer.probs(std::vector({i})); + auto prob = Measurer.probs(std::vector({i})); REQUIRE_THAT(prob_obs, Catch::Approx(prob).margin(1e-6)); } @@ -290,7 +292,7 @@ template void testProbabilitiesObs() { Measurements Measurer(sv); auto prob_obs = Measurer_obs.probs(obs); - auto prob = Measurer.probs(std::vector({i})); + auto prob = Measurer.probs(std::vector({i})); REQUIRE_THAT(prob_obs, Catch::Approx(prob).margin(1e-6)); } @@ -305,7 +307,7 @@ template void testProbabilitiesObs() { Measurements Measurer(sv); auto prob_obs = Measurer_obs.probs(obs); - auto prob = Measurer.probs(std::vector({i})); + auto prob = Measurer.probs(std::vector({i})); REQUIRE_THAT(prob_obs, Catch::Approx(prob).margin(1e-6)); } @@ -314,11 +316,11 @@ template void testProbabilitiesObs() { DYNAMIC_SECTION("Test TensorProd XYZ" << StateVectorToName::name) { auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto Z1 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto Y2 = std::make_shared>( - "PauliY", std::vector{2}); + "PauliY", std::vector{2}); auto obs = TensorProdObs::create({X0, Z1, Y2}); Measurements Measurer_obs(statevector); @@ -330,7 +332,7 @@ template void testProbabilitiesObs() { Measurements Measurer(sv); auto prob_obs = Measurer_obs.probs(*obs); - auto prob = Measurer.probs(std::vector({0, 1, 2})); + auto prob = Measurer.probs(std::vector({0, 1, 2})); REQUIRE_THAT(prob_obs, Catch::Approx(prob).margin(1e-6)); } @@ -338,11 +340,11 @@ template void testProbabilitiesObs() { DYNAMIC_SECTION("Test TensorProd YHI" << StateVectorToName::name) { auto Y0 = std::make_shared>( - "PauliY", std::vector{0}); + "PauliY", std::vector{0}); auto H1 = std::make_shared>( - "Hadamard", std::vector{1}); + "Hadamard", std::vector{1}); auto I2 = std::make_shared>( - "Identity", std::vector{2}); + "Identity", std::vector{2}); auto obs = TensorProdObs::create({Y0, H1, I2}); Measurements Measurer_obs(statevector); @@ -355,7 +357,7 @@ template void testProbabilitiesObs() { Measurements Measurer(sv); auto prob_obs = Measurer_obs.probs(*obs); - auto prob = Measurer.probs(std::vector({0, 1, 2})); + auto prob = Measurer.probs(std::vector({0, 1, 2})); REQUIRE_THAT(prob_obs, Catch::Approx(prob).margin(1e-6)); } @@ -386,11 +388,11 @@ template void testProbabilitiesObsShots() { DYNAMIC_SECTION("Test TensorProd XYZ" << StateVectorToName::name) { auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto Z1 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto Y2 = std::make_shared>( - "PauliY", std::vector{2}); + "PauliY", std::vector{2}); auto obs = TensorProdObs::create({X0, Z1, Y2}); Measurements Measurer_obs_shots(statevector); @@ -401,13 +403,13 @@ template void testProbabilitiesObsShots() { Measurements Measurer(sv); - size_t num_shots = 10000; + std::size_t num_shots = 10000; auto prob_obs_shots = Measurer_obs_shots.probs(*obs, num_shots); #ifdef _ENABLE_PLGPU - auto prob = Measurer.probs(std::vector({2, 1, 0})); + auto prob = Measurer.probs(std::vector({2, 1, 0})); #else - auto prob = Measurer.probs(std::vector({0, 1, 2})); + auto prob = Measurer.probs(std::vector({0, 1, 2})); #endif REQUIRE_THAT(prob_obs_shots, Catch::Approx(prob).margin(5e-2)); @@ -416,11 +418,11 @@ template void testProbabilitiesObsShots() { DYNAMIC_SECTION("Test TensorProd YHI" << StateVectorToName::name) { auto Y0 = std::make_shared>( - "PauliY", std::vector{0}); + "PauliY", std::vector{0}); auto H1 = std::make_shared>( - "Hadamard", std::vector{1}); + "Hadamard", std::vector{1}); auto I2 = std::make_shared>( - "Identity", std::vector{2}); + "Identity", std::vector{2}); auto obs = TensorProdObs::create({Y0, H1, I2}); Measurements Measurer_obs_shots(statevector); @@ -432,12 +434,12 @@ template void testProbabilitiesObsShots() { Measurements Measurer(sv); - size_t num_shots = 10000; + std::size_t num_shots = 10000; auto prob_obs_shots = Measurer_obs_shots.probs(*obs, num_shots); #ifdef _ENABLE_PLGPU - auto prob = Measurer.probs(std::vector({2, 1, 0})); + auto prob = Measurer.probs(std::vector({2, 1, 0})); #else - auto prob = Measurer.probs(std::vector({0, 1, 2})); + auto prob = Measurer.probs(std::vector({0, 1, 2})); #endif REQUIRE_THAT(prob_obs_shots, Catch::Approx(prob).margin(5e-2)); @@ -467,7 +469,7 @@ template void testNamedObsExpval() { // This object attaches to the statevector allowing several measures. Measurements Measurer(statevector); - std::vector> wires_list = {{0}, {1}, {2}}; + std::vector> wires_list = {{0}, {1}, {2}}; std::vector obs_name = {"PauliX", "PauliY", "PauliZ"}; // Expected results calculated with Pennylane default.qubit: std::vector> exp_values_ref = { @@ -513,7 +515,7 @@ template void testNamedObsExpvalShot() { // This object attaches to the statevector allowing several measures. Measurements Measurer(statevector); - std::vector> wires_list = {{0}, {1}, {2}}; + std::vector> wires_list = {{0}, {1}, {2}}; std::vector obs_name = {"PauliX", "PauliY", "PauliZ", "Hadamard", "Identity"}; // Expected results calculated with Pennylane default.qubit: @@ -527,8 +529,8 @@ template void testNamedObsExpvalShot() { DYNAMIC_SECTION(obs_name[ind_obs] << " - Varying wires" << StateVectorToName::name) { - size_t num_shots = 20000; - std::vector shots_range = {}; + std::size_t num_shots = 20000; + std::vector shots_range = {}; for (size_t ind_wires = 0; ind_wires < wires_list.size(); ind_wires++) { NamedObs obs(obs_name[ind_obs], @@ -547,8 +549,8 @@ template void testNamedObsExpvalShot() { DYNAMIC_SECTION(obs_name[ind_obs] << " - Varying wires-with shots_range" << StateVectorToName::name) { - size_t num_shots = 20000; - std::vector shots_range; + std::size_t num_shots = 20000; + std::vector shots_range; for (size_t i = 0; i < num_shots; i += 2) { shots_range.push_back(i); } @@ -599,8 +601,8 @@ template void testHermitianObsExpvalShot() { ComplexT{2, -1}, ComplexT{2, 0}}; HermitianObs obs(Hermitian_matrix, {1}); - size_t num_shots = 80000; - std::vector shots_range = {}; + std::size_t num_shots = 80000; + std::vector shots_range = {}; PrecisionT expected = Measurer.expval(obs); @@ -618,8 +620,8 @@ template void testHermitianObsExpvalShot() { ComplexT{0, 1}, ComplexT{0, 0}}; HermitianObs obs(Hermitian_matrix, {1}); - size_t num_shots = 20000; - std::vector shots_range = {}; + std::size_t num_shots = 20000; + std::vector shots_range = {}; PrecisionT expected = Measurer.expval(obs); @@ -637,8 +639,8 @@ template void testHermitianObsExpvalShot() { ComplexT{1, 0}, ComplexT{0, 0}}; HermitianObs obs(Hermitian_matrix, {1}); - size_t num_shots = 20000; - std::vector shots_range = {}; + std::size_t num_shots = 20000; + std::vector shots_range = {}; PrecisionT expected = Measurer.expval(obs); @@ -656,8 +658,8 @@ template void testHermitianObsExpvalShot() { ComplexT{0, 0}, ComplexT{-1, 0}}; HermitianObs obs(Hermitian_matrix, {1}); - size_t num_shots = 20000; - std::vector shots_range = {}; + std::size_t num_shots = 20000; + std::vector shots_range = {}; PrecisionT expected = Measurer.expval(obs); @@ -702,7 +704,7 @@ template void testHermitianObsExpval() { DYNAMIC_SECTION("Varying wires - 2x2 matrix - " << StateVectorToName::name) { - std::vector> wires_list = {{0}, {1}, {2}}; + std::vector> wires_list = {{0}, {1}, {2}}; // Expected results calculated with Pennylane default.qubit: std::vector exp_values_ref = { 0.644217687237691, 0.4794255386042027, 0.29552020666133955}; @@ -722,7 +724,7 @@ template void testHermitianObsExpval() { DYNAMIC_SECTION("Varying wires - 4x4 matrix - " << StateVectorToName::name) { - std::vector> wires_list = { + std::vector> wires_list = { {0, 1}, {0, 2}, {1, 2}, {2, 1}}; // Expected results calculated with Pennylane default.qubit: std::vector exp_values_ref = { @@ -776,12 +778,12 @@ template void testTensorProdObsExpvalShot() { DYNAMIC_SECTION(" - Without shots_range" << StateVectorToName::name) { - size_t num_shots = 20000; - std::vector shots_range = {}; + std::size_t num_shots = 20000; + std::vector shots_range = {}; auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto Z1 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto obs = TensorProdObs::create({X0, Z1}); auto expected = PrecisionT(-0.36); auto result = Measurer.expval(*obs, num_shots, shots_range); @@ -792,12 +794,12 @@ template void testTensorProdObsExpvalShot() { DYNAMIC_SECTION(" - With Identity but no shots_range" << StateVectorToName::name) { - size_t num_shots = 20000; - std::vector shots_range = {}; + std::size_t num_shots = 20000; + std::vector shots_range = {}; auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto I1 = std::make_shared>( - "Identity", std::vector{1}); + "Identity", std::vector{1}); auto obs = TensorProdObs::create({X0, I1}); PrecisionT expected = Measurer.expval(*obs); PrecisionT result = Measurer.expval(*obs, num_shots, shots_range); @@ -808,15 +810,15 @@ template void testTensorProdObsExpvalShot() { DYNAMIC_SECTION(" With shots_range" << StateVectorToName::name) { - size_t num_shots = 20000; - std::vector shots_range; + std::size_t num_shots = 20000; + std::vector shots_range; for (size_t i = 0; i < num_shots; i += 2) { shots_range.push_back(i); } auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto Z1 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto obs = TensorProdObs::create({X0, Z1}); auto expected = PrecisionT(-0.36); auto result = Measurer.expval(*obs, num_shots, shots_range); @@ -827,15 +829,15 @@ template void testTensorProdObsExpvalShot() { DYNAMIC_SECTION(" With Identity and shots_range" << StateVectorToName::name) { - size_t num_shots = 20000; - std::vector shots_range; + std::size_t num_shots = 20000; + std::vector shots_range; for (size_t i = 0; i < num_shots; i += 2) { shots_range.push_back(i); } auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto I1 = std::make_shared>( - "Identity", std::vector{1}); + "Identity", std::vector{1}); auto obs = TensorProdObs::create({X0, I1}); PrecisionT expected = Measurer.expval(*obs); PrecisionT result = Measurer.expval(*obs, num_shots, shots_range); @@ -847,16 +849,16 @@ template void testTensorProdObsExpvalShot() { #ifdef PL_USE_LAPACK DYNAMIC_SECTION(" With Identity and shots_range" << StateVectorToName::name) { - size_t num_shots = 80000; - std::vector shots_range = {}; + std::size_t num_shots = 80000; + std::vector shots_range = {}; auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); std::vector Hermitian_matrix1{ ComplexT{3, 0}, ComplexT{2, 1}, ComplexT{2, -1}, ComplexT{-3, 0}}; auto H1 = std::make_shared>( - Hermitian_matrix1, std::vector{1}); + Hermitian_matrix1, std::vector{1}); auto obs = TensorProdObs::create({X0, H1}); PrecisionT expected = Measurer.expval(*obs); @@ -891,7 +893,7 @@ template void testNamedObsVar() { // This object attaches to the statevector allowing several measures. Measurements Measurer(statevector); - std::vector> wires_list = {{0}, {1}, {2}}; + std::vector> wires_list = {{0}, {1}, {2}}; std::vector obs_name = {"PauliX", "PauliY", "PauliZ"}; // Expected results calculated with Pennylane default.qubit: std::vector> exp_values_ref = { @@ -921,7 +923,7 @@ template void testNamedObsVar() { NamedObs obs(obs_name[ind_obs], wires_list[ind_wires]); PrecisionT expected = exp_values_ref[ind_obs][ind_wires]; - size_t num_shots = 20000; + std::size_t num_shots = 20000; PrecisionT result = Measurer.var(obs, num_shots); REQUIRE_THAT(result, @@ -962,7 +964,7 @@ template void testHermitianObsVar() { DYNAMIC_SECTION("Varying wires - 2x2 matrix - " << StateVectorToName::name) { - std::vector> wires_list = {{0}, {1}, {2}}; + std::vector> wires_list = {{0}, {1}, {2}}; // Expected results calculated with Pennylane default.qubit: std::vector exp_values_ref = { 0.5849835714501204, 0.7701511529340699, 0.9126678074548389}; @@ -982,7 +984,7 @@ template void testHermitianObsVar() { DYNAMIC_SECTION("Varying wires - 4x4 matrix - " << StateVectorToName::name) { - std::vector> wires_list = { + std::vector> wires_list = { {0, 1}, {0, 2}, {1, 2}}; // Expected results calculated with Pennylane default.qubit: std::vector exp_values_ref = { @@ -1039,7 +1041,7 @@ template void testHermitianObsShotVar() { DYNAMIC_SECTION("Varying wires - 2x2 matrix - " << StateVectorToName::name) { - std::vector> wires_list = {{0}, {1}, {2}}; + std::vector> wires_list = {{0}, {1}, {2}}; // Expected results calculated with Pennylane default.qubit: std::vector exp_values_ref = { 0.5849835714501204, 0.7701511529340699, 0.9126678074548389}; @@ -1052,7 +1054,7 @@ template void testHermitianObsShotVar() { HermitianObs obs(Hermitian_matrix, wires_list[ind_wires]); PrecisionT expected = exp_values_ref[ind_wires]; - size_t num_shots = 20000; + std::size_t num_shots = 20000; PrecisionT result = Measurer.var(obs, num_shots); REQUIRE_THAT(result, @@ -1063,7 +1065,7 @@ template void testHermitianObsShotVar() { DYNAMIC_SECTION("Varying wires - 4x4 matrix - " << StateVectorToName::name) { - std::vector> wires_list = { + std::vector> wires_list = { {0, 1}, {0, 2}, {1, 2}}; // Expected results calculated with Pennylane default.qubit: std::vector exp_values_ref = { @@ -1082,7 +1084,7 @@ template void testHermitianObsShotVar() { HermitianObs obs(Hermitian_matrix, wires_list[ind_wires]); - size_t num_shots = 20000; + std::size_t num_shots = 20000; PrecisionT expected = exp_values_ref[ind_wires]; PrecisionT result = Measurer.var(obs, num_shots); @@ -1122,11 +1124,11 @@ template void testTensorProdObsVarShot() { DYNAMIC_SECTION(" Without Identity" << StateVectorToName::name) { - size_t num_shots = 20000; + std::size_t num_shots = 20000; auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto Z1 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto obs = TensorProdObs::create({X0, Z1}); auto expected = Measurer.var(*obs); auto result = Measurer.var(*obs, num_shots); @@ -1137,13 +1139,13 @@ template void testTensorProdObsVarShot() { DYNAMIC_SECTION(" full wires" << StateVectorToName::name) { - size_t num_shots = 20000; + std::size_t num_shots = 20000; auto X2 = std::make_shared>( - "PauliX", std::vector{2}); + "PauliX", std::vector{2}); auto Y1 = std::make_shared>( - "PauliY", std::vector{1}); + "PauliY", std::vector{1}); auto Z0 = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); auto obs = TensorProdObs::create({X2, Y1, Z0}); auto expected = Measurer.var(*obs); auto result = Measurer.var(*obs, num_shots); @@ -1156,7 +1158,7 @@ template void testTensorProdObsVarShot() { DYNAMIC_SECTION("With Hermitian and NameObs" << StateVectorToName::name) { using MatrixT = std::vector; - size_t num_shots = 20000; + std::size_t num_shots = 20000; const PrecisionT theta = M_PI / 2; const PrecisionT real_term = std::cos(theta); const PrecisionT imag_term = std::sin(theta); @@ -1170,10 +1172,10 @@ template void testTensorProdObsVarShot() { Hermitian_matrix[15] = ComplexT{1.0, 0}; auto Her = std::make_shared>( - Hermitian_matrix, std::vector{0, 2}); + Hermitian_matrix, std::vector{0, 2}); auto Y1 = std::make_shared>( - "PauliY", std::vector{1}); + "PauliY", std::vector{1}); auto obs = TensorProdObs::create({Her, Y1}); auto expected = Measurer.var(*obs); @@ -1186,14 +1188,14 @@ template void testTensorProdObsVarShot() { DYNAMIC_SECTION("With Hermitian and NameObs" << StateVectorToName::name) { using MatrixT = std::vector; - size_t num_shots = 20000; + std::size_t num_shots = 20000; MatrixT Hermitian_matrix(4, {0, 0}); Hermitian_matrix[0] = 1; Hermitian_matrix[3] = -1; auto Her = std::make_shared>( - Hermitian_matrix, std::vector{1}); + Hermitian_matrix, std::vector{1}); auto obs = TensorProdObs::create({Her}); auto expected = Measurer.var(*obs); @@ -1206,13 +1208,13 @@ template void testTensorProdObsVarShot() { DYNAMIC_SECTION(" full wires with apply operations" << StateVectorToName::name) { - size_t num_shots = 20000; + std::size_t num_shots = 20000; auto X2 = std::make_shared>( - "PauliX", std::vector{2}); + "PauliX", std::vector{2}); auto Y1 = std::make_shared>( - "PauliY", std::vector{1}); + "PauliY", std::vector{1}); auto Z0 = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); auto obs = TensorProdObs::create({X2, Y1, Z0}); statevector.applyOperations({"Hadamard", "PauliZ", "S", "Hadamard"}, @@ -1230,11 +1232,11 @@ template void testTensorProdObsVarShot() { DYNAMIC_SECTION(" With Identity" << StateVectorToName::name) { - size_t num_shots = 20000; + std::size_t num_shots = 20000; auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto I1 = std::make_shared>( - "Identity", std::vector{1}); + "Identity", std::vector{1}); auto obs = TensorProdObs::create({X0, I1}); PrecisionT expected = Measurer.var(*obs); PrecisionT result = Measurer.var(*obs, num_shots); @@ -1257,7 +1259,7 @@ template void testSamples() { using StateVectorT = typename TypeList::Type; using PrecisionT = typename StateVectorT::PrecisionT; - constexpr size_t twos[] = { + constexpr std::size_t twos[] = { 1U << 0U, 1U << 1U, 1U << 2U, 1U << 3U, 1U << 4U, 1U << 5U, 1U << 6U, 1U << 7U, 1U << 8U, 1U << 9U, 1U << 10U, 1U << 11U, 1U << 12U, 1U << 13U, 1U << 14U, 1U << 15U, 1U << 16U, 1U << 17U, @@ -1279,13 +1281,13 @@ template void testSamples() { 0.67078706, 0.03062806, 0.0870997, 0.00397696, 0.17564072, 0.00801973, 0.02280642, 0.00104134}; - size_t num_qubits = 3; - size_t N = std::pow(2, num_qubits); - size_t num_samples = 100000; + std::size_t num_qubits = 3; + std::size_t N = std::pow(2, num_qubits); + std::size_t num_samples = 100000; auto &&samples = Measurer.generate_samples(num_samples); - std::vector counts(N, 0); - std::vector samples_decimal(num_samples, 0); + std::vector counts(N, 0); + std::vector samples_decimal(num_samples, 0); // convert samples to decimal and then bin them in counts for (size_t i = 0; i < num_samples; i++) { @@ -1328,7 +1330,7 @@ template void testSamplesCountsObs() { StateVectorT statevector(statevector_data.data(), statevector_data.size()); - constexpr size_t twos[] = { + constexpr std::size_t twos[] = { 1U << 0U, 1U << 1U, 1U << 2U, 1U << 3U, 1U << 4U, 1U << 5U, 1U << 6U, 1U << 7U, 1U << 8U, 1U << 9U, 1U << 10U, 1U << 11U, 1U << 12U, 1U << 13U, 1U << 14U, 1U << 15U, 1U << 16U, 1U << 17U, @@ -1340,7 +1342,7 @@ template void testSamplesCountsObs() { // This object attaches to the statevector allowing several measures. Measurements Measurer(statevector); - std::vector> wires_list = {{0}, {1}, {2}}; + std::vector> wires_list = {{0}, {1}, {2}}; std::vector obs_name = {"PauliX", "PauliY", "PauliZ", "Hadamard", "Identity"}; // Expected results calculated with Pennylane default.qubit: @@ -1354,7 +1356,7 @@ template void testSamplesCountsObs() { DYNAMIC_SECTION(obs_name[ind_obs] << " Sample Obs - Varying wires" << StateVectorToName::name) { - size_t num_shots = 20000; + std::size_t num_shots = 20000; for (size_t ind_wires = 0; ind_wires < wires_list.size(); ind_wires++) { NamedObs obs(obs_name[ind_obs], @@ -1377,7 +1379,7 @@ template void testSamplesCountsObs() { DYNAMIC_SECTION(obs_name[ind_obs] << " Counts Obs - Varying wires" << StateVectorToName::name) { - size_t num_shots = 20000; + std::size_t num_shots = 20000; for (size_t ind_wires = 0; ind_wires < wires_list.size(); ind_wires++) { NamedObs obs(obs_name[ind_obs], @@ -1404,13 +1406,13 @@ template void testSamplesCountsObs() { 0.67078706, 0.03062806, 0.0870997, 0.00397696, 0.17564072, 0.00801973, 0.02280642, 0.00104134}; - size_t num_qubits = 3; - size_t N = std::pow(2, num_qubits); - size_t num_samples = 100000; + std::size_t num_qubits = 3; + std::size_t N = std::pow(2, num_qubits); + std::size_t num_samples = 100000; auto &&samples = Measurer.sample(num_samples); - std::vector counts(N, 0); - std::vector samples_decimal(num_samples, 0); + std::vector counts(N, 0); + std::vector samples_decimal(num_samples, 0); // convert samples to decimal and then bin them in counts for (size_t i = 0; i < num_samples; i++) { @@ -1441,13 +1443,13 @@ template void testSamplesCountsObs() { 0.67078706, 0.03062806, 0.0870997, 0.00397696, 0.17564072, 0.00801973, 0.02280642, 0.00104134}; - size_t num_qubits = 3; - size_t N = std::pow(2, num_qubits); - size_t num_samples = 100000; + std::size_t num_qubits = 3; + std::size_t N = std::pow(2, num_qubits); + std::size_t num_samples = 100000; auto &&counts_sample = Measurer.counts(num_samples); - std::vector counts(N, 0); + std::vector counts(N, 0); // convert samples to decimal and then bin them in counts for (auto &it : counts_sample) { @@ -1501,16 +1503,16 @@ template void testHamiltonianObsExpvalShot() { Measurements Measurer(statevector); auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto Z1 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto ob = Hamiltonian::create({0.3, 0.5}, {X0, Z1}); DYNAMIC_SECTION("Without shots_range " << StateVectorToName::name) { - size_t num_shots = 20000; - std::vector shots_range = {}; + std::size_t num_shots = 20000; + std::vector shots_range = {}; auto res = Measurer.expval(*ob, num_shots, shots_range); auto expected = PrecisionT(-0.086); @@ -1521,8 +1523,8 @@ template void testHamiltonianObsExpvalShot() { DYNAMIC_SECTION("With shots_range " << StateVectorToName::name) { - size_t num_shots = 20000; - std::vector shots_range; + std::size_t num_shots = 20000; + std::vector shots_range; for (size_t i = 0; i < num_shots; i += 2) { shots_range.push_back(i); } @@ -1537,15 +1539,15 @@ template void testHamiltonianObsExpvalShot() { DYNAMIC_SECTION("TensorProd with shots_range " << StateVectorToName::name) { auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto Z1 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto obs0 = TensorProdObs::create({X0, Z1}); auto Y0 = std::make_shared>( - "PauliY", std::vector{0}); + "PauliY", std::vector{0}); auto H1 = std::make_shared>( - "Hadamard", std::vector{1}); + "Hadamard", std::vector{1}); auto obs1 = TensorProdObs::create({Y0, H1}); auto obs = @@ -1554,7 +1556,7 @@ template void testHamiltonianObsExpvalShot() { Measurements Measurer_analytic(sv); auto expected = Measurer_analytic.expval(*obs); - size_t num_shots = 20000; + std::size_t num_shots = 20000; auto res = Measurer.expval(*obs, num_shots, {}); REQUIRE_THAT(res, Catch::Matchers::WithinRel( @@ -1563,17 +1565,17 @@ template void testHamiltonianObsExpvalShot() { #ifdef PL_USE_LAPACK DYNAMIC_SECTION("YHer" << StateVectorToName::name) { auto Y0 = std::make_shared>( - "PauliY", std::vector{0}); + "PauliY", std::vector{0}); std::vector Hermitian_mat{ComplexT{3, 0}, ComplexT{2, 1}, ComplexT{2, -1}, ComplexT{-3, 0}}; auto Her1 = std::make_shared>( - Hermitian_mat, std::vector{1}); + Hermitian_mat, std::vector{1}); auto ob = Hamiltonian::create({0.5, 0.5}, {Y0, Her1}); - size_t num_shots = 100000; + std::size_t num_shots = 100000; auto res = Measurer.expval(*ob, num_shots, {}); auto expected = Measurer.expval(*ob); @@ -1609,13 +1611,13 @@ template void testHamiltonianObsVarShot() { DYNAMIC_SECTION("YZ" << StateVectorToName::name) { auto Y0 = std::make_shared>( - "PauliY", std::vector{0}); + "PauliY", std::vector{0}); auto Z1 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto ob = Hamiltonian::create({0.5, 0.5}, {Y0, Z1}); - size_t num_shots = 20000; + std::size_t num_shots = 20000; auto res = Measurer.var(*ob, num_shots); auto expected = Measurer.var(*ob); @@ -1626,13 +1628,13 @@ template void testHamiltonianObsVarShot() { DYNAMIC_SECTION("YI" << StateVectorToName::name) { auto Y0 = std::make_shared>( - "PauliY", std::vector{0}); + "PauliY", std::vector{0}); auto I1 = std::make_shared>( - "Identity", std::vector{1}); + "Identity", std::vector{1}); auto ob = Hamiltonian::create({0.5, 0.5}, {Y0, I1}); - size_t num_shots = 20000; + std::size_t num_shots = 20000; auto res = Measurer.var(*ob, num_shots); auto expected = Measurer.var(*ob); @@ -1645,24 +1647,24 @@ template void testHamiltonianObsVarShot() { DYNAMIC_SECTION("YHer" << StateVectorToName::name) { using ComplexT = typename StateVectorT::ComplexT; auto Y0 = std::make_shared>( - "PauliY", std::vector{0}); + "PauliY", std::vector{0}); std::vector Hermitian_mat1{ComplexT{3, 0}, ComplexT{2, 1}, ComplexT{2, -1}, ComplexT{-3, 0}}; auto Her1 = std::make_shared>( - Hermitian_mat1, std::vector{1}); + Hermitian_mat1, std::vector{1}); std::vector Hermitian_mat2{ComplexT{2, 0}, ComplexT{1, 1}, ComplexT{1, -1}, ComplexT{-6, 0}}; auto Her2 = std::make_shared>( - Hermitian_mat2, std::vector{2}); + Hermitian_mat2, std::vector{2}); auto ob = Hamiltonian::create({0.5, 0.5, 1.0}, {Y0, Her1, Her2}); - size_t num_shots = 20000; + std::size_t num_shots = 20000; auto res = Measurer.var(*ob, num_shots); auto expected = Measurer.var(*ob); @@ -1704,8 +1706,8 @@ template void testSparseHObsMeasureShot() { DYNAMIC_SECTION("Failed for expval " << StateVectorToName::name) { - size_t num_shots = 1000; - std::vector shots_range = {}; + std::size_t num_shots = 1000; + std::vector shots_range = {}; REQUIRE_THROWS_WITH( Measurer.expval(*sparseH, num_shots, shots_range), Catch::Matchers::Contains("SparseHamiltonian observables do " @@ -1714,8 +1716,8 @@ template void testSparseHObsMeasureShot() { DYNAMIC_SECTION("Failed for var " << StateVectorToName::name) { - size_t num_shots = 1000; - std::vector shots_range = {}; + std::size_t num_shots = 1000; + std::vector shots_range = {}; REQUIRE_THROWS_WITH( Measurer.var(*sparseH, num_shots), Catch::Matchers::Contains("SparseHamiltonian observables do " diff --git a/pennylane_lightning/core/src/measurements/tests/mpi/Test_MeasurementsBaseMPI.cpp b/pennylane_lightning/core/src/measurements/tests/mpi/Test_MeasurementsBaseMPI.cpp index af3660d338..7c834201af 100644 --- a/pennylane_lightning/core/src/measurements/tests/mpi/Test_MeasurementsBaseMPI.cpp +++ b/pennylane_lightning/core/src/measurements/tests/mpi/Test_MeasurementsBaseMPI.cpp @@ -53,7 +53,8 @@ template void testProbabilities() { using PrecisionT = typename StateVectorT::PrecisionT; // Expected results calculated with Pennylane default.qubit: - std::vector, std::vector>> + std::vector< + std::pair, std::vector>> input = {// Bit index reodering conducted in the python layer // for L-GPU. Also L-GPU backend doesn't support // out of order wires for probability calculation @@ -65,16 +66,16 @@ template void testProbabilities() { auto statevector_data = createNonTrivialState>(); - size_t num_qubits = 3; + std::size_t num_qubits = 3; MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -110,7 +111,7 @@ template void testProbabilities() { DYNAMIC_SECTION("Looping over different wire configurations - shots" << StateVectorMPIToName::name) { - size_t num_shots = 1000; + std::size_t num_shots = 1000; auto probabilities = Measurer.probs(num_shots); REQUIRE_THAT(expected_prob, Catch::Approx(probabilities).margin(5e-2)); @@ -119,11 +120,11 @@ template void testProbabilities() { DYNAMIC_SECTION( "Looping over different wire configurations - shots- sub system" << StateVectorMPIToName::name) { - std::vector wires = {0, 1, 2}; + std::vector wires = {0, 1, 2}; std::vector expected_probs = { 0.67078706, 0.03062806, 0.0870997, 0.00397696, 0.17564072, 0.00801973, 0.02280642, 0.00104134}; - size_t num_shots = 10000; + std::size_t num_shots = 10000; auto probabilities = Measurer.probs(wires, num_shots); REQUIRE_THAT(expected_probs, @@ -145,7 +146,7 @@ template void testProbabilitiesObs() { using StateVectorT = typename TypeList::Type; using PrecisionT = typename StateVectorT::PrecisionT; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; // Defining the Statevector that will be measured. auto statevector_data = @@ -154,11 +155,11 @@ template void testProbabilitiesObs() { MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -193,7 +194,7 @@ template void testProbabilitiesObs() { MeasurementsMPI Measurer(sv); auto prob_obs = Measurer_obs.probs(obs); - auto prob = Measurer.probs(std::vector({i})); + auto prob = Measurer.probs(std::vector({i})); REQUIRE_THAT(prob_obs, Catch::Approx(prob).margin(1e-6)); } @@ -211,7 +212,7 @@ template void testProbabilitiesObs() { MeasurementsMPI Measurer(sv); auto prob_obs = Measurer_obs.probs(obs); - auto prob = Measurer.probs(std::vector({i})); + auto prob = Measurer.probs(std::vector({i})); REQUIRE_THAT(prob_obs, Catch::Approx(prob).margin(1e-6)); } @@ -226,7 +227,7 @@ template void testProbabilitiesObs() { MeasurementsMPI Measurer(sv); auto prob_obs = Measurer_obs.probs(obs); - auto prob = Measurer.probs(std::vector({i})); + auto prob = Measurer.probs(std::vector({i})); REQUIRE_THAT(prob_obs, Catch::Approx(prob).margin(1e-6)); } @@ -243,7 +244,7 @@ template void testProbabilitiesObs() { MeasurementsMPI Measurer(sv); auto prob_obs = Measurer_obs.probs(obs); - auto prob = Measurer.probs(std::vector({i})); + auto prob = Measurer.probs(std::vector({i})); REQUIRE_THAT(prob_obs, Catch::Approx(prob).margin(1e-6)); } @@ -258,7 +259,7 @@ template void testProbabilitiesObs() { MeasurementsMPI Measurer(sv); auto prob_obs = Measurer_obs.probs(obs); - auto prob = Measurer.probs(std::vector({i})); + auto prob = Measurer.probs(std::vector({i})); REQUIRE_THAT(prob_obs, Catch::Approx(prob).margin(1e-6)); } @@ -267,11 +268,11 @@ template void testProbabilitiesObs() { DYNAMIC_SECTION("Test TensorProd XYZ" << StateVectorMPIToName::name) { auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto Z1 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto Y2 = std::make_shared>( - "PauliY", std::vector{2}); + "PauliY", std::vector{2}); auto obs = TensorProdObsMPI::create({X0, Z1, Y2}); MeasurementsMPI Measurer_obs(statevector); @@ -283,7 +284,7 @@ template void testProbabilitiesObs() { MeasurementsMPI Measurer(sv); auto prob_obs = Measurer_obs.probs(*obs); - auto prob = Measurer.probs(std::vector({0, 1, 2})); + auto prob = Measurer.probs(std::vector({0, 1, 2})); REQUIRE_THAT(prob_obs, Catch::Approx(prob).margin(1e-6)); } @@ -291,11 +292,11 @@ template void testProbabilitiesObs() { DYNAMIC_SECTION("Test TensorProd YHI" << StateVectorMPIToName::name) { auto Y0 = std::make_shared>( - "PauliY", std::vector{0}); + "PauliY", std::vector{0}); auto H1 = std::make_shared>( - "Hadamard", std::vector{1}); + "Hadamard", std::vector{1}); auto I2 = std::make_shared>( - "Identity", std::vector{2}); + "Identity", std::vector{2}); auto obs = TensorProdObsMPI::create({Y0, H1, I2}); MeasurementsMPI Measurer_obs(statevector); @@ -308,7 +309,7 @@ template void testProbabilitiesObs() { MeasurementsMPI Measurer(sv); auto prob_obs = Measurer_obs.probs(*obs); - auto prob = Measurer.probs(std::vector({0, 1, 2})); + auto prob = Measurer.probs(std::vector({0, 1, 2})); REQUIRE_THAT(prob_obs, Catch::Approx(prob).margin(1e-6)); } @@ -328,7 +329,7 @@ template void testProbabilitiesObsShots() { using StateVectorT = typename TypeList::Type; using PrecisionT = typename StateVectorT::PrecisionT; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; // Defining the Statevector that will be measured. auto statevector_data = @@ -337,11 +338,11 @@ template void testProbabilitiesObsShots() { MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -368,11 +369,11 @@ template void testProbabilitiesObsShots() { DYNAMIC_SECTION("Test TensorProd XYZ" << StateVectorMPIToName::name) { auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto Z1 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto Y2 = std::make_shared>( - "PauliY", std::vector{2}); + "PauliY", std::vector{2}); auto obs = TensorProdObsMPI::create({X0, Z1, Y2}); MeasurementsMPI Measurer_obs_shots(statevector); @@ -383,9 +384,9 @@ template void testProbabilitiesObsShots() { MeasurementsMPI Measurer(sv); - size_t num_shots = 10000; + std::size_t num_shots = 10000; auto prob_obs_shots = Measurer_obs_shots.probs(*obs, num_shots); - auto prob = Measurer.probs(std::vector({2, 1, 0})); + auto prob = Measurer.probs(std::vector({2, 1, 0})); auto prob_all = mpi_manager.allgather(prob); REQUIRE_THAT(prob_obs_shots, Catch::Approx(prob_all).margin(5e-2)); } @@ -393,11 +394,11 @@ template void testProbabilitiesObsShots() { DYNAMIC_SECTION("Test TensorProd YHI" << StateVectorMPIToName::name) { auto Y0 = std::make_shared>( - "PauliY", std::vector{0}); + "PauliY", std::vector{0}); auto H1 = std::make_shared>( - "Hadamard", std::vector{1}); + "Hadamard", std::vector{1}); auto I2 = std::make_shared>( - "Identity", std::vector{2}); + "Identity", std::vector{2}); auto obs = TensorProdObsMPI::create({Y0, H1, I2}); MeasurementsMPI Measurer_obs_shots(statevector); @@ -409,9 +410,9 @@ template void testProbabilitiesObsShots() { MeasurementsMPI Measurer(sv); - size_t num_shots = 10000; + std::size_t num_shots = 10000; auto prob_obs_shots = Measurer_obs_shots.probs(*obs, num_shots); - auto prob = Measurer.probs(std::vector({2, 1, 0})); + auto prob = Measurer.probs(std::vector({2, 1, 0})); auto prob_all = mpi_manager.allgather(prob); REQUIRE_THAT(prob_obs_shots, Catch::Approx(prob_all).margin(5e-2)); } @@ -435,16 +436,16 @@ template void testNamedObsExpval() { auto statevector_data = createNonTrivialState>(); - size_t num_qubits = 3; + std::size_t num_qubits = 3; MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -464,7 +465,7 @@ template void testNamedObsExpval() { // This object attaches to the statevector allowing several measures. MeasurementsMPI Measurer(sv); - std::vector> wires_list = {{0}, {1}, {2}}; + std::vector> wires_list = {{0}, {1}, {2}}; std::vector obs_name = {"PauliX", "PauliY", "PauliZ"}; // Expected results calculated with Pennylane default.qubit: std::vector> exp_values_ref = { @@ -505,16 +506,16 @@ template void testNamedObsExpvalShot() { auto statevector_data = createNonTrivialState>(); - size_t num_qubits = 3; + std::size_t num_qubits = 3; MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -534,7 +535,7 @@ template void testNamedObsExpvalShot() { // This object attaches to the statevector allowing several measures. MeasurementsMPI Measurer(sv); - std::vector> wires_list = {{0}, {1}, {2}}; + std::vector> wires_list = {{0}, {1}, {2}}; std::vector obs_name = {"PauliX", "PauliY", "PauliZ", "Hadamard"}; // Expected results calculated with Pennylane default.qubit: @@ -544,8 +545,8 @@ template void testNamedObsExpvalShot() { {0.58498357, 0.77015115, 0.91266780}, {0.7620549436, 0.8420840225, 0.8449848566}}; - size_t num_shots = 10000; - std::vector shots_range = {}; + std::size_t num_shots = 10000; + std::vector shots_range = {}; for (size_t ind_obs = 0; ind_obs < obs_name.size(); ind_obs++) { DYNAMIC_SECTION(obs_name[ind_obs] @@ -583,16 +584,16 @@ template void testHermitianObsExpval() { auto statevector_data = createNonTrivialState>(); - size_t num_qubits = 3; + std::size_t num_qubits = 3; MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -619,7 +620,7 @@ template void testHermitianObsExpval() { DYNAMIC_SECTION("Varying wires - 2x2 matrix - " << StateVectorMPIToName::name) { - std::vector> wires_list = {{0}, {1}, {2}}; + std::vector> wires_list = {{0}, {1}, {2}}; // Expected results calculated with Pennylane default.qubit: std::vector exp_values_ref = { 0.644217687237691, 0.4794255386042027, 0.29552020666133955}; @@ -639,7 +640,7 @@ template void testHermitianObsExpval() { DYNAMIC_SECTION("Varying wires - 4x4 matrix - " << StateVectorMPIToName::name) { - std::vector> wires_list = { + std::vector> wires_list = { {0, 1}, {0, 2}, {1, 2}, {2, 1}}; // Expected results calculated with Pennylane default.qubit: std::vector exp_values_ref = { @@ -685,16 +686,16 @@ template void testTensorProdObsExpvalShot() { {0.0, 0.0}, {0.0, 0.1}, {0.1, 0.1}, {0.1, 0.2}, {0.2, 0.2}, {0.3, 0.3}, {0.3, 0.4}, {0.4, 0.5}}; - size_t num_qubits = 3; + std::size_t num_qubits = 3; MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -716,12 +717,12 @@ template void testTensorProdObsExpvalShot() { DYNAMIC_SECTION(" - Without shots_range" << StateVectorMPIToName::name) { - size_t num_shots = 10000; - std::vector shots_range = {}; + std::size_t num_shots = 10000; + std::vector shots_range = {}; auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto Z1 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto obs = TensorProdObsMPI::create({X0, Z1}); auto expected = PrecisionT(-0.36); auto result = Measurer.expval(*obs, num_shots, shots_range); @@ -730,12 +731,12 @@ template void testTensorProdObsExpvalShot() { DYNAMIC_SECTION(" - With Identity but no shots_range" << StateVectorMPIToName::name) { - size_t num_shots = 10000; - std::vector shots_range = {}; + std::size_t num_shots = 10000; + std::vector shots_range = {}; auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto I1 = std::make_shared>( - "Identity", std::vector{1}); + "Identity", std::vector{1}); auto obs = TensorProdObsMPI::create({X0, I1}); PrecisionT expected = Measurer.expval(*obs); PrecisionT result = Measurer.expval(*obs, num_shots, shots_range); @@ -744,15 +745,15 @@ template void testTensorProdObsExpvalShot() { DYNAMIC_SECTION(" With shots_range" << StateVectorMPIToName::name) { - size_t num_shots = 10000; - std::vector shots_range; + std::size_t num_shots = 10000; + std::vector shots_range; for (size_t i = 0; i < num_shots; i += 2) { shots_range.push_back(i); } auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto Z1 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto obs = TensorProdObsMPI::create({X0, Z1}); auto expected = PrecisionT(-0.36); auto result = Measurer.expval(*obs, num_shots, shots_range); @@ -761,15 +762,15 @@ template void testTensorProdObsExpvalShot() { DYNAMIC_SECTION(" With Identity and shots_range" << StateVectorMPIToName::name) { - size_t num_shots = 10000; - std::vector shots_range; + std::size_t num_shots = 10000; + std::vector shots_range; for (size_t i = 0; i < num_shots; i += 2) { shots_range.push_back(i); } auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto I1 = std::make_shared>( - "Identity", std::vector{1}); + "Identity", std::vector{1}); auto obs = TensorProdObsMPI::create({X0, I1}); PrecisionT expected = Measurer.expval(*obs); PrecisionT result = Measurer.expval(*obs, num_shots, shots_range); @@ -797,16 +798,16 @@ template void testHamiltonianObsExpvalShot() { {0.0, 0.0}, {0.0, 0.1}, {0.1, 0.1}, {0.1, 0.2}, {0.2, 0.2}, {0.3, 0.3}, {0.3, 0.4}, {0.4, 0.5}}; - size_t num_qubits = 3; + std::size_t num_qubits = 3; MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -828,12 +829,12 @@ template void testHamiltonianObsExpvalShot() { DYNAMIC_SECTION(" - Without shots_range" << StateVectorMPIToName::name) { - size_t num_shots = 10000; - std::vector shots_range = {}; + std::size_t num_shots = 10000; + std::vector shots_range = {}; auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto Z1 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto obs = HamiltonianMPI::create({0.3, 0.5}, {X0, Z1}); PrecisionT expected = PrecisionT(-0.086); @@ -843,12 +844,12 @@ template void testHamiltonianObsExpvalShot() { DYNAMIC_SECTION(" - With Identity but no shots_range" << StateVectorMPIToName::name) { - size_t num_shots = 10000; - std::vector shots_range = {}; + std::size_t num_shots = 10000; + std::vector shots_range = {}; auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto I1 = std::make_shared>( - "Identity", std::vector{1}); + "Identity", std::vector{1}); auto obs = HamiltonianMPI::create({0.3, 0.5}, {X0, I1}); PrecisionT expected = Measurer.expval(*obs); @@ -858,15 +859,15 @@ template void testHamiltonianObsExpvalShot() { DYNAMIC_SECTION(" With shots_range" << StateVectorMPIToName::name) { - size_t num_shots = 10000; - std::vector shots_range; + std::size_t num_shots = 10000; + std::vector shots_range; for (size_t i = 0; i < num_shots; i += 2) { shots_range.push_back(i); } auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto Z1 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto obs = HamiltonianMPI::create({0.3, 0.5}, {X0, Z1}); PrecisionT expected = PrecisionT(-0.086); @@ -876,15 +877,15 @@ template void testHamiltonianObsExpvalShot() { DYNAMIC_SECTION(" With Identity and shots_range" << StateVectorMPIToName::name) { - size_t num_shots = 10000; - std::vector shots_range; + std::size_t num_shots = 10000; + std::vector shots_range; for (size_t i = 0; i < num_shots; i += 2) { shots_range.push_back(i); } auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto I1 = std::make_shared>( - "Identity", std::vector{1}); + "Identity", std::vector{1}); auto obs = HamiltonianMPI::create({0.3, 0.5}, {X0, I1}); PrecisionT expected = Measurer.expval(*obs); @@ -911,16 +912,16 @@ template void testNamedObsVar() { auto statevector_data = createNonTrivialState>(); - size_t num_qubits = 3; + std::size_t num_qubits = 3; MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -941,7 +942,7 @@ template void testNamedObsVar() { // This object attaches to the statevector allowing several measures. MeasurementsMPI Measurer(sv); - std::vector> wires_list = {{0}, {1}, {2}}; + std::vector> wires_list = {{0}, {1}, {2}}; std::vector obs_name = {"PauliX", "PauliY", "PauliZ"}; // Expected results calculated with Pennylane default.qubit: std::vector> exp_values_ref = { @@ -971,7 +972,7 @@ template void testNamedObsVar() { NamedObsMPI obs(obs_name[ind_obs], wires_list[ind_wires]); PrecisionT expected = exp_values_ref[ind_obs][ind_wires]; - size_t num_shots = 10000; + std::size_t num_shots = 10000; PrecisionT result = Measurer.var(obs, num_shots); REQUIRE(expected == Approx(result).margin(5e-2)); } @@ -998,16 +999,16 @@ template void testHermitianObsVar() { auto statevector_data = createNonTrivialState>(); - size_t num_qubits = 3; + std::size_t num_qubits = 3; MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -1034,7 +1035,7 @@ template void testHermitianObsVar() { DYNAMIC_SECTION("Varying wires - 2x2 matrix - " << StateVectorMPIToName::name) { - std::vector> wires_list = {{0}, {1}, {2}}; + std::vector> wires_list = {{0}, {1}, {2}}; // Expected results calculated with Pennylane default.qubit: std::vector exp_values_ref = { 0.5849835714501204, 0.7701511529340699, 0.9126678074548389}; @@ -1054,7 +1055,7 @@ template void testHermitianObsVar() { DYNAMIC_SECTION("Varying wires - 4x4 matrix - " << StateVectorMPIToName::name) { - std::vector> wires_list = { + std::vector> wires_list = { {0, 1}, {0, 2}, {1, 2}}; // Expected results calculated with Pennylane default.qubit: std::vector exp_values_ref = { @@ -1099,16 +1100,16 @@ template void testTensorProdObsVarShot() { {0.0, 0.0}, {0.0, 0.1}, {0.1, 0.1}, {0.1, 0.2}, {0.2, 0.2}, {0.3, 0.3}, {0.3, 0.4}, {0.4, 0.5}}; - size_t num_qubits = 3; + std::size_t num_qubits = 3; MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -1131,11 +1132,11 @@ template void testTensorProdObsVarShot() { DYNAMIC_SECTION(" Without Identity" << StateVectorMPIToName::name) { - size_t num_shots = 10000; + std::size_t num_shots = 10000; auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto Z1 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto obs = TensorProdObsMPI::create({X0, Z1}); auto expected = Measurer.var(*obs); auto result = Measurer.var(*obs, num_shots); @@ -1144,11 +1145,11 @@ template void testTensorProdObsVarShot() { DYNAMIC_SECTION(" With Identity" << StateVectorMPIToName::name) { - size_t num_shots = 10000; + std::size_t num_shots = 10000; auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto I1 = std::make_shared>( - "Identity", std::vector{1}); + "Identity", std::vector{1}); auto obs = TensorProdObsMPI::create({X0, I1}); PrecisionT expected = Measurer.var(*obs); PrecisionT result = Measurer.var(*obs, num_shots); @@ -1170,7 +1171,7 @@ template void testSamples() { using StateVectorT = typename TypeList::Type; using PrecisionT = typename StateVectorT::PrecisionT; - constexpr size_t twos[] = { + constexpr std::size_t twos[] = { 1U << 0U, 1U << 1U, 1U << 2U, 1U << 3U, 1U << 4U, 1U << 5U, 1U << 6U, 1U << 7U, 1U << 8U, 1U << 9U, 1U << 10U, 1U << 11U, 1U << 12U, 1U << 13U, 1U << 14U, 1U << 15U, 1U << 16U, 1U << 17U, @@ -1182,16 +1183,16 @@ template void testSamples() { auto statevector_data = createNonTrivialState>(); - size_t num_qubits = 3; + std::size_t num_qubits = 3; MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -1216,12 +1217,12 @@ template void testSamples() { 0.67078706, 0.03062806, 0.0870997, 0.00397696, 0.17564072, 0.00801973, 0.02280642, 0.00104134}; - size_t N = std::pow(2, num_qubits); - size_t num_samples = 100000; + std::size_t N = std::pow(2, num_qubits); + std::size_t num_samples = 100000; auto &&samples = Measurer.generate_samples(num_samples); - std::vector counts(N, 0); - std::vector samples_decimal(num_samples, 0); + std::vector counts(N, 0); + std::vector samples_decimal(num_samples, 0); // convert samples to decimal and then bin them in counts for (size_t i = 0; i < num_samples; i++) { @@ -1263,16 +1264,16 @@ template void testSamplesCountsObs() { auto statevector_data = createNonTrivialState>(); - size_t num_qubits = 3; + std::size_t num_qubits = 3; MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -1293,7 +1294,7 @@ template void testSamplesCountsObs() { // This object attaches to the statevector allowing several measures. MeasurementsMPI Measurer(sv); - constexpr size_t twos[] = { + constexpr std::size_t twos[] = { 1U << 0U, 1U << 1U, 1U << 2U, 1U << 3U, 1U << 4U, 1U << 5U, 1U << 6U, 1U << 7U, 1U << 8U, 1U << 9U, 1U << 10U, 1U << 11U, 1U << 12U, 1U << 13U, 1U << 14U, 1U << 15U, 1U << 16U, 1U << 17U, @@ -1301,7 +1302,7 @@ template void testSamplesCountsObs() { 1U << 24U, 1U << 25U, 1U << 26U, 1U << 27U, 1U << 28U, 1U << 29U, 1U << 30U, 1U << 31U}; - std::vector> wires_list = {{0}, {1}, {2}}; + std::vector> wires_list = {{0}, {1}, {2}}; std::vector obs_name = {"PauliX", "PauliY", "PauliZ", "Hadamard", "Identity"}; // Expected results calculated with Pennylane default.qubit: @@ -1315,7 +1316,7 @@ template void testSamplesCountsObs() { DYNAMIC_SECTION(obs_name[ind_obs] << " Sample Obs - Varying wires" << StateVectorMPIToName::name) { - size_t num_shots = 10000; + std::size_t num_shots = 10000; for (size_t ind_wires = 0; ind_wires < wires_list.size(); ind_wires++) { NamedObsMPI obs(obs_name[ind_obs], @@ -1336,7 +1337,7 @@ template void testSamplesCountsObs() { DYNAMIC_SECTION(obs_name[ind_obs] << " Counts Obs - Varying wires" << StateVectorMPIToName::name) { - size_t num_shots = 10000; + std::size_t num_shots = 10000; for (size_t ind_wires = 0; ind_wires < wires_list.size(); ind_wires++) { NamedObsMPI obs(obs_name[ind_obs], @@ -1361,13 +1362,13 @@ template void testSamplesCountsObs() { 0.67078706, 0.03062806, 0.0870997, 0.00397696, 0.17564072, 0.00801973, 0.02280642, 0.00104134}; - size_t num_qubits = 3; - size_t N = std::pow(2, num_qubits); - size_t num_samples = 100000; + std::size_t num_qubits = 3; + std::size_t N = std::pow(2, num_qubits); + std::size_t num_samples = 100000; auto &&samples = Measurer.sample(num_samples); - std::vector counts(N, 0); - std::vector samples_decimal(num_samples, 0); + std::vector counts(N, 0); + std::vector samples_decimal(num_samples, 0); // convert samples to decimal and then bin them in counts for (size_t i = 0; i < num_samples; i++) { @@ -1398,13 +1399,13 @@ template void testSamplesCountsObs() { 0.67078706, 0.03062806, 0.0870997, 0.00397696, 0.17564072, 0.00801973, 0.02280642, 0.00104134}; - size_t num_qubits = 3; - size_t N = std::pow(2, num_qubits); - size_t num_samples = 100000; + std::size_t num_qubits = 3; + std::size_t N = std::pow(2, num_qubits); + std::size_t num_samples = 100000; auto &&counts_sample = Measurer.counts(num_samples); - std::vector counts(N, 0); + std::vector counts(N, 0); // convert samples to decimal and then bin them in counts for (auto &it : counts_sample) { @@ -1440,16 +1441,16 @@ template void testHamiltonianObsVarShot() { auto statevector_data = createNonTrivialState>(); - size_t num_qubits = 3; + std::size_t num_qubits = 3; MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -1472,14 +1473,14 @@ template void testHamiltonianObsVarShot() { DYNAMIC_SECTION("YZ" << StateVectorMPIToName::name) { auto Y0 = std::make_shared>( - "PauliY", std::vector{0}); + "PauliY", std::vector{0}); auto Z1 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto ob = HamiltonianMPI::create({0.5, 0.5}, {Y0, Z1}); - size_t num_shots = 10000; + std::size_t num_shots = 10000; auto res = Measurer.var(*ob, num_shots); auto expected = Measurer.var(*ob); @@ -1488,14 +1489,14 @@ template void testHamiltonianObsVarShot() { DYNAMIC_SECTION("YI" << StateVectorMPIToName::name) { auto Y0 = std::make_shared>( - "PauliY", std::vector{0}); + "PauliY", std::vector{0}); auto I1 = std::make_shared>( - "Identity", std::vector{1}); + "Identity", std::vector{1}); auto ob = HamiltonianMPI::create({0.5, 0.5}, {Y0, I1}); - size_t num_shots = 10000; + std::size_t num_shots = 10000; auto res = Measurer.var(*ob, num_shots); auto expected = Measurer.var(*ob); diff --git a/pennylane_lightning/core/src/observables/Observables.hpp b/pennylane_lightning/core/src/observables/Observables.hpp index c75dea1522..d10cb4b62a 100644 --- a/pennylane_lightning/core/src/observables/Observables.hpp +++ b/pennylane_lightning/core/src/observables/Observables.hpp @@ -77,7 +77,7 @@ template class Observable { virtual void applyInPlaceShots(StateVectorT &sv, std::vector> &eigenValues, - std::vector &ob_wires) const = 0; + std::vector &ob_wires) const = 0; /** * @brief Get the name of the observable @@ -87,7 +87,7 @@ template class Observable { /** * @brief Get the wires the observable applies to. */ - [[nodiscard]] virtual auto getWires() const -> std::vector = 0; + [[nodiscard]] virtual auto getWires() const -> std::vector = 0; /** * @brief Get the observable data. @@ -134,7 +134,7 @@ class NamedObsBase : public Observable { protected: std::string obs_name_; - std::vector wires_; + std::vector wires_; std::vector params_; private: @@ -155,7 +155,7 @@ class NamedObsBase : public Observable { * @param wires Argument to construct wires. * @param params Argument to construct parameters */ - NamedObsBase(std::string obs_name, std::vector wires, + NamedObsBase(std::string obs_name, std::vector wires, std::vector params = {}) : obs_name_{std::move(obs_name)}, wires_{std::move(wires)}, params_{std::move(params)} {} @@ -167,7 +167,7 @@ class NamedObsBase : public Observable { return obs_stream.str(); } - [[nodiscard]] auto getWires() const -> std::vector override { + [[nodiscard]] auto getWires() const -> std::vector override { return wires_; } @@ -177,7 +177,7 @@ class NamedObsBase : public Observable { void applyInPlaceShots(StateVectorT &sv, std::vector> &eigenValues, - std::vector &ob_wires) const override { + std::vector &ob_wires) const override { ob_wires.clear(); eigenValues.clear(); ob_wires.push_back(wires_[0]); @@ -218,7 +218,7 @@ class HermitianObsBase : public Observable { protected: MatrixT matrix_; - std::vector wires_; + std::vector wires_; #ifdef PL_USE_LAPACK std::vector eigenVals_; @@ -241,7 +241,7 @@ class HermitianObsBase : public Observable { * @param matrix Matrix in row major format. * @param wires Wires the observable applies to. */ - HermitianObsBase(MatrixT matrix, std::vector wires) + HermitianObsBase(MatrixT matrix, std::vector wires) : matrix_{std::move(matrix)}, wires_{std::move(wires)} { PL_ASSERT(matrix_.size() == Util::exp2(2 * wires_.size())); @@ -268,7 +268,7 @@ class HermitianObsBase : public Observable { [[nodiscard]] auto getMatrix() const -> const MatrixT & { return matrix_; } - [[nodiscard]] auto getWires() const -> std::vector override { + [[nodiscard]] auto getWires() const -> std::vector override { return wires_; } @@ -283,7 +283,7 @@ class HermitianObsBase : public Observable { void applyInPlaceShots( [[maybe_unused]] StateVectorT &sv, [[maybe_unused]] std::vector> &eigenValues, - [[maybe_unused]] std::vector &ob_wires) const override { + [[maybe_unused]] std::vector &ob_wires) const override { #ifdef PL_USE_LAPACK std::vector> mat(matrix_.size()); @@ -320,7 +320,7 @@ template class TensorProdObsBase : public Observable { protected: std::vector>> obs_; - std::vector all_wires_; + std::vector all_wires_; private: [[nodiscard]] auto isEqual(const Observable &other) const @@ -357,7 +357,7 @@ class TensorProdObsBase : public Observable { "from a single TensorProdObsBase."); } - std::unordered_set wires; + std::unordered_set wires; for (const auto &ob : obs_) { const auto ob_wires = ob->getWires(); for (const auto wire : ob_wires) { @@ -366,7 +366,7 @@ class TensorProdObsBase : public Observable { wires.insert(wire); } } - all_wires_ = std::vector(wires.begin(), wires.end()); + all_wires_ = std::vector(wires.begin(), wires.end()); std::sort(all_wires_.begin(), all_wires_.end()); } @@ -407,16 +407,16 @@ class TensorProdObsBase : public Observable { /** * @brief Get the number of operations in observable. * - * @return size_t + * @return std::size_t */ - [[nodiscard]] auto getSize() const -> size_t { return obs_.size(); } + [[nodiscard]] auto getSize() const -> std::size_t { return obs_.size(); } /** * @brief Get the wires for each observable operation. * - * @return const std::vector>& + * @return const std::vector>& */ - [[nodiscard]] auto getWires() const -> std::vector override { + [[nodiscard]] auto getWires() const -> std::vector override { return all_wires_; } @@ -436,7 +436,7 @@ class TensorProdObsBase : public Observable { void applyInPlaceShots(StateVectorT &sv, std::vector> &eigenValues, - std::vector &ob_wires) const override { + std::vector &ob_wires) const override { for (const auto &ob : obs_) { if (ob->getObsName().find("Hamiltonian") != std::string::npos) { PL_ABORT("Hamiltonian observables as a term of an TensorProd " @@ -449,7 +449,7 @@ class TensorProdObsBase : public Observable { ob_wires.clear(); for (const auto &ob : obs_) { std::vector> eigenVals; - std::vector ob_wire; + std::vector ob_wire; ob->applyInPlaceShots(sv, eigenVals, ob_wire); ob_wires.push_back(ob_wire[0]); eigenValues.push_back(eigenVals[0]); @@ -544,19 +544,19 @@ class HamiltonianBase : public Observable { void applyInPlaceShots( [[maybe_unused]] StateVectorT &sv, [[maybe_unused]] std::vector> &eigenValues, - [[maybe_unused]] std::vector &ob_wires) const override { + [[maybe_unused]] std::vector &ob_wires) const override { PL_ABORT("Hamiltonian observables as a term of an observable do not " "support shot measurement."); } - [[nodiscard]] auto getWires() const -> std::vector override { - std::unordered_set wires; + [[nodiscard]] auto getWires() const -> std::vector override { + std::unordered_set wires; for (const auto &ob : obs_) { const auto ob_wires = ob->getWires(); wires.insert(ob_wires.begin(), ob_wires.end()); } - auto all_wires = std::vector(wires.begin(), wires.end()); + auto all_wires = std::vector(wires.begin(), wires.end()); std::sort(all_wires.begin(), all_wires.end()); return all_wires; } @@ -677,7 +677,7 @@ class SparseHamiltonianBase : public Observable { void applyInPlaceShots( [[maybe_unused]] StateVectorT &sv, [[maybe_unused]] std::vector> &eigenValues, - [[maybe_unused]] std::vector &ob_wires) const override { + [[maybe_unused]] std::vector &ob_wires) const override { PL_ABORT( "SparseHamiltonian observables do not support shot measurement."); } @@ -703,7 +703,7 @@ class SparseHamiltonianBase : public Observable { /** * @brief Get the wires the observable applies to. */ - [[nodiscard]] auto getWires() const -> std::vector override { + [[nodiscard]] auto getWires() const -> std::vector override { return wires_; }; }; diff --git a/pennylane_lightning/core/src/observables/tests/Test_Observables.cpp b/pennylane_lightning/core/src/observables/tests/Test_Observables.cpp index 7fdb3a9f76..5da1f458a7 100644 --- a/pennylane_lightning/core/src/observables/tests/Test_Observables.cpp +++ b/pennylane_lightning/core/src/observables/tests/Test_Observables.cpp @@ -123,7 +123,7 @@ template void testNamedObsBase() { DYNAMIC_SECTION("Unsupported NamedObs for applyInPlaceShots") { std::mt19937_64 re{1337}; - constexpr size_t num_qubits = 3; + constexpr std::size_t num_qubits = 3; auto init_state = createRandomStateVectorData(re, num_qubits); @@ -131,7 +131,7 @@ template void testNamedObsBase() { auto obs = NamedObsT("RY", {0}, {0.4}); std::vector> eigenValues; - std::vector ob_wires; + std::vector ob_wires; REQUIRE_THROWS_WITH( obs.applyInPlaceShots(state_vector, eigenValues, ob_wires), @@ -211,7 +211,7 @@ template void testHermitianObsBase() { DYNAMIC_SECTION("Failed to create a HermitianObs- " << StateVectorToName::name) { std::mt19937_64 re{1337}; - constexpr size_t num_qubits = 3; + constexpr std::size_t num_qubits = 3; auto init_state = createRandomStateVectorData(re, num_qubits); @@ -220,7 +220,7 @@ template void testHermitianObsBase() { HermitianObsT{std::vector{1.0, 0.0, 1.0, 0.0}, {0}}; std::vector> eigenValues; - std::vector ob_wires; + std::vector ob_wires; REQUIRE_THROWS_WITH( obs.applyInPlaceShots(state_vector, eigenValues, ob_wires), @@ -231,7 +231,7 @@ template void testHermitianObsBase() { DYNAMIC_SECTION("Failed for HermitianObs for applyInPlaceShots - " << StateVectorToName::name) { std::mt19937_64 re{1337}; - constexpr size_t num_qubits = 3; + constexpr std::size_t num_qubits = 3; auto init_state = createRandomStateVectorData(re, num_qubits); @@ -240,7 +240,7 @@ template void testHermitianObsBase() { HermitianObsT{std::vector{1.0, 0.0, -1.0, 0.0}, {0}}; std::vector> eigenValues; - std::vector ob_wires; + std::vector ob_wires; REQUIRE_THROWS_WITH( obs.applyInPlaceShots(state_vector, eigenValues, ob_wires), @@ -274,11 +274,11 @@ template void testTensorProdObsBase() { << StateVectorToName::name) { auto ob1 = std::make_shared( std::vector(16, ComplexT{0.0, 0.0}), - std::vector{0, 1}); - auto ob2_1 = - std::make_shared("PauliX", std::vector{1}); - auto ob2_2 = - std::make_shared("PauliZ", std::vector{2}); + std::vector{0, 1}); + auto ob2_1 = std::make_shared( + "PauliX", std::vector{1}); + auto ob2_2 = std::make_shared( + "PauliZ", std::vector{2}); auto ob2 = TensorProdObsT::create({ob2_1, ob2_2}); REQUIRE_THROWS_AS(TensorProdObsT::create({ob1, ob2}), @@ -290,11 +290,11 @@ template void testTensorProdObsBase() { << StateVectorToName::name) { auto ob1 = std::make_shared( std::vector(16, ComplexT{0.0, 0.0}), - std::vector{0, 1}); - auto ob2_1 = - std::make_shared("PauliX", std::vector{2}); - auto ob2_2 = - std::make_shared("PauliZ", std::vector{3}); + std::vector{0, 1}); + auto ob2_1 = std::make_shared( + "PauliX", std::vector{2}); + auto ob2_2 = std::make_shared( + "PauliZ", std::vector{3}); auto ob2 = TensorProdObsT::create({ob2_1, ob2_2}); REQUIRE_NOTHROW(TensorProdObsT::create({ob1, ob2})); @@ -302,10 +302,10 @@ template void testTensorProdObsBase() { DYNAMIC_SECTION("Constructing an invalid TensorProd(TensorProd) - " << StateVectorToName::name) { - auto ob2_1 = - std::make_shared("PauliX", std::vector{2}); - auto ob2_2 = - std::make_shared("PauliZ", std::vector{3}); + auto ob2_1 = std::make_shared( + "PauliX", std::vector{2}); + auto ob2_2 = std::make_shared( + "PauliZ", std::vector{3}); auto ob2 = TensorProdObsT::create({ob2_1, ob2_2}); REQUIRE_THROWS_AS(TensorProdObsT::create({ob2}), @@ -314,29 +314,39 @@ template void testTensorProdObsBase() { DYNAMIC_SECTION("getObsName - " << StateVectorToName::name) { - auto ob = TensorProdObsT( - std::make_shared("PauliX", std::vector{0}), - std::make_shared("PauliZ", std::vector{1})); + auto ob = + TensorProdObsT(std::make_shared( + "PauliX", std::vector{0}), + std::make_shared( + "PauliZ", std::vector{1})); REQUIRE(ob.getObsName() == "PauliX[0] @ PauliZ[1]"); } DYNAMIC_SECTION("Compare tensor product observables" << StateVectorToName::name) { - auto ob1 = TensorProdObsT{ - std::make_shared("PauliX", std::vector{0}), - std::make_shared("PauliZ", std::vector{1})}; - auto ob2 = TensorProdObsT{ - std::make_shared("PauliX", std::vector{0}), - std::make_shared("PauliZ", std::vector{1})}; - auto ob3 = TensorProdObsT{ - std::make_shared("PauliX", std::vector{0}), - std::make_shared("PauliZ", std::vector{2})}; - auto ob4 = TensorProdObsT{ - std::make_shared("PauliZ", std::vector{0}), - std::make_shared("PauliZ", std::vector{1})}; - - auto ob5 = TensorProdObsT{ - std::make_shared("PauliZ", std::vector{0})}; + auto ob1 = + TensorProdObsT{std::make_shared( + "PauliX", std::vector{0}), + std::make_shared( + "PauliZ", std::vector{1})}; + auto ob2 = + TensorProdObsT{std::make_shared( + "PauliX", std::vector{0}), + std::make_shared( + "PauliZ", std::vector{1})}; + auto ob3 = + TensorProdObsT{std::make_shared( + "PauliX", std::vector{0}), + std::make_shared( + "PauliZ", std::vector{2})}; + auto ob4 = + TensorProdObsT{std::make_shared( + "PauliZ", std::vector{0}), + std::make_shared( + "PauliZ", std::vector{1})}; + + auto ob5 = TensorProdObsT{std::make_shared( + "PauliZ", std::vector{0})}; REQUIRE(ob1 == ob2); REQUIRE(ob1 != ob3); @@ -353,8 +363,10 @@ template void testTensorProdObsBase() { using VectorT = TestVector; auto obs = TensorProdObsT{ - std::make_shared("PauliX", std::vector{0}), - std::make_shared("PauliX", std::vector{2}), + std::make_shared("PauliX", + std::vector{0}), + std::make_shared("PauliX", + std::vector{2}), }; SECTION("Test using |1+0>") { @@ -393,12 +405,12 @@ template void testTensorProdObsBase() { DYNAMIC_SECTION("Failed for ApplyInPlaceShots" << StateVectorToName::name) { using VectorT = TestVector; - auto X0 = - std::make_shared("PauliX", std::vector{0}); - auto X1 = - std::make_shared("PauliX", std::vector{1}); - auto X2 = - std::make_shared("PauliX", std::vector{2}); + auto X0 = std::make_shared("PauliX", + std::vector{0}); + auto X1 = std::make_shared("PauliX", + std::vector{1}); + auto X2 = std::make_shared("PauliX", + std::vector{2}); auto ham = HamiltonianT::create({0.8, 0.5, 0.7}, { X0, @@ -413,7 +425,7 @@ template void testTensorProdObsBase() { StateVectorT state_vector(st_data.data(), st_data.size()); std::vector> eigenValues; - std::vector ob_wires; + std::vector ob_wires; REQUIRE_THROWS_AS( obs.applyInPlaceShots(state_vector, eigenValues, ob_wires), @@ -443,11 +455,13 @@ template void testHamiltonianBase() { const auto h = PrecisionT{0.809}; // half of the golden ratio auto zz = std::make_shared( - std::make_shared("PauliZ", std::vector{0}), - std::make_shared("PauliZ", std::vector{1})); + std::make_shared("PauliZ", std::vector{0}), + std::make_shared("PauliZ", std::vector{1})); - auto x1 = std::make_shared("PauliX", std::vector{0}); - auto x2 = std::make_shared("PauliX", std::vector{1}); + auto x1 = + std::make_shared("PauliX", std::vector{0}); + auto x2 = + std::make_shared("PauliX", std::vector{1}); DYNAMIC_SECTION( "Hamiltonian constructor only accepts valid arguments - " @@ -461,10 +475,10 @@ template void testHamiltonianBase() { DYNAMIC_SECTION("getObsName - " << StateVectorToName::name) { - auto X0 = std::make_shared("PauliX", - std::vector{0}); - auto Z2 = std::make_shared("PauliZ", - std::vector{2}); + auto X0 = std::make_shared( + "PauliX", std::vector{0}); + auto Z2 = std::make_shared( + "PauliZ", std::vector{2}); REQUIRE( HamiltonianT::create({0.3, 0.5}, {X0, Z2})->getObsName() == @@ -474,26 +488,26 @@ template void testHamiltonianBase() { DYNAMIC_SECTION("Compare Hamiltonians - " << StateVectorToName::name) { - auto X0 = std::make_shared("PauliX", - std::vector{0}); - auto X1 = std::make_shared("PauliX", - std::vector{1}); - auto X2 = std::make_shared("PauliX", - std::vector{2}); - - auto Y0 = std::make_shared("PauliY", - std::vector{0}); - auto Y1 = std::make_shared("PauliY", - std::vector{1}); - auto Y2 = std::make_shared("PauliY", - std::vector{2}); - - auto Z0 = std::make_shared("PauliZ", - std::vector{0}); - auto Z1 = std::make_shared("PauliZ", - std::vector{1}); - auto Z2 = std::make_shared("PauliZ", - std::vector{2}); + auto X0 = std::make_shared( + "PauliX", std::vector{0}); + auto X1 = std::make_shared( + "PauliX", std::vector{1}); + auto X2 = std::make_shared( + "PauliX", std::vector{2}); + + auto Y0 = std::make_shared( + "PauliY", std::vector{0}); + auto Y1 = std::make_shared( + "PauliY", std::vector{1}); + auto Y2 = std::make_shared( + "PauliY", std::vector{2}); + + auto Z0 = std::make_shared( + "PauliZ", std::vector{0}); + auto Z1 = std::make_shared( + "PauliZ", std::vector{1}); + auto Z2 = std::make_shared( + "PauliZ", std::vector{2}); auto ham1 = HamiltonianT::create( {0.8, 0.5, 0.7}, @@ -543,16 +557,16 @@ template void testHamiltonianBase() { DYNAMIC_SECTION("getWires - " << StateVectorToName::name) { - auto Z0 = std::make_shared("PauliZ", - std::vector{0}); - auto Z5 = std::make_shared("PauliZ", - std::vector{5}); - auto Z9 = std::make_shared("PauliZ", - std::vector{9}); + auto Z0 = std::make_shared( + "PauliZ", std::vector{0}); + auto Z5 = std::make_shared( + "PauliZ", std::vector{5}); + auto Z9 = std::make_shared( + "PauliZ", std::vector{9}); auto ham1 = HamiltonianT::create({0.8, 0.5, 0.7}, {Z0, Z5, Z9}); - REQUIRE(ham1->getWires() == std::vector{0, 5, 9}); + REQUIRE(ham1->getWires() == std::vector{0, 5, 9}); } DYNAMIC_SECTION("applyInPlace must fail - " @@ -576,7 +590,7 @@ template void testHamiltonianBase() { StateVectorT state_vector(st_data.data(), st_data.size()); std::vector> eigenValues; - std::vector ob_wires; + std::vector ob_wires; REQUIRE_THROWS_AS( ham->applyInPlaceShots(state_vector, eigenValues, ob_wires), @@ -636,7 +650,7 @@ template void testSparseHamiltonianBase() { DYNAMIC_SECTION("SparseHamiltonianBase - getWires - " << StateVectorToName::name) { - REQUIRE(sparseH->getWires() == std::vector{0, 1, 2}); + REQUIRE(sparseH->getWires() == std::vector{0, 1, 2}); } DYNAMIC_SECTION("SparseHamiltonianBase - getObsName - " @@ -672,7 +686,7 @@ template void testSparseHamiltonianBase() { StateVectorT state_vector(init_state.data(), init_state.size()); std::vector> eigenValues; - std::vector ob_wires; + std::vector ob_wires; REQUIRE_THROWS_WITH( sparseH->applyInPlaceShots(state_vector, eigenValues, ob_wires), diff --git a/pennylane_lightning/core/src/observables/tests/mpi/Test_ObservablesMPI.cpp b/pennylane_lightning/core/src/observables/tests/mpi/Test_ObservablesMPI.cpp index 201952efa5..eb39b57f5c 100644 --- a/pennylane_lightning/core/src/observables/tests/mpi/Test_ObservablesMPI.cpp +++ b/pennylane_lightning/core/src/observables/tests/mpi/Test_ObservablesMPI.cpp @@ -186,11 +186,11 @@ template void testTensorProdObsBase() { << StateVectorMPIToName::name) { auto ob1 = std::make_shared( std::vector(16, ComplexT{0.0, 0.0}), - std::vector{0, 1}); - auto ob2_1 = - std::make_shared("PauliX", std::vector{1}); - auto ob2_2 = - std::make_shared("PauliZ", std::vector{2}); + std::vector{0, 1}); + auto ob2_1 = std::make_shared( + "PauliX", std::vector{1}); + auto ob2_2 = std::make_shared( + "PauliZ", std::vector{2}); auto ob2 = TensorProdObsT::create({ob2_1, ob2_2}); REQUIRE_THROWS_AS(TensorProdObsT::create({ob1, ob2}), @@ -202,11 +202,11 @@ template void testTensorProdObsBase() { << StateVectorMPIToName::name) { auto ob1 = std::make_shared( std::vector(16, ComplexT{0.0, 0.0}), - std::vector{0, 1}); - auto ob2_1 = - std::make_shared("PauliX", std::vector{2}); - auto ob2_2 = - std::make_shared("PauliZ", std::vector{3}); + std::vector{0, 1}); + auto ob2_1 = std::make_shared( + "PauliX", std::vector{2}); + auto ob2_2 = std::make_shared( + "PauliZ", std::vector{3}); auto ob2 = TensorProdObsT::create({ob2_1, ob2_2}); REQUIRE_NOTHROW(TensorProdObsT::create({ob1, ob2})); @@ -214,29 +214,39 @@ template void testTensorProdObsBase() { DYNAMIC_SECTION("getObsName - " << StateVectorMPIToName::name) { - auto ob = TensorProdObsT( - std::make_shared("PauliX", std::vector{0}), - std::make_shared("PauliZ", std::vector{1})); + auto ob = + TensorProdObsT(std::make_shared( + "PauliX", std::vector{0}), + std::make_shared( + "PauliZ", std::vector{1})); REQUIRE(ob.getObsName() == "PauliX[0] @ PauliZ[1]"); } DYNAMIC_SECTION("Compare tensor product observables" << StateVectorMPIToName::name) { - auto ob1 = TensorProdObsT{ - std::make_shared("PauliX", std::vector{0}), - std::make_shared("PauliZ", std::vector{1})}; - auto ob2 = TensorProdObsT{ - std::make_shared("PauliX", std::vector{0}), - std::make_shared("PauliZ", std::vector{1})}; - auto ob3 = TensorProdObsT{ - std::make_shared("PauliX", std::vector{0}), - std::make_shared("PauliZ", std::vector{2})}; - auto ob4 = TensorProdObsT{ - std::make_shared("PauliZ", std::vector{0}), - std::make_shared("PauliZ", std::vector{1})}; - - auto ob5 = TensorProdObsT{ - std::make_shared("PauliZ", std::vector{0})}; + auto ob1 = + TensorProdObsT{std::make_shared( + "PauliX", std::vector{0}), + std::make_shared( + "PauliZ", std::vector{1})}; + auto ob2 = + TensorProdObsT{std::make_shared( + "PauliX", std::vector{0}), + std::make_shared( + "PauliZ", std::vector{1})}; + auto ob3 = + TensorProdObsT{std::make_shared( + "PauliX", std::vector{0}), + std::make_shared( + "PauliZ", std::vector{2})}; + auto ob4 = + TensorProdObsT{std::make_shared( + "PauliZ", std::vector{0}), + std::make_shared( + "PauliZ", std::vector{1})}; + + auto ob5 = TensorProdObsT{std::make_shared( + "PauliZ", std::vector{0})}; REQUIRE(ob1 == ob2); REQUIRE(ob1 != ob3); @@ -249,22 +259,24 @@ template void testTensorProdObsBase() { using VectorT = TestVector; auto obs = TensorProdObsT{ - std::make_shared("PauliX", std::vector{0}), - std::make_shared("PauliX", std::vector{2}), + std::make_shared("PauliX", + std::vector{0}), + std::make_shared("PauliX", + std::vector{2}), }; SECTION("Test using |1+0>") { MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - const size_t num_qubits = 3; - size_t mpi_buffersize = 1; + const std::size_t num_qubits = 3; + std::size_t mpi_buffersize = 1; int nGlobalIndexBits = std::bit_width(static_cast( mpi_manager.getSize())) - 1; int nLocalIndexBits = num_qubits - nGlobalIndexBits; - size_t subSvLength = 1 << nLocalIndexBits; + std::size_t subSvLength = 1 << nLocalIndexBits; mpi_manager.Barrier(); int nDevices = 0; // Number of GPU devices per node @@ -304,14 +316,14 @@ template void testTensorProdObsBase() { MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - const size_t num_qubits = 4; - size_t mpi_buffersize = 1; + const std::size_t num_qubits = 4; + std::size_t mpi_buffersize = 1; int nGlobalIndexBits = std::bit_width(static_cast( mpi_manager.getSize())) - 1; int nLocalIndexBits = num_qubits - nGlobalIndexBits; - size_t subSvLength = 1 << nLocalIndexBits; + std::size_t subSvLength = 1 << nLocalIndexBits; mpi_manager.Barrier(); int nDevices = 0; // Number of GPU devices per node @@ -370,8 +382,8 @@ template void testHamiltonianBase() { MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - const size_t num_qubits = 3; - size_t mpi_buffersize = 1; + const std::size_t num_qubits = 3; + std::size_t mpi_buffersize = 1; int nGlobalIndexBits = std::bit_width(static_cast(mpi_manager.getSize())) - @@ -389,11 +401,13 @@ template void testHamiltonianBase() { const auto h = PrecisionT{0.809}; // half of the golden ratio auto zz = std::make_shared( - std::make_shared("PauliZ", std::vector{0}), - std::make_shared("PauliZ", std::vector{1})); + std::make_shared("PauliZ", std::vector{0}), + std::make_shared("PauliZ", std::vector{1})); - auto x1 = std::make_shared("PauliX", std::vector{0}); - auto x2 = std::make_shared("PauliX", std::vector{1}); + auto x1 = + std::make_shared("PauliX", std::vector{0}); + auto x2 = + std::make_shared("PauliX", std::vector{1}); DYNAMIC_SECTION( "Hamiltonian constructor only accepts valid arguments - " @@ -407,10 +421,10 @@ template void testHamiltonianBase() { DYNAMIC_SECTION("getObsName - " << StateVectorMPIToName::name) { - auto X0 = std::make_shared("PauliX", - std::vector{0}); - auto Z2 = std::make_shared("PauliZ", - std::vector{2}); + auto X0 = std::make_shared( + "PauliX", std::vector{0}); + auto Z2 = std::make_shared( + "PauliZ", std::vector{2}); REQUIRE( HamiltonianT::create({0.3, 0.5}, {X0, Z2})->getObsName() == @@ -420,26 +434,26 @@ template void testHamiltonianBase() { DYNAMIC_SECTION("Compare Hamiltonians - " << StateVectorMPIToName::name) { - auto X0 = std::make_shared("PauliX", - std::vector{0}); - auto X1 = std::make_shared("PauliX", - std::vector{1}); - auto X2 = std::make_shared("PauliX", - std::vector{2}); - - auto Y0 = std::make_shared("PauliY", - std::vector{0}); - auto Y1 = std::make_shared("PauliY", - std::vector{1}); - auto Y2 = std::make_shared("PauliY", - std::vector{2}); - - auto Z0 = std::make_shared("PauliZ", - std::vector{0}); - auto Z1 = std::make_shared("PauliZ", - std::vector{1}); - auto Z2 = std::make_shared("PauliZ", - std::vector{2}); + auto X0 = std::make_shared( + "PauliX", std::vector{0}); + auto X1 = std::make_shared( + "PauliX", std::vector{1}); + auto X2 = std::make_shared( + "PauliX", std::vector{2}); + + auto Y0 = std::make_shared( + "PauliY", std::vector{0}); + auto Y1 = std::make_shared( + "PauliY", std::vector{1}); + auto Y2 = std::make_shared( + "PauliY", std::vector{2}); + + auto Z0 = std::make_shared( + "PauliZ", std::vector{0}); + auto Z1 = std::make_shared( + "PauliZ", std::vector{1}); + auto Z2 = std::make_shared( + "PauliZ", std::vector{2}); auto ham1 = HamiltonianT::create( {0.8, 0.5, 0.7}, @@ -489,16 +503,16 @@ template void testHamiltonianBase() { DYNAMIC_SECTION("getWires - " << StateVectorMPIToName::name) { - auto Z0 = std::make_shared("PauliZ", - std::vector{0}); - auto Z5 = std::make_shared("PauliZ", - std::vector{5}); - auto Z9 = std::make_shared("PauliZ", - std::vector{9}); + auto Z0 = std::make_shared( + "PauliZ", std::vector{0}); + auto Z5 = std::make_shared( + "PauliZ", std::vector{5}); + auto Z9 = std::make_shared( + "PauliZ", std::vector{9}); auto ham1 = HamiltonianT::create({0.8, 0.5, 0.7}, {Z0, Z5, Z9}); - REQUIRE(ham1->getWires() == std::vector{0, 5, 9}); + REQUIRE(ham1->getWires() == std::vector{0, 5, 9}); } DYNAMIC_SECTION("applyInPlace must fail - " @@ -536,11 +550,11 @@ template void testSparseHamiltonianBase() { MPIManager mpi_manager(MPI_COMM_WORLD); - size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; - size_t subSvLength = 1 << nLocalIndexBits; + std::size_t mpi_buffersize = 1; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t subSvLength = 1 << nLocalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); diff --git a/pennylane_lightning/core/src/simulators/base/StateVectorBase.hpp b/pennylane_lightning/core/src/simulators/base/StateVectorBase.hpp index 0baa89d0ae..df5dedf4af 100644 --- a/pennylane_lightning/core/src/simulators/base/StateVectorBase.hpp +++ b/pennylane_lightning/core/src/simulators/base/StateVectorBase.hpp @@ -42,7 +42,7 @@ namespace Pennylane { */ template class StateVectorBase { protected: - size_t num_qubits_{0}; + std::size_t num_qubits_{0}; public: /** @@ -71,7 +71,7 @@ template class StateVectorBase { * * @return std::size_t */ - [[nodiscard]] auto getTotalNumQubits() const -> size_t { + [[nodiscard]] auto getTotalNumQubits() const -> std::size_t { return num_qubits_; } @@ -80,8 +80,8 @@ template class StateVectorBase { * * @return The size of the statevector */ - [[nodiscard]] size_t getLength() const { - return static_cast(exp2(num_qubits_)); + [[nodiscard]] std::size_t getLength() const { + return static_cast(exp2(num_qubits_)); } /** @@ -106,7 +106,7 @@ template class StateVectorBase { * @param params Optional parameter list for parametric gates. */ inline void applyOperation(const std::string &opName, - const std::vector &wires, + const std::vector &wires, bool adjoint = false, const std::vector ¶ms = {}) { return static_cast(this)->applyOperation(opName, wires, @@ -125,10 +125,10 @@ template class StateVectorBase { */ void applyOperations(const std::vector &ops, - const std::vector> &ops_wires, + const std::vector> &ops_wires, const std::vector &ops_adjoint, const std::vector> &ops_params) { - const size_t numOperations = ops.size(); + const std::size_t numOperations = ops.size(); PL_ABORT_IF( numOperations != ops_wires.size(), "Invalid arguments: number of operations, wires, inverses, and " @@ -157,9 +157,9 @@ template class StateVectorBase { * inverted. */ void applyOperations(const std::vector &ops, - const std::vector> &ops_wires, + const std::vector> &ops_wires, const std::vector &ops_adjoint) { - const size_t numOperations = ops.size(); + const std::size_t numOperations = ops.size(); PL_ABORT_IF_NOT( numOperations == ops_wires.size(), "Invalid arguments: number of operations, wires, and inverses " @@ -181,7 +181,7 @@ template class StateVectorBase { * @param adjoint Indicates whether to use adjoint of operator. */ inline auto applyGenerator(const std::string &opName, - const std::vector &wires, + const std::vector &wires, bool adjoint = false) -> PrecisionT { return static_cast(this)->applyGenerator(opName, wires, adjoint); @@ -196,7 +196,7 @@ template class StateVectorBase { * @param inverse Indicate whether inverse should be taken. */ inline void applyMatrix(const ComplexT *matrix, - const std::vector &wires, + const std::vector &wires, bool inverse = false) { return static_cast(this)->applyMatrix(matrix, wires, inverse); diff --git a/pennylane_lightning/core/src/simulators/base/tests/Test_StateVectorBase.cpp b/pennylane_lightning/core/src/simulators/base/tests/Test_StateVectorBase.cpp index e79cdad042..6388c13481 100644 --- a/pennylane_lightning/core/src/simulators/base/tests/Test_StateVectorBase.cpp +++ b/pennylane_lightning/core/src/simulators/base/tests/Test_StateVectorBase.cpp @@ -67,7 +67,7 @@ template void testStateVectorBase() { using StateVectorT = typename TypeList::Type; using ComplexT = typename StateVectorT::ComplexT; - const size_t num_qubits = 4; + const std::size_t num_qubits = 4; auto st_data = createZeroState(num_qubits); StateVectorT state_vector(st_data.data(), st_data.size()); @@ -94,7 +94,7 @@ template void testApplyOperations() { using PrecisionT = typename StateVectorT::PrecisionT; using ComplexT = typename StateVectorT::ComplexT; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; DYNAMIC_SECTION("Apply operations without parameters - " << StateVectorToName::name) { diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/MPIWorker.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/MPIWorker.hpp index eae1aa2413..6e524b61fe 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/MPIWorker.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/MPIWorker.hpp @@ -35,11 +35,11 @@ using SharedLocalStream = using SharedMPIWorker = std::shared_ptr< std::remove_pointer::type>; -inline size_t mebibyteToBytes(const size_t mebibytes) { - return mebibytes * size_t{1024 * 1024}; +inline std::size_t mebibyteToBytes(const std::size_t mebibytes) { + return mebibytes * std::size_t{1024 * 1024}; } -inline double bytesToMebibytes(const size_t bytes) { +inline double bytesToMebibytes(const std::size_t bytes) { return static_cast(bytes) / (1024.0 * 1024.0); } @@ -102,26 +102,27 @@ inline SharedLocalStream make_shared_local_stream() { template SharedMPIWorker make_shared_mpi_worker(custatevecHandle_t handle, MPIManager &mpi_manager, - const size_t mpi_buf_size, CFP_t *sv, - const size_t numLocalQubits, cudaStream_t localStream) { + const std::size_t mpi_buf_size, CFP_t *sv, + const std::size_t numLocalQubits, + cudaStream_t localStream) { custatevecSVSwapWorkerDescriptor_t svSegSwapWorker = nullptr; int nDevices_int = 0; PL_CUDA_IS_SUCCESS(cudaGetDeviceCount(&nDevices_int)); - size_t nDevices = static_cast(nDevices_int); + std::size_t nDevices = static_cast(nDevices_int); // Ensure the number of P2P devices is calculated based on the number of MPI // processes within the node nDevices = mpi_manager.getSizeNode() < nDevices ? mpi_manager.getSizeNode() : nDevices; - size_t nP2PDeviceBits = std::bit_width(nDevices) - 1; + std::size_t nP2PDeviceBits = std::bit_width(nDevices) - 1; - size_t p2pEnabled_local = 1; + std::size_t p2pEnabled_local = 1; // P2P access check if (nP2PDeviceBits != 0) { - size_t local_device_id = mpi_manager.getRank() % nDevices; + std::size_t local_device_id = mpi_manager.getRank() % nDevices; for (size_t devId = 0; devId < nDevices; ++devId) { if (devId != local_device_id) { @@ -137,7 +138,7 @@ make_shared_mpi_worker(custatevecHandle_t handle, MPIManager &mpi_manager, } auto isP2PEnabled = - mpi_manager.allreduce(p2pEnabled_local, "min"); + mpi_manager.allreduce(p2pEnabled_local, "min"); // P2PDeviceBits is set as 0 for all MPI processes if P2P access is not // supported by any pair of devices of any node if (!isP2PEnabled) { @@ -187,8 +188,8 @@ make_shared_mpi_worker(custatevecHandle_t handle, MPIManager &mpi_manager, std::vector subSVIndicesP2P; std::vector remoteEvents; - size_t extraWorkspaceSize = 0; - size_t minTransferWorkspaceSize = 0; + std::size_t extraWorkspaceSize = 0; + std::size_t minTransferWorkspaceSize = 0; PL_CUSTATEVEC_IS_SUCCESS(custatevecSVSwapWorkerCreate( /* custatevecHandle_t */ handle, @@ -199,8 +200,8 @@ make_shared_mpi_worker(custatevecHandle_t handle, MPIManager &mpi_manager, /* cudaEvent_t */ localEvent, /* cudaDataType_t */ svDataType, /* cudaStream_t */ localStream, - /* size_t* */ &extraWorkspaceSize, - /* size_t* */ &minTransferWorkspaceSize)); + /* std::size_t* */ &extraWorkspaceSize, + /* std::size_t* */ &minTransferWorkspaceSize)); PL_CUDA_IS_SUCCESS(cudaMalloc(&d_extraWorkspace, extraWorkspaceSize)); @@ -208,13 +209,13 @@ make_shared_mpi_worker(custatevecHandle_t handle, MPIManager &mpi_manager, /* custatevecHandle_t */ handle, /* custatevecSVSwapWorkerDescriptor_t */ svSegSwapWorker, /* void* */ d_extraWorkspace, - /* size_t */ extraWorkspaceSize)); + /* std::size_t */ extraWorkspaceSize)); - size_t transferWorkspaceSize; // In bytes and its value should be power - // of 2. + std::size_t transferWorkspaceSize; // In bytes and its value should be power + // of 2. if (mpi_buf_size == 0) { - transferWorkspaceSize = size_t{1} << numLocalQubits; + transferWorkspaceSize = std::size_t{1} << numLocalQubits; if constexpr (std::is_same_v || std::is_same_v) { transferWorkspaceSize = transferWorkspaceSize * sizeof(double) * 2; @@ -223,7 +224,7 @@ make_shared_mpi_worker(custatevecHandle_t handle, MPIManager &mpi_manager, } // With the default setting, transfer work space is limited to 64 MiB // based on the benchmark tests on the Perlmutter. - size_t buffer_limit = 64; + std::size_t buffer_limit = 64; double transferWorkspaceSizeInMiB = bytesToMebibytes(transferWorkspaceSize); if (transferWorkspaceSizeInMiB > static_cast(buffer_limit)) { @@ -241,7 +242,7 @@ make_shared_mpi_worker(custatevecHandle_t handle, MPIManager &mpi_manager, /* custatevecHandle_t */ handle, /* custatevecSVSwapWorkerDescriptor_t */ svSegSwapWorker, /* void* */ d_transferWorkspace, - /* size_t */ transferWorkspaceSize)); + /* std::size_t */ transferWorkspaceSize)); // LCOV_EXCL_START // Won't be covered by CI checks with 2 nvidia GPUs without nvlink @@ -262,13 +263,14 @@ make_shared_mpi_worker(custatevecHandle_t handle, MPIManager &mpi_manager, mpi_manager.Allgather( eventHandle, ipcEventHandles, sizeof(eventHandle)); // get remove device pointers and events - size_t nSubSVsP2P = size_t{1} << nP2PDeviceBits; - size_t p2pSubSVIndexBegin = + std::size_t nSubSVsP2P = std::size_t{1} << nP2PDeviceBits; + std::size_t p2pSubSVIndexBegin = (mpi_manager.getRank() / nSubSVsP2P) * nSubSVsP2P; - size_t p2pSubSVIndexEnd = p2pSubSVIndexBegin + nSubSVsP2P; + std::size_t p2pSubSVIndexEnd = p2pSubSVIndexBegin + nSubSVsP2P; for (size_t p2pSubSVIndex = p2pSubSVIndexBegin; p2pSubSVIndex < p2pSubSVIndexEnd; p2pSubSVIndex++) { - if (static_cast(mpi_manager.getRank()) == p2pSubSVIndex) + if (static_cast(mpi_manager.getRank()) == + p2pSubSVIndex) continue; // don't need local sub state vector pointer void *d_subSVP2P = nullptr; const auto &dstMemHandle = ipcMemHandles[p2pSubSVIndex]; diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaBase.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaBase.hpp index fba307ebed..d4bec462a6 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaBase.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaBase.hpp @@ -153,7 +153,8 @@ class StateVectorCudaBase : public StateVectorBase { * @param sv Complex data pointer to receive data from device. */ inline void CopyGpuDataToHost(std::complex *host_sv, - size_t length, bool async = false) const { + std::size_t length, + bool async = false) const { PL_ABORT_IF_NOT(BaseType::getLength() == length, "Sizes do not match for Host and GPU data"); data_buffer_->CopyGpuDataToHost(host_sv, length, async); @@ -202,13 +203,13 @@ class StateVectorCudaBase : public StateVectorBase { * */ void initSV(bool async = false) { - size_t index = 0; + std::size_t index = 0; const std::complex value(1, 0); static_cast(this)->setBasisState(value, index, async); }; protected: - using ParFunc = std::function &, bool, + using ParFunc = std::function &, bool, const std::vector &)>; using FMap = std::unordered_map; diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaMPI.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaMPI.hpp index 0496a8042e..21192a4064 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaMPI.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaMPI.hpp @@ -57,18 +57,18 @@ namespace Pennylane::LightningGPU { // declarations of external functions (defined in initSV.cu). extern void setStateVector_CUDA(cuComplex *sv, int &num_indices, cuComplex *value, int *indices, - size_t thread_per_block, + std::size_t thread_per_block, cudaStream_t stream_id); extern void setStateVector_CUDA(cuDoubleComplex *sv, long &num_indices, cuDoubleComplex *value, long *indices, - size_t thread_per_block, + std::size_t thread_per_block, cudaStream_t stream_id); extern void setBasisState_CUDA(cuComplex *sv, cuComplex &value, - const size_t index, bool async, + const std::size_t index, bool async, cudaStream_t stream_id); extern void setBasisState_CUDA(cuDoubleComplex *sv, cuDoubleComplex &value, - const size_t index, bool async, + const std::size_t index, bool async, cudaStream_t stream_id); /** @@ -83,8 +83,8 @@ class StateVectorCudaMPI final private: using BaseType = StateVectorCudaBase; - size_t numGlobalQubits_; - size_t numLocalQubits_; + std::size_t numGlobalQubits_; + std::size_t numLocalQubits_; MPIManager mpi_manager_; SharedCusvHandle handle_; @@ -106,8 +106,8 @@ class StateVectorCudaMPI final StateVectorCudaMPI() = delete; StateVectorCudaMPI(MPIManager mpi_manager, const DevTag &dev_tag, - size_t mpi_buf_size, size_t num_global_qubits, - size_t num_local_qubits) + std::size_t mpi_buf_size, std::size_t num_global_qubits, + std::size_t num_local_qubits) : StateVectorCudaBase>( num_local_qubits, dev_tag, true), numGlobalQubits_(num_global_qubits), @@ -124,8 +124,8 @@ class StateVectorCudaMPI final }; StateVectorCudaMPI(MPI_Comm mpi_communicator, const DevTag &dev_tag, - size_t mpi_buf_size, size_t num_global_qubits, - size_t num_local_qubits) + std::size_t mpi_buf_size, std::size_t num_global_qubits, + std::size_t num_local_qubits) : StateVectorCudaBase>( num_local_qubits, dev_tag, true), numGlobalQubits_(num_global_qubits), @@ -141,8 +141,9 @@ class StateVectorCudaMPI final mpi_manager_.Barrier(); }; - StateVectorCudaMPI(const DevTag &dev_tag, size_t mpi_buf_size, - size_t num_global_qubits, size_t num_local_qubits) + StateVectorCudaMPI(const DevTag &dev_tag, std::size_t mpi_buf_size, + std::size_t num_global_qubits, + std::size_t num_local_qubits) : StateVectorCudaBase>( num_local_qubits, dev_tag, true), numGlobalQubits_(num_global_qubits), @@ -158,8 +159,9 @@ class StateVectorCudaMPI final mpi_manager_.Barrier(); }; - StateVectorCudaMPI(const DevTag &dev_tag, size_t num_global_qubits, - size_t num_local_qubits, const CFP_t *gpu_data) + StateVectorCudaMPI(const DevTag &dev_tag, + std::size_t num_global_qubits, + std::size_t num_local_qubits, const CFP_t *gpu_data) : StateVectorCudaBase>( num_local_qubits, dev_tag, true), numGlobalQubits_(num_global_qubits), @@ -171,14 +173,15 @@ class StateVectorCudaMPI final handle_.get(), mpi_manager_, 0, BaseType::getData(), num_local_qubits, localStream_.get())), gate_cache_(true, dev_tag) { - size_t length = 1 << numLocalQubits_; + std::size_t length = 1 << numLocalQubits_; BaseType::CopyGpuDataToGpuIn(gpu_data, length, false); PL_CUDA_IS_SUCCESS(cudaDeviceSynchronize()) mpi_manager_.Barrier(); } - StateVectorCudaMPI(const DevTag &dev_tag, size_t num_global_qubits, - size_t num_local_qubits) + StateVectorCudaMPI(const DevTag &dev_tag, + std::size_t num_global_qubits, + std::size_t num_local_qubits) : StateVectorCudaBase>( num_local_qubits, dev_tag, true), numGlobalQubits_(num_global_qubits), @@ -226,19 +229,19 @@ class StateVectorCudaMPI final /** * @brief Get the total number of wires. */ - auto getTotalNumQubits() const -> size_t { + auto getTotalNumQubits() const -> std::size_t { return numGlobalQubits_ + numLocalQubits_; } /** * @brief Get the number of wires distributed across devices. */ - auto getNumGlobalQubits() const -> size_t { return numGlobalQubits_; } + auto getNumGlobalQubits() const -> std::size_t { return numGlobalQubits_; } /** * @brief Get the number of wires within the local devices. */ - auto getNumLocalQubits() const -> size_t { return numLocalQubits_; } + auto getNumLocalQubits() const -> std::size_t { return numLocalQubits_; } /** * @brief Get pointer to custatevecSVSwapWorkerDescriptor. @@ -255,14 +258,14 @@ class StateVectorCudaMPI final * @param index Index of the target element. * @param async Use an asynchronous memory copy. */ - void setBasisState(const std::complex &value, const size_t index, - const bool async = false) { - size_t rankId = index >> BaseType::getNumQubits(); - - size_t local_index = - static_cast(rankId * - std::pow(2.0, static_cast( - BaseType::getNumQubits()))) ^ + void setBasisState(const std::complex &value, + const std::size_t index, const bool async = false) { + std::size_t rankId = index >> BaseType::getNumQubits(); + + std::size_t local_index = + static_cast( + rankId * std::pow(2.0, static_cast( + BaseType::getNumQubits()))) ^ index; BaseType::getDataBuffer().zeroInit(); @@ -287,7 +290,7 @@ class StateVectorCudaMPI final * @param indices Pointer to indices of the target elements. * @param async Use an asynchronous memory copy. */ - template + template void setStateVector(const index_type num_indices, const std::complex *values, const index_type *indices, const bool async = false) { @@ -296,15 +299,15 @@ class StateVectorCudaMPI final std::vector indices_local; std::vector> values_local; - for (size_t i = 0; i < static_cast(num_indices); i++) { + for (size_t i = 0; i < static_cast(num_indices); i++) { int index = indices[i]; PL_ASSERT(index >= 0); - size_t rankId = - static_cast(index) >> BaseType::getNumQubits(); + std::size_t rankId = + static_cast(index) >> BaseType::getNumQubits(); if (rankId == mpi_manager_.getRank()) { int local_index = - static_cast( + static_cast( rankId * std::pow(2.0, static_cast( BaseType::getNumQubits()))) ^ index; @@ -349,7 +352,7 @@ class StateVectorCudaMPI final * @param matrix Matrix representation of gate. */ void applyOperation(const std::string &opName, - const std::vector &wires, bool adjoint, + const std::vector &wires, bool adjoint, const std::vector ¶ms, [[maybe_unused]] const std::vector &matrix) { std::vector matrix_cu(matrix.size()); @@ -374,7 +377,7 @@ class StateVectorCudaMPI final * @param gate_matrix Matrix representation of gate. */ void applyOperation( - const std::string &opName, const std::vector &wires, + const std::string &opName, const std::vector &wires, bool adjoint = false, const std::vector ¶ms = {0.0}, [[maybe_unused]] const std::vector &gate_matrix = {}) { const auto ctrl_offset = (BaseType::getCtrlMap().find(opName) != @@ -461,8 +464,8 @@ class StateVectorCudaMPI final * @param adjoint Indicates whether to use adjoint of gate. */ auto applyGenerator(const std::string &opName, - const std::vector &wires, bool adjoint = false) - -> PrecisionT { + const std::vector &wires, + bool adjoint = false) -> PrecisionT { auto it = generator_map_.find(opName); PL_ABORT_IF(it == generator_map_.end(), "Unsupported generator!"); return (it->second)(wires, adjoint); @@ -477,10 +480,11 @@ class StateVectorCudaMPI final * @param adjoint Indicate whether inverse should be taken. */ void applyMatrix(const std::complex *gate_matrix, - const std::vector &wires, bool adjoint = false) { + const std::vector &wires, + bool adjoint = false) { PL_ABORT_IF(wires.empty(), "Number of wires must be larger than 0"); const std::string opName = {}; - size_t n = size_t{1} << wires.size(); + std::size_t n = std::size_t{1} << wires.size(); const std::vector> matrix(gate_matrix, gate_matrix + n * n); std::vector matrix_cu(matrix.size()); @@ -501,7 +505,8 @@ class StateVectorCudaMPI final * @param adjoint Indicate whether inverse should be taken. */ void applyMatrix(const std::vector> &gate_matrix, - const std::vector &wires, bool adjoint = false) { + const std::vector &wires, + bool adjoint = false) { PL_ABORT_IF(gate_matrix.size() != Pennylane::Util::exp2(2 * wires.size()), "The size of matrix does not match with the given " @@ -772,7 +777,7 @@ class StateVectorCudaMPI final * @param wires Wires to apply operation. * @param adj Takes adjoint of operation if true. Defaults to false. */ - inline PrecisionT applyGeneratorRX(const std::vector &wires, + inline PrecisionT applyGeneratorRX(const std::vector &wires, bool adj = false) { applyPauliX(wires, adj); return -static_cast(0.5); @@ -785,7 +790,7 @@ class StateVectorCudaMPI final * @param wires Wires to apply operation. * @param adj Takes adjoint of operation if true. Defaults to false. */ - inline PrecisionT applyGeneratorRY(const std::vector &wires, + inline PrecisionT applyGeneratorRY(const std::vector &wires, bool adj = false) { applyPauliY(wires, adj); return -static_cast(0.5); @@ -798,7 +803,7 @@ class StateVectorCudaMPI final * @param wires Wires to apply operation. * @param adj Takes adjoint of operation if true. Defaults to false. */ - inline PrecisionT applyGeneratorRZ(const std::vector &wires, + inline PrecisionT applyGeneratorRZ(const std::vector &wires, bool adj = false) { applyPauliZ(wires, adj); return -static_cast(0.5); @@ -864,8 +869,9 @@ class StateVectorCudaMPI final * @param wires Wires to apply operation. * @param adj Takes adjoint of operation if true. Defaults to false. */ - inline PrecisionT applyGeneratorPhaseShift(const std::vector &wires, - bool adj = false) { + inline PrecisionT + applyGeneratorPhaseShift(const std::vector &wires, + bool adj = false) { applyOperation("P_11", wires, adj, {0.0}, cuGates::getP11_CU()); return static_cast(1.0); } @@ -877,11 +883,11 @@ class StateVectorCudaMPI final * @param wires Wires to apply operation. * @param adj Takes adjoint of operation if true. Defaults to false. */ - inline PrecisionT applyGeneratorCRX(const std::vector &wires, + inline PrecisionT applyGeneratorCRX(const std::vector &wires, bool adj = false) { applyOperation("P_11", {wires.front()}, adj, {0.0}, cuGates::getP11_CU()); - applyPauliX(std::vector{wires.back()}, adj); + applyPauliX(std::vector{wires.back()}, adj); return -static_cast(0.5); } @@ -892,11 +898,11 @@ class StateVectorCudaMPI final * @param wires Wires to apply operation. * @param adj Takes adjoint of operation if true. Defaults to false. */ - inline PrecisionT applyGeneratorCRY(const std::vector &wires, + inline PrecisionT applyGeneratorCRY(const std::vector &wires, bool adj = false) { applyOperation("P_11", {wires.front()}, adj, {0.0}, cuGates::getP11_CU()); - applyPauliY(std::vector{wires.back()}, adj); + applyPauliY(std::vector{wires.back()}, adj); return -static_cast(0.5); } @@ -907,11 +913,11 @@ class StateVectorCudaMPI final * @param wires Wires to apply operation. * @param adj Takes adjoint of operation if true. Defaults to false. */ - inline PrecisionT applyGeneratorCRZ(const std::vector &wires, + inline PrecisionT applyGeneratorCRZ(const std::vector &wires, bool adj = false) { applyOperation("P_11", {wires.front()}, adj, {0.0}, cuGates::getP11_CU()); - applyPauliZ(std::vector{wires.back()}, adj); + applyPauliZ(std::vector{wires.back()}, adj); return -static_cast(0.5); } @@ -923,7 +929,7 @@ class StateVectorCudaMPI final * @param adj Takes adjoint of operation if true. Defaults to false. */ inline PrecisionT - applyGeneratorControlledPhaseShift(const std::vector &wires, + applyGeneratorControlledPhaseShift(const std::vector &wires, bool adj = false) { applyOperation("P_1111", {wires}, adj, {0.0}, cuGates::getP1111_CU()); @@ -1077,7 +1083,8 @@ class StateVectorCudaMPI final * if does not exist. * @return auto Expectation value. */ - auto expval(const std::string &obsName, const std::vector &wires, + auto expval(const std::string &obsName, + const std::vector &wires, const std::vector ¶ms = {0.0}, const std::vector &gate_matrix = {}) { auto &&par = (params.empty()) ? std::vector{0.0} : params; @@ -1096,7 +1103,7 @@ class StateVectorCudaMPI final /** * @brief See `expval(std::vector &gate_matrix = {})` */ - auto expval(const std::vector &wires, + auto expval(const std::vector &wires, const std::vector> &gate_matrix) { std::vector matrix_cu(gate_matrix.size()); @@ -1112,7 +1119,8 @@ class StateVectorCudaMPI final // Wire order reversed to match expected custatevec wire ordering for // tensor observables. - auto &&local_wires = std::vector{wires.rbegin(), wires.rend()}; + auto &&local_wires = + std::vector{wires.rbegin(), wires.rend()}; PL_CUDA_IS_SUCCESS(cudaDeviceSynchronize()); auto expect_val = getExpectationValueDeviceMatrix(matrix_cu.data(), local_wires).x; @@ -1155,7 +1163,7 @@ class StateVectorCudaMPI final std::vector tgtsSwapStatus; std::vector> tgtswirePairs; // Local target wires (required by expvalOnPauliBasis) - std::vector> localTgts; + std::vector> localTgts; localTgts.reserve(tgts.size()); tgtsVecProcess(this->getNumLocalQubits(), this->getTotalNumQubits(), @@ -1172,7 +1180,7 @@ class StateVectorCudaMPI final if (isAllTargetsLocal) { expvalOnPauliBasis(pauli_words, localTgts, expect_local); } else { - size_t wirePairsIdx = 0; + std::size_t wirePairsIdx = 0; for (size_t i = 0; i < pauli_words.size(); i++) { if (tgtsSwapStatus[i] == WiresSwapStatus::UnSwappable) { auto opsNames = pauliStringToOpNames(pauli_words[i]); @@ -1182,7 +1190,7 @@ class StateVectorCudaMPI final this->getData()); for (size_t opsIdx = 0; opsIdx < tgts[i].size(); opsIdx++) { - std::vector wires = {tgts[i][opsIdx]}; + std::vector wires = {tgts[i][opsIdx]}; tmp.applyOperation({opsNames[opsIdx]}, {tgts[i][opsIdx]}, {false}); } @@ -1198,7 +1206,7 @@ class StateVectorCudaMPI final } else { std::vector pauli_words_idx( 1, std::string(pauli_words[i])); - std::vector> tgts_idx; + std::vector> tgts_idx; tgts_idx.push_back(localTgts[i]); std::vector expval_local(1); @@ -1227,10 +1235,10 @@ class StateVectorCudaMPI final } private: - using ParFunc = std::function &, bool, + using ParFunc = std::function &, bool, const std::vector &)>; using GeneratorFunc = - std::function &, bool)>; + std::function &, bool)>; using FMap = std::unordered_map; using GMap = std::unordered_map; @@ -1674,11 +1682,11 @@ class StateVectorCudaMPI final applyCuSVPauliGate(pauli_words, ctrlsInt, tgtsInt, param, use_adjoint); } else { - size_t counts_global_wires = + std::size_t counts_global_wires = std::count_if(statusWires.begin(), statusWires.begin() + this->getNumLocalQubits(), [](int i) { return i != WireStatus::Default; }); - size_t counts_local_wires = + std::size_t counts_local_wires = ctrlsInt.size() + tgtsInt.size() - counts_global_wires; PL_ABORT_IF( counts_global_wires > @@ -1722,7 +1730,7 @@ class StateVectorCudaMPI final const std::vector &tgts, bool use_adjoint = false) { void *extraWorkspace = nullptr; - size_t extraWorkspaceSizeInBytes = 0; + std::size_t extraWorkspaceSizeInBytes = 0; int nIndexBits = BaseType::getNumQubits(); cudaDataType_t data_type; @@ -1749,7 +1757,7 @@ class StateVectorCudaMPI final /* const uint32_t */ tgts.size(), /* const uint32_t */ ctrls.size(), /* custatevecComputeType_t */ compute_type, - /* size_t* */ &extraWorkspaceSizeInBytes)); + /* std::size_t* */ &extraWorkspaceSizeInBytes)); // allocate external workspace if necessary // LCOV_EXCL_START @@ -1776,7 +1784,7 @@ class StateVectorCudaMPI final /* const uint32_t */ ctrls.size(), /* custatevecComputeType_t */ compute_type, /* void* */ extraWorkspace, - /* size_t */ extraWorkspaceSizeInBytes)); + /* std::size_t */ extraWorkspaceSizeInBytes)); // LCOV_EXCL_START if (extraWorkspaceSizeInBytes) PL_CUDA_IS_SUCCESS(cudaFree(extraWorkspace)); @@ -1833,11 +1841,11 @@ class StateVectorCudaMPI final if (!StatusGlobalWires) { applyCuSVDeviceMatrixGate(matrix, ctrlsInt, tgtsInt, use_adjoint); } else { - size_t counts_global_wires = + std::size_t counts_global_wires = std::count_if(statusWires.begin(), statusWires.begin() + this->getNumLocalQubits(), [](int i) { return i != WireStatus::Default; }); - size_t counts_local_wires = + std::size_t counts_local_wires = ctrlsInt.size() + tgtsInt.size() - counts_global_wires; PL_ABORT_IF( counts_global_wires > @@ -1876,9 +1884,9 @@ class StateVectorCudaMPI final const std::vector &tgts, CFP_t &expect) { void *extraWorkspace = nullptr; - size_t extraWorkspaceSizeInBytes = 0; + std::size_t extraWorkspaceSizeInBytes = 0; - size_t nIndexBits = BaseType::getNumQubits(); + std::size_t nIndexBits = BaseType::getNumQubits(); cudaDataType_t data_type; custatevecComputeType_t compute_type; cudaDataType_t expectationDataType = CUDA_C_64F; @@ -1902,7 +1910,7 @@ class StateVectorCudaMPI final /* custatevecMatrixLayout_t */ CUSTATEVEC_MATRIX_LAYOUT_ROW, /* const uint32_t */ tgts.size(), /* custatevecComputeType_t */ compute_type, - /* size_t* */ &extraWorkspaceSizeInBytes)); + /* std::size_t* */ &extraWorkspaceSizeInBytes)); // LCOV_EXCL_START if (extraWorkspaceSizeInBytes > 0) { PL_CUDA_IS_SUCCESS( @@ -1927,7 +1935,7 @@ class StateVectorCudaMPI final /* const uint32_t */ tgts.size(), /* custatevecComputeType_t */ compute_type, /* void* */ extraWorkspace, - /* size_t */ extraWorkspaceSizeInBytes)); + /* std::size_t */ extraWorkspaceSizeInBytes)); // LCOV_EXCL_START if (extraWorkspaceSizeInBytes) { PL_CUDA_IS_SUCCESS(cudaFree(extraWorkspace)); @@ -2046,7 +2054,7 @@ class StateVectorCudaMPI final // // the main loop of index bit swaps // - constexpr size_t nLoops = 2; + constexpr std::size_t nLoops = 2; for (size_t loop = 0; loop < nLoops; loop++) { for (int swapBatchIndex = 0; swapBatchIndex < static_cast(nSwapBatches); diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaManaged.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaManaged.hpp index 4e7c749ba6..153dd6a782 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaManaged.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaManaged.hpp @@ -53,36 +53,37 @@ namespace Pennylane::LightningGPU { // declarations of external functions (defined in initSV.cu). extern void setStateVector_CUDA(cuComplex *sv, int &num_indices, cuComplex *value, int *indices, - size_t thread_per_block, + std::size_t thread_per_block, cudaStream_t stream_id); extern void setStateVector_CUDA(cuDoubleComplex *sv, long &num_indices, cuDoubleComplex *value, long *indices, - size_t thread_per_block, + std::size_t thread_per_block, cudaStream_t stream_id); extern void setBasisState_CUDA(cuComplex *sv, cuComplex &value, - const size_t index, bool async, + const std::size_t index, bool async, cudaStream_t stream_id); extern void setBasisState_CUDA(cuDoubleComplex *sv, cuDoubleComplex &value, - const size_t index, bool async, + const std::size_t index, bool async, cudaStream_t stream_id); -extern void globalPhaseStateVector_CUDA(cuComplex *sv, size_t num_sv, +extern void globalPhaseStateVector_CUDA(cuComplex *sv, std::size_t num_sv, cuComplex phase, - size_t thread_per_block, + std::size_t thread_per_block, cudaStream_t stream_id); -extern void globalPhaseStateVector_CUDA(cuDoubleComplex *sv, size_t num_sv, +extern void globalPhaseStateVector_CUDA(cuDoubleComplex *sv, std::size_t num_sv, cuDoubleComplex phase, - size_t thread_per_block, + std::size_t thread_per_block, cudaStream_t stream_id); -extern void cGlobalPhaseStateVector_CUDA(cuComplex *sv, size_t num_sv, +extern void cGlobalPhaseStateVector_CUDA(cuComplex *sv, std::size_t num_sv, bool adjoint, cuComplex *phase, - size_t thread_per_block, + std::size_t thread_per_block, cudaStream_t stream_id); -extern void cGlobalPhaseStateVector_CUDA(cuDoubleComplex *sv, size_t num_sv, - bool adjoint, cuDoubleComplex *phase, - size_t thread_per_block, +extern void cGlobalPhaseStateVector_CUDA(cuDoubleComplex *sv, + std::size_t num_sv, bool adjoint, + cuDoubleComplex *phase, + std::size_t thread_per_block, cudaStream_t stream_id); /** @@ -113,7 +114,7 @@ class StateVectorCudaManaged cublascaller_(make_shared_cublas_caller()), gate_cache_(true){}; StateVectorCudaManaged( - size_t num_qubits, const DevTag &dev_tag, bool alloc = true, + std::size_t num_qubits, const DevTag &dev_tag, bool alloc = true, SharedCusvHandle cusvhandle_in = make_shared_cusv_handle(), SharedCublasCaller cublascaller_in = make_shared_cublas_caller(), SharedCusparseHandle cusparsehandle_in = make_shared_cusparse_handle()) @@ -126,13 +127,13 @@ class StateVectorCudaManaged BaseType::initSV(); }; - StateVectorCudaManaged(const CFP_t *gpu_data, size_t length) + StateVectorCudaManaged(const CFP_t *gpu_data, std::size_t length) : StateVectorCudaManaged(Pennylane::Util::log2(length)) { BaseType::CopyGpuDataToGpuIn(gpu_data, length, false); } StateVectorCudaManaged( - const CFP_t *gpu_data, size_t length, DevTag dev_tag, + const CFP_t *gpu_data, std::size_t length, DevTag dev_tag, SharedCusvHandle handle_in = make_shared_cusv_handle(), SharedCublasCaller cublascaller_in = make_shared_cublas_caller(), SharedCusparseHandle cusparsehandle_in = make_shared_cusparse_handle()) @@ -144,12 +145,13 @@ class StateVectorCudaManaged } StateVectorCudaManaged(const std::complex *host_data, - size_t length) + std::size_t length) : StateVectorCudaManaged(Pennylane::Util::log2(length)) { BaseType::CopyHostDataToGpu(host_data, length, false); } - StateVectorCudaManaged(std::complex *host_data, size_t length) + StateVectorCudaManaged(std::complex *host_data, + std::size_t length) : StateVectorCudaManaged(Pennylane::Util::log2(length)) { BaseType::CopyHostDataToGpu(host_data, length, false); } @@ -172,8 +174,8 @@ class StateVectorCudaManaged * @param index Index of the target element. * @param async Use an asynchronous memory copy. */ - void setBasisState(const std::complex &value, const size_t index, - const bool async = false) { + void setBasisState(const std::complex &value, + const std::size_t index, const bool async = false) { BaseType::getDataBuffer().zeroInit(); CFP_t value_cu = cuUtil::complexToCu>(value); @@ -192,7 +194,7 @@ class StateVectorCudaManaged * @param indices Pointer to indices of the target elements. * @param async Use an asynchronous memory copy. */ - template + template void setStateVector(const index_type num_indices, const std::complex *values, const index_type *indices, const bool async = false) { @@ -266,7 +268,7 @@ class StateVectorCudaManaged * @param matrix Gate data (in row-major format). */ void applyOperation(const std::string &opName, - const std::vector &wires, bool adjoint, + const std::vector &wires, bool adjoint, const std::vector ¶ms, const std::vector &matrix) { std::vector matrix_cu(matrix.size()); @@ -291,7 +293,8 @@ class StateVectorCudaManaged * @param gate_matrix Gate data (in row-major format). */ void applyOperation(const std::string &opName, - const std::vector &wires, bool adjoint = false, + const std::vector &wires, + bool adjoint = false, const std::vector ¶ms = {0.0}, const std::vector &gate_matrix = {}) { const auto ctrl_offset = (BaseType::getCtrlMap().find(opName) != @@ -381,8 +384,8 @@ class StateVectorCudaManaged * @param adjoint Indicates whether to use adjoint of gate. */ auto applyGenerator(const std::string &opName, - const std::vector &wires, bool adjoint = false) - -> PrecisionT { + const std::vector &wires, + bool adjoint = false) -> PrecisionT { auto it = generator_map_.find(opName); PL_ABORT_IF(it == generator_map_.end(), "Unsupported generator!"); return (it->second)(wires, adjoint); @@ -397,10 +400,11 @@ class StateVectorCudaManaged * @param adjoint Indicate whether inverse should be taken. */ void applyMatrix(const std::complex *gate_matrix, - const std::vector &wires, bool adjoint = false) { + const std::vector &wires, + bool adjoint = false) { PL_ABORT_IF(wires.empty(), "Number of wires must be larger than 0"); const std::string opName = {}; - size_t n = size_t{1} << wires.size(); + std::size_t n = std::size_t{1} << wires.size(); const std::vector> matrix(gate_matrix, gate_matrix + n * n); std::vector matrix_cu(matrix.size()); @@ -421,7 +425,8 @@ class StateVectorCudaManaged * @param adjoint Indicate whether inverse should be taken. */ void applyMatrix(const std::vector> &gate_matrix, - const std::vector &wires, bool adjoint = false) { + const std::vector &wires, + bool adjoint = false) { PL_ABORT_IF(gate_matrix.size() != Pennylane::Util::exp2(2 * wires.size()), "The size of matrix does not match with the given " @@ -692,9 +697,9 @@ class StateVectorCudaManaged * @param wires Wires to apply operation. * @param adj Takes adjoint of operation if true. Defaults to false. */ - inline PrecisionT - applyGeneratorGlobalPhase([[maybe_unused]] const std::vector &wires, - [[maybe_unused]] bool adj = false) { + inline PrecisionT applyGeneratorGlobalPhase( + [[maybe_unused]] const std::vector &wires, + [[maybe_unused]] bool adj = false) { return static_cast(-1.0); } @@ -706,7 +711,7 @@ class StateVectorCudaManaged * @param wires Wires to apply operation. * @param adj Takes adjoint of operation if true. Defaults to false. */ - inline PrecisionT applyGeneratorRX(const std::vector &wires, + inline PrecisionT applyGeneratorRX(const std::vector &wires, bool adj = false) { applyPauliX(wires, adj); return -static_cast(0.5); @@ -719,7 +724,7 @@ class StateVectorCudaManaged * @param wires Wires to apply operation. * @param adj Takes adjoint of operation if true. Defaults to false. */ - inline PrecisionT applyGeneratorRY(const std::vector &wires, + inline PrecisionT applyGeneratorRY(const std::vector &wires, bool adj = false) { applyPauliY(wires, adj); return -static_cast(0.5); @@ -732,7 +737,7 @@ class StateVectorCudaManaged * @param wires Wires to apply operation. * @param adj Takes adjoint of operation if true. Defaults to false. */ - inline PrecisionT applyGeneratorRZ(const std::vector &wires, + inline PrecisionT applyGeneratorRZ(const std::vector &wires, bool adj = false) { applyPauliZ(wires, adj); return -static_cast(0.5); @@ -798,8 +803,9 @@ class StateVectorCudaManaged * @param wires Wires to apply operation. * @param adj Takes adjoint of operation if true. Defaults to false. */ - inline PrecisionT applyGeneratorPhaseShift(const std::vector &wires, - bool adj = false) { + inline PrecisionT + applyGeneratorPhaseShift(const std::vector &wires, + bool adj = false) { applyOperation("P_11", wires, adj, {0.0}, cuGates::getP11_CU()); return static_cast(1.0); } @@ -811,11 +817,11 @@ class StateVectorCudaManaged * @param wires Wires to apply operation. * @param adj Takes adjoint of operation if true. Defaults to false. */ - inline PrecisionT applyGeneratorCRX(const std::vector &wires, + inline PrecisionT applyGeneratorCRX(const std::vector &wires, bool adj = false) { applyOperation("P_11", {wires.front()}, adj, {0.0}, cuGates::getP11_CU()); - applyPauliX(std::vector{wires.back()}, adj); + applyPauliX(std::vector{wires.back()}, adj); return -static_cast(0.5); } @@ -826,11 +832,11 @@ class StateVectorCudaManaged * @param wires Wires to apply operation. * @param adj Takes adjoint of operation if true. Defaults to false. */ - inline PrecisionT applyGeneratorCRY(const std::vector &wires, + inline PrecisionT applyGeneratorCRY(const std::vector &wires, bool adj = false) { applyOperation("P_11", {wires.front()}, adj, {0.0}, cuGates::getP11_CU()); - applyPauliY(std::vector{wires.back()}, adj); + applyPauliY(std::vector{wires.back()}, adj); return -static_cast(0.5); } @@ -841,11 +847,11 @@ class StateVectorCudaManaged * @param wires Wires to apply operation. * @param adj Takes adjoint of operation if true. Defaults to false. */ - inline PrecisionT applyGeneratorCRZ(const std::vector &wires, + inline PrecisionT applyGeneratorCRZ(const std::vector &wires, bool adj = false) { applyOperation("P_11", {wires.front()}, adj, {0.0}, cuGates::getP11_CU()); - applyPauliZ(std::vector{wires.back()}, adj); + applyPauliZ(std::vector{wires.back()}, adj); return -static_cast(0.5); } @@ -857,7 +863,7 @@ class StateVectorCudaManaged * @param adj Takes adjoint of operation if true. Defaults to false. */ inline PrecisionT - applyGeneratorControlledPhaseShift(const std::vector &wires, + applyGeneratorControlledPhaseShift(const std::vector &wires, bool adj = false) { applyOperation("P_1111", {wires}, adj, {0.0}, cuGates::getP1111_CU()); @@ -1005,10 +1011,10 @@ class StateVectorCudaManaged mutable SharedCusparseHandle cusparsehandle_; // This member is mutable to allow lazy initialization. GateCache gate_cache_; - using ParFunc = std::function &, bool, + using ParFunc = std::function &, bool, const std::vector &)>; using GeneratorFunc = - std::function &, bool)>; + std::function &, bool)>; using FMap = std::unordered_map; using GMap = std::unordered_map; @@ -1372,7 +1378,7 @@ class StateVectorCudaManaged const std::vector &tgts, bool use_adjoint = false) { void *extraWorkspace = nullptr; - size_t extraWorkspaceSizeInBytes = 0; + std::size_t extraWorkspaceSizeInBytes = 0; int nIndexBits = BaseType::getNumQubits(); std::vector ctrlsInt(ctrls.size()); @@ -1411,7 +1417,7 @@ class StateVectorCudaManaged /* const uint32_t */ tgts.size(), /* const uint32_t */ ctrls.size(), /* custatevecComputeType_t */ compute_type, - /* size_t* */ &extraWorkspaceSizeInBytes)); + /* std::size_t* */ &extraWorkspaceSizeInBytes)); // allocate external workspace if necessary // LCOV_EXCL_START @@ -1438,7 +1444,7 @@ class StateVectorCudaManaged /* const uint32_t */ ctrls.size(), /* custatevecComputeType_t */ compute_type, /* void* */ extraWorkspace, - /* size_t */ extraWorkspaceSizeInBytes)); + /* std::size_t */ extraWorkspaceSizeInBytes)); // LCOV_EXCL_START if (extraWorkspaceSizeInBytes) PL_CUDA_IS_SUCCESS(cudaFree(extraWorkspace)); diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/AdjointJacobianGPU.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/AdjointJacobianGPU.hpp index 350fab6f6c..05adeba8e5 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/AdjointJacobianGPU.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/AdjointJacobianGPU.hpp @@ -77,8 +77,8 @@ class AdjointJacobian final inline void updateJacobian(const std::vector &sv1s, const StateVectorT &sv2, std::span &jac, - PrecisionT scaling_coeff, size_t num_observables, - size_t param_index, size_t tp_size, + PrecisionT scaling_coeff, std::size_t num_observables, + std::size_t param_index, std::size_t tp_size, DataBuffer &device_buffer_jac_single_param, std::vector &host_buffer_jac_single_param) { host_buffer_jac_single_param.clear(); @@ -99,7 +99,7 @@ class AdjointJacobian final host_buffer_jac_single_param.data(), host_buffer_jac_single_param.size(), false); for (size_t obs_idx = 0; obs_idx < num_observables; obs_idx++) { - size_t idx = param_index + obs_idx * tp_size; + std::size_t idx = param_index + obs_idx * tp_size; jac[idx] = -2 * scaling_coeff * host_buffer_jac_single_param[obs_idx].y; } @@ -130,8 +130,9 @@ class AdjointJacobian final const auto &obs = jd.getObservables(); - const std::vector &trainableParams = jd.getTrainableParams(); - const size_t tp_size = trainableParams.size(); + const std::vector &trainableParams = + jd.getTrainableParams(); + const std::size_t tp_size = trainableParams.size(); // Create a vector of threads for separate GPU executions using namespace std::chrono_literals; @@ -243,11 +244,12 @@ class AdjointJacobian final const std::vector &ops_name = ops.getOpsName(); const auto &obs = jd.getObservables(); - const size_t num_observables = obs.size(); + const std::size_t num_observables = obs.size(); - const std::vector &trainableParams = jd.getTrainableParams(); - const size_t tp_size = trainableParams.size(); - const size_t num_param_ops = ops.getNumParOps(); + const std::vector &trainableParams = + jd.getTrainableParams(); + const std::size_t tp_size = trainableParams.size(); + const std::size_t num_param_ops = ops.getNumParOps(); PL_ABORT_IF_NOT( jac.size() == tp_size * num_observables, @@ -256,8 +258,8 @@ class AdjointJacobian final "observables provided."); // Track positions within par and non-par operations - size_t trainableParamNumber = tp_size - 1; - size_t current_param_idx = + std::size_t trainableParamNumber = tp_size - 1; + std::size_t current_param_idx = num_param_ops - 1; // total number of parametric ops auto tp_it = trainableParams.rbegin(); const auto tp_rend = trainableParams.rend(); @@ -336,7 +338,7 @@ class AdjointJacobian final current_param_idx--; } this->applyOperationsAdj(H_lambda, ops, - static_cast(op_idx)); + static_cast(op_idx)); } } }; diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/AdjointJacobianGPUMPI.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/AdjointJacobianGPUMPI.hpp index 16ee33b327..de5ebe1908 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/AdjointJacobianGPUMPI.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/AdjointJacobianGPUMPI.hpp @@ -79,8 +79,8 @@ class AdjointJacobianMPI final */ inline void updateJacobian(const StateVectorT &sv1, const StateVectorT &sv2, std::span &jac, - PrecisionT scaling_coeff, size_t obs_idx, - size_t param_index, size_t tp_size) { + PrecisionT scaling_coeff, std::size_t obs_idx, + std::size_t param_index, std::size_t tp_size) { PL_ABORT_IF_NOT(sv1.getDataBuffer().getDevTag().getDeviceID() == sv2.getDataBuffer().getDevTag().getDeviceID(), "Data exists on different GPUs. Aborting."); @@ -92,7 +92,7 @@ class AdjointJacobianMPI final auto jac_single_param = sv2.getMPIManager().template allreduce(result, "sum"); - size_t idx = param_index + obs_idx * tp_size; + std::size_t idx = param_index + obs_idx * tp_size; jac[idx] = -2 * scaling_coeff * jac_single_param.y; } @@ -120,11 +120,12 @@ class AdjointJacobianMPI final const std::vector &ops_name = ops.getOpsName(); const auto &obs = jd.getObservables(); - const size_t num_observables = obs.size(); + const std::size_t num_observables = obs.size(); - const std::vector &trainableParams = jd.getTrainableParams(); - const size_t tp_size = trainableParams.size(); - const size_t num_param_ops = ops.getNumParOps(); + const std::vector &trainableParams = + jd.getTrainableParams(); + const std::size_t tp_size = trainableParams.size(); + const std::size_t num_param_ops = ops.getNumParOps(); PL_ABORT_IF_NOT( jac.size() == tp_size * num_observables, @@ -158,9 +159,9 @@ class AdjointJacobianMPI final BaseType::applyObservable(H_lambda, *obs[obs_idx]); - size_t trainableParamNumber = tp_size - 1; + std::size_t trainableParamNumber = tp_size - 1; // Track positions within par and non-par operations - size_t current_param_idx = + std::size_t current_param_idx = num_param_ops - 1; // total number of parametric ops auto tp_it = trainableParams.rbegin(); const auto tp_rend = trainableParams.rend(); @@ -198,7 +199,7 @@ class AdjointJacobianMPI final current_param_idx--; } BaseType::applyOperationAdj(H_lambda, ops, - static_cast(op_idx)); + static_cast(op_idx)); } } } @@ -236,11 +237,12 @@ class AdjointJacobianMPI final const std::vector &ops_name = ops.getOpsName(); const auto &obs = jd.getObservables(); - const size_t num_observables = obs.size(); + const std::size_t num_observables = obs.size(); - const std::vector &trainableParams = jd.getTrainableParams(); - const size_t tp_size = trainableParams.size(); - const size_t num_param_ops = ops.getNumParOps(); + const std::vector &trainableParams = + jd.getTrainableParams(); + const std::size_t tp_size = trainableParams.size(); + const std::size_t num_param_ops = ops.getNumParOps(); PL_ABORT_IF_NOT( jac.size() == tp_size * num_observables, @@ -249,8 +251,8 @@ class AdjointJacobianMPI final "observables provided."); // Track positions within par and non-par operations - size_t trainableParamNumber = tp_size - 1; - size_t current_param_idx = + std::size_t trainableParamNumber = tp_size - 1; + std::size_t current_param_idx = num_param_ops - 1; // total number of parametric ops auto tp_it = trainableParams.rbegin(); const auto tp_rend = trainableParams.rend(); diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/JacobianDataMPI.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/JacobianDataMPI.hpp index d1f86bfc7c..6e3d5c492b 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/JacobianDataMPI.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/JacobianDataMPI.hpp @@ -29,8 +29,8 @@ template class JacobianDataMPI final : public JacobianData { private: using BaseType = JacobianData; - const size_t numGlobalQubits_; - const size_t numLocalQubits_; + const std::size_t numGlobalQubits_; + const std::size_t numLocalQubits_; MPIManager mpi_manager_; const DevTag dev_tag_; @@ -62,7 +62,7 @@ class JacobianDataMPI final : public JacobianData { */ JacobianDataMPI(size_t num_params, const StateVectorT &sv, std::vector>> obs, - OpsData ops, std::vector trainP) + OpsData ops, std::vector trainP) : JacobianData(num_params, sv.getLength(), sv.getData(), obs, ops, trainP), numGlobalQubits_(sv.getNumGlobalQubits()), @@ -85,11 +85,11 @@ class JacobianDataMPI final : public JacobianData { /** * @brief Get the number of wires distributed across devices. */ - auto getNumGlobalQubits() const -> size_t { return numGlobalQubits_; } + auto getNumGlobalQubits() const -> std::size_t { return numGlobalQubits_; } /** * @brief Get the number of wires within the local devices. */ - auto getNumLocalQubits() const -> size_t { return numLocalQubits_; } + auto getNumLocalQubits() const -> std::size_t { return numLocalQubits_; } }; } // namespace Pennylane::Algorithms diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/tests/Test_AdjointJacobianGPU.cpp b/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/tests/Test_AdjointJacobianGPU.cpp index 29441b1474..30109d64e2 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/tests/Test_AdjointJacobianGPU.cpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/tests/Test_AdjointJacobianGPU.cpp @@ -37,12 +37,12 @@ TEST_CASE("AdjointJacobianGPU::AdjointJacobianGPU Op=RX, Obs=Z", using StateVectorT = StateVectorCudaManaged; AdjointJacobian adj; std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - const std::vector tp{0}; + const std::vector tp{0}; { - const size_t num_qubits = 1; - const size_t num_obs = 1; + const std::size_t num_qubits = 1; + const std::size_t num_obs = 1; const auto obs = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); std::vector jacobian(num_obs * tp.size(), 0); @@ -67,13 +67,13 @@ TEST_CASE("AdjointJacobianGPU::adjointJacobian Op=RY, Obs=X", using StateVectorT = StateVectorCudaManaged; AdjointJacobian adj; std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - const std::vector tp{0}; + const std::vector tp{0}; { - const size_t num_qubits = 1; - const size_t num_obs = 1; + const std::size_t num_qubits = 1; + const std::size_t num_obs = 1; const auto obs = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); std::vector jacobian(num_obs * tp.size(), 0); for (const auto &p : param) { @@ -102,10 +102,10 @@ TEST_CASE("AdjointJacobianGPU::adjointJacobian Op=[QubitStateVector, " using ComplexT = StateVectorT::ComplexT; AdjointJacobian adj; std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector tp{0}; + std::vector tp{0}; { - const size_t num_qubits = 2; - const size_t num_obs = 2; + const std::size_t num_qubits = 2; + const std::size_t num_obs = 2; std::vector jacobian(num_obs * tp.size(), 0); std::vector jacobian_ref(num_obs * tp.size(), 0); std::vector matrix = { @@ -115,9 +115,9 @@ TEST_CASE("AdjointJacobianGPU::adjointJacobian Op=[QubitStateVector, " psi.initSV(); const auto obs1 = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); const auto obs2 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto ops = OpsData({test_ops}, {{param[0]}}, {{0}}, {false}, {matrix}); @@ -143,19 +143,19 @@ TEST_CASE("AdjointJacobianGPU::adjointJacobian Op=RX, Obs=[Z,Z]", using StateVectorT = StateVectorCudaManaged; AdjointJacobian adj; std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector tp{0}; + std::vector tp{0}; { - const size_t num_qubits = 2; - const size_t num_obs = 2; + const std::size_t num_qubits = 2; + const std::size_t num_obs = 2; std::vector jacobian(num_obs * tp.size(), 0); StateVectorT psi(num_qubits); psi.initSV(); const auto obs1 = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); const auto obs2 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto ops = OpsData({"RX"}, {{param[0]}}, {{0}}, {false}); @@ -176,21 +176,21 @@ TEST_CASE("AdjointJacobianGPU::AdjointJacobianGPU Op=[RX,RX,RX], Obs=[Z,Z,Z]", using StateVectorT = StateVectorCudaManaged; AdjointJacobian adj; std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector tp{0, 1, 2}; + std::vector tp{0, 1, 2}; { - const size_t num_qubits = 3; - const size_t num_obs = 3; + const std::size_t num_qubits = 3; + const std::size_t num_obs = 3; std::vector jacobian(num_obs * tp.size(), 0); StateVectorT psi(num_qubits); psi.initSV(); const auto obs1 = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); const auto obs2 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); const auto obs3 = std::make_shared>( - "PauliZ", std::vector{2}); + "PauliZ", std::vector{2}); auto ops = OpsData( {"RX", "RX", "RX"}, {{param[0]}, {param[1]}, {param[2]}}, @@ -218,21 +218,21 @@ TEST_CASE("AdjointJacobianGPU::AdjointJacobianGPU Op=[RX,RX,RX], Obs=[Z,Z,Z]," using StateVectorT = StateVectorCudaManaged; AdjointJacobian adj; std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector tp{0, 2}; + std::vector tp{0, 2}; { - const size_t num_qubits = 3; - const size_t num_obs = 3; + const std::size_t num_qubits = 3; + const std::size_t num_obs = 3; std::vector jacobian(num_obs * tp.size(), 0); StateVectorT psi(num_qubits); psi.initSV(); const auto obs1 = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); const auto obs2 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); const auto obs3 = std::make_shared>( - "PauliZ", std::vector{2}); + "PauliZ", std::vector{2}); auto ops = OpsData( {"RX", "RX", "RX"}, {{param[0]}, {param[1]}, {param[2]}}, {{0}, {1}, {2}}, {false, false, false}); @@ -258,22 +258,22 @@ TEST_CASE("Algorithms::adjointJacobian Op=[RX,RX,RX], Obs=[ZZZ]", using StateVectorT = StateVectorCudaManaged; AdjointJacobian adj; std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector tp{0, 1, 2}; + std::vector tp{0, 1, 2}; { - const size_t num_qubits = 3; - const size_t num_obs = 1; + const std::size_t num_qubits = 3; + const std::size_t num_obs = 1; std::vector jacobian(num_obs * tp.size(), 0); StateVectorT psi(num_qubits); psi.initSV(); const auto obs = std::make_shared>( - std::make_shared>("PauliZ", - std::vector{0}), - std::make_shared>("PauliZ", - std::vector{1}), - std::make_shared>("PauliZ", - std::vector{2})); + std::make_shared>( + "PauliZ", std::vector{0}), + std::make_shared>( + "PauliZ", std::vector{1}), + std::make_shared>( + "PauliZ", std::vector{2})); auto ops = OpsData( {"RX", "RX", "RX"}, {{param[0]}, {param[1]}, {param[2]}}, {{0}, {1}, {2}}, {false, false, false}); @@ -297,22 +297,22 @@ TEST_CASE("AdjointJacobianGPU::adjointJacobian Op=Mixed, Obs=[XXX]", using StateVectorT = StateVectorCudaManaged; AdjointJacobian adj; std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector tp{0, 1, 2, 3, 4, 5}; + std::vector tp{0, 1, 2, 3, 4, 5}; { - const size_t num_qubits = 3; - const size_t num_obs = 1; + const std::size_t num_qubits = 3; + const std::size_t num_obs = 1; std::vector jacobian(num_obs * tp.size(), 0); StateVectorT psi(num_qubits); psi.initSV(); const auto obs = std::make_shared>( - std::make_shared>("PauliX", - std::vector{0}), - std::make_shared>("PauliX", - std::vector{1}), - std::make_shared>("PauliX", - std::vector{2})); + std::make_shared>( + "PauliX", std::vector{0}), + std::make_shared>( + "PauliX", std::vector{1}), + std::make_shared>( + "PauliX", std::vector{2})); auto ops = OpsData( {"RZ", "RY", "RZ", "CNOT", "CNOT", "RZ", "RY", "RZ"}, {{param[0]}, @@ -349,9 +349,9 @@ TEST_CASE("AdjointJacobianGPU::adjointJacobian Decomposed Rot gate, non " using StateVectorT = StateVectorCudaManaged; AdjointJacobian adj; std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - const std::vector tp{0, 1, 2}; + const std::vector tp{0, 1, 2}; { - const size_t num_obs = 1; + const std::size_t num_obs = 1; const auto thetas = Pennylane::Util::linspace(-2 * M_PI, 2 * M_PI, 7); std::unordered_map> expec_results{ @@ -377,7 +377,7 @@ TEST_CASE("AdjointJacobianGPU::adjointJacobian Decomposed Rot gate, non " StateVectorT psi(new_data.data(), new_data.size()); const auto obs = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); auto ops = OpsData( {"RZ", "RY", "RZ"}, @@ -406,8 +406,8 @@ TEST_CASE("AdjointJacobianGPU::adjointJacobian Mixed Ops, Obs and TParams", AdjointJacobian adj; std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; { - const std::vector tp{1, 2, 3}; - const size_t num_obs = 1; + const std::vector tp{1, 2, 3}; + const std::size_t num_obs = 1; const auto thetas = Pennylane::Util::linspace(-2 * M_PI, 2 * M_PI, 8); @@ -424,10 +424,10 @@ TEST_CASE("AdjointJacobianGPU::adjointJacobian Mixed Ops, Obs and TParams", StateVectorT psi(new_data.data(), new_data.size()); const auto obs = std::make_shared>( - std::make_shared>("PauliX", - std::vector{0}), - std::make_shared>("PauliZ", - std::vector{1})); + std::make_shared>( + "PauliX", std::vector{0}), + std::make_shared>( + "PauliZ", std::vector{1})); auto ops = OpsData({"Hadamard", "RX", "CNOT", "RZ", "RY", "RZ", "RZ", "RY", "RZ", "RZ", "RY", "CNOT"}, @@ -477,8 +477,8 @@ TEST_CASE("AdjointJacobianGPU::batchAdjointJacobian Mixed Ops, Obs and TParams", AdjointJacobian adj; std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; { - const std::vector tp{1, 2, 3}; - const size_t num_obs = 1; + const std::vector tp{1, 2, 3}; + const std::size_t num_obs = 1; const auto thetas = Pennylane::Util::linspace(-2 * M_PI, 2 * M_PI, 8); @@ -495,10 +495,10 @@ TEST_CASE("AdjointJacobianGPU::batchAdjointJacobian Mixed Ops, Obs and TParams", StateVectorT psi(cdata.data(), cdata.size()); const auto obs = std::make_shared>( - std::make_shared>("PauliX", - std::vector{0}), - std::make_shared>("PauliZ", - std::vector{1})); + std::make_shared>( + "PauliX", std::vector{0}), + std::make_shared>( + "PauliZ", std::vector{1})); auto ops = OpsData({"Hadamard", "RX", "CNOT", "RZ", "RY", "RZ", "RZ", "RY", "RZ", "RZ", "RY", "CNOT"}, @@ -546,19 +546,19 @@ TEST_CASE("Algorithms::adjointJacobian Op=RX, Obs=Ham[Z0+Z1]", "[Algorithms]") { using StateVectorT = StateVectorCudaManaged; AdjointJacobian adj; std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector tp{0}; + std::vector tp{0}; { - const size_t num_qubits = 2; - const size_t num_obs = 1; + const std::size_t num_qubits = 2; + const std::size_t num_obs = 1; std::vector jacobian(num_obs * tp.size(), 0); StateVectorT psi(num_qubits); psi.initSV(); const auto obs1 = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); const auto obs2 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto ham = Hamiltonian::create({0.3, 0.7}, {obs1, obs2}); @@ -581,21 +581,21 @@ TEST_CASE( using StateVectorT = StateVectorCudaManaged; AdjointJacobian adj; std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector tp{0, 2}; + std::vector tp{0, 2}; { - const size_t num_qubits = 3; - const size_t num_obs = 1; + const std::size_t num_qubits = 3; + const std::size_t num_obs = 1; std::vector jacobian(num_obs * tp.size(), 0); StateVectorT psi(num_qubits); psi.initSV(); auto obs1 = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); auto obs2 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto obs3 = std::make_shared>( - "PauliZ", std::vector{2}); + "PauliZ", std::vector{2}); auto ham = Hamiltonian::create({0.47, 0.32, 0.96}, {obs1, obs2, obs3}); @@ -621,10 +621,10 @@ TEST_CASE("AdjointJacobianGPU::AdjointJacobianGPU Test HermitianObs", using StateVectorT = StateVectorCudaManaged; AdjointJacobian adj; std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector tp{0, 2}; + std::vector tp{0, 2}; { - const size_t num_qubits = 3; - const size_t num_obs = 1; + const std::size_t num_qubits = 3; + const std::size_t num_obs = 1; std::vector jacobian1(num_obs * tp.size(), 0); std::vector jacobian2(num_obs * tp.size(), 0); @@ -633,14 +633,14 @@ TEST_CASE("AdjointJacobianGPU::AdjointJacobianGPU Test HermitianObs", psi.initSV(); auto obs1 = std::make_shared>( - std::make_shared>("PauliZ", - std::vector{0}), - std::make_shared>("PauliZ", - std::vector{1})); + std::make_shared>( + "PauliZ", std::vector{0}), + std::make_shared>( + "PauliZ", std::vector{1})); auto obs2 = std::make_shared>( std::vector>{1, 0, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1}, - std::vector{0, 1}); + std::vector{0, 1}); auto ops = OpsData( {"RX", "RX", "RX"}, {{param[0]}, {param[1]}, {param[2]}}, diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/tests/mpi/Test_AdjointJacobianGPUMPI.cpp b/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/tests/mpi/Test_AdjointJacobianGPUMPI.cpp index 17465e770d..8cf5a487ee 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/tests/mpi/Test_AdjointJacobianGPUMPI.cpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/tests/mpi/Test_AdjointJacobianGPUMPI.cpp @@ -61,14 +61,14 @@ TEST_CASE("AdjointJacobianGPUMPI::adjointJacobianMPI Op=RX, Obs=[Z,Z]", AdjointJacobianMPI adj; std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector tp{0}; + std::vector tp{0}; - const size_t num_qubits = 2; - const size_t num_obs = 2; + const std::size_t num_qubits = 2; + const std::size_t num_obs = 2; std::vector jacobian(num_obs * tp.size(), 0); std::vector jacobian_serial(num_obs * tp.size(), 0); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; int nGlobalIndexBits = std::bit_width(static_cast(mpi_manager.getSize())) - 1; @@ -87,9 +87,9 @@ TEST_CASE("AdjointJacobianGPUMPI::adjointJacobianMPI Op=RX, Obs=[Z,Z]", psi.initSV(); const auto obs1 = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); const auto obs2 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto ops = OpsData({"RX"}, {{param[0]}}, {{0}}, {false}); @@ -120,15 +120,15 @@ TEST_CASE("AdjointJacobianGPUMPI::adjointJacobianMPI Op=[QubitStateVector, " AdjointJacobianMPI adj; std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector tp{0}; + std::vector tp{0}; - const size_t num_qubits = 4; - const size_t num_obs = 2; + const std::size_t num_qubits = 4; + const std::size_t num_obs = 2; std::vector jacobian(num_obs * tp.size(), 0); std::vector jacobian_serial(num_obs * tp.size(), 0); std::vector jacobian_ref(num_obs * tp.size(), 0); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; int nGlobalIndexBits = std::bit_width(static_cast(mpi_manager.getSize())) - 1; @@ -147,9 +147,9 @@ TEST_CASE("AdjointJacobianGPUMPI::adjointJacobianMPI Op=[QubitStateVector, " psi.initSV(); const auto obs1 = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); const auto obs2 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto ops = OpsData({test_ops}, {{param[0]}}, {{0}}, {false}); @@ -174,17 +174,17 @@ TEST_CASE( using StateVectorT = StateVectorCudaMPI; AdjointJacobianMPI adj; std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector tp{0, 1, 2}; + std::vector tp{0, 1, 2}; - const size_t num_qubits = 3; - const size_t num_obs = 3; + const std::size_t num_qubits = 3; + const std::size_t num_obs = 3; std::vector jacobian(num_obs * tp.size(), 0); std::vector jacobian_serial(num_obs * tp.size(), 0); MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; int nGlobalIndexBits = std::bit_width(static_cast(mpi_manager.getSize())) - 1; @@ -203,11 +203,11 @@ TEST_CASE( psi.initSV(); const auto obs1 = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); const auto obs2 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); const auto obs3 = std::make_shared>( - "PauliZ", std::vector{2}); + "PauliZ", std::vector{2}); auto ops = OpsData( {"RX", "RX", "RX"}, {{param[0]}, {param[1]}, {param[2]}}, @@ -243,17 +243,17 @@ TEST_CASE( using StateVectorT = StateVectorCudaMPI; AdjointJacobianMPI adj; std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector tp{0, 2}; + std::vector tp{0, 2}; - const size_t num_qubits = 3; - const size_t num_obs = 3; + const std::size_t num_qubits = 3; + const std::size_t num_obs = 3; std::vector jacobian(num_obs * tp.size(), 0); std::vector jacobian_serial(num_obs * tp.size(), 0); MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; int nGlobalIndexBits = std::bit_width(static_cast(mpi_manager.getSize())) - 1; @@ -272,11 +272,11 @@ TEST_CASE( psi.initSV(); const auto obs1 = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); const auto obs2 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); const auto obs3 = std::make_shared>( - "PauliZ", std::vector{2}); + "PauliZ", std::vector{2}); auto ops = OpsData( {"RX", "RX", "RX"}, {{param[0]}, {param[1]}, {param[2]}}, {{0}, {1}, {2}}, {false, false, false}); @@ -308,17 +308,17 @@ TEST_CASE("AdjointJacobianGPUMPI::adjointJacobian Op=[RX,RX,RX], Obs=[ZZZ]", using StateVectorT = StateVectorCudaMPI; AdjointJacobianMPI adj; std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector tp{0, 1, 2}; + std::vector tp{0, 1, 2}; - const size_t num_qubits = 3; - const size_t num_obs = 1; + const std::size_t num_qubits = 3; + const std::size_t num_obs = 1; std::vector jacobian(num_obs * tp.size(), 0); std::vector jacobian_serial(num_obs * tp.size(), 0); MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; int nGlobalIndexBits = std::bit_width(static_cast(mpi_manager.getSize())) - 1; @@ -337,12 +337,12 @@ TEST_CASE("AdjointJacobianGPUMPI::adjointJacobian Op=[RX,RX,RX], Obs=[ZZZ]", psi.initSV(); const auto obs = std::make_shared>( - std::make_shared>("PauliZ", - std::vector{0}), - std::make_shared>("PauliZ", - std::vector{1}), std::make_shared>( - "PauliZ", std::vector{2})); + "PauliZ", std::vector{0}), + std::make_shared>( + "PauliZ", std::vector{1}), + std::make_shared>( + "PauliZ", std::vector{2})); auto ops = OpsData( {"RX", "RX", "RX"}, {{param[0]}, {param[1]}, {param[2]}}, {{0}, {1}, {2}}, {false, false, false}); @@ -371,17 +371,17 @@ TEST_CASE("AdjointJacobianGPUMPI::adjointJacobian Op=Mixed, Obs=[XXX]", using StateVectorT = StateVectorCudaMPI; AdjointJacobianMPI adj; std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector tp{0, 1, 2, 3, 4, 5}; + std::vector tp{0, 1, 2, 3, 4, 5}; - const size_t num_qubits = 3; - const size_t num_obs = 1; + const std::size_t num_qubits = 3; + const std::size_t num_obs = 1; std::vector jacobian(num_obs * tp.size(), 0); std::vector jacobian_serial(num_obs * tp.size(), 0); MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; int nGlobalIndexBits = std::bit_width(static_cast(mpi_manager.getSize())) - 1; @@ -400,12 +400,12 @@ TEST_CASE("AdjointJacobianGPUMPI::adjointJacobian Op=Mixed, Obs=[XXX]", psi.initSV(); const auto obs = std::make_shared>( - std::make_shared>("PauliX", - std::vector{0}), - std::make_shared>("PauliX", - std::vector{1}), std::make_shared>( - "PauliX", std::vector{2})); + "PauliX", std::vector{0}), + std::make_shared>( + "PauliX", std::vector{1}), + std::make_shared>( + "PauliX", std::vector{2})); auto ops = OpsData( {"RZ", "RY", "RZ", "CNOT", "CNOT", "RZ", "RY", "RZ"}, {{param[0]}, @@ -451,17 +451,17 @@ TEST_CASE("AdjointJacobianGPU::AdjointJacobianGPUMPI Op=[RX,RX,RX], " using StateVectorT = StateVectorCudaMPI; AdjointJacobianMPI adj; std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector tp{0, 2}; + std::vector tp{0, 2}; - const size_t num_qubits = 3; - const size_t num_obs = 1; + const std::size_t num_qubits = 3; + const std::size_t num_obs = 1; std::vector jacobian(num_obs * tp.size(), 0); std::vector jacobian_serial(num_obs * tp.size(), 0); MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; int nGlobalIndexBits = std::bit_width(static_cast(mpi_manager.getSize())) - 1; @@ -481,11 +481,11 @@ TEST_CASE("AdjointJacobianGPU::AdjointJacobianGPUMPI Op=[RX,RX,RX], " psi.initSV(); auto obs1 = std::make_shared>( - "PauliZ", std::vector{0}); + "PauliZ", std::vector{0}); auto obs2 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto obs3 = std::make_shared>( - "PauliZ", std::vector{2}); + "PauliZ", std::vector{2}); auto ham = HamiltonianMPI::create({0.47, 0.32, 0.96}, {obs1, obs2, obs3}); @@ -517,10 +517,10 @@ TEST_CASE("AdjointJacobianGPU::AdjointJacobianGPU Test HermitianObs", using StateVectorT = StateVectorCudaMPI; AdjointJacobianMPI adj; std::vector param{-M_PI / 7, M_PI / 5, 2 * M_PI / 3}; - std::vector tp{0, 2}; + std::vector tp{0, 2}; - const size_t num_qubits = 3; - const size_t num_obs = 1; + const std::size_t num_qubits = 3; + const std::size_t num_obs = 1; std::vector jacobian1(num_obs * tp.size(), 0); std::vector jacobian2(num_obs * tp.size(), 0); @@ -531,7 +531,7 @@ TEST_CASE("AdjointJacobianGPU::AdjointJacobianGPU Test HermitianObs", MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; int nGlobalIndexBits = std::bit_width(static_cast(mpi_manager.getSize())) - 1; @@ -551,14 +551,14 @@ TEST_CASE("AdjointJacobianGPU::AdjointJacobianGPU Test HermitianObs", psi.initSV(); auto obs1 = std::make_shared>( - std::make_shared>("PauliZ", - std::vector{0}), std::make_shared>( - "PauliZ", std::vector{1})); + "PauliZ", std::vector{0}), + std::make_shared>( + "PauliZ", std::vector{1})); auto obs2 = std::make_shared>( std::vector>{1, 0, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1}, - std::vector{0, 1}); + std::vector{0, 1}); auto ops = OpsData( {"RX", "RX", "RX"}, {{param[0]}, {param[1]}, {param[2]}}, diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/bindings/LGPUBindings.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/bindings/LGPUBindings.hpp index b02fd555b3..3ba2c5adec 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/bindings/LGPUBindings.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/bindings/LGPUBindings.hpp @@ -82,7 +82,8 @@ void registerBackendClassSpecificBindings(PyClass &pyclass) { })) .def( "setBasisState", - [](StateVectorT &sv, const size_t index, const bool use_async) { + [](StateVectorT &sv, const std::size_t index, + const bool use_async) { const std::complex value(1, 0); sv.setBasisState(value, index, use_async); }, @@ -110,7 +111,7 @@ void registerBackendClassSpecificBindings(PyClass &pyclass) { }, "Synchronize data from another GPU device to current device.") .def("DeviceToHost", - py::overload_cast *, size_t, bool>( + py::overload_cast *, std::size_t, bool>( &StateVectorT::CopyGpuDataToHost, py::const_), "Synchronize data from the GPU device to host.") .def( @@ -125,8 +126,8 @@ void registerBackendClassSpecificBindings(PyClass &pyclass) { }, "Synchronize data from the GPU device to host.") .def("HostToDevice", - py::overload_cast *, size_t, bool>( - &StateVectorT::CopyHostDataToGpu), + py::overload_cast *, std::size_t, + bool>(&StateVectorT::CopyHostDataToGpu), "Synchronize data from the host device to GPU.") .def("HostToDevice", py::overload_cast> &, @@ -139,7 +140,7 @@ void registerBackendClassSpecificBindings(PyClass &pyclass) { const auto *data_ptr = static_cast *>(numpyArrayInfo.ptr); const auto length = - static_cast(numpyArrayInfo.shape[0]); + static_cast(numpyArrayInfo.shape[0]); if (length) { gpu_sv.CopyHostDataToGpu(data_ptr, length, async); } @@ -154,7 +155,7 @@ void registerBackendClassSpecificBindings(PyClass &pyclass) { .def( "apply", [](StateVectorT &sv, const std::string &str, - const std::vector &wires, bool inv, + const std::vector &wires, bool inv, const std::vector> ¶ms, const np_arr_c &gate_matrix) { const auto m_buffer = gate_matrix.request(); @@ -204,7 +205,7 @@ void registerBackendSpecificMeasurements(PyClass &pyclass) { pyclass .def("expval", static_cast::*)( - const std::string &, const std::vector &)>( + const std::string &, const std::vector &)>( &Measurements::expval), "Expected value of an operation by name.") .def( @@ -227,7 +228,7 @@ void registerBackendSpecificMeasurements(PyClass &pyclass) { "expval", [](Measurements &M, const std::vector &pauli_words, - const std::vector> &target_wires, + const std::vector> &target_wires, const np_arr_c &coeffs) { return M.expval(pauli_words, target_wires, static_cast(coeffs.request().ptr)); @@ -236,7 +237,7 @@ void registerBackendSpecificMeasurements(PyClass &pyclass) { .def( "expval", [](Measurements &M, const np_arr_c &matrix, - const std::vector &wires) { + const std::vector &wires) { const std::size_t matrix_size = exp2(2 * wires.size()); auto matrix_data = static_cast(matrix.request().ptr); @@ -247,12 +248,12 @@ void registerBackendSpecificMeasurements(PyClass &pyclass) { "Expected value of a Hermitian observable.") .def("var", [](Measurements &M, const std::string &operation, - const std::vector &wires) { + const std::vector &wires) { return M.var(operation, wires); }) .def("var", static_cast::*)( - const std::string &, const std::vector &)>( + const std::string &, const std::vector &)>( &Measurements::var), "Variance of an operation by name.") .def( diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/bindings/LGPUBindingsMPI.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/bindings/LGPUBindingsMPI.hpp index 1ca4670fe7..620fd93868 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/bindings/LGPUBindingsMPI.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/bindings/LGPUBindingsMPI.hpp @@ -86,7 +86,8 @@ void registerBackendClassSpecificBindingsMPI(PyClass &pyclass) { })) // qubits, device .def( "setBasisState", - [](StateVectorT &sv, const size_t index, const bool use_async) { + [](StateVectorT &sv, const std::size_t index, + const bool use_async) { const std::complex value(1, 0); sv.setBasisState(value, index, use_async); }, @@ -114,7 +115,7 @@ void registerBackendClassSpecificBindingsMPI(PyClass &pyclass) { }, "Synchronize data from another GPU device to current device.") .def("DeviceToHost", - py::overload_cast *, size_t, bool>( + py::overload_cast *, std::size_t, bool>( &StateVectorT::CopyGpuDataToHost, py::const_), "Synchronize data from the GPU device to host.") .def( @@ -129,8 +130,8 @@ void registerBackendClassSpecificBindingsMPI(PyClass &pyclass) { }, "Synchronize data from the GPU device to host.") .def("HostToDevice", - py::overload_cast *, size_t, bool>( - &StateVectorT::CopyHostDataToGpu), + py::overload_cast *, std::size_t, + bool>(&StateVectorT::CopyHostDataToGpu), "Synchronize data from the host device to GPU.") .def("HostToDevice", py::overload_cast> &, @@ -143,7 +144,7 @@ void registerBackendClassSpecificBindingsMPI(PyClass &pyclass) { const auto *data_ptr = static_cast *>(numpyArrayInfo.ptr); const auto length = - static_cast(numpyArrayInfo.shape[0]); + static_cast(numpyArrayInfo.shape[0]); if (length) { gpu_sv.CopyHostDataToGpu(data_ptr, length, async); } @@ -158,7 +159,7 @@ void registerBackendClassSpecificBindingsMPI(PyClass &pyclass) { .def( "apply", [](StateVectorT &sv, const std::string &str, - const std::vector &wires, bool inv, + const std::vector &wires, bool inv, [[maybe_unused]] const std::vector> ¶ms, [[maybe_unused]] const np_arr_c &gate_matrix) { const auto m_buffer = gate_matrix.request(); @@ -202,7 +203,7 @@ void registerBackendSpecificMeasurementsMPI(PyClass &pyclass) { pyclass .def("expval", static_cast::*)( - const std::string &, const std::vector &)>( + const std::string &, const std::vector &)>( &MeasurementsMPI::expval), "Expected value of an operation by name.") .def( @@ -222,7 +223,7 @@ void registerBackendSpecificMeasurementsMPI(PyClass &pyclass) { "expval", [](MeasurementsMPI &M, const std::vector &pauli_words, - const std::vector> &target_wires, + const std::vector> &target_wires, const np_arr_c &coeffs) { return M.expval(pauli_words, target_wires, static_cast(coeffs.request().ptr)); @@ -231,7 +232,7 @@ void registerBackendSpecificMeasurementsMPI(PyClass &pyclass) { .def( "expval", [](MeasurementsMPI &M, const np_arr_c &matrix, - const std::vector &wires) { + const std::vector &wires) { const std::size_t matrix_size = exp2(2 * wires.size()); auto matrix_data = static_cast(matrix.request().ptr); @@ -242,12 +243,12 @@ void registerBackendSpecificMeasurementsMPI(PyClass &pyclass) { "Expected value of a Hermitian observable.") .def("var", [](MeasurementsMPI &M, const std::string &operation, - const std::vector &wires) { + const std::vector &wires) { return M.var(operation, wires); }) .def("var", static_cast::*)( - const std::string &, const std::vector &)>( + const std::string &, const std::vector &)>( &MeasurementsMPI::var), "Variance of an operation by name.") .def( diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/gates/tests/Test_StateVectorCudaManaged_Generators.cpp b/pennylane_lightning/core/src/simulators/lightning_gpu/gates/tests/Test_StateVectorCudaManaged_Generators.cpp index 239dbd8269..edbb825d05 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/gates/tests/Test_StateVectorCudaManaged_Generators.cpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/gates/tests/Test_StateVectorCudaManaged_Generators.cpp @@ -1652,7 +1652,7 @@ TEMPLATE_TEST_CASE("StateVectorCudaManaged::applyGeneratorGlobalPhase", const std::string gate_name = "GlobalPhase"; { using ComplexT = StateVectorCudaManaged::ComplexT; - const size_t num_qubits = 4; + const std::size_t num_qubits = 4; const TestType ep = 1e-3; const TestType EP = 1e-4; diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/gates/tests/Test_StateVectorCudaManaged_NonParam.cpp b/pennylane_lightning/core/src/simulators/lightning_gpu/gates/tests/Test_StateVectorCudaManaged_NonParam.cpp index f96f4e1642..55f2710653 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/gates/tests/Test_StateVectorCudaManaged_NonParam.cpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/gates/tests/Test_StateVectorCudaManaged_NonParam.cpp @@ -1134,7 +1134,7 @@ TEMPLATE_TEST_CASE("StateVectorCudaManaged::SetStateVectorwith_thread_setting", init_state[0], init_state[2], init_state[4], init_state[6]}; // default setting of the number of threads in a block is 256. - const size_t threads_per_block = 1024; + const std::size_t threads_per_block = 1024; sv.template setStateVector( values.size(), values.data(), indices.data(), false); @@ -1165,7 +1165,7 @@ TEMPLATE_TEST_CASE("StateVectorCudaManaged::SetIthStates", StateVectorCudaManaged sv{num_qubits}; sv.CopyHostDataToGpu(init_state.data(), init_state.size()); - size_t index = 0; + std::size_t index = 0; std::complex values = init_state[1]; sv.setBasisState(values, index, false); diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/gates/tests/Test_StateVectorCudaManaged_Param.cpp b/pennylane_lightning/core/src/simulators/lightning_gpu/gates/tests/Test_StateVectorCudaManaged_Param.cpp index 0340ae9656..3938dd6168 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/gates/tests/Test_StateVectorCudaManaged_Param.cpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/gates/tests/Test_StateVectorCudaManaged_Param.cpp @@ -41,7 +41,7 @@ using namespace Pennylane::LightningGPU; TEMPLATE_TEST_CASE("LightningGPU:applyOperation", "[LightningGPU_Param]", double) { - const size_t num_qubits = 1; + const std::size_t num_qubits = 1; StateVectorCudaManaged sv{num_qubits}; sv.initSV(); @@ -54,7 +54,7 @@ TEMPLATE_TEST_CASE("LightningGPU:applyOperation", "[LightningGPU_Param]", TEMPLATE_TEST_CASE("LightningGPU::applyRX", "[LightningGPU_Param]", double) { using cp_t = std::complex; - const size_t num_qubits = 1; + const std::size_t num_qubits = 1; StateVectorCudaManaged sv{num_qubits}; sv.initSV(); @@ -186,7 +186,7 @@ TEMPLATE_TEST_CASE("LightningGPU::applyRY", "[LightningGPU_Param]", float, TEMPLATE_TEST_CASE("LightningGPU::applyRZ", "[LightningGPU_Param]", float, double) { using cp_t = std::complex; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; StateVectorCudaManaged sv{num_qubits}; sv.initSV(); @@ -248,7 +248,7 @@ TEMPLATE_TEST_CASE("LightningGPU::applyRZ", "[LightningGPU_Param]", float, TEMPLATE_TEST_CASE("LightningGPU::applyPhaseShift", "[LightningGPU_Param]", float, double) { using cp_t = std::complex; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; StateVectorCudaManaged sv{num_qubits}; sv.initSV(); @@ -311,7 +311,7 @@ TEMPLATE_TEST_CASE("LightningGPU::applyPhaseShift", "[LightningGPU_Param]", TEMPLATE_TEST_CASE("LightningGPU::applyControlledPhaseShift", "[LightningGPU_Param]", float, double) { using cp_t = std::complex; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; StateVectorCudaManaged sv{num_qubits}; sv.initSV(); @@ -362,7 +362,7 @@ TEMPLATE_TEST_CASE("LightningGPU::applyRot", "[LightningGPU_Param]", float, const bool adjoint = GENERATE(true, false); using cp_t = std::complex; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; const std::vector> angles{ std::vector{0.3, 0.8, 2.4}, @@ -420,7 +420,7 @@ TEMPLATE_TEST_CASE("LightningGPU::applyCRot", "[LightningGPU_Param]", float, const bool adjoint = GENERATE(true, false); using cp_t = std::complex; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; StateVectorCudaManaged sv{num_qubits}; sv.initSV(); @@ -487,7 +487,7 @@ TEMPLATE_TEST_CASE("LightningGPU::applyCRot", "[LightningGPU_Param]", float, TEMPLATE_TEST_CASE("LightningGPU::applyIsingXX", "[LightningGPU_Param]", float, double) { using cp_t = std::complex; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; StateVectorCudaManaged sv{num_qubits}; sv.initSV(); @@ -640,7 +640,7 @@ TEMPLATE_TEST_CASE("LightningGPU::applyIsingXY", "[LightningGPU_Param]", float, TEMPLATE_TEST_CASE("LightningGPU::applyIsingYY", "[LightningGPU_Param]", float, double) { using cp_t = std::complex; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; StateVectorCudaManaged sv{num_qubits}; sv.initSV(); @@ -735,7 +735,7 @@ TEMPLATE_TEST_CASE("LightningGPU::applyIsingYY", "[LightningGPU_Param]", float, TEMPLATE_TEST_CASE("LightningGPU::applyIsingZZ", "[LightningGPU_Param]", float, double) { using cp_t = std::complex; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; StateVectorCudaManaged sv{num_qubits}; sv.initSV(); @@ -986,7 +986,7 @@ TEMPLATE_TEST_CASE("LightningGPU::applyCRZ", "[LightningGPU_Param]", float, TEMPLATE_TEST_CASE("LightningGPU::applySingleExcitation", "[LightningGPU_Param]", float, double) { using cp_t = std::complex; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; StateVectorCudaManaged sv{num_qubits}; sv.initSV(); @@ -1033,7 +1033,7 @@ TEMPLATE_TEST_CASE("LightningGPU::applySingleExcitation", TEMPLATE_TEST_CASE("LightningGPU::applySingleExcitationMinus", "[LightningGPU_Param]", float, double) { using cp_t = std::complex; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; StateVectorCudaManaged sv{num_qubits}; sv.initSV(); @@ -1114,7 +1114,7 @@ TEMPLATE_TEST_CASE("LightningGPU::applySingleExcitationMinus", TEMPLATE_TEST_CASE("LightningGPU::applySingleExcitationPlus", "[LightningGPU_Param]", float, double) { using cp_t = std::complex; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; StateVectorCudaManaged sv{num_qubits}; sv.initSV(); @@ -1195,7 +1195,7 @@ TEMPLATE_TEST_CASE("LightningGPU::applySingleExcitationPlus", TEMPLATE_TEST_CASE("LightningGPU::applyDoubleExcitation", "[LightningGPU_Param]", float, double) { using cp_t = std::complex; - const size_t num_qubits = 4; + const std::size_t num_qubits = 4; StateVectorCudaManaged sv{num_qubits}; sv.initSV(); @@ -1234,7 +1234,7 @@ TEMPLATE_TEST_CASE("LightningGPU::applyDoubleExcitation", TEMPLATE_TEST_CASE("LightningGPU::applyDoubleExcitationMinus", "[LightningGPU_Param]", float, double) { using cp_t = std::complex; - const size_t num_qubits = 4; + const std::size_t num_qubits = 4; StateVectorCudaManaged sv{num_qubits}; sv.initSV(); @@ -1291,7 +1291,7 @@ TEMPLATE_TEST_CASE("LightningGPU::applyDoubleExcitationMinus", TEMPLATE_TEST_CASE("LightningGPU::applyDoubleExcitationPlus", "[LightningGPU_Param]", float, double) { using cp_t = std::complex; - const size_t num_qubits = 4; + const std::size_t num_qubits = 4; StateVectorCudaManaged sv{num_qubits}; sv.initSV(); @@ -1348,7 +1348,7 @@ TEMPLATE_TEST_CASE("LightningGPU::applyDoubleExcitationPlus", TEMPLATE_TEST_CASE("LightningGPU::applyMultiRZ", "[LightningGPU_Param]", float, double) { using cp_t = std::complex; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; StateVectorCudaManaged sv{num_qubits}; sv.initSV(); @@ -1426,7 +1426,7 @@ TEMPLATE_TEST_CASE("LightningGPU::applyMultiRZ", "[LightningGPU_Param]", float, TEMPLATE_TEST_CASE("LightningGPU::applyOperation 1 wire", "[LightningGPU_Param]", float, double) { using cp_t = StateVectorCudaManaged::CFP_t; - const size_t num_qubits = 5; + const std::size_t num_qubits = 5; // Note: gates are defined as right-to-left order @@ -1554,7 +1554,7 @@ TEMPLATE_TEST_CASE("LightningGPU::applyOperation 1 wire", TEMPLATE_TEST_CASE("LightningGPU::applyOperation multiple wires", "[LightningGPU_Param]", float, double) { using cp_t = StateVectorCudaManaged::CFP_t; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; StateVectorCudaManaged sv_init{num_qubits}; sv_init.initSV(); @@ -1585,9 +1585,9 @@ TEMPLATE_TEST_CASE("StateVectorCudaManaged::applyGlobalPhase", "[StateVectorCudaManaged_Param]", double) { using ComplexT = StateVectorCudaManaged::ComplexT; std::mt19937_64 re{1337}; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; const bool inverse = GENERATE(false, true); - const size_t index = GENERATE(0, 1, 2); + const std::size_t index = GENERATE(0, 1, 2); const TestType param = 0.234; const ComplexT phase = std::exp(ComplexT{0, (inverse) ? param : -param}); @@ -1607,9 +1607,9 @@ TEMPLATE_TEST_CASE("StateVectorCudaManaged::applyControlledGlobalPhase", "[StateVectorCudaManaged_Param]", double) { using ComplexT = StateVectorCudaManaged::ComplexT; std::mt19937_64 re{1337}; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; const bool inverse = GENERATE(false, true); - const size_t index = GENERATE(0, 1, 2); + const std::size_t index = GENERATE(0, 1, 2); /* The `phase` array contains the diagonal entries of the controlled-phase operator. It can be created in Python using the following command diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/gates/tests/mpi/Test_StateVectorCudaMPI_Generators.cpp b/pennylane_lightning/core/src/simulators/lightning_gpu/gates/tests/mpi/Test_StateVectorCudaMPI_Generators.cpp index f09377ffc3..0352b8dbd4 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/gates/tests/mpi/Test_StateVectorCudaMPI_Generators.cpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/gates/tests/mpi/Test_StateVectorCudaMPI_Generators.cpp @@ -65,12 +65,13 @@ using namespace Pennylane::LightningGPU::MPI; using PrecisionT = TestType; \ MPIManager mpi_manager(MPI_COMM_WORLD); \ REQUIRE(mpi_manager.getSize() == 2); \ - size_t mpi_buffersize = 1; \ - size_t nGlobalIndexBits = \ - std::bit_width(static_cast(mpi_manager.getSize())) - 1; \ - size_t nLocalIndexBits = (NUM_QUBITS)-nGlobalIndexBits; \ - size_t subSvLength = 1 << nLocalIndexBits; \ - size_t svLength = 1 << (NUM_QUBITS); \ + std::size_t mpi_buffersize = 1; \ + std::size_t nGlobalIndexBits = \ + std::bit_width(static_cast(mpi_manager.getSize())) - \ + 1; \ + std::size_t nLocalIndexBits = (NUM_QUBITS)-nGlobalIndexBits; \ + std::size_t subSvLength = 1 << nLocalIndexBits; \ + std::size_t svLength = 1 << (NUM_QUBITS); \ mpi_manager.Barrier(); \ std::vector expected_sv(svLength); \ std::vector local_state(subSvLength); \ diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/gates/tests/mpi/Test_StateVectorCudaMPI_NonParam.cpp b/pennylane_lightning/core/src/simulators/lightning_gpu/gates/tests/mpi/Test_StateVectorCudaMPI_NonParam.cpp index 7bbd54f761..3531de47c4 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/gates/tests/mpi/Test_StateVectorCudaMPI_NonParam.cpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/gates/tests/mpi/Test_StateVectorCudaMPI_NonParam.cpp @@ -79,8 +79,8 @@ TEMPLATE_TEST_CASE("StateVectorCudaMPI::StateVectorCudaMPI", std::size_t, std::size_t, std::size_t>::value); } - SECTION( - "StateVectorCudaMPI {DevTag, std::size_t, size_t, CFP_t}") { + SECTION("StateVectorCudaMPI {DevTag, std::size_t, std::size_t, " + "CFP_t}") { if (std::is_same_v) { REQUIRE(std::is_constructible, DevTag, std::size_t, std::size_t, @@ -105,12 +105,12 @@ TEMPLATE_TEST_CASE("StateVectorCudaMPI::SetStateVector", MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; - size_t subSvLength = 1 << nLocalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t subSvLength = 1 << nLocalIndexBits; mpi_manager.Barrier(); std::vector init_state(Pennylane::Util::exp2(num_qubits)); @@ -182,12 +182,12 @@ TEMPLATE_TEST_CASE("StateVectorCudaMPI::SetIthStates", MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; - size_t subSvLength = 1 << nLocalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t subSvLength = 1 << nLocalIndexBits; mpi_manager.Barrier(); int index; @@ -237,12 +237,13 @@ TEMPLATE_TEST_CASE("StateVectorCudaMPI::SetIthStates", using PrecisionT = TestType; \ MPIManager mpi_manager(MPI_COMM_WORLD); \ REQUIRE(mpi_manager.getSize() == 2); \ - size_t mpi_buffersize = 1; \ - size_t nGlobalIndexBits = \ - std::bit_width(static_cast(mpi_manager.getSize())) - 1; \ - size_t nLocalIndexBits = (NUM_QUBITS)-nGlobalIndexBits; \ - size_t subSvLength = 1 << nLocalIndexBits; \ - size_t svLength = 1 << (NUM_QUBITS); \ + std::size_t mpi_buffersize = 1; \ + std::size_t nGlobalIndexBits = \ + std::bit_width(static_cast(mpi_manager.getSize())) - \ + 1; \ + std::size_t nLocalIndexBits = (NUM_QUBITS)-nGlobalIndexBits; \ + std::size_t subSvLength = 1 << nLocalIndexBits; \ + std::size_t svLength = 1 << (NUM_QUBITS); \ mpi_manager.Barrier(); \ std::vector expected_sv(svLength); \ std::vector local_state(subSvLength); \ diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/gates/tests/mpi/Test_StateVectorCudaMPI_Param.cpp b/pennylane_lightning/core/src/simulators/lightning_gpu/gates/tests/mpi/Test_StateVectorCudaMPI_Param.cpp index 85e855c9b9..a9d5ec106d 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/gates/tests/mpi/Test_StateVectorCudaMPI_Param.cpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/gates/tests/mpi/Test_StateVectorCudaMPI_Param.cpp @@ -77,12 +77,13 @@ using namespace Pennylane::LightningGPU::MPI; using PrecisionT = TestType; \ MPIManager mpi_manager(MPI_COMM_WORLD); \ REQUIRE(mpi_manager.getSize() == 2); \ - size_t mpi_buffersize = 1; \ - size_t nGlobalIndexBits = \ - std::bit_width(static_cast(mpi_manager.getSize())) - 1; \ - size_t nLocalIndexBits = NUM_QUBITS - nGlobalIndexBits; \ - size_t subSvLength = 1 << nLocalIndexBits; \ - size_t svLength = 1 << NUM_QUBITS; \ + std::size_t mpi_buffersize = 1; \ + std::size_t nGlobalIndexBits = \ + std::bit_width(static_cast(mpi_manager.getSize())) - \ + 1; \ + std::size_t nLocalIndexBits = NUM_QUBITS - nGlobalIndexBits; \ + std::size_t subSvLength = 1 << nLocalIndexBits; \ + std::size_t svLength = 1 << NUM_QUBITS; \ mpi_manager.Barrier(); \ std::vector expected_sv(svLength); \ std::vector local_state(subSvLength); \ @@ -361,7 +362,7 @@ TEMPLATE_TEST_CASE("LightningGPUMPI:applyOperation", "[LightningGPUMPI_Param]", MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; int nGlobalIndexBits = std::bit_width(static_cast(mpi_manager.getSize())) - 1; diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/initSV.cu b/pennylane_lightning/core/src/simulators/lightning_gpu/initSV.cu index 8768eda50f..4e3e93ea79 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/initSV.cu +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/initSV.cu @@ -35,11 +35,11 @@ namespace Pennylane::LightningGPU { * @param stream_id Stream id of CUDA calls */ void setStateVector_CUDA(cuComplex *sv, int &num_indices, cuComplex *value, - int *indices, size_t thread_per_block, + int *indices, std::size_t thread_per_block, cudaStream_t stream_id); void setStateVector_CUDA(cuDoubleComplex *sv, long &num_indices, cuDoubleComplex *value, long *indices, - size_t thread_per_block, cudaStream_t stream_id); + std::size_t thread_per_block, cudaStream_t stream_id); /** * @brief Explicitly set basis state data on GPU device from the input values @@ -51,10 +51,12 @@ void setStateVector_CUDA(cuDoubleComplex *sv, long &num_indices, * @param async Use an asynchronous memory copy. * @param stream_id Stream id of CUDA calls */ -void setBasisState_CUDA(cuComplex *sv, cuComplex &value, const size_t index, - bool async, cudaStream_t stream_id); +void setBasisState_CUDA(cuComplex *sv, cuComplex &value, + const std::size_t index, bool async, + cudaStream_t stream_id); void setBasisState_CUDA(cuDoubleComplex *sv, cuDoubleComplex &value, - const size_t index, bool async, cudaStream_t stream_id); + const std::size_t index, bool async, + cudaStream_t stream_id); /** * @brief The CUDA kernel that setS state vector data on GPU device from the @@ -90,10 +92,11 @@ __global__ void setStateVectorkernel(GPUDataT *sv, index_type num_indices, template void setStateVector_CUDA_call(GPUDataT *sv, index_type &num_indices, GPUDataT *value, index_type *indices, - size_t thread_per_block, cudaStream_t stream_id) { + std::size_t thread_per_block, + cudaStream_t stream_id) { auto dv = std::div(num_indices, thread_per_block); - size_t num_blocks = dv.quot + (dv.rem == 0 ? 0 : 1); - const size_t block_per_grid = (num_blocks == 0 ? 1 : num_blocks); + std::size_t num_blocks = dv.quot + (dv.rem == 0 ? 0 : 1); + const std::size_t block_per_grid = (num_blocks == 0 ? 1 : num_blocks); dim3 blockSize(thread_per_block, 1, 1); dim3 gridSize(block_per_grid, 1); @@ -131,11 +134,12 @@ __global__ void globalPhaseStateVectorkernel(GPUDataT *sv, index_type num_sv, */ template void globalPhaseStateVector_CUDA_call(GPUDataT *sv, index_type num_sv, - GPUDataT phase, size_t thread_per_block, + GPUDataT phase, + std::size_t thread_per_block, cudaStream_t stream_id) { auto dv = std::div(static_cast(num_sv), thread_per_block); - size_t num_blocks = dv.quot + (dv.rem == 0 ? 0 : 1); - const size_t block_per_grid = (num_blocks == 0 ? 1 : num_blocks); + std::size_t num_blocks = dv.quot + (dv.rem == 0 ? 0 : 1); + const std::size_t block_per_grid = (num_blocks == 0 ? 1 : num_blocks); dim3 blockSize(thread_per_block, 1, 1); dim3 gridSize(block_per_grid, 1); @@ -178,11 +182,12 @@ __global__ void cGlobalPhaseStateVectorkernel(GPUDataT *sv, index_type num_sv, */ template void cGlobalPhaseStateVector_CUDA_call(GPUDataT *sv, index_type num_sv, - GPUDataT *phase, size_t thread_per_block, + GPUDataT *phase, + std::size_t thread_per_block, cudaStream_t stream_id) { auto dv = std::div(static_cast(num_sv), thread_per_block); - size_t num_blocks = dv.quot + (dv.rem == 0 ? 0 : 1); - const size_t block_per_grid = (num_blocks == 0 ? 1 : num_blocks); + std::size_t num_blocks = dv.quot + (dv.rem == 0 ? 0 : 1); + const std::size_t block_per_grid = (num_blocks == 0 ? 1 : num_blocks); dim3 blockSize(thread_per_block, 1, 1); dim3 gridSize(block_per_grid, 1); @@ -201,8 +206,9 @@ void cGlobalPhaseStateVector_CUDA_call(GPUDataT *sv, index_type num_sv, * @param stream_id Stream id of CUDA calls */ template -void setBasisState_CUDA_call(GPUDataT *sv, GPUDataT &value, const size_t index, - bool async, cudaStream_t stream_id) { +void setBasisState_CUDA_call(GPUDataT *sv, GPUDataT &value, + const std::size_t index, bool async, + cudaStream_t stream_id) { if (!async) { PL_CUDA_IS_SUCCESS(cudaMemcpy(&sv[index], &value, sizeof(GPUDataT), cudaMemcpyHostToDevice)); @@ -214,61 +220,64 @@ void setBasisState_CUDA_call(GPUDataT *sv, GPUDataT &value, const size_t index, // Definitions void setStateVector_CUDA(cuComplex *sv, int &num_indices, cuComplex *value, - int *indices, size_t thread_per_block, + int *indices, std::size_t thread_per_block, cudaStream_t stream_id) { setStateVector_CUDA_call(sv, num_indices, value, indices, thread_per_block, stream_id); } void setStateVector_CUDA(cuDoubleComplex *sv, long &num_indices, cuDoubleComplex *value, long *indices, - size_t thread_per_block, cudaStream_t stream_id) { + std::size_t thread_per_block, cudaStream_t stream_id) { setStateVector_CUDA_call(sv, num_indices, value, indices, thread_per_block, stream_id); } -void setBasisState_CUDA(cuComplex *sv, cuComplex &value, const size_t index, - bool async, cudaStream_t stream_id) { +void setBasisState_CUDA(cuComplex *sv, cuComplex &value, + const std::size_t index, bool async, + cudaStream_t stream_id) { setBasisState_CUDA_call(sv, value, index, async, stream_id); } void setBasisState_CUDA(cuDoubleComplex *sv, cuDoubleComplex &value, - const size_t index, bool async, + const std::size_t index, bool async, cudaStream_t stream_id) { setBasisState_CUDA_call(sv, value, index, async, stream_id); } -void globalPhaseStateVector_CUDA(cuComplex *sv, size_t num_sv, cuComplex phase, - size_t thread_per_block, +void globalPhaseStateVector_CUDA(cuComplex *sv, std::size_t num_sv, + cuComplex phase, std::size_t thread_per_block, cudaStream_t stream_id) { globalPhaseStateVector_CUDA_call(sv, num_sv, phase, thread_per_block, stream_id); } -void globalPhaseStateVector_CUDA(cuDoubleComplex *sv, size_t num_sv, - cuDoubleComplex phase, size_t thread_per_block, +void globalPhaseStateVector_CUDA(cuDoubleComplex *sv, std::size_t num_sv, + cuDoubleComplex phase, + std::size_t thread_per_block, cudaStream_t stream_id) { globalPhaseStateVector_CUDA_call(sv, num_sv, phase, thread_per_block, stream_id); } -void cGlobalPhaseStateVector_CUDA(cuComplex *sv, size_t num_sv, bool adjoint, - cuComplex *phase, size_t thread_per_block, +void cGlobalPhaseStateVector_CUDA(cuComplex *sv, std::size_t num_sv, + bool adjoint, cuComplex *phase, + std::size_t thread_per_block, cudaStream_t stream_id) { if (adjoint) { - cGlobalPhaseStateVector_CUDA_call( + cGlobalPhaseStateVector_CUDA_call( sv, num_sv, phase, thread_per_block, stream_id); } else { - cGlobalPhaseStateVector_CUDA_call( + cGlobalPhaseStateVector_CUDA_call( sv, num_sv, phase, thread_per_block, stream_id); } } -void cGlobalPhaseStateVector_CUDA(cuDoubleComplex *sv, size_t num_sv, +void cGlobalPhaseStateVector_CUDA(cuDoubleComplex *sv, std::size_t num_sv, bool adjoint, cuDoubleComplex *phase, - size_t thread_per_block, + std::size_t thread_per_block, cudaStream_t stream_id) { if (adjoint) { - cGlobalPhaseStateVector_CUDA_call( + cGlobalPhaseStateVector_CUDA_call( sv, num_sv, phase, thread_per_block, stream_id); } else { - cGlobalPhaseStateVector_CUDA_call( + cGlobalPhaseStateVector_CUDA_call( sv, num_sv, phase, thread_per_block, stream_id); } } diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/MeasurementsGPU.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/MeasurementsGPU.hpp index 3a80ca8990..41045821aa 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/MeasurementsGPU.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/MeasurementsGPU.hpp @@ -15,7 +15,7 @@ /** * @file * Defines a class for the measurement of observables in quantum states - * represented by a Lightning Qubit StateVector class. + * represented by a Lightning GPU StateVector class. */ #pragma once @@ -91,7 +91,8 @@ class Measurements final * order. * @return std::vector */ - auto probs(const std::vector &wires) -> std::vector { + auto probs(const std::vector &wires) + -> std::vector { PL_ABORT_IF_NOT(std::is_sorted(wires.cbegin(), wires.cend()) || std::is_sorted(wires.rbegin(), wires.rend()), "LightningGPU does not currently support out-of-order " @@ -153,7 +154,7 @@ class Measurements final * @return std::vector */ auto probs() -> std::vector { - std::vector wires; + std::vector wires; for (size_t i = 0; i < this->_statevector.getNumQubits(); i++) { wires.push_back(i); } @@ -171,7 +172,7 @@ class Measurements final * in lexicographic order. */ std::vector probs(const Observable &obs, - size_t num_shots = 0) { + std::size_t num_shots = 0) { return BaseType::probs(obs, num_shots); } @@ -196,8 +197,8 @@ class Measurements final * @return Floating point std::vector with probabilities. */ - std::vector probs(const std::vector &wires, - size_t num_shots) { + std::vector probs(const std::vector &wires, + std::size_t num_shots) { PL_ABORT_IF_NOT(std::is_sorted(wires.cbegin(), wires.cend()), "LightningGPU does not currently support out-of-order " "wire indices with probability calculations"); @@ -210,16 +211,16 @@ class Measurements final * * @param num_samples Number of Samples * - * @return std::vector A 1-d array storing the samples. + * @return std::vector A 1-d array storing the samples. * Each sample has a length equal to the number of qubits. Each sample can * be accessed using the stride sample_id*num_qubits, where sample_id is a * number between 0 and num_samples-1. */ - auto generate_samples(size_t num_samples) -> std::vector { + auto generate_samples(size_t num_samples) -> std::vector { std::vector rand_nums(num_samples); custatevecSamplerDescriptor_t sampler; - const size_t num_qubits = this->_statevector.getNumQubits(); + const std::size_t num_qubits = this->_statevector.getNumQubits(); const int bitStringLen = this->_statevector.getNumQubits(); std::vector bitOrdering(num_qubits); @@ -240,12 +241,12 @@ class Measurements final for (size_t n = 0; n < num_samples; n++) { rand_nums[n] = dis(this->rng); } - std::vector samples(num_samples * num_qubits, 0); - std::unordered_map cache; + std::vector samples(num_samples * num_qubits, 0); + std::unordered_map cache; std::vector bitStrings(num_samples); void *extraWorkspace = nullptr; - size_t extraWorkspaceSizeInBytes = 0; + std::size_t extraWorkspaceSizeInBytes = 0; // create sampler and check the size of external workspace PL_CUSTATEVEC_IS_SUCCESS(custatevecSamplerCreate( this->_statevector.getCusvHandle(), this->_statevector.getData(), @@ -276,7 +277,7 @@ class Measurements final auto idx = bitStrings[i]; // If cached, retrieve sample from cache if (cache.count(idx) != 0) { - size_t cache_id = cache[idx]; + std::size_t cache_id = cache[idx]; auto it_temp = samples.begin() + cache_id * num_qubits; std::copy(it_temp, it_temp + num_qubits, samples.begin() + i * num_qubits); @@ -348,8 +349,8 @@ class Measurements final * @param wires Wires where to apply the operator. * @return Floating point expected value of the observable. */ - auto expval(const std::string &operation, const std::vector &wires) - -> PrecisionT { + auto expval(const std::string &operation, + const std::vector &wires) -> PrecisionT { std::vector params = {0.0}; std::vector gate_matrix = {}; return this->expval_(operation, wires, params, gate_matrix); @@ -366,7 +367,7 @@ class Measurements final */ template auto expval(const std::vector &operations_list, - const std::vector> &wires_list) + const std::vector> &wires_list) -> std::vector { PL_ABORT_IF( (operations_list.size() != wires_list.size()), @@ -412,8 +413,9 @@ class Measurements final * @return Floating point expected value of the observable. */ - auto expval(const Observable &obs, const size_t &num_shots, - const std::vector &shot_range) -> PrecisionT { + auto expval(const Observable &obs, + const std::size_t &num_shots, + const std::vector &shot_range) -> PrecisionT { return BaseType::expval(obs, num_shots, shot_range); } @@ -425,7 +427,7 @@ class Measurements final * @return Floating point expected value of the observable. */ auto expval(const std::vector &matrix, - const std::vector &wires) -> PrecisionT { + const std::vector &wires) -> PrecisionT { return this->expval_(wires, matrix); } @@ -550,8 +552,8 @@ class Measurements final * @param wires Wires where to apply the operator. * @return Floating point with the variance of the observable. */ - auto var(const std::string &operation, const std::vector &wires) - -> PrecisionT { + auto var(const std::string &operation, + const std::vector &wires) -> PrecisionT { StateVectorT ob_sv(this->_statevector.getData(), this->_statevector.getLength()); ob_sv.applyOperation(operation, wires); @@ -581,7 +583,7 @@ class Measurements final * @return Floating point with the variance of the observable. */ auto var(const std::vector &matrix, - const std::vector &wires) -> PrecisionT { + const std::vector &wires) -> PrecisionT { StateVectorT ob_sv(this->_statevector.getData(), this->_statevector.getLength()); ob_sv.applyMatrix(matrix, wires); @@ -615,7 +617,7 @@ class Measurements final */ template auto var(const std::vector &operations_list, - const std::vector> &wires_list) + const std::vector> &wires_list) -> std::vector { PL_ABORT_IF( (operations_list.size() != wires_list.size()), @@ -692,7 +694,7 @@ class Measurements final * @return Variance of the given observable. */ - auto var(const Observable &obs, const size_t &num_shots) + auto var(const Observable &obs, const std::size_t &num_shots) -> PrecisionT { return BaseType::var(obs, num_shots); } @@ -710,7 +712,8 @@ class Measurements final * if does not exist. * @return auto Expectation value. */ - auto expval_(const std::string &obsName, const std::vector &wires, + auto expval_(const std::string &obsName, + const std::vector &wires, const std::vector ¶ms = {0.0}, const std::vector> &gate_matrix = {}) -> PrecisionT { @@ -730,7 +733,7 @@ class Measurements final /** * @brief See `expval(std::vector &gate_matrix = {})` */ - auto expval_(const std::vector &wires, + auto expval_(const std::vector &wires, const std::vector> &gate_matrix) -> PrecisionT { std::vector matrix_cu(gate_matrix.size()); @@ -747,7 +750,8 @@ class Measurements final // Wire order reversed to match expected custatevec wire ordering for // tensor observables. - auto &&local_wires = std::vector{wires.rbegin(), wires.rend()}; + auto &&local_wires = + std::vector{wires.rbegin(), wires.rend()}; auto expect_val = this->getExpectationValueDeviceMatrix_( matrix_cu.data(), local_wires); @@ -765,7 +769,7 @@ class Measurements final getExpectationValueDeviceMatrix_(const CFP_t *matrix, const std::vector &tgts) { void *extraWorkspace = nullptr; - size_t extraWorkspaceSizeInBytes = 0; + std::size_t extraWorkspaceSizeInBytes = 0; std::vector tgtsInt(tgts.size()); std::transform(tgts.begin(), tgts.end(), tgtsInt.begin(), @@ -774,7 +778,7 @@ class Measurements final this->_statevector.getNumQubits() - 1 - x); }); - size_t nIndexBits = this->_statevector.getNumQubits(); + std::size_t nIndexBits = this->_statevector.getNumQubits(); cudaDataType_t data_type; cudaDataType_t expectationDataType = CUDA_C_64F; // Requested by the custatevecComputeExpectation API @@ -799,7 +803,7 @@ class Measurements final /* custatevecMatrixLayout_t */ CUSTATEVEC_MATRIX_LAYOUT_ROW, /* const uint32_t */ tgtsInt.size(), /* custatevecComputeType_t */ compute_type, - /* size_t* */ &extraWorkspaceSizeInBytes)); + /* std::size_t* */ &extraWorkspaceSizeInBytes)); // LCOV_EXCL_START if (extraWorkspaceSizeInBytes > 0) { @@ -826,7 +830,7 @@ class Measurements final /* const uint32_t */ tgtsInt.size(), /* custatevecComputeType_t */ compute_type, /* void* */ extraWorkspace, - /* size_t */ extraWorkspaceSizeInBytes)); + /* std::size_t */ extraWorkspaceSizeInBytes)); // LCOV_EXCL_START if (extraWorkspaceSizeInBytes) diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/MeasurementsGPUMPI.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/MeasurementsGPUMPI.hpp index 9d0d986fa4..41961e26e3 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/MeasurementsGPUMPI.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/MeasurementsGPUMPI.hpp @@ -98,7 +98,8 @@ class MeasurementsMPI final * order. * @return std::vector */ - auto probs(const std::vector &wires) -> std::vector { + auto probs(const std::vector &wires) + -> std::vector { // Data return type fixed as double in custatevec function call std::vector subgroup_probabilities; @@ -145,11 +146,11 @@ class MeasurementsMPI final /* const uint32_t */ maskLen)); // create new MPI communicator groups - size_t subCommGroupId = 0; + std::size_t subCommGroupId = 0; for (size_t i = 0; i < wires_global.size(); i++) { - size_t mask = + std::size_t mask = 1 << (wires_global[i] - this->_statevector.getNumLocalQubits()); - size_t bitValue = mpi_manager_.getRank() & mask; + std::size_t bitValue = mpi_manager_.getRank() & mask; subCommGroupId += bitValue << (wires_global[i] - this->_statevector.getNumLocalQubits()); @@ -203,7 +204,7 @@ class MeasurementsMPI final * @return std::vector */ auto probs() -> std::vector { - std::vector wires; + std::vector wires; for (size_t i = 0; i < this->_statevector.getNumQubits(); i++) { wires.push_back(i); } @@ -221,7 +222,7 @@ class MeasurementsMPI final * in lexicographic order. */ std::vector probs(const Observable &obs, - size_t num_shots = 0) { + std::size_t num_shots = 0) { return BaseType::probs(obs, num_shots); } @@ -246,8 +247,8 @@ class MeasurementsMPI final * @return Floating point std::vector with probabilities. */ - std::vector probs(const std::vector &wires, - size_t num_shots) { + std::vector probs(const std::vector &wires, + std::size_t num_shots) { return BaseType::probs(wires, num_shots); } @@ -256,20 +257,20 @@ class MeasurementsMPI final * * @param num_samples Number of Samples * - * @return std::vector A 1-d array storing the samples. + * @return std::vector A 1-d array storing the samples. * Each sample has a length equal to the number of qubits. Each sample can * be accessed using the stride sample_id*num_qubits, where sample_id is a * number between 0 and num_samples-1. */ - auto generate_samples(size_t num_samples) -> std::vector { + auto generate_samples(size_t num_samples) -> std::vector { double epsilon = 1e-15; - size_t nSubSvs = 1UL << (this->_statevector.getNumGlobalQubits()); + std::size_t nSubSvs = 1UL << (this->_statevector.getNumGlobalQubits()); std::vector rand_nums(num_samples); - std::vector samples( + std::vector samples( num_samples * this->_statevector.getTotalNumQubits(), 0); - size_t bitStringLen = this->_statevector.getNumGlobalQubits() + - this->_statevector.getNumLocalQubits(); + std::size_t bitStringLen = this->_statevector.getNumGlobalQubits() + + this->_statevector.getNumLocalQubits(); std::vector bitOrdering(bitStringLen); @@ -291,7 +292,7 @@ class MeasurementsMPI final custatevecSamplerDescriptor_t sampler; void *extraWorkspace = nullptr; - size_t extraWorkspaceSizeInBytes = 0; + std::size_t extraWorkspaceSizeInBytes = 0; PL_CUSTATEVEC_IS_SUCCESS(custatevecSamplerCreate( /* custatevecHandle_t */ this->_statevector.getCusvHandle(), @@ -300,7 +301,7 @@ class MeasurementsMPI final /* const uint32_t */ this->_statevector.getNumLocalQubits(), /* custatevecSamplerDescriptor_t * */ &sampler, /* uint32_t */ num_samples, - /* size_t* */ &extraWorkspaceSizeInBytes)); + /* std::size_t* */ &extraWorkspaceSizeInBytes)); if (extraWorkspaceSizeInBytes > 0) PL_CUDA_IS_SUCCESS( @@ -310,7 +311,7 @@ class MeasurementsMPI final /* custatevecHandle_t */ this->_statevector.getCusvHandle(), /* custatevecSamplerDescriptor_t */ sampler, /* void* */ extraWorkspace, - /* const size_t */ extraWorkspaceSizeInBytes)); + /* const std::size_t */ extraWorkspaceSizeInBytes)); double subNorm = 0; PL_CUSTATEVEC_IS_SUCCESS(custatevecSamplerGetSquaredNorm( @@ -422,7 +423,7 @@ class MeasurementsMPI final const int64_t numNNZ) -> PrecisionT { if (mpi_manager_.getRank() == 0) { PL_ABORT_IF_NOT( - static_cast(csrOffsets_size - 1) == + static_cast(csrOffsets_size - 1) == (size_t{1} << this->_statevector.getTotalNumQubits()), "Incorrect size of CSR Offsets."); PL_ABORT_IF_NOT(numNNZ > 0, "Empty CSR matrix."); @@ -435,8 +436,8 @@ class MeasurementsMPI final auto stream_id = this->_statevector.getDataBuffer().getDevTag().getStreamID(); - const size_t length_local = size_t{1} - << this->_statevector.getNumLocalQubits(); + const std::size_t length_local = + std::size_t{1} << this->_statevector.getNumLocalQubits(); DataBuffer d_res_per_rowblock{length_local, device_id, stream_id, true}; d_res_per_rowblock.zeroInit(); @@ -470,8 +471,8 @@ class MeasurementsMPI final * @param wires Wires where to apply the operator. * @return Floating point expected value of the observable. */ - auto expval(const std::string &operation, const std::vector &wires) - -> PrecisionT { + auto expval(const std::string &operation, + const std::vector &wires) -> PrecisionT { std::vector params = {0.0}; std::vector gate_matrix = {}; auto expect = @@ -491,7 +492,7 @@ class MeasurementsMPI final */ template auto expval(const std::vector &operations_list, - const std::vector> &wires_list) + const std::vector> &wires_list) -> std::vector { PL_ABORT_IF( (operations_list.size() != wires_list.size()), @@ -545,8 +546,9 @@ class MeasurementsMPI final * @return Floating point expected value of the observable. */ - auto expval(const Observable &obs, const size_t &num_shots, - const std::vector &shot_range) -> PrecisionT { + auto expval(const Observable &obs, + const std::size_t &num_shots, + const std::vector &shot_range) -> PrecisionT { mpi_manager_.Barrier(); PrecisionT result = BaseType::expval(obs, num_shots, shot_range); mpi_manager_.Barrier(); @@ -561,7 +563,7 @@ class MeasurementsMPI final * @return Floating point expected value of the observable. */ auto expval(const std::vector &matrix, - const std::vector &wires) -> PrecisionT { + const std::vector &wires) -> PrecisionT { auto expect = this->_statevector.expval(wires, matrix); return static_cast(expect); } @@ -620,8 +622,8 @@ class MeasurementsMPI final * @param wires Wires where to apply the operator. * @return Floating point with the variance of the observable. */ - auto var(const std::string &operation, const std::vector &wires) - -> PrecisionT { + auto var(const std::string &operation, + const std::vector &wires) -> PrecisionT { StateVectorT ob_sv(this->_statevector); ob_sv.applyOperation(operation, wires); @@ -655,7 +657,7 @@ class MeasurementsMPI final * @return Floating point with the variance of the observable. */ auto var(const std::vector &matrix, - const std::vector &wires) -> PrecisionT { + const std::vector &wires) -> PrecisionT { StateVectorT ob_sv(this->_statevector); ob_sv.applyMatrix(matrix, wires); @@ -693,7 +695,7 @@ class MeasurementsMPI final */ template auto var(const std::vector &operations_list, - const std::vector> &wires_list) + const std::vector> &wires_list) -> std::vector { PL_ABORT_IF( (operations_list.size() != wires_list.size()), @@ -731,7 +733,7 @@ class MeasurementsMPI final const int64_t numNNZ) { if (mpi_manager_.getRank() == 0) { PL_ABORT_IF_NOT( - static_cast(csrOffsets_size - 1) == + static_cast(csrOffsets_size - 1) == (size_t{1} << this->_statevector.getTotalNumQubits()), "Incorrect size of CSR Offsets."); PL_ABORT_IF_NOT(numNNZ > 0, "Empty CSR matrix."); @@ -741,8 +743,8 @@ class MeasurementsMPI final this->_statevector.getDataBuffer().getDevTag().getDeviceID(); auto stream_id = this->_statevector.getDataBuffer().getDevTag().getStreamID(); - const size_t length_local = size_t{1} - << this->_statevector.getNumLocalQubits(); + const std::size_t length_local = + std::size_t{1} << this->_statevector.getNumLocalQubits(); DataBuffer d_res_per_rowblock{length_local, device_id, stream_id, true}; @@ -786,7 +788,7 @@ class MeasurementsMPI final * @return Variance of the given observable. */ - auto var(const Observable &obs, const size_t &num_shots) + auto var(const Observable &obs, const std::size_t &num_shots) -> PrecisionT { return BaseType::var(obs, num_shots); } diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/tests/Test_StateVectorCudaManaged_Expval.cpp b/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/tests/Test_StateVectorCudaManaged_Expval.cpp index 36f1f1f128..9f63465d55 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/tests/Test_StateVectorCudaManaged_Expval.cpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/tests/Test_StateVectorCudaManaged_Expval.cpp @@ -47,7 +47,7 @@ namespace cuUtil = Pennylane::LightningGPU::Util; TEMPLATE_TEST_CASE("[Identity]", "[StateVectorCudaManaged_Expval]", float, double) { using StateVectorT = StateVectorCudaManaged; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; auto ONE = TestType(1); StateVectorT sv{num_qubits}; sv.initSV(); @@ -66,7 +66,7 @@ TEMPLATE_TEST_CASE("[PauliX]", "[StateVectorCudaManaged_Expval]", float, double) { { using StateVectorT = StateVectorCudaManaged; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; auto ZERO = TestType(0); auto ONE = TestType(1); @@ -118,7 +118,7 @@ TEMPLATE_TEST_CASE("[PauliY]", "[StateVectorCudaManaged_Expval]", float, double) { { using StateVectorT = StateVectorCudaManaged; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; auto ZERO = TestType(0); auto ONE = TestType(1); @@ -186,7 +186,7 @@ TEMPLATE_TEST_CASE("[Hadamard]", "[StateVectorCudaManaged_Expval]", float, double) { { using StateVectorT = StateVectorCudaManaged; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; auto INVSQRT2 = TestType(0.707106781186547524401); SECTION("Using expval") { @@ -205,18 +205,18 @@ TEMPLATE_TEST_CASE("StateVectorCudaManaged::Hamiltonian_expval", "[StateVectorCudaManaged_Expval]", float, double) { using StateVectorT = StateVectorCudaManaged; using ComplexT = StateVectorT::ComplexT; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; SECTION("GetExpectationIdentity") { StateVectorT sv{num_qubits}; sv.initSV(); auto m = Measurements(sv); - std::vector wires{0, 1, 2}; + std::vector wires{0, 1, 2}; sv.applyOperations({{"Hadamard"}, {"CNOT"}, {"CNOT"}}, {{0}, {0, 1}, {1, 2}}, {{false}, {false}, {false}}); - size_t matrix_dim = static_cast(1U) << num_qubits; + std::size_t matrix_dim = static_cast(1U) << num_qubits; std::vector matrix(matrix_dim * matrix_dim); for (size_t i = 0; i < matrix.size(); i++) { @@ -237,7 +237,7 @@ TEMPLATE_TEST_CASE("StateVectorCudaManaged::Hamiltonian_expval", {0.3, 0.4}, {0.4, 0.5}}; StateVectorT sv{init_state.data(), init_state.size()}; auto m = Measurements(sv); - std::vector wires{0, 1, 2}; + std::vector wires{0, 1, 2}; std::vector matrix{ {0.5, 0.0}, {0.2, 0.5}, {0.2, -0.5}, {0.3, 0.0}, {0.2, -0.5}, {0.3, 0.0}, {0.2, -0.5}, {0.3, 0.0}, {0.2, -0.5}, {0.3, 0.0}, @@ -298,9 +298,9 @@ TEMPLATE_TEST_CASE("Test expectation value of HamiltonianObs", auto m = Measurements(sv); auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto Z1 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto ob = Hamiltonian::create({0.3, 0.5}, {X0, Z1}); auto res = m.expval(*ob); @@ -321,9 +321,9 @@ TEMPLATE_TEST_CASE("Test expectation value of TensorProdObs", auto m = Measurements(sv); auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto Z1 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto ob = TensorProdObs::create({X0, Z1}); auto res = m.expval(*ob); @@ -374,8 +374,8 @@ TEMPLATE_TEST_CASE("StateVectorCudaManaged::Hamiltonian_expval_Sparse", // This object attaches to the statevector allowing several // measurements. Measurements Measurer(sv); - const size_t num_qubits = 3; - size_t data_size = Pennylane::Util::exp2(num_qubits); + const std::size_t num_qubits = 3; + std::size_t data_size = Pennylane::Util::exp2(num_qubits); std::vector row_map; std::vector entries; diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/tests/Test_StateVectorCudaManaged_Measure.cpp b/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/tests/Test_StateVectorCudaManaged_Measure.cpp index 679cd4855a..f72bff7f1d 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/tests/Test_StateVectorCudaManaged_Measure.cpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/tests/Test_StateVectorCudaManaged_Measure.cpp @@ -48,14 +48,14 @@ TEMPLATE_TEST_CASE("Expected Values", "[Measurements]", float, double) { SECTION("Testing single operation defined by a matrix:") { std::vector PauliX = {0, 1, 1, 0}; - std::vector wires_single = {0}; + std::vector wires_single = {0}; PrecisionT exp_value = Measurer.expval(PauliX, wires_single); PrecisionT exp_values_ref = 0.492725; REQUIRE(exp_value == Approx(exp_values_ref).margin(1e-6)); } SECTION("Testing single operation defined by its name:") { - std::vector wires_single = {0}; + std::vector wires_single = {0}; PrecisionT exp_value = Measurer.expval("PauliX", wires_single); PrecisionT exp_values_ref = 0.492725; REQUIRE(exp_value == Approx(exp_values_ref).margin(1e-6)); @@ -68,7 +68,7 @@ TEMPLATE_TEST_CASE("Expected Values", "[Measurements]", float, double) { std::vector exp_values; std::vector exp_values_ref; - std::vector> wires_list = {{0}, {1}, {2}}; + std::vector> wires_list = {{0}, {1}, {2}}; std::vector> operations_list; operations_list = {PauliX, PauliX, PauliX}; @@ -90,7 +90,7 @@ TEMPLATE_TEST_CASE("Expected Values", "[Measurements]", float, double) { SECTION("Testing list of operators defined by its name:") { std::vector exp_values; std::vector exp_values_ref; - std::vector> wires_list = {{0}, {1}, {2}}; + std::vector> wires_list = {{0}, {1}, {2}}; std::vector operations_list; operations_list = {"PauliX", "PauliX", "PauliX"}; @@ -139,7 +139,7 @@ TEMPLATE_TEST_CASE("Pauli word based API", "[Measurements]", float, double) { SECTION("Testing for Pauli words:") { PrecisionT exp_values; std::vector exp_values_ref; - std::vector> wires_list = {{0}, {1}, {2}}; + std::vector> wires_list = {{0}, {1}, {2}}; std::vector operations_list; std::vector> coeffs = { ComplexT{0.1, 0.0}, ComplexT{0.2, 0.0}, ComplexT{0.3, 0.0}}; @@ -191,14 +191,14 @@ TEMPLATE_TEST_CASE("Variances", "[Measurements]", float, double) { SECTION("Testing single operation defined by a matrix:") { std::vector PauliX = {0, 1, 1, 0}; - std::vector wires_single = {0}; + std::vector wires_single = {0}; PrecisionT variance = Measurer.var(PauliX, wires_single); PrecisionT variances_ref = 0.7572222; REQUIRE(variance == Approx(variances_ref).margin(1e-6)); } SECTION("Testing single operation defined by its name:") { - std::vector wires_single = {0}; + std::vector wires_single = {0}; PrecisionT variance = Measurer.var("PauliX", wires_single); PrecisionT variances_ref = 0.7572222; REQUIRE(variance == Approx(variances_ref).margin(1e-6)); @@ -211,7 +211,7 @@ TEMPLATE_TEST_CASE("Variances", "[Measurements]", float, double) { std::vector variances; std::vector variances_ref; - std::vector> wires_list = {{0}, {1}, {2}}; + std::vector> wires_list = {{0}, {1}, {2}}; std::vector> operations_list; operations_list = {PauliX, PauliX, PauliX}; @@ -233,7 +233,7 @@ TEMPLATE_TEST_CASE("Variances", "[Measurements]", float, double) { SECTION("Testing list of operators defined by its name:") { std::vector variances; std::vector variances_ref; - std::vector> wires_list = {{0}, {1}, {2}}; + std::vector> wires_list = {{0}, {1}, {2}}; std::vector operations_list; operations_list = {"PauliX", "PauliX", "PauliX"}; @@ -256,10 +256,10 @@ TEMPLATE_TEST_CASE("Variances", "[Measurements]", float, double) { TEMPLATE_TEST_CASE("Probabilities", "[Measures]", float, double) { using StateVectorT = StateVectorCudaManaged; // Probabilities calculated with Pennylane default.qubit: - std::vector, std::vector>> input = { - {{2, 1, 0}, - {0.67078706, 0.03062806, 0.0870997, 0.00397696, 0.17564072, 0.00801973, - 0.02280642, 0.00104134}}}; + std::vector, std::vector>> + input = {{{2, 1, 0}, + {0.67078706, 0.03062806, 0.0870997, 0.00397696, 0.17564072, + 0.00801973, 0.02280642, 0.00104134}}}; // Defining the State Vector that will be measured. const std::size_t num_qubits = 3; diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/tests/Test_StateVectorCudaManaged_Var.cpp b/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/tests/Test_StateVectorCudaManaged_Var.cpp index aacdf63b29..3b40d093be 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/tests/Test_StateVectorCudaManaged_Var.cpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/tests/Test_StateVectorCudaManaged_Var.cpp @@ -130,9 +130,9 @@ TEMPLATE_TEST_CASE("Test variance of TensorProdObs", {{false}, {false}, {false}, {false}}, {{0.5}, {0.5}, {0.2}, {0.2}}); auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto Z1 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto ob = TensorProdObs::create({X0, Z1}); auto res = m.var(*ob); @@ -152,9 +152,9 @@ TEMPLATE_TEST_CASE("Test variance of HamiltonianObs", auto m = Measurements(sv); auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto Z1 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto ob = Hamiltonian::create({0.3, 0.5}, {X0, Z1}); auto res = m.var(*ob); diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/tests/mpi/Test_StateVectorCudaMPI_Expval.cpp b/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/tests/mpi/Test_StateVectorCudaMPI_Expval.cpp index b6fdab8737..d3c55ff7ae 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/tests/mpi/Test_StateVectorCudaMPI_Expval.cpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/tests/mpi/Test_StateVectorCudaMPI_Expval.cpp @@ -49,17 +49,17 @@ namespace cuUtil = Pennylane::LightningGPU::Util; TEMPLATE_TEST_CASE("[Identity]", "[StateVectorCudaMPI_Expval]", float, double) { using StateVectorT = StateVectorCudaMPI; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; auto ONE = TestType(1); MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -87,7 +87,7 @@ TEMPLATE_TEST_CASE("[Identity]", "[StateVectorCudaMPI_Expval]", float, double) { TEMPLATE_TEST_CASE("[PauliX]", "[StateVectorCudaMPI_Expval]", float, double) { { using StateVectorT = StateVectorCudaMPI; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; auto ZERO = TestType(0); auto ONE = TestType(1); @@ -95,11 +95,11 @@ TEMPLATE_TEST_CASE("[PauliX]", "[StateVectorCudaMPI_Expval]", float, double) { MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -159,7 +159,7 @@ TEMPLATE_TEST_CASE("[PauliX]", "[StateVectorCudaMPI_Expval]", float, double) { TEMPLATE_TEST_CASE("[PauliY]", "[StateVectorCudaMPI_Expval]", float, double) { { using StateVectorT = StateVectorCudaMPI; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; auto ZERO = TestType(0); auto ONE = TestType(1); @@ -168,11 +168,11 @@ TEMPLATE_TEST_CASE("[PauliY]", "[StateVectorCudaMPI_Expval]", float, double) { MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -231,16 +231,16 @@ TEMPLATE_TEST_CASE("[PauliZ]", "[StateVectorCudaMPI_Expval]", float, double) { // Defining the statevector that will be measured. auto statevector_data = createNonTrivialState>(); - size_t num_qubits = 3; + std::size_t num_qubits = 3; MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -270,17 +270,17 @@ TEMPLATE_TEST_CASE("[PauliZ]", "[StateVectorCudaMPI_Expval]", float, double) { TEMPLATE_TEST_CASE("[Hadamard]", "[StateVectorCudaMPI_Expval]", float, double) { { using StateVectorT = StateVectorCudaMPI; - const size_t num_qubits = 3; + const std::size_t num_qubits = 3; auto INVSQRT2 = TestType(0.707106781186547524401); MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -311,12 +311,12 @@ TEMPLATE_TEST_CASE("Test expectation value of HamiltonianObs", MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t num_qubits = 3; - size_t mpi_buffersize = 1; + std::size_t num_qubits = 3; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -340,9 +340,9 @@ TEMPLATE_TEST_CASE("Test expectation value of HamiltonianObs", auto m = MeasurementsMPI(sv); auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto Z1 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto ob = HamiltonianMPI::create({0.3, 0.5}, {X0, Z1}); auto res = m.expval(*ob); @@ -359,12 +359,12 @@ TEMPLATE_TEST_CASE("Test expectation value of TensorProdObs", MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t num_qubits = 3; - size_t mpi_buffersize = 1; + std::size_t num_qubits = 3; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -386,9 +386,9 @@ TEMPLATE_TEST_CASE("Test expectation value of TensorProdObs", auto m = MeasurementsMPI(sv); auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto Z1 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto ob = TensorProdObsMPI::create({X0, Z1}); auto res = m.expval(*ob); @@ -407,12 +407,12 @@ TEMPLATE_TEST_CASE("StateVectorCudaMPI::Hamiltonian_expval_Sparse", MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t num_qubits = 3; - size_t mpi_buffersize = 1; + std::size_t num_qubits = 3; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -467,7 +467,7 @@ TEMPLATE_TEST_CASE("StateVectorCudaMPI::Hamiltonian_expval_Sparse", // This object attaches to the statevector allowing several // measurements. MeasurementsMPI Measurer(sv); - size_t data_size = Pennylane::Util::exp2(num_qubits); + std::size_t data_size = Pennylane::Util::exp2(num_qubits); std::vector row_map; std::vector entries; diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/tests/mpi/Test_StateVectorCudaMPI_Measure.cpp b/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/tests/mpi/Test_StateVectorCudaMPI_Measure.cpp index 553cf1bfb4..c77f4e2215 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/tests/mpi/Test_StateVectorCudaMPI_Measure.cpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/tests/mpi/Test_StateVectorCudaMPI_Measure.cpp @@ -46,16 +46,16 @@ TEMPLATE_TEST_CASE("Expected Values", "[MeasurementsMPI]", float, double) { auto statevector_data = createNonTrivialState>(); - size_t num_qubits = 3; + std::size_t num_qubits = 3; MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -79,7 +79,7 @@ TEMPLATE_TEST_CASE("Expected Values", "[MeasurementsMPI]", float, double) { mpi_manager.Barrier(); const std::vector PauliX = {ComplexT{0, 0}, ComplexT{1, 0}, ComplexT{1, 0}, ComplexT{0, 0}}; - const std::vector wires_single = {0}; + const std::vector wires_single = {0}; PrecisionT exp_value = Measurer.expval(PauliX, wires_single); PrecisionT exp_values_ref = 0.492725; REQUIRE(exp_value == Approx(exp_values_ref).margin(1e-6)); @@ -93,7 +93,7 @@ TEMPLATE_TEST_CASE("Expected Values", "[MeasurementsMPI]", float, double) { MeasurementsMPI Measurer(sv); mpi_manager.Barrier(); - std::vector wires_single = {0}; + std::vector wires_single = {0}; PrecisionT exp_value = Measurer.expval("PauliX", wires_single); PrecisionT exp_values_ref = 0.492725; REQUIRE(exp_value == Approx(exp_values_ref).margin(1e-6)); @@ -113,7 +113,7 @@ TEMPLATE_TEST_CASE("Expected Values", "[MeasurementsMPI]", float, double) { std::vector exp_values; std::vector exp_values_ref; - std::vector> wires_list = {{0}, {1}, {2}}; + std::vector> wires_list = {{0}, {1}, {2}}; std::vector> operations_list; operations_list = {PauliX, PauliX, PauliX}; @@ -142,7 +142,7 @@ TEMPLATE_TEST_CASE("Expected Values", "[MeasurementsMPI]", float, double) { mpi_manager.Barrier(); std::vector exp_values; std::vector exp_values_ref; - std::vector> wires_list = {{0}, {1}, {2}}; + std::vector> wires_list = {{0}, {1}, {2}}; std::vector operations_list; operations_list = {"PauliX", "PauliX", "PauliX"}; @@ -198,12 +198,12 @@ TEMPLATE_TEST_CASE("Pauliwords base on expval", "[MeasurementsMPI]", float, MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t numqubits = 4; - size_t mpi_buffersize = 1; + std::size_t numqubits = 4; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = numqubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = numqubits - nGlobalIndexBits; mpi_manager.Barrier(); std::vector init_sv{{0.1653855288944372, 0.08360762242222763}, @@ -242,7 +242,8 @@ TEMPLATE_TEST_CASE("Pauliwords base on expval", "[MeasurementsMPI]", float, mpi_manager.Barrier(); std::vector pauli_words = {"XYZI", "ZZXX"}; - std::vector> tgts = {{0, 1, 2, 3}, {0, 1, 2, 3}}; + std::vector> tgts = {{0, 1, 2, 3}, + {0, 1, 2, 3}}; std::vector> coeffs = {{0.1, 0.0}, {0.2, 0.0}}; auto expval_mpi = Measurer.expval(pauli_words, tgts, coeffs.data()); @@ -255,7 +256,7 @@ TEMPLATE_TEST_CASE("Pauliwords base on expval", "[MeasurementsMPI]", float, mpi_manager.Barrier(); std::vector pauli_words = {"X", "Y", "Z", "I"}; - std::vector> tgts = {{0}, {0}, {0}, {0}}; + std::vector> tgts = {{0}, {0}, {0}, {0}}; std::vector> coeffs = { {0.1, 0.0}, {0.2, 0.0}, {0.3, 0.0}, {0.4, 0.0}}; @@ -269,7 +270,7 @@ TEMPLATE_TEST_CASE("Pauliwords base on expval", "[MeasurementsMPI]", float, mpi_manager.Barrier(); std::vector pauli_words = {"X", "Y", "Z", "I"}; - std::vector> tgts = { + std::vector> tgts = { {numqubits - 1}, {numqubits - 1}, {numqubits - 1}, {numqubits - 1}}; std::vector> coeffs = { {0.1, 0.0}, {0.2, 0.0}, {0.3, 0.0}, {0.4, 0.0}}; @@ -285,7 +286,7 @@ TEMPLATE_TEST_CASE("Pauliwords base on expval", "[MeasurementsMPI]", float, std::vector pauli_words = {"X", "XY", "XYZ", "XYZI", "X", "Y", "Z", "I"}; - std::vector> tgts = { + std::vector> tgts = { {0}, {0, 1}, {0, 1, 2}, {0, 1, 2, 3}, {3}, {3}, {3}, {3}}; std::vector> coeffs = { {0.1, 0.0}, {0.2, 0.0}, {0.3, 0.0}, {0.4, 0.0}, @@ -306,16 +307,16 @@ TEMPLATE_TEST_CASE("Variances", "[MeasurementsMPI]", double) { auto statevector_data = createNonTrivialState>(); - size_t num_qubits = 3; + std::size_t num_qubits = 3; MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -338,7 +339,7 @@ TEMPLATE_TEST_CASE("Variances", "[MeasurementsMPI]", double) { MeasurementsMPI Measurer(sv); mpi_manager.Barrier(); std::vector PauliX = {0, 1, 1, 0}; - std::vector wires_single = {0}; + std::vector wires_single = {0}; PrecisionT variance = Measurer.var(PauliX, wires_single); PrecisionT variances_ref = 0.7572222; REQUIRE(variance == Approx(variances_ref).margin(1e-6)); @@ -347,7 +348,7 @@ TEMPLATE_TEST_CASE("Variances", "[MeasurementsMPI]", double) { SECTION("Testing single operation defined by its name:") { MeasurementsMPI Measurer(sv); mpi_manager.Barrier(); - std::vector wires_single = {0}; + std::vector wires_single = {0}; PrecisionT variance = Measurer.var("PauliX", wires_single); PrecisionT variances_ref = 0.7572222; REQUIRE(variance == Approx(variances_ref).margin(1e-6)); @@ -362,7 +363,7 @@ TEMPLATE_TEST_CASE("Variances", "[MeasurementsMPI]", double) { std::vector variances; std::vector variances_ref; - std::vector> wires_list = {{0}, {1}, {2}}; + std::vector> wires_list = {{0}, {1}, {2}}; std::vector> operations_list; operations_list = {PauliX, PauliX, PauliX}; @@ -386,7 +387,7 @@ TEMPLATE_TEST_CASE("Variances", "[MeasurementsMPI]", double) { mpi_manager.Barrier(); std::vector variances; std::vector variances_ref; - std::vector> wires_list = {{0}, {1}, {2}}; + std::vector> wires_list = {{0}, {1}, {2}}; std::vector operations_list; operations_list = {"PauliX", "PauliX", "PauliX"}; @@ -409,25 +410,25 @@ TEMPLATE_TEST_CASE("Variances", "[MeasurementsMPI]", double) { TEMPLATE_TEST_CASE("Probabilities", "[MeasuresMPI]", double) { using StateVectorT = StateVectorCudaMPI; // Probabilities calculated with Pennylane default.qubit: - std::vector, std::vector>> input = { - {{2, 1, 0}, - {0.67078706, 0.03062806, 0.0870997, 0.00397696, 0.17564072, 0.00801973, - 0.02280642, 0.00104134}}}; + std::vector, std::vector>> + input = {{{2, 1, 0}, + {0.67078706, 0.03062806, 0.0870997, 0.00397696, 0.17564072, + 0.00801973, 0.02280642, 0.00104134}}}; // Defining the statevector that will be measured. auto statevector_data = createNonTrivialState>(); - size_t num_qubits = 3; + std::size_t num_qubits = 3; MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/tests/mpi/Test_StateVectorCudaMPI_Var.cpp b/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/tests/mpi/Test_StateVectorCudaMPI_Var.cpp index 02ce3caae8..0a9ed9c33b 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/tests/mpi/Test_StateVectorCudaMPI_Var.cpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/tests/mpi/Test_StateVectorCudaMPI_Var.cpp @@ -43,11 +43,11 @@ TEMPLATE_TEST_CASE("Test variance of NamedObs", "[StateVectorCudaMPI_Var]", MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -118,11 +118,11 @@ TEMPLATE_TEST_CASE("Test variance of HermitianObs", "[StateVectorCudaMPI_Var]", MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -171,11 +171,11 @@ TEMPLATE_TEST_CASE("Test variance of TensorProdObs", "[StateVectorCudaMPI_Var]", MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -197,9 +197,9 @@ TEMPLATE_TEST_CASE("Test variance of TensorProdObs", "[StateVectorCudaMPI_Var]", {{false}, {false}, {false}, {false}}, {{0.5}, {0.5}, {0.2}, {0.2}}); auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto Z1 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto ob = TensorProdObsMPI::create({X0, Z1}); auto res = m.var(*ob); @@ -216,11 +216,11 @@ TEMPLATE_TEST_CASE("Test variance of HamiltonianObs", MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; + std::size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; int nDevices = 0; cudaGetDeviceCount(&nDevices); @@ -245,9 +245,9 @@ TEMPLATE_TEST_CASE("Test variance of HamiltonianObs", auto m = MeasurementsMPI(sv); auto X0 = std::make_shared>( - "PauliX", std::vector{0}); + "PauliX", std::vector{0}); auto Z1 = std::make_shared>( - "PauliZ", std::vector{1}); + "PauliZ", std::vector{1}); auto ob = HamiltonianMPI::create({0.3, 0.5}, {X0, Z1}); auto res = m.var(*ob); diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/observables/ObservablesGPU.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/observables/ObservablesGPU.hpp index 6d0d0a94e7..fcb6a95b2d 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/observables/ObservablesGPU.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/observables/ObservablesGPU.hpp @@ -56,7 +56,7 @@ class NamedObs final : public NamedObsBase { * @param wires Argument to construct wires. * @param params Argument to construct parameters */ - NamedObs(std::string obs_name, std::vector wires, + NamedObs(std::string obs_name, std::vector wires, std::vector params = {}) : BaseType{obs_name, wires, params} { using Pennylane::Gates::Constant::gate_names; @@ -92,7 +92,7 @@ class HermitianObs final : public HermitianObsBase { * @param matrix Matrix in row major format. * @param wires Wires the observable applies to. */ - HermitianObs(MatrixT matrix, std::vector wires) + HermitianObs(MatrixT matrix, std::vector wires) : BaseType{matrix, wires} {} auto getObsName() const -> std::string final { diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/observables/ObservablesGPUMPI.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/observables/ObservablesGPUMPI.hpp index 955365915a..5d6c6b66b7 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/observables/ObservablesGPUMPI.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/observables/ObservablesGPUMPI.hpp @@ -57,7 +57,7 @@ class NamedObsMPI final : public NamedObsBase { * @param wires Argument to construct wires. * @param params Argument to construct parameters */ - NamedObsMPI(std::string obs_name, std::vector wires, + NamedObsMPI(std::string obs_name, std::vector wires, std::vector params = {}) : BaseType{obs_name, wires, params} { using Pennylane::Gates::Constant::gate_names; @@ -93,7 +93,7 @@ class HermitianObsMPI final : public HermitianObsBase { * @param matrix Matrix in row major format. * @param wires Wires the observable applies to. */ - HermitianObsMPI(MatrixT matrix, std::vector wires) + HermitianObsMPI(MatrixT matrix, std::vector wires) : BaseType{matrix, wires} {} auto getObsName() const -> std::string final { @@ -288,7 +288,8 @@ class SparseHamiltonianMPI final : public SparseHamiltonianBase { auto device_id = sv.getDataBuffer().getDevTag().getDeviceID(); auto stream_id = sv.getDataBuffer().getDevTag().getStreamID(); - const size_t length_local = size_t{1} << sv.getNumLocalQubits(); + const std::size_t length_local = std::size_t{1} + << sv.getNumLocalQubits(); std::unique_ptr> d_sv_prime = std::make_unique>(length_local, device_id, diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/observables/tests/Test_ObservablesGPU.cpp b/pennylane_lightning/core/src/simulators/lightning_gpu/observables/tests/Test_ObservablesGPU.cpp index 398f664ffc..f8850ef3ff 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/observables/tests/Test_ObservablesGPU.cpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/observables/tests/Test_ObservablesGPU.cpp @@ -35,13 +35,13 @@ TEMPLATE_PRODUCT_TEST_CASE("NamedObs", "[Observables]", SECTION("Constructibility") { REQUIRE(std::is_constructible_v>); + std::vector>); } SECTION("Constructibility - optional parameters") { - REQUIRE( - std::is_constructible_v, - std::vector>); + REQUIRE(std::is_constructible_v, + std::vector>); } SECTION("Copy constructibility") { @@ -79,7 +79,7 @@ TEMPLATE_PRODUCT_TEST_CASE("HermitianObs", "[Observables]", SECTION("Constructibility") { REQUIRE(std::is_constructible_v>); + std::vector>); } SECTION("Copy constructibility") { @@ -183,9 +183,11 @@ TEMPLATE_PRODUCT_TEST_CASE("Observables::HermitianHasher", "[Observables]", {-0.7071067811865475, 0}}; auto obs1 = - std::make_shared(hermitian_h, std::vector{0}); - auto obs2 = std::make_shared("PauliX", std::vector{2}); - auto obs3 = std::make_shared("PauliX", std::vector{3}); + std::make_shared(hermitian_h, std::vector{0}); + auto obs2 = + std::make_shared("PauliX", std::vector{2}); + auto obs3 = + std::make_shared("PauliX", std::vector{3}); auto tp_obs1 = std::make_shared(obs1, obs2); auto tp_obs2 = std::make_shared(obs2, obs3); @@ -221,11 +223,13 @@ TEMPLATE_PRODUCT_TEST_CASE("Hamiltonian::ApplyInPlace", "[Observables]", const auto h = PrecisionT{0.809}; // half of the golden ratio auto zz = std::make_shared( - std::make_shared("PauliZ", std::vector{0}), - std::make_shared("PauliZ", std::vector{1})); + std::make_shared("PauliZ", std::vector{0}), + std::make_shared("PauliZ", std::vector{1})); - auto x1 = std::make_shared("PauliX", std::vector{0}); - auto x2 = std::make_shared("PauliX", std::vector{1}); + auto x1 = + std::make_shared("PauliX", std::vector{0}); + auto x2 = + std::make_shared("PauliX", std::vector{1}); auto ham = HamiltonianT::create({PrecisionT{1.0}, h, h}, {zz, x1, x2}); diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/observables/tests/mpi/Test_ObservablesGPUMPI.cpp b/pennylane_lightning/core/src/simulators/lightning_gpu/observables/tests/mpi/Test_ObservablesGPUMPI.cpp index bc4d0e517f..b8bdbaabb4 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/observables/tests/mpi/Test_ObservablesGPUMPI.cpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/observables/tests/mpi/Test_ObservablesGPUMPI.cpp @@ -38,13 +38,13 @@ TEMPLATE_PRODUCT_TEST_CASE("NamedObsMPI", "[Observables]", (StateVectorCudaMPI), SECTION("Constructibility") { REQUIRE(std::is_constructible_v>); + std::vector>); } SECTION("Constructibility - optional parameters") { - REQUIRE( - std::is_constructible_v, - std::vector>); + REQUIRE(std::is_constructible_v, + std::vector>); } SECTION("Copy constructibility") { @@ -82,7 +82,7 @@ TEMPLATE_PRODUCT_TEST_CASE("HermitianObsMPI", "[Observables]", SECTION("Constructibility") { REQUIRE(std::is_constructible_v>); + std::vector>); } SECTION("Copy constructibility") { @@ -173,13 +173,13 @@ TEMPLATE_PRODUCT_TEST_CASE("HamiltonianMPI::ApplyInPlace", "[Observables]", MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t num_qubits = 8; - size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; - size_t subSvLength = 1 << nLocalIndexBits; - size_t svLength = 1 << num_qubits; + std::size_t num_qubits = 8; + std::size_t mpi_buffersize = 1; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t subSvLength = 1 << nLocalIndexBits; + std::size_t svLength = 1 << num_qubits; mpi_manager.Barrier(); std::vector expected_sv(svLength); @@ -201,18 +201,22 @@ TEMPLATE_PRODUCT_TEST_CASE("HamiltonianMPI::ApplyInPlace", "[Observables]", const auto h = PrecisionT{0.809}; // half of the golden ratio auto zz = std::make_shared( - std::make_shared("PauliZ", std::vector{0}), - std::make_shared("PauliZ", std::vector{1})); + std::make_shared("PauliZ", std::vector{0}), + std::make_shared("PauliZ", std::vector{1})); - auto x1 = std::make_shared("PauliX", std::vector{0}); - auto x2 = std::make_shared("PauliX", std::vector{1}); + auto x1 = + std::make_shared("PauliX", std::vector{0}); + auto x2 = + std::make_shared("PauliX", std::vector{1}); auto zz0 = std::make_shared( - std::make_shared("PauliZ", std::vector{0}), - std::make_shared("PauliZ", std::vector{1})); + std::make_shared("PauliZ", std::vector{0}), + std::make_shared("PauliZ", std::vector{1})); - auto x10 = std::make_shared("PauliX", std::vector{0}); - auto x20 = std::make_shared("PauliX", std::vector{1}); + auto x10 = + std::make_shared("PauliX", std::vector{0}); + auto x20 = + std::make_shared("PauliX", std::vector{1}); auto ham = HamiltonianT::create({PrecisionT{1.0}, h, h}, {zz, x1, x2}); auto ham0 = Hamiltonian::create({PrecisionT{1.0}, h, h}, {zz0, x10, x20}); @@ -265,9 +269,11 @@ TEMPLATE_PRODUCT_TEST_CASE("Observables::HermitianHasherMPI", "[Observables]", {-0.7071067811865475, 0}}; auto obs1 = - std::make_shared(hermitian_h, std::vector{0}); - auto obs2 = std::make_shared("PauliX", std::vector{2}); - auto obs3 = std::make_shared("PauliX", std::vector{3}); + std::make_shared(hermitian_h, std::vector{0}); + auto obs2 = + std::make_shared("PauliX", std::vector{2}); + auto obs3 = + std::make_shared("PauliX", std::vector{3}); auto tp_obs1 = std::make_shared(obs1, obs2); auto tp_obs2 = std::make_shared(obs2, obs3); @@ -307,11 +313,11 @@ TEMPLATE_PRODUCT_TEST_CASE("SparseHamiltonian::ApplyInPlace", "[Observables]", ComplexT{1.0, 0.0}, ComplexT{1.0, 0.0}}, {7, 6, 5, 4, 3, 2, 1, 0}, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {0, 1, 2}); - size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; - size_t subSvLength = 1 << nLocalIndexBits; + std::size_t mpi_buffersize = 1; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t subSvLength = 1 << nLocalIndexBits; mpi_manager.Barrier(); std::vector expected_sv(subSvLength); diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/tests/Test_StateVectorCudaManaged.cpp b/pennylane_lightning/core/src/simulators/lightning_gpu/tests/Test_StateVectorCudaManaged.cpp index e37ca77510..924dab1376 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/tests/Test_StateVectorCudaManaged.cpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/tests/Test_StateVectorCudaManaged.cpp @@ -55,8 +55,8 @@ TEMPLATE_PRODUCT_TEST_CASE("StateVectorCudaManaged::Constructibility", SECTION("StateVectorBackend") { REQUIRE(!std::is_constructible_v); } - SECTION("StateVectorBackend {ComplexT*, size_t}") { - REQUIRE(std::is_constructible_v); + SECTION("StateVectorBackend {ComplexT*, std::size_t}") { + REQUIRE(std::is_constructible_v); } SECTION( "StateVectorBackend {const StateVectorBackend&}") { @@ -76,7 +76,7 @@ TEMPLATE_PRODUCT_TEST_CASE("StateVectorCudaManaged::getDataVector", using VectorT = TestVector>; std::mt19937_64 re{1337}; - const size_t num_qubits = 4; + const std::size_t num_qubits = 4; VectorT st_data = createRandomStateVectorData(re, num_qubits); StateVectorT state_vector(reinterpret_cast(st_data.data()), st_data.size()); @@ -97,7 +97,7 @@ TEMPLATE_PRODUCT_TEST_CASE( SECTION("Test wrong matrix size") { std::vector m(7, 0.0); - const size_t num_qubits = 4; + const std::size_t num_qubits = 4; VectorT st_data = createRandomStateVectorData(re, num_qubits); StateVectorT state_vector(reinterpret_cast(st_data.data()), @@ -110,7 +110,7 @@ TEMPLATE_PRODUCT_TEST_CASE( SECTION("Test wrong number of wires") { std::vector m(8, 0.0); - const size_t num_qubits = 4; + const std::size_t num_qubits = 4; VectorT st_data = createRandomStateVectorData(re, num_qubits); @@ -134,7 +134,7 @@ TEMPLATE_PRODUCT_TEST_CASE("StateVectorCudaManaged::applyMatrix with a pointer", SECTION("Test wrong matrix") { std::vector m(8, 0.0); - const size_t num_qubits = 4; + const std::size_t num_qubits = 4; VectorT st_data = createRandomStateVectorData(re, num_qubits); @@ -147,7 +147,7 @@ TEMPLATE_PRODUCT_TEST_CASE("StateVectorCudaManaged::applyMatrix with a pointer", SECTION("Test a matrix represent PauliX") { std::vector m = { {0.0, 0.0}, {1.0, 0.0}, {1.0, 0.0}, {0.0, 0.0}}; - const size_t num_qubits = 4; + const std::size_t num_qubits = 4; VectorT st_data = createRandomStateVectorData(re, num_qubits); @@ -173,7 +173,7 @@ TEMPLATE_PRODUCT_TEST_CASE("StateVectorCudaManaged::applyOperations", std::mt19937_64 re{1337}; SECTION("Test invalid arguments without parameters") { - const size_t num_qubits = 4; + const std::size_t num_qubits = 4; VectorT st_data = createRandomStateVectorData(re, num_qubits); @@ -205,7 +205,7 @@ TEMPLATE_PRODUCT_TEST_CASE("StateVectorCudaManaged::applyOperations", } SECTION("Test invalid arguments with parameters") { - const size_t num_qubits = 4; + const std::size_t num_qubits = 4; VectorT st_data = createRandomStateVectorData(re, num_qubits); @@ -238,8 +238,8 @@ TEMPLATE_TEST_CASE("StateVectorCudaManaged::StateVectorCudaManaged", std::mt19937_64 re{1337}; SECTION("StateVectorCudaManaged {size_t}") { - REQUIRE(std::is_constructible_v); - const size_t num_qubits = 4; + REQUIRE(std::is_constructible_v); + const std::size_t num_qubits = 4; StateVectorT sv(num_qubits); REQUIRE(sv.getNumQubits() == 4); @@ -247,10 +247,10 @@ TEMPLATE_TEST_CASE("StateVectorCudaManaged::StateVectorCudaManaged", REQUIRE(sv.getDataVector().size() == 16); } - SECTION("StateVectorCudaManaged {ComplexT *, size_t}") { + SECTION("StateVectorCudaManaged {ComplexT *, std::size_t}") { using TestVectorT = TestVector>; - REQUIRE(std::is_constructible_v); - const size_t num_qubits = 5; + REQUIRE(std::is_constructible_v); + const std::size_t num_qubits = 5; TestVectorT st_data = createRandomStateVectorData(re, num_qubits); StateVectorT sv(reinterpret_cast(st_data.data()), diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/tests/mpi/Test_StateVectorCudaMPI.cpp b/pennylane_lightning/core/src/simulators/lightning_gpu/tests/mpi/Test_StateVectorCudaMPI.cpp index ddf3230eec..be6c68d1c5 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/tests/mpi/Test_StateVectorCudaMPI.cpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/tests/mpi/Test_StateVectorCudaMPI.cpp @@ -61,29 +61,31 @@ TEMPLATE_PRODUCT_TEST_CASE("StateVectorCudaMPI::Constructibility", SECTION("StateVectorBackend") { REQUIRE(!std::is_constructible_v); } - SECTION("StateVectorBackend {MPIManager, DevTag, size_t, " - "size_t, size_t}") { + SECTION( + "StateVectorBackend {MPIManager, DevTag, std::size_t, " + "size_t, std::size_t}") { REQUIRE(std::is_constructible_v, - size_t, size_t, size_t>); + std::size_t, std::size_t, std::size_t>); } - SECTION("StateVectorBackend {MPI_Comm, DevTag, size_t, " - "size_t, size_t}") { + SECTION("StateVectorBackend {MPI_Comm, DevTag, std::size_t, " + "size_t, std::size_t}") { REQUIRE(std::is_constructible_v, - size_t, size_t, size_t>); + std::size_t, std::size_t, std::size_t>); } - SECTION( - "StateVectorBackend {DevTag, size_t, size_t, size_t}") { - REQUIRE(std::is_constructible_v, size_t, - size_t, size_t>); + SECTION("StateVectorBackend {DevTag, std::size_t, " + "std::size_t, std::size_t}") { + REQUIRE(std::is_constructible_v, std::size_t, + std::size_t, std::size_t>); } - SECTION( - "StateVectorBackend {DevTag, size_t, size_t, CFP_t}") { - REQUIRE(std::is_constructible_v, size_t, - size_t, CFP_t *>); + SECTION("StateVectorBackend {DevTag, std::size_t, " + "std::size_t, CFP_t}") { + REQUIRE(std::is_constructible_v, std::size_t, + std::size_t, CFP_t *>); } - SECTION("StateVectorBackend {DevTag, size_t, size_t}") { - REQUIRE( - std::is_constructible_v, size_t, size_t>); + SECTION("StateVectorBackend {DevTag, std::size_t, " + "std::size_t}") { + REQUIRE(std::is_constructible_v, std::size_t, + std::size_t>); } SECTION( "StateVectorBackend {const StateVectorBackend&}") { @@ -98,16 +100,16 @@ TEMPLATE_PRODUCT_TEST_CASE("StateVectorCudaMPI::applyMatrix with a std::vector", using PrecisionT = typename StateVectorT::PrecisionT; using ComplexT = typename StateVectorT::ComplexT; - const size_t num_qubits = 4; + const std::size_t num_qubits = 4; MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 128; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; - size_t subSvLength = 1 << nLocalIndexBits; + std::size_t mpi_buffersize = 128; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t subSvLength = 1 << nLocalIndexBits; mpi_manager.Barrier(); std::vector local_state(subSvLength); @@ -157,16 +159,16 @@ TEMPLATE_PRODUCT_TEST_CASE("StateVectorCudaMPI::applyMatrix with a pointer", using PrecisionT = typename StateVectorT::PrecisionT; using ComplexT = typename StateVectorT::ComplexT; - const size_t num_qubits = 4; + const std::size_t num_qubits = 4; MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; - size_t subSvLength = 1 << nLocalIndexBits; + std::size_t mpi_buffersize = 1; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t subSvLength = 1 << nLocalIndexBits; mpi_manager.Barrier(); std::vector local_state(subSvLength); @@ -220,16 +222,16 @@ TEMPLATE_PRODUCT_TEST_CASE("StateVectorCudaMPI::applyOperations", using PrecisionT = typename StateVectorT::PrecisionT; using ComplexT = typename StateVectorT::ComplexT; - const size_t num_qubits = 4; + const std::size_t num_qubits = 4; MPIManager mpi_manager(MPI_COMM_WORLD); REQUIRE(mpi_manager.getSize() == 2); - size_t mpi_buffersize = 1; - size_t nGlobalIndexBits = - std::bit_width(static_cast(mpi_manager.getSize())) - 1; - size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; - size_t subSvLength = 1 << nLocalIndexBits; + std::size_t mpi_buffersize = 1; + std::size_t nGlobalIndexBits = + std::bit_width(static_cast(mpi_manager.getSize())) - 1; + std::size_t nLocalIndexBits = num_qubits - nGlobalIndexBits; + std::size_t subSvLength = 1 << nLocalIndexBits; mpi_manager.Barrier(); std::vector local_state(subSvLength); diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/utils/MPI_helpers.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/utils/MPI_helpers.hpp index 5a86dcefe7..fd9b4edcbf 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/utils/MPI_helpers.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/utils/MPI_helpers.hpp @@ -100,17 +100,17 @@ inline std::vector createWirePairs(int numLocalQubits, int numTotalQubits, * @param tgtswirePairs Vector of wire pairs for MPI operation. */ -inline void tgtsVecProcess(const size_t numLocalQubits, - const size_t numTotalQubits, +inline void tgtsVecProcess(const std::size_t numLocalQubits, + const std::size_t numTotalQubits, const std::vector> &tgts, - std::vector> &localTgts, + std::vector> &localTgts, std::vector &tgtsSwapStatus, std::vector> &tgtswirePairs) { - std::vector> tgtsIntTrans; + std::vector> tgtsIntTrans; tgtsIntTrans.reserve(tgts.size()); for (const auto &vec : tgts) { - std::vector tmpVecInt( + std::vector tmpVecInt( vec.size()); // Reserve memory for efficiency std::transform(vec.begin(), vec.end(), tmpVecInt.begin(), @@ -125,17 +125,17 @@ inline void tgtsVecProcess(const size_t numLocalQubits, statusWires[v] = WireStatus::Target; } - size_t StatusGlobalWires = std::reduce( + std::size_t StatusGlobalWires = std::reduce( statusWires.begin() + numLocalQubits, statusWires.end()); if (!StatusGlobalWires) { tgtsSwapStatus.push_back(WiresSwapStatus::Local); localTgts.push_back(vec); } else { - size_t counts_global_wires = std::count_if( + std::size_t counts_global_wires = std::count_if( statusWires.begin(), statusWires.begin() + numLocalQubits, [](int i) { return i != WireStatus::Default; }); - size_t counts_local_wires_avail = + std::size_t counts_local_wires_avail = numLocalQubits - (vec.size() - counts_global_wires); // Check if there are sufficent number of local wires for bit // swap @@ -147,10 +147,10 @@ inline void tgtsVecProcess(const size_t numLocalQubits, [&](size_t x) { return static_cast(x); }); auto wirePairs = createWirePairs(numLocalQubits, numTotalQubits, localVec, statusWires); - std::vector localVecSizeT(localVec.size()); - std::transform(localVec.begin(), localVec.end(), - localVecSizeT.begin(), - [&](int x) { return static_cast(x); }); + std::vector localVecSizeT(localVec.size()); + std::transform( + localVec.begin(), localVec.end(), localVecSizeT.begin(), + [&](int x) { return static_cast(x); }); localTgts.push_back(localVecSizeT); tgtswirePairs.push_back(wirePairs); } else { diff --git a/pennylane_lightning/core/src/simulators/lightning_kokkos/StateVectorKokkos.hpp b/pennylane_lightning/core/src/simulators/lightning_kokkos/StateVectorKokkos.hpp index d691526661..5ef82d138a 100644 --- a/pennylane_lightning/core/src/simulators/lightning_kokkos/StateVectorKokkos.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_kokkos/StateVectorKokkos.hpp @@ -85,7 +85,7 @@ class StateVectorKokkos final Kokkos::View>; using UnmanagedConstSizeTHostView = - Kokkos::View>; using UnmanagedPrecisionHostView = Kokkos::View &wires, bool inverse = false, + const std::vector &wires, + bool inverse = false, const std::vector ¶ms = {}, const std::vector &gate_matrix = {}) { if (opName == "Identity") { @@ -306,9 +307,10 @@ class StateVectorKokkos final * @param gate_matrix Optional std gate matrix if opName doesn't exist. */ void applyOperation(const std::string &opName, - const std::vector &controlled_wires, + const std::vector &controlled_wires, const std::vector &controlled_values, - const std::vector &wires, bool inverse = false, + const std::vector &wires, + bool inverse = false, const std::vector ¶ms = {}, const std::vector &gate_matrix = {}) { PL_ABORT_IF_NOT(controlled_wires.empty(), @@ -386,10 +388,11 @@ class StateVectorKokkos final * @param wires Wires to apply gate to. * @param inverse Indicate whether inverse should be taken. */ - inline void applyMatrix(ComplexT *matrix, const std::vector &wires, + inline void applyMatrix(ComplexT *matrix, + const std::vector &wires, bool inverse = false) { PL_ABORT_IF(wires.empty(), "Number of wires must be larger than 0"); - size_t n = static_cast(1U) << wires.size(); + std::size_t n = static_cast(1U) << wires.size(); KokkosVector matrix_(matrix, n * n); applyMultiQubitOp(matrix_, wires, inverse); } @@ -402,7 +405,7 @@ class StateVectorKokkos final * @param inverse Indicate whether inverse should be taken. */ inline void applyMatrix(std::vector &matrix, - const std::vector &wires, + const std::vector &wires, bool inverse = false) { PL_ABORT_IF(wires.empty(), "Number of wires must be larger than 0"); PL_ABORT_IF(matrix.size() != exp2(2 * wires.size()), @@ -420,11 +423,11 @@ class StateVectorKokkos final * @param inverse Indicate whether inverse should be taken. */ inline void applyMatrix(const ComplexT *matrix, - const std::vector &wires, + const std::vector &wires, bool inverse = false) { PL_ABORT_IF(wires.empty(), "Number of wires must be larger than 0"); - size_t n = static_cast(1U) << wires.size(); - size_t n2 = n * n; + std::size_t n = static_cast(1U) << wires.size(); + std::size_t n2 = n * n; KokkosVector matrix_("matrix_", n2); Kokkos::deep_copy(matrix_, UnmanagedConstComplexHostView(matrix, n2)); applyMultiQubitOp(matrix_, wires, inverse); @@ -438,7 +441,7 @@ class StateVectorKokkos final * @param inverse Indicate whether inverse should be taken. */ inline void applyMatrix(const std::vector &matrix, - const std::vector &wires, + const std::vector &wires, bool inverse = false) { PL_ABORT_IF(wires.empty(), "Number of wires must be larger than 0"); PL_ABORT_IF(matrix.size() != exp2(2 * wires.size()), @@ -448,7 +451,7 @@ class StateVectorKokkos final } void applyNamedOperation(const std::string &opName, - const std::vector &wires, + const std::vector &wires, bool inverse = false, const std::vector ¶ms = {}) { switch (reverse_lookup(gate_names, std::string_view{opName})) { @@ -576,7 +579,8 @@ class StateVectorKokkos final * @param params Optional parameter list for parametric gates. */ auto applyGenerator(const std::string &opName, - const std::vector &wires, bool inverse = false, + const std::vector &wires, + bool inverse = false, const std::vector ¶ms = {}) -> fp_t { switch (reverse_lookup(generator_names, std::string_view{opName})) { case GeneratorOperation::RX: @@ -666,7 +670,7 @@ class StateVectorKokkos final * @param params parameters for this gate */ template