Skip to content

Commit

Permalink
Fix clang-tidy support for tests and extensibility (#237)
Browse files Browse the repository at this point in the history
* Fix clang-tidy errors

* Auto update version

* Update changelog

* Update clang tidy and g++ versions

* Ensure unused are cast away for conditional compiles

* Fix formatting

* Update build support for clang-format

* Auto update version

* Add fixx for clang-tidy-13 recommendation

Co-authored-by: Dev version update bot <chae-yeun@xanadu.ai>
  • Loading branch information
mlxd and chaeyeunpark committed Feb 25, 2022
1 parent 86efa02 commit 602352b
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 22 deletions.
8 changes: 7 additions & 1 deletion .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@

### Improvements

* Clang-tidy is now enabled for both tests and examples builds under Github Actions.
[(#237)](https://github.com/PennyLaneAI/pennylane-lightning/pull/237)

* The return type of `StateVectorBase` data is now derived-class defined.
[(#237)](https://github.com/PennyLaneAI/pennylane-lightning/pull/237)

* Update adjointJacobian and VJP methods.
[(#222)](https://github.com/PennyLaneAI/pennylane-lightning/pull/222)

Expand All @@ -32,7 +38,7 @@

This release contains contributions from (in alphabetical order):

Ali Asadi, Chae-Yeun Park
Ali Asadi, Chae-Yeun Park, Lee James O'Riordan

---

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
python-version: 3.7

- name: Install dependencies
run: sudo apt update && sudo apt -y install clang-tidy-11 cmake g++
run: sudo apt update && sudo apt -y install clang-tidy-12 cmake g++
env:
DEBIAN_FRONTEND: noninteractive

Expand All @@ -54,7 +54,7 @@ jobs:

- name: Run clang-tidy compilation
run: |
cmake -BBuild -DENABLE_CLANG_TIDY=ON -DCLANG_TIDY_BINARY=clang-tidy-11 -DBUILD_EXAMPLES=ON .
cmake -BBuild -DENABLE_CLANG_TIDY=ON -DCLANG_TIDY_BINARY=clang-tidy-12 -DBUILD_EXAMPLES=ON -DBUILD_TESTS=on -DENABLE_WARNINGS=on .
cmake --build ./Build
format-cpp:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- uses: actions/checkout@v2

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

- name: Build and run unit tests
run: |
Expand Down Expand Up @@ -71,7 +71,7 @@ jobs:
python-version: '3.7'

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

- name: Get required Python packages
run: |
Expand Down
2 changes: 1 addition & 1 deletion pennylane_lightning/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
Version number (major.minor.patch[-label])
"""

__version__ = "0.22.0-dev8"
__version__ = "0.22.0-dev9"
4 changes: 2 additions & 2 deletions pennylane_lightning/src/simulator/StateVectorBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ template <class PrecisionT, class Derived> class StateVectorBase {
return static_cast<size_t>(Util::exp2(num_qubits_));
}

[[nodiscard]] inline auto getData() -> ComplexPrecisionT * {
[[nodiscard]] inline auto getData() -> decltype(auto) {
return static_cast<Derived *>(this)->getData();
}

[[nodiscard]] inline auto getData() const -> const ComplexPrecisionT * {
[[nodiscard]] inline auto getData() const -> decltype(auto) {
return static_cast<const Derived *>(this)->getData();
}

Expand Down
15 changes: 14 additions & 1 deletion pennylane_lightning/src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,21 @@ target_link_libraries(lightning_tests_dependency INTERFACE lightning_algorithms
lightning_simulator
lightning_utils
Catch2::Catch2)
if(ENABLE_WARNINGS)
message(STATUS "ENABLE_WARNINGS is ON.")
if(MSVC)
target_compile_options(lightning_tests_dependency INTERFACE $<$<COMPILE_LANGUAGE:CXX>:/W4;/WX>)
else()
target_compile_options(lightning_tests_dependency INTERFACE $<$<COMPILE_LANGUAGE:CXX>:-Wall;-Wextra;-Werror>)
endif()
else()
if(MSVC)
target_compile_options(lightning_tests_dependency INTERFACE $<$<COMPILE_LANGUAGE:CXX>:/W4>)
else()
target_compile_options(lightning_tests_dependency INTERFACE $<$<COMPILE_LANGUAGE:CXX>:-Wall;-Wno-unused>)
endif()
endif()

target_compile_options(lightning_tests_dependency INTERFACE "-Wall;-Wno-unused;")
target_sources(lightning_tests_dependency INTERFACE runner_main.cpp)

if(ENABLE_NATIVE)
Expand Down
10 changes: 9 additions & 1 deletion pennylane_lightning/src/tests/Test_DynamicDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ struct testDispatchForKernel {
GateImplementation::implemented_gates, gate_op),
bool> = true>
static void test(RandomEngine &re, size_t num_qubits) {
} // Do nothing if not implemented
// Keep source, but allow clang-tidy to pass for unused
static_cast<void>(re);
static_cast<void>(num_qubits);
} // Do nothing if not implemented;
// This could probably be replaced with an enable_if or SFINAE-like
// pattern.
};

template <typename PrecisionT, typename ParamT, class GateImplementation,
Expand Down Expand Up @@ -107,6 +112,9 @@ void testAllKernelsIter(RandomEngine &re, size_t max_num_qubits) {

testAllKernelsIter<PrecisionT, ParamT, typename TypeList::Next,
RandomEngine>(re, max_num_qubits);
} else {
static_cast<void>(re);
static_cast<void>(max_num_qubits);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <complex>
#include <iostream>
#include <limits>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>
Expand All @@ -31,7 +32,7 @@ using namespace Pennylane::Gates;
*/
constexpr std::string_view remove_prefix(const std::string_view &str,
size_t len) {
return std::string_view(str.data() + len, str.length() - len);
return {str.data() + len, str.length() - len};
}

constexpr auto gate_name_to_ops = Util::reverse_pairs(Constant::gate_names);
Expand Down Expand Up @@ -129,6 +130,9 @@ void testAllGeneratorForKernel(RandomEngine &re, size_t num_qubits) {
re, num_qubits);
testAllGeneratorForKernel<PrecisionT, ParamT, GateImplementation,
gntr_idx + 1>(re, num_qubits);
} else {
static_cast<void>(re);
static_cast<void>(num_qubits);
}
}

Expand All @@ -141,6 +145,9 @@ void testAllGeneratorsAndKernels(RandomEngine &re, size_t num_qubits) {
re, num_qubits);
testAllGeneratorsAndKernels<PrecisionT, ParamT,
typename TypeList::Next>(re, num_qubits);
} else {
static_cast<void>(re);
static_cast<void>(num_qubits);
}
}

Expand Down
16 changes: 14 additions & 2 deletions pennylane_lightning/src/tests/Test_GateImplementations_Inverse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ void testInverseKernelGate(RandomEngine &re, size_t num_qubits) {

REQUIRE(st == PLApprox(ini_st).margin(1e-7));
}
} else {
static_cast<void>(re);
static_cast<void>(num_qubits);
}
}

Expand All @@ -58,6 +61,9 @@ void testKernelInversesIter(RandomEngine &re, size_t num_qubits) {
re, num_qubits);
testKernelInversesIter<PrecisionT, ParamT, GateImplementation,
gate_idx + 1>(re, num_qubits);
} else {
static_cast<void>(re);
static_cast<void>(num_qubits);
}
}

Expand All @@ -68,15 +74,21 @@ void testKernelInverses(RandomEngine &re, size_t num_qubits) {
re, num_qubits);
}

template <typename PrecisionT, typename ParamT, typename TypeList,
class RandomEngine>
template <
typename PrecisionT, typename ParamT, typename TypeList,
class RandomEngine> //, typename
// std::enable_if<std::is_same<TypeList,void>::value>
//>
void testKernels(RandomEngine &re, size_t num_qubits) {
if constexpr (!std::is_same_v<TypeList, void>) {
using GateImplementation = typename TypeList::Type;
testKernelInverses<PrecisionT, ParamT, GateImplementation>(re,
num_qubits);
testKernels<PrecisionT, ParamT, typename TypeList::Next>(re,
num_qubits);
} else {
static_cast<void>(re);
static_cast<void>(num_qubits);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -785,8 +785,6 @@ TEMPLATE_TEST_CASE("GateImplementation::applyMatrix, inverse = false",

template <typename PrecisionT, class GateImplementation>
void testApplyMatrixInverse() {
using ComplexPrecisionT = std::complex<PrecisionT>;

std::mt19937 re{1337};
const int num_qubits = 4;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ PENNYLANE_RUN_TEST(T);
******************************************************************************/

template <typename PrecisionT, class GateImplementation> void testApplyCNOT() {
using ComplexPrecisionT = std::complex<PrecisionT>;
const size_t num_qubits = 3;
auto st = createZeroState<PrecisionT>(num_qubits);

Expand Down
1 change: 0 additions & 1 deletion pennylane_lightning/src/tests/Test_Internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ TEMPLATE_TEST_CASE("createProductState", "[Test_Internal]", float, double) {
*/
TEMPLATE_TEST_CASE("randomUnitary", "[Test_Internal]", float, double) {
using PrecisionT = TestType;
using ComplexPrecisionT = std::complex<PrecisionT>;

std::mt19937 re{1337};

Expand Down
7 changes: 3 additions & 4 deletions pennylane_lightning/src/tests/Test_OpToMemberFuncPtr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ constexpr bool testAllGatesImplementedIter() {
if constexpr (gate_idx < static_cast<uint32_t>(GateOperation::END)) {
constexpr auto gate_op = static_cast<GateOperation>(gate_idx);
if constexpr (gate_op != GateOperation::Matrix) {
if (GateOpToMemberFuncPtr<PrecisionT, ParamT, GateImplemenation,
gate_op>::value == nullptr) {
return false;
}
static_cast<void>(
GateOpToMemberFuncPtr<PrecisionT, ParamT, GateImplemenation,
gate_op>::value);
}
return testAllGatesImplementedIter<PrecisionT, ParamT,
GateImplemenation, gate_idx + 1>();
Expand Down
2 changes: 1 addition & 1 deletion pennylane_lightning/src/tests/Test_Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ TEMPLATE_TEST_CASE("Utility math functions", "[Util][LinearAlgebra]", float,
for (size_t i = 0; i < 64; i++) {
std::vector<size_t> data(i);
TestType rem;
TestType f2 = std::modf(sqrt(i), &rem);
std::modf(sqrt(i), &rem);
if (i < 4) {
CHECK_THROWS_AS(Util::dimSize(data), std::invalid_argument);
CHECK_THROWS_WITH(Util::dimSize(data),
Expand Down

0 comments on commit 602352b

Please sign in to comment.