Commit 84189e17 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

started exp_i_theta generator, got h2 example working with it


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 9b11e61f
Pipeline #48826 passed with stages
in 2 minutes and 19 seconds
......@@ -32,6 +32,7 @@
/.project
/.settings/
**/.ptp*
**/.*.cpp
/dist/
/*egg*/
/python/*egg*
......
......@@ -7,3 +7,4 @@ services:
- ../../..:/home/project
ports:
- 3000:3000
- 9005:9005
#include "qcor.hpp"
int main(int argc, char **argv) {
qcor::Initialize(argc, argv);
auto optimizer = qcor::getOptimizer(
"nlopt", {{"nlopt-optimizer", "cobyla"}, {"nlopt-maxeval", 1000}});
auto geom = R"geom(2
H 0.00000 0.00000 0.00000
H 0.00000 0.00000 0.7474)geom";
auto op = qcor::getObservable("chemistry",
{{"basis", "sto-3g"}, {"geometry", geom}});
auto future = qcor::submit([&](qcor::qpu_handler &qh) {
qh.vqe(
[&](double x) {
X(0);
X(2);
exp_i_theta(x, {{"pauli", "Y0 X1 X2 X3"}});
},
op, optimizer);
});
auto results = future.get();
auto energy = mpark::get<double>(results->getInformation("opt-val"));
std::cout << "Results: " << energy << "\n";
// results->print();
}
set(LIBRARY_NAME qcor-digital)
file(GLOB SRC generators/hwe/hwe.cpp *.cpp)
file(GLOB SRC generators/hwe/hwe.cpp *.cpp generators/exp/exp.cpp)
usfunctiongetresourcesource(TARGET ${LIBRARY_NAME} OUT SRC)
usfunctiongeneratebundleinit(TARGET ${LIBRARY_NAME} OUT SRC)
......@@ -9,7 +9,7 @@ add_library(${LIBRARY_NAME} SHARED ${SRC})
target_include_directories(
${LIBRARY_NAME}
PUBLIC . generators/hwe)
PUBLIC . generators/hwe generators/exp)
target_link_libraries(${LIBRARY_NAME} PUBLIC xacc CppMicroServices)
......@@ -31,6 +31,7 @@ qcor_enable_rpath(${LIBRARY_NAME})
if(QCOR_BUILD_TESTS)
add_subdirectory(generators/hwe/tests)
add_subdirectory(generators/exp/tests)
endif()
install(TARGETS ${LIBRARY_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/plugins)
#include "hwe.hpp"
#include "exp.hpp"
#include "cppmicroservices/BundleActivator.h"
#include "cppmicroservices/BundleContext.h"
......@@ -12,7 +13,10 @@ public:
void Start(BundleContext context) {
auto hwe = std::make_shared<qcor::instructions::HWE>();
auto expit = std::make_shared<qcor::instructions::Exp>();
context.RegisterService<xacc::IRGenerator>(hwe);
context.RegisterService<xacc::IRGenerator>(expit);
}
void Stop(BundleContext context) {}
......
#include "exp.hpp"
#include "IRProvider.hpp"
#include "PauliOperator.hpp"
#include "XACC.hpp"
#include "xacc_service.hpp"
#include <regex>
using namespace xacc;
using namespace xacc::quantum;
namespace qcor {
namespace instructions {
bool Exp::validateOptions() {
if (!options.count("pauli")) {
return false;
}
return true;
}
std::shared_ptr<Function>
Exp::generate(std::shared_ptr<AcceleratorBuffer> buffer,
std::vector<InstructionParameter> parameters) {
xacc::error("qcor::Exp::generate(buffer,params) not implemented.");
return nullptr;
}
std::shared_ptr<xacc::Function>
Exp::generate(std::map<std::string, xacc::InstructionParameter> &parameters) {
if (!parameters.empty()) {
options = parameters;
}
return generate(std::map<std::string, InstructionParameter>{});
}
std::shared_ptr<Function>
Exp::generate(std::map<std::string, InstructionParameter> &&parameters) {
if (!parameters.empty()) {
options = parameters;
}
auto pauliStr = options["pauli"].toString();
PauliOperator op(pauliStr);
double pi = 3.1415926;
auto gateRegistry = xacc::getService<IRProvider>("gate");
auto function = gateRegistry->createFunction("temp", {}, {});
auto terms = op.getTerms();
for (auto spinInst : terms) {
// Get the individual pauli terms
auto termsMap = std::get<2>(spinInst.second);
std::vector<std::pair<int, std::string>> terms;
for (auto &kv : termsMap) {
if (kv.second != "I" && !kv.second.empty()) {
terms.push_back({kv.first, kv.second});
}
}
// The largest qubit index is on the last term
int largestQbitIdx = terms[terms.size() - 1].first;
for (int i = 0; i < terms.size(); i++) {
auto qbitIdx = terms[i].first;
auto gateName = terms[i].second;
if (i < terms.size() - 1) {
auto cnot = gateRegistry->createInstruction(
"CNOT", std::vector<int>{qbitIdx, terms[i + 1].first});
function->addInstruction(cnot);
}
if (gateName == "X") {
auto hadamard =
gateRegistry->createInstruction("H", std::vector<int>{qbitIdx});
function->insertInstruction(0, hadamard);
} else if (gateName == "Y") {
auto rx =
gateRegistry->createInstruction("Rx", std::vector<int>{qbitIdx});
InstructionParameter p(pi / 2.0);
rx->setParameter(0, p);
function->insertInstruction(0, rx);
}
// Add the Rotation for the last term
if (i == terms.size() - 1) {
// FIXME DONT FORGET DIVIDE BY 2
// std::stringstream ss;
// ss << 2 * std::imag(std::get<0>(spinInst.second)) << " * "
// << std::get<1>(spinInst.second);
auto rz =
gateRegistry->createInstruction("Rz", std::vector<int>{qbitIdx}, {"t"});
// InstructionParameter p(ss.str());
// rz->setParameter(0, p);
function->addInstruction(rz);
}
}
int counter = function->nInstructions();
// Add the instruction on the backend of the circuit
for (int i = terms.size() - 1; i >= 0; i--) {
auto qbitIdx = terms[i].first;
auto gateName = terms[i].second;
if (i < terms.size() - 1) {
auto cnot = gateRegistry->createInstruction(
"CNOT", std::vector<int>{qbitIdx, terms[i + 1].first});
function->insertInstruction(counter, cnot);
counter++;
}
if (gateName == "X") {
auto hadamard =
gateRegistry->createInstruction("H", std::vector<int>{qbitIdx});
function->addInstruction(hadamard);
} else if (gateName == "Y") {
auto rx =
gateRegistry->createInstruction("Rx", std::vector<int>{qbitIdx});
InstructionParameter p(-1.0 * (pi / 2.0));
rx->setParameter(0, p);
function->addInstruction(rx);
}
}
}
return function;
} // namespace instructions
} // namespace instructions
} // namespace qcor
\ No newline at end of file
#ifndef QCOR_INSTRUCTIONS_EXP_HPP_
#define QCOR_INSTRUCTIONS_EXP_HPP_
#include "IRGenerator.hpp"
namespace xacc {
class AcceleratorBuffer;
class Function;
}
namespace qcor {
namespace instructions {
class Exp: public xacc::IRGenerator {
public:
std::shared_ptr<xacc::Function> generate(
std::shared_ptr<xacc::AcceleratorBuffer> buffer,
std::vector<xacc::InstructionParameter> parameters = std::vector<
xacc::InstructionParameter> { }) override;
std::shared_ptr<xacc::Function> generate(
std::map<std::string, xacc::InstructionParameter>& parameters) override;
std::shared_ptr<xacc::Function> generate(
std::map<std::string, xacc::InstructionParameter>&& parameters) override;
bool validateOptions() override;
const std::string name() const override {
return "exp_i_theta";
}
const std::string description() const override {
return "";
}
};
}
}
#endif
\ No newline at end of file
include_directories(${CMAKE_SOURCE_DIR}/ir/digital/generators/exp)
add_xacc_test(Exp)
target_link_libraries(ExpTester qcor-digital qcor)
\ No newline at end of file
#include "exp.hpp"
#include "XACC.hpp"
#include <gtest/gtest.h>
#include "qcor.hpp"
using namespace xacc;
using namespace qcor::instructions;
TEST(ExpTester, checkSimple) {
// NOW Test it somehow...
Exp exp;
auto f =
exp.generate({{"pauli","Y0 X1 X2 X3"}});
std::cout << "F:\n" << f->toString() << "\n";
}
int main(int argc, char **argv) {
qcor::Initialize(argc, argv);
// xacc::Initialize();
::testing::InitGoogleTest(&argc, argv);
auto ret = RUN_ALL_TESTS();
// xacc::Finalize();
return ret;
}
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