Skip to content

Commit

Permalink
Re-add dispatch table (#68)
Browse files Browse the repository at this point in the history
* Re-add dispatch table

* Update changelog
  • Loading branch information
ThomasLoke authored Mar 1, 2021
1 parent 4d182bc commit 7ce6aca
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 132 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,22 @@

### Improvements

* Add new lightweight backend with performance improvements
[(#57)](https://github.com/PennyLaneAI/pennylane-lightning/pull/57)

### Documentation

### Bug fixes

* Re-add dispatch table after fixing static initialisation order issue
[(#68)](https://github.com/PennyLaneAI/pennylane-lightning/pull/68)

### Contributors

This release contains contributions from (in alphabetical order):

Thomas Loke

---

# Release 0.14.1
Expand Down
2 changes: 1 addition & 1 deletion pennylane_lightning/src/rework/Apply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <set>

#include "Apply.hpp"
#include "GateFactory.hpp"
#include "Gates.hpp"
#include "StateVector.hpp"
#include "Util.hpp"

Expand Down
90 changes: 0 additions & 90 deletions pennylane_lightning/src/rework/GateFactory.cpp

This file was deleted.

37 changes: 0 additions & 37 deletions pennylane_lightning/src/rework/GateFactory.hpp

This file was deleted.

51 changes: 49 additions & 2 deletions pennylane_lightning/src/rework/Gates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@
#define _USE_MATH_DEFINES

#include <cmath>
#include <functional>
#include <map>

#include "Gates.hpp"
#include "typedefs.hpp"
#include "Util.hpp"

using Pennylane::CplxType;
using std::function;
using std::map;
using std::string;
using std::unique_ptr;
using std::vector;

using Pennylane::CplxType;

template<class T>
static void validateLength(const string& errorPrefix, const vector<T>& vec, int requiredLength) {
if (vec.size() != requiredLength)
Expand Down Expand Up @@ -375,3 +380,45 @@ const std::vector<CplxType> Pennylane::CSWAPGate::matrix{
0, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1 };

// -------------------------------------------------------------------------------------------------------------

template<class GateType>
static void addToDispatchTable(map<string, function<unique_ptr<Pennylane::AbstractGate>(const vector<double>&)>>& dispatchTable) {
dispatchTable.emplace(GateType::label, [](const vector<double>& parameters) { return std::make_unique<GateType>(GateType::create(parameters)); });
}

static map<string, function<unique_ptr<Pennylane::AbstractGate>(const vector<double>&)>> createDispatchTable() {
map<string, function<unique_ptr<Pennylane::AbstractGate>(const vector<double>&)>> dispatchTable;
addToDispatchTable<Pennylane::XGate>(dispatchTable);
addToDispatchTable<Pennylane::YGate>(dispatchTable);
addToDispatchTable<Pennylane::ZGate>(dispatchTable);
addToDispatchTable<Pennylane::HadamardGate>(dispatchTable);
addToDispatchTable<Pennylane::SGate>(dispatchTable);
addToDispatchTable<Pennylane::TGate>(dispatchTable);
addToDispatchTable<Pennylane::RotationXGate>(dispatchTable);
addToDispatchTable<Pennylane::RotationYGate>(dispatchTable);
addToDispatchTable<Pennylane::RotationZGate>(dispatchTable);
addToDispatchTable<Pennylane::PhaseShiftGate>(dispatchTable);
addToDispatchTable<Pennylane::GeneralRotationGate>(dispatchTable);
addToDispatchTable<Pennylane::CNOTGate>(dispatchTable);
addToDispatchTable<Pennylane::SWAPGate>(dispatchTable);
addToDispatchTable<Pennylane::CZGate>(dispatchTable);
addToDispatchTable<Pennylane::CRotationXGate>(dispatchTable);
addToDispatchTable<Pennylane::CRotationYGate>(dispatchTable);
addToDispatchTable<Pennylane::CRotationZGate>(dispatchTable);
addToDispatchTable<Pennylane::CGeneralRotationGate>(dispatchTable);
addToDispatchTable<Pennylane::ToffoliGate>(dispatchTable);
addToDispatchTable<Pennylane::CSWAPGate>(dispatchTable);
return dispatchTable;
}

static const map<string, function<unique_ptr<Pennylane::AbstractGate>(const vector<double>&)>> dispatchTable = createDispatchTable();

unique_ptr<Pennylane::AbstractGate> Pennylane::constructGate(const string& label, const vector<double>& parameters) {
auto dispatchTableIterator = dispatchTable.find(label);
if (dispatchTableIterator == dispatchTable.end())
throw std::invalid_argument(label + " is not a supported gate type");

return dispatchTableIterator->second(parameters);
}
11 changes: 11 additions & 0 deletions pennylane_lightning/src/rework/Gates.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
#pragma once

#include <memory>
#include <vector>

#include "typedefs.hpp"
Expand Down Expand Up @@ -298,4 +299,14 @@ namespace Pennylane {
}
};

/**
* Produces the requested gate, defined by a label and the list of parameters
*
* @param label unique string corresponding to a gate type
* @param parameters defines the gate parameterisation (may be zero-length for some gates)
* @return the gate wrapped in std::unique_ptr
* @throws std::invalid_argument thrown if the gate type is not defined, or if the number of parameters to the gate is incorrect
*/
std::unique_ptr<AbstractGate> constructGate(const std::string& label, const std::vector<double>& parameters);

}
2 changes: 0 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,11 @@ def build_extensions(self):
"lightning_qubit_new_ops",
sources=[
"pennylane_lightning/src/rework/Apply.cpp",
"pennylane_lightning/src/rework/GateFactory.cpp",
"pennylane_lightning/src/rework/Gates.cpp",
"pennylane_lightning/src/rework/StateVector.cpp",
],
depends=[
"pennylane_lightning/src/rework/Apply.hpp",
"pennylane_lightning/src/rework/GateFactory.hpp",
"pennylane_lightning/src/rework/Gates.hpp",
"pennylane_lightning/src/rework/StateVector.hpp",
"pennylane_lightning/src/rework/typedefs.hpp",
Expand Down

0 comments on commit 7ce6aca

Please sign in to comment.