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

creating fermionic creation and annihilation operators #2166

Merged
merged 16 commits into from Jun 2, 2023

Conversation

khnikhil
Copy link
Contributor

Description
Adding spin-1/2 fermionic creation and annihilation operators to close #2165 (for UnitaryHack 2023).

Specifically, in qutip.core.operators, I added the fcreate and fdestroy functions, following the Jordan-Wigner transformation described in this guide.

Specifically, I implemented the following using qutip.tensor objects, and documented this in the code.

$$a_j = \sigma_z^{\otimes j} \otimes (\frac{\sigma_x + i \sigma_y}{2}) \otimes I^{\otimes N-j-1}$$

$$a_j^{\dagger} = \sigma_z^{\otimes j} \otimes (\frac{\sigma_x - i \sigma_y}{2}) \otimes I^{\otimes N-j-1}$$

Additionally, I wrote a new test in qutip.tests.core.test_operators that verified the commutation relations. I also ran the new code through pycodestyle to verify it was written nicely.

Please let me know if there are any issues or anything I should be doing on the documentation side! I hope I understood the problem correctly :)

Related issues or PRs
close #2165
I closely followed the protocol in #863

Copy link
Member

@BoxiLi BoxiLi 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 very much for the PR! The implementation and code style look good.

Here are a few comments:

  • The two functions share very similar code. Maybe it is better to define one private function and then use it in fcreate and fdestroy.
  • You could also add your source to the doc. Please make sure that URL follows the rst syntax used by Sphinx.

qutip/core/operators.py Outdated Show resolved Hide resolved
qutip/core/operators.py Outdated Show resolved Hide resolved
qutip/core/operators.py Outdated Show resolved Hide resolved
qutip/tests/core/test_operators.py Outdated Show resolved Hide resolved
qutip/core/operators.py Outdated Show resolved Hide resolved
@khnikhil
Copy link
Contributor Author

khnikhil commented May 27, 2023

@BoxiLi thank you so much for the comments!

I addressed all your comments in my new commit, but when I try to run the tests with pytest I get a strange error:

Screen Shot 2023-05-27 at 1 01 17 PM

This seems to be an issue with a circular import in qutip.core.data.__init__.py file, but I haven't touched anything in that file, or for that matter, any file besides qutip.core.operators.py and qutip.tests.core.test_operators.py. I wanted to reach out and ask if this problem has been resolved somewhere else.

Also, to avoid the circular import problem with tensor, would it be better if I created a new py file (qutip.core.fermionic_operators.py) for these new operators? happy to do this if it would be helpful.

Thank you so much, and please let me know if you have any insight on the pytest error :)

@BoxiLi
Copy link
Member

BoxiLi commented May 27, 2023

I addressed all your comments in my new commit, but when I try to run the tests with pytest I get a strange error:

This is likely because qutip is not correctly installed. Could you try uninstalling qutip and reinstalling again?

Also, to avoid the circular import problem with tensor, would it be better if I created a new py file (qutip.core.fermionic_operators.py) for these new operators?

A new file for this seems too much for me... I think operators.py is the best choice but it includes indeed mostly basic operators not using tensor. Maybe there is a better place for this @Ericgig ?

@khnikhil
Copy link
Contributor Author

This is likely because qutip is not correctly installed. Could you try uninstalling qutip and reinstalling again?

@BoxiLi I did this and it worked, thank you! The issue was that I had originally installed it with the build package instead of with the setuptools build.

Also, the code now passes both my new tests and existing tests in qutip.tests.core.test_operators.py. Additionally, I ran the pytest qutip/tests -k command on f_op, fcreate, and fdestroy to run all the tests associated with the new functions, and they pass as well.

Lastly, regarding the circular import problem, I did as you suggested and moved the import inside the f_op function.

@coveralls
Copy link

coveralls commented May 28, 2023

Coverage Status

coverage: 78.129% (+0.04%) from 78.092% when pulling f0c0919 on khnikhil:master into 6b6af0a on qutip:master.

Copy link
Member

@BoxiLi BoxiLi left a comment

Choose a reason for hiding this comment

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

Looks good in general. Two minor suggestions on the naming and docstring.

Let's wait a bit for others to give a double-check, if this should be in operators.py or moved to other places.

qutip/core/operators.py Outdated Show resolved Hide resolved
qutip/core/operators.py Outdated Show resolved Hide resolved
@khnikhil
Copy link
Contributor Author

hi @BoxiLi, thank you for the minor suggestions! I implemented them and pushed the changes.

Let's wait a bit for others to give a double-check, if this should be in operators.py or moved to other places.

sounds good :)

@BoxiLi
Copy link
Member

BoxiLi commented May 31, 2023

@khnikhil I just realized that this function has not been added to the API doc. Could you add fcreate, and fdestroy to
qutip/doc/apidoc/functions.rst, just like create? Sorry that I didn't realize this in the previous check.

Also, could you formulate the equations in math syntax so that Sphinx can render it correctly? For instance, like this:

def squeezing(a1, a2, z):
"""Generalized squeezing operator.
.. math::
S(z) = \\exp\\left(\\frac{1}{2}\\left(z^*a_1a_2
- za_1^\\dagger a_2^\\dagger\\right)\\right)

Does anyone want to give a double-check? @Ericgig, @hodgestar? Is it okay to put these functions in operators.py even if they use tensor?

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.

I think it's fine to include them in operators.py, unless we create a few more fermionic operators it's not worth it's own file.

Please include the functions in the documentation as Boxi mentioned.

qutip/core/operators.py Outdated Show resolved Hide resolved
qutip/core/operators.py Outdated Show resolved Hide resolved
qutip/tests/core/test_operators.py Outdated Show resolved Hide resolved
qutip/tests/core/test_operators.py Outdated Show resolved Hide resolved
@khnikhil
Copy link
Contributor Author

khnikhil commented Jun 1, 2023

hi @BoxiLi and @Ericgig,

thank you for the comments! I made the following changes:

  1. Added fcreate and fdestroy to qutip/doc/apidoc/functions.rst
  2. Put the equations in the docstrings into the proper Sphinx .. math:: format
  3. Incorporated Eric's suggestions.

@BoxiLi
Copy link
Member

BoxiLi commented Jun 1, 2023

@khnikhil Something went wrong with the dimension.

For the doctrines, the empty line before and after ..math:: is, unfortunately, part of the syntax and cannot be omitted. So there is a warning and the build fails.

qutip/core/operators.py Outdated Show resolved Hide resolved
@khnikhil
Copy link
Contributor Author

khnikhil commented Jun 1, 2023

@BoxiLi @Ericgig thank you for finding these bugs! I ran test_operators.py on my local device and it seems to be working now.

@BoxiLi BoxiLi merged commit 199394b into qutip:master Jun 2, 2023
12 checks passed
@BoxiLi
Copy link
Member

BoxiLi commented Jun 2, 2023

@khnikhil Congrats!

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.

Fermionic creation and annihilation operators
4 participants