Commit 027ad2c2 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

adding gate instruction registration

parent bb877d5c
......@@ -32,6 +32,7 @@
#define QUANTUM_GATE_GATEQIR_GATEINSTRUCTION_HPP_
#include "QInstruction.hpp"
#include "Registry.hpp"
namespace xacc {
namespace quantum {
......@@ -74,6 +75,10 @@ public:
std::vector<int> { }) {
}
GateInstruction(int id, int layer, std::vector<int> qubts) :
gateId(id), circuitLayer(layer), gateName("UNKNOWN"), qbits(qubts) {
}
/**
* The constructor, takes the id, name, layer, and qubits
* this instruction acts on.
......@@ -144,9 +149,26 @@ public:
virtual ~GateInstruction() {
}
};
}
}
/**
*/
using GateInstructionRegistry = Registry<GateInstruction, int, int, std::vector<int>>;
/**
*/
template<typename T>
class RegisterGateInstruction {
public:
RegisterGateInstruction(const std::string& name) {
GateInstructionRegistry::instance()->add(name,
(std::function<
std::shared_ptr<xacc::quantum::GateInstruction>(int,
int, std::vector<int>)>) ([](int id, int layer, std::vector<int> qubits) {
return std::make_shared<T>(id, layer, qubits);
}));
}
};
}
}
#endif /* QUANTUM_GATE_GATEQIR_GATEINSTRUCTION_HPP_ */
......@@ -104,6 +104,27 @@ public:
}
};
}}
/**
*/
template<typename... Params>
using ParameterizedGateInstructionRegistry = Registry<ParameterizedGateInstruction<Params...>, int, int, std::vector<int>, Params...>;
/**
*/
template<typename T, typename... Params>
class RegisterParameterizedGateInstruction {
public:
RegisterParameterizedGateInstruction(const std::string& name) {
ParameterizedGateInstructionRegistry<Params...>::instance()->add(name,
(std::function<
std::shared_ptr<xacc::quantum::ParameterizedGateInstruction<Params...>>(int,
int, std::vector<int>, Params...)>) ([](int id, int layer, std::vector<int> qubits, Params... args) {
return std::make_shared<T>(id, layer, qubits, args...);
}));
}
};
}
}
#endif
......@@ -31,7 +31,7 @@
#ifndef QUANTUM_GATE_GATEQIR_INSTRUCTIONS_CNOT_HPP_
#define QUANTUM_GATE_GATEQIR_INSTRUCTIONS_CNOT_HPP_
#include "../../gateqir/GateInstruction.hpp"
#include "GateInstruction.hpp"
class QInstructionVisitor;
namespace xacc {
......@@ -42,6 +42,10 @@ namespace quantum {
*/
class CNOT: public virtual GateInstruction {
public:
CNOT(int id, int layer, std::vector<int> qbits) :
GateInstruction(id, layer, "CNOT", qbits) {
}
CNOT(int id, int layer, int srcqbit, int tgtqbit) :
GateInstruction(id, layer, "CNOT", std::vector<int> { srcqbit,
tgtqbit }) {
......@@ -52,6 +56,9 @@ public:
}
};
RegisterGateInstruction<CNOT> CNOTTEMP("CNOT");
}
}
#endif /* QUANTUM_GATE_GATEQIR_INSTRUCTIONS_CNOT_HPP_ */
......@@ -42,6 +42,10 @@ namespace quantum {
*/
class Hadamard : public virtual GateInstruction {
public:
Hadamard(int id, int layer, std::vector<int> qbit) :
GateInstruction(id, layer, "H", qbit) {
}
Hadamard(int id, int layer, int qbit) :
GateInstruction(id, layer, "H", std::vector<int> { qbit }) {
}
......@@ -51,6 +55,8 @@ public:
}
};
RegisterGateInstruction<Hadamard> HTEMP("H");
}
}
......
......@@ -38,6 +38,10 @@ namespace xacc {
namespace quantum {
class Rz: public virtual ParameterizedGateInstruction<double> {
public:
Rz(int id, int layer, std::vector<int> qbits, double theta) :
ParameterizedGateInstruction<double>(theta), GateInstruction(id,
layer, "Rz", qbits) {
}
Rz(int id, int layer, int qbit, double theta) :
ParameterizedGateInstruction<double>(theta), GateInstruction(id,
layer, "Rz", std::vector<int> { qbit }) {
......@@ -48,6 +52,8 @@ public:
}
};
RegisterParameterizedGateInstruction<Rz, double> RZTEMP("Rz");
}
}
......
......@@ -59,3 +59,9 @@ BOOST_AUTO_TEST_CASE(checkCreation) {
}
BOOST_AUTO_TEST_CASE(checkAutoRegistration) {
auto cnot = GateInstructionRegistry::instance()->create("CNOT", 1, 1, std::vector<int>{0});
BOOST_VERIFY(cnot->getId() == 1);
}
......@@ -57,3 +57,10 @@ BOOST_AUTO_TEST_CASE(checkCreation) {
}
BOOST_AUTO_TEST_CASE(checkAutoRegistration) {
auto hadamard = GateInstructionRegistry::instance()->create("H", 1, 1, std::vector<int>{0});
BOOST_VERIFY(hadamard->getId() == 1);
}
......@@ -59,3 +59,11 @@ BOOST_AUTO_TEST_CASE(checkCreation) {
}
BOOST_AUTO_TEST_CASE(checkAutoRegistration) {
auto rz = ParameterizedGateInstructionRegistry<double>::instance()->create("Rz", 1, 1, std::vector<int>{0}, 3.14);
//
BOOST_VERIFY(rz->getName() == "Rz");
BOOST_VERIFY(rz->getParameter(0) == 3.14);
}
......@@ -49,14 +49,14 @@ namespace xacc {
* the template parameter T.
*/
template<typename T, typename... TArgs>
class Registry : public Singleton<Registry<T>> {
class Registry : public Singleton<Registry<T, TArgs...>> {
protected:
/**
* Reference to the database of creation functions
* for classes of superclass type T.
*/
std::map<std::string, std::function<std::shared_ptr<T>()>> registry;
std::map<std::string, std::function<std::shared_ptr<T>(TArgs...)>> registry;
public:
......@@ -68,7 +68,7 @@ public:
* @param f
* @return
*/
bool add(const std::string& id, std::function<std::shared_ptr<T>()> f) {
bool add(const std::string& id, std::function<std::shared_ptr<T>(TArgs...)> f) {
bool s = registry.insert(std::make_pair(id, f)).second;
if (!s) {
XACCError("Could not add " + id + " to the Registry.");
......@@ -83,10 +83,10 @@ public:
* @param id
* @return
*/
std::shared_ptr<T> create(const std::string& id) {
std::shared_ptr<T> create(const std::string& id, TArgs... args) {
auto search = registry.find(id);
if (search != registry.end()) {
return registry[id]();
return registry[id](args...);
} else {
XACCError("Invalid Registry map id string - " + id);
}
......
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