Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
boschmitt committed Oct 17, 2024
1 parent e44d133 commit 6a28bee
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
14 changes: 8 additions & 6 deletions runtime/cudaq/spin/spin_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ actionOnBra(spin_op &term, const std::string &bitConfiguration) {
}

std::pair<std::vector<bool>, std::complex<double>>
mult(const std::vector<bool>& p1, const std::vector<bool>& p2,
mult(const std::vector<bool> &p1, const std::vector<bool> &p2,
const std::complex<double> &p1Coeff, const std::complex<double> &p2Coeff) {
auto [minSize, maxSize] = std::minmax({p1.size(), p2.size()});
std::size_t minNumQubits = minSize / 2;
Expand All @@ -82,18 +82,19 @@ mult(const std::vector<bool>& p1, const std::vector<bool>& 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<bool>& big = p1.size() < p2.size() ? p2 : p1;
const std::vector<bool> &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;

Expand Down Expand Up @@ -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<spin_op_term, std::complex<double>>::const_iterator;
using Iter =
std::unordered_map<spin_op_term, std::complex<double>>::const_iterator;
std::vector<Iter> thisTermIt;
std::vector<Iter> otherTermIt;
thisTermIt.reserve(terms.size());
Expand Down Expand Up @@ -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;
Expand Down
61 changes: 34 additions & 27 deletions unittests/spin_op/SpinOpTester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::complex<double>, Pauli> multiply_paulis(Pauli a, Pauli b) {
static std::pair<std::complex<double>, Pauli> multiply_paulis(Pauli a,
Pauli b) {
using namespace std::complex_literals;
// I X Y Z
constexpr std::complex<double> 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<double> 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::complex<double>, std::vector<Pauli>>
multiply_pauli_words(const std::vector<Pauli>& a, const std::vector<Pauli>& b, bool verbose = false) {
multiply_pauli_words(const std::vector<Pauli> &a, const std::vector<Pauli> &b,
bool verbose = false) {
std::complex<double> phase = 1.0;
std::string info;
std::vector<Pauli> result(a.size(), Pauli::I);
Expand All @@ -54,32 +60,33 @@ static std::vector<Pauli> generate_pauli_word(int64_t id, int64_t num_qubits) {
return word;
}

static std::string generate_pauli_string(const std::vector<Pauli>& word) {
constexpr char paulis_name[4] = {'I', 'X', 'Y', 'Z' };
static std::string generate_pauli_string(const std::vector<Pauli> &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;
}
Expand Down

0 comments on commit 6a28bee

Please sign in to comment.