27 #ifndef CLASSES_GATES_H_ 28 #define CLASSES_GATES_H_ 65 H << 1 / std::sqrt(2.), 1 / std::sqrt(2.),
66 1 / std::sqrt(2.), -1 / std::sqrt(2.);
71 T << 1, 0, 0, std::exp(1_i *
pi / 4.0);
72 CNOT.block(2, 2, 2, 2) =
X;
79 SWAP.block(1, 1, 2, 2) =
X;
80 TOF.block(6, 6, 2, 2) =
X;
102 cmat Rn(
double theta,
const std::vector<double>& n)
const 109 "n is not a 3-dimensional vector!");
113 result = std::cos(theta / 2) *
Id2 114 - 1_i * std::sin(theta / 2) * (n[0] *
X + n[1] *
Y + n[2] *
Z);
137 cmat result = cmat::Zero(D, D);
138 for (
idx i = 0; i < D; ++i)
139 result(i, i) = std::pow(
omega(D), i);
164 #pragma omp parallel for collapse(2) 165 #endif // WITH_OPENMP_ 166 for (
idx j = 0; j < D; ++j)
167 for (
idx i = 0; i < D; ++i)
168 result(i, j) = 1 / std::sqrt(D) * std::pow(
omega(D), i * j);
190 return Fd(D).inverse() *
Zd(D) *
Fd(D);
202 template<
typename Derived = Eigen::MatrixXcd>
211 return Derived::Identity(D, D);
229 template<
typename Derived>
231 const std::vector<idx>& ctrl,
232 const std::vector<idx>& subsys,
248 if (ctrl.size() == 0)
250 if (subsys.size() == 0)
262 std::vector<idx> ctrlgate = ctrl;
263 ctrlgate.insert(std::end(ctrlgate), std::begin(subsys),
265 std::sort(std::begin(ctrlgate), std::end(ctrlgate));
267 std::vector<idx> dims(N, d);
277 static_cast<Index
>(std::llround(std::pow(d, subsys.size()))))
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)));
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));
307 for (
idx k = 0; k < N; ++k)
309 midx_row[k] = midx_col[k] = 0;
313 for (
idx k = 0; k < Nsubsys_bar; ++k)
319 for (
idx k = 0; k < Ngate; ++k)
321 midxA_row[k] = midxA_col[k] = 0;
326 typename Derived::Scalar>
331 for (
idx i = 0; i < Dsubsys_bar; ++i)
335 for (
idx k = 0; k < d; ++k)
339 for (
idx a = 0; a < DA; ++a)
347 for (
idx c = 0; c < Nctrl; ++c)
348 midx_row[ctrl[c]] = midx_col[ctrl[c]] = k;
351 for (
idx c = 0; c < Nsubsys_bar; ++c)
352 midx_row[Csubsys_bar[c]] = midx_col[Csubsys_bar[c]] =
356 for (
idx c = 0; c < Ngate; ++c)
357 midx_row[subsys[c]] = midxA_row[c];
360 for (
idx b = 0; b < DA; ++b)
366 for (
idx c = 0; c < Ngate; ++c)
367 midx_col[subsys[c]] = midxA_col[c];
396 template<
typename Derived>
398 const Eigen::MatrixBase<Derived>& A,
idx pos,
399 const std::vector<idx>& dims)
const 418 if (pos > dims.size() - 1)
422 if (static_cast<idx>(rA.rows()) != dims[pos])
426 idx D = std::accumulate(std::begin(dims), std::end(dims),
427 static_cast<idx>(1), std::multiplies<idx>());
429 typename Derived::Scalar>
436 for (
idx k = 0; k < dims.size(); ++k)
438 midx_row[k] = midx_col[k] = 0;
443 for (
idx i = 0; i < D; ++i)
450 for (
idx a = 0; a < static_cast<idx>(rA.rows());
458 b < static_cast<idx>(rA.cols()); ++b)
495 template<
typename Derived>
497 const Eigen::MatrixBase<Derived>& A,
idx pos,
498 const std::initializer_list<idx>& dims)
const 500 return this->
expandout(A, pos, std::vector<idx>(dims));
519 template<
typename Derived>
521 const Eigen::MatrixBase<Derived>& A,
idx pos,
idx N,
idx d = 2)
535 std::vector<idx> dims(N, d);
bool check_nonzero_size(const T &x) noexcept
Definition: util.h:129
Dimension(s) mismatch matrix size exception.
Definition: exception.h:322
cmat SWAP
SWAP gate.
Definition: gates.h:54
constexpr idx maxn
Maximum number of allowed qubits/qudits (subsystems)
Definition: constants.h:71
bool check_subsys_match_dims(const std::vector< idx > &subsys, const std::vector< idx > &dims)
Definition: util.h:221
Derived Id(idx D=2) const
Identity gate.
Definition: gates.h:203
~Gates()=default
Default destructor.
cplx omega(idx D)
D-th root of unity.
Definition: constants.h:93
Custom exception.
Definition: exception.h:636
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > dyn_mat
Dynamic Eigen matrix over the field specified by Scalar.
Definition: types.h:77
idx multiidx2n(const idx *const midx, idx numdims, const idx *const dims) noexcept
Definition: util.h:70
dyn_mat< typename Derived::Scalar > expandout(const Eigen::MatrixBase< Derived > &A, idx pos, const std::initializer_list< idx > &dims) const
Expands out.
Definition: gates.h:496
void n2multiidx(idx n, idx numdims, const idx *const dims, idx *result) noexcept
Definition: util.h:40
Singleton policy class, used internally to implement the singleton pattern via CRTP (Curiously recurr...
Definition: singleton.h:76
Subsystems mismatch dimensions exception.
Definition: exception.h:395
const Singleton class that implements most commonly used gates
Definition: gates.h:36
Quantum++ main namespace.
Definition: codes.h:30
cmat Xd(idx D=2) const
Generalized X gate for qudits.
Definition: gates.h:182
Invalid dimension(s) exception.
Definition: exception.h:287
cmat S
S gate.
Definition: gates.h:47
std::vector< T > complement(std::vector< T > subsys, idx N)
Constructs the complement of a subsystem vector.
Definition: functions.h:1811
cmat T
T gate.
Definition: gates.h:48
cmat FRED
Fredkin gate.
Definition: gates.h:58
cmat CNOTba
Controlled-NOT target control gate.
Definition: gates.h:53
dyn_mat< typename Derived::Scalar > expandout(const Eigen::MatrixBase< Derived > &A, idx pos, idx N, idx d=2) const
Expands out.
Definition: gates.h:520
cmat Zd(idx D=2) const
Generalized Z gate for qudits.
Definition: gates.h:129
cmat H
Hadamard gate.
Definition: gates.h:43
cmat Rn(double theta, const std::vector< double > &n) const
Qubit rotation of theta about the 3-dimensional real (unit) vector n.
Definition: gates.h:102
cmat Id2
Identity gate.
Definition: gates.h:42
cmat X
Pauli Sigma-X gate.
Definition: gates.h:44
dyn_mat< typename Derived::Scalar > powm(const Eigen::MatrixBase< Derived > &A, idx n)
Fast matrix power based on the SQUARE-AND-MULTIPLY algorithm.
Definition: functions.h:777
cmat Fd(idx D=2) const
Fourier transform gate for qudits.
Definition: gates.h:153
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
bool check_square_mat(const Eigen::MatrixBase< Derived > &A)
Definition: util.h:101
constexpr double pi
Definition: constants.h:76
Parameter out of range exception.
Definition: exception.h:567
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:230
Eigen::MatrixXcd cmat
Complex (double precision) dynamic Eigen matrix.
Definition: types.h:60
bool check_dims(const std::vector< idx > &dims)
Definition: util.h:142
cmat CNOT
Controlled-NOT control target gate.
Definition: gates.h:51
std::size_t idx
Non-negative integer index.
Definition: types.h:35
cmat Z
Pauli Sigma-Z gate.
Definition: gates.h:46
cmat CZ
Controlled-Phase gate.
Definition: gates.h:52
Matrix is not square exception.
Definition: exception.h:151
cmat Y
Pauli Sigma-Y gate.
Definition: gates.h:45
Gates()
Initializes the gates.
Definition: gates.h:63
cmat TOF
Toffoli gate.
Definition: gates.h:57
Object has zero size exception.
Definition: exception.h:134