From 69e7c9a4ef29e3a19bba938c694fd09c3132c72d Mon Sep 17 00:00:00 2001 From: jamesETsmith Date: Fri, 28 Jun 2024 16:55:52 -0400 Subject: [PATCH 1/4] [feature/feature_buildout] Adding initial docs for the features we want implement --- .gitignore | 1 + README.md | 17 ++++++----------- docs/planning.md | 22 ++++++++++++++++++++++ include/__pauli_string.hpp | 2 +- src/fast_pauli.cpp | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 docs/planning.md create mode 100644 src/fast_pauli.cpp diff --git a/.gitignore b/.gitignore index bd3e442..aaf460a 100644 --- a/.gitignore +++ b/.gitignore @@ -79,3 +79,4 @@ __pycache__ logs scratch notes +.cache diff --git a/README.md b/README.md index a32b36c..356574f 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,18 @@ # Summary # TODOs -- [ ] Figure out the tranpose non-sense or support both -- [ ] Clean up `apply_batch` we shouldn't need to pass a coeff - [ ] Add docstrings - [X] Pauli - [X] PauliString - [ ] PauliOp - [ ] SummedPauliOp +- [ ] Figure out the tranpose non-sense or support both (some functions take the transpose of the states and others don't) +- [ ] Clean up `apply_batch` we shouldn't need to pass a coeff - [ ] Clean up tests -- [X] Clean up test utils -- [X] Add type aliases and factory functions to utils for fast_pauli -- [X] Seach the names and make sure we don't have any overlap with other projects -- [ ] Build out pauli decomposer -- [X] Remove the weights argument and rename to data -- [X] Add namespace - [ ] Add apply method to SummedPauliOp that takes precomputed weighted data -- [ ] Writeup for docs - [ ] Add pybind11 interface and python examples -- [ ] Change functions names over to default to parallel impl and use `_serial` for the serial implementation +- [ ] Change functions that may run in parallel to take [`std::execution_policy`](https://en.cppreference.com/w/cpp/algorithm/execution_policy_tag_t) +- [ ] Possibly add levels to methods like BLAS to group methods by scaling - [ ] Migrate `PauliOp` and `SummedPauliOp` to only store mdspans rather than copies of the data itself ## Requirements @@ -32,7 +26,7 @@ ## Build and Test ```bash -cmake -B build -DCMAKE_CXX_COMPILER= +cmake -B build -DCMAKE_CXX_COMPILER=clang++ cmake --build build ctest --test-dir build ``` @@ -42,3 +36,4 @@ ctest --test-dir build The C++ portion of this library relies heavily on spans and views. These lightweight accessors are helpful and performant, but can lead to dangling spans or accessing bad memory if used improperly. Developers should familiarize themselves with these dangers by reviewing [this post](https://hackingcpp.com/cpp/std/span.html). + diff --git a/docs/planning.md b/docs/planning.md new file mode 100644 index 0000000..246cc42 --- /dev/null +++ b/docs/planning.md @@ -0,0 +1,22 @@ +# General + + +## Notation + +- Pauli Matrix $\sigma_i \in \{ I,X,Y,Z \}$ +- Pauli String $\mathcal{\hat{P}} = \bigotimes_i \sigma_i$ +- Sum of weighted Pauli strings (currently called `PauliOp`) $A_k = \sum_i h_i \mathcal{\hat{P_i}}$ +- Sum of summed weighted Pauli strings (currently called `SummedPauliOp`) $B = \sum_k \sum_i h_{ik} \mathcal{\hat{P_i}}$ + +# List of Operations + +Here's a terse list of the type of operations we want to support in `fast_pauli` (this list will grow over time): + +1. Pauli String to sparse matrix (Pauli Composer) +2. $\mathcal{\hat{P}} \ket{\psi}$ +3. $\mathcal{\hat{P}} \ket{\psi_t}$ +4. $\big( \sum_i h_i \mathcal{\hat{P}}_i \big) \ket{\psi_t}$ +5. $\big(\sum_k \sum_i h_{ik} \mathcal{\hat{P}}_i \big) \ket{\psi_t}$ +6. $\big(\sum_k x_{tk} \sum_i h_{ik} \mathcal{\hat{P}}_i \big) \ket{\psi_t}$ +7. $\bigg(\sum_k \big( \sum_i h_{ik} \mathcal{\hat{P}}_i \big)^2 \bigg) \ket{\psi_t}$ +8. Calculate $\bra{\psi_t} \{ \mathcal{\hat{P_i}}, \hat{A_k} \} \ket{\psi}$ and $\bra{\psi} \mathcal{\hat{P_i}} \ket{\psi}$ diff --git a/include/__pauli_string.hpp b/include/__pauli_string.hpp index 4f59038..9060b4d 100644 --- a/include/__pauli_string.hpp +++ b/include/__pauli_string.hpp @@ -39,7 +39,7 @@ struct PauliString { * calculates the weight. * */ - constexpr PauliString(std::span const &paulis) + constexpr PauliString(std::span const paulis) : weight(0), paulis(paulis.begin(), paulis.end()) { for (auto const &pauli : paulis) { weight += pauli.code > 0; diff --git a/src/fast_pauli.cpp b/src/fast_pauli.cpp new file mode 100644 index 0000000..22db19a --- /dev/null +++ b/src/fast_pauli.cpp @@ -0,0 +1,33 @@ +#include "fast_pauli.hpp" + +#include +#include +#include +#include + +namespace py = pybind11; + +void scale_tensor_3d(py::array_t array, double scale) { + auto arr = array.mutable_unchecked<>(); + std::mdspan tensor(arr.mutable_data(), arr.shape(0), arr.shape(1), + arr.shape(2)); + +#pragma omp parallel for collapse(3) + for (size_t i = 0; i < tensor.extent(0); i++) { + for (size_t j = 0; j < tensor.extent(1); j++) { + for (size_t k = 0; k < tensor.extent(2); k++) { + tensor(i, j, k) *= scale; + } + } + } +} + +PYBIND11_MODULE(py_fast_pauli, m) { + m.doc() = "Example NumPy/C++ Interface Using std::mdspan"; // optional module + // docstring + m.def("scale_tensor_3d", &scale_tensor_3d, "Scale a 3D tensor by a scalar.", + py::arg().noconvert(), py::arg("scale")); + + py::class_>(m, "SummedPauliOp") + .def(py::init<>()); +} \ No newline at end of file From 27d32553a21e595d2692eac3ec3f78b2df5b1e32 Mon Sep 17 00:00:00 2001 From: jamesETsmith Date: Fri, 28 Jun 2024 17:07:00 -0400 Subject: [PATCH 2/4] [feature/feature_buildout] Problems with GCC builds --- include/__factory.hpp | 7 +++++-- include/__pauli.hpp | 4 ++-- include/__pauli_string.hpp | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/include/__factory.hpp b/include/__factory.hpp index 7e7469f..5de38e1 100644 --- a/include/__factory.hpp +++ b/include/__factory.hpp @@ -1,6 +1,7 @@ #ifndef __FAST_PAULI_FACTORY_HPP #define __FAST_PAULI_FACTORY_HPP +#include #include #include #include @@ -85,11 +86,13 @@ auto rand(std::vector &blob, std::array extents) { if constexpr (is_complex::value) { std::uniform_real_distribution dis(0, 1.0); - std::ranges::generate(blob, [&]() { return T{dis(gen), dis(gen)}; }); + std::generate(blob.begin(), blob.end(), [&]() { + return T{dis(gen), dis(gen)}; + }); } else { std::uniform_real_distribution dis(0, 1.0); - std::ranges::generate(blob, [&]() { return T{dis(gen)}; }); + std::generate(blob.begin(), blob.end(), [&]() { return T{dis(gen)}; }); } return std::mdspan>(blob.data(), extents); diff --git a/include/__pauli.hpp b/include/__pauli.hpp index 1c01443..0dad92d 100644 --- a/include/__pauli.hpp +++ b/include/__pauli.hpp @@ -161,7 +161,7 @@ struct Pauli { // Adding specialization to the fmt library so we can easily print Pauli namespace fmt { -template <> struct fmt::formatter { +template <> struct formatter { constexpr auto parse(format_parse_context &ctx) { return ctx.begin(); } template @@ -190,7 +190,7 @@ template <> struct fmt::formatter { // Add complex numbers because they aren't in there already, TODO __pauli.hpp // may not be the best place for these -template <> struct fmt::formatter> { +template <> struct formatter> { constexpr auto parse(format_parse_context &ctx) { return ctx.begin(); } template diff --git a/include/__pauli_string.hpp b/include/__pauli_string.hpp index 9060b4d..1e63934 100644 --- a/include/__pauli_string.hpp +++ b/include/__pauli_string.hpp @@ -452,7 +452,7 @@ std::vector calculate_pauli_strings_max_weight(size_t n_qubits, namespace fmt { // -template <> struct fmt::formatter { +template <> struct formatter { constexpr auto parse(format_parse_context &ctx) { return ctx.begin(); } template From 908b3ae3a40cd68768421f8614a5d0b88d4c3294 Mon Sep 17 00:00:00 2001 From: Eugene Rublenko <16805621+stand-by@users.noreply.github.com> Date: Tue, 9 Jul 2024 17:28:04 -0400 Subject: [PATCH 3/4] Add several new expressions to planning doc --- docs/planning.md | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/docs/planning.md b/docs/planning.md index 246cc42..f6273c4 100644 --- a/docs/planning.md +++ b/docs/planning.md @@ -5,18 +5,26 @@ - Pauli Matrix $\sigma_i \in \{ I,X,Y,Z \}$ - Pauli String $\mathcal{\hat{P}} = \bigotimes_i \sigma_i$ +- State vector ${\ket{\psi}}$ and a set of ${n}$ state vectors $\ket{\psi_t}$ represented as columns in matrix - Sum of weighted Pauli strings (currently called `PauliOp`) $A_k = \sum_i h_i \mathcal{\hat{P_i}}$ - Sum of summed weighted Pauli strings (currently called `SummedPauliOp`) $B = \sum_k \sum_i h_{ik} \mathcal{\hat{P_i}}$ + # List of Operations Here's a terse list of the type of operations we want to support in `fast_pauli` (this list will grow over time): -1. Pauli String to sparse matrix (Pauli Composer) -2. $\mathcal{\hat{P}} \ket{\psi}$ -3. $\mathcal{\hat{P}} \ket{\psi_t}$ -4. $\big( \sum_i h_i \mathcal{\hat{P}}_i \big) \ket{\psi_t}$ -5. $\big(\sum_k \sum_i h_{ik} \mathcal{\hat{P}}_i \big) \ket{\psi_t}$ -6. $\big(\sum_k x_{tk} \sum_i h_{ik} \mathcal{\hat{P}}_i \big) \ket{\psi_t}$ -7. $\bigg(\sum_k \big( \sum_i h_{ik} \mathcal{\hat{P}}_i \big)^2 \bigg) \ket{\psi_t}$ -8. Calculate $\bra{\psi_t} \{ \mathcal{\hat{P_i}}, \hat{A_k} \} \ket{\psi}$ and $\bra{\psi} \mathcal{\hat{P_i}} \ket{\psi}$ +1. Pauli String to sparse matrix (Pauli Composer) +2. $\mathcal{\hat{P}} \ket{\psi}$ +3. $\mathcal{\hat{P}} \ket{\psi_t}$ +4. $\bra{\psi_t} \mathcal{\hat{P_i}} \ket{\psi_t}$ +5. $\bra{\psi_t} \mathcal{x_{ti}\hat{P_i}} \ket{\psi_t}$ +6. $\big( \sum_i h_i \mathcal{\hat{P}}_i \big) \ket{\psi_t}$ +7. $\big(\sum_k \sum_i h_{ik} \mathcal{\hat{P}}_i \big) \ket{\psi_t}$ +8. $\big(\sum_k x_{tk} \sum_i h_{ik} \mathcal{\hat{P}}_i \big) \ket{\psi_t}$ +9. $\big(\sum_k ( \sum_i h_{ik} \mathcal{\hat{P}}_i )^2 \big) \ket{\psi_t}$ +10. $\bra{\psi_t} \{ \mathcal{\hat{P_i}}, \hat{A_k} \} \ket{\psi_t}$ +11. $\bra{\psi_t} ( \sum_i h_{ik} \mathcal{\hat{P}}_i ) \ket{\psi_t}$ +12. $\bra{\psi_t} \big(\sum_k x_{tk} \sum_i h_{ik} \mathcal{\hat{P}}_i \big) \ket{\psi_t}$ +13. $\bra{\psi_t} ( \sum_i h_{ik} \mathcal{\hat{P}}_i )^2 \ket{\psi_t}$ +14. $\bra{\psi_t} \big(\sum_k ( \sum_i h_{ik} \mathcal{\hat{P}}_i )^2 \big) \ket{\psi_t}$ From c97592a8073f71cab1cfee0c7d5ba48fdbd9121f Mon Sep 17 00:00:00 2001 From: jamesETsmith Date: Wed, 10 Jul 2024 08:48:24 -0400 Subject: [PATCH 4/4] [feature/feature_buildout] Hopefully cleaning up some CI problems --- .github/workflows/all_push.yml | 1 + CMakeLists.txt | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/all_push.yml b/.github/workflows/all_push.yml index a11f94f..318c0f3 100644 --- a/.github/workflows/all_push.yml +++ b/.github/workflows/all_push.yml @@ -6,6 +6,7 @@ jobs: build: runs-on: ${{ matrix.os }} strategy: + fail-fast: false matrix: os: [ubuntu-latest] compiler: [g++-12, clang++-17] diff --git a/CMakeLists.txt b/CMakeLists.txt index 20652c1..f51c6bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,9 +62,8 @@ target_compile_options( -Werror # -stdlib=libc++ ${FAST_PAULI_EXTRA_CXX_COMPILE_FLAGS}) -target_link_options(fast_pauli INTERFACE ${FAST_PAULI_EXTRA_CXX_LD_FLAGS} - -fuse-ld=mold) -# target_compile_definitions(fast_pauli INTERFACE) +# target_link_options(fast_pauli INTERFACE ${FAST_PAULI_EXTRA_CXX_LD_FLAGS} +# -fuse-ld=mold) target_compile_definitions(fast_pauli INTERFACE) # Testing include(CTest)