Commit c94a5dc0 authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Added QAOA tests and test with aer


Signed-off-by: Nguyen, Thien Minh's avatarThien Nguyen <nguyentm@ornl.gov>
parent 8e466c76
...@@ -1005,7 +1005,7 @@ double PauliOperator::calcExpValFromGroupedExecution( ...@@ -1005,7 +1005,7 @@ double PauliOperator::calcExpValFromGroupedExecution(
temp_buffer->setMeasurements(resultBuffer->getMarginalCounts(meas_bits, bit_order)); temp_buffer->setMeasurements(resultBuffer->getMarginalCounts(meas_bits, bit_order));
const auto coeff = spinInst.coeff(); const auto coeff = spinInst.coeff();
const double term_exp_val = temp_buffer->getExpectationValueZ(); const double term_exp_val = temp_buffer->getExpectationValueZ();
temp_buffer->print(); // temp_buffer->print();
// std::cout << "Exp = " << term_exp_val << "\n"; // std::cout << "Exp = " << term_exp_val << "\n";
// std::cout << "Coeff = " << coeff << "\n"; // std::cout << "Coeff = " << coeff << "\n";
energy += (term_exp_val * coeff); energy += (term_exp_val * coeff);
...@@ -1021,7 +1021,7 @@ double PauliOperator::postProcess(std::shared_ptr<AcceleratorBuffer> buffer, ...@@ -1021,7 +1021,7 @@ double PauliOperator::postProcess(std::shared_ptr<AcceleratorBuffer> buffer,
buffer->getChildren()[0]->name().find("GroupObserve") != buffer->getChildren()[0]->name().find("GroupObserve") !=
std::string::npos && std::string::npos &&
!buffer->getChildren()[0]->getMeasurementCounts().empty()) { !buffer->getChildren()[0]->getMeasurementCounts().empty()) {
std::cout << "Grouping post processing!\n"; // std::cout << "Grouping post processing!\n";
return calcExpValFromGroupedExecution(buffer); return calcExpValFromGroupedExecution(buffer);
} }
......
...@@ -513,23 +513,51 @@ TEST(PauliOperatorTester, checkGroupingQaoa) { ...@@ -513,23 +513,51 @@ TEST(PauliOperatorTester, checkGroupingQaoa) {
EXPECT_EQ(observed_non_commute.size(), 3); EXPECT_EQ(observed_non_commute.size(), 3);
} }
TEST(PauliOperatorTester, checkGroupingQaoaPostProcess) { TEST(PauliOperatorTester, checkGroupingQaoaPostProcessLSB) {
PauliOperator op; PauliOperator op;
op.fromString("(0, -1) Z0 Z1 + (0, -1) Z1 Z2 + (0, -1) Z2 Z0"); op.fromString("(1.5,0) + (-0.5,0) Z0 Z1 + (-0.5,0) Z0 Z2 + (-0.5,0) Z1 Z2");
std::cout << op.toString() << "\n"; std::cout << op.toString() << "\n";
xacc::Observable *obs = &op;
auto qpu = xacc::getAccelerator("qpp", {{"shots", 1024}}); auto qpu = xacc::getAccelerator("qpp", {{"shots", 1024}});
auto gateRegistry = xacc::getService<xacc::IRProvider>("quantum"); auto qaoa_ansatz =
auto f = gateRegistry->createComposite("f"); xacc::createComposite("qaoa", {{"nbQubits", 3},
auto h0 = gateRegistry->createInstruction("H", 0); {"nbSteps", 1},
auto h1 = gateRegistry->createInstruction("H", 1); {"cost-ham", obs},
auto h2 = gateRegistry->createInstruction("H", 2); {"parameter-scheme", "Standard"}});
f->addInstructions({h0, h1, h2}); const std::vector<double> opt_params{0.308, 0.614205};
auto f = (*qaoa_ansatz)(opt_params);
auto observed = op.observe(f, {{"accelerator", qpu}}); auto observed = op.observe(f, {{"accelerator", qpu}});
EXPECT_EQ(observed.size(), 1);
auto buffer = xacc::qalloc(3);
qpu->execute(buffer, observed);
buffer->print();
auto exp_val = op.postProcess(
buffer, xacc::Observable::PostProcessingTask::EXP_VAL_CALC, {});
EXPECT_NEAR(exp_val, 2.0, 0.1);
}
TEST(PauliOperatorTester, checkGroupingQaoaPostProcessMSB) {
PauliOperator op;
op.fromString("(1.5,0) + (-0.5,0) Z0 Z1 + (-0.5,0) Z0 Z2 + (-0.5,0) Z1 Z2");
std::cout << op.toString() << "\n";
xacc::Observable *obs = &op;
// Aer => MSB
auto qpu = xacc::getAccelerator("aer", {{"shots", 1024}});
auto qaoa_ansatz =
xacc::createComposite("qaoa", {{"nbQubits", 3},
{"nbSteps", 1},
{"cost-ham", obs},
{"parameter-scheme", "Standard"}});
const std::vector<double> opt_params{0.308, 0.614205};
auto f = (*qaoa_ansatz)(opt_params);
auto observed = op.observe(f, {{"accelerator", qpu}});
EXPECT_EQ(observed.size(), 1);
auto buffer = xacc::qalloc(3); auto buffer = xacc::qalloc(3);
qpu->execute(buffer, observed); qpu->execute(buffer, observed);
buffer->print(); buffer->print();
auto exp_val = op.postProcess(buffer, xacc::Observable::PostProcessingTask::EXP_VAL_CALC, {}); auto exp_val = op.postProcess(
std::cout << "Exp-val = " << exp_val << "\n"; buffer, xacc::Observable::PostProcessingTask::EXP_VAL_CALC, {});
EXPECT_NEAR(exp_val, 2.0, 0.1);
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
......
...@@ -48,7 +48,10 @@ std::string hex_string_to_binary_string(std::string hex) { ...@@ -48,7 +48,10 @@ std::string hex_string_to_binary_string(std::string hex) {
} }
HeterogeneousMap AerAccelerator::getProperties() { HeterogeneousMap AerAccelerator::getProperties() {
return physical_backend_properties; auto props = physical_backend_properties;
// Insert 'shots' data
props.insert("shots", m_shots);
return props;
} }
void AerAccelerator::initialize(const HeterogeneousMap &params) { void AerAccelerator::initialize(const HeterogeneousMap &params) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment