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

Add new store_density_matricies Result option #2303

Merged

Conversation

Matt-Ord
Copy link
Contributor

@Matt-Ord Matt-Ord commented Jan 23, 2024

Checklist
Thank you for contributing to QuTiP! Please make sure you have finished the following tasks before opening the PR.

  • Please read Contributing to QuTiP Development
  • Contributions to qutip should follow the pep8 style.
    You can use pycodestyle to check your code automatically
  • Please add tests to cover your changes if applicable.
  • If the behavior of the code has changed or new feature has been added, please also update the documentation in the doc folder, and the notebook. Feel free to ask if you are not sure.
  • Include the changelog in a file named: doc/changes/<PR number>.<type> 'type' can be one of the following: feature, bugfix, doc, removal, misc, or deprecation (see here for more information).

Delete this checklist after you have completed all the tasks. If you have not finished them all, you can also open a Draft Pull Request to let the others know this on-going work and keep this checklist in the PR description.

Description
Add a new store_density_matricies option to Result

Related issues or PRs
Fixes #2299

@Matt-Ord Matt-Ord force-pushed the Split-density-matrix-and-state-results branch 2 times, most recently from 1467a7b to 16799a2 Compare January 23, 2024 13:16
@Matt-Ord
Copy link
Contributor Author

@Ericgig does this look like it is along the right lines - It fixes my issue but I want it signed off before I go adding docs. Also unrelated what is your opinion on type annotations. Would it be accepted if I was to make another pr adding annotations to some functions I use?

Copy link
Member

@Ericgig Ericgig left a comment

Choose a reason for hiding this comment

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

Thank you for looking at this.

store_state should still be the final factor whether states are stored or not.
It's the one available everywhere and for v4. mesolve with store_state will have result.states being the output density matrix. mcsolve with the same store_state and default options should return a similar result.

So it would be better to have the new option be precompute_average_states and have it only control if the average is compute when all trajectories is stored. The if the average_states property it use, it could compute the average then, but never do so if not used.

Please keep result.states and result.final_states as is. They have the same behaviour as in v4 and we want to keep them for backward compatibility.

runs_... and average_... are new to v5 and could be renamed, but it's not clear that density_matricies is the average while final_states are for each trajectories. smesolve and sometime for mcsolve individual trajectories states are already density matrices.
Thus smesolve's result density_matricies could work as well for all states from all trajectories, causing confusion.

You implemented the density_matricies property twice.

Having the options to keep only the final states without keeping all the trajectories is a good idea. Be careful that the trajectory result have the final state stored

qutip/solver/result.py Show resolved Hide resolved
qutip/solver/mcsolve.py Outdated Show resolved Hide resolved
qutip/solver/result.py Outdated Show resolved Hide resolved
@Matt-Ord
Copy link
Contributor Author

Thank you for looking at this.

store_state should still be the final factor whether states are stored or not. It's the one available everywhere and for v4. mesolve with store_state will have result.states being the output density matrix. mcsolve with the same store_state and default options should return a similar result.

So it would be better to have the new option be precompute_average_states and have it only control if the average is compute when all trajectories is stored. The if the average_states property it use, it could compute the average then, but never do so if not used.

Please keep result.states and result.final_states as is. They have the same behaviour as in v4 and we want to keep them for backward compatibility.

runs_... and average_... are new to v5 and could be renamed, but it's not clear that density_matricies is the average while final_states are for each trajectories. smesolve and sometime for mcsolve individual trajectories states are already density matrices. Thus smesolve's result density_matricies could work as well for all states from all trajectories, causing confusion.

You implemented the density_matricies property twice.

Having the options to keep only the final states without keeping all the trajectories is a good idea. Be careful that the trajectory result have the final state stored

So I'm not entirely sure what you want the behaviour to be for each set of options. I think it is confusing to return a density matrix if you request store_states - the density matrix is not the same as the state of the system, and surely v5 is the perfect time to clear up issues like this. Personally I found this behaviour very surprising when updating to V5. I do agree that density_matricies should maybe be average_density_matricies. I am happy to change mesolve as part of this for consistency. If you want to keep it backwards compatible would it be possible to send a list of all combination of options and exactly what you want states to return in each case, just to prevent any back and forth.

@Ericgig
Copy link
Member

Ericgig commented Jan 25, 2024

Here is how I see it:

store_states keep_runs_results precompute_average_states -> states average_states runs_states
False Any Any -> None None None
True False Any -> Dms Dms None
True True False -> [runs.states] Computed in property [runs.states]
True True True -> [runs.states] Computed at add [runs.states]

mcsolve can call mesolve which can call sesolve. The options must match.

I asked around and states being the average did not confuse anyone, a kind of professional deformation.
It would break a lot of code and annoy a lot of users to change it from the only option to not existing.

density_matricies could be added as a new property.
Ideally it should be everywhere, for sesolve it would return the projection of the ket, etc.

@Matt-Ord Matt-Ord force-pushed the Split-density-matrix-and-state-results branch 2 times, most recently from 27ea2e0 to 401f770 Compare January 26, 2024 09:18
@Matt-Ord
Copy link
Contributor Author

Thanks for the detailed response. One issue - what if a user wants to store only the final state, not the final density matrix for example? For now I've made what I think should be the minimum fix - I've made it so that the density matrix is only pre-computed if keep_all_runs is not specified

@Matt-Ord Matt-Ord marked this pull request as ready for review January 26, 2024 09:20
@Matt-Ord Matt-Ord force-pushed the Split-density-matrix-and-state-results branch 2 times, most recently from f8e0c38 to 290cbb7 Compare January 26, 2024 09:29
@Ericgig
Copy link
Member

Ericgig commented Jan 26, 2024

The approach of only computing average per default after the evolution is good.
But with >300 lines of change, I don't see it as the minimum fix...

For keeping trajectories' last state without saving the full trajectories, I think we need a new option.

@Matt-Ord
Copy link
Contributor Author

@Ericgig any updates on this? Also can you enable CI?

@Matt-Ord Matt-Ord force-pushed the Split-density-matrix-and-state-results branch 2 times, most recently from d48e651 to 259ede8 Compare February 2, 2024 14:57
@Matt-Ord
Copy link
Contributor Author

Matt-Ord commented Feb 2, 2024

@Ericgig could you enable CI again

@Matt-Ord Matt-Ord force-pushed the Split-density-matrix-and-state-results branch 3 times, most recently from 432f39f to 486ef92 Compare February 2, 2024 16:29
@coveralls
Copy link

coveralls commented Feb 2, 2024

Coverage Status

coverage: 85.42% (-0.06%) from 85.479%
when pulling d550e7c on Matt-Ord:Split-density-matrix-and-state-results
into c3a1916 on qutip:master.

@Matt-Ord
Copy link
Contributor Author

Matt-Ord commented Feb 4, 2024

@Ericgig Should now be passing all tests, and essentially be ready

Copy link
Member

@Ericgig Ericgig left a comment

Choose a reason for hiding this comment

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

The code look nice.

Could you revert the names to what they were:
_reduce_density_matrix -> _reduce_states,
_sum_final_density_matrix -> _sum_final_states
etc.
We will be the one having to work with these in years to come, please to not impose your preferences on us.

Ideally I would prefer changes from black, flake8 etc. in their own PR as to not confuse with actual code changes. Also I do not blindly accept black formatting, it must read well for humans as well.

qutip/solver/result.py Outdated Show resolved Hide resolved
qutip/solver/result.py Outdated Show resolved Hide resolved
qutip/solver/result.py Outdated Show resolved Hide resolved
qutip/solver/result.py Outdated Show resolved Hide resolved
@Matt-Ord Matt-Ord force-pushed the Split-density-matrix-and-state-results branch from 87c99be to d550e7c Compare February 6, 2024 09:48
@Matt-Ord Matt-Ord requested a review from Ericgig February 6, 2024 09:48
@Matt-Ord
Copy link
Contributor Author

Matt-Ord commented Feb 6, 2024

Thanks for your patience reviewing. I should have fixed all of your comments. Agree with the black formatting - my usual workflow is to format with black and add brackets until both black and I am happy with the result. Seems a few slipped through this time unfortunately.

Copy link
Member

@Ericgig Ericgig left a comment

Choose a reason for hiding this comment

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

Great work. Thank you.

@Ericgig Ericgig merged commit 48ff43b into qutip:master Feb 6, 2024
12 checks passed
@Matt-Ord Matt-Ord deleted the Split-density-matrix-and-state-results branch February 6, 2024 13:14
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.

MemoryError when updating to latest qutip
3 participants