Commit 764ba1da authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

saving work done on removing gate registry, this branch is broken


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 420a6ea4
...@@ -26,6 +26,5 @@ Ry::Ry(std::vector<int> qbits) : ...@@ -26,6 +26,5 @@ Ry::Ry(std::vector<int> qbits) :
} }
RegisterGateInstruction<Ry> RYTEMP("Ry");
} }
} }
...@@ -19,8 +19,21 @@ namespace xacc { ...@@ -19,8 +19,21 @@ namespace xacc {
namespace quantum { namespace quantum {
class Ry: public virtual GateInstruction { class Ry: public virtual GateInstruction {
public: public:
Ry() :GateInstruction("Ry", std::vector<InstructionParameter> {
InstructionParameter(0.0) }) {}
Ry(std::vector<int> qbits); Ry(std::vector<int> qbits);
Ry(int qbit, double theta); Ry(int qbit, double theta);
virtual std::shared_ptr<GateInstruction> clone() {
return std::make_shared<Ry>();
}
/**
* Return the description of this instance
* @return description The description of this object.
*/
virtual const std::string description() const {
return "";
}
DEFINE_VISITABLE() DEFINE_VISITABLE()
......
...@@ -26,7 +26,5 @@ Rz::Rz(std::vector<int> qbits) : ...@@ -26,7 +26,5 @@ Rz::Rz(std::vector<int> qbits) :
} }
RegisterGateInstruction<Rz> RZTEMP("Rz");
//RegisterParameterizedGateInstruction<Rz, double> RZTEMP("Rz");
} }
} }
...@@ -19,8 +19,21 @@ namespace xacc { ...@@ -19,8 +19,21 @@ namespace xacc {
namespace quantum { namespace quantum {
class Rz: public virtual GateInstruction { class Rz: public virtual GateInstruction {
public: public:
Rz() : GateInstruction("Rz", std::vector<InstructionParameter> {
InstructionParameter(0.0) }) {}
Rz(std::vector<int> qbits); Rz(std::vector<int> qbits);
Rz(int qbit, double theta); Rz(int qbit, double theta);
virtual std::shared_ptr<GateInstruction> clone() {
return std::make_shared<Rz>();
}
/**
* Return the description of this instance
* @return description The description of this object.
*/
virtual const std::string description() const {
return "";
}
DEFINE_VISITABLE() DEFINE_VISITABLE()
......
...@@ -25,6 +25,5 @@ Swap::Swap(std::vector<int> qbits) : ...@@ -25,6 +25,5 @@ Swap::Swap(std::vector<int> qbits) :
} }
RegisterGateInstruction<Swap> SWAPTEMP("Swap");
} }
} }
...@@ -19,8 +19,20 @@ namespace xacc { ...@@ -19,8 +19,20 @@ namespace xacc {
namespace quantum { namespace quantum {
class Swap: public virtual GateInstruction { class Swap: public virtual GateInstruction {
public: public:
Swap() :GateInstruction("Swap"){}
Swap(std::vector<int> qbits); Swap(std::vector<int> qbits);
Swap(int controlQubit, int targetQubit); Swap(int controlQubit, int targetQubit);
virtual std::shared_ptr<GateInstruction> clone() {
return std::make_shared<Swap>();
}
/**
* Return the description of this instance
* @return description The description of this object.
*/
virtual const std::string description() const {
return "";
}
DEFINE_VISITABLE() DEFINE_VISITABLE()
......
...@@ -23,8 +23,6 @@ X::X(int qbit) : ...@@ -23,8 +23,6 @@ X::X(int qbit) :
X(std::vector<int> { qbit }) { X(std::vector<int> { qbit }) {
} }
RegisterGateInstruction<X> XTEMP("X");
} }
} }
...@@ -23,11 +23,23 @@ namespace quantum { ...@@ -23,11 +23,23 @@ namespace quantum {
*/ */
class X : public virtual GateInstruction { class X : public virtual GateInstruction {
public: public:
X() :GateInstruction("X"){}
X(std::vector<int> qbit); X(std::vector<int> qbit);
X(int qbit); X(int qbit);
// virtual void accept(std::shared_ptr<InstructionVisitor> visitor); // virtual void accept(std::shared_ptr<InstructionVisitor> visitor);
virtual std::shared_ptr<GateInstruction> clone() {
return std::make_shared<X>();
}
/**
* Return the description of this instance
* @return description The description of this object.
*/
virtual const std::string description() const {
return "";
}
DEFINE_VISITABLE() DEFINE_VISITABLE()
......
...@@ -22,8 +22,6 @@ Y::Y(std::vector<int> qbit) : ...@@ -22,8 +22,6 @@ Y::Y(std::vector<int> qbit) :
Y::Y(int qbit) : Y::Y(int qbit) :
Y(std::vector<int> { qbit }) { Y(std::vector<int> { qbit }) {
} }
RegisterGateInstruction<Y> YTEMP("Y");
} }
} }
...@@ -23,9 +23,21 @@ namespace quantum { ...@@ -23,9 +23,21 @@ namespace quantum {
*/ */
class Y : public virtual GateInstruction { class Y : public virtual GateInstruction {
public: public:
Y() : GateInstruction("Y"){}
Y(std::vector<int> qbit); Y(std::vector<int> qbit);
Y(int qbit); Y(int qbit);
virtual std::shared_ptr<GateInstruction> clone() {
return std::make_shared<Y>();
}
/**
* Return the description of this instance
* @return description The description of this object.
*/
virtual const std::string description() const {
return "";
}
DEFINE_VISITABLE() DEFINE_VISITABLE()
......
...@@ -22,8 +22,6 @@ Z::Z(std::vector<int> qbit) : ...@@ -22,8 +22,6 @@ Z::Z(std::vector<int> qbit) :
Z::Z(int qbit) : Z::Z(int qbit) :
Z(std::vector<int> { qbit }) { Z(std::vector<int> { qbit }) {
} }
RegisterGateInstruction<Z> ZTEMP("Z");
} }
} }
...@@ -23,9 +23,21 @@ namespace quantum { ...@@ -23,9 +23,21 @@ namespace quantum {
*/ */
class Z : public virtual GateInstruction { class Z : public virtual GateInstruction {
public: public:
Z() : GateInstruction("Z"){}
Z(std::vector<int> qbit); Z(std::vector<int> qbit);
Z(int qbit); Z(int qbit);
virtual std::shared_ptr<GateInstruction> clone() {
return std::make_shared<Z>();
}
/**
* Return the description of this instance
* @return description The description of this object.
*/
virtual const std::string description() const {
return "";
}
DEFINE_VISITABLE() DEFINE_VISITABLE()
......
...@@ -25,21 +25,14 @@ BOOST_AUTO_TEST_CASE(checkCreation) { ...@@ -25,21 +25,14 @@ BOOST_AUTO_TEST_CASE(checkCreation) {
BOOST_VERIFY(cnot.bits().size() == 2); BOOST_VERIFY(cnot.bits().size() == 2);
BOOST_VERIFY(cnot.bits()[0] == 0); BOOST_VERIFY(cnot.bits()[0] == 0);
BOOST_VERIFY(cnot.bits()[1] == 1); BOOST_VERIFY(cnot.bits()[1] == 1);
BOOST_VERIFY(cnot.getName() == "CNOT"); BOOST_VERIFY(cnot.name() == "CNOT");
CNOT cnot2(44, 46); CNOT cnot2(44, 46);
BOOST_VERIFY(cnot2.toString("qreg") == "CNOT qreg44,qreg46"); BOOST_VERIFY(cnot2.toString("qreg") == "CNOT qreg44,qreg46");
BOOST_VERIFY(cnot2.bits().size() == 2); BOOST_VERIFY(cnot2.bits().size() == 2);
BOOST_VERIFY(cnot2.bits()[0] == 44); BOOST_VERIFY(cnot2.bits()[0] == 44);
BOOST_VERIFY(cnot2.bits()[1] == 46); BOOST_VERIFY(cnot2.bits()[1] == 46);
BOOST_VERIFY(cnot2.getName() == "CNOT"); BOOST_VERIFY(cnot2.name() == "CNOT");
} }
BOOST_AUTO_TEST_CASE(checkAutoRegistration) {
auto cnot = GateInstructionRegistry::instance()->create("CNOT",
std::vector<int> { 0, 1 });
BOOST_VERIFY(cnot->getName() == "CNOT");
BOOST_VERIFY(cnot->bits().size() == 2);
BOOST_VERIFY(cnot->bits()[0] == 0);
BOOST_VERIFY(cnot->bits()[1] == 1);
}
...@@ -26,7 +26,7 @@ BOOST_AUTO_TEST_CASE(checkCreation) { ...@@ -26,7 +26,7 @@ BOOST_AUTO_TEST_CASE(checkCreation) {
BOOST_VERIFY(cp.bits().size() == 2); BOOST_VERIFY(cp.bits().size() == 2);
BOOST_VERIFY(cp.bits()[0] == 0); BOOST_VERIFY(cp.bits()[0] == 0);
BOOST_VERIFY(cp.bits()[1] == 1); BOOST_VERIFY(cp.bits()[1] == 1);
BOOST_VERIFY(cp.getName() == "CPhase"); BOOST_VERIFY(cp.name() == "CPhase");
CPhase cp2(44, 45, 1.71234); CPhase cp2(44, 45, 1.71234);
...@@ -35,15 +35,7 @@ BOOST_AUTO_TEST_CASE(checkCreation) { ...@@ -35,15 +35,7 @@ BOOST_AUTO_TEST_CASE(checkCreation) {
BOOST_VERIFY(cp2.bits().size() == 2); BOOST_VERIFY(cp2.bits().size() == 2);
BOOST_VERIFY(cp2.bits()[0] == 44); BOOST_VERIFY(cp2.bits()[0] == 44);
BOOST_VERIFY(cp2.bits()[1] == 45); BOOST_VERIFY(cp2.bits()[1] == 45);
BOOST_VERIFY(cp2.getName() == "CPhase"); BOOST_VERIFY(cp2.name() == "CPhase");
} }
BOOST_AUTO_TEST_CASE(checkAutoRegistration) {
xacc::InstructionParameter p = 3.1415;
auto cp = GateInstructionRegistry::instance()->create("CPhase", std::vector<int>{0, 1});
cp->setParameter(0, p);
BOOST_VERIFY(cp->getName() == "CPhase");
BOOST_VERIFY(boost::get<double>(cp->getParameter(0)) == 3.1415);
}
...@@ -31,7 +31,7 @@ const std::string expectedQasm = ...@@ -31,7 +31,7 @@ const std::string expectedQasm =
BOOST_AUTO_TEST_CASE(checkFunctionMethods) { BOOST_AUTO_TEST_CASE(checkFunctionMethods) {
GateFunction f("foo"); GateFunction f("foo");
BOOST_VERIFY(f.getName() == "foo"); BOOST_VERIFY(f.name() == "foo");
BOOST_VERIFY(f.nInstructions() == 0); BOOST_VERIFY(f.nInstructions() == 0);
auto h = std::make_shared<Hadamard>(1); auto h = std::make_shared<Hadamard>(1);
...@@ -89,7 +89,7 @@ BOOST_AUTO_TEST_CASE(checkWalkFunctionTree) { ...@@ -89,7 +89,7 @@ BOOST_AUTO_TEST_CASE(checkWalkFunctionTree) {
xacc::InstructionIterator it(f); xacc::InstructionIterator it(f);
while(it.hasNext()) { while(it.hasNext()) {
auto inst = it.next(); auto inst = it.next();
if (!inst->isComposite()) std::cout << inst->getName() << "\n"; if (!inst->isComposite()) std::cout << inst->name() << "\n";
} }
} }
......
...@@ -24,22 +24,13 @@ BOOST_AUTO_TEST_CASE(checkCreation) { ...@@ -24,22 +24,13 @@ BOOST_AUTO_TEST_CASE(checkCreation) {
BOOST_VERIFY(h.toString("qreg") == "H qreg0"); BOOST_VERIFY(h.toString("qreg") == "H qreg0");
BOOST_VERIFY(h.bits().size() == 1); BOOST_VERIFY(h.bits().size() == 1);
BOOST_VERIFY(h.bits()[0] == 0); BOOST_VERIFY(h.bits()[0] == 0);
BOOST_VERIFY(h.getName() == "H"); BOOST_VERIFY(h.name() == "H");
Hadamard h2(44); Hadamard h2(44);
BOOST_VERIFY(h2.toString("qreg") == "H qreg44"); BOOST_VERIFY(h2.toString("qreg") == "H qreg44");
BOOST_VERIFY(h2.bits().size() == 1); BOOST_VERIFY(h2.bits().size() == 1);
BOOST_VERIFY(h2.bits()[0] == 44); BOOST_VERIFY(h2.bits()[0] == 44);
BOOST_VERIFY(h2.getName() == "H"); BOOST_VERIFY(h2.name() == "H");
} }
BOOST_AUTO_TEST_CASE(checkAutoRegistration) {
auto hadamard = GateInstructionRegistry::instance()->create("H",
std::vector<int> { 0 });
BOOST_VERIFY(hadamard->bits().size() == 1);
BOOST_VERIFY(hadamard->bits()[0] == 0);
BOOST_VERIFY(hadamard->getName() == "H");
}
...@@ -17,12 +17,14 @@ ...@@ -17,12 +17,14 @@
#include "InverseQFT.hpp" #include "InverseQFT.hpp"
#include "JsonVisitor.hpp" #include "JsonVisitor.hpp"
#include "GateQIR.hpp" #include "GateQIR.hpp"
#include "XACC.hpp"
using namespace xacc; using namespace xacc;
using namespace xacc::quantum; using namespace xacc::quantum;
BOOST_AUTO_TEST_CASE(checkCreation) { BOOST_AUTO_TEST_CASE(checkCreation) {
xacc::Initialize();
auto iqft = std::make_shared<InverseQFT>(); auto iqft = std::make_shared<InverseQFT>();
auto buffer = std::make_shared<AcceleratorBuffer>("",3); auto buffer = std::make_shared<AcceleratorBuffer>("",3);
...@@ -34,6 +36,7 @@ BOOST_AUTO_TEST_CASE(checkCreation) { ...@@ -34,6 +36,7 @@ BOOST_AUTO_TEST_CASE(checkCreation) {
JsonVisitor v(iqftKernel); JsonVisitor v(iqftKernel);
std::cout << v.write() << "\n"; std::cout << v.write() << "\n";
xacc::Finalize();
// auto h1 = std::make_shared<Hadamard>(2); // auto h1 = std::make_shared<Hadamard>(2);
// auto cphase1 = std::make_shared<CPhase>(1, 2, 1.5707963); // auto cphase1 = std::make_shared<CPhase>(1, 2, 1.5707963);
// auto h2 = std::make_shared<Hadamard>(2); // auto h2 = std::make_shared<Hadamard>(2);
......
...@@ -25,16 +25,7 @@ BOOST_AUTO_TEST_CASE(checkCreation) { ...@@ -25,16 +25,7 @@ BOOST_AUTO_TEST_CASE(checkCreation) {
BOOST_VERIFY(meas.toString("qreg") == "Measure qreg0"); BOOST_VERIFY(meas.toString("qreg") == "Measure qreg0");
BOOST_VERIFY(meas.bits().size() == 1); BOOST_VERIFY(meas.bits().size() == 1);
BOOST_VERIFY(meas.bits()[0] == 0); BOOST_VERIFY(meas.bits()[0] == 0);
BOOST_VERIFY(meas.getName() == "Measure"); BOOST_VERIFY(meas.name() == "Measure");
} }
BOOST_AUTO_TEST_CASE(checkAutoRegistration) {
xacc::InstructionParameter p(1);
auto meas = GateInstructionRegistry::instance()->create("Measure", std::vector<int>{0});
meas->setParameter(0, p);
BOOST_VERIFY(meas->getName() == "Measure");
BOOST_VERIFY(boost::get<int>(meas->getParameter(0)) == 1);
}
...@@ -17,12 +17,14 @@ ...@@ -17,12 +17,14 @@
#include "QFT.hpp" #include "QFT.hpp"
#include "JsonVisitor.hpp" #include "JsonVisitor.hpp"
#include "GateQIR.hpp" #include "GateQIR.hpp"
#include "XACC.hpp"
using namespace xacc; using namespace xacc;
using namespace xacc::quantum; using namespace xacc::quantum;
BOOST_AUTO_TEST_CASE(checkCreation) { BOOST_AUTO_TEST_CASE(checkCreation) {
xacc::Initialize();
auto qft = std::make_shared<QFT>(); auto qft = std::make_shared<QFT>();
auto buffer = std::make_shared<AcceleratorBuffer>("",3); auto buffer = std::make_shared<AcceleratorBuffer>("",3);
...@@ -53,5 +55,6 @@ BOOST_AUTO_TEST_CASE(checkCreation) { ...@@ -53,5 +55,6 @@ BOOST_AUTO_TEST_CASE(checkCreation) {
auto expectedIR = std::make_shared<GateQIR>(); auto expectedIR = std::make_shared<GateQIR>();
expectedIR->addKernel(expectedF); expectedIR->addKernel(expectedF);
xacc::Finalize();
} }
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "GateQIR.hpp" #include "GateQIR.hpp"
#include <boost/math/constants/constants.hpp> #include <boost/math/constants/constants.hpp>
#include "XACC.hpp" #include "XACC.hpp"
#include "GateInstructionService.hpp"
using namespace xacc; using namespace xacc;
...@@ -31,6 +32,8 @@ std::shared_ptr<IR> createXACCIR(std::unordered_map<std::string, Term> terms) { ...@@ -31,6 +32,8 @@ std::shared_ptr<IR> createXACCIR(std::unordered_map<std::string, Term> terms) {
int counter = 0; int counter = 0;
auto pi = boost::math::constants::pi<double>(); auto pi = boost::math::constants::pi<double>();
GateInstructionService gateRegistry;
// Populate GateQIR now... // Populate GateQIR now...
for (auto& inst : terms) { for (auto& inst : terms) {
...@@ -44,7 +47,7 @@ std::shared_ptr<IR> createXACCIR(std::unordered_map<std::string, Term> terms) { ...@@ -44,7 +47,7 @@ std::shared_ptr<IR> createXACCIR(std::unordered_map<std::string, Term> terms) {
// Loop over all terms in the Spin Instruction // Loop over all terms in the Spin Instruction
// and create instructions to run on the Gate QPU. // and create instructions to run on the Gate QPU.
std::vector<std::shared_ptr<xacc::quantum::GateInstruction>> measurements; std::vector<std::shared_ptr<xacc::Instruction>> measurements;
std::vector<std::pair<int, std::string>> terms; std::vector<std::pair<int, std::string>> terms;
for (auto& kv : spinInst) { for (auto& kv : spinInst) {
...@@ -56,20 +59,18 @@ std::shared_ptr<IR> createXACCIR(std::unordered_map<std::string, Term> terms) { ...@@ -56,20 +59,18 @@ std::shared_ptr<IR> createXACCIR(std::unordered_map<std::string, Term> terms) {
for (int i = terms.size() - 1; i >= 0; i--) { for (int i = terms.size() - 1; i >= 0; i--) {
auto qbit = terms[i].first; auto qbit = terms[i].first;
auto gateName = terms[i].second; auto gateName = terms[i].second;
auto gateRegistry = auto meas = gateRegistry.create("Measure",
xacc::quantum::GateInstructionRegistry::instance();
auto meas = gateRegistry->create("Measure",
std::vector<int> { qbit }); std::vector<int> { qbit });
xacc::InstructionParameter classicalIdx(qbit); xacc::InstructionParameter classicalIdx(qbit);
meas->setParameter(0, classicalIdx); meas->setParameter(0, classicalIdx);
measurements.push_back(meas); measurements.push_back(meas);
if (gateName == "X") { if (gateName == "X") {
auto hadamard = gateRegistry->create("H", std::vector<int> { auto hadamard = gateRegistry.create("H", std::vector<int> {
qbit }); qbit });
gateFunction->addInstruction(hadamard); gateFunction->addInstruction(hadamard);
} else if (gateName == "Y") { } else if (gateName == "Y") {
auto rx = gateRegistry->create("Rx", std::vector<int> { qbit }); auto rx = gateRegistry.create("Rx", std::vector<int> { qbit });
InstructionParameter p(pi / 2.0); InstructionParameter p(pi / 2.0);
rx->setParameter(0, p); rx->setParameter(0, p);
gateFunction->addInstruction(rx); gateFunction->addInstruction(rx);
...@@ -103,6 +104,7 @@ BOOST_AUTO_TEST_CASE(checkSimple) { ...@@ -103,6 +104,7 @@ BOOST_AUTO_TEST_CASE(checkSimple) {
// (-2.143303525+0j)*Y0*Y1 + (-3.91311896+0j)*Y1*Y2 + (0.218290555+0j)*Z0 + (-6.125+0j)*Z1 + (-9.625+0j)*Z2 // (-2.143303525+0j)*Y0*Y1 + (-3.91311896+0j)*Y1*Y2 + (0.218290555+0j)*Z0 + (-6.125+0j)*Z1 + (-9.625+0j)*Z2
// needs x0, x1, x2, y0, y1, y2 // needs x0, x1, x2, y0, y1, y2
xacc::Initialize();
std::unordered_map<std::string, Term> test {{"X0X1", {{0,"X"}, {1,"X"}}}, std::unordered_map<std::string, Term> test {{"X0X1", {{0,"X"}, {1,"X"}}},
{"X1X2", {{1,"X"}, {2,"X"}}}, {"X1X2", {{1,"X"}, {2,"X"}}},
{"Y0Y1", {{0,"Y"}, {1,"Y"}}}, {"Y0Y1", {{0,"Y"}, {1,"Y"}}},
...@@ -125,4 +127,5 @@ BOOST_AUTO_TEST_CASE(checkSimple) { ...@@ -125,4 +127,5 @@ BOOST_AUTO_TEST_CASE(checkSimple) {
std::cout << "K:\n" << k->toString("q") << "\n"; std::cout << "K:\n" << k->toString("q") << "\n";
} }
xacc::Finalize();
} }
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