From eb4054d97696d6d017fbcf0d80be1da9bbbfd0ae Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Wed, 13 Aug 2014 18:16:40 +0800 Subject: [PATCH] sim: use MPO --- artiq/sim/devices.py | 34 +++++++++++++++------------------- examples/simple_simulation.py | 11 ++++++----- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/artiq/sim/devices.py b/artiq/sim/devices.py index 65e814eb9..cb0e49e79 100644 --- a/artiq/sim/devices.py +++ b/artiq/sim/devices.py @@ -1,5 +1,6 @@ from random import Random +from artiq.language.core import MPO, delay from artiq.language import units from artiq.sim import time @@ -7,37 +8,32 @@ class Core: def run(self, k_function, k_args, k_kwargs): return k_function(*k_args, **k_kwargs) -class Input: - def __init__(self, name, prng_seed=None, wait_max=20, count_max=100, wait_min=0, count_min=0): - self.name = name - self.wait_min = wait_min - self.wait_max = wait_max - self.count_min = count_min - self.count_max = count_max - self.prng = Random(prng_seed) +class Input(MPO): + parameters = "name" + + def build(self): + self.prng = Random() def wait_edge(self): - duration = self.prng.randrange(self.wait_min, self.wait_max)*units.ms + duration = self.prng.randrange(0, 20)*units.ms time.manager.event(("wait_edge", self.name, duration)) - time.manager.take_time(duration) + delay(duration) def count_gate(self, duration): - result = self.prng.randrange(self.count_min, self.count_max) + result = self.prng.randrange(0, 100) time.manager.event(("count_gate", self.name, duration, result)) - time.manager.take_time(duration) + delay(duration) return result -class WaveOutput: - def __init__(self, name): - self.name = name +class WaveOutput(MPO): + parameters = "name" def pulse(self, frequency, duration): time.manager.event(("pulse", self.name, frequency, duration)) - time.manager.take_time(duration) + delay(duration) -class VoltageOutput: - def __init__(self, name): - self.name = name +class VoltageOutput(MPO): + parameters = "name" def set(self, value): time.manager.event(("set_voltage", self.name, value)) diff --git a/examples/simple_simulation.py b/examples/simple_simulation.py index d44a93af6..d60b26f5d 100644 --- a/examples/simple_simulation.py +++ b/examples/simple_simulation.py @@ -18,12 +18,13 @@ if __name__ == "__main__": from artiq.sim import devices as sd from artiq.sim import time + coredev = sd.Core() exp = SimpleSimulation( - core=sd.Core(), - a=sd.WaveOutput("a"), - b=sd.WaveOutput("b"), - c=sd.WaveOutput("c"), - d=sd.WaveOutput("d"), + core=coredev, + a=sd.WaveOutput(core=coredev, name="a"), + b=sd.WaveOutput(core=coredev, name="b"), + c=sd.WaveOutput(core=coredev, name="c"), + d=sd.WaveOutput(core=coredev, name="d"), ) exp.run() print(time.manager.format_timeline())