From bdcd9d120c0e77c3b00b4336e61309716ea58137 Mon Sep 17 00:00:00 2001 From: Tianyi Wang Date: Wed, 9 Aug 2023 01:14:33 +0200 Subject: [PATCH 01/16] Add w state --- include/algorithms/WState.hpp | 10 ++++++++++ src/CMakeLists.txt | 2 ++ src/algorithms/WState.cpp | 27 +++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 include/algorithms/WState.hpp create mode 100644 src/algorithms/WState.cpp diff --git a/include/algorithms/WState.hpp b/include/algorithms/WState.hpp new file mode 100644 index 000000000..bd7e60be1 --- /dev/null +++ b/include/algorithms/WState.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include + +namespace qc { +class WState : public QuantumComputation { +public: + explicit WState(std::size_t nq); +}; +} // namespace qc \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8d1b64966..2c6fcc9bb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -28,6 +28,7 @@ if(NOT TARGET ${PROJECT_NAME}) ${PROJECT_SOURCE_DIR}/include/algorithms/QFT.hpp ${PROJECT_SOURCE_DIR}/include/algorithms/QPE.hpp ${PROJECT_SOURCE_DIR}/include/algorithms/RandomCliffordCircuit.hpp + ${PROJECT_SOURCE_DIR}/include/algorithms/WState.hpp ${PROJECT_SOURCE_DIR}/include/CircuitOptimizer.hpp ${PROJECT_SOURCE_DIR}/include/Definitions.hpp ${PROJECT_SOURCE_DIR}/include/operations/Expression.hpp @@ -50,6 +51,7 @@ if(NOT TARGET ${PROJECT_NAME}) algorithms/QFT.cpp algorithms/QPE.cpp algorithms/RandomCliffordCircuit.cpp + algorithms/WState.cpp CircuitOptimizer.cpp operations/Expression.cpp operations/NonUnitaryOperation.cpp diff --git a/src/algorithms/WState.cpp b/src/algorithms/WState.cpp new file mode 100644 index 000000000..f378b43a4 --- /dev/null +++ b/src/algorithms/WState.cpp @@ -0,0 +1,27 @@ +#include "algorithms/WState.hpp" + +namespace qc { +void fGate(QuantumComputation& qc, std::size_t i, std::size_t j, std::size_t n, std::size_t k) { + auto theta = std::acos(std::sqrt(1.0 / (n - k + 1))); + qc.ry(static_cast(j), -theta); + qc.z(static_cast(j), qc::Control{static_cast(i)}); + qc.ry(static_cast(j), theta); +} + + +WState::WState(std::size_t nq) : QuantumComputation(nq){ + name = "wstate_" + std::to_string(nq); + const auto top = static_cast(nq - 1); + + x(top); + + for (std::size_t m = 1; m < nq; m++) { + fGate(*this, nq - m, nq - m - 1, nq, m); + } + + for (std::size_t k = nq - 1; k > 0; k--) { + x(static_cast(k), qc::Control{static_cast(k - 1)}); + } + +} +} // namespace qc \ No newline at end of file From 33b4c6ea3e089d3fb2488e28ddb332eb544f7176 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 8 Aug 2023 23:22:54 +0000 Subject: [PATCH 02/16] =?UTF-8?q?=F0=9F=8E=A8=20pre-commit=20fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/algorithms/WState.hpp | 2 +- src/algorithms/WState.cpp | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/include/algorithms/WState.hpp b/include/algorithms/WState.hpp index bd7e60be1..fb3c49695 100644 --- a/include/algorithms/WState.hpp +++ b/include/algorithms/WState.hpp @@ -7,4 +7,4 @@ class WState : public QuantumComputation { public: explicit WState(std::size_t nq); }; -} // namespace qc \ No newline at end of file +} // namespace qc diff --git a/src/algorithms/WState.cpp b/src/algorithms/WState.cpp index f378b43a4..bbfa3cdf7 100644 --- a/src/algorithms/WState.cpp +++ b/src/algorithms/WState.cpp @@ -1,15 +1,15 @@ #include "algorithms/WState.hpp" namespace qc { -void fGate(QuantumComputation& qc, std::size_t i, std::size_t j, std::size_t n, std::size_t k) { +void fGate(QuantumComputation& qc, std::size_t i, std::size_t j, std::size_t n, + std::size_t k) { auto theta = std::acos(std::sqrt(1.0 / (n - k + 1))); qc.ry(static_cast(j), -theta); qc.z(static_cast(j), qc::Control{static_cast(i)}); qc.ry(static_cast(j), theta); } - -WState::WState(std::size_t nq) : QuantumComputation(nq){ +WState::WState(std::size_t nq) : QuantumComputation(nq) { name = "wstate_" + std::to_string(nq); const auto top = static_cast(nq - 1); @@ -22,6 +22,5 @@ WState::WState(std::size_t nq) : QuantumComputation(nq){ for (std::size_t k = nq - 1; k > 0; k--) { x(static_cast(k), qc::Control{static_cast(k - 1)}); } - } -} // namespace qc \ No newline at end of file +} // namespace qc From cdd64685ce0732f39160540b9fe2fa98db999ef6 Mon Sep 17 00:00:00 2001 From: Tianyi Wang <56802240+tyi1025@users.noreply.github.com> Date: Wed, 9 Aug 2023 09:45:19 +0200 Subject: [PATCH 03/16] Apply suggestions from code review Co-authored-by: Lukas Burgholzer --- src/algorithms/WState.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/algorithms/WState.cpp b/src/algorithms/WState.cpp index bbfa3cdf7..0b2cf571c 100644 --- a/src/algorithms/WState.cpp +++ b/src/algorithms/WState.cpp @@ -1,15 +1,15 @@ #include "algorithms/WState.hpp" namespace qc { -void fGate(QuantumComputation& qc, std::size_t i, std::size_t j, std::size_t n, - std::size_t k) { - auto theta = std::acos(std::sqrt(1.0 / (n - k + 1))); +void fGate(QuantumComputation& qc, const std::size_t i, const std::size_t j, const std::size_t n, + const std::size_t k) { + const auto theta = std::acos(std::sqrt(1.0 / static_cast(n - k + 1))); qc.ry(static_cast(j), -theta); qc.z(static_cast(j), qc::Control{static_cast(i)}); qc.ry(static_cast(j), theta); } -WState::WState(std::size_t nq) : QuantumComputation(nq) { +WState::WState(const std::size_t nq) : QuantumComputation(nq) { name = "wstate_" + std::to_string(nq); const auto top = static_cast(nq - 1); From 9f2c93acff7efa653b1052edf3fa5089737c7792 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 9 Aug 2023 07:45:30 +0000 Subject: [PATCH 04/16] =?UTF-8?q?=F0=9F=8E=A8=20pre-commit=20fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/algorithms/WState.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/algorithms/WState.cpp b/src/algorithms/WState.cpp index 0b2cf571c..77089b5b3 100644 --- a/src/algorithms/WState.cpp +++ b/src/algorithms/WState.cpp @@ -1,8 +1,8 @@ #include "algorithms/WState.hpp" namespace qc { -void fGate(QuantumComputation& qc, const std::size_t i, const std::size_t j, const std::size_t n, - const std::size_t k) { +void fGate(QuantumComputation& qc, const std::size_t i, const std::size_t j, + const std::size_t n, const std::size_t k) { const auto theta = std::acos(std::sqrt(1.0 / static_cast(n - k + 1))); qc.ry(static_cast(j), -theta); qc.z(static_cast(j), qc::Control{static_cast(i)}); From fd107befb26cc73ded7cd6a076982ee9358d47b8 Mon Sep 17 00:00:00 2001 From: Tianyi Wang Date: Wed, 9 Aug 2023 10:43:02 +0200 Subject: [PATCH 05/16] Refactor wstate and add test --- src/algorithms/WState.cpp | 8 +++-- test/CMakeLists.txt | 3 +- test/algorithms/test_wstate.cpp | 59 +++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 test/algorithms/test_wstate.cpp diff --git a/src/algorithms/WState.cpp b/src/algorithms/WState.cpp index 77089b5b3..5a3ae98e3 100644 --- a/src/algorithms/WState.cpp +++ b/src/algorithms/WState.cpp @@ -2,14 +2,18 @@ namespace qc { void fGate(QuantumComputation& qc, const std::size_t i, const std::size_t j, - const std::size_t n, const std::size_t k) { - const auto theta = std::acos(std::sqrt(1.0 / static_cast(n - k + 1))); + const std::size_t k, const std::size_t n) { + const auto theta = std::acos(std::sqrt(1.0 / static_cast(k - n + 1))); qc.ry(static_cast(j), -theta); qc.z(static_cast(j), qc::Control{static_cast(i)}); qc.ry(static_cast(j), theta); } WState::WState(const std::size_t nq) : QuantumComputation(nq) { + if (nq == 0) { + return; + } + name = "wstate_" + std::to_string(nq); const auto top = static_cast(nq - 1); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8af5d51ad..fabbc7b1f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -25,7 +25,8 @@ package_add_test( algorithms/test_entanglement.cpp algorithms/test_grcs.cpp algorithms/test_random_clifford.cpp - algorithms/test_qpe.cpp) + algorithms/test_qpe.cpp + algorithms/test_wstate.cpp) package_add_test( ${PROJECT_NAME}-test-zx diff --git a/test/algorithms/test_wstate.cpp b/test/algorithms/test_wstate.cpp new file mode 100644 index 000000000..780560fca --- /dev/null +++ b/test/algorithms/test_wstate.cpp @@ -0,0 +1,59 @@ +#include "algorithms/WState.hpp" +#include "dd/FunctionalityConstruction.hpp" +#include "dd/Simulation.hpp" + +#include "gtest/gtest.h" + +#include +#include + +class WState : public testing::TestWithParam { +protected: + void TearDown() override {} + void SetUp() override {} +}; + +std::vector generateWStateStrings(std::size_t length) { + std::vector result; + for (int i = 0; i < (1 << length); i++) { + int countOnes = 0; + std::string binaryString; + for (size_t j = 0; j < length; j++) { + if (((i >> j) & 1) != 0) { + countOnes++; + binaryString += "1"; + } else { + binaryString += "0"; + } + } + if (countOnes == 1) { + result.push_back(binaryString); + } + } + return result; +} + +INSTANTIATE_TEST_SUITE_P( + WState, WState, testing::Range(2U, 30U, 6U), + [](const testing::TestParamInfo& inf) { + // Generate names for test cases + const auto nqubits = inf.param; + std::stringstream ss{}; + ss << nqubits << "_qubits"; + return ss.str(); + }); + +TEST_P(WState, FunctionTest) { + const auto nq = GetParam(); + + auto qc = qc::WState(nq); + auto dd = std::make_unique>(qc.getNqubits()); + const std::size_t shots = 1024; + auto measurements = + simulate(&qc, dd->makeZeroState(qc.getNqubits()), dd, shots); + auto results = generateWStateStrings(nq); + + for (const auto& result : results) { + EXPECT_TRUE(measurements.find(result) != measurements.end()); + } +} From d52f79c1429b103a145313831ebe32951dde382e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 9 Aug 2023 08:44:16 +0000 Subject: [PATCH 06/16] =?UTF-8?q?=F0=9F=8E=A8=20pre-commit=20fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/algorithms/test_wstate.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/test/algorithms/test_wstate.cpp b/test/algorithms/test_wstate.cpp index 780560fca..2a9b65210 100644 --- a/test/algorithms/test_wstate.cpp +++ b/test/algorithms/test_wstate.cpp @@ -3,7 +3,6 @@ #include "dd/Simulation.hpp" #include "gtest/gtest.h" - #include #include From 894095401d0eb9c852fee467427a8796959db6fc Mon Sep 17 00:00:00 2001 From: Tianyi Wang Date: Wed, 9 Aug 2023 11:07:12 +0200 Subject: [PATCH 07/16] Refactor to get rid of casts --- include/algorithms/WState.hpp | 2 +- src/algorithms/WState.cpp | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/include/algorithms/WState.hpp b/include/algorithms/WState.hpp index fb3c49695..cd879c7a0 100644 --- a/include/algorithms/WState.hpp +++ b/include/algorithms/WState.hpp @@ -5,6 +5,6 @@ namespace qc { class WState : public QuantumComputation { public: - explicit WState(std::size_t nq); + explicit WState(Qubit nq); }; } // namespace qc diff --git a/src/algorithms/WState.cpp b/src/algorithms/WState.cpp index 5a3ae98e3..59dc4d947 100644 --- a/src/algorithms/WState.cpp +++ b/src/algorithms/WState.cpp @@ -1,30 +1,30 @@ #include "algorithms/WState.hpp" namespace qc { -void fGate(QuantumComputation& qc, const std::size_t i, const std::size_t j, - const std::size_t k, const std::size_t n) { +void fGate(QuantumComputation& qc, const Qubit i, const Qubit j, + const Qubit k, const Qubit n) { const auto theta = std::acos(std::sqrt(1.0 / static_cast(k - n + 1))); - qc.ry(static_cast(j), -theta); - qc.z(static_cast(j), qc::Control{static_cast(i)}); - qc.ry(static_cast(j), theta); + qc.ry(j, -theta); + qc.z(j, qc::Control{i}); + qc.ry(j, theta); } -WState::WState(const std::size_t nq) : QuantumComputation(nq) { +WState::WState(const Qubit nq) : QuantumComputation(nq) { if (nq == 0) { return; } name = "wstate_" + std::to_string(nq); - const auto top = static_cast(nq - 1); + const auto top = nq - 1; x(top); - for (std::size_t m = 1; m < nq; m++) { + for (Qubit m = 1; m < nq; m++) { fGate(*this, nq - m, nq - m - 1, nq, m); } - for (std::size_t k = nq - 1; k > 0; k--) { - x(static_cast(k), qc::Control{static_cast(k - 1)}); + for (Qubit k = nq - 1; k > 0; k--) { + x(k, qc::Control{k - 1}); } } } // namespace qc From 7b586d56b3c8f4352ea006e1ec64c5bfe2d2d534 Mon Sep 17 00:00:00 2001 From: Tianyi Wang <56802240+tyi1025@users.noreply.github.com> Date: Wed, 9 Aug 2023 11:10:32 +0200 Subject: [PATCH 08/16] Apply suggestions from code review Co-authored-by: Lukas Burgholzer --- test/algorithms/test_wstate.cpp | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/test/algorithms/test_wstate.cpp b/test/algorithms/test_wstate.cpp index 2a9b65210..72363f78b 100644 --- a/test/algorithms/test_wstate.cpp +++ b/test/algorithms/test_wstate.cpp @@ -7,27 +7,15 @@ #include class WState : public testing::TestWithParam { -protected: - void TearDown() override {} - void SetUp() override {} }; -std::vector generateWStateStrings(std::size_t length) { +std::vector generateWStateStrings(const std::size_t length) { std::vector result; - for (int i = 0; i < (1 << length); i++) { - int countOnes = 0; - std::string binaryString; - for (size_t j = 0; j < length; j++) { - if (((i >> j) & 1) != 0) { - countOnes++; - binaryString += "1"; - } else { - binaryString += "0"; - } - } - if (countOnes == 1) { - result.push_back(binaryString); - } + result.reserve(length); + for (std::size_t i = 0U; i < length; ++i) { + auto binaryString = std::string('0', length); + binaryString[i] = '1'; + result.emplace_back(binaryString); } return result; } @@ -50,9 +38,7 @@ TEST_P(WState, FunctionTest) { const std::size_t shots = 1024; auto measurements = simulate(&qc, dd->makeZeroState(qc.getNqubits()), dd, shots); - auto results = generateWStateStrings(nq); - - for (const auto& result : results) { + for (const auto& result : generateWStateStrings(nq)) { EXPECT_TRUE(measurements.find(result) != measurements.end()); } } From 74c661d416f42f653ad86584cff471fea141ea96 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 9 Aug 2023 09:12:38 +0000 Subject: [PATCH 09/16] =?UTF-8?q?=F0=9F=8E=A8=20pre-commit=20fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/algorithms/test_wstate.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/algorithms/test_wstate.cpp b/test/algorithms/test_wstate.cpp index 72363f78b..8758c4660 100644 --- a/test/algorithms/test_wstate.cpp +++ b/test/algorithms/test_wstate.cpp @@ -6,8 +6,7 @@ #include #include -class WState : public testing::TestWithParam { -}; +class WState : public testing::TestWithParam {}; std::vector generateWStateStrings(const std::size_t length) { std::vector result; From 680a6f154bbd35c7c90c9ca3958e409a02ae7c9a Mon Sep 17 00:00:00 2001 From: Tianyi Wang Date: Wed, 9 Aug 2023 11:16:43 +0200 Subject: [PATCH 10/16] Fix string constructor and raise test to 128 qubits --- src/CMakeLists.txt | 76 ++++++++++++++++----------------- test/algorithms/test_wstate.cpp | 4 +- 2 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2c6fcc9bb..c0872db1f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -90,41 +90,41 @@ add_subdirectory(zx) # add ECC library add_subdirectory(ecc) -# ** Note ** The following target will soon be removed from the project. All top-level projects -# should switch to nanobind. After that, the pybind submodules will be removed. -if(NOT TARGET ${PROJECT_NAME}-python) - # add pybind11 library - add_subdirectory("${PROJECT_SOURCE_DIR}/extern/pybind11" "extern/pybind11" EXCLUDE_FROM_ALL) - - if(NOT TARGET pybind11_json) - # add pybind11_json library - add_subdirectory("${PROJECT_SOURCE_DIR}/extern/pybind11_json" "extern/pybind11_json" - EXCLUDE_FROM_ALL) - endif() - - # add Python interface library - add_library( - ${PROJECT_NAME}-python - ${PROJECT_SOURCE_DIR}/include/python/qiskit/QuantumCircuit.hpp - ${PROJECT_SOURCE_DIR}/include/python/qiskit/QasmQobjExperiment.hpp - python/qiskit/QuantumCircuit.cpp python/qiskit/QasmQobjExperiment.cpp) - - # link with main project library and pybind11 libraries - target_link_libraries(${PROJECT_NAME}-python PUBLIC ${PROJECT_NAME} pybind11::pybind11 - pybind11_json) - - # the following sets the SYSTEM flag for the include dirs of the pybind11_json libs to suppress - # warnings - set_target_properties( - pybind11_json PROPERTIES INTERFACE_SYSTEM_INCLUDE_DIRECTORIES - $ - )# cmake-lint: disable=C0307 - - # add MQT alias - add_library(MQT::CorePython ALIAS ${PROJECT_NAME}-python) - add_library(MQT::${OLD_PROJECT_NAME}_python ALIAS ${PROJECT_NAME}-python) -endif() - -if(BUILD_MQT_CORE_BINDINGS) - add_subdirectory(python) -endif() +## ** Note ** The following target will soon be removed from the project. All top-level projects +## should switch to nanobind. After that, the pybind submodules will be removed. +#if(NOT TARGET ${PROJECT_NAME}-python) +# # add pybind11 library +# add_subdirectory("${PROJECT_SOURCE_DIR}/extern/pybind11" "extern/pybind11" EXCLUDE_FROM_ALL) +# +# if(NOT TARGET pybind11_json) +# # add pybind11_json library +# add_subdirectory("${PROJECT_SOURCE_DIR}/extern/pybind11_json" "extern/pybind11_json" +# EXCLUDE_FROM_ALL) +# endif() +# +# # add Python interface library +# add_library( +# ${PROJECT_NAME}-python +# ${PROJECT_SOURCE_DIR}/include/python/qiskit/QuantumCircuit.hpp +# ${PROJECT_SOURCE_DIR}/include/python/qiskit/QasmQobjExperiment.hpp +# python/qiskit/QuantumCircuit.cpp python/qiskit/QasmQobjExperiment.cpp) +# +# # link with main project library and pybind11 libraries +# target_link_libraries(${PROJECT_NAME}-python PUBLIC ${PROJECT_NAME} pybind11::pybind11 +# pybind11_json) +# +# # the following sets the SYSTEM flag for the include dirs of the pybind11_json libs to suppress +# # warnings +# set_target_properties( +# pybind11_json PROPERTIES INTERFACE_SYSTEM_INCLUDE_DIRECTORIES +# $ +# )# cmake-lint: disable=C0307 +# +# # add MQT alias +# add_library(MQT::CorePython ALIAS ${PROJECT_NAME}-python) +# add_library(MQT::${OLD_PROJECT_NAME}_python ALIAS ${PROJECT_NAME}-python) +#endif() + +#if(BUILD_MQT_CORE_BINDINGS) +# add_subdirectory(python) +#endif() diff --git a/test/algorithms/test_wstate.cpp b/test/algorithms/test_wstate.cpp index 8758c4660..891116975 100644 --- a/test/algorithms/test_wstate.cpp +++ b/test/algorithms/test_wstate.cpp @@ -12,7 +12,7 @@ std::vector generateWStateStrings(const std::size_t length) { std::vector result; result.reserve(length); for (std::size_t i = 0U; i < length; ++i) { - auto binaryString = std::string('0', length); + auto binaryString = std::string(length, '0'); binaryString[i] = '1'; result.emplace_back(binaryString); } @@ -20,7 +20,7 @@ std::vector generateWStateStrings(const std::size_t length) { } INSTANTIATE_TEST_SUITE_P( - WState, WState, testing::Range(2U, 30U, 6U), + WState, WState, testing::Range(2U, 128U, 7U), [](const testing::TestParamInfo& inf) { // Generate names for test cases const auto nqubits = inf.param; From 7af596415beea8050ac607af18fe31184eb92aac Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 9 Aug 2023 09:17:04 +0000 Subject: [PATCH 11/16] =?UTF-8?q?=F0=9F=8E=A8=20pre-commit=20fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CMakeLists.txt | 52 +++++++++++++++------------------------ src/algorithms/WState.cpp | 4 +-- 2 files changed, 22 insertions(+), 34 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c0872db1f..12198a66e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -90,41 +90,29 @@ add_subdirectory(zx) # add ECC library add_subdirectory(ecc) -## ** Note ** The following target will soon be removed from the project. All top-level projects -## should switch to nanobind. After that, the pybind submodules will be removed. -#if(NOT TARGET ${PROJECT_NAME}-python) -# # add pybind11 library -# add_subdirectory("${PROJECT_SOURCE_DIR}/extern/pybind11" "extern/pybind11" EXCLUDE_FROM_ALL) +# ** Note ** The following target will soon be removed from the project. All top-level projects +# should switch to nanobind. After that, the pybind submodules will be removed. if(NOT TARGET +# ${PROJECT_NAME}-python) # add pybind11 library +# add_subdirectory("${PROJECT_SOURCE_DIR}/extern/pybind11" "extern/pybind11" EXCLUDE_FROM_ALL) # -# if(NOT TARGET pybind11_json) -# # add pybind11_json library -# add_subdirectory("${PROJECT_SOURCE_DIR}/extern/pybind11_json" "extern/pybind11_json" -# EXCLUDE_FROM_ALL) -# endif() +# if(NOT TARGET pybind11_json) # add pybind11_json library +# add_subdirectory("${PROJECT_SOURCE_DIR}/extern/pybind11_json" "extern/pybind11_json" +# EXCLUDE_FROM_ALL) endif() # -# # add Python interface library -# add_library( -# ${PROJECT_NAME}-python -# ${PROJECT_SOURCE_DIR}/include/python/qiskit/QuantumCircuit.hpp -# ${PROJECT_SOURCE_DIR}/include/python/qiskit/QasmQobjExperiment.hpp -# python/qiskit/QuantumCircuit.cpp python/qiskit/QasmQobjExperiment.cpp) +# # add Python interface library add_library( ${PROJECT_NAME}-python +# ${PROJECT_SOURCE_DIR}/include/python/qiskit/QuantumCircuit.hpp +# ${PROJECT_SOURCE_DIR}/include/python/qiskit/QasmQobjExperiment.hpp +# python/qiskit/QuantumCircuit.cpp python/qiskit/QasmQobjExperiment.cpp) # -# # link with main project library and pybind11 libraries -# target_link_libraries(${PROJECT_NAME}-python PUBLIC ${PROJECT_NAME} pybind11::pybind11 -# pybind11_json) +# # link with main project library and pybind11 libraries +# target_link_libraries(${PROJECT_NAME}-python PUBLIC ${PROJECT_NAME} pybind11::pybind11 +# pybind11_json) # -# # the following sets the SYSTEM flag for the include dirs of the pybind11_json libs to suppress -# # warnings -# set_target_properties( -# pybind11_json PROPERTIES INTERFACE_SYSTEM_INCLUDE_DIRECTORIES -# $ -# )# cmake-lint: disable=C0307 +# # the following sets the SYSTEM flag for the include dirs of the pybind11_json libs to suppress # +# warnings set_target_properties( pybind11_json PROPERTIES INTERFACE_SYSTEM_INCLUDE_DIRECTORIES +# $ )# cmake-lint: disable=C0307 # -# # add MQT alias -# add_library(MQT::CorePython ALIAS ${PROJECT_NAME}-python) -# add_library(MQT::${OLD_PROJECT_NAME}_python ALIAS ${PROJECT_NAME}-python) -#endif() +# # add MQT alias add_library(MQT::CorePython ALIAS ${PROJECT_NAME}-python) +# add_library(MQT::${OLD_PROJECT_NAME}_python ALIAS ${PROJECT_NAME}-python) endif() -#if(BUILD_MQT_CORE_BINDINGS) -# add_subdirectory(python) -#endif() +# if(BUILD_MQT_CORE_BINDINGS) add_subdirectory(python) endif() diff --git a/src/algorithms/WState.cpp b/src/algorithms/WState.cpp index 59dc4d947..e6ab025ab 100644 --- a/src/algorithms/WState.cpp +++ b/src/algorithms/WState.cpp @@ -1,8 +1,8 @@ #include "algorithms/WState.hpp" namespace qc { -void fGate(QuantumComputation& qc, const Qubit i, const Qubit j, - const Qubit k, const Qubit n) { +void fGate(QuantumComputation& qc, const Qubit i, const Qubit j, const Qubit k, + const Qubit n) { const auto theta = std::acos(std::sqrt(1.0 / static_cast(k - n + 1))); qc.ry(j, -theta); qc.z(j, qc::Control{i}); From 04d48f90000c397f0b3c4488716432bd5043ac45 Mon Sep 17 00:00:00 2001 From: Tianyi Wang Date: Wed, 9 Aug 2023 11:23:56 +0200 Subject: [PATCH 12/16] Fix accidentally changed cmake --- src/CMakeLists.txt | 62 +++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 12198a66e..3d47b2d68 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -91,28 +91,40 @@ add_subdirectory(zx) add_subdirectory(ecc) # ** Note ** The following target will soon be removed from the project. All top-level projects -# should switch to nanobind. After that, the pybind submodules will be removed. if(NOT TARGET -# ${PROJECT_NAME}-python) # add pybind11 library -# add_subdirectory("${PROJECT_SOURCE_DIR}/extern/pybind11" "extern/pybind11" EXCLUDE_FROM_ALL) -# -# if(NOT TARGET pybind11_json) # add pybind11_json library -# add_subdirectory("${PROJECT_SOURCE_DIR}/extern/pybind11_json" "extern/pybind11_json" -# EXCLUDE_FROM_ALL) endif() -# -# # add Python interface library add_library( ${PROJECT_NAME}-python -# ${PROJECT_SOURCE_DIR}/include/python/qiskit/QuantumCircuit.hpp -# ${PROJECT_SOURCE_DIR}/include/python/qiskit/QasmQobjExperiment.hpp -# python/qiskit/QuantumCircuit.cpp python/qiskit/QasmQobjExperiment.cpp) -# -# # link with main project library and pybind11 libraries -# target_link_libraries(${PROJECT_NAME}-python PUBLIC ${PROJECT_NAME} pybind11::pybind11 -# pybind11_json) -# -# # the following sets the SYSTEM flag for the include dirs of the pybind11_json libs to suppress # -# warnings set_target_properties( pybind11_json PROPERTIES INTERFACE_SYSTEM_INCLUDE_DIRECTORIES -# $ )# cmake-lint: disable=C0307 -# -# # add MQT alias add_library(MQT::CorePython ALIAS ${PROJECT_NAME}-python) -# add_library(MQT::${OLD_PROJECT_NAME}_python ALIAS ${PROJECT_NAME}-python) endif() - -# if(BUILD_MQT_CORE_BINDINGS) add_subdirectory(python) endif() +# should switch to nanobind. After that, the pybind submodules will be removed. +if(NOT TARGET ${PROJECT_NAME}-python) + # add pybind11 library + add_subdirectory("${PROJECT_SOURCE_DIR}/extern/pybind11" "extern/pybind11" EXCLUDE_FROM_ALL) + + if(NOT TARGET pybind11_json) + # add pybind11_json library + add_subdirectory("${PROJECT_SOURCE_DIR}/extern/pybind11_json" "extern/pybind11_json" + EXCLUDE_FROM_ALL) + endif() + + # add Python interface library + add_library( + ${PROJECT_NAME}-python + ${PROJECT_SOURCE_DIR}/include/python/qiskit/QuantumCircuit.hpp + ${PROJECT_SOURCE_DIR}/include/python/qiskit/QasmQobjExperiment.hpp + python/qiskit/QuantumCircuit.cpp python/qiskit/QasmQobjExperiment.cpp) + + # link with main project library and pybind11 libraries + target_link_libraries(${PROJECT_NAME}-python PUBLIC ${PROJECT_NAME} pybind11::pybind11 + pybind11_json) + + # the following sets the SYSTEM flag for the include dirs of the pybind11_json libs to suppress + # warnings + set_target_properties( + pybind11_json PROPERTIES INTERFACE_SYSTEM_INCLUDE_DIRECTORIES + $ + )# cmake-lint: disable=C0307 + + # add MQT alias + add_library(MQT::CorePython ALIAS ${PROJECT_NAME}-python) + add_library(MQT::${OLD_PROJECT_NAME}_python ALIAS ${PROJECT_NAME}-python) +endif() + +if(BUILD_MQT_CORE_BINDINGS) + add_subdirectory(python) +endif() \ No newline at end of file From 45c74503288601538d33b9800f6d6b207328bbaa Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 9 Aug 2023 09:25:44 +0000 Subject: [PATCH 13/16] =?UTF-8?q?=F0=9F=8E=A8=20pre-commit=20fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CMakeLists.txt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3d47b2d68..2c6fcc9bb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -99,25 +99,25 @@ if(NOT TARGET ${PROJECT_NAME}-python) if(NOT TARGET pybind11_json) # add pybind11_json library add_subdirectory("${PROJECT_SOURCE_DIR}/extern/pybind11_json" "extern/pybind11_json" - EXCLUDE_FROM_ALL) + EXCLUDE_FROM_ALL) endif() # add Python interface library add_library( - ${PROJECT_NAME}-python - ${PROJECT_SOURCE_DIR}/include/python/qiskit/QuantumCircuit.hpp - ${PROJECT_SOURCE_DIR}/include/python/qiskit/QasmQobjExperiment.hpp - python/qiskit/QuantumCircuit.cpp python/qiskit/QasmQobjExperiment.cpp) + ${PROJECT_NAME}-python + ${PROJECT_SOURCE_DIR}/include/python/qiskit/QuantumCircuit.hpp + ${PROJECT_SOURCE_DIR}/include/python/qiskit/QasmQobjExperiment.hpp + python/qiskit/QuantumCircuit.cpp python/qiskit/QasmQobjExperiment.cpp) # link with main project library and pybind11 libraries target_link_libraries(${PROJECT_NAME}-python PUBLIC ${PROJECT_NAME} pybind11::pybind11 - pybind11_json) + pybind11_json) # the following sets the SYSTEM flag for the include dirs of the pybind11_json libs to suppress # warnings set_target_properties( - pybind11_json PROPERTIES INTERFACE_SYSTEM_INCLUDE_DIRECTORIES - $ + pybind11_json PROPERTIES INTERFACE_SYSTEM_INCLUDE_DIRECTORIES + $ )# cmake-lint: disable=C0307 # add MQT alias @@ -127,4 +127,4 @@ endif() if(BUILD_MQT_CORE_BINDINGS) add_subdirectory(python) -endif() \ No newline at end of file +endif() From 9d99d025e82f94851a6c7d767574d1b88119b6d5 Mon Sep 17 00:00:00 2001 From: Tianyi Wang Date: Wed, 9 Aug 2023 11:33:44 +0200 Subject: [PATCH 14/16] Refactor to fix cpp linter warning --- include/algorithms/WState.hpp | 2 +- src/algorithms/WState.cpp | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/include/algorithms/WState.hpp b/include/algorithms/WState.hpp index cd879c7a0..fb3c49695 100644 --- a/include/algorithms/WState.hpp +++ b/include/algorithms/WState.hpp @@ -5,6 +5,6 @@ namespace qc { class WState : public QuantumComputation { public: - explicit WState(Qubit nq); + explicit WState(std::size_t nq); }; } // namespace qc diff --git a/src/algorithms/WState.cpp b/src/algorithms/WState.cpp index e6ab025ab..3204e1c14 100644 --- a/src/algorithms/WState.cpp +++ b/src/algorithms/WState.cpp @@ -9,21 +9,22 @@ void fGate(QuantumComputation& qc, const Qubit i, const Qubit j, const Qubit k, qc.ry(j, theta); } -WState::WState(const Qubit nq) : QuantumComputation(nq) { +WState::WState(const std::size_t nq) : QuantumComputation(nq) { if (nq == 0) { return; } + auto nQubits = static_cast(nq); name = "wstate_" + std::to_string(nq); - const auto top = nq - 1; + const auto top = nQubits - 1; x(top); for (Qubit m = 1; m < nq; m++) { - fGate(*this, nq - m, nq - m - 1, nq, m); + fGate(*this, nQubits - m, nQubits - m - 1, nQubits, m); } - for (Qubit k = nq - 1; k > 0; k--) { + for (Qubit k = nQubits - 1; k > 0; k--) { x(k, qc::Control{k - 1}); } } From 1be724bb7fafe5cc4eff947022465e7fa7438c3b Mon Sep 17 00:00:00 2001 From: Tianyi Wang Date: Wed, 9 Aug 2023 11:45:39 +0200 Subject: [PATCH 15/16] Revert change and apply code review suggestions --- include/algorithms/WState.hpp | 2 +- src/algorithms/WState.cpp | 10 ++++------ test/algorithms/test_wstate.cpp | 7 +++---- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/include/algorithms/WState.hpp b/include/algorithms/WState.hpp index fb3c49695..cd879c7a0 100644 --- a/include/algorithms/WState.hpp +++ b/include/algorithms/WState.hpp @@ -5,6 +5,6 @@ namespace qc { class WState : public QuantumComputation { public: - explicit WState(std::size_t nq); + explicit WState(Qubit nq); }; } // namespace qc diff --git a/src/algorithms/WState.cpp b/src/algorithms/WState.cpp index 3204e1c14..437de5b9d 100644 --- a/src/algorithms/WState.cpp +++ b/src/algorithms/WState.cpp @@ -9,22 +9,20 @@ void fGate(QuantumComputation& qc, const Qubit i, const Qubit j, const Qubit k, qc.ry(j, theta); } -WState::WState(const std::size_t nq) : QuantumComputation(nq) { +WState::WState(const Qubit nq) : QuantumComputation(nq) { if (nq == 0) { return; } - auto nQubits = static_cast(nq); name = "wstate_" + std::to_string(nq); - const auto top = nQubits - 1; - x(top); + x(nq - 1); for (Qubit m = 1; m < nq; m++) { - fGate(*this, nQubits - m, nQubits - m - 1, nQubits, m); + fGate(*this, nq - m, nq - m - 1, nq, m); } - for (Qubit k = nQubits - 1; k > 0; k--) { + for (Qubit k = nq - 1; k > 0; k--) { x(k, qc::Control{k - 1}); } } diff --git a/test/algorithms/test_wstate.cpp b/test/algorithms/test_wstate.cpp index 891116975..71fa9b498 100644 --- a/test/algorithms/test_wstate.cpp +++ b/test/algorithms/test_wstate.cpp @@ -1,12 +1,11 @@ #include "algorithms/WState.hpp" -#include "dd/FunctionalityConstruction.hpp" #include "dd/Simulation.hpp" #include "gtest/gtest.h" #include #include -class WState : public testing::TestWithParam {}; +class WState : public testing::TestWithParam {}; std::vector generateWStateStrings(const std::size_t length) { std::vector result; @@ -20,7 +19,7 @@ std::vector generateWStateStrings(const std::size_t length) { } INSTANTIATE_TEST_SUITE_P( - WState, WState, testing::Range(2U, 128U, 7U), + WState, WState, testing::Range(0U, 128U, 7U), [](const testing::TestParamInfo& inf) { // Generate names for test cases const auto nqubits = inf.param; @@ -35,7 +34,7 @@ TEST_P(WState, FunctionTest) { auto qc = qc::WState(nq); auto dd = std::make_unique>(qc.getNqubits()); const std::size_t shots = 1024; - auto measurements = + const auto measurements = simulate(&qc, dd->makeZeroState(qc.getNqubits()), dd, shots); for (const auto& result : generateWStateStrings(nq)) { EXPECT_TRUE(measurements.find(result) != measurements.end()); From 4d3eb182978659869a267ecbae20fbfd5ac8ace0 Mon Sep 17 00:00:00 2001 From: Tianyi Wang Date: Wed, 9 Aug 2023 11:46:34 +0200 Subject: [PATCH 16/16] Remove nq=0 case --- src/algorithms/WState.cpp | 4 ---- test/algorithms/test_wstate.cpp | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/algorithms/WState.cpp b/src/algorithms/WState.cpp index 437de5b9d..db2800ae5 100644 --- a/src/algorithms/WState.cpp +++ b/src/algorithms/WState.cpp @@ -10,10 +10,6 @@ void fGate(QuantumComputation& qc, const Qubit i, const Qubit j, const Qubit k, } WState::WState(const Qubit nq) : QuantumComputation(nq) { - if (nq == 0) { - return; - } - name = "wstate_" + std::to_string(nq); x(nq - 1); diff --git a/test/algorithms/test_wstate.cpp b/test/algorithms/test_wstate.cpp index 71fa9b498..c076b8378 100644 --- a/test/algorithms/test_wstate.cpp +++ b/test/algorithms/test_wstate.cpp @@ -19,7 +19,7 @@ std::vector generateWStateStrings(const std::size_t length) { } INSTANTIATE_TEST_SUITE_P( - WState, WState, testing::Range(0U, 128U, 7U), + WState, WState, testing::Range(1U, 128U, 7U), [](const testing::TestParamInfo& inf) { // Generate names for test cases const auto nqubits = inf.param;