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

Improving circuit optimization tests, exposing to service registry and python,...


Improving circuit optimization tests, exposing to service registry and python, adding py script demo

Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 2f09fda3
import xacc
xacc.Initialize()
u = xacc.getIRGenerator('uccsd')
f = u.generate([2,4]).eval([0,0])
print('Before:\n',f.toString('q'))
opt = xacc.getIRTransformation('circuit-optimizer')
ir = xacc.gate.createIR()
ir.addKernel(f)
opt.transform(ir)
print('After:\n', ir.getKernels()[0].toString('q'))
xacc.Finalize()
\ No newline at end of file
......@@ -133,6 +133,7 @@ PYBIND11_MODULE(_pyxacc, m) {
.def("description", &xacc::Function::description, "")
.def("nParameters", &xacc::Function::nParameters, "")
.def("toString", &xacc::Function::toString, "")
.def("enabledView", &xacc::Function::enabledView, "")
.def("getParameter", &xacc::Function::getParameter, "")
.def("getParameters", &xacc::Function::getParameters, "")
.def("setParameter", &xacc::Function::setParameter, "")
......@@ -152,6 +153,10 @@ PYBIND11_MODULE(_pyxacc, m) {
py::class_<xacc::IRPreprocessor, std::shared_ptr<xacc::IRPreprocessor>> (m, "IRPreprocesor", "")
.def("process", &xacc::IRPreprocessor::process, "");
py::class_<xacc::IRTransformation, std::shared_ptr<xacc::IRTransformation>> (m, "IRTransformation", "")
.def("transform", &xacc::IRTransformation::transform, "");
py::class_<xacc::AcceleratorBufferPostprocessor, std::shared_ptr<xacc::AcceleratorBufferPostprocessor>> (m, "AcceleratorBufferPostprocessor", "")
.def("process", &xacc::AcceleratorBufferPostprocessor::process, "");
......@@ -278,6 +283,9 @@ PYBIND11_MODULE(_pyxacc, m) {
m.def("getIRPreprocessor", (std::shared_ptr<xacc::IRPreprocessor> (*)(const std::string&))
&xacc::getService<IRPreprocessor>, py::return_value_policy::reference,
"Return the IRPreprocessor of given name.");
m.def("getIRTransformation", (std::shared_ptr<xacc::IRTransformation> (*)(const std::string&))
&xacc::getService<IRTransformation>, py::return_value_policy::reference,
"Return the IRTransformation of given name.");
m.def("getIRGenerator", (std::shared_ptr<xacc::IRGenerator> (*)(const std::string&))
&xacc::getService<IRGenerator>, py::return_value_policy::reference,
"Return the IRGenerator of given name.");
......
......@@ -35,6 +35,8 @@
#include "Identity.hpp"
#include "CZ.hpp"
#include "CircuitOptimizer.hpp"
#include <memory>
#include <set>
......@@ -64,6 +66,8 @@ public:
auto giservice = std::make_shared<xacc::quantum::GateIRProvider>();
auto opt = std::make_shared<xacc::quantum::CircuitOptimizer>();
context.RegisterService<xacc::IRProvider>(giservice);
context.RegisterService<xacc::Preprocessor>(kp);
context.RegisterService<xacc::IRGenerator>(iqft);
......@@ -72,6 +76,9 @@ public:
context.RegisterService<xacc::IRPreprocessor>(readout);
context.RegisterService<xacc::IRPreprocessor>(qbitmap);
context.RegisterService<xacc::IRTransformation>(opt);
context.RegisterService<xacc::OptionsProvider>(opt);
auto h = std::make_shared<xacc::quantum::Hadamard>();
auto cn = std::make_shared<xacc::quantum::CNOT>();
auto cp = std::make_shared<xacc::quantum::CPhase>();
......
......@@ -35,8 +35,10 @@ std::shared_ptr<IR> CircuitOptimizer::transform(std::shared_ptr<IR> ir) {
return 0.0;
};
int nTries = 2;
for (auto& k : gateir->getKernels()) {
for (int j = 0; j < nTries; ++j) {
auto gateFunction = std::dynamic_pointer_cast<GateFunction>(k);
// Remove any zero rotations (Rz(theta<1e-12))
......@@ -57,7 +59,7 @@ std::shared_ptr<IR> CircuitOptimizer::transform(std::shared_ptr<IR> ir) {
bool modified = false;
gateFunction = std::dynamic_pointer_cast<GateFunction>(gateFunction->enabledView());
auto graphView = gateFunction->toGraph();
for (int i = 1; i < graphView.order()-1; i++) {
for (int i = 1; i < graphView.order()-2; i++) {
auto node = graphView.getVertex(i);
if (node.name() == "CNOT"
&& gateFunction->getInstruction(node.id()-1)->isEnabled()) {
......@@ -111,6 +113,7 @@ std::shared_ptr<IR> CircuitOptimizer::transform(std::shared_ptr<IR> ir) {
if (!modified) break;
}
}
}
......
......@@ -8,7 +8,7 @@
namespace xacc {
namespace quantum {
class CircuitOptimizer : public IRTransformation {
class CircuitOptimizer : public IRTransformation, public OptionsProvider {
public:
......@@ -23,6 +23,18 @@ public:
virtual const std::string description() const {
return "";
}
virtual std::shared_ptr<options_description> getOptions() {
auto desc = std::make_shared<options_description>(
"CircuitOptimizer Options");
desc->add_options()("circuit-opt-n-tries", value<std::string>(),
"Provide the number of passes to use in optimizing this circuit. Default = 2.");
return desc;
}
virtual bool handleOptions(variables_map& map) {
return false;
}
};
}
}
......
......@@ -217,6 +217,45 @@ TEST(CircuitOptimizerTester, checkOptimize) {
}
}
TEST(CircuitOptimizerTester, checkSimple) {
if (xacc::hasCompiler("xacc-py")) {
auto c = xacc::getService<xacc::Compiler>("xacc-py");
auto f = c->compile("def foo():\n CNOT(0,1)\n CNOT(0,1)\n")->getKernels()[0];
auto ir = std::make_shared<GateIR>();
ir->addKernel(f);
CircuitOptimizer opt;
auto newir = opt.transform(ir);
auto optF = newir->getKernels()[0];
optF = std::dynamic_pointer_cast<GateFunction>(optF->enabledView());
EXPECT_EQ(0,optF->nInstructions());
f = c->compile("def foo():\n H(0)\n CNOT(0,1)\n CNOT(0,1)\n H(0)\n")->getKernels()[0];
ir = std::make_shared<GateIR>();
ir->addKernel(f);
newir = opt.transform(ir);
optF = newir->getKernels()[0];
optF = std::dynamic_pointer_cast<GateFunction>(optF->enabledView());
EXPECT_EQ(0,optF->nInstructions());
f = c->compile("def foo():\n CNOT(0,1)\n H(1)\n H(1)\n CNOT(0,1)\n")->getKernels()[0];
ir = std::make_shared<GateIR>();
ir->addKernel(f);
newir = opt.transform(ir);
optF = newir->getKernels()[0];
optF = std::dynamic_pointer_cast<GateFunction>(optF->enabledView());
EXPECT_EQ(0,optF->nInstructions());
}
}
int main(int argc, char **argv) {
xacc::Initialize(argc,argv);
......
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