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
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);
162 #pragma omp parallel for collapse(2)
163 for (
idx j = 0; j < D; ++j)
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);
187 return Fd(D).inverse() *
Zd(D) *
Fd(D);
199 template<
typename Derived = Eigen::MatrixXcd>
208 return Derived::Identity(D, D);
226 template<
typename Derived>
228 const std::vector<idx>& ctrl,
229 const std::vector<idx>& subsys,
246 if (ctrl.size() == 0)
248 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);
276 if (rA.rows() != 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
407 throw Exception(
"qpp::Gates::expandout()",
412 throw Exception(
"qpp::Gates::expandout()",
417 throw Exception(
"qpp::Gates::expandout()",
421 if (pos > dims.size() - 1)
422 throw Exception(
"qpp::Gates::expandout()",
426 if (static_cast<idx>(rA.rows()) != dims[pos])
427 throw Exception(
"qpp::Gates::expandout()",
431 idx D = std::accumulate(std::begin(dims), std::end(dims),
432 static_cast<idx>(1), std::multiplies<idx>());
434 typename Derived::Scalar>
441 for (
idx k = 0; k < dims.size(); ++k)
443 midx_row[k] = midx_col[k] = 0;
448 for (
idx i = 0; i < D; ++i)
455 for (
idx a = 0; a < static_cast<idx>(rA.rows());
463 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: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