-
Notifications
You must be signed in to change notification settings - Fork 16
Description
DeepQuantum v4.4.0 has flattened the operators list. Previously, operators contained layers as nested gates lists (e.g., operators.0.gates.0.theta), but now the model treats all gates as a single flat sequence of operators (e.g., operators.0.theta, operators.1.matrix, etc.).
In consequence, loading model state_dict trained by old version will cause a RuntimeError:
RuntimeError: Error(s) in loading state_dict for QubitCircuit: Missing key(s) in state_dict: "operators.0.theta", "operators.1.theta", "operators.3.theta", "operators.4.theta", "operators.5.matrix", "operators.6.matrix". Unexpected key(s) in state_dict: "operators.0.gates.0.theta", "operators.0.gates.1.theta", "operators.1.gates.0.theta", "operators.1.gates.1.theta", "operators.3.gates.0.matrix", "operators.3.gates.1.matrix".
In order to load an old state_dict, convert it to the new format via the following script:
from collections import OrderedDict
def convert_state_dict(old_state_dict):
"""
Converts the old hierarchical state_dict to the new flattened operator format.
"""
new_state_dict = OrderedDict()
# We initialize a counter specifically for the new flattened 'operators' list
operator_index = 0
for key, value in old_state_dict.items():
if key.startswith("operators"):
# Logic: The new model flattens the list.
# We take the parameter name (last part of the key, e.g., 'theta' or 'matrix')
# and assign it to the incrementing operator_index.
param_name = key.split('.')[-1] # Gets 'theta' or 'matrix'
new_key = f"operators.{operator_index}.{param_name}"
new_state_dict[new_key] = value
operator_index += 1
else:
# Keep 'init_state' and 'observables' exactly as they are
new_state_dict[key] = value
return new_state_dict
Then all keys matched successfully:
