diff --git a/runtime/cudaq/spin/spin_op.cpp b/runtime/cudaq/spin/spin_op.cpp index d76ce98f5a6..5a0aaa0592c 100644 --- a/runtime/cudaq/spin/spin_op.cpp +++ b/runtime/cudaq/spin/spin_op.cpp @@ -63,7 +63,7 @@ actionOnBra(spin_op &term, const std::string &bitConfiguration) { } std::pair, std::complex> -mult(const std::vector& p1, const std::vector& p2, +mult(const std::vector &p1, const std::vector &p2, const std::complex &p1Coeff, const std::complex &p2Coeff) { auto [minSize, maxSize] = std::minmax({p1.size(), p2.size()}); std::size_t minNumQubits = minSize / 2; @@ -82,18 +82,19 @@ mult(const std::vector& p1, const std::vector& p2, result[i] = p1_x ^ p2_x; result[i + maxNumQubits] = p1_z ^ p2_z; - yCount += (p1_x & p1_z) + (p2_x & p2_z) - (result[i] & result[i + maxNumQubits]); + yCount += + (p1_x & p1_z) + (p2_x & p2_z) - (result[i] & result[i + maxNumQubits]); cPhase += p1_x & p2_z; } - const std::vector& big = p1.size() < p2.size() ? p2 : p1; + const std::vector &big = p1.size() < p2.size() ? p2 : p1; for (std::size_t i = minNumQubits; i < maxNumQubits; ++i) { result[i] = big[i]; result[i + maxNumQubits] = big[i + maxNumQubits]; } // Normalize the phase to a value in the range [0, 3] - int phaseFactor = (2*cPhase + yCount) % 4; + int phaseFactor = (2 * cPhase + yCount) % 4; if (phaseFactor < 0) phaseFactor += 4; @@ -435,7 +436,8 @@ spin_op &spin_op::operator*=(const spin_op &v) noexcept { // Take the `unordered_map` iterators to minimize pointer chasing when doing // the cartesian product of the terms of these spin operators. - using Iter = std::unordered_map>::const_iterator; + using Iter = + std::unordered_map>::const_iterator; std::vector thisTermIt; std::vector otherTermIt; thisTermIt.reserve(terms.size()); @@ -463,7 +465,7 @@ spin_op &spin_op::operator*=(const spin_op &v) noexcept { terms.clear(); terms.reserve(numTerms); - for (auto&& [term, coeff] : result) { + for (auto &&[term, coeff] : result) { auto [it, created] = terms.emplace(term, coeff); if (!created) it->second += coeff; diff --git a/unittests/spin_op/SpinOpTester.cpp b/unittests/spin_op/SpinOpTester.cpp index 945934a6aa8..51b24e3942b 100644 --- a/unittests/spin_op/SpinOpTester.cpp +++ b/unittests/spin_op/SpinOpTester.cpp @@ -11,26 +11,32 @@ #include "cudaq/spin_op.h" enum Pauli : int8_t { I = 0, X, Y, Z }; -constexpr Pauli paulis[4] = {Pauli::I, Pauli::X, Pauli::Y, Pauli::Z }; +constexpr Pauli paulis[4] = {Pauli::I, Pauli::X, Pauli::Y, Pauli::Z}; // Function to multiply two single-qubit Pauli operators -static std::pair, Pauli> multiply_paulis(Pauli a, Pauli b) { +static std::pair, Pauli> multiply_paulis(Pauli a, + Pauli b) { using namespace std::complex_literals; - // I X Y Z - constexpr std::complex table[4][4] = {{ 1., 1., 1, 1}, // I - { 1., 1., 1i, -1i}, // X - { 1., -1i, 1, 1i}, // Y - { 1., 1i, -1i, 1} // Z + // I X Y Z + constexpr std::complex table[4][4] = { + {1., 1., 1, 1}, // I + {1., 1., 1i, -1i}, // X + {1., -1i, 1, 1i}, // Y + {1., 1i, -1i, 1} // Z }; - if (a == b) return {1.0, Pauli::I}; - if (a == Pauli::I) return {1.0, b}; - if (b == Pauli::I) return {1.0, a}; + if (a == b) + return {1.0, Pauli::I}; + if (a == Pauli::I) + return {1.0, b}; + if (b == Pauli::I) + return {1.0, a}; return {table[a][b], paulis[a ^ b]}; } // Function to multiply two multi-qubit Pauli words static std::pair, std::vector> -multiply_pauli_words(const std::vector& a, const std::vector& b, bool verbose = false) { +multiply_pauli_words(const std::vector &a, const std::vector &b, + bool verbose = false) { std::complex phase = 1.0; std::string info; std::vector result(a.size(), Pauli::I); @@ -54,32 +60,33 @@ static std::vector generate_pauli_word(int64_t id, int64_t num_qubits) { return word; } -static std::string generate_pauli_string(const std::vector& word) { - constexpr char paulis_name[4] = {'I', 'X', 'Y', 'Z' }; +static std::string generate_pauli_string(const std::vector &word) { + constexpr char paulis_name[4] = {'I', 'X', 'Y', 'Z'}; std::string result(word.size(), 'I'); for (int64_t i = 0; i < word.size(); ++i) result[i] = paulis_name[word[i]]; return result; } -static cudaq::spin_op generate_cudaq_spin(int64_t id, int64_t num_qubits, bool addI = true) { +static cudaq::spin_op generate_cudaq_spin(int64_t id, int64_t num_qubits, + bool addI = true) { constexpr int64_t mask = 0x3; cudaq::spin_op result; for (int64_t i = 0; i < num_qubits; ++i) { switch (paulis[id & mask]) { - case Pauli::I: - if (addI) - result *= cudaq::spin::i(i); - break; - case Pauli::X: - result *= cudaq::spin::x(i); - break; - case Pauli::Y: - result *= cudaq::spin::y(i); - break; - case Pauli::Z: - result *= cudaq::spin::z(i); - break; + case Pauli::I: + if (addI) + result *= cudaq::spin::i(i); + break; + case Pauli::X: + result *= cudaq::spin::x(i); + break; + case Pauli::Y: + result *= cudaq::spin::y(i); + break; + case Pauli::Z: + result *= cudaq::spin::z(i); + break; } id >>= 2; }