Skip to content

Add shift kwarg to solve_fermion #201

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

Merged
merged 3 commits into from
May 29, 2025
Merged

Add shift kwarg to solve_fermion #201

merged 3 commits into from
May 29, 2025

Conversation

caleb-johnson
Copy link
Collaborator

Fixes #139

@caleb-johnson caleb-johnson requested a review from kevinsung May 7, 2025 15:49
If ``None``, no spin will be imposed.
shift: Level shift for states which have different spin. :math:`(H + shift * S^2)|ψ> = E|ψ>`
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

maybe this equation should go up in the spin_sq description? Maybe even in top-level description of this function?

@caleb-johnson
Copy link
Collaborator Author

caleb-johnson commented May 7, 2025

@kevinsung it seems like if you pass a spin_sq value (e.g. 0.0) then you must pass a numerical shift (i.e. don't leave the default None)? Passing spin_sq=0.0 and shift=None is giving pyscf errors as seen in the CI. I can fix this by changing the spin_sq to None in the notebooks. Does this sound right? I'll document those args to make this clear if so

@kevinsung
Copy link
Collaborator

Actually, we should set a default numerical value for the shift, instead of None. From https://pyscf.org/_modules/pyscf/fci/addons.html it's either 0.2 or 0.1. To know for sure we may have to look at the value it takes at runtime, i.e. print it out or use the debugger.

@@ -75,6 +75,7 @@ def solve_fermion(
*,
open_shell: bool = False,
spin_sq: float | None = None,
shift: float | None = None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

As I mentioned in the comment, we should make the default a number. From https://pyscf.org/_modules/pyscf/fci/addons.html it's either 0.2 or 0.1, but it's unclear which one. To be sure, we would need to run some code and print it out or use the debugger.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We have been calling fix_spin_ with the default value, so I think 0.1 is what has been the default for us

Copy link
Collaborator Author

@caleb-johnson caleb-johnson May 7, 2025

Choose a reason for hiding this comment

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

I decided to make the type hint float and cast to np.float64 inside. The linter didn't want me to use np.float64() in the annotation. I could do shift: np.number | float = 0.1, but I figured this is fine

if isinstance(bitstring_matrix, tuple):
ci_strs = bitstring_matrix
else:
ci_strs = bitstring_matrix_to_ci_strs(bitstring_matrix, open_shell=open_shell)
ci_strs = _check_ci_strs(ci_strs)
shift = np.float64(shift)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does the code actually break without this type conversion?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No I just did it bc its what pyscf says it wants

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok. In that case, I think you can delete it.

@caleb-johnson
Copy link
Collaborator Author

I think we need to change the default spin_sq to None to match pyscf.

@kevinsung
Copy link
Collaborator

I think we need to change the default spin_sq to None to match pyscf.

I did that for the solve_sci function I introduce in #195, which I would like to replace solve_fermion.

@caleb-johnson caleb-johnson merged commit ca4c49e into main May 29, 2025
11 of 12 checks passed
@caleb-johnson caleb-johnson deleted the shift branch May 29, 2025 15:17
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.

Support shift argument to solve_fermion for fix_spin_
2 participants