-
Notifications
You must be signed in to change notification settings - Fork 1
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
Make all truncations/max_order/number parameters key/value pairs in dictionary #15
Comments
reminder that my code samples don't have the long form names for the keys, but they should |
changed due date to March 24, 2022 |
So we should have a python file called For example: this truncation is valid for the z_t ansatz (see https://git.uwaterloo.ca/ngraymon/exploratory-project/-/blob/implement_pytest/latex_zhz.py#L1723) the maximum_h_rank = 2
maximum_cc_rank = 6
omega_max_order = 3 this truncation is valid for the full cc ansatz maximum_h_rank = 2
maximum_cc_rank = 6
s_taylor_max_order = 2
omega_max_order = 3 but for the eT_z_t ansats we need a truncation like this maximum_h_rank = 2
maximum_cc_rank = 4
maximum_T_rank = 1
eT_taylor_max_order = 4
omega_max_order = 4 |
The file should have similar verification functions and (some sort of) creation functions as in the deep tests |
Per 46c0c555624deede3ccd32ad05ac4853c3c196d0 one way to do this is using enums also note that enum docs are here : https://docs.python.org/3.8/library/enum.html?highlight=namedtuples and then you can import it like so from .vibronic_model_keys import VibronicModelKeys as VMK
print(VMK.E) |
Saving to json looks like: def _save_to_JSON(path, dictionary):
dict_copy = copy.deepcopy(dictionary)
VMK.change_dictionary_keys_from_enum_members_to_strings(dict_copy)
""" converts each numpy array to a list so that json can serialize them properly"""
for key, value in list(dict_copy.items()):
if isinstance(value, (np.ndarray, np.generic)):
if np.count_nonzero(value) > 0:
dict_copy[key] = value.tolist()
else:
del dict_copy[key]
else:
log.debug(f"Value {value} with Key {key} does not appear to be an ndarray")
with open(path, mode='w', encoding='UTF8') as target_file:
target_file.write(json.dumps(dict_copy))
return
def save_model_to_JSON(path, dictionary):
""" wrapper for _save_to_JSON
calls verify_model_parameters() before calling _save_to_JSON()
"""
verify_model_parameters(dictionary)
log.debug(f"Saving model to {path:s}")
_save_to_JSON(path, dictionary)
return
and loading def _load_from_JSON(path):
"""returns a dictionary filled with the values stored in the .json file located at path"""
with open(path, mode='r', encoding='UTF8') as file:
input_dictionary = json.loads(file.read())
VMK.change_dictionary_keys_from_strings_to_enum_members(input_dictionary)
for key, value in input_dictionary.items():
if isinstance(value, list):
# if we don't predefine the shape, can we run into problems?
input_dictionary[key] = np.array(value, dtype=F64)
# special case to always create an array of energies that are 0.0 if not provided in the .json file
if VMK.E not in input_dictionary:
A, N = _extract_dimensions_from_dictionary(input_dictionary)
shape = model_shape_dict(A, N)
input_dictionary[VMK.E] = np.zeros(shape[VMK.E], dtype=F64)
# TODO - design decision about which arrays to fill with zeros by default?
return input_dictionary
def load_model_from_JSON(path, dictionary=None):
"""
if kwargs is not provided then returns a dictionary filled with the values stored in the .json file located at path
if kwargs is provided then all values are overwritten (in place) with the values stored in the .json file located at path
"""
log.debug(f"Loading model from {path:s}")
# no arrays were provided so return newly created arrays after filling them with the appropriate values
if not bool(dictionary):
new_model_dict = _load_from_JSON(path)
# TODO - we might want to make sure that none of the values in the dictionary have all zero values or are None
verify_model_parameters(new_model_dict)
return new_model_dict
# arrays were provided so fill them with the appropriate values
else:
verify_model_parameters(dictionary)
_load_inplace_from_JSON(path, dictionary)
# check twice? might as well be cautious for the moment until test cases are written
verify_model_parameters(dictionary)
return |
def load_trunc_from_JSON(path, dictionary):
# steal from vibronic
def save_to_JSON(path, dictionary):
""" to make cleaner code this ATTEMPTS to automatically determine dictionary type
and then save the appropriate truncation
"""
key_list = list(dictionary.keys())
# assume fcc?
if len(key_list) == 4:
for key in TruncationsKeys.fcc_key_list()
if key not in key_list
raise Exception("something")
# only get here if all keys are in key_list
else:
_save_fcc_trunc_to_JSON(path, dictionary)
elif len(key_list) == 5:
for key in TruncationsKeys.eT_z_t_key_list()
if key not in key_list
raise Exception("something")
# only get here if all keys are in key_list
else:
_save_fcc_trunc_to_JSON(path, dictionary) |
Then the next step would be to start replacing relevant parts inside from truncations import TruncationsKeys as tkeys
fcc_trunc = {
tkeys.H: 2,
tkeys.H: 6,
tkeys.H: 2,
tkeys.H: 3,
} and instead of assert len(truncations) == 4, "truncations argument needs to be tuple of four integers!!" we want to call some verification function from and also extract out like maximum_h_rank = truncations[tkeys.H]
maximum_cc_rank = truncations[tkeys.CC]
s_taylor_max_order = truncations[tkeys.S]
omega_max_order = truncations[tkeys.P] |
tkeys._verify_eT_z_t_truncations(truncations)
maximum_h_rank = truncations[tkeys.O]
maximum_cc_rank = truncations[tkeys.O]
maximum_T_rank = truncations[tkeys.O]
eT_taylor_max_order = truncations[tkeys.O]
omega_max_order = truncations[tkeys.O] |
to replace the |
On the 29th some comments:
# if empty dict
if not bool(kwargs):
kwargs = default_kwargs
else:
# maybe do value checks?
for key, value in default_kwargs.items():
if key in kwargs:
assert type(value) == type(kwargs[key])
default_kwargs.update(kwargs)
|
The last thing is after doing all of the above change the blanket test(as per #43) to pass and match the new specs |
Then we can close this issue |
Rather then having them be a list of numbers orindividual keywords (which makes the function definitions super crazy long and unreadable) lets just have all of these 'master/parent' functions take an argument
truncation
which should always be a dictionary. Also add in simple checks for each function to make sure the expected keys are present and that the numbers provided are in an acceptable range (0 < N <max_number
is probably fine for all of them, define the maximums at the top of the file)Also we probably want the dictionary keys to be expanded in full so as to not confuse them with the variables used in the functions.
Meaning
maximum_h_rank
becomesmaximum_hamiltonian_rank
maximum_cc_rank
becomesmaximum_coupled_cluster_rank
so like just below here
and then around line 3265 (cut out the red lines)
![image](https://camo.githubusercontent.com/9c443e0aa434156aa49b59ef6b9d5fb45c37302486a6c1cc80204438b0492296/68747470733a2f2f6769742e7577617465726c6f6f2e63612f6e677261796d6f6e2f6578706c6f7261746f72792d70726f6a6563742f75706c6f6164732f37363738383465326266633865363438626630313134393936353339323562322f696d6167652e706e67)
The text was updated successfully, but these errors were encountered: