10 #define QNLP_GATECACHE 12 #include <unordered_map> 13 #include <unordered_set> 37 template <
class fpType>
38 inline bool fpComp(fpType theta0, fpType theta1) {
39 return std::fabs(theta0 - theta1) < std::numeric_limits<fpType>::epsilon();
83 ^ (std::hash<double>{}(gmd.
theta) << 1)) >> 1)
84 ^ (std::hash<std::size_t>{}(gmd.
sqrt_depth) << 1);
94 template <
class SimulatorType>
114 using GateType = decltype(std::declval<SimulatorType>().getGateX());
117 std::unordered_map<std::string, std::vector< std::pair<GateType, GateType> > >
gateCacheMap;
147 for( std::size_t depth = 1; depth <= sqrt_depth; depth++ ){
149 kv.second.reserve(sqrt_depth + 1);
150 auto m = matrixSqrt<GateType>(kv.second[depth-1].first);
151 kv.second.emplace(kv.second.begin() + depth, std::make_pair( m,
adjointMatrix( m ) ) );
173 std::vector< std::pair<GateType, GateType> > v;
177 for( std::size_t depth = 1; depth <=
max_depth; depth++ ){
178 auto m = matrixSqrt<GateType>( v[depth-1].first );
179 v.emplace(v.begin() + depth, std::make_pair( m,
adjointMatrix( m ) ) );
Meta container object for gate caching.
void initCache(SimulatorType &sim, std::size_t sqrt_depth)
Initialise the gate cache with PauliX,Y,Z and H up to a given sqrt depth.
Class to cache intermediate matrix values used within other parts of the computation....
Mat2x2Type adjointMatrix(const Mat2x2Type &U)
Function to calculate the adjoint of an input matrix.
Templated methods to manipulate small matrices.
GateCache(SimulatorType &qSim)
bool operator==(const GateMetaData &other) const
GateMetaData(std::string labelGate="", std::size_t sqrt_depth=0, bool adjoint=false, double theta=0.0)
void addToCache(SimulatorType &sim, const std::string gateLabel, const GateType &gate, std::size_t max_depth)
Adds new gate to the cache up to a given sqrt depth.
decltype(std::declval< SimulatorType >().getGateX()) GateType
std::unordered_map< std::string, std::vector< std::pair< GateType, GateType > > > gateCacheMap
std::size_t operator()(const GateMetaData &gmd) const
bool fpComp(fpType theta0, fpType theta1)
Comparison of floating point values within the given machine epsilon.
Hashing function for GateMetaData objects, to allow storage in unordered_map. Taken from default docs...
GateCache(SimulatorType &qSim, std::size_t default_depth)
friend std::ostream & operator<<(std::ostream &os, const GateMetaData &gmd)