Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove use of get_raw_data and for_each_term #114

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions libs/qec/lib/stabilizer_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ to_parity_matrix(const std::vector<cudaq::spin_op> &stabilizers,
p_stabilizers = &stabilizers_copy;
}

auto isZ = [](const cudaq::spin_op &op, std::size_t idx) {
return op.to_string(false)[idx] == 'Z';
};
auto isX = [](const cudaq::spin_op &op, std::size_t idx) {
return op.to_string(false)[idx] == 'X';
};

if (type == stabilizer_type::XZ) {
auto numQubits = (*p_stabilizers)[0].num_qubits();
cudaqx::tensor<uint8_t> t({p_stabilizers->size(), 2 * numQubits});
Expand All @@ -76,7 +83,7 @@ to_parity_matrix(const std::vector<cudaq::spin_op> &stabilizers,
// Need to shift Z bits left
for (std::size_t row = 0; row < numZRows; row++) {
for (std::size_t i = numQubits; i < 2 * numQubits; i++) {
if ((*p_stabilizers)[row].get_raw_data().first[0][i])
if (isZ((*p_stabilizers)[row], i - numQubits))
t.at({row, i - numQubits}) = 1;
}
}
Expand All @@ -85,7 +92,7 @@ to_parity_matrix(const std::vector<cudaq::spin_op> &stabilizers,

for (std::size_t row = 0; row < numXRows; row++) {
for (std::size_t i = 0; i < numQubits; i++) {
if ((*p_stabilizers)[numZRows + row].get_raw_data().first[0][i])
if (isX((*p_stabilizers)[numZRows + row], i))
t.at({numZRows + row, i + numQubits}) = 1;
}
}
Expand All @@ -109,7 +116,7 @@ to_parity_matrix(const std::vector<cudaq::spin_op> &stabilizers,
cudaqx::tensor<uint8_t> ret({numZRows, numQubits});
for (std::size_t row = 0; row < numZRows; row++) {
for (std::size_t i = numQubits; i < 2 * numQubits; i++) {
if ((*p_stabilizers)[row].get_raw_data().first[0][i])
if (isZ((*p_stabilizers)[row], i - numQubits))
ret.at({row, i - numQubits}) = 1;
}
}
Expand All @@ -134,7 +141,7 @@ to_parity_matrix(const std::vector<cudaq::spin_op> &stabilizers,
cudaqx::tensor<uint8_t> ret({numXRows, numQubits});
for (std::size_t row = 0; row < numXRows; row++) {
for (std::size_t i = 0; i < numQubits; i++) {
if ((*p_stabilizers)[numZRows + row].get_raw_data().first[0][i])
if (isX((*p_stabilizers)[numZRows + row], i))
ret.at({row, i}) = 1;
}
}
Expand Down
48 changes: 3 additions & 45 deletions libs/solvers/lib/operators/operator_pools/spin_complement_gsd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,7 @@ spin_complement_gsd::generate(const heterogeneous_map &config) const {
adag(numQubits, p) * a(numQubits, q);
oneElectron += adag(numQubits, q + 1) * a(numQubits, p + 1) -
adag(numQubits, p + 1) * a(numQubits, q + 1);

std::unordered_map<cudaq::spin_op::spin_op_term, std::complex<double>>
terms;
oneElectron.for_each_term([&](cudaq::spin_op &term) {
auto coeff = term.get_coefficient();
if (std::fabs(coeff.real()) < 1e-12 && std::fabs(coeff.imag()) < 1e-12)
return;

if (std::fabs(coeff.real()) < 1e-12)
terms.insert({term.get_raw_data().first[0],
std::complex<double>{0., coeff.imag()}});
});

if (!terms.empty())
pool.emplace_back(terms);
pool.push_back(oneElectron);
}
}

Expand All @@ -88,22 +74,7 @@ spin_complement_gsd::generate(const heterogeneous_map &config) const {
adag(numQubits, s + 1) * a(numQubits, q + 1) -
adag(numQubits, q + 1) * a(numQubits, s + 1) *
adag(numQubits, p + 1) * a(numQubits, r + 1);

std::unordered_map<cudaq::spin_op::spin_op_term, std::complex<double>>
terms;
twoElectron.for_each_term([&](cudaq::spin_op &term) {
auto coeff = term.get_coefficient();
if (std::fabs(coeff.real()) < 1e-12 &&
std::fabs(coeff.imag()) < 1e-12)
return;

if (std::fabs(coeff.real()) < 1e-12)
terms.insert({term.get_raw_data().first[0],
std::complex<double>{0., coeff.imag()}});
});

if (!terms.empty())
pool.push_back(terms);
pool.push_back(twoElectron);
rs++;
}
}
Expand Down Expand Up @@ -133,20 +104,7 @@ spin_complement_gsd::generate(const heterogeneous_map &config) const {
adag(numQubits, r + 1) * a(numQubits, p + 1) -
adag(numQubits, p + 1) * a(numQubits, r + 1) *
adag(numQubits, q - 1) * a(numQubits, s - 1);
std::unordered_map<cudaq::spin_op::spin_op_term, std::complex<double>>
terms;
twoElectron.for_each_term([&](cudaq::spin_op &term) {
auto coeff = term.get_coefficient();
if (std::fabs(coeff.real()) < 1e-12 &&
std::fabs(coeff.imag()) < 1e-12)
return;

if (std::fabs(coeff.real()) < 1e-12)
terms.insert({term.get_raw_data().first[0],
std::complex<double>{0., coeff.imag()}});
});
if (!terms.empty())
pool.push_back(terms);
pool.push_back(twoElectron);
rs++;
}
}
Expand Down
4 changes: 2 additions & 2 deletions libs/solvers/lib/qaoa/qaoa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ std::size_t get_num_qaoa_parameters(const cudaq::spin_op &problemHamiltonian,
std::size_t expectedNumParams = 0;
if (full_parameterization) {
auto nonIdTerms = 0;
referenceHamiltonian.for_each_term([&](cudaq::spin_op &term) {
for (const auto &term : referenceHamiltonian)
if (!term.is_identity())
nonIdTerms++;
});

expectedNumParams =
numLayers * (problemHamiltonian.num_terms() + nonIdTerms);
} else {
Expand Down
9 changes: 2 additions & 7 deletions libs/solvers/python/tests/test_jordan_wigner.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,11 @@


def extract_words(hamiltonian: cudaq.SpinOperator):
result = []
hamiltonian.for_each_term(lambda term: result.append(term.to_string(False)))
return result
return [term.to_string(False) for term in hamiltonian]


def extract_coefficients(hamiltonian: cudaq.SpinOperator):
result = []
hamiltonian.for_each_term(
lambda term: result.append(term.get_coefficient()))
return result
return [term.get_coefficient() for term in hamiltonian]


def extract_spin_op_to_dict(op: cudaq.SpinOperator) -> dict:
Expand Down
29 changes: 18 additions & 11 deletions libs/solvers/unittests/test_bravyi_kitaev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@

#include "cudaq/solvers/operators/molecule/fermion_compilers/bravyi_kitaev.h"

auto get_coefficients(const cudaq::spin_op &op) {
std::vector<std::complex<double>> ret;
for (const auto &term : op)
ret.push_back(term.get_coefficient());
return ret;
}

// One- and Two-body integrals were copied from test_molecule.cpp.
// They were further validated using the script ./support/h2_pyscf_hf.py.
//
Expand Down Expand Up @@ -77,7 +84,7 @@ TEST(BravyiKitaev, testH2Hamiltonian) {
0.12020049071260128 * z(0) * z(2) * z(3) + 0.1683359862516207 * z(1) -
0.22004130022421792 * z(1) * z(2) * z(3) +
0.17407289249680227 * z(1) * z(3) - 0.22004130022421792 * z(2);
auto [terms, residuals] = (result - gold).get_raw_data();
auto residuals = get_coefficients(result - gold);
for (auto r : residuals)
EXPECT_NEAR(std::abs(r), 0.0, 1e-4);
}
Expand All @@ -91,7 +98,7 @@ TEST(BravyiKitaev, testSRLCase0) {
cudaq::spin_op gold = double_complex(-2.0, 0.0) * i(0) * i(1) * z(2) +
double_complex(2.0, 0.0) * i(0) * i(1) * i(2);

auto [terms, residuals] = (result - gold).get_raw_data();
auto residuals = get_coefficients(result - gold);
for (auto r : residuals)
EXPECT_NEAR(std::abs(r), 0.0, 1e-4);
}
Expand All @@ -109,7 +116,7 @@ TEST(BravyiKitaev, testSRLCase1) {
i(4) * z(5) * x(6) +
double_complex(0.0, -1.0) * i(0) * z(1) * y(2) * y(3) *
i(4) * z(5) * y(6);
auto [terms, residuals] = (result - gold).get_raw_data();
auto residuals = get_coefficients(result - gold);
for (auto r : residuals)
EXPECT_NEAR(std::abs(r), 0.0, 1e-4);
}
Expand All @@ -125,7 +132,7 @@ TEST(BravyiKitaev, testSRLCase2) {
double_complex(1.0, 0.0) * z(1) * x(2) * y(3) * y(5) +
double_complex(0.0, 1.0) * z(1) * y(2) * y(3) * y(5);

auto [terms, residuals] = (result - gold).get_raw_data();
auto residuals = get_coefficients(result - gold);
for (auto r : residuals)
EXPECT_NEAR(std::abs(r), 0.0, 1e-4);
}
Expand All @@ -140,7 +147,7 @@ TEST(BravyiKitaev, testSRLCase3) {
double_complex(1.0, 0.0) * i(0) * x(1) * x(2) +
double_complex(0.0, 1.0) * i(0) * x(1) * y(2);

auto [terms, residuals] = (result - gold).get_raw_data();
auto residuals = get_coefficients(result - gold);
for (auto r : residuals)
EXPECT_NEAR(std::abs(r), 0.0, 1e-4);
}
Expand All @@ -156,7 +163,7 @@ TEST(BravyiKitaev, testSRLCase4) {
double_complex(1.0, 0.0) * x(0) * x(1) * i(2) * y(3) * i(4) * y(5) +
double_complex(0.0, -1.0) * y(0) * x(1) * i(2) * y(3) * i(4) * y(5);

auto [terms, residuals] = (result - gold).get_raw_data();
auto residuals = get_coefficients(result - gold);
for (auto r : residuals)
EXPECT_NEAR(std::abs(r), 0.0, 1e-4);
}
Expand All @@ -171,7 +178,7 @@ TEST(BravyiKitaev, testSRLCase6) {
double_complex(0.0, 1.0) * z(17) * y(18) * z(19) +
double_complex(-1.0, 0.0) * z(17) * x(18) * z(19);

auto [terms, residuals] = (result - gold).get_raw_data();
auto residuals = get_coefficients(result - gold);
for (auto r : residuals)
EXPECT_NEAR(std::abs(r), 0.0, 1e-4);
}
Expand All @@ -188,7 +195,7 @@ TEST(BravyiKitaev, testSRLCase7) {
double_complex(1.0, 0.0) * z(3) * z(4) * x(5) * y(7) * y(11) +
double_complex(0.0, 1.0) * z(3) * y(5) * y(7) * y(11);

auto [terms, residuals] = (result - gold).get_raw_data();
auto residuals = get_coefficients(result - gold);
for (auto r : residuals)
EXPECT_NEAR(std::abs(r), 0.0, 1e-4);
}
Expand All @@ -205,7 +212,7 @@ TEST(BravyiKitaev, testSRLCase8) {
double_complex(1.0, 0.0) * x(7) * z(8) * x(9) * x(11) +
double_complex(0.0, 1.0) * x(7) * y(9) * x(11);

auto [terms, residuals] = (result - gold).get_raw_data();
auto residuals = get_coefficients(result - gold);
for (auto r : residuals)
EXPECT_NEAR(std::abs(r), 0.0, 1e-4);
}
Expand All @@ -221,7 +228,7 @@ TEST(BravyiKitaev, testSRLCase9) {
double_complex(-1.0, 0.0) * z(7) * z(8) * x(9) * x(11) * z(15) +
double_complex(0.0, 1.0) * z(7) * y(9) * x(11) * z(15);

auto [terms, residuals] = (result - gold).get_raw_data();
auto residuals = get_coefficients(result - gold);
for (auto r : residuals)
EXPECT_NEAR(std::abs(r), 0.0, 1e-4);
}
Expand All @@ -237,7 +244,7 @@ TEST(BravyiKitaev, testSRLCase10) {
double_complex(-1.0, 0.0) * z(1) * z(2) * x(3) * z(7) +
double_complex(0.0, 1.0) * y(3) * z(7);

auto [terms, residuals] = (result - gold).get_raw_data();
auto residuals = get_coefficients(result - gold);
for (auto r : residuals)
EXPECT_NEAR(std::abs(r), 0.0, 1e-4);
}