forked from M-Labs/artiq
language/core/MPO: new parameter/channel mechanism
This commit is contained in:
parent
6509f1fd04
commit
529b83bb58
|
@ -1,31 +1,49 @@
|
||||||
import itertools
|
import itertools
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
|
from artiq.language import units
|
||||||
|
|
||||||
|
def _make_kernel_ro(value):
|
||||||
|
return isinstance(value, (int, float, str, units.Quantity))
|
||||||
|
|
||||||
class MPO:
|
class MPO:
|
||||||
channels = ""
|
|
||||||
parameters = ""
|
parameters = ""
|
||||||
implicit_core = True
|
implicit_core = True
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, mvs=None, **kwargs):
|
||||||
argnames = self.channels.split() + self.parameters.split()
|
kernel_attr_ro = []
|
||||||
|
|
||||||
|
self.mvs = mvs
|
||||||
|
for k, v in kwargs.items():
|
||||||
|
setattr(self, k, v)
|
||||||
|
if _make_kernel_ro(v):
|
||||||
|
kernel_attr_ro.append(k)
|
||||||
|
|
||||||
|
parameters = self.parameters.split()
|
||||||
if self.implicit_core:
|
if self.implicit_core:
|
||||||
argnames.insert(0, "core")
|
parameters.append("core")
|
||||||
undef_args = list(argnames)
|
for parameter in parameters:
|
||||||
|
try:
|
||||||
|
value = getattr(self, parameter)
|
||||||
|
except AttributeError:
|
||||||
|
value = self.mvs.get_missing_value(parameter)
|
||||||
|
setattr(self, parameter, value)
|
||||||
|
if _make_kernel_ro(value):
|
||||||
|
kernel_attr_ro.append(parameter)
|
||||||
|
|
||||||
|
self.kernel_attr_ro = " ".join(kernel_attr_ro)
|
||||||
|
|
||||||
if len(argnames) < len(args):
|
self.build()
|
||||||
raise TypeError("__init__() takes {} positional arguments but {} were given".format(len(argnames), len(args)))
|
|
||||||
for argname, value in itertools.chain(zip(argnames, args), kwargs.items()):
|
|
||||||
if hasattr(self, argname):
|
|
||||||
raise TypeError("__init__() got multiple values for argument '{}'".format(argname))
|
|
||||||
if argname not in argnames:
|
|
||||||
raise TypeError("__init__() got an unexpected keyword argument: '{}'".format(argname))
|
|
||||||
setattr(self, argname, value)
|
|
||||||
undef_args.remove(argname)
|
|
||||||
if undef_args:
|
|
||||||
raise TypeError("__init__() missing {} argument(s): ".format(len(undef_args),
|
|
||||||
", ".join(["'"+s+"'" for s in undef_args])))
|
|
||||||
|
|
||||||
self.kernel_attr_ro = self.parameters
|
def get_missing_value(self, parameter):
|
||||||
|
try:
|
||||||
|
return getattr(self, parameter)
|
||||||
|
except AttributeError:
|
||||||
|
return self.mvs.get_missing_value(parameter)
|
||||||
|
|
||||||
|
def build(self):
|
||||||
|
""" Overload this function to add sub-experiments"""
|
||||||
|
pass
|
||||||
|
|
||||||
KernelFunctionInfo = namedtuple("KernelFunctionInfo", "core_name k_function")
|
KernelFunctionInfo = namedtuple("KernelFunctionInfo", "core_name k_function")
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,8 @@ from artiq.language.units import *
|
||||||
from artiq.language.core import *
|
from artiq.language.core import *
|
||||||
|
|
||||||
class AluminumSpectroscopy(MPO):
|
class AluminumSpectroscopy(MPO):
|
||||||
channels = "mains_sync laser_cooling spectroscopy spectroscopy_b state_detection pmt"
|
parameters = "mains_sync laser_cooling spectroscopy spectroscopy_b state_detection pmt \
|
||||||
parameters = "spectroscopy_freq photon_limit_low photon_limit_high"
|
spectroscopy_freq photon_limit_low photon_limit_high"
|
||||||
|
|
||||||
@kernel
|
@kernel
|
||||||
def run(self):
|
def run(self):
|
||||||
|
|
|
@ -4,7 +4,7 @@ from artiq.language.core import *
|
||||||
my_range = range
|
my_range = range
|
||||||
|
|
||||||
class CompilerTest(MPO):
|
class CompilerTest(MPO):
|
||||||
channels = "a b A B"
|
parameters = "a b A B"
|
||||||
|
|
||||||
def print_done(self):
|
def print_done(self):
|
||||||
print("Done!")
|
print("Done!")
|
||||||
|
|
|
@ -2,7 +2,7 @@ from artiq.language.core import MPO, kernel
|
||||||
from artiq.devices import corecom_serial, runtime, core, gpio_core
|
from artiq.devices import corecom_serial, runtime, core, gpio_core
|
||||||
|
|
||||||
class CompilerTest(MPO):
|
class CompilerTest(MPO):
|
||||||
channels = "led"
|
parameters = "led"
|
||||||
|
|
||||||
def output(self, n):
|
def output(self, n):
|
||||||
print("Received: "+str(n))
|
print("Received: "+str(n))
|
||||||
|
|
|
@ -2,7 +2,7 @@ from artiq.language.units import *
|
||||||
from artiq.language.core import *
|
from artiq.language.core import *
|
||||||
|
|
||||||
class SimpleSimulation(MPO):
|
class SimpleSimulation(MPO):
|
||||||
channels = "a b c d"
|
parameters = "a b c d"
|
||||||
|
|
||||||
@kernel
|
@kernel
|
||||||
def run(self):
|
def run(self):
|
||||||
|
|
Loading…
Reference in New Issue