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);
164 #pragma omp parallel for collapse(2)
166 for (
idx j = 0; j < D; ++j )
167 for (
idx i = 0; i < D; ++i )
168 result(i, j) = 1 / std::sqrt(static_cast<double>(D))
169 * 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,
249 if ( ctrl.size() == 0 )
251 if ( subsys.size() == 0 )
265 std::vector<idx> ctrlgate = ctrl;
266 ctrlgate.insert(std::end(ctrlgate), std::begin(subsys),
268 std::sort(std::begin(ctrlgate), std::end(ctrlgate));
270 std::vector<idx> dims(n, d);
279 if ( rA.rows() != std::llround(std::pow(d, subsys.size())))
297 idx ngate = subsys.size();
298 idx nctrl = ctrl.size();
299 idx nsubsys_bar = n - ctrlgate.size();
300 idx D =
static_cast<idx>(std::llround(std::pow(d, n)));
301 idx DA =
static_cast<idx>(rA.rows());
302 idx Dsubsys_bar =
static_cast<idx>(
303 std::llround(std::pow(d, nsubsys_bar)));
306 std::vector<idx> subsys_bar =
complement(ctrlgate, n);
307 std::copy(std::begin(subsys_bar), std::end(subsys_bar),
308 std::begin(Csubsys_bar));
310 for (
idx k = 0; k < n; ++k )
312 midx_row[k] = midx_col[k] = 0;
316 for (
idx k = 0; k < nsubsys_bar; ++k )
322 for (
idx k = 0; k < ngate; ++k )
324 midxA_row[k] = midxA_col[k] = 0;
329 typename Derived::Scalar>
334 for (
idx i = 0; i < Dsubsys_bar; ++i )
338 for (
idx k = 0; k < d; ++k )
342 for (
idx a = 0; a < DA; ++a )
350 for (
idx c = 0; c < nctrl; ++c )
351 midx_row[ctrl[c]] = midx_col[ctrl[c]] = k;
354 for (
idx c = 0; c < nsubsys_bar; ++c )
355 midx_row[Csubsys_bar[c]] = midx_col[Csubsys_bar[c]] =
359 for (
idx c = 0; c < ngate; ++c )
360 midx_row[subsys[c]] = midxA_row[c];
363 for (
idx b = 0; b < DA; ++b )
369 for (
idx c = 0; c < ngate; ++c )
370 midx_col[subsys[c]] = midxA_col[c];
399 template<
typename Derived>
401 const Eigen::MatrixBase<Derived>& A,
idx pos,
402 const std::vector<idx>& dims)
const
410 throw Exception(
"qpp::Gates::expandout()",
415 throw Exception(
"qpp::Gates::expandout()",
420 throw Exception(
"qpp::Gates::expandout()",
424 if ( pos > dims.size() - 1 )
425 throw Exception(
"qpp::Gates::expandout()",
429 if ( static_cast<idx>(rA.rows()) != dims[pos] )
430 throw Exception(
"qpp::Gates::expandout()",
434 idx D = std::accumulate(std::begin(dims), std::end(dims),
435 static_cast<idx>(1), std::multiplies<idx>());
437 typename Derived::Scalar>
444 for (
idx k = 0; k < dims.size(); ++k )
446 midx_row[k] = midx_col[k] = 0;
451 for (
idx i = 0; i < D; ++i )
458 for (
idx a = 0; a < static_cast<idx>(rA.rows());
466 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:400
cmat SWAP
SWAP gate.
Definition: gates.h:55
constexpr idx maxn
Maximum number of allowed qu(d)its (subsystems)
Definition: constants.h:74
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:96
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:84
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:1818
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:61
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:112
Derived Id(idx D) const
Identity gate.
Definition: gates.h:203
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
constexpr double pi
Definition: constants.h:79
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:48
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
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:182