-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from lfd/annealing_schedules.py
Pipeline for computing anneal schedules from QAOA parameters
- Loading branch information
Showing
19 changed files
with
871 additions
and
530 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,15 @@ | ||
output_path: "results" | ||
trackml_input_path: "hepqpr-qallse/qallse/dsmaker/data/event000001000" | ||
data_fraction: 0.1 | ||
num_angle_parts: 64 | ||
|
||
seed: 1000 | ||
max_p: 2 | ||
|
||
max_p: 20 | ||
q: -1 | ||
|
||
num_anneal_fractions: 11 | ||
|
||
maxcut_max_qubits: 10 | ||
|
||
qaoa_result_file: "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/fromhopetoheuristics/pipelines/adiabatic_maxcut/nodes.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
from datetime import datetime | ||
import numpy as np | ||
from typing import Iterable | ||
import os | ||
|
||
from fromhopetoheuristics.utils.maxcut_utils import provide_random_maxcut_QUBO | ||
from fromhopetoheuristics.utils.data_utils import save_to_csv | ||
from fromhopetoheuristics.utils.spectral_gap_calculator import ( | ||
calculate_spectral_gap, | ||
) | ||
import logging | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def maxcut_annealing( | ||
num_qubits: int, | ||
density: float, | ||
seed: int, | ||
fractions: Iterable[float], | ||
result_path_prefix: str, | ||
include_header: bool = True, | ||
): | ||
csv_data_list = [] | ||
if include_header: | ||
csv_data_list.append( | ||
[ | ||
"problem", | ||
"num_qubits", | ||
"density", | ||
"seed", | ||
"fraction", | ||
"gs", | ||
"fes", | ||
"gap", | ||
] | ||
) | ||
|
||
qubo = provide_random_maxcut_QUBO(num_qubits, density, seed) | ||
|
||
for fraction in fractions: | ||
gs_energy, fes_energy, gap = calculate_spectral_gap(fraction, qubo) | ||
csv_data_list.append( | ||
[ | ||
"maxcut", | ||
num_qubits, | ||
density, | ||
seed, | ||
np.round(fraction, 2), | ||
gs_energy, | ||
fes_energy, | ||
gap, | ||
] | ||
) | ||
|
||
for csv_data in csv_data_list: | ||
save_to_csv( | ||
csv_data, | ||
result_path_prefix, | ||
"spectral_gap_evolution.csv", | ||
) | ||
|
||
|
||
def run_maxcut_annealing( | ||
result_path_prefix: str, | ||
seed: int, | ||
num_anneal_fractions: int, | ||
maxcut_max_qubits: int, | ||
): | ||
time_stamp = datetime.now().strftime("%Y-%m-%d-%H-%M-%S") | ||
|
||
fractions = np.linspace(0, 1, num=num_anneal_fractions, endpoint=True) | ||
result_path_prefix = os.path.join( | ||
result_path_prefix, "MAXCUT/spectral_gap", time_stamp | ||
) | ||
first = True | ||
for n in range(4, maxcut_max_qubits + 1): | ||
log.info( | ||
f"Computing spectral gaps for QUBO with n={n} of " | ||
"{maxcut_max_qubits}" | ||
) | ||
for density in np.linspace(0.5, 1, num=6, endpoint=True): | ||
log.info(f"\twith density={density}") | ||
maxcut_annealing( | ||
n, | ||
density, | ||
seed, | ||
fractions, | ||
result_path_prefix, | ||
include_header=first, # FIXME | ||
) | ||
first = False | ||
|
||
return {} # FIXME |
22 changes: 22 additions & 0 deletions
22
src/fromhopetoheuristics/pipelines/adiabatic_maxcut/pipeline.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from kedro.pipeline import Pipeline, node, pipeline | ||
|
||
from .nodes import ( | ||
run_maxcut_annealing, | ||
) | ||
|
||
|
||
def create_pipeline() -> Pipeline: | ||
return pipeline( | ||
[ | ||
node( | ||
run_maxcut_annealing, | ||
{ | ||
"result_path_prefix": "params:output_path", | ||
"seed": "params:seed", | ||
"num_anneal_fractions": "params:num_anneal_fractions", | ||
"maxcut_max_qubits": "params:maxcut_max_qubits", | ||
}, | ||
{}, | ||
) | ||
] | ||
) |
92 changes: 92 additions & 0 deletions
92
src/fromhopetoheuristics/pipelines/adiabatic_trackrec/nodes.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import os | ||
from datetime import datetime | ||
import numpy as np | ||
from typing import Iterable, Optional, List | ||
|
||
from fromhopetoheuristics.utils.spectral_gap_calculator import ( | ||
calculate_spectral_gap, | ||
) | ||
from fromhopetoheuristics.utils.data_utils import save_to_csv | ||
import logging | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def track_reconstruction_annealing( | ||
qubo: np.ndarray, | ||
seed: int, | ||
fractions: Iterable[float], | ||
result_path_prefix: str, | ||
geometric_index: int = 0, | ||
include_header: bool = True, | ||
): | ||
csv_data_list = [] | ||
if include_header: | ||
csv_data_list.append( | ||
[ | ||
"problem", | ||
"num_qubits", | ||
"geometric_index", | ||
"seed", | ||
"fraction", | ||
"gs", | ||
"fes", | ||
"gap", | ||
] | ||
) | ||
|
||
for fraction in fractions: | ||
gs_energy, fes_energy, gap = calculate_spectral_gap( | ||
fraction, | ||
qubo, | ||
) | ||
csv_data_list.append( | ||
[ | ||
"track reconstruction", | ||
len(qubo), | ||
geometric_index, | ||
seed, | ||
np.round(fraction, 2), | ||
gs_energy, | ||
fes_energy, | ||
gap, | ||
] | ||
) | ||
|
||
for csv_data in csv_data_list: | ||
save_to_csv(csv_data, result_path_prefix, "spectral_gap_evolution.csv") | ||
|
||
|
||
def run_track_reconstruction_annealing( | ||
qubos: List[Optional[np.ndarray]], | ||
event_path: str, | ||
seed: int, | ||
num_anneal_fractions: int, | ||
): | ||
time_stamp = datetime.now().strftime("%Y-%m-%d-%H-%M-%S") | ||
|
||
fractions = np.linspace(0, 1, num=num_anneal_fractions, endpoint=True) | ||
result_path_prefix = os.path.join( | ||
os.path.dirname(event_path), | ||
"spectral_gap", | ||
time_stamp, | ||
) | ||
first = True | ||
|
||
for i, qubo in enumerate(qubos): | ||
if qubo is not None: | ||
log.info( | ||
f"Computing spectral gaps for QUBO {i+1}/{len(qubos)} " | ||
f"(n={len(qubo)})" | ||
) | ||
track_reconstruction_annealing( | ||
qubo, | ||
seed, | ||
fractions, | ||
result_path_prefix, | ||
geometric_index=i, | ||
include_header=first, # FIXME | ||
) | ||
first = False | ||
|
||
return {} # FIXME |
22 changes: 22 additions & 0 deletions
22
src/fromhopetoheuristics/pipelines/adiabatic_trackrec/pipeline.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from kedro.pipeline import Pipeline, node, pipeline | ||
|
||
from .nodes import ( | ||
run_track_reconstruction_annealing, | ||
) | ||
|
||
|
||
def create_pipeline() -> Pipeline: | ||
return pipeline( | ||
[ | ||
node( | ||
run_track_reconstruction_annealing, | ||
{ | ||
"qubos": "qubos", | ||
"event_path": "event_path", | ||
"seed": "params:seed", | ||
"num_anneal_fractions": "params:num_anneal_fractions", | ||
}, | ||
{}, | ||
), | ||
] | ||
) |
Oops, something went wrong.