Skip to content

Commit debd776

Browse files
AmintorDuskomlxdchaeyeunpark
authored
Implements native support for Sparse Hamiltonians expval() calculation with Kokkos (#283)
* add Kokkos building and test options * Auto update version * Auto update version * checking threads in windows * add Kokkos serial test * set Kokkos Serial device for Win and Mac * Increase maximum full-path length allowed in Win * oops wrong size * set Win wheel to be built without Ninja * setup clang-cl for Win * implement methods to apply a sparse matrix to a vector with Kokkos * Implement the expected value calculation of a Sparse Hamiltonian * add tests * Auto update version * improve format * set Kokkos and Kokkos Kernel directories as system type to avoid clang-tidy inspection * add python tests * add python/C++ interface * make Kokkos installations Position Independent Code * Auto update version * Trigger CI * set environment variables for Kokkos * remove set environment variables for Kokkos * long path in Windows * fix windows wheel * codecov * long path * powershell command * default generator for windows * remove ilammy from Win Wheel * powershell command back * comment CMAKE_OBJECT_PATH_MAX * set CMAKE_OBJECT_PATH_MAX * Auto update version * Trigger CI * Reduce character limit * Auto update version * Fix veresion * Add shell=True in cmake subprocess for Windows * Add shell=True in cmake subprocess for Windows * Call cibuildwheel directly from the shell * Revert shell * improve Python bindings * change expval of sparse Hamiltonians to use pointers as input * update the new PL_REQUIRE_THROWS_MATCHES and PL_CHECK_THROWS_MATCHES to work nicely with PL_ABORT_IF * update Measures.hpp safety checks to PL_ABORT_IF * update c++ tests * update Kokkos and Kokkos Kernels version * fix format * improve documentation * simplify cmake file and set Serial device for all cases. * update CHANGELOG * Auto update version * Trigger CI * update CHANGELOG * remove use_csingle * Update .github/CHANGELOG.md Co-authored-by: Lee James O'Riordan <[email protected]> * update python tests and bring use_csingle back * format * update test options * remove use_csingle * remove references ENABLE_THREADS * win64_path_fix: ensure paths are x-platform compatible * win64_path_fix: Manually unpack and modify wheel / Fix pwsh * codefactor * add collaboration * tag windows as latest * uncommenting release line * testing size_t as index type in the new Kokkos version and reducing preprocessor statements * set index_type as long int * remove underscore and Enable Kokkos as a compile option * fix typo * removing preprocessor ifdef statements * refactor apply_Sparse_Matrix * move and update some helpers and update tests for measures * places tests for sparse measures in its own file * codefactor * Tidy * fix element placement * Trigger CI * Trigger CI * Default Kokkos to OFF * tidying up test-cpp-kokkos * add Kokkos tests for Linux * default expval to default.qubit in a no-Kokkos scenario * format * codefactor * add maybe unused to no Kokkos apply_Sparse_Matrix * add tests with Kokkos in Windows * clang-cl compiler * improve PL_ABORT message * Enable long path in Win * set location * Auto-update version * set a file for sparse measures python tests * expand python tests * expand c++ coverage * format * refactor apply_Sparse_Matrix and expand python test coverage in Linux. * codefactor * add setup install * exclude line in coverage * update tests * remove codecov comment * improve python tests * testing no cover * undo test * test exclude block from codecov analysis * test merge tests and upgrade Linux * update tests_linux * add unit tests for Kokkos Sparse * add control to throwing exceptions and remove unnecessary attributes * remove unnecessary tests * include Acknowledgements in the Readme file Co-authored-by: Lee J. O'Riordan <[email protected]> Co-authored-by: Chae-Yeun Park <[email protected]>
1 parent e4db7f1 commit debd776

24 files changed

+1132
-148
lines changed

.github/CHANGELOG.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ dev2 = qml.device('lightning.qubit', wires=4, c_dtype=np.complex128) # for doubl
2020
* Split matrix operations, refactor dispatch mechanisms, and add a benchmark suits.
2121
[(#274)](https://github.com/PennyLaneAI/pennylane-lightning/pull/274)
2222

23+
* Add native support for the calculation of sparse Hamiltonians' expectation values. Sparse operations are offloaded to [Kokkos](https://github.com/kokkos/kokkos) and [Kokkos-Kernels](https://github.com/kokkos/kokkos-kernels).
24+
[(#283)](https://github.com/PennyLaneAI/pennylane-lightning/pull/283)
25+
2326
### Documentation
2427

2528
* Use the centralized [Xanadu Sphinx Theme](https://github.com/XanaduAI/xanadu-sphinx-theme)
@@ -35,7 +38,7 @@ dev2 = qml.device('lightning.qubit', wires=4, c_dtype=np.complex128) # for doubl
3538

3639
This release contains contributions from (in alphabetical order):
3740

38-
Mikhail Andrenkov, Ali Asadi, Chae-Yeun Park
41+
Ali Asadi, Amintor Dusko, Chae-Yeun Park, Lee James O'Riordan, Mikhail Andrenkov
3942

4043
---
4144

.github/workflows/tests_linux.yml

+109
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,60 @@ jobs:
132132
files: ./BuildCov/coverage.info
133133
fail_ci_if_error: true
134134

135+
cpptestswithkokkos:
136+
name: C++ tests (Linux, Kokkos and Kokkos Kernels)
137+
runs-on: ${{ matrix.os }}
138+
strategy:
139+
matrix:
140+
os: [ubuntu-20.04]
141+
steps:
142+
- name: Cancel previous runs
143+
uses: styfle/[email protected]
144+
with:
145+
access_token: ${{ github.token }}
146+
147+
- uses: actions/setup-python@v2
148+
name: Install Python
149+
with:
150+
python-version: '3.7'
151+
152+
- uses: actions/checkout@v2
153+
with:
154+
fetch-depth: 2
155+
156+
- name: Install dependencies
157+
run: sudo apt-get update && sudo apt-get -y -q install cmake gcc g++ gcovr lcov
158+
159+
- name: Build and run unit tests for code coverage
160+
run: |
161+
cmake pennylane_lightning/src -BBuildCov -DCMAKE_BUILD_TYPE=RelWithDebInfo -DBUILD_TESTS=ON -DENABLE_COVERAGE=ON -DENABLE_KOKKOS=ON
162+
cmake --build ./BuildCov
163+
cd ./BuildCov
164+
mkdir -p ./tests/results
165+
./tests/runner --order lex --reporter junit --out ./tests/results/report.xml
166+
lcov --directory . -b ../pennylane_lightning/src --capture --output-file coverage.info
167+
lcov --remove coverage.info '/usr/*' --output-file coverage.info
168+
169+
- name: Upload coverage to Codecov
170+
uses: codecov/codecov-action@v2
171+
with:
172+
files: ./BuildCov/coverage.info
173+
fail_ci_if_error: true
174+
175+
- name: Upload test results
176+
uses: actions/upload-artifact@v2
177+
if: always()
178+
with:
179+
name: ubuntu-test-report
180+
path: BuildCov/tests/results/report.xml
181+
182+
- name: Publish test results
183+
uses: EnricoMi/publish-unit-test-result-action@v1
184+
if: always()
185+
with:
186+
check_name: Test Report (C++) on Ubuntu
187+
files: BuildCov/tests/results/report.xml
188+
135189
cppbenchmarksuite:
136190
name: C++ Benchmark Suite (Linux, OpenBLAS)
137191
runs-on: ${{ matrix.os }}
@@ -216,3 +270,58 @@ jobs:
216270
with:
217271
files: ./main/coverage.xml
218272
fail_ci_if_error: true
273+
274+
pythontestsKokkos:
275+
name: Python tests with Kokkos and Kokkos Kernels
276+
runs-on: ${{ matrix.os }}
277+
strategy:
278+
matrix:
279+
os: [ubuntu-20.04]
280+
281+
steps:
282+
- name: Checkout PennyLane-Lightning
283+
uses: actions/checkout@v2
284+
with:
285+
path: main
286+
fetch-depth: 2
287+
288+
- uses: actions/setup-python@v2
289+
name: Install Python
290+
with:
291+
python-version: '3.7'
292+
293+
- name: Install dependencies
294+
run: sudo apt-get update && sudo apt-get -y -q install cmake gcc g++
295+
296+
- name: Get required Python packages
297+
run: |
298+
cd main
299+
python -m pip install --upgrade pip
300+
pip install -r requirements.txt
301+
pip uninstall pennylane -y
302+
pip install git+https://github.com/PennyLaneAI/pennylane.git
303+
304+
- name: Install ML libraries for interfaces
305+
run: |
306+
pip install --upgrade torch==$TORCH_VERSION -f https://download.pytorch.org/whl/cpu/torch_stable.html
307+
pip install --upgrade "jax[cpu]" # This also installs jaxlib
308+
pip install --upgrade tensorflow==$TF_VERSION keras==$TF_VERSION
309+
310+
- name: Install lightning.qubit device
311+
run: |
312+
cd main
313+
python setup.py build_ext --define="ENABLE_KOKKOS=ON"
314+
pip install -e .
315+
316+
- name: Run PennyLane-Lightning unit tests
317+
run: |
318+
cd main/
319+
pytest tests/ $COVERAGE_FLAGS
320+
pl-device-test --device lightning.qubit --skip-ops --shots=20000 $COVERAGE_FLAGS --cov-append
321+
pl-device-test --device lightning.qubit --shots=None --skip-ops $COVERAGE_FLAGS --cov-append
322+
323+
- name: Upload coverage to Codecov
324+
uses: codecov/codecov-action@v2
325+
with:
326+
files: ./main/coverage.xml
327+
fail_ci_if_error: true

.github/workflows/tests_windows.yml

+52
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,55 @@ jobs:
5151
with:
5252
files: ./coverage.xml
5353
fail_ci_if_error: true
54+
55+
cpptestswithkokkos:
56+
name: C++ tests (Windows, Kokkos and Kokkos Kernels)
57+
runs-on: ${{ matrix.os }}
58+
strategy:
59+
matrix:
60+
os: [windows-latest]
61+
steps:
62+
- name: Cancel previous runs
63+
uses: styfle/[email protected]
64+
with:
65+
access_token: ${{ github.token }}
66+
- uses: actions/checkout@v2
67+
with:
68+
fetch-depth: 2
69+
70+
- name: Configure MSVC for amd64 # Use cl.exe as a default compiler
71+
uses: ilammy/msvc-dev-cmd@v1
72+
with:
73+
arch: amd64
74+
75+
- name: Setup OpenCppCoverage and add to PATH
76+
run: |
77+
choco install OpenCppCoverage -y
78+
echo "C:\Program Files\OpenCppCoverage" >> $env:GITHUB_PATH
79+
80+
- name: Enable long paths
81+
run: |
82+
powershell.exe New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" -Name "LongPathsEnabled" -Value 1 -PropertyType DWORD -Force
83+
84+
- name: Build and run unit tests for code coverage
85+
run: |
86+
Subst Z: (pwd)
87+
Set-Location -Path "Z:\"
88+
cmake ./pennylane_lightning/src -BBuild -DBUILD_TESTS=ON -DENABLE_KOKKOS=ON -DENABLE_OPENMP=OFF -DENABLE_WARNINGS=OFF -T clangcl
89+
cmake --build ./Build --config Debug
90+
mkdir -p ./Build/tests/results
91+
.\Build\tests\Debug\runner.exe --order lex --reporter junit --out .\Build\tests\results\report.xml
92+
OpenCppCoverage --sources pennylane_lightning\src --export_type cobertura:coverage.xml Build\tests\Debug\runner.exe
93+
94+
- name: Upload test results
95+
uses: actions/upload-artifact@v2
96+
if: always()
97+
with:
98+
name: windows-test-report
99+
path: .\Build\tests\results\report.xml
100+
101+
- name: Upload Report to Codecov
102+
uses: codecov/codecov-action@v2
103+
with:
104+
files: ./coverage.xml
105+
fail_ci_if_error: true

.github/workflows/wheel_win_x86_64.yml

+36-26
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,6 @@ on:
88
types: [published]
99

1010
env:
11-
CIBW_BUILD: 'cp37-* cp38-* cp39-* cp310-*'
12-
13-
CIBW_ARCHS_WINDOWS: "AMD64"
14-
15-
# Python build settings
16-
CIBW_BEFORE_BUILD: |
17-
pip install pybind11 ninja cmake
18-
19-
# Testing of built wheels
20-
CIBW_TEST_REQUIRES: numpy~=1.21 scipy pytest pytest-cov pytest-mock flaky
21-
22-
CIBW_BEFORE_TEST: |
23-
pip install git+https://github.com/PennyLaneAI/pennylane.git@master
24-
python -c "import sys; print(sys.path)"
25-
26-
CIBW_TEST_COMMAND: |
27-
pl-device-test --device=lightning.qubit --skip-ops -x --tb=short --no-flaky-report
28-
2911
DISTUTILS_USE_SDK: 1
3012

3113
MSSdk: 1
@@ -36,6 +18,7 @@ jobs:
3618
fail-fast: false
3719
matrix:
3820
os: [windows-latest]
21+
py_version: ["3.7", "3.8", "3.9", "3.10"]
3922
name: ${{ matrix.os }}
4023
runs-on: ${{ matrix.os }}
4124

@@ -49,20 +32,46 @@ jobs:
4932
id: branch-name
5033
uses: tj-actions/branch-names@v5
5134

52-
- uses: actions/checkout@v2
35+
- name: Enable long paths
36+
run: |
37+
powershell.exe New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" -Name "LongPathsEnabled" -Value 1 -PropertyType DWORD -Force
38+
39+
- uses: actions/checkout@v3
5340

5441
- uses: actions/setup-python@v2
5542
name: Install Python
5643
with:
57-
python-version: '3.7'
44+
python-version: ${{ matrix.py_version }}
5845

59-
- name: Configure MSVC for amd64 # Use cl.exe as a default compiler
60-
uses: ilammy/msvc-dev-cmd@v1
61-
with:
62-
arch: amd64
46+
- name: Install cibuildwheel
47+
run: python -m pip install cibuildwheel==2.3.0
6348

6449
- name: Build wheels
65-
uses: pypa/[email protected]
50+
run: |
51+
Subst Z: (pwd)
52+
Set-Location -Path "Z:\"
53+
python -m pip install wheel cmake ninja build
54+
python setup.py bdist_wheel
55+
Set-Location -Path "./dist"
56+
foreach ($i in Get-ChildItem -Path ./*.whl){
57+
python -m wheel unpack $i.Name
58+
$name = $i.Name
59+
$dirName = python -c "s = '$name'; print('-'.join(s.split('-')[0:2]))"
60+
if (Test-Path -Path $dirName\pennylane_lightning\Debug) {
61+
Move-Item -Path $dirName\pennylane_lightning\Debug\* -Destination $dirName\pennylane_lightning
62+
Remove-Item $dirName\pennylane_lightning\Debug -Recurse
63+
python -m wheel pack $dirName
64+
}
65+
Remove-Item $dirName -Recurse
66+
}
67+
68+
- name: Test wheel
69+
run: |
70+
Set-Location -Path "Z:\"
71+
foreach ($i in Get-ChildItem -Path Z:\dist\*.whl){ python -m pip install $i }
72+
python -m pip install git+https://github.com/PennyLaneAI/pennylane.git@master && `
73+
python -m pip install pytest pytest-cov pytest-mock flaky && `
74+
pl-device-test --device=lightning.qubit --skip-ops -x --tb=short --no-flaky-report
6675
6776
- uses: actions-ecosystem/action-regex-match@v2
6877
id: rc_build
@@ -74,7 +83,8 @@ jobs:
7483
if: ${{ github.event_name == 'release' || github.ref == 'refs/heads/master' || steps.rc_build.outputs.match != ''}}
7584
with:
7685
name: ${{ runner.os }}-wheels.zip
77-
path: ./wheelhouse/*.whl
86+
path: Z:\dist\*.whl
87+
7888

7989
upload-pypi:
8090
needs: win-wheels

CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.14)
1+
cmake_minimum_required(VERSION 3.16)
22

33
set(LOGO [=[
44
░█░░░▀█▀░█▀▀░█░█░▀█▀░█▀█░▀█▀░█▀█░█▀▀░░░░▄▀▄░█░█░█▀▄░▀█▀░▀█▀
@@ -50,6 +50,7 @@ option(ENABLE_WARNINGS "Enable warnings" ON)
5050
option(ENABLE_NATIVE "Enable native CPU build tuning" OFF)
5151
option(ENABLE_AVX "Enable AVX support" OFF)
5252
option(ENABLE_OPENMP "Enable OpenMP" ON)
53+
option(ENABLE_KOKKOS "Enable Kokkos" OFF)
5354
option(ENABLE_BLAS "Enable BLAS" OFF)
5455

5556
# Other build options

Makefile

+13
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,19 @@ test-cpp-omp:
9292
cmake --build ./BuildTests --target runner
9393
cmake --build ./BuildTests --target test
9494

95+
test-cpp-kokkos:
96+
rm -rf ./BuildTests
97+
cmake $(LIGHTNING_CPP_DIR) -BBuildTests -DBUILD_TESTS=ON -DENABLE_KOKKOS=ON
98+
cmake --build ./BuildTests --target runner
99+
cmake --build ./BuildTests --target test
100+
101+
.PHONY: benchmark
102+
benchmark:
103+
cmake --build BuildBench --target clean || true
104+
rm -rf ./BuildBench/CMakeCache.txt ./BuildBench/compiler_info.txt ./BuildBench/run_gate_benchmark.sh
105+
cmake $(LIGHTNING_CPP_DIR) -BBuildBench -DBUILD_EXAMPLES=ON -DCMAKE_BUILD_TYPE=Release -DENABLE_AVX=ON
106+
cmake --build ./BuildBench
107+
95108
.PHONY: gbenchmark
96109
gbenchmark:
97110
rm -rf ./BuildGBench

README.rst

+12-4
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,9 @@ To test the C++ code:
141141
$ make
142142
143143
Other supported options are ``-DENABLE_WARNINGS=ON``,
144-
``-DENABLE_NATIVE=ON`` (for ``-march=native``),
145-
``-DENALBE_OPENMP=ON``, ``-DENALBE_BLAS=ON``, and
146-
``-DENABLE_CLANG_TIDY=ON``.
147-
144+
``-DENABLE_NATIVE=ON`` (for ``-march=native``),
145+
``-DENABLE_BLAS=ON``, ``-DENABLE_OPENMP=ON``,
146+
``-DENABLE_KOKKOS=ON``, and ``-DENABLE_CLANG_TIDY=ON``.
148147

149148
Compile on Windows with MSVC
150149
----------------------------
@@ -237,3 +236,12 @@ The PennyLane lightning plugin is **free** and **open source**, released under
237236
the `Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>`_.
238237

239238
.. license-end-inclusion-marker-do-not-remove
239+
240+
Acknowledgements
241+
================
242+
243+
PennyLane Lightning makes use of the following libraries and tools, which are under their own respective licenses:
244+
245+
- **pybind11:** https://pybind11.readthedocs.io/en/stable/ [`License <https://github.com/pybind/pybind11/blob/master/LICENSE>`_]
246+
- **Kokkos Core:** https://github.com/kokkos/kokkos [`License <https://github.com/kokkos/kokkos/blob/master/LICENSE>`_]
247+
- **Kokkos Kernels:** https://github.com/kokkos/kokkos-kernels [`License <https://github.com/kokkos/kokkos-kernels/blob/master/LICENSE>`_]

0 commit comments

Comments
 (0)