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

Added QAOA tests and test with aer


Signed-off-by: Nguyen, Thien's avatarThien Nguyen <nguyentm@ornl.gov>
parent 8e466c76
......@@ -1005,7 +1005,7 @@ double PauliOperator::calcExpValFromGroupedExecution(
temp_buffer->setMeasurements(resultBuffer->getMarginalCounts(meas_bits, bit_order));
const auto coeff = spinInst.coeff();
const double term_exp_val = temp_buffer->getExpectationValueZ();
temp_buffer->print();
// temp_buffer->print();
// std::cout << "Exp = " << term_exp_val << "\n";
// std::cout << "Coeff = " << coeff << "\n";
energy += (term_exp_val * coeff);
......@@ -1021,7 +1021,7 @@ double PauliOperator::postProcess(std::shared_ptr<AcceleratorBuffer> buffer,
buffer->getChildren()[0]->name().find("GroupObserve") !=
std::string::npos &&
!buffer->getChildren()[0]->getMeasurementCounts().empty()) {
std::cout << "Grouping post processing!\n";
// std::cout << "Grouping post processing!\n";
return calcExpValFromGroupedExecution(buffer);
}
......
......@@ -513,23 +513,51 @@ TEST(PauliOperatorTester, checkGroupingQaoa) {
EXPECT_EQ(observed_non_commute.size(), 3);
}
TEST(PauliOperatorTester, checkGroupingQaoaPostProcess) {
TEST(PauliOperatorTester, checkGroupingQaoaPostProcessLSB) {
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";
xacc::Observable *obs = &op;
auto qpu = xacc::getAccelerator("qpp", {{"shots", 1024}});
auto gateRegistry = xacc::getService<xacc::IRProvider>("quantum");
auto f = gateRegistry->createComposite("f");
auto h0 = gateRegistry->createInstruction("H", 0);
auto h1 = gateRegistry->createInstruction("H", 1);
auto h2 = gateRegistry->createInstruction("H", 2);
f->addInstructions({h0, h1, h2});
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);
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);
qpu->execute(buffer, observed);
buffer->print();
auto exp_val = op.postProcess(buffer, xacc::Observable::PostProcessingTask::EXP_VAL_CALC, {});
std::cout << "Exp-val = " << exp_val << "\n";
auto exp_val = op.postProcess(
buffer, xacc::Observable::PostProcessingTask::EXP_VAL_CALC, {});
EXPECT_NEAR(exp_val, 2.0, 0.1);
}
int main(int argc, char **argv) {
......
......@@ -48,7 +48,10 @@ std::string hex_string_to_binary_string(std::string hex) {
}
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) {
......
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