Skip to content

Commit

Permalink
Merge pull request #57 from qiboteam/dump
Browse files Browse the repository at this point in the history
Dump the whole optimization result as a dictionary
  • Loading branch information
andrea-pasquale authored Jun 27, 2024
2 parents 87d11cd + d0b9556 commit d475742
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
6 changes: 4 additions & 2 deletions compiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,16 @@ def main(args):
save_path="gci_step",
)

opt_dict = {"sgd_extras": "To be defined"}

else:
if gci_step_nmb == 0:
p0 = [0.01]
p0.extend([4 - np.sin(x / 3) for x in range(nqubits)])
else:
p0 = [best_s]
p0.extend(best_b)
optimized_params = optimize_D(
optimized_params, opt_dict = optimize_D(
params=p0,
gci=gci,
method=args.optimization_method,
Expand All @@ -156,7 +158,7 @@ def main(args):

this_report = report(vqe, hamiltonian, gci)
print_report(this_report)
metadata[gci_step_nmb + 1] = this_report | step_data
metadata[gci_step_nmb + 1] = this_report | step_data | opt_dict

(dump_path / "boosting_data.json").write_text(json.dumps(metadata, indent=4))

Expand Down
19 changes: 17 additions & 2 deletions src/boostvqe/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,8 @@ def optimize_D(
args=(gci,),
options={"bounds": bounds, "maxiter": maxiter},
)
return opt_results[0]
result_dict = convert_numpy(opt_results[-2].result._asdict())
return opt_results[0], {f"{method}_extras": result_dict}
# scipy optimizations
else:
bounds = [s_bounds]
Expand Down Expand Up @@ -606,4 +607,18 @@ def optimize_D(
method=method,
options={"disp": 1, "maxiter": maxiter},
)
return opt_results.x
return opt_results.x, {f"{method}_extras": convert_numpy(dict(opt_results))}


def convert_numpy(obj):
"""Convert numpy objects into python types which can be dumped."""
if isinstance(obj, np.ndarray):
return obj.tolist()
elif isinstance(obj, np.generic):
return obj.item()
elif isinstance(obj, dict):
return {key: convert_numpy(val) for key, val in obj.items()}
elif isinstance(obj, list):
return [convert_numpy(item) for item in obj]
else:
return obj

0 comments on commit d475742

Please sign in to comment.