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

Copying SecondQuantizedMolecule #361

Open
alexfleury-sb opened this issue Dec 7, 2023 · 0 comments
Open

Copying SecondQuantizedMolecule #361

alexfleury-sb opened this issue Dec 7, 2023 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@alexfleury-sb
Copy link
Collaborator

alexfleury-sb commented Dec 7, 2023

Feature Request: Deepcopy of SecondQuantizedMolecule

At this time, the SecondQuantizedMolecule can't be deepcopied:

from copy import deepcopy
from tangelo import SecondQuantizedMolecule

xyz_H2 = """
    H 0. 0. 0.00
    H 0. 0. 0.75
"""
mol = SecondQuantizedMolecule(xyz_H2)
deepcopy(mol)

outputs

Traceback (most recent call last):
  File "/home/alex/Scratch/mo_coeff_bug.py", line 14, in <module>
    deepcopy(mol)
  File "/home/alex/.pyenv/versions/3.10.13/lib/python3.10/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/home/alex/.pyenv/versions/3.10.13/lib/python3.10/copy.py", line 271, in _reconstruct
    state = deepcopy(state, memo)
  File "/home/alex/.pyenv/versions/3.10.13/lib/python3.10/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/home/alex/.pyenv/versions/3.10.13/lib/python3.10/copy.py", line 231, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/home/alex/.pyenv/versions/3.10.13/lib/python3.10/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/home/alex/.pyenv/versions/3.10.13/lib/python3.10/copy.py", line 271, in _reconstruct
    state = deepcopy(state, memo)
  File "/home/alex/.pyenv/versions/3.10.13/lib/python3.10/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/home/alex/.pyenv/versions/3.10.13/lib/python3.10/copy.py", line 231, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/home/alex/.pyenv/versions/3.10.13/lib/python3.10/copy.py", line 161, in deepcopy
    rv = reductor(4)
TypeError: cannot pickle 'module' object

This is not a critical bug at this moment, but it would be useful to be able to create a copy and update its attributes. For e.g.,

from copy import copy
from openfermion.linalg import eigenspectrum

def doing_something_with_molecule(sqmol):
    sqmol_copy = copy(sqmol)
    # Doing awesome stuff with mo_coeff and creating new_mo_coeff.
    sqmol_copy.mo_coeff = new_coeff
    return sqmol_copy

eigenvalues_original = eigenspectrum(mol.fermionic_hamiltonian)
updated_mol = doing_something_with_molecule(mol)
eigenvalues_verif = eigenspectrum(mol.fermionic_hamiltonian) # Should be the same as eigenvalues_original, but it is not.

# Doing other stuff with updated_mol...

The function doing_something_with_molecule is changing the MO coefficients of the sqmol_copy, but as the copy is shallow, it is also changing the MO coefficients of the mol object. This can introduce unexpected behaviour, as I experienced when working with FNO -> the MO coefficients were bound to the active space selection, so the fermionic Hamiltonian computed after the doing_something_with_molecule call wasn't trowing an error, but was physically wrong.

@alexfleury-sb alexfleury-sb added the enhancement New feature or request label Dec 7, 2023
@alexfleury-sb alexfleury-sb changed the title [FEATURE REQUEST] Copying SecondQuantizedMolecule Copying SecondQuantizedMolecule May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants