Commit ad7dd4c7 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

adding xacc::qasm() + vqe algorithm example. Update ExtraInfo to be a xacc::Variant


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 35e4ea36
Pipeline #65144 passed with stage
in 6 minutes and 9 seconds
add_executable(deuteron_from_qasm deuteron.cpp)
target_link_libraries(deuteron_from_qasm PRIVATE xacc xacc-pauli)
\ No newline at end of file
target_link_libraries(deuteron_from_qasm PRIVATE xacc xacc-pauli)
add_executable(deuteron_from_qasm_vqe_algo deuteron_vqe_algo.cpp)
target_link_libraries(deuteron_from_qasm_vqe_algo PRIVATE xacc xacc-pauli)
\ No newline at end of file
#include "XACC.hpp"
#include "Optimizer.hpp"
#include "PauliOperator.hpp"
#include "xacc_service.hpp"
int main(int argc, char **argv) {
xacc::Initialize(argc, argv);
// Get reference to the Accelerator
// specified by --accelerator argument
auto accelerator = xacc::getAccelerator();
// Create the N=2 deuteron Hamiltonian
auto H_N_2 =
std::make_shared<xacc::quantum::PauliOperator>("5.907 - 2.1433 X0X1 "
"- 2.1433 Y0Y1"
"+ .21829 Z0 - 6.125 Z1");
auto optimizer = xacc::getService<xacc::Optimizer>("nlopt");
// JIT map Quil QASM Ansatz to IR
xacc::qasm(R"(
.compiler quil
.function deuteron_ansatz
X 0
Ry(theta) 1
CNOT 1 0
)");
auto ansatz = xacc::getCompiled("deuteron_ansatz");
// Get the VQE Algorithm and initialize it
auto vqe = xacc::getAlgorithm("vqe");
vqe->initialize({{"ansatz", ansatz},
{"observable", H_N_2},
{"accelerator", accelerator},
{"optimizer", optimizer}});
// Allocate some qubits and execute
auto buffer = xacc::qalloc(2);
vqe->execute(buffer);
// Print the result
std::cout << "Energy: "
<< buffer->getInformation("opt-val").as<double>() << "\n";
}
\ No newline at end of file
......@@ -267,6 +267,15 @@ std::shared_ptr<Compiler> getCompiler(const std::string &name) {
}
return c;
}
std::shared_ptr<Algorithm> getAlgorithm(const std::string name) {
if (!xacc::xaccFrameworkInitialized) {
error("XACC not initialized before use. Please execute "
"xacc::Initialize() before using API.");
}
return xacc::getService<Algorithm>(name);
}
std::shared_ptr<IRProvider> getIRProvider(const std::string &name) {
if (!xacc::xaccFrameworkInitialized) {
error("XACC not initialized before use. Please execute "
......
......@@ -16,6 +16,7 @@
#include "Compiler.hpp"
#include "RemoteAccelerator.hpp"
#include "IRProvider.hpp"
#include "Algorithm.hpp"
namespace xacc {
......@@ -244,6 +245,7 @@ std::shared_ptr<Compiler> getCompiler(const std::string &name);
*/
std::shared_ptr<Compiler> getCompiler();
std::shared_ptr<Algorithm> getAlgorithm(const std::string name);
using qbit = std::shared_ptr<xacc::AcceleratorBuffer>;
qbit qalloc(const int n);
......
......@@ -25,7 +25,7 @@ class AcceleratorBuffer;
using AcceleratorBufferChildPair =
std::pair<std::string, std::shared_ptr<AcceleratorBuffer>>;
using ExtraInfo = mpark::variant<int, double, std::string, std::vector<int>,
using ExtraInfo = xacc::Variant<int, double, std::string, std::vector<int>,
std::vector<double>, std::vector<std::string>,
std::map<int, std::vector<int>>,
std::vector<std::pair<double, double>>,
......@@ -295,7 +295,7 @@ public:
void setName(const std::string n) {
bufferId = n;
}
/**
* Print information about this AcceleratorBuffer to the
* given output stream.
......
......@@ -20,6 +20,7 @@
#include <functional>
#include <sstream>
#include <algorithm>
#include <map>
namespace spdlog {
class logger;
......@@ -67,6 +68,20 @@ std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
return os;
}
template <typename T>
std::ostream &operator<<(std::ostream &os, const std::map<int, T> &m) {
os << "[";
int i = 0;
for (auto& kv : m) {
os << "(" << kv.first << "," << kv.second << ")";
if (i != m.size() - 1) {
os << ",";
}
i++;
}
os << "]";
return os;
}
static inline bool is_base64(unsigned char c);
std::string base64_decode(std::string const &encoded_string);
......
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