Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies = [
"pydantic",
"nicegui",
"flask",
"oqd-core",
]


Expand Down
66 changes: 38 additions & 28 deletions src/oqd_teaching_demo/analog.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
# Copyright 2024 OPEN QUANTUM DESIGN
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Copyright 2024-2025 Open Quantum Design

from src.base import TypeReflectBaseModel
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

class MathExpr(TypeReflectBaseModel):
pass
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Literal
from pydantic import BaseModel, Field

class Linear(MathExpr):
max: float = 0.0
min: float = 1.0
duration: float = 1.0 # seconds

class AmplitudeEnvelope(BaseModel):
kind: Literal["constant", "linear", "sinusoidal", "expression"] = "constant"
# constant
value: float = 1.0
# linear
start: float = 0.0
end: float = 1.0
# sinusoidal
frequency: float = 1.0
phase: float = 0.0
# expression (free-text math expression using variable 't')
expression: str = "1.0"

class ExponentialDecay(MathExpr):
max: float = 0.0
min: float = 1.0
duration: float = 1.0 # seconds

class HamiltonianTerm(BaseModel):
pauli: Literal["I", "X", "Y", "Z"] = "X"
ion: int = 0
coefficient: float = 1.0
envelope: AmplitudeEnvelope = Field(default_factory=AmplitudeEnvelope)

class Sinusoidal(MathExpr):
max: float = 0.0
min: float = 1.0
duration: float = 1.0 # seconds

class EvolveStep(BaseModel):
terms: list[HamiltonianTerm] = Field(default_factory=lambda: [HamiltonianTerm()])
duration: float = 1.0


class AnalogProgramSpec(BaseModel):
n_ions: int = 4
steps: list[EvolveStep] = Field(default_factory=lambda: [EvolveStep()])
81 changes: 81 additions & 0 deletions src/oqd_teaching_demo/analog_compiler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Copyright 2024-2025 Open Quantum Design

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import math

import numpy as np

from oqd_teaching_demo.analog import AnalogProgramSpec, AmplitudeEnvelope
from oqd_teaching_demo.program import Program

SAFE_MATH_NAMESPACE = {
"sin": math.sin,
"cos": math.cos,
"tan": math.tan,
"exp": math.exp,
"log": math.log,
"sqrt": math.sqrt,
"abs": abs,
"pi": math.pi,
"e": math.e,
}


def evaluate_envelope(envelope: AmplitudeEnvelope, t: float, duration: float) -> float:
if envelope.kind == "constant":
return envelope.value

elif envelope.kind == "linear":
if duration > 0:
frac = t / duration
else:
frac = 1.0
return envelope.start + (envelope.end - envelope.start) * frac

elif envelope.kind == "sinusoidal":
return math.sin(envelope.frequency * t + envelope.phase)

elif envelope.kind == "expression":
namespace = {**SAFE_MATH_NAMESPACE, "t": t}
try:
return float(eval(envelope.expression, {"__builtins__": {}}, namespace))
except Exception:
return 0.0

return 1.0


def compile_to_program(spec: AnalogProgramSpec, dt: float = 0.1) -> Program:
all_intensities = []

for step in spec.steps:
n_steps = max(1, int(step.duration / dt))
t_array = np.linspace(0, step.duration, n_steps, endpoint=False)

for t in t_array:
ion_intensity = [0.0] * spec.n_ions
for term in step.terms:
if term.pauli == "I":
continue
amp = abs(term.coefficient * evaluate_envelope(term.envelope, t, step.duration))
idx = min(term.ion, spec.n_ions - 1)
ion_intensity[idx] += amp

ion_intensity = [max(0.0, min(1.0, v)) for v in ion_intensity]
all_intensities.append(ion_intensity)

if not all_intensities:
all_intensities = [[0.0] * spec.n_ions]

return Program(red_lasers_intensity=all_intensities, dt=dt)
Loading