Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Divide Util.hpp and fix some bugs there #228

Merged
merged 10 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,12 @@ FetchContent_MakeAvailable(pybind11)
# All CMakeLists.txt in subdirectories use pennylane_lightning_compile_options and pennylane_lightning_external_libs
add_subdirectory(pennylane_lightning/src)

# Define pennylane_lightning target
add_library(pennylane_lightning INTERFACE)
target_link_libraries(pennylane_lightning INTERFACE lightning_utils
lightning_simulator
lightning_algorithms)
target_include_directories(pennylane_lightning INTERFACE "pennylane_lightning/src")

pybind11_add_module(lightning_qubit_ops "pennylane_lightning/src/bindings/Bindings.cpp")
target_link_libraries(lightning_qubit_ops PRIVATE pennylane_lightning
pennylane_lightning_compile_options
pennylane_lightning_external_libs)
target_link_libraries(lightning_qubit_ops PRIVATE lightning_compile_options
lightning_external_libs
lightning_utils
lightning_simulator
lightning_algorithms)
set_target_properties(lightning_qubit_ops PROPERTIES CXX_VISIBILITY_PRESET hidden)

target_compile_definitions(lightning_qubit_ops PRIVATE VERSION_INFO=${VERSION_STRING})
Expand Down
19 changes: 13 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,27 @@ coverage:

test-cpp:
rm -rf ./BuildTests
cmake . -BBuildTests -DBUILD_TESTS=ON
cmake $(LIGHTNING_CPP_DIR) -BBuildTests -DBUILD_TESTS=ON
cmake --build ./BuildTests --target runner
cmake --build ./BuildTests --target test

test-cpp-blas:
rm -rf ./BuildTests
cmake $(LIGHTNING_CPP_DIR) -BBuildTests -DBUILD_TESTS=ON -DENABLE_BLAS=ON
cmake --build ./BuildTests --target runner
cmake --build ./BuildTests --target test

test-cpp-omp:
rm -rf ./BuildTests
cmake $(LIGHTNING_CPP_DIR) -BBuildTests -DBUILD_TESTS=ON -DENABLE_OPENMP=ON
cmake --build ./BuildTests --target runner
cmake --build ./BuildTests --target test

.PHONY: benchmark
benchmark:
cmake --build BuildBench --target clean || true
rm -rf ./BuildBench/CMakeCache.txt ./BuildBench/compiler_info.txt ./BuildBench/run_gate_benchmark.sh
ifdef CXX
CXX=${CXX} cmake $(LIGHTNING_CPP_DIR) -BBuildBench -DBUILD_EXAMPLES=ON -DCMAKE_BUILD_TYPE=Release -DENABLE_AVX=ON
else
cmake . -BBuildBench -DBUILD_EXAMPLES=ON -DCMAKE_BUILD_TYPE=Release -DENABLE_AVX=ON
endif
cmake $(LIGHTNING_CPP_DIR) -BBuildBench -DBUILD_EXAMPLES=ON -DCMAKE_BUILD_TYPE=Release -DENABLE_AVX=ON
cmake --build ./BuildBench

.PHONY: format format-cpp format-python
Expand Down
28 changes: 14 additions & 14 deletions cmake/process_options.cmake
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
##############################################################################
# This file processes ENABLE_WARNINGS, ENABLE_NATIVE, ENABLE_AVX,
# ENABLE_OPENMP, ENABLE_BLAS options and produces interface libraries
# pennylane_lightning_compile_options and pennylane_lightning_external_libs.
# lightning_compile_options and lightning_external_libs.
##############################################################################

# Include this file only once
include_guard()

# Set compile flags and library dependencies
add_library(pennylane_lightning_compile_options INTERFACE)
add_library(pennylane_lightning_external_libs INTERFACE)
add_library(lightning_compile_options INTERFACE)
add_library(lightning_external_libs INTERFACE)

# Initial attempt to find which BLAS implementation is chosen
function(get_blas_impl)
Expand All @@ -26,36 +26,36 @@ function(get_blas_impl)
endfunction()

if(MSVC) # For M_PI
target_compile_options(pennylane_lightning_compile_options INTERFACE /D_USE_MATH_DEFINES)
target_compile_options(lightning_compile_options INTERFACE /D_USE_MATH_DEFINES)
endif()

# Add -fwrapv, -fno-plt in Clang
if ((CMAKE_CXX_COMPILER_ID STREQUAL "Clang") OR (CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM"))
target_compile_options(pennylane_lightning_compile_options INTERFACE
target_compile_options(lightning_compile_options INTERFACE
$<$<COMPILE_LANGUAGE:CXX>:-fwrapv;-fno-plt>)
# Add -fwrapv, -fno-plt, -pipe in GCC
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_compile_options(pennylane_lightning_compile_options INTERFACE
target_compile_options(lightning_compile_options INTERFACE
$<$<COMPILE_LANGUAGE:CXX>:-fwrapv;-fno-plt;-pipe>)
endif()

if(ENABLE_WARNINGS)
message(STATUS "ENABLE_WARNINGS is ON.")
if(MSVC)
target_compile_options(pennylane_lightning_compile_options INTERFACE $<$<COMPILE_LANGUAGE:CXX>:/W4;/WX>)
target_compile_options(lightning_compile_options INTERFACE $<$<COMPILE_LANGUAGE:CXX>:/W4;/WX>)
else()
target_compile_options(pennylane_lightning_compile_options INTERFACE $<$<COMPILE_LANGUAGE:CXX>:-Wall;-Wextra;-Werror>)
target_compile_options(lightning_compile_options INTERFACE $<$<COMPILE_LANGUAGE:CXX>:-Wall;-Wextra;-Werror>)
endif()
endif()

if(ENABLE_NATIVE)
message(STATUS "ENABLE_NATIVE is ON. Using -march=native.")
target_compile_options(pennylane_lightning_compile_options INTERFACE -march=native)
target_compile_options(lightning_compile_options INTERFACE -march=native)
endif()

if(ENABLE_AVX)
message(STATUS "ENABLE_AVX is ON.")
target_compile_options(pennylane_lightning_compile_options INTERFACE -mavx)
target_compile_options(lightning_compile_options INTERFACE -mavx)
endif()

if(ENABLE_OPENMP)
Expand All @@ -67,7 +67,7 @@ if(ENABLE_OPENMP)
"Install OpenMP or set ENABLE_OPENMP OFF.")
endif()

target_link_libraries(pennylane_lightning_external_libs INTERFACE OpenMP::OpenMP_CXX)
target_link_libraries(lightning_external_libs INTERFACE OpenMP::OpenMP_CXX)
endif()


Expand All @@ -85,7 +85,7 @@ if(ENABLE_BLAS)
"See https://cmake.org/cmake/help/latest/module/FindBLAS.html"
"#blas-lapack-vendors for available options.")

target_link_libraries(pennylane_lightning_external_libs INTERFACE "${BLAS_LIBRARIES}")
target_link_options(pennylane_lightning_external_libs INTERFACE "${BLAS_LINKER_FLAGS}")
target_compile_options(pennylane_lightning_compile_options INTERFACE "-D_ENABLE_BLAS=1")
target_link_libraries(lightning_external_libs INTERFACE "${BLAS_LIBRARIES}")
target_link_options(lightning_external_libs INTERFACE "${BLAS_LINKER_FLAGS}")
target_compile_options(lightning_compile_options INTERFACE "-D_ENABLE_BLAS=1")
endif()
2 changes: 1 addition & 1 deletion pennylane_lightning/src/algorithms/AdjointDiff.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
#include <vector>

#include "Error.hpp"
#include "LinearAlgebra.hpp"
#include "StateVectorManaged.hpp"
#include "Util.hpp"

#include <iostream>

Expand Down
4 changes: 2 additions & 2 deletions pennylane_lightning/src/algorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ set(CMAKE_CXX_STANDARD 17)
set(ALGORITHM_FILES AdjointDiff.hpp AdjointDiff.cpp JacobianProd.hpp JacobianProd.cpp CACHE INTERNAL "" FORCE)
add_library(lightning_algorithms STATIC ${ALGORITHM_FILES})

target_link_libraries(lightning_algorithms PRIVATE pennylane_lightning_compile_options
pennylane_lightning_external_libs)
target_link_libraries(lightning_algorithms PRIVATE lightning_compile_options
lightning_external_libs)

target_include_directories(lightning_algorithms PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} )
target_link_libraries(lightning_algorithms PRIVATE lightning_simulator lightning_utils)
Expand Down
3 changes: 2 additions & 1 deletion pennylane_lightning/src/algorithms/JacobianProd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <algorithm>

#include "AdjointDiff.hpp"
#include "LinearAlgebra.hpp"

namespace Pennylane {
namespace Algorithms {
Expand Down Expand Up @@ -166,4 +167,4 @@ class VectorJacobianProduct : public AdjointJacobian<T> {
}; // class VectorJacobianProduct

} // namespace Algorithms
} // namespace Pennylane
} // namespace Pennylane
9 changes: 6 additions & 3 deletions pennylane_lightning/src/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ project("gate_benchmark"

# add_executable(gate_benchmark gate_benchmark.cpp)
# target_link_libraries(gate_benchmark lightning_utils lightning_simulator
# pennylane_lightning_compile_options pennylane_lightning_external_libs)
# lightning_compile_options
# lightning_external_libs)

add_executable(gate_benchmark_oplist gate_benchmark_oplist.cpp)
target_link_libraries(gate_benchmark_oplist lightning_utils lightning_simulator
pennylane_lightning_compile_options pennylane_lightning_external_libs)
target_link_libraries(gate_benchmark_oplist PRIVATE lightning_utils
lightning_simulator
lightning_compile_options
lightning_external_libs)

configure_file("compiler_info.in" "compiler_info.txt")

Expand Down
4 changes: 2 additions & 2 deletions pennylane_lightning/src/simulator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ set(CMAKE_CXX_STANDARD 17)
set(SIMULATOR_FILES IndicesUtil.cpp CACHE INTERNAL "" FORCE)

add_library(lightning_simulator STATIC ${SIMULATOR_FILES})
target_link_libraries(lightning_simulator PRIVATE pennylane_lightning_compile_options
pennylane_lightning_external_libs)
target_link_libraries(lightning_simulator PRIVATE lightning_compile_options
lightning_external_libs)
target_include_directories(lightning_simulator PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(lightning_simulator PRIVATE lightning_utils)

Expand Down
2 changes: 1 addition & 1 deletion pennylane_lightning/src/simulator/Measures.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
#include <cstdio>
#include <vector>

#include "LinearAlgebra.hpp"
#include "StateVectorManaged.hpp"
#include "StateVectorRaw.hpp"
#include "Util.hpp"

namespace Pennylane {
/**
Expand Down
1 change: 1 addition & 0 deletions pennylane_lightning/src/simulator/SelectGateOps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
#pragma once

#include "ConstantUtil.hpp"
#include "GateOperations.hpp"
#include "GateOperationsLM.hpp"
#include "GateOperationsPI.hpp"
Expand Down
2 changes: 2 additions & 0 deletions pennylane_lightning/src/simulator/StateVectorManaged.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
// limitations under the License.
#pragma once

#include "BitUtil.hpp"
#include "StateVectorBase.hpp"
#include "Util.hpp"

namespace Pennylane {

Expand Down
2 changes: 1 addition & 1 deletion pennylane_lightning/src/simulator/StateVectorRaw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
#include <utility>
#include <vector>

#include "BitUtil.hpp"
#include "Error.hpp"
#include "Gates.hpp"
#include "StateVectorBase.hpp"
#include "Util.hpp"

#include <iostream>

Expand Down
8 changes: 4 additions & 4 deletions pennylane_lightning/src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ cmake_minimum_required(VERSION 3.14)
project(pennylane_lightning_tests)

set(CMAKE_CXX_STANDARD 17)
find_package(OpenMP)

# Default build type for test code is Debug
if(NOT CMAKE_BUILD_TYPE)
Expand Down Expand Up @@ -36,7 +35,7 @@ include(Catch)

add_library(lightning_tests_dependency INTERFACE)
target_link_libraries(lightning_tests_dependency INTERFACE lightning_simulator lightning_algorithms lightning_utils Catch2::Catch2)
target_compile_options(lightning_tests_dependency INTERFACE "$<$<CONFIG:DEBUG>:-Wall>")
target_compile_options(lightning_tests_dependency INTERFACE "-Wno-unused;")
target_sources(lightning_tests_dependency INTERFACE runner_main.cpp)

if(ENABLE_NATIVE)
Expand All @@ -61,7 +60,8 @@ set(TEST_SOURCES Test_AdjDiff.cpp
Test_Util.cpp
Test_VectorJacobianProduct.cpp)


add_executable(runner ${TEST_SOURCES})
target_link_libraries(runner PRIVATE lightning_tests_dependency)
target_link_libraries(runner PRIVATE lightning_tests_dependency
lightning_compile_options
lightning_external_libs)
catch_discover_tests(runner)
8 changes: 6 additions & 2 deletions pennylane_lightning/src/tests/Test_DynamicDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ std::vector<size_t> createWires(GateOperations op) {
case 3:
return {0, 1, 2};
default:
PL_ABORT("The number of wires for a given gate is unset.");
break;
}
PL_ABORT("The number of wires for a given gate is unset.");
return {};
}

template <class PrecisionT>
Expand All @@ -47,8 +49,10 @@ std::vector<PrecisionT> createParams(GateOperations op) {
case 3:
return {0.128, -0.563, 1.414};
default:
PL_ABORT("The number of wires for a given gate is unset.");
break;
}
PL_ABORT("The number of wires for a given gate is unset.");
return {};
}

template <typename PrecisionT, typename ParamT, KernelType kernel>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "ConstantUtil.hpp"
#include "GateOperationsLM.hpp"
#include "GateOperationsPI.hpp"
#include "Gates.hpp"
#include "TestHelpers.hpp"
#include "TestMacros.hpp"
#include "Util.hpp"

#include <catch2/catch.hpp>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "ConstantUtil.hpp"
#include "GateOperationsLM.hpp"
#include "GateOperationsPI.hpp"
#include "Gates.hpp"
Expand Down
7 changes: 3 additions & 4 deletions pennylane_lightning/src/tests/Test_SelectGateOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void testGateFuncPtrPair() {
}

template <class fp_t, size_t kernel_idx, size_t num_params>
constexpr void testGateFuncPtrPairIter() {
void testGateFuncPtrPairIter() {
using Pennylane::Internal::GateOpsFuncPtrPairs;
if constexpr (kernel_idx < Constant::available_kernels.size()) {
const auto kernel =
Expand All @@ -130,11 +130,10 @@ constexpr void testGateFuncPtrPairIter() {
GateOpsFuncPtrPairs<fp_t, fp_t, kernel, num_params>::value)) ==
CountGatesWithNumParams<num_params>::value,
"Gate operations in GateOpsFuncPtrPairs are not distinct");
static_assert(
REQUIRE(
count_unique(Util::second_elts_of(
GateOpsFuncPtrPairs<fp_t, fp_t, kernel, num_params>::value)) ==
CountGatesWithNumParams<num_params>::value,
"Function pointers in GateOpsFuncPtrPairs are not distinct");
CountGatesWithNumParams<num_params>::value);
}
}

Expand Down
15 changes: 9 additions & 6 deletions pennylane_lightning/src/tests/Test_Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <catch2/catch.hpp>

#include "BitUtil.hpp"
#include "LinearAlgebra.hpp"
#include "Util.hpp"

#include "TestHelpers.hpp"
Expand Down Expand Up @@ -56,7 +58,8 @@ TEMPLATE_TEST_CASE("Constant values", "[Util]", float, double) {
}

// NOLINTNEXTLINE: Avoid complexity errors
TEMPLATE_TEST_CASE("Utility math functions", "[Util]", float, double) {
TEMPLATE_TEST_CASE("Utility math functions", "[Util][LinearAlgebra]", float,
double) {
SECTION("exp2: 2^n") {
for (size_t i = 0; i < 10; i++) {
CHECK(Util::exp2(i) == static_cast<size_t>(std::pow(2, i)));
Expand Down Expand Up @@ -392,8 +395,8 @@ TEMPLATE_TEST_CASE("Utility math functions", "[Util]", float, double) {
*
* This is a slow version of countBit1 defined in Util.hpp
*/
int popcount_slow(uint64_t x) {
int c = 0;
size_t popcount_slow(uint64_t x) {
size_t c = 0;
for (; x != 0; x >>= 1) {
if ((x & 1U) != 0U) {
c++;
Expand All @@ -407,16 +410,16 @@ int popcount_slow(uint64_t x) {
*
* This is a slow version of countTrailing0 defined in Util.hpp
*/
int ctz_slow(uint64_t x) {
int c = 0;
size_t ctz_slow(uint64_t x) {
size_t c = 0;
while ((x & 1) == 0) {
x >>= 1;
c++;
}
return c;
}
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
TEST_CASE("Utility bit operations", "[Util]") {
TEST_CASE("Utility bit operations", "[Util][BitUtil]") {
SECTION("Internal::countBit1Fast") {
{ // for uint32_t
uint32_t n = 0;
Expand Down
Loading