Skip to content

Commit

Permalink
Merge pull request #20 from qiboteam/avoid_storing_h
Browse files Browse the repository at this point in the history
Avoid storing hamiltonian at each iteration
  • Loading branch information
Edoardo-Pedicillo authored Mar 22, 2024
2 parents c486db6 + 7959e25 commit 0835d30
Show file tree
Hide file tree
Showing 16 changed files with 74 additions and 24 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ python main.py --help
```

```sh
usage: main.py [-h] [--backend BACKEND] [--platform PLATFORM] [--nthreads NTHREADS] [--optimizer OPTIMIZER] [--tol TOL]
[--nqubits NQUBITS] [--nlayers NLAYERS] [--output_folder OUTPUT_FOLDER] [--nboost NBOOST]
[--boost_frequency BOOST_FREQUENCY] [--dbi_steps DBI_STEPS] [--stepsize STEPSIZE]
[--optimize_dbi_step OPTIMIZE_DBI_STEP] [--hamiltonian HAMILTONIAN]
usage: main.py [-h] [--backend BACKEND] [--platform PLATFORM] [--nthreads NTHREADS] [--optimizer OPTIMIZER] [--tol TOL] [--nqubits NQUBITS] [--nlayers NLAYERS] [--output_folder OUTPUT_FOLDER] [--nboost NBOOST]
[--boost_frequency BOOST_FREQUENCY] [--dbi_steps DBI_STEPS] [--stepsize STEPSIZE] [--optimize_dbi_step OPTIMIZE_DBI_STEP] [--store_h STORE_H] [--hamiltonian HAMILTONIAN] [--seed SEED]

VQE with DBI training hyper-parameters.

Expand All @@ -59,10 +57,10 @@ options:
--stepsize STEPSIZE DBI step size.
--optimize_dbi_step OPTIMIZE_DBI_STEP
Set to True to hyperoptimize the DBI step size.
--store_h STORE_H If true H is stored for each iteration
--hamiltonian HAMILTONIAN
Hamiltonian available in qibo.hamiltonians.
(test_env) andreapasquale@TII-APASQUALE01:~/boostvqe(readme)$

--seed SEED Random seed
```

<img src="example.png" style="solid #000; max-width:600px; max-height:1000px;">
Expand Down
Binary file added bp_tests/Powell_11q_20l_1/dbi_d_matrices.npz
Binary file not shown.
Binary file added bp_tests/Powell_11q_20l_1/dbi_energies.npz
Binary file not shown.
Binary file added bp_tests/Powell_11q_20l_1/dbi_fluctuations.npz
Binary file not shown.
Binary file added bp_tests/Powell_11q_20l_1/dbi_steps.npz
Binary file not shown.
Binary file added bp_tests/Powell_11q_20l_1/energies.npz
Binary file not shown.
Binary file added bp_tests/Powell_11q_20l_1/fluctuations.npz
Binary file not shown.
Binary file added bp_tests/Powell_11q_20l_1/gradients.npz
Binary file not shown.
Binary file added bp_tests/Powell_11q_20l_1/grads_Grads history.pdf
Binary file not shown.
Binary file not shown.
24 changes: 24 additions & 0 deletions bp_tests/Powell_11q_20l_1/optimization_results.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"backend": "numpy",
"platform": null,
"nthreads": 1,
"optimizer": "Powell",
"tol": 1e-10,
"nqubits": 11,
"nlayers": 20,
"output_folder": "bp_tests",
"nboost": 2,
"boost_frequency": 100,
"dbi_steps": 1,
"stepsize": 0.01,
"optimize_dbi_step": false,
"store_h": false,
"hamiltonian": "XXZ",
"seed": "1",
"best_loss": -15.793532338989099,
"true_ground_energy": -15.94339416470582,
"success": false,
"message": "Maximum number of function evaluations has been exceeded.",
"energy": -15.78486757135421,
"fluctuations": 1.2778472869243782
}
Binary file added bp_tests/Powell_11q_20l_1/parameters_history.npy
Binary file not shown.
42 changes: 36 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
from boostvqe.ansatze import build_circuit
from boostvqe.plotscripts import plot_gradients, plot_loss
from boostvqe.utils import (
DBI_D_MATRIX,
DBI_ENERGIES,
DBI_FLUCTUATIONS,
DBI_STEPS,
FLUCTUATION_FILE,
GRADS_FILE,
HAMILTONIAN_FILE,
Expand Down Expand Up @@ -59,7 +61,7 @@ def main(args):
zero_state = backend.zero_state(args.nqubits)

# print the circuit
logging.info("\n" + circ.draw())
# logging.info("\n" + circ.draw())

# fix numpy seed to ensure replicability of the experiment
np.random.seed(SEED)
Expand All @@ -68,7 +70,7 @@ def main(args):
# vqe lists
params_history, loss_history, grads_history, fluctuations = {}, {}, {}, {}
# dbi lists
boost_energies, boost_fluctuations_dbi = {}, {}
boost_energies, boost_fluctuations_dbi, boost_steps, boost_d_matrix = {}, {}, {}, {}
# hamiltonian history
hamiltonians_history = []
hamiltonians_history.append(ham.matrix)
Expand Down Expand Up @@ -121,7 +123,13 @@ def main(args):
fluctuations_h0 = float(dbi.h.energy_fluctuation(zero_state))

# apply DBI
dbi_hamiltonians, dbi_energies, dbi_fluctuations = apply_dbi_steps(
(
dbi_hamiltonians,
dbi_energies,
dbi_fluctuations,
dbi_steps,
dbi_d_matrix,
) = apply_dbi_steps(
dbi=dbi, nsteps=args.dbi_steps, optimize_step=args.optimize_dbi_step
)
hamiltonians_history.extend(dbi_hamiltonians)
Expand All @@ -130,13 +138,13 @@ def main(args):
dbi_energies.insert(0, energy_h0)
boost_fluctuations_dbi[b] = np.array(dbi_fluctuations)
boost_energies[b] = np.array(dbi_energies)
boost_steps[b] = np.array(dbi_steps)
boost_d_matrix[b] = np.array(dbi_d_matrix)
vqe.hamiltonian = dbi_hamiltonians[-1]
initial_parameters = np.zeros(len(initial_parameters))
# print(hamiltonians_history)
opt_results = partial_results[2]
# save final results
output_dict = vars(args)

output_dict.update(
{
"best_loss": float(opt_results.fun),
Expand All @@ -159,18 +167,28 @@ def main(args):
path / FLUCTUATION_FILE,
**{json.dumps(key): np.array(value) for key, value in fluctuations.items()},
)
np.savez(path / HAMILTONIAN_FILE, *hamiltonians_history)
if args.store_h:
np.savez(path / HAMILTONIAN_FILE, *hamiltonians_history)
np.savez(
path / DBI_ENERGIES,
**{json.dumps(key): np.array(value) for key, value in boost_energies.items()},
)
np.savez(
path / DBI_D_MATRIX,
**{json.dumps(key): np.array(value) for key, value in boost_d_matrix.items()},
)
np.savez(
path / DBI_STEPS,
**{json.dumps(key): np.array(value) for key, value in boost_steps.items()},
)
np.savez(
path / DBI_FLUCTUATIONS,
**{
json.dumps(key): np.array(value)
for key, value in boost_fluctuations_dbi.items()
},
)

logging.info("Dump the results")
results_dump(path, params_history, output_dict)
plot_loss(
Expand Down Expand Up @@ -234,11 +252,23 @@ def main(args):
default=False,
help="Set to True to hyperoptimize the DBI step size.",
)
parser.add_argument(
"--store_h",
type=bool,
default=False,
help="If true H is stored for each iteration",
)
parser.add_argument(
"--hamiltonian",
type=str,
default="XXZ",
help="Hamiltonian available in qibo.hamiltonians.",
)
parser.add_argument(
"--seed",
type=str,
default=SEED,
help="Random seed",
)
args = parser.parse_args()
main(args)
8 changes: 1 addition & 7 deletions src/boostvqe/plotscripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@
DBI_ENERGIES,
DBI_FLUCTUATIONS,
FLUCTUATION_FILE,
FLUCTUATION_FILE2,
GRADS_FILE,
LOSS_FILE,
LOSS_FILE2,
OPTIMIZATION_FILE,
PLOT_FILE,
json_load,
)

RED = "#f54242"
Expand Down Expand Up @@ -149,9 +145,7 @@ def plot_gradients(
"""
grads = dict(np.load(path / f"{GRADS_FILE + '.npz'}"))
config = json.loads((path / OPTIMIZATION_FILE).read_text())

ave_grads = []

for epoch in grads:
for grads_list in grads[epoch]:
ave_grads.append(np.mean(np.abs(grads_list)))
Expand All @@ -166,7 +160,7 @@ def plot_gradients(
label=r"$\langle |\partial_{\theta_i}\text{L}| \rangle_i$",
)
for b in range(config["nboost"] - 1):
boost_x = config["boost_frequency"] * (b + 1)
boost_x = len(grads[str(b)]) * (b + 1)
if b == 0:
plt.plot(
(boost_x, boost_x + 1),
Expand Down
12 changes: 7 additions & 5 deletions src/boostvqe/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
LOSS_FILE = "energies"
GRADS_FILE = "gradients"
HAMILTONIAN_FILE = "hamiltonian_matrix.npz"
FLUCTUATION_FILE2 = "fluctuations2"
LOSS_FILE2 = "energies2"
SEED = 42
TOL = 1e-10
DBI_ENERGIES = "dbi_energies"
DBI_FLUCTUATIONS = "dbi_fluctuations"
DBI_STEPS = "dbi_steps"
DBI_D_MATRIX = "dbi_d_matrices"


logging.basicConfig(level=logging.INFO)
Expand All @@ -32,7 +32,7 @@ def generate_path(args) -> str:
output_folder = "results"
else:
output_folder = args.output_folder
return f"./{output_folder}/{args.optimizer}_{args.nqubits}q_{args.nlayers}l"
return f"./{output_folder}/{args.optimizer}_{args.nqubits}q_{args.nlayers}l_{args.seed}"


def create_folder(path: str) -> Path:
Expand Down Expand Up @@ -159,7 +159,7 @@ def rotate_h_with_vqe(hamiltonian, vqe):
def apply_dbi_steps(dbi, nsteps, stepsize=0.01, optimize_step=False):
"""Apply `nsteps` of `dbi` to `hamiltonian`."""
step = stepsize
energies, fluctuations, hamiltonians = [], [], []
energies, fluctuations, hamiltonians, steps, d_matrix = [], [], [], [], []
logging.info(f"Applying {nsteps} steps of DBI to the given hamiltonian.")
for _ in range(nsteps):
if optimize_step:
Expand All @@ -171,9 +171,11 @@ def apply_dbi_steps(dbi, nsteps, stepsize=0.01, optimize_step=False):
# Restore the original logging level
logging.getLogger().setLevel(logging.INFO)
dbi(step=step, d=dbi.diagonal_h_matrix)
steps.append(step)
d_matrix.append(np.diag(dbi.diagonal_h_matrix))
energies.append(dbi.h.expectation(dbi.h.backend.zero_state(dbi.h.nqubits)))
fluctuations.append(
dbi.energy_fluctuation(dbi.h.backend.zero_state(dbi.h.nqubits))
)
hamiltonians.append(dbi.h.matrix)
return hamiltonians, energies, fluctuations
return hamiltonians, energies, fluctuations, steps, d_matrix
2 changes: 2 additions & 0 deletions vqe.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
python main.py --nqubits 5 --nlayers 1 --optimizer Powell --output_folder test --backend numpy --nboost 0 --dbi_steps 0 --boost_frequency 1000

0 comments on commit 0835d30

Please sign in to comment.