QNLP  v1.0
QNLP::GateCache< SimulatorType > Class Template Reference

Class to cache intermediate matrix values used within other parts of the computation. Heavily depended upon by NCU to store sqrt matrix values following Barenco et al. (1995) decomposition. More...

#include <GateCache.hpp>

Collaboration diagram for QNLP::GateCache< SimulatorType >:
Collaboration graph

Public Types

using GateType = decltype(std::declval< SimulatorType >().getGateX())
 

Public Member Functions

 GateCache ()
 
 GateCache (SimulatorType &qSim)
 
 GateCache (SimulatorType &qSim, std::size_t default_depth)
 
 ~GateCache ()
 
void clearCache ()
 
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. More...
 
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. More...
 

Data Fields

std::unordered_map< std::string, std::vector< std::pair< GateType, GateType > > > gateCacheMap
 

Private Attributes

std::size_t cache_depth
 

Detailed Description

template<class SimulatorType>
class QNLP::GateCache< SimulatorType >

Class to cache intermediate matrix values used within other parts of the computation. Heavily depended upon by NCU to store sqrt matrix values following Barenco et al. (1995) decomposition.

Template Parameters
SimulatorTypeThe simulator type with SimulatorGeneral as base class

Definition at line 95 of file GateCache.hpp.

Member Typedef Documentation

◆ GateType

template<class SimulatorType>
using QNLP::GateCache< SimulatorType >::GateType = decltype(std::declval<SimulatorType>().getGateX())

Definition at line 114 of file GateCache.hpp.

Constructor & Destructor Documentation

◆ GateCache() [1/3]

template<class SimulatorType>
QNLP::GateCache< SimulatorType >::GateCache ( )
inline

Definition at line 100 of file GateCache.hpp.

100 : cache_depth(0) { };
std::size_t cache_depth
Definition: GateCache.hpp:97

◆ GateCache() [2/3]

template<class SimulatorType>
QNLP::GateCache< SimulatorType >::GateCache ( SimulatorType &  qSim)
inline

Definition at line 102 of file GateCache.hpp.

102  : GateCache() {
103  //initCache(qSim, 16);
104  //cache_depth = 16;
105  }

◆ GateCache() [3/3]

template<class SimulatorType>
QNLP::GateCache< SimulatorType >::GateCache ( SimulatorType &  qSim,
std::size_t  default_depth 
)
inline

Definition at line 107 of file GateCache.hpp.

107  : cache_depth(default_depth) {
108  initCache(qSim, cache_depth);
109  }
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.
Definition: GateCache.hpp:130
std::size_t cache_depth
Definition: GateCache.hpp:97

References QNLP::GateCache< SimulatorType >::cache_depth, and QNLP::GateCache< SimulatorType >::initCache().

Here is the call graph for this function:

◆ ~GateCache()

template<class SimulatorType>
QNLP::GateCache< SimulatorType >::~GateCache ( )
inline

Definition at line 111 of file GateCache.hpp.

111 { clearCache(); }

References QNLP::GateCache< SimulatorType >::clearCache().

Here is the call graph for this function:

Member Function Documentation

◆ addToCache()

template<class SimulatorType>
void QNLP::GateCache< SimulatorType >::addToCache ( SimulatorType &  sim,
const std::string  gateLabel,
const GateType gate,
std::size_t  max_depth 
)
inline

Adds new gate to the cache up to a given sqrt depth.

Parameters
gateLabelLabel of gate to index into map
gateGate matrix
max_depthDepth of calculations for sqrt and associate adjoints

Definition at line 165 of file GateCache.hpp.

165  {
166  if(max_depth <= cache_depth && gateCacheMap.find(gateLabel) != gateCacheMap.end() ){
167  return;
168  }
169  else if(max_depth > cache_depth){
171  }
172 
173  std::vector< std::pair<GateType, GateType> > v;
174 
175  v.reserve(max_depth + 1);
176  v.push_back(std::make_pair( gate, adjointMatrix( gate ) ) );
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 ) ) );
180  }
181  gateCacheMap.emplace(std::make_pair(gateLabel, v) );
182  }
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.
Definition: GateCache.hpp:130
Mat2x2Type adjointMatrix(const Mat2x2Type &U)
Function to calculate the adjoint of an input matrix.
Definition: mat_ops.hpp:54
std::size_t cache_depth
Definition: GateCache.hpp:97
std::unordered_map< std::string, std::vector< std::pair< GateType, GateType > > > gateCacheMap
Definition: GateCache.hpp:117

References QNLP::adjointMatrix(), QNLP::GateCache< SimulatorType >::cache_depth, QNLP::GateCache< SimulatorType >::gateCacheMap, QNLP::GateCache< SimulatorType >::initCache(), CircuitPrinter.circuit_printer::max_depth, and ncu_opt_tester::sim.

Here is the call graph for this function:

◆ clearCache()

template<class SimulatorType>
void QNLP::GateCache< SimulatorType >::clearCache ( )
inline

Definition at line 119 of file GateCache.hpp.

119  {
120  gateCacheMap.clear();
121  cache_depth = 0;
122  }
std::size_t cache_depth
Definition: GateCache.hpp:97
std::unordered_map< std::string, std::vector< std::pair< GateType, GateType > > > gateCacheMap
Definition: GateCache.hpp:117

References QNLP::GateCache< SimulatorType >::cache_depth, and QNLP::GateCache< SimulatorType >::gateCacheMap.

Referenced by QNLP::GateCache< SimulatorType >::~GateCache().

Here is the caller graph for this function:

◆ initCache()

template<class SimulatorType>
void QNLP::GateCache< SimulatorType >::initCache ( SimulatorType &  sim,
std::size_t  sqrt_depth 
)
inline

Initialise the gate cache with PauliX,Y,Z and H up to a given sqrt depth.

Parameters
simThe simulator object
sqrt_depthThe depth to which calculate sqrt matrices and their respective adjoints

Definition at line 130 of file GateCache.hpp.

130  {
131  // If we do not have a sufficient circuit depth, clear and rebuild up to given depth.
132  // Could ideally use existing values and rebuild from lowest depth, but consider this
133  // after we have a working implementation.
134 
135  if(cache_depth < sqrt_depth ){
136  gateCacheMap.clear();
137  cache_depth = 0;
138  }
139 
140  if (gateCacheMap.empty()){
141 
142  gateCacheMap["X"] = std::vector< std::pair<GateType, GateType> > { std::make_pair( sim.getGateX(), adjointMatrix( sim.getGateX() ) ) };
143  gateCacheMap["Y"] = std::vector< std::pair<GateType, GateType> > { std::make_pair( sim.getGateY(), adjointMatrix( sim.getGateY() ) ) };
144  gateCacheMap["Z"] = std::vector< std::pair<GateType, GateType> > { std::make_pair( sim.getGateZ(), adjointMatrix( sim.getGateZ() ) ) };
145  gateCacheMap["H"] = std::vector< std::pair<GateType, GateType> > { std::make_pair( sim.getGateH(), adjointMatrix( sim.getGateH() ) ) };
146 
147  for( std::size_t depth = 1; depth <= sqrt_depth; depth++ ){
148  for( auto& kv : gateCacheMap ){
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 ) ) );
152  }
153  }
154  cache_depth = sqrt_depth;
155  }
156  }
Mat2x2Type adjointMatrix(const Mat2x2Type &U)
Function to calculate the adjoint of an input matrix.
Definition: mat_ops.hpp:54
std::size_t cache_depth
Definition: GateCache.hpp:97
std::unordered_map< std::string, std::vector< std::pair< GateType, GateType > > > gateCacheMap
Definition: GateCache.hpp:117

References QNLP::adjointMatrix(), QNLP::GateCache< SimulatorType >::cache_depth, QNLP::GateCache< SimulatorType >::gateCacheMap, and ncu_opt_tester::sim.

Referenced by QNLP::GateCache< SimulatorType >::addToCache(), and QNLP::GateCache< SimulatorType >::GateCache().

Here is the call graph for this function:
Here is the caller graph for this function:

Field Documentation

◆ cache_depth

◆ gateCacheMap

template<class SimulatorType>
std::unordered_map<std::string, std::vector< std::pair<GateType, GateType> > > QNLP::GateCache< SimulatorType >::gateCacheMap

The documentation for this class was generated from the following file: