Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-implement entropy_relative. #1553

Merged

Conversation

hodgestar
Copy link
Contributor

@hodgestar hodgestar commented May 12, 2021

Description
Re-implement entropy_relative.

This function was implemented in March 2012, but then removed a month later. After some digging, it appears that the reason the function was removed is that it assumed the eigenvectors of the density matrices were identical (and in the same order).

After some reading of Nielsen & Chuang, I have re-implemented it to address the issue.

Still todo

  • Update the documentation.
  • Add an example to an example notebook.
  • Add some tests for specific cases where we know what the answer should be.
  • Stretch goal: Vectorize the calculation of P_ij and the rest of the loop as much as we can (but let's make sure the answers are correct first).

Changelog
Re-implement entropy_relative which returns the quantum relative entropy between two density matrices.

@coveralls
Copy link

coveralls commented May 12, 2021

Coverage Status

Coverage increased (+0.6%) to 64.928% when pulling c254091 on hodgestar:feature/re-implement-relative-entropy into 6845908 on qutip:master.

qutip/entropy.py Outdated Show resolved Hide resolved
qutip/entropy.py Outdated Show resolved Hide resolved
Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for both finding this and fixing it! I have a suspicion there was an old issue or mailing list message that was asking for some quantity derived from relative entropy as well, so this is definitely good to get working.

qutip/entropy.py Outdated Show resolved Hide resolved
qutip/entropy.py Outdated Show resolved Hide resolved
qutip/entropy.py Outdated Show resolved Hide resolved
qutip/entropy.py Outdated Show resolved Hide resolved
qutip/entropy.py Outdated Show resolved Hide resolved
qutip/entropy.py Outdated Show resolved Hide resolved
qutip/entropy.py Show resolved Hide resolved
qutip/tests/test_entropy.py Outdated Show resolved Hide resolved
qutip/tests/test_entropy.py Outdated Show resolved Hide resolved
qutip/tests/test_entropy.py Show resolved Hide resolved
@jakelishman
Copy link
Member

Oh, I just noticed the comment at the top about vectorisation of the calculation of P. I could be wrong, but I think P = np.abs(np.inner(rvecs, np.conj(svecs)))**2 gets you there.

@hodgestar
Copy link
Contributor Author

The intermittently failing test seems unrelated and is test_usage_in_solvers[mesolve-complex-spline,string].

@hodgestar
Copy link
Contributor Author

@jakelishman @Ericgig @Albantakis Ready for a second round of review, I think!

@hodgestar
Copy link
Contributor Author

I'm didn't find an obvious notebook in qutip-notebooks to add an example to. Not sure how we usually proceed in such cases -- skip having an example in a notebook? Make a new notebook?

Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good. For notebooks, we've historically not been good at updating them, I think. For a single, clearly defined function, probably just adding an Examples section in the docstring might be more appropriate? I'm not entirely sure.

qutip/entropy.py Outdated Show resolved Hide resolved
qutip/entropy.py Outdated Show resolved Hide resolved
Comment on lines +291 to +294
# Avoid -inf from log(0) -- these terms will be multiplied by zero later
# anyway
svals[abs(svals) < tol] = 1
nzrvals = rvals[abs(rvals) >= tol]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't use it without swapping how we handle the logarithms, but if it's useful in the future, scipy has scipy.special.xlogy that specifically handles 0 ln(0) := 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay with not involving scipy.special.xlogy for now -- it's another thing for readers of the code to think about and I'm not sure where the bottlenecks in the current code are or how much anyone cares about performance (my guess is that finding the eigenvalues is the slowest step now).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, no need to put it in now. Now that everything's vectorised, I'd be surprised if the dominant time factor wasn't the eigenstate calculation.

qutip/entropy.py Outdated Show resolved Hide resolved
qutip/tests/test_entropy.py Outdated Show resolved Hide resolved
@hodgestar
Copy link
Contributor Author

@jakelishman I added an example to the docstring.

@jakelishman
Copy link
Member

Nice, and it's all rendered correctly in the HTML documentation

@hodgestar hodgestar merged commit ef88d0b into qutip:master May 13, 2021
@jakelishman jakelishman added this to the QuTiP 4.6.2 milestone May 14, 2021
quantshah pushed a commit to quantshah/qutip that referenced this pull request Jun 2, 2021
…ative-entropy

Re-implement entropy_relative.
@hodgestar hodgestar deleted the feature/re-implement-relative-entropy branch July 6, 2021 21:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants