Quantum++  v0.8
C++11 quantum computing library
gates.h
Go to the documentation of this file.
1 /*
2  * Quantum++
3  *
4  * Copyright (c) 2013 - 2015 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  // check 3-dimensional vector
105  if (n.size() != 3)
106  throw Exception("qpp::Gates::Rn()",
107  "n is not a 3-dimensional vector!");
108 
109  cmat result(2, 2);
110  result = std::cos(theta / 2) * Id2
111  - 1_i * std::sin(theta / 2) * (n[0] * X + n[1] * Y + n[2] * Z);
112 
113  return result;
114  }
115 
116  // one quDit gates
117 
126  cmat Zd(idx D) const
127  {
128  if (D == 0)
129  throw Exception("qpp::Gates::Zd()", Exception::Type::DIMS_INVALID);
130 
131  cmat result = cmat::Zero(D, D);
132  for (idx i = 0; i < D; ++i)
133  result(i, i) = std::pow(omega(D), i);
134 
135  return result;
136  }
137 
147  cmat Fd(idx D) const
148  {
149  if (D == 0)
150  throw Exception("qpp::Gates::Fd()", Exception::Type::DIMS_INVALID);
151 
152  cmat result(D, D);
153 #pragma omp parallel for collapse(2)
154  for (idx j = 0; j < D; ++j) // column major order for speed
155  for (idx i = 0; i < D; ++i)
156  result(i, j) = 1 / std::sqrt(static_cast<double>(D))
157  * std::pow(omega(D), i * j);
158 
159  return result;
160  }
161 
170  cmat Xd(idx D) const
171  {
172  if (D == 0)
173  throw Exception("qpp::Gates::Xd()", Exception::Type::DIMS_INVALID);
174 
175  return Fd(D).inverse() * Zd(D) * Fd(D);
176  }
177 
187  template<typename Derived = Eigen::MatrixXcd>
188  Derived Id(idx D) const
189  {
190  if (D == 0)
191  throw Exception("qpp::Gates::Id()", Exception::Type::DIMS_INVALID);
192 
193  return Derived::Identity(D, D);
194  }
195 
211  template<typename Derived>
212  dyn_mat<typename Derived::Scalar> CTRL(const Eigen::MatrixBase<Derived>& A,
213  const std::vector <idx>& ctrl,
214  const std::vector <idx>& subsys,
215  idx n, idx d = 2) const
216  {
217  const dyn_mat<typename Derived::Scalar>& rA = A;
218 
219  // EXCEPTION CHECKS
220  // check matrix zero size
222  throw Exception("qpp::Gates::CTRL()", Exception::Type::ZERO_SIZE);
223 
224  // check square matrix
226  throw Exception("qpp::Gates::CTRL()",
228 
229  // check lists zero size
230  if (ctrl.size() == 0)
231  throw Exception("qpp::Gates::CTRL()", Exception::Type::ZERO_SIZE);
232  if (subsys.size() == 0)
233  throw Exception("qpp::Gates::CTRL()", Exception::Type::ZERO_SIZE);
234 
235  // check out of range
236  if (n == 0)
237  throw Exception("qpp::Gates::CTRL()",
239 
240  // check valid local dimension
241  if (d == 0)
242  throw Exception("qpp::Gates::CTRL()",
244 
245  // ctrl + gate subsystem vector
246  std::vector <idx> ctrlgate = ctrl;
247  ctrlgate.insert(std::end(ctrlgate), std::begin(subsys),
248  std::end(subsys));
249  std::sort(std::begin(ctrlgate), std::end(ctrlgate));
250 
251  std::vector <idx> dims(n, d); // local dimensions vector
252 
253  // check that ctrl + gate subsystem is valid
254  // with respect to local dimensions
255  if (!internal::_check_subsys_match_dims(ctrlgate, dims))
256  throw Exception("qpp::Gates::CTRL()",
258 
259  // check that subsys list match the dimension of the matrix
260  if (rA.rows() != std::llround(std::pow(d, subsys.size())))
261  throw Exception("qpp::Gates::CTRL()",
263  // END EXCEPTION CHECKS
264 
265  // Use static allocation for speed!
266  idx Cdims[maxn];
267  idx midx_row[maxn];
268  idx midx_col[maxn];
269 
270  idx CdimsA[maxn];
271  idx midxA_row[maxn];
272  idx midxA_col[maxn];
273 
274  idx Cdims_bar[maxn];
275  idx Csubsys_bar[maxn];
276  idx midx_bar[maxn];
277 
278  idx ngate = subsys.size();
279  idx nctrl = ctrl.size();
280  idx nsubsys_bar = n - ctrlgate.size();
281  idx D = static_cast<idx>(std::llround(std::pow(d, n)));
282  idx DA = static_cast<idx>(rA.rows());
283  idx Dsubsys_bar = static_cast<idx>(
284  std::llround(std::pow(d, nsubsys_bar)));
285 
286  // compute the complementary subsystem of ctrlgate w.r.t. dims
287  std::vector <idx> subsys_bar = complement(ctrlgate, n);
288  std::copy(std::begin(subsys_bar), std::end(subsys_bar),
289  std::begin(Csubsys_bar));
290 
291  for (idx k = 0; k < n; ++k)
292  {
293  midx_row[k] = midx_col[k] = 0;
294  Cdims[k] = d;
295  }
296 
297  for (idx k = 0; k < nsubsys_bar; ++k)
298  {
299  Cdims_bar[k] = d;
300  midx_bar[k] = 0;
301  }
302 
303  for (idx k = 0; k < ngate; ++k)
304  {
305  midxA_row[k] = midxA_col[k] = 0;
306  CdimsA[k] = d;
307  }
308 
310  typename Derived::Scalar >
311  ::Identity(D, D);
313 
314  // run over the complement indexes
315  for (idx i = 0; i < Dsubsys_bar; ++i)
316  {
317  // get the complement row multi-index
318  internal::_n2multiidx(i, nsubsys_bar, Cdims_bar, midx_bar);
319  for (idx k = 0; k < d; ++k)
320  {
321  Ak = powm(rA, k); // compute rA^k
322  // run over the subsys row multi-index
323  for (idx a = 0; a < DA; ++a)
324  {
325  // get the subsys row multi-index
326  internal::_n2multiidx(a, ngate, CdimsA, midxA_row);
327 
328  // construct the result row multi-index
329 
330  // first the ctrl part (equal for both row and column)
331  for (idx c = 0; c < nctrl; ++c)
332  midx_row[ctrl[c]] = midx_col[ctrl[c]] = k;
333 
334  // then the complement part (equal for column)
335  for (idx c = 0; c < nsubsys_bar; ++c)
336  midx_row[Csubsys_bar[c]] = midx_col[Csubsys_bar[c]] =
337  midx_bar[c];
338 
339  // then the subsys part
340  for (idx c = 0; c < ngate; ++c)
341  midx_row[subsys[c]] = midxA_row[c];
342 
343  // run over the subsys column multi-index
344  for (idx b = 0; b < DA; ++b)
345  {
346  // get the subsys column multi-index
347  internal::_n2multiidx(b, ngate, CdimsA, midxA_col);
348 
349  // construct the result column multi-index
350  for (idx c = 0; c < ngate; ++c)
351  midx_col[subsys[c]] = midxA_col[c];
352 
353  // finally write the values
354  result(internal::_multiidx2n(midx_row, n, Cdims),
355  internal::_multiidx2n(midx_col, n, Cdims))
356  = Ak(a, b);
357  }
358  }
359  }
360  }
361 
362  return result;
363  }
364 
380  template<typename Derived>
382  const Eigen::MatrixBase<Derived>& A, idx pos,
383  const std::vector <idx>& dims) const
384  {
385  const dyn_mat<typename Derived::Scalar>& rA = A;
386 
387  // check zero-size
389  throw Exception("qpp::Gates::expandout()",
391 
392  // check that dims is a valid dimension vector
393  if (!internal::_check_dims(dims))
394  throw Exception("qpp::Gates::expandout()",
396 
397  // check square matrix
399  throw Exception("qpp::Gates::expandout()",
401 
402  // check that position is valid
403  if (pos > dims.size() - 1)
404  throw Exception("qpp::Gates::expandout()",
406 
407  // check that dims[pos] match the dimension of A
408  if (static_cast<idx>(rA.rows()) != dims[pos])
409  throw Exception("qpp::Gates::expandout()",
411 
412  idx D = std::accumulate(std::begin(dims), std::end(dims),
413  static_cast<idx>(1), std::multiplies<idx>());
415  typename Derived::Scalar >
416  ::Identity(D, D);
417 
418  idx Cdims[maxn];
419  idx midx_row[maxn];
420  idx midx_col[maxn];
421 
422  for (idx k = 0; k < dims.size(); ++k)
423  {
424  midx_row[k] = midx_col[k] = 0;
425  Cdims[k] = dims[k];
426  }
427 
428  // run over the main diagonal multi-indexes
429  for (idx i = 0; i < D; ++i)
430  {
431  // get row multi_index
432  internal::_n2multiidx(i, dims.size(), Cdims, midx_row);
433  // get column multi_index (same as row)
434  internal::_n2multiidx(i, dims.size(), Cdims, midx_col);
435  // run over the gate row multi-index
436  for (idx a = 0; a < static_cast<idx>(rA.rows());
437  ++a)
438  {
439  // construct the total row multi-index
440  midx_row[pos] = a;
441 
442  // run over the gate column multi-index
443  for (idx b = 0;
444  b < static_cast<idx>(rA.cols()); ++b)
445  {
446  // construct the total column multi-index
447  midx_col[pos] = b;
448 
449  // finally write the values
450  result(internal::_multiidx2n(midx_row, dims.size(), Cdims),
451  internal::_multiidx2n(midx_col, dims.size(), Cdims))
452  = rA(a, b);
453  }
454  }
455  }
456 
457  return result;
458  }
459 
460 }; /* class Gates */
461 
462 } /* namespace qpp */
463 
464 #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:381
cmat SWAP
SWAP gate.
Definition: gates.h:55
constexpr idx maxn
Maximum number of allowed qu(d)its (subsystems)
Definition: constants.h:82
bool _check_subsys_match_dims(const std::vector< idx > &subsys, const std::vector< idx > &dims)
Definition: util.h:183
~Gates()=default
Default destructor.
cplx omega(idx D)
D-th root of unity.
Definition: constants.h:104
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > dyn_mat
Dynamic Eigen matrix over the field specified by Scalar.
Definition: types.h:84
cmat Zd(idx D) const
Generalized Z gate for qudits.
Definition: gates.h:126
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:1653
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:147
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:701
bool _check_nonzero_size(const T &x) noexcept
Definition: util.h:113
Derived Id(idx D) const
Identity gate.
Definition: gates.h:188
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:212
constexpr double pi
Definition: constants.h:87
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:67
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:119
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:170