Skip to content

Commit

Permalink
# This is a combination of 9 commits.
Browse files Browse the repository at this point in the history
# This is the 1st commit message:

[feature/CI_setup] more CI tinkering

# This is the commit message #2:

[feature/CI_setup] more CI tinkering

# This is the commit message #3:

[feature/CI_setup] more CI tinkering

# This is the commit message #4:

[feature/CI_setup] more CI tinkering

# This is the commit message #5:

[feature/CI_setup] more CI tinkering

# This is the commit message #6:

[feature/CI_setup] more CI tinkering

# This is the commit message #7:

[feature/CI_setup] more CI tinkering

# This is the commit message #8:

[feature/CI_setup] more CI tinkering

# This is the commit message #9:

[feature/CI_setup] more CI tinkering
  • Loading branch information
jamesETsmith committed Jun 11, 2024
1 parent 0f2ae67 commit 0bd8b84
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 31 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/all_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ jobs:

- name: Build
# Build your program with the given configuration
run: cmake --build ${{github.workspace}}/build --verbose --parallel
run: |
cmake --build ${{github.workspace}}/build --verbose --parallel
du build/tests/test_pauli_op
- name: Test C++
env:
Expand Down
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,14 @@ target_compile_options(
-Wall
-Wextra
-Werror
<<<<<<< HEAD
-Wpedantic
# -stdlib=libc++
)
=======
# -fanalyzer -stdlib=libc++
${FAST_PAULI_EXTRA_CXX_COMPILE_FLAGS})
>>>>>>> a607dfe ([feature/CI_setup] more CI tinkering)
# target_link_options(fast_pauli INTERFACE ${FAST_PAULI_EXTRA_CXX_LD_FLAGS}
# -fuse-ld=mold) target_compile_definitions(fast_pauli INTERFACE)

Expand Down
20 changes: 10 additions & 10 deletions include/__pauli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@ struct Pauli {
* @brief Default constructor, initializes to I.
*
*/
constexpr Pauli() : code(0) {}
Pauli() : code(0) {}

/**
* @brief Constructor given a numeric code. TODO add input checking
*
* @tparam T Any type convertible to uint8_t
* @param code 0: I, 1: X, 2: Y, 3: Z
* @return requires constexpr
* @return requires
*/
template <class T>
requires std::convertible_to<T, uint8_t>
constexpr Pauli(T const code) : code(code) {}
Pauli(T const code) : code(code) {}

// Copy ctor
constexpr Pauli(Pauli const &other) = default;
Pauli(Pauli const &other) = default;

// Copy assignment
constexpr Pauli &operator=(Pauli const &other) noexcept = default;
Pauli &operator=(Pauli const &other) noexcept = default;

// The default operator does everything we want and make this more composable
friend auto operator<=>(Pauli const &, Pauli const &) = default;
Expand All @@ -48,10 +48,10 @@ struct Pauli {
*
* @param lhs
* @param rhs
* @return constexpr std::pair<std::complex<double>, Pauli>
* @return std::pair<std::complex<double>, Pauli>
*/
friend constexpr std::pair<std::complex<double>, Pauli>
operator*(Pauli lhs, Pauli const &rhs) {
friend std::pair<std::complex<double>, Pauli> operator*(Pauli lhs,
Pauli const &rhs) {
switch (lhs.code) {
case 0:
switch (rhs.code) {
Expand Down Expand Up @@ -130,10 +130,10 @@ struct Pauli {
* @brief Returns the pauli matrix as a 2D vector of complex numbers.
*
* @tparam T floating point type
* @return constexpr std::vector<std::vector<std::complex<T>>>
* @return std::vector<std::vector<std::complex<T>>>
*/
template <std::floating_point T>
constexpr std::vector<std::vector<std::complex<T>>> to_tensor() const {
std::vector<std::vector<std::complex<T>>> to_tensor() const {
std::vector<std::vector<std::complex<T>>> result;
switch (code) {
case 0:
Expand Down
39 changes: 22 additions & 17 deletions include/__pauli_op.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,25 +147,30 @@ template <std::floating_point T> struct PauliOp {
std::mdspan<std::complex<T>, std::dextents<size_t, 3>> new_states_thr(
new_states_thr_raw.data(), n_threads, n_dim, n_data);

//
#pragma omp parallel for schedule(static)
for (size_t i = 0; i < pauli_strings.size(); ++i) {
size_t const tid = omp_get_thread_num();
//

PauliString const &ps = pauli_strings[i];
std::complex<T> c = coeffs[i];
std::mdspan<std::complex<T>, std::dextents<size_t, 2>> new_states_local =
std::submdspan(new_states_thr, tid, std::full_extent,
std::full_extent);
ps.apply_batch(new_states_local, states, c);
}
#pragma omp parallel
{
#pragma omp for schedule(static)
for (size_t i = 0; i < pauli_strings.size(); ++i) {
size_t const tid = omp_get_thread_num();
fmt::println("thread {} index {}", tid, i);

PauliString const &ps = pauli_strings[i];
std::complex<T> c = coeffs[i];
std::mdspan<std::complex<T>, std::dextents<size_t, 2>>
new_states_local = std::submdspan(
new_states_thr, tid, std::full_extent, std::full_extent);
ps.apply_batch(new_states_local, states, c);
}

// Do the reduction and tranpose back
#pragma omp parallel for schedule(static) collapse(2)
for (size_t i = 0; i < new_states.extent(0); ++i) {
for (size_t t = 0; t < new_states.extent(1); ++t) {
for (size_t th = 0; th < n_threads; ++th) {
new_states(i, t) += new_states_thr(th, i, t);
// Do the reduction and tranpose back
#pragma omp for schedule(static) collapse(2)
for (size_t i = 0; i < new_states.extent(0); ++i) {
for (size_t t = 0; t < new_states.extent(1); ++t) {
for (size_t th = 0; th < n_threads; ++th) {
new_states(i, t) += new_states_thr(th, i, t);
}
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions tests/test_pauli_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,18 @@ TEST_CASE("test apply multistate multistring") {
}

TEST_CASE("test apply multistate multistring identity") {
fmt::println("test apply multistate multistring identity");

// Set up PauliOp
std::vector<PauliString> pauli_strings(10, "IIII");
std::vector<PauliString> pauli_strings(16, "IIII");
std::vector<std::complex<double>> coeffs(pauli_strings.size(),
1. / pauli_strings.size());
PauliOp<double> pauli_op(coeffs, pauli_strings);

size_t const dims = pauli_strings[0].dims();

// Set up random states
size_t const n_states = 10;
std::vector<std::complex<double>> states_raw(dims * n_states, 1);
std::vector<std::complex<double>> states_raw;
std::mdspan states =
fast_pauli::rand<std::complex<double>, 2>(states_raw, {dims, n_states});

Expand All @@ -207,6 +208,11 @@ TEST_CASE("test apply multistate multistring identity") {
for (size_t t = 0; t < n_states; ++t) {
for (size_t i = 0; i < dims; ++i) {
CHECK(abs(new_states(i, t) - states(i, t)) < 1e-6);
if (abs(new_states(i, t) - states(i, t)) > 1e-6) {
fmt::print("new_states(i, t): {}, states(i, t): {}", new_states(i, t),
states(i, t));
fmt::println(" ratio {}", new_states(i, t) / states(i, t));
}
}
}
}

0 comments on commit 0bd8b84

Please sign in to comment.