tequilahub / tequila

A High-Level Abstraction Framework for Quantum Algorithms
MIT License
362 stars 101 forks source link

Multi-line string causes ParametersQC to return self.geometry = [] #175

Closed MIWdlB closed 3 years ago

MIWdlB commented 3 years ago

I was trying to build a QuantumChemistryBase object, for which I needed a ParametersQC.

My geometry was set up from earlier pyscf/psi4 calculations like:

geometry = """
    H 0.0 0.0 0.0
    O 0.0 0.0 1.0
    """

As a singe line string this is "\n H ... \n O ... \ When ParametersQS gets to calling the convert_to_list method to reformat the geomtery it return an empty list.

That's because it splits the string into lines, then loops through lines, breaking at the first which doesn't have length 4.

    def convert_to_list(geometry):
        result = []
        for line in geometry.split('\n'):
            words = line.split()
            if len(words) != 4:  break
            try:
                tmp = (ParametersQC.format_element_name(words[0]),
                       (float(words[1]), float(words[2]), float(words[3])))
                result.append(tmp)
            except ValueError:
                print("get_geometry list unknown line:\n ", line, "\n proceed with caution!")
        return result

This error was a little tricky to find because it only becomes an issue in the Openfermion package when it tries to name a molecule based on the atoms described by its geometry.

As far as I can tell this will also be a problem for anyone using the format below, which (i think) all the popular chemistry packages accept:

geometry = """
    H 0.0
    O 0.0 1.0
    H 0.0 0.0 1.0
    """

I'm happy to either add in a check to make sure the geometry is in the expected single line format (not my preference for readability), or to add a fix to the function that remove trialling blank lines and fills in 0s if needed. Let me know what you prefer :smile:

kottmanj commented 3 years ago

Hi Michael, thanks for the detailed description of the issue! It looks like you have already figured it all out. So adding the full fix would be terrific. Thank you :-) If you want you can just make a pr to the devel branch with the fix. Feel free to let me know if you have any questions regarding that.

Just to be sure that I don't miss something: I don't fully understand the last format, are the missing lines supposed to be filled with 0.0 (if so the last line was probably supposed to have some entry differ from 0.0) or is this not xyz? geometry = """ H 0.0 O 0.0 1.0 H 0.0 0.0 0.0 """

MIWdlB commented 3 years ago

No problem, I'll set up a PR and link it here when I do.

And yes, that's my mistake - I've update the original comment to make it clearer, thanks.

kottmanj commented 3 years ago

Awesome! I'll keep the issue open until then.