# Buggy code
def restore(self, name="default", filename=None):
    """
    restore(name='default', filename=None)

    Retore the state of the network and all included objects.

    Parameters
    ----------
    name : str, optional
        The name of the snapshot to restore, if not specified uses
        ``'default'``.
    filename : str, optional
        The name of the file from where the state should be restored. If
        not specified, it is expected that the state exist in memory
        (i.e. `Network.store` was previously called without the ``filename``
        argument).
    """
    if filename is None:
        state = self._stored_state[name]
    else:
        with open(filename, "rb") as f:
            state = pickle.load(f)[name]
    self.t_ = state.pop("0_t")
    clocks = set([obj.clock for obj in self.objects])
    restored_objects = set()
    for obj in self.objects:
        if obj.name in state:
            obj._restore_from_full_state(state[obj.name])
            restored_objects.add(obj.name)
        elif hasattr(obj, "_restore_from_full_state"):
            raise KeyError(
                (
                    "Stored state does not have a stored state for "
                    '"%s". Note that the names of all objects have '
                    "to be identical to the names when they were "
                    "stored."
                )
                % obj.name
            )
    for clock in clocks:
        clock._restore_from_full_state(state[clock.name])
    clock_names = {c.name for c in clocks}

    unnused = set(state.keys()) - restored_objects - clock_names
    if len(unnused):
        raise KeyError(
            "The stored state contains the state of the "
            "following objects which were not present in the "
            "network: %s. Note that the names of all objects "
            "have to be identical to the names when they were "
            "stored." % (", ".join(unnused))
        )


# Traceback (KeyError)
In [1]: from brian2 import *

In [2]: G = NeuronGroup(1, 'v: 1')

In [3]: store()

In [4]: restore()

In [5]: restore()
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-5-ad7bb0bcaaad> in <module>()
----> 1 restore()

/home/marcel/programming/brian2/brian2/core/magic.pyc in restore(name, filename)
411     Network.restore
412     '''
--> 413     magic_network.restore(name=name, filename=filename, level=1)
414
415

/home/marcel/programming/brian2/brian2/core/magic.pyc in restore(self, name, filename, level)
244         '''
245         self._update_magic_objects(level=level+1)
--> 246         super(MagicNetwork, self).restore(name=name, filename=filename)
247         self.objects[:] = []
248

/home/marcel/programming/brian2/brian2/core/base.pyc in device_override_decorated_function(*args, **kwds)
276                 return getattr(curdev, name)(*args, **kwds)
277             else:
--> 278                 return func(*args, **kwds)
279
280         device_override_decorated_function.__doc__ = func.__doc__

/home/marcel/programming/brian2/brian2/core/network.pyc in restore(self, name, filename)
431             with open(filename, 'rb') as f:
432                 state = pickle.load(f)[name]
--> 433         self.t_ = state.pop('0_t')
434         clocks = set([obj.clock for obj in self.objects])
435         restored_objects = set()

KeyError: '0_t'

# Fixed code
def restore(self, name="default", filename=None):
    """
    restore(name='default', filename=None)

    Retore the state of the network and all included objects.

    Parameters
    ----------
    name : str, optional
        The name of the snapshot to restore, if not specified uses
        ``'default'``.
    filename : str, optional
        The name of the file from where the state should be restored. If
        not specified, it is expected that the state exist in memory
        (i.e. `Network.store` was previously called without the ``filename``
        argument).
    """
    if filename is None:
        state = self._stored_state[name]
    else:
        with open(filename, "rb") as f:
            state = pickle.load(f)[name]
    self.t_ = state["0_t"]
    clocks = set([obj.clock for obj in self.objects])
    restored_objects = set()
    for obj in self.objects:
        if obj.name in state:
            obj._restore_from_full_state(state[obj.name])
            restored_objects.add(obj.name)
        elif hasattr(obj, "_restore_from_full_state"):
            raise KeyError(
                (
                    "Stored state does not have a stored state for "
                    '"%s". Note that the names of all objects have '
                    "to be identical to the names when they were "
                    "stored."
                )
                % obj.name
            )
    for clock in clocks:
        clock._restore_from_full_state(state[clock.name])
    clock_names = {c.name for c in clocks}

    unnused = set(state.keys()) - restored_objects - clock_names - {"0_t"}
    if len(unnused):
        raise KeyError(
            "The stored state contains the state of the "
            "following objects which were not present in the "
            "network: %s. Note that the names of all objects "
            "have to be identical to the names when they were "
            "stored." % (", ".join(unnused))
        )