28 #ifndef TOMOGRAPHER_DENSEDM_INDEPMEASLLH_H 29 #define TOMOGRAPHER_DENSEDM_INDEPMEASLLH_H 35 #include <Eigen/Eigen> 37 #include <boost/serialization/serialization.hpp> 66 template<
typename DMTypes_,
typename LLHValueType_ =
typename DMTypes_::RealScalar,
67 typename IntFreqType_ = int,
int FixedMaxParamList_ = Eigen::Dynamic,
68 bool UseNMeasAmplifyFactor_ =
false>
80 static constexpr
int FixedMaxParamList = FixedMaxParamList_;
82 static constexpr
bool IsDynamicMaxParamList = (FixedMaxParamList_ == Eigen::Dynamic);
84 static constexpr
bool UseNMeasAmplifyFactor = UseNMeasAmplifyFactor_;
122 : dmt(dmt_), _Exn(VectorParamListType::Zero(0, (
Eigen::Index)dmt.dim2())), _Nx(FreqListType::Zero(0)),
123 _NMeasAmplifyFactor(1)
131 inline IndepMeasLLH(DMTypes dmt_, VectorParamListTypeConstRef Exn_, FreqListTypeConstRef Nx_)
132 : dmt(dmt_), _Exn(VectorParamListType::Zero(0, dmt.dim2())), _Nx(FreqListType::Zero(0)), _NMeasAmplifyFactor(1)
146 inline const IndexType
numEffects()
const {
return _Exn.rows(); }
158 inline const VectorParamListType &
Exn()
const {
return _Exn; }
168 return _Exn.row(i).transpose();
183 inline const FreqListType &
Nx()
const {
return _Nx; }
192 inline const IntFreqType
Nx(IndexType i)
const {
return _Nx(i); }
201 _Exn.resize(0, (Eigen::Index)dmt.dim2());
221 bool check_validity =
true)
230 if (check_validity) {
236 IndexType newi = _Exn.rows();
239 _Exn.conservativeResize(newi + 1, Eigen::NoChange);
240 _Exn.row(newi) = E_x.transpose();
241 _Nx.conservativeResize(newi + 1, Eigen::NoChange);
257 bool check_validity =
true)
267 if (check_validity) {
279 inline void setMeas(VectorParamListTypeConstRef Exn_, FreqListTypeConstRef Nx_,
bool check_validity =
true)
285 if ((Nx_ > 0).all()) {
287 _Exn.resize(Exn_.rows(), (Eigen::Index)dmt.dim2());
289 _Nx.resize(Nx_.rows(), 1);
294 for (IndexType i = 0; i < Exn_.rows(); ++i) {
295 addMeasEffect(Exn_.row(i), Nx_(i),
false);
298 if (check_validity) {
303 inline void checkAllMeas()
const 309 for (IndexType i = 0; i < _Exn.rows(); ++i) {
318 inline void checkEffect(IndexType i)
const 334 if ( ! (
double( (E_m - E_m.adjoint()).norm() ) < 1e-8) ) {
339 const typename DMTypes::RealScalar mineigval = slv.
eigenvalues().minCoeff();
343 << mineigval <<
") : E_m =\n" 346 if ( ! (
double(E_m.norm()) > 1e-6) ) {
358 TOMOGRAPHER_ENABLED_IF(UseNMeasAmplifyFactor)
359 inline LLHValueType NMeasAmplifyFactor()
const {
return _NMeasAmplifyFactor.value; }
362 TOMOGRAPHER_ENABLED_IF(!UseNMeasAmplifyFactor)
371 TOMOGRAPHER_ENABLED_IF(UseNMeasAmplifyFactor)
372 inline
void setNMeasAmplifyFactor(
LLHValueType val) { _NMeasAmplifyFactor.value = val; }
386 return _mult_by_nmeasfactor(
387 (_Nx.template cast<LLHValueType>() * (_Exn * x).template cast<LLHValueType>().array().log()).sum()
392 template<
typename Expr, TOMOGRAPHER_ENABLED_IF_TMPL(UseNMeasAmplifyFactor)>
393 inline auto _mult_by_nmeasfactor(Expr&& expr)
const -> decltype(
LLHValueType(1) * expr)
395 return _NMeasAmplifyFactor.value * expr;
397 template<
typename Expr, TOMOGRAPHER_ENABLED_IF_TMPL(!UseNMeasAmplifyFactor)>
398 inline auto _mult_by_nmeasfactor(Expr&& expr)
const -> decltype(std::forward<Expr>(expr))
400 return std::forward<Expr>(expr);
412 friend boost::serialization::access;
413 template<
typename Archive>
414 void serialize(Archive & a,
const unsigned int version)
418 maybe_serialize_nmeasamplifyfactor(a, version);
420 template<
typename Archive, TOMOGRAPHER_ENABLED_IF_TMPL(UseNMeasAmplifyFactor)>
421 inline void maybe_serialize_nmeasamplifyfactor(Archive & a,
const unsigned int )
423 a & _NMeasAmplifyFactor.value;
425 template<
typename Archive, TOMOGRAPHER_ENABLED_IF_TMPL(!UseNMeasAmplifyFactor)>
426 inline void maybe_serialize_nmeasamplifyfactor(Archive & ,
const unsigned int) { }
429 template<
typename DMTypes_,
typename LLHValueType_,
430 typename IntFreqType_,
int FixedMaxParamList_,
431 bool UseNMeasAmplifyFactor_>
434 template<
typename DMTypes_,
typename LLHValueType_,
435 typename IntFreqType_,
int FixedMaxParamList_,
436 bool UseNMeasAmplifyFactor_>
439 template<
typename DMTypes_,
typename LLHValueType_,
440 typename IntFreqType_,
int FixedMaxParamList_,
441 bool UseNMeasAmplifyFactor_>
454 namespace serialization {
455 template<
typename Archive,
456 typename DMTypes_,
typename LLHValueType_,
typename IntFreqType_,
457 int FixedMaxParamList_,
bool UseNMeasAmplifyFactor_>
458 inline void save_construct_data(
461 FixedMaxParamList_, UseNMeasAmplifyFactor_> * t,
465 Eigen::Index dim = t->dmt.dim();
469 template<
class Archive,
470 typename DMTypes_,
typename LLHValueType_,
typename IntFreqType_,
471 int FixedMaxParamList_,
bool UseNMeasAmplifyFactor_>
472 inline void load_construct_data(
475 FixedMaxParamList_, UseNMeasAmplifyFactor_> * t,
479 FixedMaxParamList_, UseNMeasAmplifyFactor_> TheFreakinType;
484 ::new(t) TheFreakinType(
typename TheFreakinType::DMTypes(dim));
Utilities for formatting strings.
const Eigen::Ref< const VectorParamListType > & VectorParamListTypeConstRef
Const ref to a VectorParamListType.
const Eigen::Ref< const FreqListType > & FreqListTypeConstRef
Const ref to a FreqListType.
Base namespace for the Tomographer project.
const RealVectorType & eigenvalues() const
C++ types for describing dense density matrices in various parameterizations.
internal::traits< Matrix< typename DMTypes::RealScalar, Eigen::Dynamic, DMTypes::FixedDim2, Eigen::RowMajor, FixedMaxParamList, DMTypes::FixedDim2 > >::Index Index
IndepMeasLLH(DMTypes dmt_, VectorParamListTypeConstRef Exn_, FreqListTypeConstRef Nx_)
Constructor with full measurement data.
const DMTypes dmt
The DMTypes object, storing e.g. the dimension of the problem.
void setMeas(VectorParamListTypeConstRef Exn_, FreqListTypeConstRef Nx_, bool check_validity=true)
Specify the full measurement data at once.
void addMeasEffect(typename DMTypes::VectorParamTypeConstRef E_x, IntFreqType n, bool check_validity=true)
Store a measurement POVM effect along with a frequency count.
const IntFreqType Nx(IndexType i) const
The number of counts stored for the i-th POVM effect.
C++ types and functions for calculating the log-likelihood for POVM effects which can be written as a...
static constexpr int FixedDim2
The square of the dimension of the quantum system, or Eigen::Dynamic.
LLHValueType_ LLHValueType
Type used to calculate the log-likelihood function (see DenseLLH Interface)
Tools for parameterizing hermitian matrices with the X Parameterization.
Exception class notifying of invalid measurement data.
DMTypes_ DMTypes
The DMTypes in use here.
Some declarations for the DenseLLH type interface.
VectorParamListType::Index IndexType
Type used to index entries in VectorParamListType (and also used for indexing entries in FreqListType...
IntFreqType_ IntFreqType
Type used to store integer measurement counts.
const VectorParamListType & Exn() const
The stored individual POVM effects, in X Parameterization.
Some C++ utilities, with a tad of C++11 tricks.
Eigen::Matrix< typename DMTypes::RealScalar, Eigen::Dynamic, DMTypes::FixedDim2, Eigen::RowMajor, FixedMaxParamList, DMTypes::FixedDim2 > VectorParamListType
dynamic Matrix with rows = dim*dim Vectors (row-major) [maximum FixedMaxParamList rows...
Managing the need for specific overrides to operator new() for some types (especially Eigen types) ...
Eigen::Array< IntFreqType, Eigen::Dynamic, 1, 0, FixedMaxParamList, 1 > FreqListType
dynamic Array of integers [maximum FixedMaxParamList entries or Dynamic]
LLHValueType logLikelihoodX(typename DMTypes::VectorParamTypeConstRef x) const
Calculates the log-likelihood function, in X parameterization.
const IndexType numEffects() const
The number of POVM effects in the list.
#define streamstr(tokens)
Utility macro to format stream tokens to a std::string.
T setprecision(T... args)
void resetMeas()
Reset the measurement vectors and frequency counts.
IndepMeasLLH(DMTypes dmt_)
Simple constructor.
#define tomographer_assert(...)
Assertion test macro.
The DenseLLH-compatible object exposes a method logLikelihoodX(), taking as argument a (const ref to ...
const FreqListType & Nx() const
The stored frequency counts for each individual POVM effect.
RealScalar_ RealScalar
Real scalar type, given in template parameter. Usually double is fine.
void addMeasEffect(typename DMTypes::MatrixTypeConstRef E_m, IntFreqType n, bool check_validity=true)
Store a POVM measurement effect with frequency count.
Eigen::Ref< const typename DMTypes::VectorParamType > Exn(IndexType i) const
The i-th stored POVM effect, in X Parameterization.