27 #ifndef TOMOGRAPHER_DENSEDM_INDEPMEASLLH_H 28 #define TOMOGRAPHER_DENSEDM_INDEPMEASLLH_H 34 #include <Eigen/Eigen> 60 template<
typename DMTypes_,
typename LLHValueType_ =
typename DMTypes_::RealScalar,
61 typename IntFreqType_ = int,
int FixedMaxParamList_ = Eigen::Dynamic,
62 bool UseNMeasAmplifyFactor_ =
false>
116 :
dmt(dmt_), _Exn(VectorParamListType::Zero(0,
dmt.dim2())), _Nx(FreqListType::Zero(0)),
117 _NMeasAmplifyFactor(1)
125 inline IndepMeasLLH(DMTypes dmt_, VectorParamListTypeConstRef Exn_, FreqListTypeConstRef Nx_)
126 :
dmt(dmt_), _Exn(VectorParamListType::Zero(0,
dmt.dim2())), _Nx(FreqListType::Zero(0)), _NMeasAmplifyFactor(1)
140 inline const IndexType
numEffects()
const {
return _Exn.rows(); }
152 inline const VectorParamListType &
Exn()
const {
return _Exn; }
162 return _Exn.
row(i).transpose();
177 inline const FreqListType &
Nx()
const {
return _Nx; }
186 inline const IntFreqType
Nx(IndexType i)
const {
return _Nx(i); }
195 _Exn.
resize(0, dmt.dim2());
215 bool check_validity =
true)
222 tomographer_assert(n > 0);
224 if (check_validity) {
230 IndexType newi = _Exn.rows();
231 tomographer_assert(newi == _Nx.rows());
234 _Exn.
row(newi) = E_x.transpose();
238 tomographer_assert(_Exn.rows() == _Nx.rows());
251 bool check_validity =
true)
253 tomographer_assert(E_m.rows() == E_m.cols());
254 tomographer_assert(E_m.rows() == (
IndexType)dmt.dim());
261 if (check_validity) {
262 tomographer_assert(n > 0);
273 inline void setMeas(VectorParamListTypeConstRef Exn_, FreqListTypeConstRef Nx_,
bool check_validity =
true)
275 tomographer_assert(Exn_.cols() == (
IndexType)dmt.dim2());
276 tomographer_assert(Exn_.rows() == Nx_.rows());
277 tomographer_assert(Nx_.cols() == 1);
279 if ((Nx_ > 0).all()) {
281 _Exn.
resize(Exn_.rows(), dmt.dim2());
283 _Nx.
resize(Nx_.rows(), 1);
288 for (IndexType i = 0; i < Exn_.rows(); ++i) {
292 if (check_validity) {
297 inline void checkAllMeas()
const 299 tomographer_assert(_Exn.cols() == (
IndexType)dmt.dim2());
300 tomographer_assert(_Exn.rows() == _Nx.rows());
301 tomographer_assert(_Nx.cols() == 1);
303 for (IndexType i = 0; i < _Exn.rows(); ++i) {
305 tomographer_assert(_Nx(i) > 0);
312 inline void checkEffect(IndexType i)
const 314 tomographer_assert(_Exn.cols() == (
IndexType)dmt.dim2());
315 tomographer_assert(_Exn.rows() == _Nx.rows());
316 tomographer_assert(_Nx.cols() == 1);
317 tomographer_assert(i >= 0 && i < _Exn.rows());
318 tomographer_assert(_Nx(i) > 0);
328 if ( ! (
double( (E_m - E_m.adjoint()).norm() ) < 1e-8) ) {
333 const double mineigval = slv.
eigenvalues().minCoeff();
334 if ( ! (mineigval >= -1e-12) ) {
338 if ( ! (
double(E_m.norm()) > 1e-6) ) {
350 TOMOGRAPHER_ENABLED_IF(UseNMeasAmplifyFactor)
378 return _mult_by_nmeasfactor(
379 (_Nx.template cast<LLHValueType>() * (_Exn * x).template cast<LLHValueType>().array().
log()).sum()
384 template<
typename Expr, TOMOGRAPHER_ENABLED_IF_TMPL(UseNMeasAmplifyFactor)>
385 inline auto _mult_by_nmeasfactor(Expr&& expr)
const -> decltype(
LLHValueType(1) * expr)
387 return _NMeasAmplifyFactor.value * expr;
389 template<
typename Expr, TOMOGRAPHER_ENABLED_IF_TMPL(!UseNMeasAmplifyFactor)>
390 inline auto _mult_by_nmeasfactor(Expr&& expr)
const -> decltype(std::forward<Expr>(expr))
392 return std::forward<Expr>(expr);
405 template<
typename DMTypes_,
typename LLHValueType_,
406 typename IntFreqType_,
int FixedMaxParamList_,
407 bool UseNMeasAmplifyFactor_>
410 template<
typename DMTypes_,
typename LLHValueType_,
411 typename IntFreqType_,
int FixedMaxParamList_,
412 bool UseNMeasAmplifyFactor_>
415 template<
typename DMTypes_,
typename LLHValueType_,
416 typename IntFreqType_,
int FixedMaxParamList_,
417 bool UseNMeasAmplifyFactor_>
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.
static constexpr bool IsDynamicMaxParamList
Whether the dimension is specified dynamically at run-time or statically at compile-time.
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.
const IndexType numEffects() const
The number of POVM effects in the list.
Eigen::Ref< const typename DMTypes::VectorParamType > Exn(IndexType i) const
The i-th stored POVM effect, in X Parameterization.
void addMeasEffect(typename DMTypes::VectorParamTypeConstRef E_x, IntFreqType n, bool check_validity=true)
Store a measurement POVM effect along with a frequency count.
const FreqListType & Nx() const
The stored frequency counts for each individual POVM effect.
C++ types and functions for calculating the log-likelihood for POVM effects which can be written as a...
#define streamstr(tokens)
Utility macro to format stream tokens to a std::string.
LLHValueType NMeasAmplifyFactor() const
Factor by which to artificially amplify the number of measurements.
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)
static constexpr int FixedMaxParamList
Maximum number of POVM effects, fixed at compile-time or Eigen::Dynamic.
void conservativeResize(Index nbRows, Index nbCols)
const VectorParamListType & Exn() const
The stored individual POVM effects, in X Parameterization.
Tools for parameterizing hermitian matrices with the X Parameterization.
Exception class notifying of invalid measurement data.
DMTypes_ DMTypes
The DMTypes in use here.
const IntFreqType Nx(IndexType i) const
The number of counts stored for the i-th POVM effect.
Some declarations for the DenseLLH type interface.
static constexpr bool UseNMeasAmplifyFactor
Whether we allow NMeasAmplifyFactor to be set.
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.
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.
Declare that this DenseLLH object exposes a logLikelihoodX() method.
void setNMeasAmplifyFactor(LLHValueType val)
Set the factor by which to artificially multiply all frequency counts.
const CwiseUnaryOp< internal::scalar_log_op< Scalar >, const Array< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > > log() const
void resize(Index nbRows, Index nbCols)
void resetMeas()
Reset the measurement vectors and frequency counts.
IndepMeasLLH(DMTypes dmt_)
Simple constructor.
The DenseLLH-compatible object exposes a method logLikelihoodX(), taking as argument a (const ref to ...
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.