Commit 89fde0aa authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

Adding Richardson Extrapolation Decorator



Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent fe08b858
......@@ -344,6 +344,7 @@ PYBIND11_MODULE(_pyxacc, m) {
(ExtraInfo(xacc::AcceleratorBuffer::*)(const std::string)) &
xacc::AcceleratorBuffer::getInformation,
"")
.def("hasExtraInfoKey", &xacc::AcceleratorBuffer::hasExtraInfoKey, "")
.def("name", &xacc::AcceleratorBuffer::name, "")
.def("getAllUnique", &xacc::AcceleratorBuffer::getAllUnique,
"Return all unique information with the provided string name")
......
......@@ -39,6 +39,7 @@
#include "ROErrorDecorator.hpp"
#include "ImprovedSamplingDecorator.hpp"
#include "RichExtrapDecorator.hpp"
#include <memory>
#include <set>
......@@ -71,6 +72,7 @@ public:
auto roed = std::make_shared<xacc::quantum::ROErrorDecorator>();
auto impsamplingd = std::make_shared<xacc::quantum::ImprovedSamplingDecorator>();
auto richextrap = std::make_shared<xacc::quantum::RichExtrapDecorator>();
context.RegisterService<xacc::IRProvider>(giservice);
context.RegisterService<xacc::Preprocessor>(kp);
......@@ -86,6 +88,9 @@ public:
context.RegisterService<xacc::AcceleratorDecorator>(roed);
context.RegisterService<xacc::Accelerator>(roed);
context.RegisterService<xacc::AcceleratorDecorator>(richextrap);
context.RegisterService<xacc::Accelerator>(richextrap);
context.RegisterService<xacc::AcceleratorDecorator>(impsamplingd);
context.RegisterService<xacc::Accelerator>(impsamplingd);
......
#include "RichExtrapDecorator.hpp"
#include "InstructionIterator.hpp"
#include "XACC.hpp"
#include "IRProvider.hpp"
namespace xacc {
namespace quantum {
void RichExtrapDecorator::execute(std::shared_ptr<AcceleratorBuffer> buffer,
const std::shared_ptr<Function> function) {
if (!decoratedAccelerator) {
xacc::error("Null Decorated Accelerator Error");
}
if (!xacc::optionExists("rich-extrap-r")) {
xacc::error(
"Cannot find rich-extrap-r. Skipping Richardson Extrapolation.");
return;
}
// Get RO error probs
auto r = std::stoi(xacc::getOption("rich-extrap-r"));
buffer->addExtraInfo("rich-extrap-r", ExtraInfo(r));
auto provider = xacc::getService<IRProvider>("gate");
auto f = provider->createFunction(function->name(), function->bits(),
function->getParameters());
InstructionIterator it(function);
while (it.hasNext()) {
auto nextInst = it.next();
if (!nextInst->isComposite() && nextInst->isEnabled()) {
if (nextInst->name() == "CNOT") {
for (int i = 0; i < r; i++)
f->addInstruction(nextInst);
} else {
f->addInstruction(nextInst);
}
}
}
// std::cout << "HELLO: \n" << f->toString("q");
decoratedAccelerator->execute(buffer, f);
return;
}
std::vector<std::shared_ptr<AcceleratorBuffer>> RichExtrapDecorator::execute(
std::shared_ptr<AcceleratorBuffer> buffer,
const std::vector<std::shared_ptr<Function>> functions) {
std::vector<std::shared_ptr<AcceleratorBuffer>> buffers;
if (!decoratedAccelerator) {
xacc::error("RichExtrap - Null Decorated Accelerator Error");
}
if (!xacc::optionExists("rich-extrap-r")) {
xacc::error(
"Cannot find rich-extrap-r. Skipping Richardson Extrapolation.");
}
// Get RO error probs
auto r = std::stoi(xacc::getOption("rich-extrap-r"));
buffer->addExtraInfo("rich-extrap-r", ExtraInfo(r));
auto provider = xacc::getService<IRProvider>("gate");
std::vector<std::shared_ptr<Function>> newFuncs;
for (auto &f : functions) {
auto newF =
provider->createFunction(f->name(), f->bits(), f->getParameters());
InstructionIterator it(f);
while (it.hasNext()) {
auto nextInst = it.next();
if (!nextInst->isComposite() && nextInst->isEnabled()) {
if (nextInst->name() == "CNOT") {
for (int i = 0; i < r; i++)
newF->addInstruction(nextInst);
} else {
newF->addInstruction(nextInst);
}
}
}
// std::cout << "HI: " << newF->toString("q") << "\n";
newFuncs.push_back(newF);
}
return decoratedAccelerator->execute(buffer, newFuncs);
}
} // namespace quantum
} // namespace xacc
\ No newline at end of file
/*******************************************************************************
* Copyright (c) 2018 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:
* Alexander J. McCaskey - initial API and implementation
*******************************************************************************/
#ifndef XACC_RICHEXTRAPDECORATOR_HPP_
#define XACC_RICHEXTRAPDECORATOR_HPP_
#include "AcceleratorDecorator.hpp"
namespace xacc {
namespace quantum {
class RichExtrapDecorator : public AcceleratorDecorator {
public:
void execute(std::shared_ptr<AcceleratorBuffer> buffer,
const std::shared_ptr<Function> function) override;
std::vector<std::shared_ptr<AcceleratorBuffer>>
execute(std::shared_ptr<AcceleratorBuffer> buffer,
const std::vector<std::shared_ptr<Function>> functions) override;
const std::string name() const override { return "rich-extrap"; }
const std::string description() const override { return ""; }
std::shared_ptr<options_description> getOptions() override {
auto desc = std::make_shared<options_description>();
desc->add_options()("rich-extrap-r", value<std::string>(), "");
return desc;
}
~RichExtrapDecorator() override {}
};
} // namespace quantum
} // namespace xacc
#endif
#add_xacc_test(ReadoutErrorAcceleratorBufferPostprocessor)
add_xacc_test(ImprovedSamplingDecorator)
\ No newline at end of file
add_xacc_test(ImprovedSamplingDecorator)
add_xacc_test(RichExtrapDecorator)
\ No newline at end of file
/*******************************************************************************
* Copyright (c) 2018 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:
* Alexander J. McCaskey - initial API and implementation
*******************************************************************************/
#include <gtest/gtest.h>
#include "XACC.hpp"
#include "RichExtrapDecorator.hpp"
using namespace xacc;
using namespace xacc::quantum;
TEST(RichExtrapDecoratorTester, checkSimple) {
int shots = 8192;
int nExecs = 4;
if (xacc::hasAccelerator("tnqvm")) {
auto acc = xacc::getAccelerator("tnqvm");
auto buffer = acc->createBuffer("buffer", 2);
auto compiler = xacc::getService<xacc::Compiler>("xacc-py");
const std::string src = R"src(def f(buffer):
H(0)
CNOT(0,1)
Measure(0,0)
)src";
auto ir = compiler->compile(src, acc);
auto f = ir->getKernel("f");
xacc::setOption("rich-extrap-r", "5");
RichExtrapDecorator decorator;
decorator.setDecorated(acc);
decorator.execute(buffer, f);
}
}
TEST(ImprovedSamplingDecoratorTester, checkMultiple) {
int shots = 8192;
int nExecs = 2;
if (xacc::hasAccelerator("tnqvm")) {
auto acc = xacc::getAccelerator("tnqvm");
auto buffer = acc->createBuffer("buffer", 2);
auto compiler = xacc::getService<xacc::Compiler>("xacc-py");
const std::string src = R"src(def f(buffer):
H(0)
CNOT(0,1)
Measure(0,0)
)src";
const std::string src2 = R"src(def g(buffer):
H(0)
CNOT(0,1)
Measure(0,0)
)src";
auto ir = compiler->compile(src, acc);
auto ir2 = compiler->compile(src2, acc);
auto f = ir->getKernel("f");
auto g = ir2->getKernel("g");
RichExtrapDecorator decorator;
decorator.setDecorated(acc);
xacc::setOption("rich-extrap-r", "3");
auto buffers = decorator.execute(buffer, {f, g});
}
}
int main(int argc, char **argv) {
xacc::Initialize();
::testing::InitGoogleTest(&argc, argv);
auto ret = RUN_ALL_TESTS();
xacc::Finalize();
return ret;
}
Supports Markdown
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