Tomographer  v5.4
Tomographer C++ Framework Documentation
param_rho_a.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  * Copyright (c) 2017 Caltech, Institute for Quantum Information and Matter, Philippe Faist
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26  */
27 
28 #ifndef TOMOGRAPHER_DENSEDM_PARAM_RHO_A_H
29 #define TOMOGRAPHER_DENSEDM_PARAM_RHO_A_H
30 
38 #include <Eigen/Core>
39 #include <Eigen/StdVector>
40 
42 #include <tomographer/tools/cxxutil.h> // tomographer_assert()
44 
45 #include <boost/math/constants/constants.hpp>
46 
47 
48 namespace Tomographer
49 {
50 
51 // based on http://mathworld.wolfram.com/GeneralizedGell-MannMatrix.html
52 
53 namespace tomo_internal
54 {
55 
56 template<typename DMTypes_, typename Coeffs>
57 struct GenGellMannFunctor12
58 {
59  typedef DMTypes_ DMTypes;
60  typedef typename DMTypes::MatrixType::Index MatIndex;
61  GenGellMannFunctor12(DMTypes matq_, MatIndex j_, MatIndex k_)
62  : matq(matq_),
63  j(j_),
64  k(k_)
65  {
66  tomographer_assert(j < k);
67  tomographer_assert(k <= (MatIndex)matq.dim());
68  }
69  DMTypes matq;
70  MatIndex j;
71  MatIndex k;
72 
73  template<typename Index>
74  inline const typename DMTypes::ComplexScalar operator() (Index row, Index col) const
75  {
76  if (row == j && col == k) {
77  return Coeffs::CoeffJK();
78  }
79  if (row == k && col == j) {
80  return Coeffs::CoeffKJ();
81  }
82  return 0;
83  }
84 };
85 
86 template<typename DMTypes>
87 struct coeffs1 {
88  static inline typename DMTypes::ComplexScalar CoeffJK() { return typename DMTypes::ComplexScalar(1, 0); }
89  static inline typename DMTypes::ComplexScalar CoeffKJ() { return typename DMTypes::ComplexScalar(1, 0); }
90 };
91 template<typename DMTypes>
92 struct coeffs2 {
93  static inline typename DMTypes::ComplexScalar CoeffJK() { return typename DMTypes::ComplexScalar(0, -1); }
94  static inline typename DMTypes::ComplexScalar CoeffKJ() { return typename DMTypes::ComplexScalar(0, +1); }
95 };
96 
97 template<typename DMTypes>
98 struct GenGellMannFunctor1
99 {
100  typedef GenGellMannFunctor12<DMTypes, coeffs1<DMTypes> > type;
101 };
102 template<typename DMTypes>
103 struct GenGellMannFunctor2
104 {
105  typedef GenGellMannFunctor12<DMTypes, coeffs2<DMTypes> > type;
106 };
107 
108 
109 template<typename DMTypes_>
110 struct GenGellMannFunctor3
111 {
112  typedef GenGellMannFunctor3<DMTypes_> type;
113 
114  typedef DMTypes_ DMTypes;
115  typedef typename DMTypes::RealScalar RealScalar;
116  typedef typename DMTypes::MatrixType::Index MatIndex;
117 
118  GenGellMannFunctor3(DMTypes matq_, MatIndex l_)
119  : matq(matq_),
120  l(l_)
121  {
122  // remember: l = 0, 1, ..., d-2 and not: 1, ..., d-1
123  normalization = std::sqrt( RealScalar(2) / ((l+1)*(l+2)) );
124  tomographer_assert(l < (MatIndex)matq.dim()-1);
125  }
126  DMTypes matq;
127  MatIndex l;
128  RealScalar normalization;
129 
130  template<typename Index>
131  inline const typename DMTypes::ComplexScalar operator() (Index row, Index col) const
132  {
133  if (row != col) {
134  return 0;
135  } else if (row <= l) {
136  return normalization;
137  } else if (row == l+1) {
138  return - (l+1) * normalization;
139  } else {
140  // row > l+1
141  return 0;
142  }
143  }
144 };
145 
146 } // namespace tomo_internal
147 } // namespace Tomographer
148 
149 namespace Eigen { namespace internal {
150 // ---
151 // functor_traits for GenGellMannFunctor12
152 template<typename DMTypes, typename coeffs>
153 struct functor_traits<Tomographer::tomo_internal::GenGellMannFunctor12<DMTypes, coeffs> >
154 {
155  enum { Cost = 4*NumTraits<typename DMTypes::ComplexScalar>::AddCost, PacketAccess = false, IsRepeatable = true };
156 };
157 // functor_has_linear_access for GenGellMannFunctor12
158 template<typename DMTypes, typename coeffs>
159 struct functor_has_linear_access<Tomographer::tomo_internal::GenGellMannFunctor12<DMTypes, coeffs> >
160 { enum { ret = 0 }; };
161 // functor_traits for GenGellMannFunctor3
162 template<typename DMTypes>
163 struct functor_traits<Tomographer::tomo_internal::GenGellMannFunctor3<DMTypes> >
164 { enum { Cost = 4*NumTraits<typename DMTypes::ComplexScalar>::AddCost, PacketAccess = false, IsRepeatable = true }; };
165 // functor_has_linear_access for GenGellMannFunctor3
166 template<typename DMTypes>
167 struct functor_has_linear_access<Tomographer::tomo_internal::GenGellMannFunctor3<DMTypes> >
168 { enum { ret = 0 }; };
169 // ---
170 } } // namespace Eigen::internal, Eigen
171 
172 namespace Tomographer {
173 namespace DenseDM {
174 
185 template<typename DMTypes_>
186 class TOMOGRAPHER_EXPORT ParamA
187 {
188 public:
190  typedef DMTypes_ DMTypes;
192  typedef typename DMTypes::MatrixType MatrixType;
196  typedef typename DMTypes::RealScalar RealScalar;
197 
198 private:
199  const DMTypes _dmt;
200 
203 
204 public:
212  ParamA(DMTypes dmt_)
213  : _dmt(dmt_)
214  {
215  // calculate and cache the generalized Gell-Mann matrices
216  lambda.resize((std::size_t)_dmt.ndof());
217  std::size_t count = 0;
218  // first kind
219  for (Eigen::Index j = 0; j < _dmt.dim(); ++j) {
220  for (Eigen::Index k = j+1; k < _dmt.dim(); ++k) {
221  lambda[count] = MatrixType::NullaryExpr(
222  _dmt.dim(),
223  _dmt.dim(),
224  typename tomo_internal::GenGellMannFunctor1<DMTypes>::type(_dmt, j, k)
225  );
226  ++count;
227  }
228  }
229  // second kind
230  for (Eigen::Index j = 0; j < _dmt.dim(); ++j) {
231  for (Eigen::Index k = j+1; k < _dmt.dim(); ++k) {
232  lambda[count] = MatrixType::NullaryExpr(
233  _dmt.dim(),
234  _dmt.dim(),
235  typename tomo_internal::GenGellMannFunctor2<DMTypes>::type(_dmt, j, k)
236  );
237  ++count;
238  }
239  }
240  // third kind
241  for (Eigen::Index l = 0; l < _dmt.dim()-1; ++l) {
242  lambda[count] = MatrixType::NullaryExpr(
243  _dmt.dim(),
244  _dmt.dim(),
245  typename tomo_internal::GenGellMannFunctor3<DMTypes>::type(_dmt, l)
246  );
247  ++count;
248  }
249  // got them all?
250  tomographer_assert(count == lambda.size());
251  tomographer_assert(count == (std::size_t)_dmt.ndof());
252  }
253 
270  inline const MatrixType & getLambda(std::size_t j) const
271  {
272  tomographer_assert(j < (std::size_t)_dmt.ndof());
273  return lambda[j];
274  }
275 
284  inline VectorParamNdofType
286  {
287  VectorParamNdofType a(_dmt.initVectorParamNdofType());
288  tomographer_assert(a.size() == _dmt.ndof());
289  tomographer_assert(rho.rows() == _dmt.dim());
290  tomographer_assert(rho.cols() == _dmt.dim());
291  for (std::size_t n = 0; n < lambda.size(); ++n) {
292  a((Eigen::Index)n) = (rho * lambda[n].template selfadjointView<Eigen::Lower>())
293  .real().trace() * boost::math::constants::half_root_two<RealScalar>();
294  }
295  return a;
296  }
297 
303  inline MatrixType
304  aToRho(const Eigen::Ref<const VectorParamNdofType> & a, RealScalar trace = 1.0) const
305  {
306  MatrixType rho(_dmt.initMatrixType());
307  tomographer_assert(a.size() == _dmt.ndof());
308  tomographer_assert(rho.rows() == _dmt.dim());
309  tomographer_assert(rho.cols() == _dmt.dim());
310  rho = trace * MatrixType::Identity(rho.rows(), rho.cols()) / _dmt.dim();
311  for (std::size_t n = 0; n < lambda.size(); ++n) {
312  rho += a((Eigen::Index)n) * boost::math::constants::half_root_two<RealScalar>() * lambda[n];
313  }
314  return rho;
315  }
316 
317 }; // class ParamA
318 
319 
320 } // namespace DenseDM
321 } // namespace Tomographer
322 
323 #endif
Tomographer::DenseDM::DMTypes< Eigen::Dynamic, RealScalar > DMTypes
The Tomographer::DenseDM::DMTypes we should use by default, with dynamic sized matrices.
Definition: pydensedm.h:43
Base namespace for the Tomographer project.
Definition: densellh.h:45
MatrixType aToRho(const Eigen::Ref< const VectorParamNdofType > &a, RealScalar trace=1.0) const
Reconstruct a hermitian traceless matrix from its A Parameterization.
Definition: param_rho_a.h:304
DMTypes::MatrixType MatrixType
The Matrix type to use to describe hermitian matrices.
Definition: param_rho_a.h:192
C++ types for describing dense density matrices in various parameterizations.
DMTypes_ DMTypes
The C++ types for quantum objects and parameterizations.
Definition: param_rho_a.h:190
T internal(T... args)
T resize(T... args)
VectorParamNdofType rhoToA(const Eigen::Ref< const MatrixType > &rho) const
Compute the A Parameterization of a hermitian matrix.
Definition: param_rho_a.h:285
Basic utilities for dealing with Eigen matrices and other types.
DMTypes::RealScalar RealScalar
The real scalar type. Usually this is double.
Definition: param_rho_a.h:196
const MatrixType & getLambda(std::size_t j) const
Generalized Gell-Mann matrices.
Definition: param_rho_a.h:270
Some C++ utilities, with a tad of C++11 tricks.
double RealScalar
Real type for general calculation precisions (density matrix elements, log-likelihood value calculati...
Definition: common.h:92
Parameterization of density matrices in su(N) generators.
Definition: param_rho_a.h:186
T size(T... args)
STL class.
ParamA(DMTypes dmt_)
Construct an object which can perform A parameterization transformations.
Definition: param_rho_a.h:212
DMTypes::VectorParamNdofType VectorParamNdofType
The Matrix type to use to describe the A Parameterization of a hermitian matrix.
Definition: param_rho_a.h:194
T sqrt(T... args)
#define tomographer_assert(...)
Assertion test macro.
Definition: cxxdefs.h:84
RealScalar_ RealScalar
Real scalar type, given in template parameter. Usually double is fine.
Definition: dmtypes.h:121