Tomographer  v1.0a
Tomographer C++ Framework Documentation
dmmhrw.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) 2015 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 DMLLHINTEGRATOR_H
28 #define DMLLHINTEGRATOR_H
29 
30 #include <cstddef>
31 #include <cmath>
32 
33 #include <random>
34 
35 #include <tomographer/qit/util.h>
36 #include <tomographer/qit/dist.h>
39 #include <tomographer/mhrw.h>
40 
41 #include <boost/math/constants/constants.hpp>
42 
51 namespace Tomographer {
52 
53 
69 template<typename TomoProblem, typename Rng, typename Log>
71 {
72 public:
74  typedef typename TomoProblem::MatrQ MatrQ;
76  typedef typename TomoProblem::LLHValueType LLHValueType;
78  typedef typename MatrQ::MatrixType MatrixType;
82  typedef typename MatrQ::RealScalar RealScalar;
85 
87  typedef MatrixType PointType;
89  typedef LLHValueType FnValueType;
90  enum {
95  };
96 
97 private:
98 
99  const TomoProblem & _tomo;
100  Rng & _rng;
101  std::normal_distribution<RealScalar> _normal_distr_rnd;
102 
103  Log & _log;
104 
105  MatrixType _startpt;
106 
107 public:
108 
114  DMStateSpaceLLHMHWalker(const MatrixType & startpt, const TomoProblem & tomo, Rng & rng,
115  Log & log_)
116  : _tomo(tomo),
117  _rng(rng),
118  _normal_distr_rnd(0.0, 1.0),
119  _log(log_),
120  _startpt(startpt)
121  {
122  }
123 
133  inline void init()
134  {
135  _log.debug("DMStateSpaceLLHMHWalker", "Starting random walk");
136  }
137 
139  inline const MatrixType & startpoint()
140  {
141  if (_startpt.norm() > 1e-3) {
142  // nonzero matrix given: that's the starting point
143  return _startpt;
144  }
145 
146  // zero matrix given: means to choose random starting point
147  MatrixType T = _tomo.matq.initMatrixType();
148  T = dense_random<MatrixType>(
149  _rng, _normal_distr_rnd, _tomo.matq.dim(), _tomo.matq.dim()
150  );
151  _startpt = T/T.norm(); // normalize to be on surface of the sphere
152 
153  _log.debug("DMStateSpaceLLHMHWalker", [&T](std::ostream & str) {
154  str << "Chosen random start point T = \n" << T;
155  });
156 
157  // return start point
158  return _startpt;
159  }
160 
162  inline void thermalizing_done()
163  {
164  }
165 
167  inline void done()
168  {
169  }
170 
177  inline LLHValueType fnlogval(const MatrixType & T)
178  {
179  MatrixType rho = _tomo.matq.initMatrixType();
180  rho.noalias() = T*T.adjoint();
181 
182  VectorParamType x = _tomo.matq.initVectorParamType();
183  param_herm_to_x(x, rho);
184 
185  LLHValueType llhval = -boost::math::constants::half<RealScalar>() * _tomo.calc_llh(x);
186  // _log.longdebug("fnlogval(%s) = %g\n", streamcstr(x.transpose()), llhval);
187  return llhval;
188  }
189 
191  inline MatrixType jump_fn(const MatrixType& cur_T, RealScalar step_size)
192  {
193  MatrixType DeltaT = dense_random<MatrixType>(
194  _rng, _normal_distr_rnd, _tomo.matq.dim(), _tomo.matq.dim()
195  );
196 
197  MatrixType new_T = _tomo.matq.initMatrixType();
198 
199  new_T.noalias() = cur_T + step_size * DeltaT;
200 
201  // renormalize to "project" onto the large T-space sphere
202  new_T /= new_T.norm(); //Matrix<>.norm() is Frobenius norm.
203 
204  // _log.longdebug("jump_fn(): step_size=%g, cur_T =\n%s\nDeltaT = \n%s\nnew_T = \n%s",
205  // step_size, streamstr(cur_T).c_str(), streamstr(DeltaT).c_str(),
206  // streamstr(new_T).c_str());
207 
208  // hope for Return Value Optimization by compiler
209  return new_T;
210  }
211 
212 };
213 
214 
215 
216 
217 
221 template<typename TomoProblem_, typename FidValueType_ = double>
223 {
224 public:
225  typedef TomoProblem_ TomoProblem;
226  typedef typename TomoProblem::MatrQ MatrQ;
227  typedef typename MatrQ::MatrixType MatrixType;
228 
230  typedef FidValueType_ ValueType;
231 
232 private:
233  MatrixType _ref_T;
234 
235 public:
237  FidelityToRefCalculator(const TomoProblem & tomo)
238  : _ref_T(tomo.matq.initMatrixType())
239  {
240  _ref_T = tomo.T_MLE;
241  }
243  FidelityToRefCalculator(const TomoProblem & tomo, const MatrixType & T_ref)
244  : _ref_T(tomo.matq.initMatrixType())
245  {
246  _ref_T = T_ref;
247  }
248 
249  inline ValueType getValue(const MatrixType & T) const
250  {
251  return fidelity_T<ValueType>(T, _ref_T);
252  }
253 };
254 
262 template<typename TomoProblem_, typename ValueType_ = double>
264 {
265 public:
266  typedef TomoProblem_ TomoProblem;
267  typedef typename TomoProblem::MatrQ MatrQ;
268  typedef typename MatrQ::MatrixType MatrixType;
269 
271  typedef ValueType_ ValueType;
272 
273 private:
274  MatrixType _ref_T;
275 
276 public:
278  PurifDistToRefCalculator(const TomoProblem & tomo)
279  : _ref_T(tomo.matq.initMatrixType())
280  {
281  _ref_T = tomo.T_MLE;
282  }
284  PurifDistToRefCalculator(const TomoProblem & tomo, const MatrixType & T_ref)
285  : _ref_T(tomo.matq.initMatrixType())
286  {
287  _ref_T = T_ref;
288  }
289 
290  inline ValueType getValue(const MatrixType & T) const
291  {
292  auto F = fidelity_T<ValueType>(T, _ref_T);
293  return std::sqrt(ValueType(1) - F*F);
294  }
295 };
296 
300 template<typename TomoProblem_, typename TrDistValueType_ = double>
302 {
303 public:
304  typedef TomoProblem_ TomoProblem;
305  typedef typename TomoProblem::MatrQ MatrQ;
306  typedef typename MatrQ::MatrixType MatrixType;
307 
309  typedef TrDistValueType_ ValueType;
310 
311 private:
312  MatrixType _ref_rho;
313 
314 public:
316  TrDistToRefCalculator(const TomoProblem & tomo)
317  : _ref_rho(tomo.matq.initMatrixType())
318  {
319  _ref_rho = tomo.rho_MLE;
320  }
322  TrDistToRefCalculator(const TomoProblem & tomo, const MatrixType& rho_ref)
323  : _ref_rho(tomo.matq.initMatrixType())
324  {
325  _ref_rho = rho_ref;
326  }
327 
328  inline ValueType getValue(const MatrixType & T) const
329  {
330  return boost::math::constants::half<ValueType>() *
331  (T*T.adjoint() - _ref_rho).template selfadjointView<Eigen::Lower>()
332  .eigenvalues().cwiseAbs().sum();
333  }
334 };
335 
339 template<typename TomoProblem_>
341 {
342 public:
343  typedef TomoProblem_ TomoProblem;
344  typedef typename TomoProblem::MatrQ MatrQ;
345  typedef typename MatrQ::MatrixType MatrixType;
346  typedef typename MatrQ::VectorParamType VectorParamType;
347 
349  typedef typename MatrQ::RealScalar ValueType;
350 
351 private:
352  const TomoProblem & _tomo;
353 
355  VectorParamType _A_x;
356 
357  // MatrixType _A; // not needed
358 
359 public:
361  ObservableValueCalculator(const TomoProblem & tomo, const MatrixType & A)
362  : _tomo(tomo),
363  _A_x(tomo.matq.initVectorParamType())
364  {
365  param_herm_to_x(_A_x, A);
366  }
367 
369  ObservableValueCalculator(const TomoProblem & tomo, const VectorParamType & A_x)
370  : _tomo(tomo),
371  _A_x(tomo.matq.initVectorParamType())
372  {
373  _A_x = A_x;
374  }
375 
376  inline ValueType getValue(const MatrixType & T) const
377  {
378  MatrixType rho = _tomo.matq.initMatrixType();
379  rho = T*T.adjoint();
380  VectorParamType x = _tomo.matq.initVectorParamType();
381  param_herm_to_x(x, rho);
382  return _A_x.transpose() * x;
383  }
384 };
385 
386 
387 
388 } // namespace Tomographer
389 
390 
391 #endif
PurifDistToRefCalculator(const TomoProblem &tomo, const MatrixType &T_ref)
Constructor, the reference state is T_ref (in T Parameterization)
Definition: dmmhrw.h:284
TrDistValueType_ ValueType
For ValueCalculator interface : value type.
Definition: dmmhrw.h:309
Eigen::Matrix< std::complex< RealScalar >, FixedDim, FixedDim > MatrixType
Complex dim x dim Matrix.
Definition: matrq.h:132
LLHValueType fnlogval(const MatrixType &T)
Calculate the logarithm of the Metropolis-Hastings function value.
Definition: dmmhrw.h:177
Base namespace for the Tomographer project.
Definition: dmmhrw.h:51
FidelityToRefCalculator(const TomoProblem &tomo)
Constructor, take the reference state to be the MLE.
Definition: dmmhrw.h:237
Calculate the trace distance to a reference state for each sample.
Definition: dmmhrw.h:301
MatrixType PointType
Provided for MHRandomWalk. A point in our random walk = a density matrix.
Definition: dmmhrw.h:87
LLHValueType FnValueType
Provided for MHRandomWalk. The function value type is the loglikelihood value type.
Definition: dmmhrw.h:89
TomoProblem::LLHValueType LLHValueType
The loglikelihood function value type (see IndepMeasTomoProblem)
Definition: dmmhrw.h:76
TrDistToRefCalculator(const TomoProblem &tomo)
Constructor, take the reference state to be the MLE.
Definition: dmmhrw.h:316
FidValueType_ ValueType
For ValueCalculator interface : value type.
Definition: dmmhrw.h:230
void init()
Provided for MHRandomWalk. Initializes some fields and prepares for a random walk.
Definition: dmmhrw.h:133
MatrixType jump_fn(const MatrixType &cur_T, RealScalar step_size)
Decides of a new point to jump to for the random walk.
Definition: dmmhrw.h:191
PurifDistToRefCalculator(const TomoProblem &tomo)
Constructor, take the reference state to be the MLE.
Definition: dmmhrw.h:278
MatrQ::RealScalar ValueType
For ValueCalculator interface : value type.
Definition: dmmhrw.h:349
TrDistToRefCalculator(const TomoProblem &tomo, const MatrixType &rho_ref)
Constructor, the reference state is rho_ref.
Definition: dmmhrw.h:322
TomoProblem::MatrQ MatrQ
The data types of our problem.
Definition: dmmhrw.h:74
MatrQ::ComplexScalar ComplexScalar
The complex real scalar corresponding to our data types. Usually a std::complex.
Definition: dmmhrw.h:84
Data corresponding to a quantum tomography setting.
MatrQ::MatrixType MatrixType
The matrix type for a density operator on our quantum system.
Definition: dmmhrw.h:78
Calculate the "purified distance" to a reference state for each sample.
Definition: dmmhrw.h:263
tomo_internal::matrq_traits< MatrQ< FixedDim_, FixedMaxParamList_, RealScalar_, IntFreqType_ > >::RealScalar RealScalar
Real scalar type (usually double)
Definition: matrq.h:125
MatrQ::RealScalar RealScalar
The real scalar corresponding to our data types. Usually a double.
Definition: dmmhrw.h:82
Calculate the fidelity to a reference state for each sample.
Definition: dmmhrw.h:222
Distance measures in Quantum Information Theory – e.g. fidelity.
tomo_internal::matrq_traits< MatrQ< FixedDim_, FixedMaxParamList_, RealScalar_, IntFreqType_ > >::ComplexScalar ComplexScalar
Complex scalar type (usually std::complex)
Definition: matrq.h:127
Provides the logarithm MH function value at each point (see Role of UseFnSyntaxType) ...
Definition: mhrw.h:161
void done()
Callback for after random walk is finished. No-op.
Definition: dmmhrw.h:167
A random walk in the density matrix space of a Hilbert state space of a quantum system.
Definition: dmmhrw.h:70
void thermalizing_done()
Callback for after thermalizing is done. No-op.
Definition: dmmhrw.h:162
FidelityToRefCalculator(const TomoProblem &tomo, const MatrixType &T_ref)
Constructor, the reference state is T_ref (in T Parameterization)
Definition: dmmhrw.h:243
ObservableValueCalculator(const TomoProblem &tomo, const VectorParamType &A_x)
Constructor directly accepting the X parameterization of A.
Definition: dmmhrw.h:369
T sqrt(T...args)
Routines for performing a Metropolis-Hastings random walk.
ObservableValueCalculator(const TomoProblem &tomo, const MatrixType &A)
Constructor directly accepting A as a hermitian matrix.
Definition: dmmhrw.h:361
STL class.
DMStateSpaceLLHMHWalker(const MatrixType &startpt, const TomoProblem &tomo, Rng &rng, Log &log_)
Constructor which just initializes the given fields.
Definition: dmmhrw.h:114
Basic utilities for dealing with Eigen matrices and other types.
const MatrixType & startpoint()
Return the starting point given in the constructor, or a random start point.
Definition: dmmhrw.h:139
Calculate expectation value of an observable for each sample.
Definition: dmmhrw.h:340
Utilities for logging messages.
ValueType_ ValueType
For ValueCalculator interface : value type.
Definition: dmmhrw.h:271
MatrQ::VectorParamType VectorParamType
Type of an X-parameterization of a density operator (see param_x_to_herm())
Definition: dmmhrw.h:80
We will calculate the log-likelihood function, which is the logarithm of the Metropolis-Hastings func...
Definition: dmmhrw.h:94
void param_herm_to_x(Eigen::DenseBase< Derived1 > &x, const Eigen::MatrixBase< Derived2 > &Herm)
Get the X-parameterization corresponding to a given hermitian matrix.
Definition: param_herm_x.h:93