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

added optional subspace to diagonalization #146

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions qiskit_addon_sqd/fermion.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
from pyscf import fci
from qiskit.utils import deprecate_func
from scipy import linalg as LA
import itertools
from itertools import combinations
from typing import Optional

config.update("jax_enable_x64", True) # To deal with large integers

Expand Down Expand Up @@ -79,6 +82,10 @@ def solve_fermion(
spin_sq: float | None = None,
max_davidson: int = 100,
verbose: int | None = None,
subspace: bool = False,
subspace_orb: Optional[int] = None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
subspace_orb: Optional[int] = None,
subspace_orb: int | None = None,

and similarly elsewhere

Copy link
Collaborator

Choose a reason for hiding this comment

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

You need to document these arguments in the docstring.

subspace_alpha: Optional[int] = None,
subspace_beta: Optional[int] = None,
) -> tuple[float, SCIState, list[np.ndarray], float]:
"""Approximate the ground state given molecular integrals and a set of electronic configurations.

Expand Down Expand Up @@ -125,9 +132,19 @@ def solve_fermion(

num_up = format(ci_strs[0][0], "b").count("1")
num_dn = format(ci_strs[1][0], "b").count("1")


# Number of molecular orbitals
norb = hcore.shape[0]

#If subspace true, append the CIs if not found
if subspace:
subspace_m = generate_bitstrings(norb, num_up,num_dn, subspace_orb, subspace_alpha, subspace_beta)
subspace_ci = bitstring_matrix_to_ci_strs(subspace_m, open_shell=open_shell)
for ind in range(len(subspace_ci[0])):
ci_strs = np.append(ci_strs[0],subspace_ci[0][ind]) if subspace_ci[0][ind] not in ci_strs[0] else ci_strs[0],np.append(ci_strs[1],subspace_ci[1][ind]) if subspace_ci[1][ind] not in ci_strs[1] else ci_strs[1]
ci_strs = np.sort(ci_strs)

# Call the projection + eigenstate finder
myci = fci.selected_ci.SelectedCI()
if spin_sq is not None:
Expand Down Expand Up @@ -593,3 +610,17 @@ def _transition_str_to_bool(string_rep: np.ndarray) -> tuple[np.ndarray, np.ndar
annihilate = np.logical_or(string_rep == "-", string_rep == "n")

return diag, create, annihilate

def generate_bitstrings(norb, neleca,nelecb, n_orb, n_alpha, n_beta):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please write a docstring for this function. If it's meant to be a private function, prefix it with an underscore _. Also, please add type annotations to the function arguments.

def get_strings(n, k):
return [''.join('1' if i in comb else '0' for i in range(n))
for comb in combinations(range(n), k)]
alpha_strings = get_strings(n_orb, n_alpha)
beta_strings = get_strings(n_orb, n_beta)
d_orb = neleca+nelecb - n_alpha - n_beta
doubly_occ = [''.join(seq) for seq in itertools.product('1', repeat=int(d_orb/2))]
Copy link
Collaborator

Choose a reason for hiding this comment

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

This line is equivalent to

double_occ = ['1' * int(d_orb / 2)]

Is this what you meant to write? I'm having a bit of trouble following the logic.

virtual_occ = [''.join(seq) for seq in itertools.product('0', repeat=int(norb - n_orb - d_orb/2))]
bitstrings = [v + b +d +v + a + d for a in alpha_strings for b in beta_strings for d in doubly_occ for v in virtual_occ]
core_bistrings = [b + a for a in alpha_strings for b in beta_strings]
bitstrings_bool = np.array([[bit == '1' for bit in bs] for bs in bitstrings], dtype=bool)
return bitstrings_bool
Loading