28 #ifndef TOMOGRAPHER_DENSEDM_TSPACELLHWALKER_H 29 #define TOMOGRAPHER_DENSEDM_TSPACELLHWALKER_H 36 #include <boost/math/constants/constants.hpp> 60 namespace tomo_internal {
62 template<
typename DenseLLHType,
typename =
void>
63 struct DenseLLHInvoker
65 typedef typename DenseLLHType::DMTypes
DMTypes;
68 typedef typename DenseLLHType::LLHValueType LLHValueType;
70 const DenseLLHType & llh;
71 const Tools::StoreIfEnabled<const ParamX<DMTypes>, ((int)DenseLLHType::LLHCalcType == (
int)
LLHCalcTypeX)> param_x;
73 DenseLLHInvoker(const DenseLLHType & llh_)
81 TOMOGRAPHER_ENABLED_IF(DenseLLHType::LLHCalcType ==
LLHCalcTypeX)
82 inline LLHValueType fnLogVal(const MatrixType & T)
const 84 MatrixType rho(T*T.adjoint());
85 VectorParamType x = param_x.value.HermToX(rho);
86 LLHValueType llhval = llh.logLikelihoodX(x);
92 TOMOGRAPHER_ENABLED_IF(DenseLLHType::LLHCalcType ==
LLHCalcTypeRho)
93 inline LLHValueType fnLogVal(const MatrixType & T)
const 95 LLHValueType llhval = llh.logLikelihoodRho(T*T.adjoint());
119 template<
typename DenseLLHType_,
typename RngType_,
typename LoggerType_>
132 typedef typename DenseLLHType::DMTypes
DMTypes;
163 const DenseLLHType & _llh;
164 const tomo_internal::DenseLLHInvoker<DenseLLHType> _llhinvoker;
184 LLHMHWalker(
const MatrixType & startpt,
const DenseLLHType & llh, RngType & rng, LoggerType & log_)
188 _normal_distr_rnd(0.0, 1.0),
206 _log.debug(
"TSpace::LLHMHWalker",
"Starting random walk");
213 if (_startpt.norm() > 1e-3) {
219 MatrixType T(_llh.dmt.initMatrixType());
220 T = Tools::denseRandom<MatrixType>(
221 _rng, _normal_distr_rnd, (Eigen::Index)_llh.dmt.dim(), (Eigen::Index)_llh.dmt.dim()
223 _startpt = T/T.norm();
225 _log.debug(
"TSpace::LLHMHWalker", [&](
std::ostream & str) {
226 str <<
"Chosen random start point T = \n" << _startpt;
248 inline LLHValueType
fnLogVal(
const MatrixType & T)
const 250 return _llhinvoker.fnLogVal(T);
254 inline MatrixType
jumpFn(
const MatrixType& cur_T, WalkerParams params)
256 MatrixType DeltaT(Tools::denseRandom<MatrixType>(
257 _rng, _normal_distr_rnd,
258 (Eigen::Index)_llh.dmt.dim(), (Eigen::Index)_llh.dmt.dim()
261 MatrixType new_T(cur_T + params.step_size * DeltaT);
264 new_T /= new_T.norm();
306 template<
typename StepRealType>
340 template<
typename DenseLLHType_,
typename RngType_,
typename LoggerType_>
356 typedef typename DenseLLHType::DMTypes
DMTypes;
388 const DenseLLHType & _llh;
389 const tomo_internal::DenseLLHInvoker<DenseLLHType> _llhinvoker;
411 LLHMHWalkerLight(
const MatrixType & startpt,
const DenseLLHType & llh, RngType & rng, LoggerType & baselogger)
415 _normal_distr_rnd(0.0, 1.0),
416 _jumptype_distr_rnd(0, 2),
417 _jumpdir_distr_rnd(0, (
Eigen::Index)llh.dmt.dim2()-1),
418 _llogger(
"Tomographer::DenseDM::TSpace::LLHMHWalkerLight", baselogger),
436 logger.debug(
"Starting random walk");
445 if (_startpt.norm() > 1e-3) {
451 MatrixType T(_llh.dmt.initMatrixType());
452 T = Tools::denseRandom<MatrixType>(
453 _rng, _normal_distr_rnd, (Eigen::Index)_llh.dmt.dim(), (Eigen::Index)_llh.dmt.dim()
455 _startpt = T/T.norm();
458 str <<
"Chosen random start point T = \n" << _startpt;
480 inline LLHValueType
fnLogVal(
const MatrixType & T)
const 482 return _llhinvoker.fnLogVal(T);
488 MatrixType new_T(cur_T);
493 for (
int j = 0; j < (int)_llh.dmt.dim(); ++j) {
496 Eigen::Index k1 = _jumpdir_distr_rnd(_rng);
498 do { k2 = _jumpdir_distr_rnd(_rng); }
while (k1 == k2);
502 RealScalar sina = params.step_size * _normal_distr_rnd(_rng);
503 if (sina < -1) { sina = -1; }
504 if (sina > 1) { sina = 1; }
505 RealScalar cosa =
std::sqrt(1 - sina*sina);
511 tr2d(0,0) = cosa; tr2d(0,1) = ComplexScalar(0,sina);
512 tr2d(1,0) = ComplexScalar(0,sina); tr2d(1,1) = cosa;
515 tr2d(0,0) = cosa; tr2d(0,1) = sina;
516 tr2d(1,0) = -sina; tr2d(1,1) = cosa;
519 tr2d(0,0) = ComplexScalar(cosa, sina); tr2d(0,1) = 0;
520 tr2d(1,0) = 0; tr2d(1,1) = ComplexScalar(cosa, -sina);
526 const Eigen::Index i1 = k1 / (Eigen::Index)_llh.dmt.dim();
527 const Eigen::Index j1 = k1 % (Eigen::Index)_llh.dmt.dim();
528 const Eigen::Index i2 = k2 / (Eigen::Index)_llh.dmt.dim();
529 const Eigen::Index j2 = k2 % (Eigen::Index)_llh.dmt.dim();
532 stream <<
"Elementary jump rotation: " 533 <<
"k1="<<k1<<
" -> i1="<<i1<<
" j1="<<j1<<
" k2="<<k2<<
" -> i2="<<i2<<
" j2="<<j2<<
"\n" 537 const auto x = tr2d(0,0) * new_T(i1,j1) + tr2d(0,1) * new_T(i2,j2) ;
538 const auto y = tr2d(1,0) * new_T(i1,j1) + tr2d(1,1) * new_T(i2,j2) ;
544 new_T /= new_T.norm();
MatrixType jumpFn(const MatrixType &cur_T, WalkerParams params)
Decides of a new point to jump to for the random walk.
LoggerType_ LoggerType
The logger type.
MatrixType PointType
Provided for MHRandomWalk. A point in our random walk = a density matrix.
Tomographer::DenseDM::DMTypes< Eigen::Dynamic, RealScalar > DMTypes
The Tomographer::DenseDM::DMTypes we should use by default, with dynamic sized matrices.
const MatrixType & startPoint()
Return the starting point given in the constructor, or a random start point.
Eigen::Matrix< RealScalar, FixedDim2, 1 > VectorParamType
Real vector with dim*dim elements.
MHWalkerParamsStepSize< RealScalar > WalkerParams
The real scalar type for the step size — this scalar type is the only parameter we need (see MHWalke...
Base namespace for the Tomographer project.
MatrixType PointType
Provided for MHRandomWalk. A point in our random walk = a density matrix.
LoggerType_ LoggerType
The logger type.
C++ types for describing dense density matrices in various parameterizations.
DMTypes::RealScalar RealScalar
The real scalar corresponding to our data types. Usually a double.
RngType_ RngType
The random number generator type.
DenseLLHType_ DenseLLHType
The DenseLLH interface object type.
DMTypes::VectorParamType VectorParamType
Type of an X-parameterization of a density operator (see X Parameterization)
An MHWalkerParams type which just stores a step size.
void init()
Provided for MHRandomWalk. Initializes some fields and prepares for a random walk.
MatrixType jumpFn(const MatrixType &cur_T, WalkerParams params)
Decides of a new point to jump to for the random walk.
DMTypes::MatrixType MatrixType
The matrix type for a density operator on our quantum system.
LLHMHWalkerLightParams< RealScalar > WalkerParams
The real scalar type for the step size — this scalar type is the only parameter we need (see MHWalke...
void done()
Callback for after random walk is finished. No-op.
#define TOMO_ORIGIN
Use this as argument for a Tomographer::Logger::LocalLogger constructor .
LocalLogger< LocalLogger< BaseLoggerType > > subLogger(std::string new_prefix)
Create a sub-logger.
DMTypes::MatrixType MatrixType
The matrix type for a density operator on our quantum system.
DMTypes::ComplexScalar ComplexScalar
The complex real scalar corresponding to our data types. Usually a std::complex<double>.
DenseLLHType::LLHValueType LLHValueType
The loglikelihood function value type (see DenseLLH Interface e.g. IndepMeasLLH)
DenseLLHType::DMTypes DMTypes
The data types of our problem.
LLHMHWalker(const MatrixType &startpt, const DenseLLHType &llh, RngType &rng, LoggerType &log_)
Constructor which just initializes the given fields.
Provides the logarithm MH function value at each point (see Role of UseFnSyntaxType) ...
void init()
Provided for MHRandomWalk. Initializes some fields and prepares for a random walk.
Tools for parameterizing hermitian matrices with the X Parameterization.
Some declarations for the DenseLLH type interface.
LLHValueType fnLogVal(const MatrixType &T) const
Calculate the logarithm of the Metropolis-Hastings function value.
A random walk in the density matrix space of a Hilbert state space of a quantum system (uniform rando...
Managing the need for specific overrides to operator new() for some types (especially Eigen types) ...
RngType_ RngType
The random number generator type.
LLHMHWalkerLight(const MatrixType &startpt, const DenseLLHType &llh, RngType &rng, LoggerType &baselogger)
Constructor which just initializes the given fields.
The DenseLLH-compatible object exposes a method logLikelihoodRho(), taking as argument a (const ref t...
Eigen::Matrix< ComplexScalar, FixedDim, FixedDim, Eigen::Matrix< ComplexScalar, FixedDim, FixedDim >::Options, MaxFixedDim_, MaxFixedDim_ > MatrixType
Matrix type, to store the density operator as a dense matrix.
A random walk in the density matrix space of a Hilbert state space of a quantum system (elementary ro...
LLHValueType FnValueType
Provided for MHRandomWalk. The function value type is the loglikelihood value type.
DenseLLHType::LLHValueType LLHValueType
The loglikelihood function value type (see DenseLLH Interface e.g. IndepMeasLLH)
void done()
Callback for after random walk is finished. No-op.
DMTypes::ComplexScalar ComplexScalar
The complex real scalar corresponding to our data types. Usually a std::complex<double>.
DenseLLHType::DMTypes DMTypes
The data types of our problem.
DenseLLHType_ DenseLLHType
The DenseLLH interface object type.
void thermalizingDone()
Callback for after thermalizing is done. No-op.
DMTypes::RealScalar RealScalar
The real scalar corresponding to our data types. Usually a double.
Routines for performing a Metropolis-Hastings random walk.
const MatrixType & startPoint()
Return the starting point given in the constructor, or a random start point.
LLHValueType fnLogVal(const MatrixType &T) const
Calculate the logarithm of the Metropolis-Hastings function value.
#define tomographer_assert(...)
Assertion test macro.
The DenseLLH-compatible object exposes a method logLikelihoodX(), taking as argument a (const ref to ...
void thermalizingDone()
Callback for after thermalizing is done. No-op.
Utilities for logging messages.
LLHValueType FnValueType
Provided for MHRandomWalk. The function value type is the loglikelihood value type.
RealScalar_ RealScalar
Real scalar type, given in template parameter. Usually double is fine.
DMTypes::VectorParamType VectorParamType
Type of an X-parameterization of a density operator (see X Parameterization)