27 #ifndef INTERNAL_UTIL_H_
28 #define INTERNAL_UTIL_H_
45 for (
idx i = 0; i < numdims; ++i)
47 result[numdims - i - 1] = n % (dims[numdims - i - 1]);
48 n /= (dims[numdims - i - 1]);
64 part_prod[numdims - 1] = 1;
65 for (
idx i = 1; i < numdims; ++i)
67 part_prod[numdims - i - 1] =
68 part_prod[numdims - i] * dims[numdims - i];
69 result += midx[numdims - i - 1] * part_prod[numdims - i - 1];
72 return result + midx[numdims - 1];
76 template<
typename Derived>
81 return rA.rows() == rA.cols();
85 template<
typename Derived>
90 return rA.rows() == 1 || rA.cols() == 1;
94 template<
typename Derived>
99 return rA.rows() == 1;
103 template<
typename Derived>
108 return rA.cols() == 1;
115 return x.size() != 0;
119 template<
typename T1,
typename T2>
122 return lhs.size() == rhs.size();
128 if (dims.size() == 0)
131 return std::find_if(std::begin(dims), std::end(dims),
132 [dims](
idx i) ->
bool
134 if (i == 0)
return true;
136 }) == std::end(dims);
141 template<
typename Derived>
143 const Eigen::MatrixBase<Derived>& A)
147 idx proddim = std::accumulate(std::begin(dims), std::end(dims),
148 static_cast<idx>(1), std::multiplies<idx>());
150 return proddim ==
static_cast<idx>(rA.rows());
154 template<
typename Derived>
156 const Eigen::MatrixBase<Derived>& V)
160 idx proddim = std::accumulate(std::begin(dims), std::end(dims),
161 static_cast<idx>(1), std::multiplies<idx>());
163 return proddim ==
static_cast<idx>(rV.rows());
167 template<
typename Derived>
169 const Eigen::MatrixBase<Derived>& V)
173 idx proddim = std::accumulate(std::begin(dims), std::end(dims),
174 static_cast<idx>(1), std::multiplies<idx>());;
176 return proddim ==
static_cast<idx>(rV.cols());
191 const std::vector <idx>& dims)
198 if (subsys.size() > dims.size())
202 std::vector <idx> subsyssort = subsys;
203 std::sort(std::begin(subsyssort), std::end(subsyssort));
206 if (std::unique(std::begin(subsyssort), std::end(subsyssort))
207 != std::end(subsyssort))
211 return std::find_if(std::begin(subsyssort), std::end(subsyssort),
212 [dims](
idx i) ->
bool
214 return i > dims.size() - 1;
215 }) == std::end(subsyssort);
219 template<
typename Derived>
224 return rA.rows() == 2 && rA.cols() == 2;
228 template<
typename Derived>
233 return rV.rows() == 2 && rV.cols() == 1;
237 template<
typename Derived>
242 return rV.rows() == 1 && rV.cols() == 2;
246 template<
typename Derived>
251 return (rV.rows() == 1 && rV.cols() == 2) ||
252 (rV.rows() == 2 && rV.cols() == 1);
259 if (perm.size() == 0)
262 std::vector <idx> ordered(perm.size());
263 std::iota(std::begin(ordered), std::end(ordered), 0);
265 return std::is_permutation(std::begin(ordered), std::end(ordered),
271 template<
typename Derived1,
typename Derived2>
273 const Eigen::MatrixBase<Derived2>& B)
281 if (!std::is_same<
typename Derived1::Scalar,
282 typename Derived2::Scalar>::value)
293 idx Acols =
static_cast<idx>(rA.cols());
294 idx Arows =
static_cast<idx>(rA.rows());
295 idx Bcols =
static_cast<idx>(rB.cols());
296 idx Brows =
static_cast<idx>(rB.rows());
299 result.resize(Arows * Brows, Acols * Bcols);
301 #pragma omp parallel for collapse(2)
302 for (
idx j = 0; j < Acols; ++j)
303 for (
idx i = 0; i < Arows; ++i)
304 result.block(i * Brows, j * Bcols, Brows, Bcols) = rA(i, j) * rB;
311 template<
typename Derived1,
typename Derived2>
313 const Eigen::MatrixBase<Derived1>& A,
314 const Eigen::MatrixBase<Derived2>& B)
322 if (!std::is_same<
typename Derived1::Scalar,
323 typename Derived2::Scalar>::value)
334 idx Acols =
static_cast<idx>(rA.cols());
335 idx Arows =
static_cast<idx>(rA.rows());
336 idx Bcols =
static_cast<idx>(rB.cols());
337 idx Brows =
static_cast<idx>(rB.rows());
343 result.block(0, 0, Arows, Acols) = rA;
344 result.block(Arows, Acols, Brows, Bcols) = rB;
357 template<
typename T,
typename First,
typename ... Args>
360 v.emplace_back(std::forward<First>(first));
bool _check_cvector(const Eigen::MatrixBase< Derived > &A)
Definition: util.h:104
bool _check_qubit_cvector(const Eigen::MatrixBase< Derived > &V) noexcept
Definition: util.h:229
constexpr idx maxn
Maximum number of allowed qu(d)its (subsystems)
Definition: constants.h:80
bool _check_matching_sizes(const T1 &lhs, const T2 &rhs) noexcept
Definition: util.h:120
bool _check_subsys_match_dims(const std::vector< idx > &subsys, const std::vector< idx > &dims)
Definition: util.h:190
bool _check_dims_match_mat(const std::vector< idx > &dims, const Eigen::MatrixBase< Derived > &A)
Definition: util.h:142
bool _check_dims_match_cvect(const std::vector< idx > &dims, const Eigen::MatrixBase< Derived > &V)
Definition: util.h:155
dyn_mat< typename Derived1::Scalar > _kron2(const Eigen::MatrixBase< Derived1 > &A, const Eigen::MatrixBase< Derived2 > &B)
Definition: util.h:272
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > dyn_mat
Dynamic Eigen matrix over the field specified by Scalar.
Definition: types.h:83
bool _check_vector(const Eigen::MatrixBase< Derived > &A)
Definition: util.h:86
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:168
bool _check_square_mat(const Eigen::MatrixBase< Derived > &A)
Definition: util.h:77
bool _check_rvector(const Eigen::MatrixBase< Derived > &A)
Definition: util.h:95
bool _check_qubit_matrix(const Eigen::MatrixBase< Derived > &A) noexcept
Definition: util.h:220
bool _check_perm(const std::vector< idx > &perm)
Definition: util.h:257
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
void variadic_vector_emplace(std::vector< T > &)
Definition: util.h:352
bool _check_nonzero_size(const T &x) noexcept
Definition: util.h:113
dyn_mat< typename Derived1::Scalar > _dirsum2(const Eigen::MatrixBase< Derived1 > &A, const Eigen::MatrixBase< Derived2 > &B)
Definition: util.h:312
bool _check_qubit_vector(const Eigen::MatrixBase< Derived > &V) noexcept
Definition: util.h:247
void _n2multiidx(idx n, idx numdims, const idx *dims, idx *result) noexcept
Definition: util.h:41
std::size_t idx
Non-negative integer index.
Definition: types.h:36
bool _check_dims(const std::vector< idx > &dims)
Definition: util.h:126
bool _check_eq_dims(const std::vector< idx > &dims, idx dim) noexcept
Definition: util.h:180
bool _check_qubit_rvector(const Eigen::MatrixBase< Derived > &V) noexcept
Definition: util.h:238