QITETester.cpp 6.14 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*******************************************************************************
 * Copyright (c) 2019 UT-Battelle, LLC.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompanies this
 * distribution. The Eclipse Public License is available at
 * http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution
 *License is available at https://eclipse.org/org/documents/edl-v10.php
 *
 * Contributors:
 *  Thien Nguyen - initial API and implementation
 *******************************************************************************/
#include <gtest/gtest.h>

#include "xacc.hpp"
#include "xacc_service.hpp"
#include "Optimizer.hpp"
#include "Observable.hpp"
#include "Algorithm.hpp"
#include "PauliOperator.hpp"

TEST(QITETester, checkSimple) 
{
Nguyen, Thien Minh's avatar
Nguyen, Thien Minh committed
24
  auto qite = xacc::getService<xacc::Algorithm>("qite");
25
  std::shared_ptr<xacc::Observable> observable = std::make_shared<xacc::quantum::PauliOperator>();
26
  observable->fromString("0.7071067811865475 X0 + 0.7071067811865475 Z0");
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  auto acc = xacc::getAccelerator("qpp");
  const int nbSteps = 25;
  const double stepSize = 0.1;

  const bool initOk =  qite->initialize({
    std::make_pair("accelerator", acc),
    std::make_pair("steps", nbSteps),
    std::make_pair("observable", observable),
    std::make_pair("step-size", stepSize)
  });

  EXPECT_TRUE(initOk);
  
  auto buffer = xacc::qalloc(1);
Nguyen, Thien Minh's avatar
Nguyen, Thien Minh committed
41
  qite->execute(buffer);
42
43
44
45
46
47
48
  const double finalEnergy = (*buffer)["opt-val"].as<double>();
  std::cout << "Final Energy: " << finalEnergy << "\n";
  // Fig (2.e) of https://www.nature.com/articles/s41567-019-0704-4
  // Minimal Energy = -1
  EXPECT_NEAR(finalEnergy, -1.0, 1e-3);
  const std::vector<double> energyValues = (*buffer)["exp-vals"].as<std::vector<double>>();
  EXPECT_EQ(energyValues.size(), nbSteps + 1);
49
50
}

51
TEST(QITETester, checkDeuteuronH2) 
52
53
54
{
  auto qite = xacc::getService<xacc::Algorithm>("qite");
  std::shared_ptr<xacc::Observable> observable = std::make_shared<xacc::quantum::PauliOperator>();
55
  observable->fromString("5.907 - 2.1433 X0X1 - 2.1433 Y0Y1 + .21829 Z0 - 6.125 Z1");
56
  auto acc = xacc::getAccelerator("qpp");
Nguyen, Thien Minh's avatar
Nguyen, Thien Minh committed
57
  const int nbSteps = 5;
58
  const double stepSize = 0.1;
Nguyen, Thien Minh's avatar
Nguyen, Thien Minh committed
59
60
61
  auto compiler = xacc::getCompiler("xasm");
  auto ir = compiler->compile(R"(__qpu__ void f(qbit q) { X(q[0]); })", nullptr);
  auto x = ir->getComposite("f");
62
63
64
65
66

  const bool initOk =  qite->initialize({
    std::make_pair("accelerator", acc),
    std::make_pair("steps", nbSteps),
    std::make_pair("observable", observable),
Nguyen, Thien Minh's avatar
Nguyen, Thien Minh committed
67
68
    std::make_pair("step-size", stepSize),
    std::make_pair("ansatz", x),
69
70
71
72
73
74
75
76
  });

  EXPECT_TRUE(initOk);
  
  auto buffer = xacc::qalloc(2);
  qite->execute(buffer);
  const double finalEnergy = (*buffer)["opt-val"].as<double>();
  std::cout << "Final Energy: " << finalEnergy << "\n";
77
  EXPECT_NEAR(finalEnergy, -1.74886, 1e-3);
Nguyen, Thien Minh's avatar
Nguyen, Thien Minh committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
}

TEST(QITETester, checkDeuteuronH2Analytical) 
{
  auto qite = xacc::getService<xacc::Algorithm>("qite");
  std::shared_ptr<xacc::Observable> observable = std::make_shared<xacc::quantum::PauliOperator>();
  observable->fromString("5.907 - 2.1433 X0X1 - 2.1433 Y0Y1 + .21829 Z0 - 6.125 Z1");
  auto acc = xacc::getAccelerator("qpp");
  const int nbSteps = 10;
  const double stepSize = 0.1;

  const bool initOk =  qite->initialize({
    std::make_pair("accelerator", acc),
    std::make_pair("steps", nbSteps),
    std::make_pair("observable", observable),
    std::make_pair("step-size", stepSize),
    std::make_pair("analytical", true),
    std::make_pair("initial-state", 1)
  });

  EXPECT_TRUE(initOk);
  auto buffer = xacc::qalloc(2);
  qite->execute(buffer);
  const double finalEnergy = (*buffer)["opt-val"].as<double>();
  std::cout << "Final Energy: " << finalEnergy << "\n";
  EXPECT_NEAR(finalEnergy, -1.74886, 1e-3);
104
  buffer->print();
Nguyen, Thien Minh's avatar
Nguyen, Thien Minh committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  const std::string Aop_Analytical = (*buffer)["A-op"].as<std::string>();
  std::shared_ptr<xacc::quantum::PauliOperator> aOpObservable = std::make_shared<xacc::quantum::PauliOperator>();
  aOpObservable->fromString(Aop_Analytical);
  // Expected: recover UCC ansatz: "X0Y1 − X1Y0"
  EXPECT_EQ(aOpObservable->getSubTerms().size(), 2);
  auto term1 = aOpObservable->getSubTerms()[0];
  auto term2 = aOpObservable->getSubTerms()[1];
  const auto term1Coeff = term1->coefficient();
  const auto term2Coeff = term2->coefficient();
  // Two terms have opposite signs
  const auto diff = term1Coeff + term2Coeff;
  EXPECT_NEAR(std::norm(diff), 0.0, 1e-6);
  const auto endsWith = [](const std::string& in_string, const std::string& in_ending) -> bool {
    return std::equal(in_ending.rbegin(), in_ending.rend(), in_string.rbegin());
  };

  // two UCC's terms: "Y0 X1" "X0 Y1"
  const bool check1 = endsWith(term1->toString(), "Y0 X1") || endsWith(term1->toString(), "X0 Y1");
  const bool check2 = endsWith(term2->toString(), "Y0 X1") || endsWith(term2->toString(), "X0 Y1");
  EXPECT_TRUE(check1 && check2);
125
126
}

Nguyen, Thien Minh's avatar
Nguyen, Thien Minh committed
127

128
TEST(QITETester, checkDeuteuronH3) 
129
130
131
{
  auto qite = xacc::getService<xacc::Algorithm>("qite");
  std::shared_ptr<xacc::Observable> observable = std::make_shared<xacc::quantum::PauliOperator>();
132
  observable->fromString("5.907 - 2.1433 X0X1 - 2.1433 Y0Y1 + .21829 Z0 - 6.125 Z1 + 9.625 - 9.625 Z2 - 3.91 X1 X2 - 3.91 Y1 Y2");
133
  auto acc = xacc::getAccelerator("qpp");
134
  const int nbSteps = 10;
135
  const double stepSize = 0.1;
Nguyen, Thien Minh's avatar
Nguyen, Thien Minh committed
136
137
138
  auto compiler = xacc::getCompiler("xasm");
  auto ir = compiler->compile(R"(__qpu__ void f1(qbit q) { X(q[0]); })", nullptr);
  auto x = ir->getComposite("f1");
139
140
141
142
143

  const bool initOk =  qite->initialize({
    std::make_pair("accelerator", acc),
    std::make_pair("steps", nbSteps),
    std::make_pair("observable", observable),
Nguyen, Thien Minh's avatar
Nguyen, Thien Minh committed
144
145
    std::make_pair("step-size", stepSize),
    std::make_pair("ansatz", x)
146
147
148
149
  });

  EXPECT_TRUE(initOk);
  
150
  auto buffer = xacc::qalloc(3);
151
152
153
  qite->execute(buffer);
  const double finalEnergy = (*buffer)["opt-val"].as<double>();
  std::cout << "Final Energy: " << finalEnergy << "\n";
Nguyen, Thien Minh's avatar
Nguyen, Thien Minh committed
154
  EXPECT_NEAR(finalEnergy, -2.04482, 0.01);
155
156
}

157
158
159
160
161
162
163
int main(int argc, char **argv) {
  xacc::Initialize(argc, argv);
  ::testing::InitGoogleTest(&argc, argv);
  auto ret = RUN_ALL_TESTS();
  xacc::Finalize();
  return ret;
}