27 #ifndef CLASSES_GATES_H_
28 #define CLASSES_GATES_H_
66 H << 1 / std::sqrt(2.), 1 / std::sqrt(2.),
67 1 / std::sqrt(2.), -1 / std::sqrt(2.);
72 T << 1, 0, 0, std::exp(1_i *
pi / 4.0);
73 CNOT.block(2, 2, 2, 2) =
X;
80 SWAP.block(1, 1, 2, 2) =
X;
81 TOF.block(6, 6, 2, 2) =
X;
102 cmat Rn(
double theta,
const std::vector<double>& n)
const
107 "n is not a 3-dimensional vector!");
110 result = std::cos(theta / 2) *
Id2
111 - 1_i * std::sin(theta / 2) * (n[0] *
X + n[1] *
Y + n[2] *
Z);
131 cmat result = cmat::Zero(D, D);
132 for (
idx i = 0; i < D; ++i)
133 result(i, i) = std::pow(
omega(D), i);
153 #pragma omp parallel for collapse(2)
154 for (
idx j = 0; j < D; ++j)
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);
175 return Fd(D).inverse() *
Zd(D) *
Fd(D);
187 template<
typename Derived = Eigen::MatrixXcd>
193 return Derived::Identity(D, D);
211 template<
typename Derived>
213 const std::vector<idx>& ctrl,
214 const std::vector<idx>& subsys,
230 if (ctrl.size() == 0)
232 if (subsys.size() == 0)
246 std::vector<idx> ctrlgate = ctrl;
247 ctrlgate.insert(std::end(ctrlgate), std::begin(subsys),
249 std::sort(std::begin(ctrlgate), std::end(ctrlgate));
251 std::vector<idx> dims(n, d);
260 if (rA.rows() != std::llround(std::pow(d, subsys.size())))
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)));
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));
291 for (
idx k = 0; k < n; ++k)
293 midx_row[k] = midx_col[k] = 0;
297 for (
idx k = 0; k < nsubsys_bar; ++k)
303 for (
idx k = 0; k < ngate; ++k)
305 midxA_row[k] = midxA_col[k] = 0;
310 typename Derived::Scalar>::Identity(D, D);
314 for (
idx i = 0; i < Dsubsys_bar; ++i)
318 for (
idx k = 0; k < d; ++k)
322 for (
idx a = 0; a < DA; ++a)
330 for (
idx c = 0; c < nctrl; ++c)
331 midx_row[ctrl[c]] = midx_col[ctrl[c]] = k;
334 for (
idx c = 0; c < nsubsys_bar; ++c)
335 midx_row[Csubsys_bar[c]] = midx_col[Csubsys_bar[c]] =
339 for (
idx c = 0; c < ngate; ++c)
340 midx_row[subsys[c]] = midxA_row[c];
343 for (
idx b = 0; b < DA; ++b)
349 for (
idx c = 0; c < ngate; ++c)
350 midx_col[subsys[c]] = midxA_col[c];
379 template<
typename Derived>
381 const Eigen::MatrixBase<Derived>& A,
idx pos,
382 const std::vector<idx>& dims)
const
388 throw Exception(
"qpp::Gates::expandout()",
393 throw Exception(
"qpp::Gates::expandout()",
398 throw Exception(
"qpp::Gates::expandout()",
402 if (pos > dims.size() - 1)
403 throw Exception(
"qpp::Gates::expandout()",
407 if (static_cast<idx>(rA.rows()) != dims[pos])
408 throw Exception(
"qpp::Gates::expandout()",
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);
420 for (
idx k = 0; k < dims.size(); ++k)
422 midx_row[k] = midx_col[k] = 0;
427 for (
idx i = 0; i < D; ++i)
434 for (
idx a = 0; a < static_cast<idx>(rA.rows());
442 b < static_cast<idx>(rA.cols()); ++b)
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