Commit 95983d99 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

adding deuteron H3 exp_gen example


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 970f6c1f
Pipeline #48836 passed with stages
in 2 minutes and 19 seconds
......@@ -67,17 +67,18 @@ bool LambdaVisitor::CallExprToGateInstructionVisitor::VisitIntegerLiteral(
return true;
}
bool LambdaVisitor::CallExprToGateInstructionVisitor::VisitUnaryOperator(UnaryOperator* op) {
if (op->getOpcode() == UnaryOperator::Opcode::UO_Minus) {
addMinus = true;
}
return true;
bool LambdaVisitor::CallExprToGateInstructionVisitor::VisitUnaryOperator(
UnaryOperator *op) {
if (op->getOpcode() == UnaryOperator::Opcode::UO_Minus) {
addMinus = true;
}
return true;
}
bool LambdaVisitor::CallExprToGateInstructionVisitor::VisitFloatingLiteral(
FloatingLiteral *literal) {
double value = literal->getValue().convertToDouble();
InstructionParameter p(addMinus ? -1.0*value : value);
InstructionParameter p(addMinus ? -1.0 * value : value);
addMinus = false;
parameters.push_back(p);
return true;
......@@ -87,14 +88,13 @@ bool LambdaVisitor::CallExprToGateInstructionVisitor::VisitDeclRefExpr(
DeclRefExpr *decl) {
auto declName = decl->getNameInfo().getAsString();
if (addMinus) {
declName = "-"+declName;
declName = "-" + declName;
}
if (dyn_cast<ParmVarDecl>(decl->getDecl())) {
parameters.push_back(
InstructionParameter(declName));
parameters.push_back(InstructionParameter(declName));
} else if (dyn_cast<VarDecl>(decl->getDecl())) {
std::cout << "THIS IS A VARDECL: " << declName << "\n";
parameters.push_back(InstructionParameter(declName));
std::cout << "THIS IS A VARDECL: " << declName << "\n";
parameters.push_back(InstructionParameter(declName));
}
return true;
}
......@@ -127,6 +127,16 @@ bool LambdaVisitor::CallExprToIRGenerator::VisitInitListExpr(
}
return true;
}
bool LambdaVisitor::CallExprToIRGenerator::VisitDeclRefExpr(DeclRefExpr *decl) {
if (dyn_cast<ParmVarDecl>(decl->getDecl())) {
auto declName = decl->getNameInfo().getAsString();
// std::cout << "IRGENERATOR FOUND PARAM: " << declName << "\n";
options.insert({"param-id", declName});
}
return true;
}
std::shared_ptr<IRGenerator>
LambdaVisitor::CallExprToIRGenerator::getIRGenerator() {
auto irg = xacc::getService<IRGenerator>(name);
......@@ -322,11 +332,9 @@ bool LambdaVisitor::VisitLambdaExpr(LambdaExpr *LE) {
{varName, (int)int_value->getValue().signedRoundToDouble()});
continue;
} else if (float_value) {
std::cout << varName << ", THIS DOUBLE VALUE IS KNOWN AT COMPILE TIME: "
<< float_value->getValue().convertToDouble()
<< "\n";
captures.insert(
{varName, float_value->getValue().convertToDouble()});
std::cout << varName << ", THIS DOUBLE VALUE IS KNOWN AT COMPILE TIME: "
<< float_value->getValue().convertToDouble() << "\n";
captures.insert({varName, float_value->getValue().convertToDouble()});
continue;
}
......@@ -344,26 +352,26 @@ bool LambdaVisitor::VisitLambdaExpr(LambdaExpr *LE) {
auto function = visitor.getFunction();
for (auto &inst : function->getInstructions()) {
if (!inst->isComposite() && inst->nParameters() > 0) {
int counter = 0;
for (auto& p : inst->getParameters()) {
if (p.isVariable()) {
// see if we have a runtime value in the captures map
for (auto& kv : captures) {
if (p.toString() == kv.first && kv.second.isNumeric()) {
inst->setParameter(counter, kv.second);
} else if (p.toString() == "-"+kv.first && kv.second.which() == 1) {
InstructionParameter pp(-1.0 * mpark::get<double>(kv.second));
inst->setParameter(counter, pp);
}
}
}
counter++;
if (!inst->isComposite() && inst->nParameters() > 0) {
int counter = 0;
for (auto &p : inst->getParameters()) {
if (p.isVariable()) {
// see if we have a runtime value in the captures map
for (auto &kv : captures) {
if (p.toString() == kv.first && kv.second.isNumeric()) {
inst->setParameter(counter, kv.second);
} else if (p.toString() == "-" + kv.first &&
kv.second.which() == 1) {
InstructionParameter pp(-1.0 * mpark::get<double>(kv.second));
inst->setParameter(counter, pp);
}
}
}
counter++;
}
}
}
std::cout << "\n\nXACC IR:\n" << function->toString() << "\n";
// Check if we have IRGenerators in the tree
......
......@@ -89,6 +89,7 @@ protected:
: name(n), provider(p) {}
// bool VisitDeclRefExpr(DeclRefExpr* expr);
bool VisitInitListExpr(InitListExpr *expr);
bool VisitDeclRefExpr(DeclRefExpr *expr);
std::shared_ptr<IRGenerator> getIRGenerator();
};
class ScanInitListExpr : public RecursiveASTVisitor<ScanInitListExpr> {
......
#include "qcor.hpp"
int main(int argc, char **argv) {
qcor::Initialize(argc, argv);
auto optimizer = qcor::getOptimizer(
"nlopt", {{"nlopt-optimizer", "cobyla"}, {"nlopt-maxeval", 20}});
auto op = qcor::getObservable(
"pauli", "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");
auto future = qcor::submit([&](qcor::qpu_handler &qh) {
qh.vqe(
[&](double t0, double t1) {
X(0);
exp_i_theta(t0, {{"pauli", "X0 Y1 - Y0 X1"}});
exp_i_theta(t1, {{"pauli", "X0 Z1 Y2 - X2 Z1 Y0"}});
},
op, optimizer);
});
auto results = future.get();
auto energy = mpark::get<double>(results->getInformation("opt-val"));
std::cout << "Results: " << energy << "\n";
}
......@@ -39,6 +39,10 @@ Exp::generate(std::map<std::string, InstructionParameter> &&parameters) {
options = parameters;
}
std::string paramLetter = "t";
if (options.count("param-id")) {
paramLetter = options["param-id"].toString();
}
auto pauliStr = options["pauli"].toString();
PauliOperator op(pauliStr);
......@@ -92,7 +96,7 @@ Exp::generate(std::map<std::string, InstructionParameter> &&parameters) {
// ss << 2 * std::imag(std::get<0>(spinInst.second)) << " * "
// << std::get<1>(spinInst.second);
auto rz =
gateRegistry->createInstruction("Rz", std::vector<int>{qbitIdx}, {"t"});
gateRegistry->createInstruction("Rz", std::vector<int>{qbitIdx}, {paramLetter});
// InstructionParameter p(ss.str());
// rz->setParameter(0, p);
......
......@@ -56,10 +56,14 @@ std::shared_ptr<Function> HWE::generate(
xacc::error("Could not cast HWE parameter to correct type: " +
std::string(e.what()));
}
std::string paramLetter = "t";
if (options.count("param-id")) {
paramLetter = options["param-id"].toString();
}
std::vector<InstructionParameter> fParams;
for (int nP = 0; nP < (2*nQubits + 3 * nQubits * layers); nP++)
fParams.push_back(InstructionParameter("t" + std::to_string(nP)));
fParams.push_back(InstructionParameter(paramLetter + std::to_string(nP)));
auto provider = xacc::getService<IRProvider>("gate");
auto f = provider->createFunction("hwe", {}, fParams);
......@@ -68,9 +72,9 @@ std::shared_ptr<Function> HWE::generate(
// Zeroth layer, start with X and Z rotations
for (int q = 0; q < nQubits; q++) {
auto rx = provider->createInstruction(
"Rx", {q}, {InstructionParameter("t" + std::to_string(angleCounter))});
"Rx", {q}, {InstructionParameter(paramLetter + std::to_string(angleCounter))});
auto rz = provider->createInstruction(
"Rz", {q}, {InstructionParameter("t" + std::to_string(angleCounter+1))});
"Rz", {q}, {InstructionParameter(paramLetter + std::to_string(angleCounter+1))});
f->addInstruction(rx);
f->addInstruction(rz);
angleCounter+=2;
......@@ -84,17 +88,17 @@ std::shared_ptr<Function> HWE::generate(
for (int q = 0; q < nQubits; q++) {
auto rz1 = provider->createInstruction(
"Rz", {q},
{InstructionParameter("t" + std::to_string(angleCounter))});
{InstructionParameter(paramLetter + std::to_string(angleCounter))});
f->addInstruction(rz1);
auto rx = provider->createInstruction(
"Rx", {q},
{InstructionParameter("t" + std::to_string(angleCounter+1))});
{InstructionParameter(paramLetter + std::to_string(angleCounter+1))});
f->addInstruction(rx);
auto rz2 = provider->createInstruction(
"Rz", {q},
{InstructionParameter("t" + std::to_string(angleCounter + 2))});
{InstructionParameter(paramLetter + std::to_string(angleCounter + 2))});
f->addInstruction(rz2);
angleCounter += 3;
......
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