m-labs / artiq

A leading-edge control system for quantum information experiments
https://m-labs.hk/artiq
GNU Lesser General Public License v3.0
422 stars 193 forks source link

Unexpected behavior with dynamic function calls #1451

Open KaifengC opened 4 years ago

KaifengC commented 4 years ago

Bug Report

One-Line Summary

I tried to put some function names in a list and let the kernel to call those functions by traversing this list. And get some weird behavior.

Issue Details

Steps to Reproduce

Run the following code:

class DynamicFunction(EnvExperiment):
    def build(self):
        self.core = self.get_device('core')
        self.ttl_2 = self.get_device('ttl2')
        self.ttl_3 = self.get_device('ttl3')

        self.action_list = []

    def prepare(self):
        self.action_list.append(getattr(self, 'pulse_1'))
        self.action_list.append(getattr(self, 'pulse_2'))
        # if replace 'pulse_2' with the following `pulse_3`, it works
        # self.action_list.append(getattr(self, 'pulse_3'))

    @kernel
    def run(self):
        self.init_device()
        #self.run_static()  # this one works
        self.run_dynamic()  # this does not, see text.

    @kernel
    def run_static(self):
        self.pulse_1()
        self.pulse_2()

    @kernel
    def run_dynamic(self):
        for action in self.action_list:
            action()

    @kernel
    def pulse_1(self):
        self.ttl_2.pulse(10 * us)
        delay(10 * us)

    @kernel
    def pulse_2(self):
        with parallel:
            self.ttl_2.pulse(10 * us)
            self.ttl_3.pulse(15 * us)
            delay(15 * us)

    @kernel
    def pulse_3(self):
        self.ttl_3.pulse(10 * us)
        delay(10 * us)

run_static function works nicely but run_dynamic does not work for some functions. and the debug information can't give me any help:

root:While compiling <repository>\NewFeatureTest\Dynamic_load_bug.py
<repository>\NewFeatureTest\Dynamic_load_bug.py:44:5-47:23: fatal: delay delay(15998 mu) was inferred for this function, but its delay is already constrained externally to delay(?)
    @kernel
    ^^^^^^^

I guess it has something to do with the compiler?

Expected Behavior

run_dynamic should have the same behavior with run_static.

Your System (omit irrelevant parts)

sbourdeauducq commented 4 years ago

Yep, by legacy code in the compiler that should be removed (we don't do compile-time delay analysis anymore).