[go: up one dir, main page]

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

Update to the HarmonicOscillatorWaveFunction with a wave function recurrence that uses Cython. #2553

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

fobos123deimos
Copy link
@fobos123deimos fobos123deimos commented Nov 4, 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

I am adding an efficient module for calculating the wave function that uses a recurrence for it in Cython, and I am applying one of its functions in the update method of the HarmonicOscillatorWaveFunction class in the distribution.py module. This module is an adaptation of one of the modules from the package I developed for efficient wave function calculation: Fast Wave.

Related issues or PRs
Please mention the related issues or PRs here. If the PR fixes an issue, use the keyword fix/fixes/fixed followed by the issue id, e.g. fix #1184

@fobos123deimos
Copy link
Author

I will resolve the issues!

Matheus.

@fobos123deimos
Copy link
Author

@hodgestar

@fobos123deimos fobos123deimos marked this pull request as draft November 7, 2024 10:38
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 the contribution.

Could you add a towncrier entry?
Some tests would also be nice. I can't easily tell if you are computing the same thing looking at the code (not that I can confirm there is no bug in the original version.)
Also it would be better to have the cython code in qutip/_distribution.pyx. A new folder for one file feels overkill.


self.data += k * psi.data_as()[n, 0]
self.data += (
wc.psi_n_single_fock_multiple_position_complex(
Copy link
Member

Choose a reason for hiding this comment

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

We have a scaling factor omega. It should be kept (or removed from every Distribution.)

Copy link
Author

Choose a reason for hiding this comment

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

I solved it.

Copy link
Author

Choose a reason for hiding this comment

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

I created the _distribution.pyx method, I will provide tests and towncrier entries.

Comment on lines 361 to 373
"""
This class uses a function from the wavefunction_cython
module, which is a compiled version for the Numpy and
Cython version of QuTip, from the wavefunction_cython
module of the Fast_Wave <https://github.com/fobos123deimos/fast-wave>
package, which is released under
the BSD license, with the following copyright notice:
CORDEIRO, MATHEUS; BEZERRA, I. P. ; VASCONCELOS, H. H. M. . Efficient
Computation of the Wave Function ψn(x) using Hermite Coefficient Matrix in
Python. In: 7º Workshop Escola de Computação e Informação Quântica
(7ª WECIQ),2024, Rio de Janeiro.ANAIS DO 7º WORKSHOP-ESCOLA DE COMPUTAÇÃO
E INFORMAÇÃO QUÂNTICA. Rio de Janeiro: CEFET/RJ, 2024. p. 56-60.
"""
Copy link
Member

Choose a reason for hiding this comment

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

This docstring is not added to the Distribution one, but replace it.
So it should have a description of what it is, it's parameters etc.

It's fine to add reference to your paper, but that not as the only description of the class.

Copy link
Author

Choose a reason for hiding this comment

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

I solved it

Comment on lines 1 to 30
# BSD 3-Clause License
#
# Copyright (c) 2024, Matheus Gomes Cordeiro
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Copy link
Member

Choose a reason for hiding this comment

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

Would you be comfortable to be simply included as "QuTiP developers and contributors" of the main BSD 3-Clause License of Qutip?

Having you as the author of the file give the feeling that no one else should touch it and we don't want that.
If merged, we will eventually need to modify this file, maybe merge it or split it etc.

Copy link
Author

Choose a reason for hiding this comment

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

I solved it

@cython.cfunc
@cython.locals(x_size=np.npy_intp, j=int, i=int, k=int, temp1=complex, temp2=complex)
@cython.boundscheck(False)
cpdef np.ndarray[np.complex128_t, ndim=1] psi_n_single_fock_multiple_position_complex(int n, np.ndarray[np.complex128_t, ndim=1] x):
Copy link
Member

Choose a reason for hiding this comment

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

This is the only function used...
Can you remove the others?

Copy link
Author

Choose a reason for hiding this comment

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

I solved it

cimport numpy as np
import numpy as np
from libc.math cimport exp, sqrt, pi
from cmath import exp as cexp, sqrt as csqrt, pi as cpi
Copy link
Member

Choose a reason for hiding this comment

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

To import the c++ versions:

Suggested change
from cmath import exp as cexp, sqrt as csqrt, pi as cpi
cdef extern from "<complex>" namespace "std" nogil:
double complex cexp "exp"(double complex x)
double complex csqrt "sqrt"(double complex x)

Copy link
Author

Choose a reason for hiding this comment

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

I had trouble solving this, so I left it as is.

@cython.nogil
@cython.cfunc
@cython.locals(x_size=np.npy_intp, j=int, i=int, k=int, temp1=complex, temp2=complex)
@cython.boundscheck(False)
Copy link
Member

Choose a reason for hiding this comment

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

You could add @cython.cdivision(True) to run in pure cython.

Copy link
Author

Choose a reason for hiding this comment

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

I solved it

Comment on lines 242 to 243
for j in range(x_size):
result[0, j] = (cpi ** (-0.25)) * cexp(-(x[j] ** 2) / 2)
Copy link
Member

Choose a reason for hiding this comment

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

If you want to optimize as much as possible, you could pre-compute the power temp1 = cpi ** -0.25 / 2.
But I wouldn't be surprised the compiler do it with the optimization we use (-O3).

Copy link
Author

Choose a reason for hiding this comment

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

I solved it

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 the fixes and for writing a docstrings

The wave function generated does not match what I expected.
Here is what I got for the probability of the 5 first states:
download

Could add tests to check it's right.

@@ -20,6 +20,7 @@

from . import isket, ket2dm, state_number_index
from .wigner import wigner, qfunc
from _distributions import psi_n_single_fock_multiple_position_complex
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
from _distributions import psi_n_single_fock_multiple_position_complex
from ._distributions import psi_n_single_fock_multiple_position_complex

Copy link
Author

Choose a reason for hiding this comment

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

Thank you! Could you provide your test code so I can use it as a reference to find the problem with this issue?

Copy link
Author

Choose a reason for hiding this comment

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

I managed to identify the error.

Copy link
Author

Choose a reason for hiding this comment

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

The problem was the decorator @cython.cdivision(True). I removed it.

Copy link
Member

Choose a reason for hiding this comment

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

With cdivision(True), we need to be more careful about types.
Still having test would be good.

Copy link
Author

Choose a reason for hiding this comment

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

I will do the tests, but is it ok to remove cdivision(True)?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, it's ok.

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.

More detailed information for GSoC 2020 project
2 participants