27 #ifndef INTERNAL_UTIL_H_
28 #define INTERNAL_UTIL_H_
51 for (
idx i = 0; i < numdims; ++i)
53 result[numdims - i - 1] = n % (dims[numdims - i - 1]);
54 n /= (dims[numdims - i - 1]);
70 part_prod[numdims - 1] = 1;
71 for (
idx i = 1; i < numdims; ++i)
73 part_prod[numdims - i - 1] =
74 part_prod[numdims - i] * dims[numdims - i];
75 result += midx[numdims - i - 1] * part_prod[numdims - i - 1];
78 return result + midx[numdims - 1];
82 template<
typename Derived>
87 return rA.rows() == rA.cols();
91 template<
typename Derived>
96 return rA.rows() == 1 || rA.cols() == 1;
100 template<
typename Derived>
105 return rA.rows() == 1;
109 template<
typename Derived>
114 return rA.cols() == 1;
121 return x.size() != 0;
127 if (dims.size() == 0)
130 return std::find_if(std::begin(dims), std::end(dims),
131 [dims](
idx i) ->
bool
133 if (i == 0)
return true;
135 }) == std::end(dims);
140 template<
typename Derived>
142 const Eigen::MatrixBase<Derived>& A)
146 idx proddim = std::accumulate(std::begin(dims), std::end(dims),
147 static_cast<idx>(1), std::multiplies<idx>());
149 return proddim ==
static_cast<idx>(rA.rows());
153 template<
typename Derived>
155 const Eigen::MatrixBase<Derived>& V)
159 idx proddim = std::accumulate(std::begin(dims), std::end(dims),
160 static_cast<idx>(1), std::multiplies<idx>());
162 return proddim ==
static_cast<idx>(rV.rows());
166 template<
typename Derived>
168 const Eigen::MatrixBase<Derived>& V)
172 idx proddim = std::accumulate(std::begin(dims), std::end(dims),
173 static_cast<idx>(1), std::multiplies<idx>());;
175 return proddim ==
static_cast<idx>(rV.cols());
190 const std::vector<idx>& dims)
197 if (subsys.size() > dims.size())
201 std::vector<idx> subsyssort = subsys;
202 std::sort(std::begin(subsyssort), std::end(subsyssort));
205 if (std::unique(std::begin(subsyssort), std::end(subsyssort))
206 != std::end(subsyssort))
210 return std::find_if(std::begin(subsyssort), std::end(subsyssort),
211 [dims](
idx i) ->
bool
213 return i > dims.size() - 1;
214 }) == std::end(subsyssort);
218 template<
typename Derived>
223 return rA.rows() == 2 && rA.cols() == 2;
227 template<
typename Derived>
232 return rV.rows() == 2 && rV.cols() == 1;
236 template<
typename Derived>
241 return rV.rows() == 1 && rV.cols() == 2;
245 template<
typename Derived>
250 return (rV.rows() == 1 && rV.cols() == 2) ||
251 (rV.rows() == 2 && rV.cols() == 1);
258 if (perm.size() == 0)
261 std::vector<idx> ordered(perm.size());
262 std::iota(std::begin(ordered), std::end(ordered), 0);
264 return std::is_permutation(std::begin(ordered), std::end(ordered),
270 template<
typename Derived1,
typename Derived2>
272 const Eigen::MatrixBase<Derived2>& B)
280 if (!std::is_same<
typename Derived1::Scalar,
281 typename Derived2::Scalar>::value)
292 idx Acols =
static_cast<idx>(rA.cols());
293 idx Arows =
static_cast<idx>(rA.rows());
294 idx Bcols =
static_cast<idx>(rB.cols());
295 idx Brows =
static_cast<idx>(rB.rows());
298 result.resize(Arows * Brows, Acols * Bcols);
300 #pragma omp parallel for collapse(2)
301 for (
idx j = 0; j < Acols; ++j)
302 for (
idx i = 0; i < Arows; ++i)
303 result.block(i * Brows, j * Bcols, Brows, Bcols) = rA(i, j) * rB;
310 template<
typename Derived1,
typename Derived2>
312 const Eigen::MatrixBase<Derived1>& A,
313 const Eigen::MatrixBase<Derived2>& B)
321 if (!std::is_same<
typename Derived1::Scalar,
322 typename Derived2::Scalar>::value)
333 idx Acols =
static_cast<idx>(rA.cols());
334 idx Arows =
static_cast<idx>(rA.rows());
335 idx Bcols =
static_cast<idx>(rB.cols());
336 idx Brows =
static_cast<idx>(rB.rows());
342 result.block(0, 0, Arows, Acols) = rA;
343 result.block(Arows, Acols, Brows, Bcols) = rB;
356 template<
typename T,
typename First,
typename ... Args>
359 v.emplace_back(std::forward<First>(first));
bool _check_cvector(const Eigen::MatrixBase< Derived > &A)
Definition: util.h:110
bool _check_qubit_cvector(const Eigen::MatrixBase< Derived > &V) noexcept
Definition: util.h:228
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
bool _check_dims_match_mat(const std::vector< idx > &dims, const Eigen::MatrixBase< Derived > &A)
Definition: util.h:141
bool _check_dims_match_cvect(const std::vector< idx > &dims, const Eigen::MatrixBase< Derived > &V)
Definition: util.h:154
dyn_mat< typename Derived1::Scalar > _kron2(const Eigen::MatrixBase< Derived1 > &A, const Eigen::MatrixBase< Derived2 > &B)
Definition: util.h:271
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > dyn_mat
Dynamic Eigen matrix over the field specified by Scalar.
Definition: types.h:73
bool _check_vector(const Eigen::MatrixBase< Derived > &A)
Definition: util.h:92
Quantum++ main namespace.
Definition: codes.h:30
bool _check_dims_match_rvect(const std::vector< idx > &dims, const Eigen::MatrixBase< Derived > &V)
Definition: util.h:167
bool _check_square_mat(const Eigen::MatrixBase< Derived > &A)
Definition: util.h:83
bool _check_rvector(const Eigen::MatrixBase< Derived > &A)
Definition: util.h:101
bool _check_qubit_matrix(const Eigen::MatrixBase< Derived > &A) noexcept
Definition: util.h:219
bool _check_perm(const std::vector< idx > &perm)
Definition: util.h:256
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
void variadic_vector_emplace(std::vector< T > &)
Definition: util.h:351
bool _check_nonzero_size(const T &x) noexcept
Definition: util.h:119
dyn_mat< typename Derived1::Scalar > _dirsum2(const Eigen::MatrixBase< Derived1 > &A, const Eigen::MatrixBase< Derived2 > &B)
Definition: util.h:311
bool _check_qubit_vector(const Eigen::MatrixBase< Derived > &V) noexcept
Definition: util.h:246
void _n2multiidx(idx n, idx numdims, const idx *dims, idx *result) noexcept
Definition: util.h:47
std::size_t idx
Non-negative integer index.
Definition: types.h:36
bool _check_dims(const std::vector< idx > &dims)
Definition: util.h:125
bool _check_eq_dims(const std::vector< idx > &dims, idx dim) noexcept
Definition: util.h:179
bool _check_qubit_rvector(const Eigen::MatrixBase< Derived > &V) noexcept
Definition: util.h:237