Skip to content

Commit

Permalink
Merge pull request #9 from lfd/annealing_schedules.py
Browse files Browse the repository at this point in the history
Pipeline for computing anneal schedules from QAOA parameters
  • Loading branch information
stroblme authored Oct 17, 2024
2 parents b793eb7 + bfa28da commit 34a5819
Show file tree
Hide file tree
Showing 19 changed files with 871 additions and 530 deletions.
10 changes: 9 additions & 1 deletion conf/base/parameters.yml
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: ""
51 changes: 47 additions & 4 deletions src/fromhopetoheuristics/pipeline_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,26 @@
from kedro.framework.project import find_pipelines
from kedro.pipeline import Pipeline

from fromhopetoheuristics.pipelines.qubo.pipeline import (
create_pipeline as create_qubo_pipeline,
)
from fromhopetoheuristics.pipelines.adiabatic_maxcut.pipeline import (
create_pipeline as create_adiabatic_maxcut_pipeline,
)
from fromhopetoheuristics.pipelines.qaoa_maxcut.pipeline import (
create_pipeline as create_qaoa_maxcut_pipeline,
)
from fromhopetoheuristics.pipelines.adiabatic_trackrec.pipeline import (
create_pipeline as create_adiabatic_trackrec_pipeline,
)
from fromhopetoheuristics.pipelines.qaoa_trackrec.pipeline import (
create_pipeline as create_qaoa_trackrec_pipeline,
)
from fromhopetoheuristics.pipelines.generation.pipeline import (
create_pipeline as create_generation_pipeline,
)
from fromhopetoheuristics.pipelines.science.pipeline import (
create_pipeline as create_science_pipeline,
from fromhopetoheuristics.pipelines.anneal_schedule.pipeline import (
create_pipeline as create_anneal_schedule_pipeline,
)
from fromhopetoheuristics.pipelines.visualization.pipeline import (
create_pipeline as create_visualization_pipeline,
Expand All @@ -23,9 +38,37 @@ def register_pipelines() -> Dict[str, Pipeline]:
A mapping from pipeline names to ``Pipeline`` objects.
"""
pipelines = {}
pipelines["__default__"] = (

pipelines["qaoa_maxcut"] = (
create_qaoa_maxcut_pipeline() + create_anneal_schedule_pipeline()
)
pipelines["adiabatic_maxcut"] = create_adiabatic_maxcut_pipeline()
pipelines["maxcut"] = (
create_qaoa_maxcut_pipeline()
+ create_adiabatic_maxcut_pipeline()
+ create_anneal_schedule_pipeline()
)

pipelines["qubo"] = create_generation_pipeline() + create_qubo_pipeline()
pipelines["qaoa_trackrec"] = (
create_generation_pipeline()
+ create_qubo_pipeline()
+ create_qaoa_trackrec_pipeline()
+ create_anneal_schedule_pipeline()
)
pipelines["adiabatic_trackrec"] = (
create_generation_pipeline()
+ create_qubo_pipeline()
+ create_adiabatic_trackrec_pipeline()
)
pipelines["anneal_schedule"] = create_anneal_schedule_pipeline()
pipelines["trackrec"] = (
create_generation_pipeline()
+ create_science_pipeline()
+ create_qubo_pipeline()
+ create_qaoa_trackrec_pipeline()
+ create_adiabatic_trackrec_pipeline()
+ create_anneal_schedule_pipeline()
# + create_visualization_pipeline() # FIXME
)
pipelines["__default__"] = pipelines["trackrec"]
return pipelines
94 changes: 94 additions & 0 deletions src/fromhopetoheuristics/pipelines/adiabatic_maxcut/nodes.py
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 src/fromhopetoheuristics/pipelines/adiabatic_maxcut/pipeline.py
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 src/fromhopetoheuristics/pipelines/adiabatic_trackrec/nodes.py
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 src/fromhopetoheuristics/pipelines/adiabatic_trackrec/pipeline.py
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",
},
{},
),
]
)
Loading

0 comments on commit 34a5819

Please sign in to comment.