Skip to content

Commit

Permalink
Merge pull request #46 from cirKITers/instr
Browse files Browse the repository at this point in the history
Instr
  • Loading branch information
stroblme authored Nov 15, 2024
2 parents db6cacd + 1f27d02 commit 85bfc0e
Show file tree
Hide file tree
Showing 10 changed files with 374 additions and 246 deletions.
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"args": [
"--debug"
],
"justMyCode": false
}
]
}
28 changes: 22 additions & 6 deletions app/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
from layouts.app_page_layout import sidebar_top, sidebar_bottom, content
from layouts.app_page_layout import (
sidebar_top,
sidebar_bottom,
content,
DEFAULT_ANSATZ,
DEFAULT_DATA_REUPLOAD,
DEFAULT_N_LAYERS,
DEFAULT_N_QUBITS,
DEFAULT_SEED,
)

import dash
import dash_bootstrap_components as dbc
from dash import Input, Output, html, callback, State
Expand Down Expand Up @@ -65,15 +75,21 @@ def on_preference_changed(
# Give a default data dict with 0 clicks if there's no data.
data = data or {}
data["number_qubits"] = (
max(min(number_qubits, 10), 0) if number_qubits is not None else None
max(min(number_qubits, 10), 0)
if number_qubits is not None
else DEFAULT_N_QUBITS
)
data["number_layers"] = (
max(min(number_layers, 10), 0) if number_layers is not None else None
max(min(number_layers, 10), 0)
if number_layers is not None
else DEFAULT_N_LAYERS
)
data["circuit_type"] = circuit_type if circuit_type is not None else DEFAULT_ANSATZ
data["data_reupload"] = (
data_reupload if data_reupload is not None else DEFAULT_DATA_REUPLOAD
)
data["circuit_type"] = circuit_type if circuit_type is not None else "No_Ansatz"
data["data_reupload"] = data_reupload
data["tffm"] = False # tffm
data["seed"] = max(min(seed, 999), 100)
data["seed"] = max(min(seed, 999), 1000) if seed is not None else DEFAULT_SEED

return data

Expand Down
2 changes: 1 addition & 1 deletion app/layouts/app_page_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
DEFAULT_N_LAYERS = 1
DEFAULT_SEED = 100
DEFAULT_DATA_REUPLOAD = True
DEFAULT_ANSATZ = "No Ansatz"
DEFAULT_ANSATZ = "No_Ansatz"

sidebar_top = [
dcc.Store(id="main-storage", storage_type="session"),
Expand Down
6 changes: 3 additions & 3 deletions app/layouts/training_page_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
)
import dash_bootstrap_components as dbc

DEFAULT_N_STEPS = 10
DEFAULT_N_FREQS = 3
DEFAULT_N_STEPS = 50
DEFAULT_N_FREQS = 4
DEFAULT_STEPSIZE = 0.01

layout = html.Div(
Expand Down Expand Up @@ -154,7 +154,7 @@
dbc.Input(
type="number",
min=1,
max=201,
max=801,
step=1,
value=DEFAULT_N_STEPS,
id="training-steps-numeric-input",
Expand Down
2 changes: 1 addition & 1 deletion app/pages/0-main-page.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

dash.register_page(__name__, name="Home", path="/")

from layouts.main_page_layout import layout # noqa
from layouts.main_page_layout import layout # noqa: E402
108 changes: 71 additions & 37 deletions app/pages/1-training.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from layouts.training_page_layout import layout # noqa
from utils.instructor import Instructor
from layouts.training_page_layout import layout # noqa: E402
from layouts.training_page_layout import (
DEFAULT_N_STEPS,
DEFAULT_N_FREQS,
Expand All @@ -9,8 +10,8 @@
DEFAULT_N_LAYERS,
DEFAULT_SEED,
DEFAULT_DATA_REUPLOAD,
DEFAULT_ANSATZ,
)
from utils.instructor import Instructor

import dash
import numpy as np
Expand All @@ -25,9 +26,24 @@

from typing import Dict, Any, List, Optional

import logging

log = logging.getLogger(__name__)

dash.register_page(__name__, name="Training")


instructor = Instructor(
DEFAULT_N_QUBITS,
DEFAULT_N_LAYERS,
n_freqs=DEFAULT_N_FREQS,
stepsize=DEFAULT_STEPSIZE,
seed=DEFAULT_SEED,
circuit_type=DEFAULT_ANSATZ,
data_reupload=DEFAULT_DATA_REUPLOAD,
)


def reset_log() -> Dict[str, list]:
"""
Resets the training log to contain empty lists for the following keys:
Expand Down Expand Up @@ -120,9 +136,11 @@ def on_preference_changed(
"PhaseDamping": pd,
"Depolarization": dp,
},
"steps": steps,
"n_freqs": n_freqs,
"stepsize": stepsize,
"steps": steps if steps is not None and steps > 0 else DEFAULT_N_STEPS,
"n_freqs": n_freqs if n_freqs is not None and n_freqs > 0 else DEFAULT_N_FREQS,
"stepsize": (
stepsize if stepsize is not None and stepsize > 0 else DEFAULT_STEPSIZE
),
"running": state != "Reset Training",
}
page_log_training = reset_log()
Expand Down Expand Up @@ -297,12 +315,15 @@ def update_expval(
x=page_log_training["x"], y=page_log_training["y"], name="Target"
)

miny = np.min(page_log_training["y"]) if len(page_log_training["y"]) > 0 else -1
maxy = np.max(page_log_training["y"]) if len(page_log_training["y"]) > 0 else 1

fig_expval.update_layout(
title="Output",
template="simple_white",
xaxis_title="X Domain",
yaxis_title="Expectation Value",
yaxis_range=[-0.5, 0.5],
yaxis_range=[miny, maxy],
legend=dict(yanchor="top", y=0.99, xanchor="left", x=0.01),
)

Expand Down Expand Up @@ -386,48 +407,61 @@ def training(
Returns:
The updated data in the training log storage.
"""
global instructor

if page_log_training is None or page_data is None:
raise PreventUpdate()

if len(page_log_training["loss"]) > page_data["steps"]:
page_log_training = reset_log()

instructor = Instructor(
main_data["number_qubits"],
main_data["number_layers"],
n_freqs=page_data["n_freqs"],
stepsize=page_data["stepsize"],
seed=main_data["seed"],
circuit_type=main_data["circuit_type"],
data_reupload=main_data["data_reupload"],
)

page_log_training["params"], cost, pred = instructor.step(
page_log_training["params"], page_data["noise_params"]
)

page_log_training["loss"].append(cost.item())
page_log_training["y_hat"] = pred
if (
instructor.seed != main_data["seed"]
or instructor.model.n_qubits != main_data["number_qubits"]
or instructor.model.n_layers != main_data["number_layers"]
or instructor.stepsize != page_data["stepsize"]
or instructor.n_freqs != page_data["n_freqs"]
or instructor.circuit_type != main_data["circuit_type"]
or instructor.model.data_reupload != main_data["data_reupload"]
):
print("Re-init inst")
instructor = Instructor(
main_data["number_qubits"],
main_data["number_layers"],
n_freqs=page_data["n_freqs"],
stepsize=page_data["stepsize"],
seed=main_data["seed"],
circuit_type=main_data["circuit_type"],
data_reupload=main_data["data_reupload"],
)
page_log_training["x"] = instructor.x_d
page_log_training["y"] = instructor.y_d

data = instructor.calc_hist(
params=page_log_training["params"],
noise_params=page_data["noise_params"],
)

page_log_training["X"] = np.arange(
-len(data) // 2 + 1, len(data) // 2 + 1, 1
).tolist()
page_log_training["steps"] = [i for i in range(len(page_log_training["loss"]))]
page_log_training["Y"].append(data.tolist())

if main_data["number_qubits"] > 1:
instructor.model.params = page_log_training["params"]
ent_cap = instructor.meyer_wallach(
try:
data = instructor.calc_hist(
params=instructor.model.params,
noise_params=page_data["noise_params"],
)
page_log_training["X"] = np.arange(
-len(data) // 2 + 1, len(data) // 2 + 1, 1
).tolist()

page_log_training["Y"].append(data.tolist())

if main_data["number_qubits"] > 1:
instructor.model.params = instructor.model.params
ent_cap = instructor.meyer_wallach(
noise_params=page_data["noise_params"],
)

page_log_training["ent_cap"].append(ent_cap)
except Exception as e:
log.error(e)

page_log_training["ent_cap"].append(ent_cap)
cost, pred = instructor.step(page_data["noise_params"])

page_log_training["loss"].append(cost.item())
page_log_training["steps"] = [i for i in range(len(page_log_training["loss"]))]
page_log_training["y_hat"] = pred

return page_log_training
2 changes: 1 addition & 1 deletion app/pages/2-expressibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

dash.register_page(__name__, name="Expressibility")

from layouts.expressibility_page_layout import layout # noqa
from layouts.expressibility_page_layout import layout # noqa: E402


@callback(
Expand Down
Loading

0 comments on commit 85bfc0e

Please sign in to comment.