Skip to content

Commit

Permalink
Add support for float and double precision data (#113)
Browse files Browse the repository at this point in the history
* Initial reformat and templating of gate tests

* Redefine gate mappings with templates

* Record given state

* Move all gate classes to become StateVector methods

* Fix compile errors for SV

* Refactor gates implementation

* Enable vector param methods

* Enable label to gatename map

* Support dispatch directly from StateVector class

* Add apply methods to SV class

* Add log2 utility function

* Remove old file arch

* Ensure bindings build support for float and double sizes complex data

* Remove outdated tests for deleted modules

* Remove old definition headers

* Tidy dispatch map

* Replace header with correct type

* Ensure cpp17 is now used

* Remove unneeded files in compilation

* Ensure apply args are given in correct order

* Allow make test to be run from working dir without cleaning

* Fix cpp formatting

* Remove old code and fix codefactor complaints

* Add cast to enable wheel build on MacOS

* Enable MSVC intrinsics if using Windows

* Ensure C++17 as a requirement

* Fix compile-time ifdef

* Replace intrinsic with BSR

* Avoid intrinsics for portability

* Rename log2 function

* Add support for 64bit and 128 complex numbers from C++ backend

* Update bindings to allow class instantiation and method use

* Enable support for different precision parameters passed to backend

* Fix log2 change

* Fix binding names

* Remove io from statevector

* Refix the log2 -- replace with instrinsics later

* Update format and remove ununsed warnings

* Refactor the testing infrastructure for templated StateVector implementation (#115)

* Add preliminary catch2 support

* Move private methods to public for testing

* Overload applyOperations for param and non param calls

* Add testing support for X,Y,Z,H gates

* Add S, T gate tests

* Add support for RX,RY,RZ gate tests

* Add PhaseShift gate tests

* Add Rot tests

* Add CNOT tests

* Add support for CSWAP, Toffoli, CZ, CRot tests

* Update testing CI

* Fix CodeFactor complaints

* Update CI image before running tests

* Favour use of reverse iterator over counter in for loops

* Fix contructor tests

* Fix formatting

* Fix narrowing complaints

* Fix MSVC math errors

* Run black

* Remove unneeded ops for real*complex products

* Fix test builder

* Ensure cmake has a version to avoid warnings

* Fix formatting of SV

* Fix test reporting

* Remove whitespace for CF complaints

* Apply static analyser fixes

* Add imaginary utils

* Refactor gate implementation and utility definitions

* Fix RY gate defn

* Add compile-time complex multiplication functions

* Use gate definition functions in tests and add constexpr where applicable

* Remove outdated test utilities and tests

* Update changelog

* Port ControlledPhaseShift to new simulator structure

* Add ControlledPhaseShift method to bindings

* Ensure tests are run using cmake

* Rename label CPhaseShift to ControlledPhasedShift
  • Loading branch information
mlxd authored Jul 28, 2021
1 parent 550ee8a commit bdf8aa8
Show file tree
Hide file tree
Showing 30 changed files with 2,335 additions and 2,206 deletions.
20 changes: 17 additions & 3 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,36 @@

### New features

- C++ layer now supports float (32-bit) and double (64-bit) templated complex data ([#113](https://github.com/PennyLaneAI/pennylane-lightning/pull/113))

### Improvements

* Move changelog to `.github` and add a changelog reminder.
- C++ tests have been ported to use Catch2 framework. ([#115](https://github.com/PennyLaneAI/pennylane-lightning/pull/115))

- Testing now exists for both float and double precision methods in C++ layer. ([#113](https://github.com/PennyLaneAI/pennylane-lightning/pull/113),[#115](https://github.com/PennyLaneAI/pennylane-lightning/pull/115))

- Compile-time utility methods with `constexpr` have been added. ([#113](https://github.com/PennyLaneAI/pennylane-lightning/pull/113))

- Wheel-build support for ARM64 (Linux and MacOS) and PowerPC (Linux) added. ([#110](https://github.com/PennyLaneAI/pennylane-lightning/pull/110))

* Add support for Controlled Phase Gate (CPhaseShift).
- Add support for Controlled Phase Gate (CPhaseShift).
[(#112)](https://github.com/PennyLaneAI/pennylane-lightning/issues/112)

- Move changelog to `.github` and add a changelog reminder.

### Breaking changes

- Compilers with C++17 support are now required to build C++ module. ([#113](https://github.com/PennyLaneAI/pennylane-lightning/pull/113))

- Gate classes have been removed with functionality added to StateVector class. ([#113](https://github.com/PennyLaneAI/pennylane-lightning/pull/113))

### Bug fixes

### Contributors

This release contains contributions from (in alphabetical order):

Ali Asadi, Thomas Bromley
Ali Asadi, Thomas Bromley, Lee J. O'Riordan

---

Expand Down
31 changes: 23 additions & 8 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,36 @@ jobs:
matrix:
os: [ubuntu-20.04]
steps:
- name: Cancel previous runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}

- uses: actions/checkout@v2

- name: Install dependencies
run: sudo apt-get update && sudo apt-get -y -q install cmake gcc

- name: Install Google Test
run: |
wget -qO - https://github.com/google/googletest/archive/release-1.10.0.tar.gz | tar -xz
cmake -D CMAKE_INSTALL_PREFIX:PATH=$HOME/googletest -D CMAKE_BUILD_TYPE=Release googletest-release-1.10.0
make install
- name: Build and run unit tests
run: |
cd pennylane_lightning/src/tests
GOOGLETEST_DIR=$HOME/googletest make test
cmake . -BBuild -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTS=1
cmake --build ./Build
mkdir -p ./Build/tests/results
./Build/tests/runner --order lex --reporter junit --out ./Build/tests/results/report.xml
- name: Upload test results
uses: actions/upload-artifact@v2
if: always()
with:
name: ubuntu-test-report
path: Build/tests/results/report.xml

- name: Publish test results
uses: EnricoMi/publish-unit-test-result-action@v1
if: always()
with:
check_name: Test Report (C++) on Ubuntu
files: Build/tests/results/report.xml

pythontests:
name: Python tests
Expand Down
40 changes: 19 additions & 21 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ set_pennylane_lightning_version("${CMAKE_SOURCE_DIR}/pennylane_lightning/_versio
message(STATUS "pennylane_lightning version ${VERSION_STRING}")
set(PROJECT_VERSION ${VERSION_STRING})

set(CMAKE_CXX_STANDARD 11) # At least C++11 is required
set(CMAKE_CXX_STANDARD 17) # At least C++17 is required
find_package(OpenMP REQUIRED) # find OpenMP


if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
Expand All @@ -36,32 +35,31 @@ option(BUILD_TESTS "Build cpp tests" OFF)
# Add pybind11
include(FetchContent)
FetchContent_Declare(
pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11.git
GIT_TAG v2.6.2
pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11.git
GIT_TAG v2.6.2
)
FetchContent_MakeAvailable(pybind11)

add_library(pennylane_lightning SHARED "pennylane_lightning/src/StateVector.cpp")
target_include_directories(pennylane_lightning INTERFACE "pennylane_lightning/src")

add_library(external_dependency INTERFACE)


if ("$ENV{USE_LAPACK}" OR "${USE_LAPACK}")
message(STATUS "Use LAPACKE")
target_link_libraries(external_dependency INTERFACE lapacke)
target_compile_options(external_dependency INTERFACE "-DLAPACKE=1")
message(STATUS "Use LAPACKE")
target_link_libraries(external_dependency INTERFACE lapacke)
target_compile_options(external_dependency INTERFACE "-DLAPACKE=1")
endif()

if ("$ENV{USE_OPENBLAS}" OR "${USE_OPENBLAS}")
message(STATUS "Use OPENBLAS")
target_link_libraries(external_dependency INTERFACE openblas)
target_compile_options(external_dependency INTERFACE "-DOPENBLAS=1")
message(STATUS "Use OPENBLAS")
target_link_libraries(external_dependency INTERFACE openblas)
target_compile_options(external_dependency INTERFACE "-DOPENBLAS=1")
endif()

pybind11_add_module(lightning_qubit_ops "pennylane_lightning/src/Apply.cpp"
"pennylane_lightning/src/Bindings.cpp"
"pennylane_lightning/src/Gates.cpp"
"pennylane_lightning/src/StateVector.cpp")
pybind11_add_module(lightning_qubit_ops "pennylane_lightning/src/StateVector.cpp"
"pennylane_lightning/src/Bindings.cpp")
target_link_libraries(lightning_qubit_ops PRIVATE external_dependency)
set_target_properties(lightning_qubit_ops PROPERTIES CXX_VISIBILITY_PRESET hidden)

Expand All @@ -71,12 +69,12 @@ target_compile_options(lightning_qubit_ops PRIVATE "$<$<CONFIG:DEBUG>:-Wall>")
target_compile_definitions(lightning_qubit_ops PRIVATE VERSION_INFO=${VERSION_STRING})

if(ENABLE_NATIVE)
message(STATUS "ENABLE_NATIVE is ON. Use -march=native for lightning_qubit_ops.")
target_compile_options(lightning_qubit_ops PRIVATE -march=native)
message(STATUS "ENABLE_NATIVE is ON. Use -march=native for lightning_qubit_ops.")
target_compile_options(pennylane_lightning PRIVATE -march=native)
target_compile_options(lightning_qubit_ops PRIVATE -march=native)
endif()


if (BUILD_TESTS)
enable_testing()
add_subdirectory("pennylane_lightning/src/tests" "tests")
enable_testing()
add_subdirectory("pennylane_lightning/src/tests" "tests")
endif()
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,17 @@ clean-docs:
make -C doc clean

test:
$(PYTHON) $(TESTRUNNER)
$(PYTHON) -I $(TESTRUNNER)

coverage:
@echo "Generating coverage report..."
$(PYTHON) $(TESTRUNNER) $(COVERAGE)

test-cpp:
make -C pennylane_lightning/src/tests clean
GOOGLETEST_DIR=$(HOME)/googletest make -C pennylane_lightning/src/tests test
rm -rf ./BuildTests
cmake . -BBuildTests -DBUILD_TESTS=1
cmake --build ./BuildTests
./BuildTests/tests/runner

.PHONY: format
format:
Expand Down
1 change: 0 additions & 1 deletion bin/format
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ IGNORE_PATTERN = "external"

BASE_CMD = ("clang-format", f"-style={json.dumps(CLANG_FMT_STYLE_CFG)}")


def check_bin():
try:
subprocess.run(
Expand Down
4 changes: 2 additions & 2 deletions pennylane_lightning/lightning_qubit.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
interfaces with C++ for fast linear algebra calculations.
"""
from pennylane.devices import DefaultQubit
from .lightning_qubit_ops import apply
import numpy as np
from pennylane import QubitStateVector, BasisState, DeviceError, QubitUnitary
from .lightning_qubit_ops import apply, StateVectorC64, StateVectorC128

from ._version import __version__

Expand Down Expand Up @@ -137,7 +137,7 @@ def apply_lightning(self, state, operations):
op_inverse = [o.inverse for o in operations]

state_vector = np.ravel(state)
apply(state_vector, op_names, op_wires, op_param, op_inverse, self.num_wires)
apply(state_vector, op_names, op_wires, op_inverse, op_param)
return np.reshape(state_vector, state.shape)

@staticmethod
Expand Down
113 changes: 0 additions & 113 deletions pennylane_lightning/src/Apply.cpp

This file was deleted.

Loading

0 comments on commit bdf8aa8

Please sign in to comment.