32 #ifndef INTERNAL_UTIL_H_ 33 #define INTERNAL_UTIL_H_ 44 idx* result) noexcept {
50 for (
idx i = 0; i < numdims; ++i)
56 for (
idx i = 0; i < numdims; ++i) {
57 result[numdims - i - 1] = n % (dims[numdims - i - 1]);
58 n /= (dims[numdims - i - 1]);
64 #if (__GNUC__ && !__clang__) 65 #pragma GCC diagnostic push 66 #pragma GCC diagnostic ignored "-Warray-bounds" 67 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" 72 const idx*
const dims) noexcept {
85 part_prod[numdims - 1] = 1;
86 for (
idx i = 1; i < numdims; ++i) {
87 part_prod[numdims - i - 1] = part_prod[numdims - i] * dims[numdims - i];
88 result += midx[numdims - i - 1] * part_prod[numdims - i - 1];
91 return result + midx[numdims - 1];
93 #if (__GNUC__ && !__clang__) 94 #pragma GCC diagnostic pop 98 template <
typename Derived>
100 return A.rows() == A.cols();
104 template <
typename Derived>
106 return A.rows() == 1 || A.cols() == 1;
110 template <
typename Derived>
112 return A.rows() == 1;
116 template <
typename Derived>
118 return A.cols() == 1;
122 template <
typename T>
124 return x.size() != 0;
128 template <
typename T1,
typename T2>
130 return lhs.size() == rhs.size();
135 if (dims.size() == 0)
138 return std::find_if(std::begin(dims), std::end(dims),
139 [dims](
idx i) ->
bool {
144 }) == std::end(dims);
149 template <
typename Derived>
151 const Eigen::MatrixBase<Derived>& A) {
154 assert(dims.size() > 0);
155 assert(A.rows() == A.cols());
157 idx proddim = std::accumulate(std::begin(dims), std::end(dims),
158 static_cast<idx>(1), std::multiplies<idx>());
160 return proddim ==
static_cast<idx>(A.rows());
164 template <
typename Derived>
166 const Eigen::MatrixBase<Derived>& A) {
169 assert(dims.size() > 0);
170 assert(A.rows() > 0);
171 assert(A.cols() == 1);
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>(A.rows());
180 template <
typename Derived>
182 const Eigen::MatrixBase<Derived>& A) {
185 assert(dims.size() > 0);
186 assert(A.cols() > 0);
187 assert(A.rows() == 1);
189 idx proddim = std::accumulate(std::begin(dims), std::end(dims),
190 static_cast<idx>(1), std::multiplies<idx>());
193 return proddim ==
static_cast<idx>(A.cols());
200 assert(dims.size() > 0);
211 std::sort(std::begin(v), std::end(v));
212 if (std::unique(std::begin(v), std::end(v)) != std::end(v))
220 const std::vector<idx>& dims) {
224 if (subsys.size() > dims.size())
232 return std::find_if(std::begin(subsys), std::end(subsys),
233 [dims](
idx i) ->
bool {
234 return i + 1 > dims.size();
235 }) == std::end(subsys);
239 template <
typename Derived>
241 return A.rows() == 2 && A.cols() == 2;
245 template <
typename Derived>
247 return A.rows() == 2 && A.cols() == 1;
251 template <
typename Derived>
253 return A.rows() == 1 && A.cols() == 2;
257 template <
typename Derived>
259 return (A.rows() == 1 && A.cols() == 2) || (A.rows() == 2 && A.cols() == 1);
264 if (perm.size() == 0)
267 std::vector<idx> ordered(perm.size());
268 std::iota(std::begin(ordered), std::end(ordered), 0);
270 return std::is_permutation(std::begin(ordered), std::end(ordered),
276 template <
typename Derived1,
typename Derived2>
278 const Eigen::MatrixBase<Derived2>& B) {
285 if (!std::is_same<
typename Derived1::Scalar,
286 typename Derived2::Scalar>::value)
298 idx Acols =
static_cast<idx>(rA.cols());
299 idx Arows =
static_cast<idx>(rA.rows());
300 idx Bcols =
static_cast<idx>(rB.cols());
301 idx Brows =
static_cast<idx>(rB.rows());
304 result.resize(Arows * Brows, Acols * Bcols);
307 #pragma omp parallel for collapse(2) 308 #endif // WITH_OPENMP_ 310 for (
idx j = 0; j < Acols; ++j)
311 for (
idx i = 0; i < Arows; ++i)
312 result.block(i * Brows, j * Bcols, Brows, Bcols) = rA(i, j) * rB;
319 template <
typename Derived1,
typename Derived2>
322 const Eigen::MatrixBase<Derived2>& B) {
329 if (!std::is_same<
typename Derived1::Scalar,
330 typename Derived2::Scalar>::value)
342 idx Acols =
static_cast<idx>(rA.cols());
343 idx Arows =
static_cast<idx>(rA.rows());
344 idx Bcols =
static_cast<idx>(rB.cols());
345 idx Brows =
static_cast<idx>(rB.rows());
350 result.block(0, 0, Arows, Acols) = rA;
351 result.block(Arows, Acols, Brows, Bcols) = rB;
357 template <
typename T>
362 template <
typename T,
typename First,
typename... Args>
364 v.emplace_back(std::forward<First>(first));
376 return static_cast<idx>(std::llround(std::log2(D) / std::log2(d)));
389 return static_cast<idx>(std::llround(std::sqrt(sz)));
391 return static_cast<idx>(std::llround(std::pow(sz, 1. / N)));
396 template <
typename T>
400 std::ostringstream ostr;
403 std::vector<std::string> vstr;
406 for (
idx i = 0; i < static_cast<idx>(A.rows()); ++i) {
407 for (
idx j = 0; j < static_cast<idx>(A.cols()); ++j) {
410 ostr.str(std::string{});
413 double re =
static_cast<cplx>(A(i, j)).real();
414 double im =
static_cast<cplx>(A(i, j)).imag();
416 if (std::abs(re) <
chop && std::abs(im) <
chop) {
420 vstr.push_back(ostr.str());
421 }
else if (std::abs(re) <
chop) {
423 vstr.push_back(ostr.str() +
"i");
424 }
else if (std::abs(im) <
chop) {
426 vstr.push_back(ostr.str() +
" ");
431 strA += (im > 0 ?
" + " :
" - ");
433 ostr.str(std::string());
434 ostr << std::abs(im);
437 vstr.push_back(strA);
443 std::vector<idx> maxlengthcols(A.cols(), 0);
445 for (
idx i = 0; i < static_cast<idx>(A.rows()); ++i)
446 for (
idx j = 0; j < static_cast<idx>(A.cols()); ++j)
447 if (vstr[i * A.cols() + j].size() > maxlengthcols[j])
448 maxlengthcols[j] = vstr[i * A.cols() + j].size();
451 for (
idx i = 0; i < static_cast<idx>(A.rows()); ++i) {
452 os << std::setw(static_cast<int>(maxlengthcols[0])) << std::right
453 << vstr[i * A.cols()];
455 for (
idx j = 1; j < static_cast<idx>(A.cols()); ++j)
456 os << std::setw(static_cast<int>(maxlengthcols[j] + 2))
457 << std::right << vstr[i * A.cols() + j];
459 if (i < static_cast<idx>(A.rows()) - 1)
bool check_nonzero_size(const T &x) noexcept
Definition: util.h:123
constexpr double chop
Used in qpp::disp() for setting to zero numbers that have their absolute value smaller than qpp::chop...
Definition: constants.h:60
constexpr idx maxn
Maximum number of allowed qubits/qudits (subsystems)
Definition: constants.h:67
bool check_subsys_match_dims(const std::vector< idx > &subsys, const std::vector< idx > &dims)
Definition: util.h:219
dyn_mat< typename Derived1::Scalar > kron2(const Eigen::MatrixBase< Derived1 > &A, const Eigen::MatrixBase< Derived2 > &B)
Definition: util.h:277
bool check_cvector(const Eigen::MatrixBase< Derived > &A)
Definition: util.h:117
std::ostream & display_impl_(const T &A, std::ostream &os, double chop=qpp::chop) const
Definition: util.h:398
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > dyn_mat
Dynamic Eigen matrix over the field specified by Scalar.
Definition: types.h:81
bool check_dims_match_mat(const std::vector< idx > &dims, const Eigen::MatrixBase< Derived > &A)
Definition: util.h:150
idx multiidx2n(const idx *const midx, idx numdims, const idx *const dims) noexcept
Definition: util.h:71
void n2multiidx(idx n, idx numdims, const idx *const dims, idx *result) noexcept
Definition: util.h:43
bool check_no_duplicates(std::vector< idx > v)
Definition: util.h:210
Quantum++ main namespace.
Definition: circuits.h:35
bool check_perm(const std::vector< idx > &perm)
Definition: util.h:263
bool check_rvector(const Eigen::MatrixBase< Derived > &A)
Definition: util.h:111
bool check_eq_dims(const std::vector< idx > &dims, idx dim) noexcept
Definition: util.h:197
bool check_vector(const Eigen::MatrixBase< Derived > &A)
Definition: util.h:105
idx get_num_subsys(idx D, idx d)
Definition: util.h:370
bool check_dims_match_cvect(const std::vector< idx > &dims, const Eigen::MatrixBase< Derived > &A)
Definition: util.h:165
bool check_dims_match_rvect(const std::vector< idx > &dims, const Eigen::MatrixBase< Derived > &A)
Definition: util.h:181
bool check_matching_sizes(const T1 &lhs, const T2 &rhs) noexcept
Definition: util.h:129
std::complex< double > cplx
Complex number in double precision.
Definition: types.h:49
void variadic_vector_emplace(std::vector< T > &)
Definition: util.h:359
Type mismatch exception.
Definition: exception.h:530
idx get_dim_subsys(idx sz, idx N)
Definition: util.h:382
bool check_square_mat(const Eigen::MatrixBase< Derived > &A)
Definition: util.h:99
dyn_mat< typename Derived1::Scalar > dirsum2(const Eigen::MatrixBase< Derived1 > &A, const Eigen::MatrixBase< Derived2 > &B)
Definition: util.h:321
bool check_qubit_rvector(const Eigen::MatrixBase< Derived > &A) noexcept
Definition: util.h:252
bool check_dims(const std::vector< idx > &dims)
Definition: util.h:134
bool check_qubit_vector(const Eigen::MatrixBase< Derived > &A) noexcept
Definition: util.h:258
std::size_t idx
Non-negative integer index, make sure you use an unsigned type.
Definition: types.h:39
bool check_qubit_cvector(const Eigen::MatrixBase< Derived > &A) noexcept
Definition: util.h:246
Object has zero size exception.
Definition: exception.h:134
bool check_qubit_matrix(const Eigen::MatrixBase< Derived > &A) noexcept
Definition: util.h:240