lightlab.laboratory.virtualization module

Provides a framework for making virtual instruments that present the same interface and simulated behavior as the real ones. Allows a similar thing with functions, methods, and experiments.

Dualization is a way of tying together a real instrument with its virtual counterpart. This is a powerful way to test procedures in a virtual environment before flipping the switch to reality. This is documented in tests.test_virtualization.

virtualOnly

bool – If virtualOnly is True, any “with” statements using asReal will just skip the block. When not using a context manager (i.e. exp.virtual = False), it will eventually produce a VirtualizationError.

Summary

Exceptions:

VirtualizationError

Classes:

DualFunction This class implements a descriptor for a function whose behavior depends on an instance’s variable.
DualInstrument Holds a real instrument and a virtual instrument.
DualMethod This differs from DualFunction because it exists outside of the object instance.
VirtualInstrument Just a placeholder for future functionality
Virtualizable Virtualizable means that it can switch between two states, usually corresponding to a real-life situation and a virtual/simulated situation.

Data:

virtualOnly bool(x) -> bool

Reference

class Virtualizable[source]

Bases: object

Virtualizable means that it can switch between two states, usually corresponding to a real-life situation and a virtual/simulated situation.

The attribute synced refers to other Virtualizables whose states will be synchronized with this one

synced = None
synchronize(*newVirtualizables)[source]

Adds another object that this one will put in the same virtual state as itself.

Parameters:newVirtualizables (*args) – Other virtualizable things
virtual

Returns the virtual state of this object

asVirtual()[source]

Temporarily puts this and synchronized in a virtual state. The state is reset at the end of the with block.

Example usage:

exp = Virtualizable()
with exp.asVirtual():
    print(exp.virtual)  # prints True
print(exp.virtual)  # VirtualizationError
asReal()[source]

Temporarily puts this and synchronized in a virtual state. The state is reset at the end of the with block.

If virtualOnly is True, it will skip the block without error

Example usage:

exp = Virtualizable()
with exp.asVirtual():
    print(exp.virtual)  # prints False
print(exp.virtual)  # VirtualizationError
class VirtualInstrument[source]

Bases: object

Just a placeholder for future functionality

asVirtual()[source]

do nothing

class DualInstrument(real_obj=None, virt_obj=None)[source]

Bases: lightlab.laboratory.virtualization.Virtualizable

Holds a real instrument and a virtual instrument. Feeds through __getattribute__ and __setattr__: very powerful. It basically appears as one or the other instrument, as determined by whether it is in virtual or real mode.

This is especially useful if you have an instrument stored in the JSON labstate, and would then like to virtualize it in your notebook. In that case, it does not reinitialize the driver.

This is documented in tests.test_virtualization.

isinstance() and .__class__ will tell you the underlying instrument type type() will give you the DualInstrument subclass:

dual = DualInstrument(realOne, virtOne)
with dual.asReal():
    isinstance(dual, type(realOne))  # True
    dual.meth is realOne.meth  # True
isinstance(dual, type(realOne))  # False
Parameters:
real_obj = None
virt_obj = None
virtual

Returns the virtual state of this object

class DualFunction(virtual_function=None, hardware_function=None, doc=None)[source]

Bases: object

This class implements a descriptor for a function whose behavior depends on an instance’s variable. This was inspired by core python’s property descriptor.

Example usage:

@DualFunction
def measure(self, *args, **kwargs):
    # use a model to simulate outputs based on args and kwargs and self.
    return simulated_output

@measure.hardware
def measure(self, *args, **kwargs):
    # collect data from hardware using args and kwargs and self.
    return output

The “virtual” function will be called if self.virtual equals True, otherwise the hardware decorated function will be called instead.

hardware(func)[source]
virtual(func)[source]
class DualMethod(dualInstrument=None, virtual_function=None, hardware_function=None, doc=None)[source]

Bases: object

This differs from DualFunction because it exists outside of the object instance. Instead it takes the object when initializing.

It uses __call__ instead of __get__ because it is its own object

Todo

The naming for DualFunction and DualMethod are backwards. Will break notebooks when changed.

exception VirtualizationError[source]

Bases: RuntimeError