qpp
qpp copied to clipboard
qpp::measure causes Eigen assert
Hi,
qpp::measure causes the following Eigen assert:
/usr/include/eigen3/Eigen/src/Core/PlainObjectBase.h:366:
void Eigen::PlainObjectBase<Derived>::resizeLike(const Eigen::EigenBase<OtherDerived>&)
[with OtherDerived = Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<std::complex<double> >,
Eigen::Matrix<std::complex<double>, -1, -1> >;
Derived = Eigen::Matrix<std::complex<double>, 4, 1>]: Assertion `other.rows() == 1 || other.cols() == 1' failed.
I've reproduced it with the following test: https://github.com/antoine-bussy/qpp/commit/b873e7ee6d5b939d540daacf22f8b3789487c563 and fixed it this way: https://github.com/antoine-bussy/qpp/commit/36b0343d124d69e9b64347126ff625aff50b5440
The bug is that outstates is being initialized with square matrices, even though it stores vectors in this use case.
Do you want me to submit a PR?
Thanks! Good catch. I'll incorporate the fix into the current dev branch (where I'm fixing the other issue), so no need for a PR for this one.
@antoine-bussy Fixed on dev branch. In fact, there's no need for that resize second argument, so simply defining std::vector<expr_t<Derived>> outstates(Ks.size()); suffices.
Thanks!
I uncovered another "bug" stemming from my PR https://github.com/softwareQinc/qpp/pull/110.
Let's take the case of qpp::measure
- Before the PR,
qpp::measureaccepted any Eigen matrix as input, and returnedoutstatesascmat - After the PR,
qpp::measureaccepted any Eigen matrix as input, and returnedoutstateswith the same type as the input
Point 2 is fine as long as users stick to ket and cmat. It doesn't work anymore when the input is a fixed-size vector and destructive = true, since outstates don't have the same dimension as the input anymore. So it's more a possibility of misuse rather than a bug.
I can think of 3 possible "fixes":
- Leave the code as is, users should be using
ketandcmat - Protect the code with checks (e.g.
static_assert) - Define
outstatesto beketorcmatwhen the input is a fixed-size vector or matrix, like this https://github.com/antoine-bussy/qpp/blob/62b937ebe2b7027a5d1fb3ad572fdaad86734e55/include/types.hpp#L107-L144
@vsoftco What do you think?
@antoine-bussy Yes, I bumped into that (when testing your code). I think the latest solution is the most straightforward, and least intrusive.