Tasks¶
Tasks
are normal Python functions that serve as processing steps inside Workflows.
To turn a Python function into a task, simply decorate it with @workflow.task
:
from laboneq import workflow
@workflow.task
def add(a, b):
return a + b
When used by themselves, Tasks
behave like regular Python functions:
add(1, 2)
When a Task
is used inside a Workflow
, its input and output parameters are stored in the result of the executed Workflow
;
check out the Workflow tutorial to see how this works.
The inputs and outputs of the Task
are also saved
to disk if a FolderStore
has been configured; see the LogBook tutorial.
A Task
can be configured to never have it's inputs and outputs saved to disk during the execution of a Workflow
. This is done when the Task
is created,
using the save=False
argument:
@workflow.task(save=False)
def add(a, b):
return a + b
Note: When used inside a Workflow
, a Task
does not behave as a simple Python function anymore. Instead, it become an instance
of a Reference class,
which gets resolved to the Python function implemented by the Task
only when the Workflow
is executed. This is explained in more detail in the
tutorial on the Workflow Syntax.
Ready-Made Tasks in LabOne Q¶
When running quantum-computing experiments with LabOne Q there are usually a few higher-level steps that one has to perform:
- Build an Experiment describing the pulse-sequence description of your measurement.
- Compile the
Experiment
. - Run the
Experiment
.
LabOne Q provides ready-made Tasks
for compiling
and running
Experiments
, and for combining several Results
into one single instance.
Let's have a look at the first two:
from laboneq.workflow.tasks import compile_experiment, run_experiment
Inspect the docstrings
# docstring
compile_experiment?
# docstring
run_experiment?
Inspect the source code
# source code
compile_experiment.src
# source code
run_experiment.src
Tasks for Building an Experiment¶
The Task
for building an Experiment
pulse sequence depends on the quantum experiment that is being created.
In LabOne Q, we call these tasks create_experiment
. You can find our implementations of such tasks for many common quantum-computing
experiments in the LabOne Q Applications Library. Check them out!
Why have tasks at all?¶
Using Tasks
and Workflows
comes with many advantages:
- Being able to produce a well-organised experiment record when tasks are used in workflows; see here.
- Being able to supply options to tasks in a structured way.
- Being able to recover partial results when errors occur.
- Being able to pause and resume workflow execution.
- Being able to build complex dynamic workflows that can execute tasks conditionally or run tasks in loops.