Quantum++  v0.8.8
C++11 quantum computing library
operations.h
Go to the documentation of this file.
1 /*
2  * Quantum++
3  *
4  * Copyright (c) 2013 - 2016 Vlad Gheorghiu (vgheorgh@gmail.com)
5  *
6  * This file is part of Quantum++.
7  *
8  * Quantum++ is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * Quantum++ is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with Quantum++. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
27 #ifndef OPERATIONS_H_
28 #define OPERATIONS_H_
29 
30 // silence g++4.8 bogus warning -Wunused-but-set-variable in lambda functions
31 #if (__GNUC__ && !__clang__)
32 #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
33 #endif
34 
35 namespace qpp
36 {
53 template<typename Derived1, typename Derived2>
55  const Eigen::MatrixBase<Derived1>& state,
56  const Eigen::MatrixBase<Derived2>& A,
57  const std::vector<idx>& ctrl,
58  const std::vector<idx>& subsys,
59  const std::vector<idx>& dims)
60 {
61  const typename Eigen::MatrixBase<Derived1>::EvalReturnType& rstate
62  = state.derived();
63  const dyn_mat<typename Derived2::Scalar>& rA = A.derived();
64 
65  // EXCEPTION CHECKS
66 
67  // check types
68  if ( !std::is_same<typename Derived1::Scalar,
69  typename Derived2::Scalar>::value )
70  throw Exception("qpp::applyCTRL()", Exception::Type::TYPE_MISMATCH);
71 
72  // check zero sizes
74  throw Exception("qpp::applyCTRL()", Exception::Type::ZERO_SIZE);
75 
76  // check zero sizes
77  if ( !internal::_check_nonzero_size(rstate))
78  throw Exception("qpp::applyCTRL()", Exception::Type::ZERO_SIZE);
79 
80  // check square matrix for the gate
82  throw Exception("qpp::applyCTRL()",
84 
85  // check that all control subsystems have the same dimension
86  idx d = ctrl.size() > 0 ? dims[ctrl[0]] : 1;
87  for ( idx i = 1; i < ctrl.size(); ++i )
88  if ( dims[ctrl[i]] != d )
89  throw Exception("qpp::applyCTRL()",
91 
92  // check that dimension is valid
93  if ( !internal::_check_dims(dims))
94  throw Exception("qpp::applyCTRL()", Exception::Type::DIMS_INVALID);
95 
96  // check subsys is valid w.r.t. dims
97  if ( !internal::_check_subsys_match_dims(subsys, dims))
98  throw Exception("qpp::applyCTRL()",
100 
101  // check that gate matches the dimensions of the subsys
102  std::vector<idx> subsys_dims(subsys.size());
103  for ( idx i = 0; i < subsys.size(); ++i )
104  subsys_dims[i] = dims[subsys[i]];
105  if ( !internal::_check_dims_match_mat(subsys_dims, rA))
106  throw Exception("qpp::applyCTRL()",
108 
109  std::vector<idx> ctrlgate = ctrl; // ctrl + gate subsystem vector
110  ctrlgate.insert(std::end(ctrlgate), std::begin(subsys), std::end(subsys));
111  std::sort(std::begin(ctrlgate), std::end(ctrlgate));
112 
113  // check that ctrl + gate subsystem is valid
114  // with respect to local dimensions
115  if ( !internal::_check_subsys_match_dims(ctrlgate, dims))
116  throw Exception("qpp::applyCTRL()",
118  // END EXCEPTION CHECKS
119 
120  // construct the table of A^i and (A^dagger)^i
121  std::vector<dyn_mat<typename Derived1::Scalar>> Ai;
122  std::vector<dyn_mat<typename Derived1::Scalar>> Aidagger;
123  for ( idx i = 0; i < std::max(d, static_cast<idx>(2)); ++i )
124  {
125  Ai.push_back(powm(rA, i));
126  Aidagger.push_back(powm(adjoint(rA), i));
127  }
128 
129  idx D = static_cast<idx>(rstate.rows()); // total dimension
130  idx n = dims.size(); // total number of subsystems
131  idx ctrlsize = ctrl.size(); // number of ctrl subsystem
132  idx ctrlgatesize = ctrlgate.size(); // number of ctrl+gate subsystems
133  idx subsyssize = subsys.size(); // number of subsystems of the target
134  // dimension of ctrl subsystem
135  idx Dctrl = static_cast<idx>(std::llround(std::pow(d, ctrlsize)));
136  idx DA = static_cast<idx>(rA.rows()); // dimension of gate subsystem
137 
138  idx Cdims[maxn]; // local dimensions
139  idx CdimsA[maxn]; // local dimensions
140  idx CdimsCTRL[maxn]; // local dimensions
141  idx CdimsCTRLAbar[maxn]; // local dimensions
142 
143  // compute the complementary subsystem of ctrlgate w.r.t. dims
144  std::vector<idx> ctrlgatebar = complement(ctrlgate, n);
145  // number of subsystems that are complementary to the ctrl+gate
146  idx ctrlgatebarsize = ctrlgatebar.size();
147 
148  idx DCTRLAbar = 1; // dimension of the rest
149  for ( idx i = 0; i < ctrlgatebarsize; ++i )
150  DCTRLAbar *= dims[ctrlgatebar[i]];
151 
152  for ( idx k = 0; k < n; ++k )
153  Cdims[k] = dims[k];
154  for ( idx k = 0; k < subsyssize; ++k )
155  CdimsA[k] = dims[subsys[k]];
156  for ( idx k = 0; k < ctrlsize; ++k )
157  CdimsCTRL[k] = d;
158  for ( idx k = 0; k < ctrlgatebarsize; ++k )
159  CdimsCTRLAbar[k] = dims[ctrlgatebar[k]];
160 
161  // worker, computes the coefficient and the index for the ket case
162  // used in #pragma omp parallel for collapse
163  auto coeff_idx_ket = [&](idx _i, idx _m, idx _r) noexcept
164  -> std::pair<typename Derived1::Scalar, idx>
165  {
166  idx indx = 0;
167  typename Derived1::Scalar coeff = 0;
168 
169  idx Cmidx[maxn]; // the total multi-index
170  idx CmidxA[maxn]; // the gate part multi-index
171  idx CmidxCTRLAbar[maxn]; // the rest multi-index
172 
173  // compute the index
174 
175  // set the CTRL part
176  for ( idx k = 0; k < ctrlsize; ++k )
177  {
178  Cmidx[ctrl[k]] = _i;
179  }
180 
181  // set the rest
182  internal::_n2multiidx(_r, n - ctrlgatesize,
183  CdimsCTRLAbar, CmidxCTRLAbar);
184  for ( idx k = 0; k < n - ctrlgatesize; ++k )
185  {
186  Cmidx[ctrlgatebar[k]] = CmidxCTRLAbar[k];
187  }
188 
189  // set the A part
190  internal::_n2multiidx(_m, subsyssize, CdimsA, CmidxA);
191  for ( idx k = 0; k < subsyssize; ++k )
192  {
193  Cmidx[subsys[k]] = CmidxA[k];
194  }
195 
196  // we now got the total index
197  indx = internal::_multiidx2n(Cmidx, n, Cdims);
198 
199  // compute the coefficient
200  for ( idx _n = 0; _n < DA; ++_n )
201  {
202  internal::_n2multiidx(_n, subsyssize, CdimsA, CmidxA);
203  for ( idx k = 0; k < subsyssize; ++k )
204  {
205  Cmidx[subsys[k]] = CmidxA[k];
206  }
207  coeff += Ai[_i](_m, _n) *
208  rstate(internal::_multiidx2n(Cmidx, n, Cdims));
209  }
210 
211  return std::make_pair(coeff, indx);
212  }; /* end coeff_idx_ket */
213 
214  // worker, computes the coefficient and the index
215  // for the density matrix case
216  // used in #pragma omp parallel for collapse
217  auto coeff_idx_rho = [&](idx _i1, idx _m1,
218  idx _r1, idx _i2, idx _m2, idx _r2) noexcept
219  -> std::tuple<typename Derived1::Scalar, idx, idx>
220  {
221  idx idxrow = 0;
222  idx idxcol = 0;
223  typename Derived1::Scalar coeff = 0, lhs = 1, rhs = 1;
224 
225  idx Cmidxrow[maxn]; // the total row multi-index
226  idx Cmidxcol[maxn]; // the total col multi-index
227  idx CmidxArow[maxn]; // the gate part row multi-index
228  idx CmidxAcol[maxn]; // the gate part col multi-index
229  idx CmidxCTRLrow[maxn]; // the control row multi-index
230  idx CmidxCTRLcol[maxn]; // the control col multi-index
231  idx CmidxCTRLAbarrow[maxn]; // the rest row multi-index
232  idx CmidxCTRLAbarcol[maxn]; // the rest col multi-index
233 
234  // compute the ket/bra indexes
235 
236  // set the CTRL part
237  internal::_n2multiidx(_i1, ctrlsize,
238  CdimsCTRL, CmidxCTRLrow);
239  internal::_n2multiidx(_i2, ctrlsize,
240  CdimsCTRL, CmidxCTRLcol);
241 
242  for ( idx k = 0; k < ctrlsize; ++k )
243  {
244  Cmidxrow[ctrl[k]] = CmidxCTRLrow[k];
245  Cmidxcol[ctrl[k]] = CmidxCTRLcol[k];
246  }
247 
248  // set the rest
249  internal::_n2multiidx(_r1, n - ctrlgatesize,
250  CdimsCTRLAbar, CmidxCTRLAbarrow);
251  internal::_n2multiidx(_r2, n - ctrlgatesize,
252  CdimsCTRLAbar, CmidxCTRLAbarcol);
253  for ( idx k = 0; k < n - ctrlgatesize; ++k )
254  {
255  Cmidxrow[ctrlgatebar[k]] = CmidxCTRLAbarrow[k];
256  Cmidxcol[ctrlgatebar[k]] = CmidxCTRLAbarcol[k];
257  }
258 
259  // set the A part
260  internal::_n2multiidx(_m1, subsyssize, CdimsA, CmidxArow);
261  internal::_n2multiidx(_m2, subsyssize, CdimsA, CmidxAcol);
262  for ( idx k = 0; k < subsys.size(); ++k )
263  {
264  Cmidxrow[subsys[k]] = CmidxArow[k];
265  Cmidxcol[subsys[k]] = CmidxAcol[k];
266  }
267 
268  // we now got the total row/col indexes
269  idxrow = internal::_multiidx2n(Cmidxrow, n, Cdims);
270  idxcol = internal::_multiidx2n(Cmidxcol, n, Cdims);
271 
272  // check whether all CTRL row and col multi indexes are equal
273  bool all_ctrl_rows_equal = true;
274  bool all_ctrl_cols_equal = true;
275 
276  idx first_ctrl_row, first_ctrl_col;
277  if ( ctrlsize > 0 )
278  {
279  first_ctrl_row = CmidxCTRLrow[0];
280  first_ctrl_col = CmidxCTRLcol[0];
281  }
282  else
283  {
284  first_ctrl_row = first_ctrl_col = 1;
285  }
286 
287  for ( idx k = 1; k < ctrlsize; ++k )
288  {
289  if ( CmidxCTRLrow[k] != first_ctrl_row )
290  {
291  all_ctrl_rows_equal = false;
292  break;
293  }
294  }
295  for ( idx k = 1; k < ctrlsize; ++k )
296  {
297  if ( CmidxCTRLcol[k] != first_ctrl_col )
298  {
299  all_ctrl_cols_equal = false;
300  break;
301  }
302  }
303 
304  // at least one control activated, compute the coefficient
305  for ( idx _n1 = 0; _n1 < DA; ++_n1 )
306  {
307  internal::_n2multiidx(_n1, subsyssize, CdimsA, CmidxArow);
308  for ( idx k = 0; k < subsyssize; ++k )
309  {
310  Cmidxrow[subsys[k]] = CmidxArow[k];
311  }
312  idx idxrowtmp = internal::_multiidx2n(Cmidxrow, n, Cdims);
313 
314  if ( all_ctrl_rows_equal )
315  {
316  lhs = Ai[first_ctrl_row](_m1, _n1);
317  }
318  else
319  {
320  lhs = (_m1 == _n1) ? 1 : 0; // identity matrix
321  }
322 
323  for ( idx _n2 = 0; _n2 < DA; ++_n2 )
324  {
325  internal::_n2multiidx(_n2, subsyssize, CdimsA, CmidxAcol);
326  for ( idx k = 0; k < subsyssize; ++k )
327  {
328  Cmidxcol[subsys[k]] = CmidxAcol[k];
329  }
330 
331  if ( all_ctrl_cols_equal )
332  {
333  rhs = Aidagger[first_ctrl_col](_n2, _m2);
334  }
335  else
336  {
337  rhs = (_n2 == _m2) ? 1 : 0; // identity matrix
338  }
339 
340  idx idxcoltmp = internal::_multiidx2n(Cmidxcol, n, Cdims);
341 
342  coeff += lhs * rstate(idxrowtmp, idxcoltmp) * rhs;
343  }
344  }
345 
346  return std::make_tuple(coeff, idxrow, idxcol);
347  }; /* end coeff_idx_rho */
348 
349  //************ ket ************//
350  if ( internal::_check_cvector(rstate)) // we have a ket
351  {
352  // check that dims match state vector
353  if ( !internal::_check_dims_match_cvect(dims, rstate))
354  throw Exception("qpp::applyCTRL()",
356  if ( D == 1 )
357  return rstate;
358 
359  dyn_mat<typename Derived1::Scalar> result = rstate;
360 
361 #ifdef _WITH_OPENMP_
362 #pragma omp parallel for collapse(2)
363 #endif
364  for ( idx m = 0; m < DA; ++m )
365  for ( idx r = 0; r < DCTRLAbar; ++r )
366  {
367  if ( ctrlsize == 0 ) // no control
368  {
369  result(coeff_idx_ket(1, m, r).second) =
370  coeff_idx_ket(1, m, r).first;
371  }
372  else
373  for ( idx i = 0; i < d; ++i )
374  {
375  result(coeff_idx_ket(i, m, r).second) =
376  coeff_idx_ket(i, m, r).first;
377  }
378  }
379 
380  return result;
381  }
382  //************ density matrix ************//
383  else if ( internal::_check_square_mat(rstate)) // we have a density operator
384  {
385  // check that dims match state matrix
386  if ( !internal::_check_dims_match_mat(dims, rstate))
387  throw Exception("qpp::applyCTRL()",
389 
390  if ( D == 1 )
391  return rstate;
392 
393  dyn_mat<typename Derived1::Scalar> result = rstate;
394 
395 #ifdef _WITH_OPENMP_
396 #pragma omp parallel for collapse(4)
397 #endif
398  for ( idx m1 = 0; m1 < DA; ++m1 )
399  for ( idx r1 = 0; r1 < DCTRLAbar; ++r1 )
400  for ( idx m2 = 0; m2 < DA; ++m2 )
401  for ( idx r2 = 0; r2 < DCTRLAbar; ++r2 )
402  if ( ctrlsize == 0 ) // no control
403  {
404  auto coeff_idxes = coeff_idx_rho(1, m1, r1,
405  1, m2, r2);
406  result(std::get<1>(coeff_idxes),
407  std::get<2>(coeff_idxes)) =
408  std::get<0>(coeff_idxes);
409  }
410  else
411  {
412  for ( idx i1 = 0; i1 < Dctrl; ++i1 )
413  for ( idx i2 = 0; i2 < Dctrl; ++i2 )
414  {
415  auto coeff_idxes = coeff_idx_rho(
416  i1, m1, r1,
417  i2, m2, r2);
418  result(std::get<1>(coeff_idxes),
419  std::get<2>(coeff_idxes)) =
420  std::get<0>(coeff_idxes);
421  }
422  }
423 
424  return result;
425  }
426  //************ Exception: not ket nor density matrix ************//
427  else
428  throw Exception("qpp::applyCTRL()",
430 }
431 
447 template<typename Derived1, typename Derived2>
449  const Eigen::MatrixBase<Derived1>& state,
450  const Eigen::MatrixBase<Derived2>& A,
451  const std::vector<idx>& ctrl,
452  const std::vector<idx>& subsys,
453  idx d = 2)
454 {
455  const typename Eigen::MatrixBase<Derived1>::EvalReturnType& rstate
456  = state.derived();
457  const dyn_mat<typename Derived1::Scalar>& rA = A.derived();
458 
459  // EXCEPTION CHECKS
460 
461  // check zero size
462  if ( !internal::_check_nonzero_size(rstate))
463  throw Exception("qpp::applyCTRL()", Exception::Type::ZERO_SIZE);
464 
465  // check valid dims
466  if ( d == 0 )
467  throw Exception("qpp::applyCTRL()", Exception::Type::DIMS_INVALID);
468  // END EXCEPTION CHECKS
469 
470  idx n =
471  static_cast<idx>(std::llround(std::log2(rstate.rows()) /
472  std::log2(d)));
473  std::vector<idx> dims(n, d); // local dimensions vector
474 
475  return applyCTRL(rstate, rA, ctrl, subsys, dims);
476 }
477 
491 template<typename Derived1, typename Derived2>
493  const Eigen::MatrixBase<Derived1>& state,
494  const Eigen::MatrixBase<Derived2>& A,
495  const std::vector<idx>& subsys,
496  const std::vector<idx>& dims)
497 {
498  const typename Eigen::MatrixBase<Derived1>::EvalReturnType& rstate
499  = state.derived();
500  const dyn_mat<typename Derived2::Scalar>& rA = A.derived();
501 
502  // EXCEPTION CHECKS
503 
504  // check types
505  if ( !std::is_same<typename Derived1::Scalar,
506  typename Derived2::Scalar>::value )
507  throw Exception("qpp::apply()", Exception::Type::TYPE_MISMATCH);
508 
509  // check zero sizes
511  throw Exception("qpp::apply()", Exception::Type::ZERO_SIZE);
512 
513  // check zero sizes
514  if ( !internal::_check_nonzero_size(rstate))
515  throw Exception("qpp::apply()", Exception::Type::ZERO_SIZE);
516 
517  // check square matrix for the gate
518  if ( !internal::_check_square_mat(rA))
519  throw Exception("qpp::apply()", Exception::Type::MATRIX_NOT_SQUARE);
520 
521  // check that dimension is valid
522  if ( !internal::_check_dims(dims))
523  throw Exception("qpp::apply()", Exception::Type::DIMS_INVALID);
524 
525  // check subsys is valid w.r.t. dims
526  if ( !internal::_check_subsys_match_dims(subsys, dims))
527  throw Exception("qpp::apply()", Exception::Type::SUBSYS_MISMATCH_DIMS);
528 
529  // check that gate matches the dimensions of the subsys
530  std::vector<idx> subsys_dims(subsys.size());
531  for ( idx i = 0; i < subsys.size(); ++i )
532  subsys_dims[i] = dims[subsys[i]];
533  if ( !internal::_check_dims_match_mat(subsys_dims, rA))
534  throw Exception("qpp::apply()",
536  // END EXCEPTION CHECKS
537 
538  //************ ket ************//
539  if ( internal::_check_cvector(rstate)) // we have a ket
540  {
541  // check that dims match state vector
542  if ( !internal::_check_dims_match_cvect(dims, rstate))
543  throw Exception("qpp::apply()",
545 
546  return applyCTRL(rstate, rA, {}, subsys, dims);
547  }
548  //************ density matrix ************//
549  else if ( internal::_check_square_mat(rstate)) // we have a density operator
550  {
551 
552  // check that dims match state matrix
553  if ( !internal::_check_dims_match_mat(dims, rstate))
554  throw Exception("qpp::apply()",
556 
557  return applyCTRL(rstate, rA, {}, subsys, dims);
558  }
559  //************ Exception: not ket nor density matrix ************//
560  else
561  throw Exception("qpp::apply()",
563 }
564 
578 template<typename Derived1, typename Derived2>
580  const Eigen::MatrixBase<Derived1>& state,
581  const Eigen::MatrixBase<Derived2>& A,
582  const std::vector<idx>& subsys,
583  idx d = 2)
584 {
585  const typename Eigen::MatrixBase<Derived1>::EvalReturnType& rstate
586  = state.derived();
587  const dyn_mat<typename Derived1::Scalar>& rA = A.derived();
588 
589  // EXCEPTION CHECKS
590 
591  // check zero size
592  if ( !internal::_check_nonzero_size(rstate))
593  throw Exception("qpp::apply()", Exception::Type::ZERO_SIZE);
594 
595  // check valid dims
596  if ( d == 0 )
597  throw Exception("qpp::apply()", Exception::Type::DIMS_INVALID);
598  // END EXCEPTION CHECKS
599 
600  idx n =
601  static_cast<idx>(std::llround(std::log2(rstate.rows()) /
602  std::log2(d)));
603  std::vector<idx> dims(n, d); // local dimensions vector
604 
605  return apply(rstate, rA, subsys, dims);
606 }
607 
616 template<typename Derived>
617 cmat apply(const Eigen::MatrixBase<Derived>& rho,
618  const std::vector<cmat>& Ks)
619 {
620  const cmat& rrho = rho.derived();
621 
622  // EXCEPTION CHECKS
623 
624  if ( !internal::_check_nonzero_size(rrho))
625  throw Exception("qpp::apply()", Exception::Type::ZERO_SIZE);
626  if ( !internal::_check_square_mat(rrho))
627  throw Exception("qpp::apply()", Exception::Type::MATRIX_NOT_SQUARE);
628  if ( Ks.size() == 0 )
629  throw Exception("qpp::apply()", Exception::Type::ZERO_SIZE);
630  if ( !internal::_check_square_mat(Ks[0]))
631  throw Exception("qpp::apply()", Exception::Type::MATRIX_NOT_SQUARE);
632  if ( Ks[0].rows() != rrho.rows())
633  throw Exception("qpp::apply()",
635  for ( auto&& it : Ks )
636  if ( it.rows() != Ks[0].rows() || it.cols() != Ks[0].rows())
637  throw Exception("qpp::apply()", Exception::Type::DIMS_NOT_EQUAL);
638  // END EXCEPTION CHECKS
639 
640  cmat result = cmat::Zero(rrho.rows(), rrho.rows());
641 
642 #ifdef _WITH_OPENMP_
643 #pragma omp parallel for
644 #endif
645  for ( idx i = 0; i < Ks.size(); ++i )
646  {
647 #ifdef _WITH_OPENMP_
648 #pragma omp critical
649 #endif
650  {
651  result += Ks[i] * rrho * adjoint(Ks[i]);
652  }
653  }
654 
655  return result;
656 }
657 
668 template<typename Derived>
669 cmat apply(const Eigen::MatrixBase<Derived>& rho,
670  const std::vector<cmat>& Ks,
671  const std::vector<idx>& subsys,
672  const std::vector<idx>& dims)
673 {
674  const cmat& rrho = rho.derived();
675 
676  // EXCEPTION CHECKS
677 
678  // check zero sizes
679  if ( !internal::_check_nonzero_size(rrho))
680  throw Exception("qpp::apply()", Exception::Type::ZERO_SIZE);
681 
682  // check square matrix for the rho
683  if ( !internal::_check_square_mat(rrho))
684  throw Exception("qpp::apply()", Exception::Type::MATRIX_NOT_SQUARE);
685 
686  // check that dimension is valid
687  if ( !internal::_check_dims(dims))
688  throw Exception("qpp::apply()", Exception::Type::DIMS_INVALID);
689 
690  // check that dims match rho matrix
691  if ( !internal::_check_dims_match_mat(dims, rrho))
692  throw Exception("qpp::apply()",
694 
695  // check subsys is valid w.r.t. dims
696  if ( !internal::_check_subsys_match_dims(subsys, dims))
697  throw Exception("qpp::apply()",
699 
700  std::vector<idx> subsys_dims(subsys.size());
701  for ( idx i = 0; i < subsys.size(); ++i )
702  subsys_dims[i] = dims[subsys[i]];
703 
704  // check the Kraus operators
705  if ( Ks.size() == 0 )
706  throw Exception("qpp::apply()", Exception::Type::ZERO_SIZE);
707  if ( !internal::_check_square_mat(Ks[0]))
708  throw Exception("qpp::apply()", Exception::Type::MATRIX_NOT_SQUARE);
709  if ( !internal::_check_dims_match_mat(subsys_dims, Ks[0]))
710  throw Exception("qpp::apply()",
712  for ( auto&& it : Ks )
713  if ( it.rows() != Ks[0].rows() || it.cols() != Ks[0].rows())
714  throw Exception("qpp::apply()", Exception::Type::DIMS_NOT_EQUAL);
715  // END EXCEPTION CHECKS
716 
717  cmat result = cmat::Zero(rrho.rows(), rrho.rows());
718 
719  for ( idx i = 0; i < Ks.size(); ++i )
720  result += apply(rrho, Ks[i], subsys, dims);
721 
722  return result;
723 }
724 
735 template<typename Derived>
736 cmat apply(const Eigen::MatrixBase<Derived>& rho,
737  const std::vector<cmat>& Ks,
738  const std::vector<idx>& subsys,
739  idx d = 2)
740 {
741  const cmat& rrho = rho.derived();
742 
743  // EXCEPTION CHECKS
744 
745  // check zero sizes
746  if ( !internal::_check_nonzero_size(rrho))
747  throw Exception("qpp::apply()", Exception::Type::ZERO_SIZE);
748 
749  // check valid dims
750  if ( d == 0 )
751  throw Exception("qpp::apply()", Exception::Type::DIMS_INVALID);
752  // END EXCEPTION CHECKS
753 
754  idx n =
755  static_cast<idx>(std::llround(std::log2(rrho.rows()) /
756  std::log2(d)));
757  std::vector<idx> dims(n, d); // local dimensions vector
758 
759  return apply(rrho, Ks, subsys, dims);
760 }
761 
773 inline cmat kraus2super(const std::vector<cmat>& Ks)
774 {
775  // EXCEPTION CHECKS
776 
777  if ( Ks.size() == 0 )
778  throw Exception("qpp::kraus2super()", Exception::Type::ZERO_SIZE);
779  if ( !internal::_check_nonzero_size(Ks[0]))
780  throw Exception("qpp::kraus2super()", Exception::Type::ZERO_SIZE);
781  if ( !internal::_check_square_mat(Ks[0]))
782  throw Exception("qpp::kraus2super()",
784  for ( auto&& it : Ks )
785  if ( it.rows() != Ks[0].rows() || it.cols() != Ks[0].rows())
786  throw Exception("qpp::kraus2super()",
788  // END EXCEPTION CHECKS
789 
790  idx D = static_cast<idx>(Ks[0].rows());
791 
792  cmat result(D * D, D * D);
793  cmat MN = cmat::Zero(D, D);
794  bra A = bra::Zero(D);
795  ket B = ket::Zero(D);
796  cmat EMN = cmat::Zero(D, D);
797 
798 #ifdef _WITH_OPENMP_
799 #pragma omp parallel for collapse(2)
800 #endif
801  for ( idx m = 0; m < D; ++m )
802  {
803  for ( idx n = 0; n < D; ++n )
804  {
805 #ifdef _WITH_OPENMP_
806 #pragma omp critical
807 #endif
808  {
809  // compute E(|m><n|)
810  MN(m, n) = 1;
811  for ( idx i = 0; i < Ks.size(); ++i )
812  EMN += Ks[i] * MN * adjoint(Ks[i]);
813  MN(m, n) = 0;
814 
815  for ( idx a = 0; a < D; ++a )
816  {
817  A(a) = 1;
818  for ( idx b = 0; b < D; ++b )
819  {
820  // compute result(ab,mn)=<a|E(|m><n)|b>
821  B(b) = 1;
822  result(a * D + b, m * D + n) =
823  static_cast<cmat>(A * EMN * B).value();
824  B(b) = 0;
825  }
826  A(a) = 0;
827  }
828  EMN = cmat::Zero(D, D);
829  }
830  }
831  }
832 
833  return result;
834 }
835 
851 inline cmat kraus2choi(const std::vector<cmat>& Ks)
852 {
853  // EXCEPTION CHECKS
854 
855  if ( Ks.size() == 0 )
856  throw Exception("qpp::kraus2choi()", Exception::Type::ZERO_SIZE);
857  if ( !internal::_check_nonzero_size(Ks[0]))
858  throw Exception("qpp::kraus2choi()", Exception::Type::ZERO_SIZE);
859  if ( !internal::_check_square_mat(Ks[0]))
860  throw Exception("qpp::kraus2choi()",
862  for ( auto&& it : Ks )
863  if ( it.rows() != Ks[0].rows() || it.cols() != Ks[0].rows())
864  throw Exception("qpp::kraus2choi()",
866  // END EXCEPTION CHECKS
867 
868  idx D = static_cast<idx>(Ks[0].rows());
869 
870  // construct the D x D \sum |jj> vector
871  // (un-normalized maximally entangled state)
872  cmat MES = cmat::Zero(D * D, 1);
873  for ( idx a = 0; a < D; ++a )
874  MES(a * D + a) = 1;
875 
876  cmat Omega = MES * adjoint(MES);
877 
878  cmat result = cmat::Zero(D * D, D * D);
879 
880 #ifdef _WITH_OPENMP_
881 #pragma omp parallel for
882 #endif
883  for ( idx i = 0; i < Ks.size(); ++i )
884  {
885 #ifdef _WITH_OPENMP_
886 #pragma omp critical
887 #endif
888  {
889  result += kron(cmat::Identity(D, D), Ks[i]) * Omega
890  * adjoint(kron(cmat::Identity(D, D), Ks[i]));
891  }
892  }
893 
894  return result;
895 }
896 
910 inline std::vector<cmat> choi2kraus(const cmat& A)
911 {
912  // EXCEPTION CHECKS
913 
915  throw Exception("qpp::choi2kraus()", Exception::Type::ZERO_SIZE);
917  throw Exception("qpp::choi2kraus()",
919  idx D = static_cast<idx>(std::llround(
920  std::sqrt(static_cast<double>(A.rows()))));
921  if ( D * D != static_cast<idx>(A.rows()))
922  throw Exception("qpp::choi2kraus()", Exception::Type::DIMS_INVALID);
923  // END EXCEPTION CHECKS
924 
925  dmat ev = hevals(A);
926  cmat evec = hevects(A);
927  std::vector<cmat> result;
928 
929  for ( idx i = 0; i < D * D; ++i )
930  {
931  if ( std::abs(ev(i)) > eps )
932  result.push_back(
933  std::sqrt(std::abs(ev(i))) * reshape(evec.col(i), D, D));
934  }
935 
936  return result;
937 }
938 
946 inline cmat choi2super(const cmat& A)
947 {
948  // EXCEPTION CHECKS
949 
951  throw Exception("qpp::choi2super()", Exception::Type::ZERO_SIZE);
953  throw Exception("qpp::choi2super()",
955  idx D = static_cast<idx>(std::llround(
956  std::sqrt(static_cast<double>(A.rows()))));
957  if ( D * D != static_cast<idx>(A.rows()))
958  throw Exception("qpp::choi2super()", Exception::Type::DIMS_INVALID);
959  // END EXCEPTION CHECKS
960 
961  cmat result(D * D, D * D);
962 
963 #ifdef _WITH_OPENMP_
964 #pragma omp parallel for collapse(4)
965 #endif
966  for ( idx a = 0; a < D; ++a )
967  for ( idx b = 0; b < D; ++b )
968  for ( idx m = 0; m < D; ++m )
969  for ( idx n = 0; n < D; ++n )
970  result(a * D + b, m * D + n) = A(m * D + a, n * D + b);
971 
972  return result;
973 }
974 
982 inline cmat super2choi(const cmat& A)
983 {
984  // EXCEPTION CHECKS
985 
987  throw Exception("qpp::super2choi()", Exception::Type::ZERO_SIZE);
989  throw Exception("qpp::super2choi()",
991  idx D = static_cast<idx>(std::llround(
992  std::sqrt(static_cast<double>(A.rows()))));
993  if ( D * D != static_cast<idx>(A.rows()))
994  throw Exception("qpp::super2choi()", Exception::Type::DIMS_INVALID);
995  // END EXCEPTION CHECKS
996 
997  cmat result(D * D, D * D);
998 
999 #ifdef _WITH_OPENMP_
1000 #pragma omp parallel for collapse(4)
1001 #endif
1002  for ( idx a = 0; a < D; ++a )
1003  for ( idx b = 0; b < D; ++b )
1004  for ( idx m = 0; m < D; ++m )
1005  for ( idx n = 0; n < D; ++n )
1006  result(m * D + a, n * D + b) = A(a * D + b, m * D + n);
1007 
1008  return result;
1009 }
1010 
1025 template<typename Derived>
1026 dyn_mat<typename Derived::Scalar> ptrace1(const Eigen::MatrixBase<Derived>& A,
1027  const std::vector<idx>& dims)
1028 {
1029  const dyn_mat<typename Derived::Scalar>& rA = A.derived();
1030 
1031  // EXCEPTION CHECKS
1032 
1033  // check zero-size
1035  throw Exception("qpp::ptrace1()", Exception::Type::ZERO_SIZE);
1036 
1037  // check that dims is a valid dimension vector
1038  if ( !internal::_check_dims(dims))
1039  throw Exception("qpp::ptrace1()", Exception::Type::DIMS_INVALID);
1040 
1041  // check dims has only 2 elements
1042  if ( dims.size() != 2 )
1043  throw Exception("qpp::ptrace1()", Exception::Type::NOT_BIPARTITE);
1044  // END EXCEPTION CHECKS
1045 
1046  idx DA = dims[0];
1047  idx DB = dims[1];
1048 
1051 
1052  //************ ket ************//
1053  if ( internal::_check_cvector(rA)) // we have a ket
1054  {
1055  // check that dims match the dimension of A
1056  if ( !internal::_check_dims_match_cvect(dims, rA))
1057  throw Exception("qpp::ptrace1()",
1059 
1060  auto worker = [=](idx i, idx j) noexcept
1061  -> typename Derived::Scalar
1062  {
1063  typename Derived::Scalar sum = 0;
1064  for ( idx m = 0; m < DA; ++m )
1065  sum += rA(m * DB + i) * std::conj(rA(m * DB + j));
1066 
1067  return sum;
1068  }; /* end worker */
1069 
1070 #ifdef _WITH_OPENMP_
1071 #pragma omp parallel for collapse(2)
1072 #endif
1073  for ( idx j = 0; j < DB; ++j ) // column major order for speed
1074  for ( idx i = 0; i < DB; ++i )
1075  result(i, j) = worker(i, j);
1076 
1077  return result;
1078  }
1079  //************ density matrix ************//
1080  else if ( internal::_check_square_mat(rA)) // we have a density operator
1081  {
1082  // check that dims match the dimension of A
1083  if ( !internal::_check_dims_match_mat(dims, rA))
1084  throw Exception("qpp::ptrace1()",
1086 
1087  auto worker = [=](idx i, idx j) noexcept
1088  -> typename Derived::Scalar
1089  {
1090  typename Derived::Scalar sum = 0;
1091  for ( idx m = 0; m < DA; ++m )
1092  sum += rA(m * DB + i, m * DB + j);
1093 
1094  return sum;
1095  }; /* end worker */
1096 
1097 #ifdef _WITH_OPENMP_
1098 #pragma omp parallel for collapse(2)
1099 #endif
1100  for ( idx j = 0; j < DB; ++j ) // column major order for speed
1101  for ( idx i = 0; i < DB; ++i )
1102  result(i, j) = worker(i, j);
1103 
1104  return result;
1105  }
1106  //************ Exception: not ket nor density matrix ************//
1107  else
1108  throw Exception("qpp::ptrace1()",
1110 }
1111 
1126 template<typename Derived>
1127 dyn_mat<typename Derived::Scalar> ptrace2(const Eigen::MatrixBase<Derived>& A,
1128  const std::vector<idx>& dims)
1129 {
1130  const dyn_mat<typename Derived::Scalar>& rA = A.derived();
1131 
1132  // EXCEPTION CHECKS
1133 
1134  // check zero-size
1136  throw Exception("qpp::ptrace2()", Exception::Type::ZERO_SIZE);
1137 
1138  // check that dims is a valid dimension vector
1139  if ( !internal::_check_dims(dims))
1140  throw Exception("qpp::ptrace2()", Exception::Type::DIMS_INVALID);
1141 
1142  // check dims has only 2 elements
1143  if ( dims.size() != 2 )
1144  throw Exception("qpp::ptrace2()", Exception::Type::NOT_BIPARTITE);
1145  // END EXCEPTION CHECKS
1146 
1147  idx DA = dims[0];
1148  idx DB = dims[1];
1149 
1152 
1153  //************ ket ************//
1154  if ( internal::_check_cvector(rA)) // we have a ket
1155  {
1156  // check that dims match the dimension of A
1157  if ( !internal::_check_dims_match_cvect(dims, rA))
1158  throw Exception("qpp::ptrace2()",
1160 
1161  auto worker = [=](idx i, idx j) noexcept
1162  -> typename Derived::Scalar
1163  {
1164  typename Derived::Scalar sum = 0;
1165  for ( idx m = 0; m < DB; ++m )
1166  sum += rA(i * DB + m) * std::conj(rA(j * DB + m));
1167 
1168  return sum;
1169  }; /* end worker */
1170 
1171 #ifdef _WITH_OPENMP_
1172 #pragma omp parallel for collapse(2)
1173 #endif
1174  for ( idx j = 0; j < DA; ++j ) // column major order for speed
1175  for ( idx i = 0; i < DA; ++i )
1176  result(i, j) = worker(i, j);
1177 
1178  return result;
1179  }
1180  //************ density matrix ************//
1181  else if ( internal::_check_square_mat(rA)) // we have a density operator
1182  {
1183  // check that dims match the dimension of A
1184  if ( !internal::_check_dims_match_mat(dims, rA))
1185  throw Exception("qpp::ptrace2()",
1187 
1188 #ifdef _WITH_OPENMP_
1189 #pragma omp parallel for collapse(2)
1190 #endif
1191  for ( idx j = 0; j < DA; ++j ) // column major order for speed
1192  for ( idx i = 0; i < DA; ++i )
1193  result(i, j) = trace(rA.block(i * DB, j * DB, DB, DB));
1194 
1195  return result;
1196  }
1197  //************ Exception: not ket nor density matrix ************//
1198  else
1199  throw Exception("qpp::ptrace1()",
1201 }
1202 
1217 template<typename Derived>
1218 dyn_mat<typename Derived::Scalar> ptrace(const Eigen::MatrixBase<Derived>& A,
1219  const std::vector<idx>& subsys,
1220  const std::vector<idx>& dims)
1221 {
1222  const dyn_mat<typename Derived::Scalar>& rA = A.derived();
1223 
1224  // EXCEPTION CHECKS
1225 
1226  // check zero-size
1228  throw Exception("qpp::ptrace()", Exception::Type::ZERO_SIZE);
1229 
1230  // check that dims is a valid dimension vector
1231  if ( !internal::_check_dims(dims))
1232  throw Exception("qpp::ptrace()", Exception::Type::DIMS_INVALID);
1233 
1234  // check that subsys are valid
1235  if ( !internal::_check_subsys_match_dims(subsys, dims))
1236  throw Exception("qpp::ptrace()",
1238  // END EXCEPTION CHECKS
1239 
1240  idx D = static_cast<idx>(rA.rows());
1241  idx n = dims.size();
1242  idx nsubsys = subsys.size();
1243  idx nsubsysbar = n - nsubsys;
1244  idx Dsubsys = 1;
1245  for ( idx i = 0; i < nsubsys; ++i )
1246  Dsubsys *= dims[subsys[i]];
1247  idx Dsubsysbar = D / Dsubsys;
1248 
1249  idx Cdims[maxn];
1250  idx Csubsys[maxn];
1251  idx Cdimssubsys[maxn];
1252  idx Csubsysbar[maxn];
1253  idx Cdimssubsysbar[maxn];
1254 
1255  idx Cmidxcolsubsysbar[maxn];
1256 
1257  std::vector<idx> subsys_bar = complement(subsys, n);
1258  std::copy(std::begin(subsys_bar), std::end(subsys_bar),
1259  std::begin(Csubsysbar));
1260 
1261  for ( idx i = 0; i < n; ++i )
1262  {
1263  Cdims[i] = dims[i];
1264  }
1265  for ( idx i = 0; i < nsubsys; ++i )
1266  {
1267  Csubsys[i] = subsys[i];
1268  Cdimssubsys[i] = dims[subsys[i]];
1269  }
1270  for ( idx i = 0; i < nsubsysbar; ++i )
1271  {
1272  Cdimssubsysbar[i] = dims[subsys_bar[i]];
1273  }
1274 
1276  dyn_mat<typename Derived::Scalar>(Dsubsysbar, Dsubsysbar);
1277 
1278  //************ ket ************//
1279  if ( internal::_check_cvector(rA)) // we have a ket
1280  {
1281  // check that dims match the dimension of A
1282  if ( !internal::_check_dims_match_cvect(dims, rA))
1283  throw Exception("qpp::ptrace()",
1285 
1286  if ( subsys.size() == dims.size())
1287  {
1288  result(0, 0) = (adjoint(rA) * rA).value();
1289  return result;
1290  }
1291 
1292  if ( subsys.size() == 0 )
1293  return rA * adjoint(rA);
1294 
1295  auto worker = [=, &Cmidxcolsubsysbar](idx i) noexcept
1296  -> typename Derived::Scalar
1297  {
1298  // use static allocation for speed!
1299 
1300  idx Cmidxrow[maxn];
1301  idx Cmidxcol[maxn];
1302  idx Cmidxrowsubsysbar[maxn];
1303  idx Cmidxsubsys[maxn];
1304 
1305  /* get the row multi-indexes of the complement */
1306  internal::_n2multiidx(i, nsubsysbar,
1307  Cdimssubsysbar, Cmidxrowsubsysbar);
1308  /* write them in the global row/col multi-indexes */
1309  for ( idx k = 0; k < nsubsysbar; ++k )
1310  {
1311  Cmidxrow[Csubsysbar[k]] = Cmidxrowsubsysbar[k];
1312  Cmidxcol[Csubsysbar[k]] = Cmidxcolsubsysbar[k];
1313  }
1314  typename Derived::Scalar sm = 0;
1315  for ( idx a = 0; a < Dsubsys; ++a )
1316  {
1317  // get the multi-index over which we do the summation
1318  internal::_n2multiidx(a, nsubsys, Cdimssubsys, Cmidxsubsys);
1319  // write it into the global row/col multi-indexes
1320  for ( idx k = 0; k < nsubsys; ++k )
1321  Cmidxrow[Csubsys[k]] = Cmidxcol[Csubsys[k]]
1322  = Cmidxsubsys[k];
1323 
1324  // now do the sum
1325  sm += rA(internal::_multiidx2n(Cmidxrow, n, Cdims)) *
1326  std::conj(rA(internal::_multiidx2n(Cmidxcol, n,
1327  Cdims)));
1328  }
1329 
1330  return sm;
1331  }; /* end worker */
1332 
1333  for ( idx j = 0; j < Dsubsysbar; ++j ) // column major order for speed
1334  {
1335  // compute the column multi-indexes of the complement
1336  internal::_n2multiidx(j, nsubsysbar,
1337  Cdimssubsysbar, Cmidxcolsubsysbar);
1338 #ifdef _WITH_OPENMP_
1339 #pragma omp parallel for
1340 #endif
1341  for ( idx i = 0; i < Dsubsysbar; ++i )
1342  {
1343  result(i, j) = worker(i);
1344  }
1345  }
1346 
1347  return result;
1348  }
1349  //************ density matrix ************//
1350  else if ( internal::_check_square_mat(rA)) // we have a density operator
1351  {
1352  // check that dims match the dimension of A
1353  if ( !internal::_check_dims_match_mat(dims, rA))
1354  throw Exception("qpp::ptrace()",
1356 
1357  if ( subsys.size() == dims.size())
1358  {
1359  result(0, 0) = rA.trace();
1360  return result;
1361  }
1362 
1363  if ( subsys.size() == 0 )
1364  return rA;
1365 
1366  auto worker = [=, &Cmidxcolsubsysbar](idx i) noexcept
1367  -> typename Derived::Scalar
1368  {
1369  // use static allocation for speed!
1370 
1371  idx Cmidxrow[maxn];
1372  idx Cmidxcol[maxn];
1373  idx Cmidxrowsubsysbar[maxn];
1374  idx Cmidxsubsys[maxn];
1375 
1376  /* get the row/col multi-indexes of the complement */
1377  internal::_n2multiidx(i, nsubsysbar,
1378  Cdimssubsysbar, Cmidxrowsubsysbar);
1379  /* write them in the global row/col multi-indexes */
1380  for ( idx k = 0; k < nsubsysbar; ++k )
1381  {
1382  Cmidxrow[Csubsysbar[k]] = Cmidxrowsubsysbar[k];
1383  Cmidxcol[Csubsysbar[k]] = Cmidxcolsubsysbar[k];
1384  }
1385  typename Derived::Scalar sm = 0;
1386  for ( idx a = 0; a < Dsubsys; ++a )
1387  {
1388  // get the multi-index over which we do the summation
1389  internal::_n2multiidx(a, nsubsys, Cdimssubsys, Cmidxsubsys);
1390  // write it into the global row/col multi-indexes
1391  for ( idx k = 0; k < nsubsys; ++k )
1392  Cmidxrow[Csubsys[k]] = Cmidxcol[Csubsys[k]]
1393  = Cmidxsubsys[k];
1394 
1395  // now do the sum
1396  sm += rA(internal::_multiidx2n(Cmidxrow, n, Cdims),
1397  internal::_multiidx2n(Cmidxcol, n, Cdims));
1398  }
1399 
1400  return sm;
1401  }; /* end worker */
1402 
1403  for ( idx j = 0; j < Dsubsysbar; ++j ) // column major order for speed
1404  {
1405  // compute the column multi-indexes of the complement
1406  internal::_n2multiidx(j, nsubsysbar,
1407  Cdimssubsysbar, Cmidxcolsubsysbar);
1408 #ifdef _WITH_OPENMP_
1409 #pragma omp parallel for
1410 #endif
1411  for ( idx i = 0; i < Dsubsysbar; ++i )
1412  {
1413  result(i, j) = worker(i);
1414  }
1415  }
1416 
1417  return result;
1418  }
1419  //************ Exception: not ket nor density matrix ************//
1420  else
1421  throw Exception("qpp::ptrace()",
1423 }
1424 
1439 template<typename Derived>
1440 dyn_mat<typename Derived::Scalar> ptrace(const Eigen::MatrixBase<Derived>& A,
1441  const std::vector<idx>& subsys,
1442  idx d = 2)
1443 {
1444  const dyn_mat<typename Derived::Scalar>& rA = A.derived();
1445 
1446  // EXCEPTION CHECKS
1447 
1448  // check zero size
1450  throw Exception("qpp::ptrace()", Exception::Type::ZERO_SIZE);
1451 
1452  // check valid dims
1453  if ( d == 0 )
1454  throw Exception("qpp::ptrace()", Exception::Type::DIMS_INVALID);
1455  // END EXCEPTION CHECKS
1456 
1457  idx n =
1458  static_cast<idx>(std::llround(std::log2(rA.rows()) /
1459  std::log2(d)));
1460  std::vector<idx> dims(n, d); // local dimensions vector
1461 
1462  return ptrace(rA, subsys, dims);
1463 }
1464 
1478 template<typename Derived>
1480  const Eigen::MatrixBase<Derived>& A,
1481  const std::vector<idx>& subsys,
1482  const std::vector<idx>& dims)
1483 {
1484  const dyn_mat<typename Derived::Scalar>& rA = A.derived();
1485 
1486  // EXCEPTION CHECKS
1487 
1488  // check zero-size
1490  throw Exception("qpp::ptranspose()", Exception::Type::ZERO_SIZE);
1491 
1492  // check that dims is a valid dimension vector
1493  if ( !internal::_check_dims(dims))
1494  throw Exception("qpp::ptranspose()", Exception::Type::DIMS_INVALID);
1495 
1496  // check that subsys are valid
1497  if ( !internal::_check_subsys_match_dims(subsys, dims))
1498  throw Exception("qpp::ptranspose()",
1500  // END EXCEPTION CHECKS
1501 
1502  idx D = static_cast<idx>(rA.rows());
1503  idx numdims = dims.size();
1504  idx numsubsys = subsys.size();
1505  idx Cdims[maxn];
1506  idx Cmidxcol[maxn];
1507  idx Csubsys[maxn];
1508 
1509  // copy dims in Cdims and subsys in Csubsys
1510  for ( idx i = 0; i < numdims; ++i )
1511  Cdims[i] = dims[i];
1512  for ( idx i = 0; i < numsubsys; ++i )
1513  Csubsys[i] = subsys[i];
1514 
1515  dyn_mat<typename Derived::Scalar> result(D, D);
1516 
1517  //************ ket ************//
1518  if ( internal::_check_cvector(rA)) // we have a ket
1519  {
1520  // check that dims match the dimension of A
1521  if ( !internal::_check_dims_match_cvect(dims, rA))
1522  throw Exception("qpp::ptranspose()",
1524 
1525  if ( subsys.size() == dims.size())
1526  return (rA * adjoint(rA)).transpose();
1527 
1528  if ( subsys.size() == 0 )
1529  return rA * adjoint(rA);
1530 
1531  auto worker = [=, &Cmidxcol](idx i) noexcept
1532  -> typename Derived::Scalar
1533  {
1534  // use static allocation for speed!
1535  idx midxcoltmp[maxn];
1536  idx midxrow[maxn];
1537 
1538  for ( idx k = 0; k < numdims; ++k )
1539  midxcoltmp[k] = Cmidxcol[k];
1540 
1541  /* compute the row multi-index */
1542  internal::_n2multiidx(i, numdims, Cdims, midxrow);
1543 
1544  for ( idx k = 0; k < numsubsys; ++k )
1545  std::swap(midxcoltmp[Csubsys[k]], midxrow[Csubsys[k]]);
1546 
1547  /* writes the result */
1548  return rA(internal::_multiidx2n(midxrow, numdims, Cdims)) *
1549  std::conj(rA(internal::_multiidx2n(midxcoltmp, numdims,
1550  Cdims)));
1551  }; /* end worker */
1552 
1553  for ( idx j = 0; j < D; ++j )
1554  {
1555  // compute the column multi-index
1556  internal::_n2multiidx(j, numdims, Cdims, Cmidxcol);
1557 
1558 #ifdef _WITH_OPENMP_
1559 #pragma omp parallel for
1560 #endif
1561  for ( idx i = 0; i < D; ++i )
1562  result(i, j) = worker(i);
1563  }
1564 
1565  return result;
1566  }
1567  //************ density matrix ************//
1568  else if ( internal::_check_square_mat(rA)) // we have a density operator
1569  {
1570  // check that dims match the dimension of A
1571  if ( !internal::_check_dims_match_mat(dims, rA))
1572  throw Exception("qpp::ptranspose()",
1574 
1575  if ( subsys.size() == dims.size())
1576  return rA.transpose();
1577 
1578  if ( subsys.size() == 0 )
1579  return rA;
1580 
1581  auto worker = [=, &Cmidxcol](idx i) noexcept
1582  -> typename Derived::Scalar
1583  {
1584  // use static allocation for speed!
1585  idx midxcoltmp[maxn];
1586  idx midxrow[maxn];
1587 
1588  for ( idx k = 0; k < numdims; ++k )
1589  midxcoltmp[k] = Cmidxcol[k];
1590 
1591  /* compute the row multi-index */
1592  internal::_n2multiidx(i, numdims, Cdims, midxrow);
1593 
1594  for ( idx k = 0; k < numsubsys; ++k )
1595  std::swap(midxcoltmp[Csubsys[k]], midxrow[Csubsys[k]]);
1596 
1597  /* writes the result */
1598  return rA(internal::_multiidx2n(midxrow, numdims, Cdims),
1599  internal::_multiidx2n(midxcoltmp, numdims, Cdims));
1600  }; /* end worker */
1601 
1602  for ( idx j = 0; j < D; ++j )
1603  {
1604  // compute the column multi-index
1605  internal::_n2multiidx(j, numdims, Cdims, Cmidxcol);
1606 
1607 #ifdef _WITH_OPENMP_
1608 #pragma omp parallel for
1609 #endif
1610  for ( idx i = 0; i < D; ++i )
1611  result(i, j) = worker(i);
1612  }
1613 
1614  return result;
1615  }
1616  //************ Exception: not ket nor density matrix ************//
1617  else
1618  throw Exception("qpp::ptranspose()",
1620 }
1621 
1635 template<typename Derived>
1637  const Eigen::MatrixBase<Derived>& A,
1638  const std::vector<idx>& subsys,
1639  idx d = 2)
1640 {
1641  const dyn_mat<typename Derived::Scalar>& rA = A.derived();
1642 
1643  // EXCEPTION CHECKS
1644 
1645  // check zero size
1647  throw Exception("qpp::ptranspose()", Exception::Type::ZERO_SIZE);
1648 
1649  // check valid dims
1650  if ( d == 0 )
1651  throw Exception("qpp::ptranspose()", Exception::Type::DIMS_INVALID);
1652  // END EXCEPTION CHECKS
1653 
1654  idx n =
1655  static_cast<idx>(std::llround(std::log2(rA.rows()) /
1656  std::log2(d)));
1657  std::vector<idx> dims(n, d); // local dimensions vector
1658 
1659  return ptranspose(rA, subsys, dims);
1660 }
1661 
1674 template<typename Derived>
1676  const Eigen::MatrixBase<Derived>& A,
1677  const std::vector<idx>& perm,
1678  const std::vector<idx>& dims)
1679 {
1680  const dyn_mat<typename Derived::Scalar>& rA = A.derived();
1681 
1682  // EXCEPTION CHECKS
1683 
1684  // check zero-size
1686  throw Exception("qpp::syspermute()", Exception::Type::ZERO_SIZE);
1687 
1688  // check that dims is a valid dimension vector
1689  if ( !internal::_check_dims(dims))
1690  throw Exception("qpp::syspermute()", Exception::Type::DIMS_INVALID);
1691 
1692  // check that we have a valid permutation
1693  if ( !internal::_check_perm(perm))
1694  throw Exception("qpp::syspermute()", Exception::Type::PERM_INVALID);
1695 
1696  // check that permutation match dimensions
1697  if ( perm.size() != dims.size())
1698  throw Exception("qpp::syspermute()",
1700  // END EXCEPTION CHECKS
1701 
1702  idx D = static_cast<idx>(rA.rows());
1703  idx numdims = dims.size();
1704 
1706 
1707  //************ ket ************//
1708  if ( internal::_check_cvector(rA)) // we have a column vector
1709  {
1710  idx Cdims[maxn];
1711  idx Cperm[maxn];
1712 
1713  // check that dims match the dimension of rA
1714  if ( !internal::_check_dims_match_cvect(dims, rA))
1715  throw Exception("qpp::syspermute()",
1717 
1718  // copy dims in Cdims and perm in Cperm
1719  for ( idx i = 0; i < numdims; ++i )
1720  {
1721  Cdims[i] = dims[i];
1722  Cperm[i] = perm[i];
1723  }
1724  result.resize(D, 1);
1725 
1726  auto worker = [&Cdims, &Cperm, numdims](idx i) noexcept
1727  -> idx
1728  {
1729  // use static allocation for speed,
1730  // double the size for matrices reshaped as vectors
1731  idx midx[maxn];
1732  idx midxtmp[maxn];
1733  idx permdims[maxn];
1734 
1735  /* compute the multi-index */
1736  internal::_n2multiidx(i, numdims, Cdims, midx);
1737 
1738  for ( idx k = 0; k < numdims; ++k )
1739  {
1740  permdims[k] = Cdims[Cperm[k]]; // permuted dimensions
1741  midxtmp[k] = midx[Cperm[k]];// permuted multi-indexes
1742  }
1743 
1744  return internal::_multiidx2n(midxtmp, numdims, permdims);
1745  }; /* end worker */
1746 
1747 #ifdef _WITH_OPENMP_
1748 #pragma omp parallel for
1749 #endif
1750  for ( idx i = 0; i < D; ++i )
1751  result(worker(i)) = rA(i);
1752 
1753  return result;
1754  }
1755  //************ density matrix ************//
1756  else if ( internal::_check_square_mat(rA)) // we have a density operator
1757  {
1758  idx Cdims[2 * maxn];
1759  idx Cperm[2 * maxn];
1760 
1761  // check that dims match the dimension of rA
1762  if ( !internal::_check_dims_match_mat(dims, rA))
1763  throw Exception("qpp::syspermute()",
1765 
1766  // copy dims in Cdims and perm in Cperm
1767  for ( idx i = 0; i < numdims; ++i )
1768  {
1769  Cdims[i] = dims[i];
1770  Cdims[i + numdims] = dims[i];
1771  Cperm[i] = perm[i];
1772  Cperm[i + numdims] = perm[i] + numdims;
1773  }
1774  result.resize(D * D, 1);
1775  // map A to a column vector
1777  Eigen::Map<dyn_mat<typename Derived::Scalar>>(
1778  const_cast<typename Derived::Scalar*>(rA.data()), D * D,
1779  1);
1780 
1781  auto worker = [&Cdims, &Cperm, numdims](idx i) noexcept
1782  -> idx
1783  {
1784  // use static allocation for speed,
1785  // double the size for matrices reshaped as vectors
1786  idx midx[2 * maxn];
1787  idx midxtmp[2 * maxn];
1788  idx permdims[2 * maxn];
1789 
1790  /* compute the multi-index */
1791  internal::_n2multiidx(i, 2 * numdims, Cdims, midx);
1792 
1793  for ( idx k = 0; k < 2 * numdims; ++k )
1794  {
1795  permdims[k] = Cdims[Cperm[k]]; // permuted dimensions
1796  midxtmp[k] = midx[Cperm[k]];// permuted multi-indexes
1797  }
1798 
1799  return internal::_multiidx2n(midxtmp, 2 * numdims, permdims);
1800  }; /* end worker */
1801 
1802 #ifdef _WITH_OPENMP_
1803 #pragma omp parallel for
1804 #endif
1805  for ( idx i = 0; i < D * D; ++i )
1806  result(worker(i)) = rA(i);
1807 
1808  return reshape(result, D, D);
1809  }
1810  //************ Exception: not ket nor density matrix ************//
1811  else
1812  throw Exception("qpp::syspermute()",
1814 }
1815 
1828 template<typename Derived>
1830  const Eigen::MatrixBase<Derived>& A,
1831  const std::vector<idx>& perm,
1832  idx d = 2)
1833 {
1834  const dyn_mat<typename Derived::Scalar>& rA = A.derived();
1835 
1836  // EXCEPTION CHECKS
1837 
1838  // check zero size
1840  throw Exception("qpp::syspermute()", Exception::Type::ZERO_SIZE);
1841 
1842  // check valid dims
1843  if ( d == 0 )
1844  throw Exception("qpp::syspermute()", Exception::Type::DIMS_INVALID);
1845  // END EXCEPTION CHECKS
1846 
1847  idx n =
1848  static_cast<idx>(std::llround(std::log2(rA.rows()) /
1849  std::log2(d)));
1850  std::vector<idx> dims(n, d); // local dimensions vector
1851 
1852  return syspermute(rA, perm, dims);
1853 }
1854 
1855 } /* namespace qpp */
1856 
1857 #endif /* OPERATIONS_H_ */
bool _check_cvector(const Eigen::MatrixBase< Derived > &A)
Definition: util.h:105
constexpr idx maxn
Maximum number of allowed qu(d)its (subsystems)
Definition: constants.h:74
std::vector< cmat > choi2kraus(const cmat &A)
Orthogonal Kraus operators from Choi matrix.
Definition: operations.h:910
bool _check_subsys_match_dims(const std::vector< idx > &subsys, const std::vector< idx > &dims)
Definition: util.h:183
bool _check_dims_match_mat(const std::vector< idx > &dims, const Eigen::MatrixBase< Derived > &A)
Definition: util.h:141
Eigen::MatrixXd dmat
Real (double precision) dynamic Eigen matrix.
Definition: types.h:71
bool _check_dims_match_cvect(const std::vector< idx > &dims, const Eigen::MatrixBase< Derived > &V)
Definition: util.h:152
constexpr double eps
Used to decide whether a number or expression in double precision is zero or not. ...
Definition: constants.h:67
Eigen::RowVectorXcd bra
Complex (double precision) dynamic Eigen row vector.
Definition: types.h:61
dyn_mat< typename Derived::Scalar > syspermute(const Eigen::MatrixBase< Derived > &A, const std::vector< idx > &perm, const std::vector< idx > &dims)
Subsystem permutation.
Definition: operations.h:1675
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > dyn_mat
Dynamic Eigen matrix over the field specified by Scalar.
Definition: types.h:83
Eigen::VectorXcd ket
Complex (double precision) dynamic Eigen column vector.
Definition: types.h:56
dyn_mat< typename Derived1::Scalar > apply(const Eigen::MatrixBase< Derived1 > &state, const Eigen::MatrixBase< Derived2 > &A, const std::vector< idx > &subsys, const std::vector< idx > &dims)
Applies the gate A to the part subsys of the multi-partite state vector or density matrix state...
Definition: operations.h:492
Quantum++ main namespace.
Definition: codes.h:30
bool _check_square_mat(const Eigen::MatrixBase< Derived > &A)
Definition: util.h:84
dyn_col_vect< double > hevals(const Eigen::MatrixBase< Derived > &A)
Hermitian eigenvalues.
Definition: functions.h:391
cmat choi2super(const cmat &A)
Converts Choi matrix to superoperator matrix.
Definition: operations.h:946
cmat super2choi(const cmat &A)
Converts superoperator matrix to Choi matrix.
Definition: operations.h:982
std::vector< T > complement(std::vector< T > subsys, idx N)
Constructs the complement of a subsystem vector.
Definition: functions.h:1818
dyn_mat< typename Derived::Scalar > ptrace1(const Eigen::MatrixBase< Derived > &A, const std::vector< idx > &dims)
Partial trace.
Definition: operations.h:1026
dyn_mat< typename Derived::Scalar > adjoint(const Eigen::MatrixBase< Derived > &A)
Adjoint.
Definition: functions.h:84
dyn_mat< typename T::Scalar > kron(const T &head)
Kronecker product.
Definition: functions.h:900
dyn_mat< typename Derived::Scalar > ptrace2(const Eigen::MatrixBase< Derived > &A, const std::vector< idx > &dims)
Partial trace.
Definition: operations.h:1127
bool _check_perm(const std::vector< idx > &perm)
Definition: util.h:242
idx _multiidx2n(const idx *midx, idx numdims, const idx *dims) noexcept
Definition: util.h:61
Generates custom exceptions, used when validating function parameters.
Definition: exception.h:39
dyn_mat< typename Derived::Scalar > transpose(const Eigen::MatrixBase< Derived > &A)
Transpose.
Definition: functions.h:44
dyn_mat< typename Derived::Scalar > powm(const Eigen::MatrixBase< Derived > &A, idx n)
Matrix power.
Definition: functions.h:783
cmat hevects(const Eigen::MatrixBase< Derived > &A)
Hermitian eigenvectors.
Definition: functions.h:417
bool _check_nonzero_size(const T &x) noexcept
Definition: util.h:112
cmat kraus2choi(const std::vector< cmat > &Ks)
Choi matrix.
Definition: operations.h:851
dyn_mat< typename Derived::Scalar > ptrace(const Eigen::MatrixBase< Derived > &A, const std::vector< idx > &subsys, const std::vector< idx > &dims)
Partial trace.
Definition: operations.h:1218
Derived::Scalar trace(const Eigen::MatrixBase< Derived > &A)
Trace.
Definition: functions.h:127
cmat kraus2super(const std::vector< cmat > &Ks)
Superoperator matrix.
Definition: operations.h:773
dyn_mat< typename Derived1::Scalar > applyCTRL(const Eigen::MatrixBase< Derived1 > &state, const Eigen::MatrixBase< Derived2 > &A, const std::vector< idx > &ctrl, const std::vector< idx > &subsys, const std::vector< idx > &dims)
Applies the controlled-gate A to the part subsys of the multi-partite state vector or density matrix ...
Definition: operations.h:54
dyn_mat< typename Derived::Scalar > ptranspose(const Eigen::MatrixBase< Derived > &A, const std::vector< idx > &subsys, const std::vector< idx > &dims)
Partial transpose.
Definition: operations.h:1479
Eigen::MatrixXcd cmat
Complex (double precision) dynamic Eigen matrix.
Definition: types.h:66
void _n2multiidx(idx n, idx numdims, const idx *dims, idx *result) noexcept
Definition: util.h:48
std::size_t idx
Non-negative integer index.
Definition: types.h:36
bool _check_dims(const std::vector< idx > &dims)
Definition: util.h:125
Derived::Scalar sum(const Eigen::MatrixBase< Derived > &A)
Element-wise sum of A.
Definition: functions.h:209
dyn_mat< typename Derived::Scalar > reshape(const Eigen::MatrixBase< Derived > &A, idx rows, idx cols)
Reshape.
Definition: functions.h:1140