Quantum++  v0.7
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 : 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:
64  Gates()
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>::Identity(D, D);
312 
313  // run over the complement indexes
314  for (idx i = 0; i < Dsubsys_bar; ++i)
315  {
316  // get the complement row multi-index
317  internal::_n2multiidx(i, nsubsys_bar, Cdims_bar, midx_bar);
318  for (idx k = 0; k < d; ++k)
319  {
320  Ak = powm(rA, k); // compute rA^k
321  // run over the subsys row multi-index
322  for (idx a = 0; a < DA; ++a)
323  {
324  // get the subsys row multi-index
325  internal::_n2multiidx(a, ngate, CdimsA, midxA_row);
326 
327  // construct the result row multi-index
328 
329  // first the ctrl part (equal for both row and column)
330  for (idx c = 0; c < nctrl; ++c)
331  midx_row[ctrl[c]] = midx_col[ctrl[c]] = k;
332 
333  // then the complement part (equal for column)
334  for (idx c = 0; c < nsubsys_bar; ++c)
335  midx_row[Csubsys_bar[c]] = midx_col[Csubsys_bar[c]] =
336  midx_bar[c];
337 
338  // then the subsys part
339  for (idx c = 0; c < ngate; ++c)
340  midx_row[subsys[c]] = midxA_row[c];
341 
342  // run over the subsys column multi-index
343  for (idx b = 0; b < DA; ++b)
344  {
345  // get the subsys column multi-index
346  internal::_n2multiidx(b, ngate, CdimsA, midxA_col);
347 
348  // construct the result column multi-index
349  for (idx c = 0; c < ngate; ++c)
350  midx_col[subsys[c]] = midxA_col[c];
351 
352  // finally write the values
353  result(internal::_multiidx2n(midx_row, n, Cdims),
354  internal::_multiidx2n(midx_col, n, Cdims))
355  = Ak(a, b);
356  }
357  }
358  }
359  }
360 
361  return result;
362  }
363 
379  template<typename Derived>
381  const Eigen::MatrixBase<Derived>& A, idx pos,
382  const std::vector<idx>& dims) const
383  {
384  const dyn_mat<typename Derived::Scalar>& rA = A;
385 
386  // check zero-size
388  throw Exception("qpp::Gates::expandout()",
390 
391  // check that dims is a valid dimension vector
392  if (!internal::_check_dims(dims))
393  throw Exception("qpp::Gates::expandout()",
395 
396  // check square matrix
398  throw Exception("qpp::Gates::expandout()",
400 
401  // check that position is valid
402  if (pos > dims.size() - 1)
403  throw Exception("qpp::Gates::expandout()",
405 
406  // check that dims[pos] match the dimension of A
407  if (static_cast<idx>(rA.rows()) != dims[pos])
408  throw Exception("qpp::Gates::expandout()",
410 
411  idx D = std::accumulate(std::begin(dims), std::end(dims),
412  static_cast<idx>(1), std::multiplies<idx>());
414  typename Derived::Scalar>::Identity(D, D);
415 
416  idx Cdims[maxn];
417  idx midx_row[maxn];
418  idx midx_col[maxn];
419 
420  for (idx k = 0; k < dims.size(); ++k)
421  {
422  midx_row[k] = midx_col[k] = 0;
423  Cdims[k] = dims[k];
424  }
425 
426  // run over the main diagonal multi-indexes
427  for (idx i = 0; i < D; ++i)
428  {
429  // get row multi_index
430  internal::_n2multiidx(i, dims.size(), Cdims, midx_row);
431  // get column multi_index (same as row)
432  internal::_n2multiidx(i, dims.size(), Cdims, midx_col);
433  // run over the gate row multi-index
434  for (idx a = 0; a < static_cast<idx>(rA.rows());
435  ++a)
436  {
437  // construct the total row multi-index
438  midx_row[pos] = a;
439 
440  // run over the gate column multi-index
441  for (idx b = 0;
442  b < static_cast<idx>(rA.cols()); ++b)
443  {
444  // construct the total column multi-index
445  midx_col[pos] = b;
446 
447  // finally write the values
448  result(internal::_multiidx2n(midx_row, dims.size(), Cdims),
449  internal::_multiidx2n(midx_col, dims.size(), Cdims))
450  = rA(a, b);
451  }
452  }
453  }
454 
455  return result;
456  }
457 
458 }; /* class Gates */
459 
460 } /* namespace qpp */
461 
462 #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:380
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:189
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:73
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:83
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:1652
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:60
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:119
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:56
void _n2multiidx(idx n, idx numdims, const idx *dims, idx *result) noexcept
Definition: util.h:47
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:125
cmat TOF
Toffoli gate.
Definition: gates.h:58
cmat Xd(idx D) const
Generalized X gate for qudits.
Definition: gates.h:170