Skip to content

Commit

Permalink
Merge pull request #44 from cirKITers/adjustable-frequencies
Browse files Browse the repository at this point in the history
Adjustable frequencies
  • Loading branch information
majafranz authored Nov 12, 2024
2 parents a55555b + 967d8fc commit 6cb31e3
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 8 deletions.
1 change: 1 addition & 0 deletions app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ def on_preference_changed(
logging.basicConfig(
level=logging.ERROR, format="%(levelname)s:%(name)s:%(message)s"
)
logging.getLogger("werkzeug").setLevel(logging.ERROR)

logging.info("(Re-)launching Application..")

Expand Down
75 changes: 72 additions & 3 deletions app/pages/1-training.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
dash.register_page(__name__, name="Training")

DEFAULT_N_STEPS = 10
DEFAULT_N_FREQS = 3
DEFAULT_STEPSIZE = 0.01

layout = html.Div(
[
Expand Down Expand Up @@ -123,6 +125,35 @@
),
dbc.Row(
[
dbc.Col(
[
html.Div(
[
dbc.Label("# of Freqs.:"),
],
style={
"display": "inline-block",
},
),
html.Div(
[
dbc.Input(
type="number",
min=1,
max=11,
step=1,
value=DEFAULT_N_FREQS,
id="training-freqs-numeric-input",
),
],
style={
"width": "4vw",
"display": "inline-block",
"padding-left": "8px",
},
),
]
),
dbc.Col(
[
html.Div(
Expand All @@ -138,16 +169,45 @@
dbc.Input(
type="number",
min=1,
max=101,
max=201,
step=1,
value=DEFAULT_N_STEPS,
id="training-steps-numeric-input",
),
],
style={
"width": "4vw",
"display": "inline-block",
"padding-left": "8px",
},
),
]
),
dbc.Col(
[
html.Div(
[
dbc.Label("Stepsize: "),
],
style={
"display": "inline-block",
},
),
html.Div(
[
dbc.Input(
type="number",
min=0.001,
max=0.1,
step=0.001,
value=DEFAULT_STEPSIZE,
id="training-stepsize-numeric-input",
),
],
style={
"width": "5vw",
"display": "inline-block",
"padding-left": "20px",
"padding-left": "8px",
},
),
]
Expand Down Expand Up @@ -264,7 +324,9 @@ def reset_log() -> Dict[str, list]:
Input("training-amplitude-damping-prob-slider", "value"),
Input("training-phase-damping-prob-slider", "value"),
Input("training-depolarization-prob-slider", "value"),
Input("training-freqs-numeric-input", "value"),
Input("training-steps-numeric-input", "value"),
Input("training-stepsize-numeric-input", "value"),
Input("training-start-button", "n_clicks"),
],
State("training-start-button", "children"),
Expand All @@ -277,7 +339,9 @@ def on_preference_changed(
ad: float,
pd: float,
dp: float,
n_freqs: int,
steps: int,
stepsize: int,
n: int,
state: str,
) -> list:
Expand All @@ -291,6 +355,7 @@ def on_preference_changed(
ad: Amplitude damping probability from the slider input.
pd: Phase damping probability from the slider input.
dp: Depolarization probability from the slider input.
n_freqs: Number of frequencies from the numeric input.
steps: Number of training steps from the numeric input.
n: Number of clicks on the start button.
state: The current text on the training start button.
Expand All @@ -310,6 +375,8 @@ def on_preference_changed(
"Depolarization": dp,
},
"steps": steps,
"n_freqs": n_freqs,
"stepsize": stepsize,
"running": state != "Reset Training",
}
page_log_training = reset_log()
Expand Down Expand Up @@ -488,7 +555,7 @@ def update_expval(
template="simple_white",
xaxis_title="X Domain",
yaxis_title="Expectation Value",
yaxis_range=[-1, 1],
yaxis_range=[-0.5, 0.5],
legend=dict(yanchor="top", y=0.99, xanchor="left", x=0.01),
)

Expand Down Expand Up @@ -581,6 +648,8 @@ def training(
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"],
Expand Down
13 changes: 8 additions & 5 deletions app/utils/instructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ def __init__(
self,
n_qubits: int,
n_layers: int,
seed: int = 100,
circuit_type: int = 19,
n_freqs: int = 3,
stepsize: float = 0.01,
seed: int = 1000,
circuit_type: str = "Circuit_19",
data_reupload: bool = True,
**kwargs,
) -> None:
Expand All @@ -41,19 +43,20 @@ def __init__(
**kwargs,
)
self.x_domain = [-1 * np.pi, 1 * np.pi] # [-4 * np.pi, 4 * np.pi]
omega_d = np.array([1, 2, 3])
omega_d = np.array([i for i in range(1, n_freqs + 1)])

n_d = int(np.ceil(2 * np.max(np.abs(self.x_domain)) * np.max(omega_d)))
self.x_d = np.linspace(
self.x_domain[0], self.x_domain[1], n_d, requires_grad=False
)
amplitude = 0.5

def y_fct(x):
return 1 / np.linalg.norm(omega_d) * np.sum(np.cos(omega_d * x))
return 1 / np.linalg.norm(omega_d) * np.sum(amplitude * np.cos(omega_d * x))

self.y_d = np.array([y_fct(x) for x in self.x_d], requires_grad=False)

self.opt = qml.AdamOptimizer(stepsize=0.01)
self.opt = qml.AdamOptimizer(stepsize=stepsize)

def calc_hist(
self,
Expand Down

0 comments on commit 6cb31e3

Please sign in to comment.