diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index 30bcff0576..7366388ddd 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -25,6 +25,9 @@ * Update tests to remove `JacobianTape`. [(#260)](https://github.com/PennyLaneAI/pennylane-lightning/pull/260) +* Fix tests for MSVC. +[(#264)](https://github.com/PennyLaneAI/pennylane-lightning/pull/264) + * Fix `#include ` for PPC and AArch64 in Linux. [(#266)](https://github.com/PennyLaneAI/pennylane-lightning/pull/266) diff --git a/pennylane_lightning/_version.py b/pennylane_lightning/_version.py index 84de49de90..2981d26d4a 100644 --- a/pennylane_lightning/_version.py +++ b/pennylane_lightning/_version.py @@ -16,4 +16,4 @@ Version number (major.minor.patch[-label]) """ -__version__ = "0.23.0-dev8" +__version__ = "0.23.0-dev9" diff --git a/pennylane_lightning/src/algorithms/AdjointDiff.hpp b/pennylane_lightning/src/algorithms/AdjointDiff.hpp index 1d84d139b6..98409d328e 100644 --- a/pennylane_lightning/src/algorithms/AdjointDiff.hpp +++ b/pennylane_lightning/src/algorithms/AdjointDiff.hpp @@ -340,8 +340,8 @@ template class AdjointJacobian { applyOperations(lambda, ops); } - const auto tp_begin = tp.begin(); - auto tp_it = tp.end(); + auto tp_it = tp.rbegin(); + const auto tp_rend = tp.rend(); // Create observable-applied state-vectors std::vector> H_lambda( @@ -355,49 +355,51 @@ template class AdjointJacobian { PL_ABORT_IF(ops.getOpsParams()[op_idx].size() > 1, "The operation is not supported using the adjoint " "differentiation method"); - if ((ops_name[op_idx] != "QubitStateVector") && - (ops_name[op_idx] != "BasisState")) { - mu.updateData(lambda.getDataVector()); - applyOperationAdj(lambda, ops, op_idx); - - if (ops.hasParams(op_idx)) { - if ((current_param_idx == *(std::prev(tp_it))) || - std::find(tp_begin, tp_it, current_param_idx) != - tp_it) { - const T scalingFactor = - applyGenerator(mu, ops_name[op_idx], - ops.getOpsWires()[op_idx], - !ops.getOpsInverses()[op_idx]) * - (ops.getOpsInverses()[op_idx] ? -1 : 1); - - const size_t mat_row_idx = - trainableParamNumber * num_observables; - - // clang-format off - - #if defined(_OPENMP) - #pragma omp parallel for default(none) \ - shared(H_lambda, jac, mu, scalingFactor, \ - mat_row_idx, \ - num_observables) - #endif - - // clang-format on - for (size_t obs_idx = 0; obs_idx < num_observables; - obs_idx++) { - jac[mat_row_idx + obs_idx] = - -2 * scalingFactor * - std::imag(innerProdC( - H_lambda[obs_idx].getDataVector(), - mu.getDataVector())); - } - trainableParamNumber--; - std::advance(tp_it, -1); + if ((ops_name[op_idx] == "QubitStateVector") || + (ops_name[op_idx] == "BasisState")) { + continue; + } + if (tp_it == tp_rend) { + break; // All done + } + mu.updateData(lambda.getDataVector()); + applyOperationAdj(lambda, ops, op_idx); + + if (ops.hasParams(op_idx)) { + if (current_param_idx == *tp_it) { + const T scalingFactor = + applyGenerator(mu, ops_name[op_idx], + ops.getOpsWires()[op_idx], + !ops.getOpsInverses()[op_idx]) * + (ops.getOpsInverses()[op_idx] ? -1 : 1); + + const size_t mat_row_idx = + trainableParamNumber * num_observables; + + // clang-format off + + #if defined(_OPENMP) + #pragma omp parallel for default(none) \ + shared(H_lambda, jac, mu, scalingFactor, \ + mat_row_idx, \ + num_observables) + #endif + + // clang-format on + for (size_t obs_idx = 0; obs_idx < num_observables; + obs_idx++) { + jac[mat_row_idx + obs_idx] = + -2 * scalingFactor * + std::imag( + innerProdC(H_lambda[obs_idx].getDataVector(), + mu.getDataVector())); } - current_param_idx--; + trainableParamNumber--; + ++tp_it; } - applyOperationsAdj(H_lambda, ops, static_cast(op_idx)); + current_param_idx--; } + applyOperationsAdj(H_lambda, ops, static_cast(op_idx)); } jac = Transpose(jac, jd.getNumParams(), num_observables); } diff --git a/pennylane_lightning/src/algorithms/JacobianTape.hpp b/pennylane_lightning/src/algorithms/JacobianTape.hpp index ca7d0ac6f7..26ff1e2951 100644 --- a/pennylane_lightning/src/algorithms/JacobianTape.hpp +++ b/pennylane_lightning/src/algorithms/JacobianTape.hpp @@ -275,7 +275,7 @@ template class JacobianData { * @param obs Observables for which to calculate Jacobian. * @param ops Operations used to create given state. * @param trainP List of parameters participating in Jacobian - * calculation. + * calculation. This must be sorted. */ JacobianData(size_t num_params, size_t num_elem, std::complex *ps, std::vector> obs, OpsData ops, diff --git a/pennylane_lightning/src/gates/GateImplementationsLM.hpp b/pennylane_lightning/src/gates/GateImplementationsLM.hpp index a985e0e373..2fc8e4b4ef 100644 --- a/pennylane_lightning/src/gates/GateImplementationsLM.hpp +++ b/pennylane_lightning/src/gates/GateImplementationsLM.hpp @@ -246,7 +246,7 @@ class GateImplementationsLM : public PauliGenerator { applyTwoQubitOp(arr, num_qubits, matrix, wires, inverse); break; default: { - size_t dim = 1U << wires.size(); + size_t dim = size_t{1U} << wires.size(); std::vector indices; indices.resize(dim); @@ -1089,8 +1089,8 @@ class GateImplementationsLM : public PauliGenerator { const size_t i10 = i00 | rev_wire1_shift; const size_t i11 = i00 | rev_wire0_shift | rev_wire1_shift; - arr[i00] = ComplexPrecisionT{}; - arr[i01] = ComplexPrecisionT{}; + arr[i00] = ComplexPrecisionT{0.0, 0.0}; + arr[i01] = ComplexPrecisionT{0.0, 0.0}; std::swap(arr[i10], arr[i11]); } @@ -1189,7 +1189,8 @@ class GateImplementationsLM : public PauliGenerator { } for (size_t k = 0; k < Util::exp2(num_qubits); k++) { - arr[k] *= (2 * int(Util::popcount(k & wires_parity) % 2) - 1); + arr[k] *= static_cast( + 2 * int(Util::popcount(k & wires_parity) % 2) - 1); } // NOLINTNEXTLINE(readability-magic-numbers) return static_cast(0.5); diff --git a/pennylane_lightning/src/simulator/Measures.hpp b/pennylane_lightning/src/simulator/Measures.hpp index a20bf30efc..a4a8841b69 100644 --- a/pennylane_lightning/src/simulator/Measures.hpp +++ b/pennylane_lightning/src/simulator/Measures.hpp @@ -324,7 +324,7 @@ class Measures { // Pick samples for (size_t i = 0; i < num_samples; i++) { fp_t pct = distribution(generator) * N; - size_t idx = pct; + auto idx = static_cast(pct); if (pct - idx > bucket[idx]) { idx = bucket_partner[idx]; } diff --git a/pennylane_lightning/src/tests/TestHelpers.hpp b/pennylane_lightning/src/tests/TestHelpers.hpp index a5c87328f5..b204cbd87f 100644 --- a/pennylane_lightning/src/tests/TestHelpers.hpp +++ b/pennylane_lightning/src/tests/TestHelpers.hpp @@ -171,7 +171,8 @@ void scaleVector(std::vector> &data, Data_t scalar) { template auto createZeroState(size_t num_qubits) -> std::vector> { - std::vector> res(1U << num_qubits, {0.0, 0.0}); + std::vector> res(size_t{1U} << num_qubits, + {0.0, 0.0}); res[0] = std::complex{1.0, 0.0}; return res; } @@ -182,7 +183,8 @@ auto createZeroState(size_t num_qubits) template auto createPlusState(size_t num_qubits) -> std::vector> { - std::vector> res(1U << num_qubits, {1.0, 0.0}); + std::vector> res(size_t{1U} << num_qubits, + {1.0, 0.0}); for (auto &elt : res) { elt /= std::sqrt(1U << num_qubits); } @@ -195,9 +197,10 @@ auto createPlusState(size_t num_qubits) template auto createRandomState(RandomEngine &re, size_t num_qubits) -> std::vector> { - std::vector> res(1U << num_qubits, {0.0, 0.0}); + std::vector> res(size_t{1U} << num_qubits, + {0.0, 0.0}); std::uniform_real_distribution dist; - for (size_t idx = 0; idx < (1U << num_qubits); idx++) { + for (size_t idx = 0; idx < (size_t{1U} << num_qubits); idx++) { res[idx] = {dist(re), dist(re)}; } @@ -214,7 +217,7 @@ auto createRandomState(RandomEngine &re, size_t num_qubits) template auto createProductState(std::string_view str) { using Pennylane::Util::INVSQRT2; std::vector> st; - st.resize(1U << str.length()); + st.resize(size_t{1U} << str.length()); std::vector zero{1.0, 0.0}; std::vector one{0.0, 1.0}; @@ -224,7 +227,7 @@ template auto createProductState(std::string_view str) { std::vector minus{INVSQRT2(), -INVSQRT2()}; - for (size_t k = 0; k < (1U << str.length()); k++) { + for (size_t k = 0; k < (size_t{1U} << str.length()); k++) { PrecisionT elt = 1.0; for (size_t n = 0; n < str.length(); n++) { char c = str[n]; @@ -276,9 +279,9 @@ auto createParams(Gates::GateOperation op) -> std::vector { case 0: return {}; case 1: - return {0.312}; + return {PrecisionT{0.312}}; case 3: - return {0.128, -0.563, 1.414}; + return {PrecisionT{0.128}, PrecisionT{-0.563}, PrecisionT{1.414}}; default: PL_ABORT("The number of parameters for a given gate is unknown."); } diff --git a/pennylane_lightning/src/tests/Test_AdjDiff.cpp b/pennylane_lightning/src/tests/Test_AdjDiff.cpp index 2ca9e3213b..7b82495837 100644 --- a/pennylane_lightning/src/tests/Test_AdjDiff.cpp +++ b/pennylane_lightning/src/tests/Test_AdjDiff.cpp @@ -1,5 +1,3 @@ -#define _USE_MATH_DEFINES - #include #include #include @@ -18,6 +16,10 @@ #include "TestHelpers.hpp" +#ifndef _USE_MATH_DEFINES +#define _USE_MATH_DEFINES +#endif + using namespace Pennylane; using namespace Pennylane::Algorithms; @@ -127,7 +129,7 @@ TEST_CASE("AdjointJacobian::adjointJacobian Op=RX, Obs=[Z,Z]", CAPTURE(jacobian); CHECK(-sin(param[0]) == Approx(jacobian[0]).margin(1e-7)); - CHECK(0.0 == Approx(jacobian[1 * num_params + 1]).margin(1e-7)); + CHECK(0.0 == Approx(jacobian[1 * num_obs - 1]).margin(1e-7)); } } TEST_CASE("AdjointJacobian::adjointJacobian Op=[RX,RX,RX], Obs=[Z,Z,Z]", diff --git a/pennylane_lightning/src/tests/Test_DynamicDispatcher.cpp b/pennylane_lightning/src/tests/Test_DynamicDispatcher.cpp index e429b676a6..1b024a2230 100644 --- a/pennylane_lightning/src/tests/Test_DynamicDispatcher.cpp +++ b/pennylane_lightning/src/tests/Test_DynamicDispatcher.cpp @@ -28,13 +28,20 @@ using Pennylane::Gates::callGateOps; * We just check DynamicDispacther calls the correct functuion by comparing * the result from it with that of the direct call. */ -template +template struct testDispatchForKernel { - template < - GateOperation gate_op, class RandomEngine, - std::enable_if_t< - Util::array_has_elt(GateImplementation::implemented_gates, gate_op), - bool> = true> + static void test(RandomEngine &re, size_t num_qubits) { + static_cast(re); + static_cast(num_qubits); + } +}; +template +struct testDispatchForKernel< + PrecisionT, ParamT, GateImplementation, gate_op, RandomEngine, + std::enable_if_t> { static void test(RandomEngine &re, size_t num_qubits) { using CFP_t = std::complex; const std::vector ini_st = @@ -61,20 +68,13 @@ struct testDispatchForKernel { gate_name, wires, false, params); REQUIRE(test_st == expected); } - - template < - GateOperation gate_op, class RandomEngine, - std::enable_if_t = true> - static void test(RandomEngine &re, size_t num_qubits) { - // Keep source, but allow clang-tidy to pass for unused - static_cast(re); - static_cast(num_qubits); - } // Do nothing if not implemented; - // This could probably be replaced with an enable_if or SFINAE-like - // pattern. }; +template +void testDynamicDispatch(RandomEngine &re, size_t num_qubits) { + testDispatchForKernel::test(re, num_qubits); +} template @@ -86,11 +86,10 @@ constexpr void testAllGatesForKernelIter(RandomEngine &re, if constexpr (gate_op != GateOperation::Matrix) { // ignore Matrix for (size_t num_qubits = 3; num_qubits <= max_num_qubits; num_qubits++) { - testDispatchForKernel:: - template test(re, num_qubits); + testDynamicDispatch(re, num_qubits); } } - testAllGatesForKernelIter(re, max_num_qubits); } diff --git a/pennylane_lightning/src/tests/Test_GateImplementations_Generator.cpp b/pennylane_lightning/src/tests/Test_GateImplementations_Generator.cpp index 1440fcda1a..cf4569a1d2 100644 --- a/pennylane_lightning/src/tests/Test_GateImplementations_Generator.cpp +++ b/pennylane_lightning/src/tests/Test_GateImplementations_Generator.cpp @@ -74,7 +74,7 @@ void testGeneratorForGate(RandomEngine &re, size_t num_qubits) { using ComplexPrecisionT = std::complex; constexpr auto I = Util::IMAG(); - constexpr ParamT eps = 1e-4; // For finite difference + constexpr auto eps = PrecisionT{1e-4}; // For finite difference constexpr auto gate_op = Util::static_lookup(generator_gate_pairs); constexpr auto gate_name = @@ -106,7 +106,7 @@ void testGeneratorForGate(RandomEngine &re, size_t num_qubits) { gate_func(diff_st_1.data(), num_qubits, wires, false, eps); gate_func(diff_st_2.data(), num_qubits, wires, false, -eps); - std::vector gate_der_st(1U << num_qubits); + std::vector gate_der_st(size_t{1U} << num_qubits); std::transform( diff_st_1.cbegin(), diff_st_1.cend(), diff_st_2.cbegin(), @@ -115,7 +115,7 @@ void testGeneratorForGate(RandomEngine &re, size_t num_qubits) { scaleVector(gate_der_st, static_cast(0.5) / eps); - REQUIRE(gntr_st == approx(gate_der_st).margin(1e-3)); + REQUIRE(gntr_st == approx(gate_der_st).margin(PrecisionT{1e-3})); } } template (re); diff --git a/pennylane_lightning/src/tests/Test_GateImplementations_Matrix.cpp b/pennylane_lightning/src/tests/Test_GateImplementations_Matrix.cpp index 90ede20986..1982b41d6b 100644 --- a/pennylane_lightning/src/tests/Test_GateImplementations_Matrix.cpp +++ b/pennylane_lightning/src/tests/Test_GateImplementations_Matrix.cpp @@ -5,6 +5,10 @@ #include +#if defined(_MSC_VER) +#pragma warning(disable : 4305) +#endif + using namespace Pennylane; using Pennylane::Util::randomUnitary; @@ -788,6 +792,7 @@ template void testApplyMatrixInverse() { std::mt19937 re{1337}; const int num_qubits = 4; + const auto margin = PrecisionT{1e-5}; DYNAMIC_SECTION(GateImplementation::name << ", wires = {0} - " @@ -803,7 +808,7 @@ void testApplyMatrixInverse() { wires, false); GateImplementation::applyMatrix(st.data(), num_qubits, matrix.data(), wires, true); - REQUIRE(st == approx(ini_st).margin(1e-5)); + REQUIRE(st == approx(ini_st).margin(margin)); } DYNAMIC_SECTION(GateImplementation::name @@ -821,7 +826,7 @@ void testApplyMatrixInverse() { GateImplementation::applyMatrix(st.data(), num_qubits, matrix.data(), wires, true); - REQUIRE(st == approx(ini_st).margin(1e-5)); + REQUIRE(st == approx(ini_st).margin(margin)); } DYNAMIC_SECTION(GateImplementation::name @@ -839,7 +844,7 @@ void testApplyMatrixInverse() { GateImplementation::applyMatrix(st.data(), num_qubits, matrix.data(), wires, true); - REQUIRE(st == approx(ini_st).margin(1e-5)); + REQUIRE(st == approx(ini_st).margin(margin)); } DYNAMIC_SECTION(GateImplementation::name @@ -857,7 +862,7 @@ void testApplyMatrixInverse() { GateImplementation::applyMatrix(st.data(), num_qubits, matrix.data(), wires, true); - REQUIRE(st == approx(ini_st).margin(1e-5)); + REQUIRE(st == approx(ini_st).margin(margin)); } DYNAMIC_SECTION(GateImplementation::name @@ -875,7 +880,7 @@ void testApplyMatrixInverse() { GateImplementation::applyMatrix(st.data(), num_qubits, matrix.data(), wires, true); - REQUIRE(st == approx(ini_st).margin(1e-5)); + REQUIRE(st == approx(ini_st).margin(margin)); } DYNAMIC_SECTION(GateImplementation::name << ", wires = {1,2} - " @@ -891,7 +896,7 @@ void testApplyMatrixInverse() { GateImplementation::applyMatrix(st.data(), num_qubits, matrix.data(), wires, true); - REQUIRE(st == approx(ini_st).margin(1e-5)); + REQUIRE(st == approx(ini_st).margin(margin)); } DYNAMIC_SECTION(GateImplementation::name << ", wires = {1,3} - " @@ -907,7 +912,7 @@ void testApplyMatrixInverse() { GateImplementation::applyMatrix(st.data(), num_qubits, matrix.data(), wires, true); - REQUIRE(st == approx(ini_st).margin(1e-5)); + REQUIRE(st == approx(ini_st).margin(margin)); } DYNAMIC_SECTION(GateImplementation::name @@ -924,7 +929,7 @@ void testApplyMatrixInverse() { GateImplementation::applyMatrix(st.data(), num_qubits, matrix.data(), wires, true); - REQUIRE(st == approx(ini_st).margin(1e-5)); + REQUIRE(st == approx(ini_st).margin(margin)); } DYNAMIC_SECTION(GateImplementation::name << ", wires = {0,1,2,3} - " @@ -940,7 +945,7 @@ void testApplyMatrixInverse() { GateImplementation::applyMatrix(st.data(), num_qubits, matrix.data(), wires, true); - REQUIRE(st == approx(ini_st).margin(1e-5)); + REQUIRE(st == approx(ini_st).margin(margin)); } } diff --git a/pennylane_lightning/src/tests/Test_GateImplementations_Nonparam.cpp b/pennylane_lightning/src/tests/Test_GateImplementations_Nonparam.cpp index c5b3e01227..fcc32be6b2 100644 --- a/pennylane_lightning/src/tests/Test_GateImplementations_Nonparam.cpp +++ b/pennylane_lightning/src/tests/Test_GateImplementations_Nonparam.cpp @@ -73,7 +73,8 @@ void testApplyPauliX() { GateImplementation::applyPauliX(st.data(), num_qubits, {index}, false); CHECK(st[0] == Util::ZERO()); - CHECK(st[1U << (num_qubits - index - 1)] == Util::ONE()); + CHECK(st[size_t{1U} << (num_qubits - index - 1)] == + Util::ONE()); } } PENNYLANE_RUN_TEST(PauliX); @@ -143,9 +144,9 @@ void testApplyHadamard() { CHECK(expected.imag() == Approx(st[0].imag())); CHECK(expected.real() == - Approx(st[1U << (num_qubits - index - 1)].real())); + Approx(st[size_t{1U} << (num_qubits - index - 1)].real())); CHECK(expected.imag() == - Approx(st[1U << (num_qubits - index - 1)].imag())); + Approx(st[size_t{1U} << (num_qubits - index - 1)].imag())); } } PENNYLANE_RUN_TEST(Hadamard); diff --git a/pennylane_lightning/src/tests/Test_GateImplementations_Param.cpp b/pennylane_lightning/src/tests/Test_GateImplementations_Param.cpp index 33ec8656a7..b1161091c1 100644 --- a/pennylane_lightning/src/tests/Test_GateImplementations_Param.cpp +++ b/pennylane_lightning/src/tests/Test_GateImplementations_Param.cpp @@ -12,6 +12,10 @@ #include #include +#if defined(_MSC_VER) +#pragma warning(disable : 4305) +#endif + /** * @file This file contains tests for parameterized gates. List of such gates is * [RX, RY, RZ, PhaseShift, Rot, ControlledPhaseShift, CRX, CRY, CRZ, CRot] @@ -71,9 +75,9 @@ void testApplyPhaseShift() { const size_t num_qubits = 3; // Test using |+++> state - + const auto isqrt2 = PrecisionT{Util::INVSQRT2()}; const std::vector angles{0.3, 0.8, 2.4}; - const ComplexPrecisionT coef(1.0 / (2 * std::sqrt(2)), 0); + const ComplexPrecisionT coef{isqrt2 / PrecisionT{2.0}, PrecisionT{0.0}}; std::vector> ps_data; ps_data.reserve(angles.size()); @@ -184,9 +188,10 @@ void testApplyRZ() { const size_t num_qubits = 3; // Test using |+++> state + const auto isqrt2 = PrecisionT{Util::INVSQRT2()}; const std::vector angles{0.2, 0.7, 2.9}; - const ComplexPrecisionT coef(1.0 / (2 * std::sqrt(2)), 0); + const ComplexPrecisionT coef{isqrt2 / PrecisionT{2.0}, PrecisionT{0.0}}; std::vector> rz_data; rz_data.reserve(angles.size()); @@ -245,7 +250,7 @@ void testApplyRot() { const auto rot_mat = Gates::getRot(angles[i][0], angles[i][1], angles[i][2]); expected_results[i][0] = rot_mat[0]; - expected_results[i][1U << (num_qubits - i - 1)] = rot_mat[2]; + expected_results[i][size_t{1U} << (num_qubits - i - 1)] = rot_mat[2]; } for (size_t index = 0; index < num_qubits; index++) { @@ -710,8 +715,10 @@ void testApplyControlledPhaseShift() { // Test using |+++> state auto ini_st = createPlusState(num_qubits); + const auto isqrt2 = Util::INVSQRT2(); + const std::vector angles{0.3, 2.4}; - const ComplexPrecisionT coef(1.0 / (2 * std::sqrt(2)), 0); + const ComplexPrecisionT coef{isqrt2 / PrecisionT{2.0}, PrecisionT{0.0}}; std::vector> ps_data; ps_data.reserve(angles.size()); @@ -1216,21 +1223,15 @@ PENNYLANE_RUN_TEST(CRZ); template void testApplyCRot() { using ComplexPrecisionT = std::complex; - const size_t num_qubits = 3; - - const auto ini_st = createZeroState(num_qubits); const std::vector angles{0.3, 0.8, 2.4}; - std::vector expected_results(8); - const auto rot_mat = - Gates::getRot(angles[0], angles[1], angles[2]); - expected_results[1U << (num_qubits - 1)] = rot_mat[0]; - expected_results[(1U << num_qubits) - 2] = rot_mat[2]; - DYNAMIC_SECTION(GateImplementation::name << ", CRot0,1 |000> -> |000> - " << PrecisionToName::value) { + const size_t num_qubits = 3; + const auto ini_st = createZeroState(num_qubits); + auto st = createZeroState(num_qubits); GateImplementation::applyCRot(st.data(), num_qubits, {0, 1}, false, angles[0], angles[1], angles[2]); @@ -1240,7 +1241,16 @@ void testApplyCRot() { DYNAMIC_SECTION(GateImplementation::name << ", CRot0,1 |100> -> |1>(a|0>+b|1>)|0> - " << PrecisionToName::value) { + const size_t num_qubits = 3; + auto st = createZeroState(num_qubits); + + std::vector expected_results(8); + const auto rot_mat = + Gates::getRot(angles[0], angles[1], angles[2]); + expected_results[size_t{1U} << (num_qubits - 1)] = rot_mat[0]; + expected_results[(size_t{1U} << num_qubits) - 2] = rot_mat[2]; + GateImplementation::applyPauliX(st.data(), num_qubits, {0}, false); GateImplementation::applyCRot(st.data(), num_qubits, {0, 1}, false, diff --git a/pennylane_lightning/src/tests/Test_GateUtil.cpp b/pennylane_lightning/src/tests/Test_GateUtil.cpp index 15642d4303..4206321ea9 100644 --- a/pennylane_lightning/src/tests/Test_GateUtil.cpp +++ b/pennylane_lightning/src/tests/Test_GateUtil.cpp @@ -25,7 +25,7 @@ TEST_CASE("generateBitPatterns", "[IndicesUtil]") { } SECTION("Qubit indices {i}") { for (size_t i = 0; i < num_qubits; i++) { - std::vector expected{0, 0b1UL << (num_qubits - i - 1)}; + std::vector expected{0, size_t{1U} << (num_qubits - i - 1)}; auto bit_pattern = generateBitPatterns({i}, num_qubits); CHECK(bit_pattern == expected); } diff --git a/pennylane_lightning/src/tests/Test_Internal.cpp b/pennylane_lightning/src/tests/Test_Internal.cpp index d5fadfe14b..3383678d77 100644 --- a/pennylane_lightning/src/tests/Test_Internal.cpp +++ b/pennylane_lightning/src/tests/Test_Internal.cpp @@ -7,6 +7,10 @@ #include #include +#if defined(_MSC_VER) +#pragma warning(disable : 4305) +#endif + /** * We test internal functions for test suite. */ @@ -18,6 +22,8 @@ TEMPLATE_TEST_CASE("Approx", "[Test_Internal]", float, double) { using PrecisionT = TestType; using ComplexPrecisionT = std::complex; + const auto margin = PrecisionT{0.00015}; + SECTION("vector{1.0, 1.0*I} approx vector{1.0001, 0.9999*I} with margin " "0.00015") { const std::vector test1{ @@ -28,7 +34,7 @@ TEMPLATE_TEST_CASE("Approx", "[Test_Internal]", float, double) { ComplexPrecisionT{1.0001, 0.0}, ComplexPrecisionT{0.0, 0.9999}, }; - REQUIRE(test1 == approx(test2).margin(0.00015)); + REQUIRE(test1 == approx(test2).margin(margin)); } SECTION("vector{1.0, 1.0*I} does not approx vector{1.0002, 0.9998*I} with " "margin 0.00015") { @@ -40,7 +46,7 @@ TEMPLATE_TEST_CASE("Approx", "[Test_Internal]", float, double) { ComplexPrecisionT{1.0002, 0.0}, ComplexPrecisionT{0.0, 0.9998}, }; - REQUIRE(test1 != approx(test2).margin(0.00015)); + REQUIRE(test1 != approx(test2).margin(margin)); } SECTION("vector{1.0, 1.0*I} does not approx vector{1.0I, 1.0} with margin " "0.00015") { @@ -52,7 +58,7 @@ TEMPLATE_TEST_CASE("Approx", "[Test_Internal]", float, double) { ComplexPrecisionT{0.0, 1.0}, ComplexPrecisionT{1.0, 0.0}, }; - REQUIRE(test1 != approx(test2).margin(0.00015)); + REQUIRE(test1 != approx(test2).margin(margin)); } } @@ -68,7 +74,7 @@ TEMPLATE_TEST_CASE("createProductState", "[Test_Internal]", float, double) { GateImplementationsPI::applyPauliX(expected.data(), 3, {1}, false); GateImplementationsPI::applyHadamard(expected.data(), 3, {1}, false); - REQUIRE(st == approx(expected).margin(1e-7)); + REQUIRE(st == approx(expected).margin(PrecisionT{1e-7})); } SECTION("createProductState(\"+-0\") == |+-1> ") { const auto st = createProductState("+-0"); @@ -81,7 +87,7 @@ TEMPLATE_TEST_CASE("createProductState", "[Test_Internal]", float, double) { GateImplementationsPI::applyPauliX(expected.data(), 3, {2}, false); - REQUIRE(st != approx(expected).margin(1e-7)); + REQUIRE(st != approx(expected).margin(PrecisionT{1e-7})); } } diff --git a/pennylane_lightning/src/tests/Test_Measures.cpp b/pennylane_lightning/src/tests/Test_Measures.cpp index 6e15570f06..7d2c7c3d4c 100644 --- a/pennylane_lightning/src/tests/Test_Measures.cpp +++ b/pennylane_lightning/src/tests/Test_Measures.cpp @@ -9,6 +9,10 @@ #include +#if defined(_MSC_VER) +#pragma warning(disable : 4305) +#endif + using namespace Pennylane; namespace { diff --git a/pennylane_lightning/src/tests/Test_Util.cpp b/pennylane_lightning/src/tests/Test_Util.cpp index 83c19c828d..162d73fa26 100644 --- a/pennylane_lightning/src/tests/Test_Util.cpp +++ b/pennylane_lightning/src/tests/Test_Util.cpp @@ -13,6 +13,10 @@ #include "TestHelpers.hpp" +#if defined(_MSC_VER) +#pragma warning(disable : 4305) +#endif + using namespace Pennylane; /** @@ -20,9 +24,9 @@ using namespace Pennylane; * multiplication. */ TEMPLATE_TEST_CASE("Util::ConstMult", "[Util]", float, double) { - constexpr TestType r_val = 0.679; - constexpr std::complex c0_val{1.321, -0.175}; - constexpr std::complex c1_val{0.579, 1.334}; + constexpr TestType r_val{0.679}; + constexpr std::complex c0_val{TestType{1.321}, TestType{-0.175}}; + constexpr std::complex c1_val{TestType{0.579}, TestType{1.334}}; SECTION("Real times Complex") { constexpr std::complex result = @@ -79,7 +83,7 @@ TEMPLATE_TEST_CASE("Utility math functions", "[Util][LinearAlgebra]", float, for (size_t i = 0; i < 64; i++) { std::vector data(i); TestType rem; - std::modf(sqrt(i), &rem); + std::modf(sqrt(static_cast(i)), &rem); if (i < 4) { CHECK_THROWS_AS(Util::dimSize(data), std::invalid_argument); CHECK_THROWS_WITH(Util::dimSize(data), @@ -101,9 +105,12 @@ TEMPLATE_TEST_CASE("Utility math functions", "[Util][LinearAlgebra]", float, SECTION("innerProd") { SECTION("Iterative increment") { for (size_t i = 0; i < 12; i++) { - std::vector> data1(1UL << i, {1, 1}); - std::vector> data2(1UL << i, {1, 1}); - std::complex expected_result(0, 1UL << (i + 1)); + std::vector> data1(size_t{1U} << i, + {1, 1}); + std::vector> data2(size_t{1U} << i, + {1, 1}); + std::complex expected_result(0, + size_t{1U} << (i + 1)); std::complex result = Util::innerProd(data1, data2); CHECK(isApproxEqual(result, expected_result)); } @@ -123,9 +130,12 @@ TEMPLATE_TEST_CASE("Utility math functions", "[Util][LinearAlgebra]", float, SECTION("innerProdC") { SECTION("Iterative increment") { for (size_t i = 0; i < 12; i++) { - std::vector> data1(1UL << i, {1, 1}); - std::vector> data2(1UL << i, {1, 1}); - std::complex expected_result(1UL << (i + 1), 0); + std::vector> data1(size_t{1U} << i, + {1, 1}); + std::vector> data2(size_t{1U} << i, + {1, 1}); + std::complex expected_result(size_t{1U} << (i + 1), + 0); std::complex result = Util::innerProdC(data1, data2); CAPTURE(result); CAPTURE(expected_result); @@ -206,9 +216,9 @@ TEMPLATE_TEST_CASE("Utility math functions", "[Util][LinearAlgebra]", float, SECTION("vecMatrixProd") { SECTION("Simple Iterative") { for (size_t m = 2; m < 8; m++) { - std::vector mat(m * m, 1); - std::vector v_in(m, 1); - std::vector v_expected(m, m); + std::vector mat(m * m, TestType{1.0}); + std::vector v_in(m, TestType{1.0}); + std::vector v_expected(m, static_cast(m)); std::vector v_out = Util::vecMatrixProd(v_in, mat, m, m); @@ -558,7 +568,8 @@ TEST_CASE("Utility bit operations", "[Util][BitUtil]") { uint64_t n = static_cast(1U) << static_cast(c); CHECK(Util::Internal::countTrailing0(n) == c); - CHECK(Util::Internal::countTrailing0(n | (1UL << 63U)) == c); + CHECK(Util::Internal::countTrailing0( + n | (uint64_t{1U} << 63U)) == c); } } }