Tomographer  v2.0
Tomographer C++ Framework Documentation
tspacellhwalker.h
Go to the documentation of this file.
1 /* This file is part of the Tomographer project, which is distributed under the
2  * terms of the MIT license.
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2016 ETH Zurich, Institute for Theoretical Physics, Philippe Faist
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26 
27 #ifndef TOMOGRAPHER_DENSEDM_TSPACELLHWALKER_H
28 #define TOMOGRAPHER_DENSEDM_TSPACELLHWALKER_H
29 
30 #include <cstddef>
31 #include <cmath>
32 
33 #include <random>
34 
35 #include <boost/math/constants/constants.hpp>
36 
42 #include <tomographer2/mhrw.h>
43 
52 namespace Tomographer {
53 namespace DenseDM {
54 namespace TSpace {
55 
70 template<typename DenseLLH_, typename Rng, typename LoggerType>
72  : public Tools::NeedOwnOperatorNew<typename DenseLLH_::DMTypes::MatrixType>::ProviderType
73 {
74 public:
76  typedef DenseLLH_ DenseLLH;
78  typedef typename DenseLLH::DMTypes DMTypes;
80  typedef typename DenseLLH::LLHValueType LLHValueType;
82  typedef typename DMTypes::MatrixType MatrixType;
86  typedef typename DMTypes::RealScalar RealScalar;
89 
93  typedef RealScalar StepRealType;
94 
96  typedef MatrixType PointType;
98  typedef LLHValueType FnValueType;
100  enum {
105  };
106 
107 private:
108 
109  const DenseLLH & _llh;
110  Rng & _rng;
111  std::normal_distribution<RealScalar> _normal_distr_rnd;
112  const ParamX<DMTypes> _px;
113 
114  LoggerType & _log;
115 
116  MatrixType _startpt;
117 
118 public:
119 
130  LLHMHWalker(const MatrixType & startpt, const DenseLLH & llh, Rng & rng,
131  LoggerType & log_)
132  : _llh(llh),
133  _rng(rng),
134  _normal_distr_rnd(0.0, 1.0),
135  _px(llh.dmt),
136  _log(log_),
137  _startpt(startpt)
138  {
139  }
140 
145  LLHMHWalker(LLHMHWalker && moveother)
146  : _llh(std::move(moveother._llh)),
147  _rng(moveother._rng),
148  _normal_distr_rnd(moveother._normal_distr_rnd),
149  _px(std::move(moveother._px)),
150  _log(moveother._log),
151  _startpt(moveother._startpt)
152  {
153  }
154 
155 
165  inline void init()
166  {
167  _log.debug("TSpace::LLHMHWalker", "Starting random walk");
168  }
169 
171  inline const MatrixType & startPoint()
172  {
173  // It's fine to hard-code "1e-3" because for any type, valid T-matrices have norm == 1
174  if (_startpt.norm() > 1e-3) {
175  // nonzero matrix given: that's the starting point.
176  return _startpt;
177  }
178 
179  // zero matrix given: means to choose random starting point
180  MatrixType T(_llh.dmt.initMatrixType());
181  T = Tools::denseRandom<MatrixType>(
182  _rng, _normal_distr_rnd, _llh.dmt.dim(), _llh.dmt.dim()
183  );
184  _startpt = T/T.norm(); // normalize to be on surface of the sphere
185 
186  _log.debug("TSpace::LLHMHWalker", [&T](std::ostream & str) {
187  str << "Chosen random start point T = \n" << T;
188  });
189 
190  // return start point
191  return _startpt;
192  }
193 
195  inline void thermalizingDone()
196  {
197  }
198 
200  inline void done()
201  {
202  }
203 
211  TOMOGRAPHER_ENABLED_IF(DenseLLH::LLHCalcType == LLHCalcTypeX)
212  inline LLHValueType fnLogVal(const MatrixType & T) const
213  {
214  MatrixType rho(T*T.adjoint());
215  VectorParamType x = _px.HermToX(rho);
216  LLHValueType llhval = _llh.logLikelihoodX(x);
217  // _log.longdebug("fnlogval(%s) = %g\n", streamcstr(x.transpose()), llhval);
218  return llhval;
219  }
220 
228  TOMOGRAPHER_ENABLED_IF(DenseLLH::LLHCalcType == LLHCalcTypeRho)
229  inline LLHValueType fnLogVal(const MatrixType & T) const
230  {
231  LLHValueType llhval = _llh.logLikelihoodRho(T*T.adjoint());
232  // _log.longdebug("fnlogval( %s ) = %g\n", streamcstr(T*T.adjoint()), llhval);
233  return llhval;
234  }
235 
236 
238  inline MatrixType jumpFn(const MatrixType& cur_T, RealScalar step_size)
239  {
240  MatrixType DeltaT(Tools::denseRandom<MatrixType>(
241  _rng, _normal_distr_rnd, _llh.dmt.dim(), _llh.dmt.dim()
242  ));
243 
244  MatrixType new_T(cur_T + step_size * DeltaT);
245 
246  // renormalize to "project" onto the large T-space sphere
247  new_T /= new_T.norm(); //Matrix<>.norm() is Frobenius norm.
248 
249  // _log.longdebug("jump_fn(): step_size=%g, cur_T =\n%s\nDeltaT = \n%s\nnew_T = \n%s",
250  // step_size, streamstr(cur_T).c_str(), streamstr(DeltaT).c_str(),
251  // streamstr(new_T).c_str());
252 
253  // hope for Return Value Optimization by compiler
254  return new_T;
255  }
256 
257 };
258 
259 
260 
261 } // namespace TSpace
262 } // namespace DenseDM
263 } // namespace Tomographer
264 
265 
266 #endif
VectorParamType HermToX(MatrixTypeConstRef Herm) const
Get the X-parameterization corresponding to a given hermitian matrix.
Definition: param_herm_x.h:182
void init()
Provided for MHRandomWalk. Initializes some fields and prepares for a random walk.
Base namespace for the Tomographer project.
Definition: densellh.h:44
const MatrixType & startPoint()
Return the starting point given in the constructor, or a random start point.
DMTypes::VectorParamType VectorParamType
Type of an X-parameterization of a density operator (see X Parameterization)
C++ types for describing dense density matrices in various parameterizations.
DenseLLH::DMTypes DMTypes
The data types of our problem.
Provide appropriate operator new() definitions for a structure which has a member of the given stored...
STL namespace.
LLHMHWalker(LLHMHWalker &&moveother)
A MHWalker Interface needs to have a move constructor.
LLHMHWalker(const MatrixType &startpt, const DenseLLH &llh, Rng &rng, LoggerType &log_)
Constructor which just initializes the given fields.
STL class.
DMTypes::RealScalar RealScalar
The real scalar corresponding to our data types. Usually a double.
DenseLLH::LLHValueType LLHValueType
The loglikelihood function value type (see DenseLLH Interface e.g. IndepMeasLLH)
RealScalar StepRealType
The real scalar type for the step size — this is our scalar type (see MHWalker Interface) ...
MatrixType PointType
Provided for MHRandomWalk. A point in our random walk = a density matrix.
Tools for parameterizing hermitian matrices with the X Parameterization.
Some declarations for the DenseLLH type interface.
A random walk in the density matrix space of a Hilbert state space of a quantum system.
Provides the logarithm MH function value at each point (see Role of UseFnSyntaxType) ...
Definition: mhrw.h:66
void done()
Callback for after random walk is finished. No-op.
LLHValueType fnLogVal(const MatrixType &T) const
Calculate the logarithm of the Metropolis-Hastings function value.
DenseLLH_ DenseLLH
The DenseLLH interface object type.
Managing the need for specific overrides to operator new() for some types (especially Eigen types) ...
void thermalizingDone()
Callback for after thermalizing is done. No-op.
LLHValueType FnValueType
Provided for MHRandomWalk. The function value type is the loglikelihood value type.
The DenseLLH-compatible object exposes a method logLikelihoodRho(), taking as argument a (const ref t...
Definition: densellh.h:60
MatrixType jumpFn(const MatrixType &cur_T, RealScalar step_size)
Decides of a new point to jump to for the random walk.
We will calculate the log-likelihood function, which is the logarithm of the Metropolis-Hastings func...
Routines for performing a Metropolis-Hastings random walk.
DMTypes::MatrixType MatrixType
The matrix type for a density operator on our quantum system.
STL class.
DMTypes::ComplexScalar ComplexScalar
The complex real scalar corresponding to our data types. Usually a std::complex<double>.
The DenseLLH-compatible object exposes a method logLikelihoodX(), taking as argument a (const ref to ...
Definition: densellh.h:65
Utilities for logging messages.
RealScalar_ RealScalar
Real scalar type, given in template parameter. Usually double is fine.
Definition: dmtypes.h:119