Quantum++  v0.8.6
C++11 quantum computing library
gates.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 CLASSES_GATES_H_
28 #define CLASSES_GATES_H_
29 
30 namespace qpp
31 {
32 
37 class Gates final : public internal::Singleton<const Gates> // const Singleton
38 {
39  friend class internal::Singleton<const Gates>;
40 
41 public:
42  // One qubit gates
43  cmat Id2{cmat::Identity(2, 2)};
44  cmat H{cmat::Zero(2, 2)};
45  cmat X{cmat::Zero(2, 2)};
46  cmat Y{cmat::Zero(2, 2)};
47  cmat Z{cmat::Zero(2, 2)};
48  cmat S{cmat::Zero(2, 2)};
49  cmat T{cmat::Zero(2, 2)};
50 
51  // two qubit gates
52  cmat CNOT{cmat::Identity(4, 4)};
53  cmat CZ{cmat::Identity(4, 4)};
54  cmat CNOTba{cmat::Zero(4, 4)};
55  cmat SWAP{cmat::Identity(4, 4)};
56 
57  // three qubit gates
58  cmat TOF{cmat::Identity(8, 8)};
59  cmat FRED{cmat::Identity(8, 8)};
60 private:
65  {
66  H << 1 / std::sqrt(2.), 1 / std::sqrt(2.),
67  1 / std::sqrt(2.), -1 / std::sqrt(2.);
68  X << 0, 1, 1, 0;
69  Z << 1, 0, 0, -1;
70  Y << 0, -1_i, 1_i, 0;
71  S << 1, 0, 0, 1_i;
72  T << 1, 0, 0, std::exp(1_i * pi / 4.0);
73  CNOT.block(2, 2, 2, 2) = X;
74  CNOTba(0, 0) = 1;
75  CNOTba(1, 3) = 1;
76  CNOTba(2, 2) = 1;
77  CNOTba(3, 1) = 1;
78  CZ(3, 3) = -1;
79 
80  SWAP.block(1, 1, 2, 2) = X;
81  TOF.block(6, 6, 2, 2) = X;
82  FRED.block(4, 4, 4, 4) = SWAP;
83  }
84 
88  ~Gates() = default;
89 
90 public:
91  // variable gates
92 
93  // one qubit gates
94 
102  cmat Rn(double theta, const std::vector<double>& n) const
103  {
104  // EXCEPTION CHECKS
105 
106  // check 3-dimensional vector
107  if (n.size() != 3)
108  throw Exception("qpp::Gates::Rn()",
109  "n is not a 3-dimensional vector!");
110  // END EXCEPTION CHECKS
111 
112  cmat result(2, 2);
113  result = std::cos(theta / 2) * Id2
114  - 1_i * std::sin(theta / 2) * (n[0] * X + n[1] * Y + n[2] * Z);
115 
116  return result;
117  }
118 
119  // one quDit gates
120 
129  cmat Zd(idx D) const
130  {
131  // EXCEPTION CHECKS
132 
133  if (D == 0)
134  throw Exception("qpp::Gates::Zd()", Exception::Type::DIMS_INVALID);
135  // END EXCEPTION CHECKS
136 
137  cmat result = cmat::Zero(D, D);
138  for (idx i = 0; i < D; ++i)
139  result(i, i) = std::pow(omega(D), i);
140 
141  return result;
142  }
143 
153  cmat Fd(idx D) const
154  {
155  // EXCEPTION CHECKS
156 
157  if (D == 0)
158  throw Exception("qpp::Gates::Fd()", Exception::Type::DIMS_INVALID);
159  // END EXCEPTION CHECKS
160 
161  cmat result(D, D);
162 #pragma omp parallel for collapse(2)
163  for (idx j = 0; j < D; ++j) // column major order for speed
164  for (idx i = 0; i < D; ++i)
165  result(i, j) = 1 / std::sqrt(static_cast<double>(D))
166  * std::pow(omega(D), i * j);
167 
168  return result;
169  }
170 
179  cmat Xd(idx D) const
180  {
181  // EXCEPTION CHECKS
182 
183  if (D == 0)
184  throw Exception("qpp::Gates::Xd()", Exception::Type::DIMS_INVALID);
185  // END EXCEPTION CHECKS
186 
187  return Fd(D).inverse() * Zd(D) * Fd(D);
188  }
189 
199  template<typename Derived = Eigen::MatrixXcd>
200  Derived Id(idx D) const
201  {
202  // EXCEPTION CHECKS
203 
204  if (D == 0)
205  throw Exception("qpp::Gates::Id()", Exception::Type::DIMS_INVALID);
206  // END EXCEPTION CHECKS
207 
208  return Derived::Identity(D, D);
209  }
210 
226  template<typename Derived>
227  dyn_mat<typename Derived::Scalar> CTRL(const Eigen::MatrixBase<Derived>& A,
228  const std::vector<idx>& ctrl,
229  const std::vector<idx>& subsys,
230  idx n, idx d = 2) const
231  {
232  const dyn_mat<typename Derived::Scalar>& rA = A;
233 
234  // EXCEPTION CHECKS
235 
236  // check matrix zero size
238  throw Exception("qpp::Gates::CTRL()", Exception::Type::ZERO_SIZE);
239 
240  // check square matrix
242  throw Exception("qpp::Gates::CTRL()",
244 
245  // check lists zero size
246  if (ctrl.size() == 0)
247  throw Exception("qpp::Gates::CTRL()", Exception::Type::ZERO_SIZE);
248  if (subsys.size() == 0)
249  throw Exception("qpp::Gates::CTRL()", Exception::Type::ZERO_SIZE);
250 
251  // check out of range
252  if (n == 0)
253  throw Exception("qpp::Gates::CTRL()",
255 
256  // check valid local dimension
257  if (d == 0)
258  throw Exception("qpp::Gates::CTRL()",
260 
261  // ctrl + gate subsystem vector
262  std::vector<idx> ctrlgate = ctrl;
263  ctrlgate.insert(std::end(ctrlgate), std::begin(subsys),
264  std::end(subsys));
265  std::sort(std::begin(ctrlgate), std::end(ctrlgate));
266 
267  std::vector<idx> dims(n, d); // local dimensions vector
268 
269  // check that ctrl + gate subsystem is valid
270  // with respect to local dimensions
271  if (!internal::_check_subsys_match_dims(ctrlgate, dims))
272  throw Exception("qpp::Gates::CTRL()",
274 
275  // check that subsys list match the dimension of the matrix
276  if (rA.rows() != std::llround(std::pow(d, subsys.size())))
277  throw Exception("qpp::Gates::CTRL()",
279  // END EXCEPTION CHECKS
280 
281  // Use static allocation for speed!
282  idx Cdims[maxn];
283  idx midx_row[maxn];
284  idx midx_col[maxn];
285 
286  idx CdimsA[maxn];
287  idx midxA_row[maxn];
288  idx midxA_col[maxn];
289 
290  idx Cdims_bar[maxn];
291  idx Csubsys_bar[maxn];
292  idx midx_bar[maxn];
293 
294  idx ngate = subsys.size();
295  idx nctrl = ctrl.size();
296  idx nsubsys_bar = n - ctrlgate.size();
297  idx D = static_cast<idx>(std::llround(std::pow(d, n)));
298  idx DA = static_cast<idx>(rA.rows());
299  idx Dsubsys_bar = static_cast<idx>(
300  std::llround(std::pow(d, nsubsys_bar)));
301 
302  // compute the complementary subsystem of ctrlgate w.r.t. dims
303  std::vector<idx> subsys_bar = complement(ctrlgate, n);
304  std::copy(std::begin(subsys_bar), std::end(subsys_bar),
305  std::begin(Csubsys_bar));
306 
307  for (idx k = 0; k < n; ++k)
308  {
309  midx_row[k] = midx_col[k] = 0;
310  Cdims[k] = d;
311  }
312 
313  for (idx k = 0; k < nsubsys_bar; ++k)
314  {
315  Cdims_bar[k] = d;
316  midx_bar[k] = 0;
317  }
318 
319  for (idx k = 0; k < ngate; ++k)
320  {
321  midxA_row[k] = midxA_col[k] = 0;
322  CdimsA[k] = d;
323  }
324 
326  typename Derived::Scalar>
327  ::Identity(D, D);
329 
330  // run over the complement indexes
331  for (idx i = 0; i < Dsubsys_bar; ++i)
332  {
333  // get the complement row multi-index
334  internal::_n2multiidx(i, nsubsys_bar, Cdims_bar, midx_bar);
335  for (idx k = 0; k < d; ++k)
336  {
337  Ak = powm(rA, k); // compute rA^k
338  // run over the subsys row multi-index
339  for (idx a = 0; a < DA; ++a)
340  {
341  // get the subsys row multi-index
342  internal::_n2multiidx(a, ngate, CdimsA, midxA_row);
343 
344  // construct the result row multi-index
345 
346  // first the ctrl part (equal for both row and column)
347  for (idx c = 0; c < nctrl; ++c)
348  midx_row[ctrl[c]] = midx_col[ctrl[c]] = k;
349 
350  // then the complement part (equal for column)
351  for (idx c = 0; c < nsubsys_bar; ++c)
352  midx_row[Csubsys_bar[c]] = midx_col[Csubsys_bar[c]] =
353  midx_bar[c];
354 
355  // then the subsys part
356  for (idx c = 0; c < ngate; ++c)
357  midx_row[subsys[c]] = midxA_row[c];
358 
359  // run over the subsys column multi-index
360  for (idx b = 0; b < DA; ++b)
361  {
362  // get the subsys column multi-index
363  internal::_n2multiidx(b, ngate, CdimsA, midxA_col);
364 
365  // construct the result column multi-index
366  for (idx c = 0; c < ngate; ++c)
367  midx_col[subsys[c]] = midxA_col[c];
368 
369  // finally write the values
370  result(internal::_multiidx2n(midx_row, n, Cdims),
371  internal::_multiidx2n(midx_col, n, Cdims))
372  = Ak(a, b);
373  }
374  }
375  }
376  }
377 
378  return result;
379  }
380 
396  template<typename Derived>
398  const Eigen::MatrixBase<Derived>& A, idx pos,
399  const std::vector<idx>& dims) const
400  {
401  const dyn_mat<typename Derived::Scalar>& rA = A;
402 
403  // EXCEPTION CHECKS
404 
405  // check zero-size
407  throw Exception("qpp::Gates::expandout()",
409 
410  // check that dims is a valid dimension vector
411  if (!internal::_check_dims(dims))
412  throw Exception("qpp::Gates::expandout()",
414 
415  // check square matrix
417  throw Exception("qpp::Gates::expandout()",
419 
420  // check that position is valid
421  if (pos > dims.size() - 1)
422  throw Exception("qpp::Gates::expandout()",
424 
425  // check that dims[pos] match the dimension of A
426  if (static_cast<idx>(rA.rows()) != dims[pos])
427  throw Exception("qpp::Gates::expandout()",
429  // END EXCEPTION CHECKS
430 
431  idx D = std::accumulate(std::begin(dims), std::end(dims),
432  static_cast<idx>(1), std::multiplies<idx>());
434  typename Derived::Scalar>
435  ::Identity(D, D);
436 
437  idx Cdims[maxn];
438  idx midx_row[maxn];
439  idx midx_col[maxn];
440 
441  for (idx k = 0; k < dims.size(); ++k)
442  {
443  midx_row[k] = midx_col[k] = 0;
444  Cdims[k] = dims[k];
445  }
446 
447  // run over the main diagonal multi-indexes
448  for (idx i = 0; i < D; ++i)
449  {
450  // get row multi_index
451  internal::_n2multiidx(i, dims.size(), Cdims, midx_row);
452  // get column multi_index (same as row)
453  internal::_n2multiidx(i, dims.size(), Cdims, midx_col);
454  // run over the gate row multi-index
455  for (idx a = 0; a < static_cast<idx>(rA.rows());
456  ++a)
457  {
458  // construct the total row multi-index
459  midx_row[pos] = a;
460 
461  // run over the gate column multi-index
462  for (idx b = 0;
463  b < static_cast<idx>(rA.cols()); ++b)
464  {
465  // construct the total column multi-index
466  midx_col[pos] = b;
467 
468  // finally write the values
469  result(internal::_multiidx2n(midx_row, dims.size(), Cdims),
470  internal::_multiidx2n(midx_col, dims.size(), Cdims))
471  = rA(a, b);
472  }
473  }
474  }
475 
476  return result;
477  }
478 
479 }; /* class Gates */
480 
481 } /* namespace qpp */
482 
483 #endif /* CLASSES_GATES_H_ */
dyn_mat< typename Derived::Scalar > expandout(const Eigen::MatrixBase< Derived > &A, idx pos, const std::vector< idx > &dims) const
Expands out.
Definition: gates.h:397
cmat SWAP
SWAP gate.
Definition: gates.h:55
constexpr idx maxn
Maximum number of allowed qu(d)its (subsystems)
Definition: constants.h:80
bool _check_subsys_match_dims(const std::vector< idx > &subsys, const std::vector< idx > &dims)
Definition: util.h:190
~Gates()=default
Default destructor.
cplx omega(idx D)
D-th root of unity.
Definition: constants.h:102
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > dyn_mat
Dynamic Eigen matrix over the field specified by Scalar.
Definition: types.h:83
cmat Zd(idx D) const
Generalized Z gate for qudits.
Definition: gates.h:129
Singleton policy class, used internally to implement the singleton pattern via CRTP (Curiously recurr...
Definition: singleton.h:77
const Singleton class that implements most commonly used gates
Definition: gates.h:37
Quantum++ main namespace.
Definition: codes.h:30
bool _check_square_mat(const Eigen::MatrixBase< Derived > &A)
Definition: util.h:77
cmat S
S gate.
Definition: gates.h:48
std::vector< T > complement(std::vector< T > subsys, idx N)
Constructs the complement of a subsystem vector.
Definition: functions.h:1806
cmat T
T gate.
Definition: gates.h:49
cmat FRED
Fredkin gate.
Definition: gates.h:59
cmat CNOTba
Controlled-NOT target control gate.
Definition: gates.h:54
cmat Fd(idx D) const
Fourier transform gate for qudits.
Definition: gates.h:153
cmat H
Hadamard gate.
Definition: gates.h:44
cmat Id2
Identity gate.
Definition: gates.h:43
cmat X
Pauli Sigma-X gate.
Definition: gates.h:45
idx _multiidx2n(const idx *midx, idx numdims, const idx *dims) noexcept
Definition: util.h:54
Generates custom exceptions, used when validating function parameters.
Definition: exception.h:39
dyn_mat< typename Derived::Scalar > powm(const Eigen::MatrixBase< Derived > &A, idx n)
Matrix power.
Definition: functions.h:783
bool _check_nonzero_size(const T &x) noexcept
Definition: util.h:113
Derived Id(idx D) const
Identity gate.
Definition: gates.h:200
dyn_mat< typename Derived::Scalar > CTRL(const Eigen::MatrixBase< Derived > &A, const std::vector< idx > &ctrl, const std::vector< idx > &subsys, idx n, idx d=2) const
Generates the multi-partite multiple-controlled-A gate in matrix form.
Definition: gates.h:227
constexpr double pi
Definition: constants.h:85
cmat Rn(double theta, const std::vector< double > &n) const
Rotation of theta about the 3-dimensional real unit vector n.
Definition: gates.h:102
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:41
cmat CNOT
Controlled-NOT control target gate.
Definition: gates.h:52
std::size_t idx
Non-negative integer index.
Definition: types.h:36
cmat Z
Pauli Sigma-Z gate.
Definition: gates.h:47
cmat CZ
Controlled-Phase gate.
Definition: gates.h:53
cmat Y
Pauli Sigma-Y gate.
Definition: gates.h:46
bool _check_dims(const std::vector< idx > &dims)
Definition: util.h:126
Gates()
Initializes the gates.
Definition: gates.h:64
cmat TOF
Toffoli gate.
Definition: gates.h:58
cmat Xd(idx D) const
Generalized X gate for qudits.
Definition: gates.h:179