Return Electron Output Values from Lattices#

To avoid causing graph construction issues and being scolded by the Covalent server post-processor, ensure that the value returned by a lattice is the output of an electron.

Many best practices with Covalent boil down to “use the decorators.” Putting as much of the working code as possible inside Covalent’s decorators (electrons and lattices) enables the Covalent server to manage execution as intended.


Covalent allows you to put business logic in a lattice outside of an electron, but that doesn’t mean you should do so. Violating this practice puts results outside of Covalent’s ability to run code on executors. Following this practice ensures that your lattices, at least in this respect, will work with future versions of Covalent.

Best Practice#

Keep computations inside electrons. Use lattices to execute sequences of electrons, not to perform computations.


Contrast the two examples below.

Example 2: Improved#

In contrast, the following code properly contains the construction of res_list in an electron, task_2_new.

import covalent as ct
import numpy as np
import random

# Technique 2:

def task_1(x):
    return x * 2

def task_2_new(x):
    res_list = random.sample(range(10, 30), x)
    return np.array(res_list)

def workflow_2(a):
    res_1 = task_1(a)
    return task_2_new(res_1)

id = ct.dispatch(workflow_2)(1)
result = ct.get_result(id, wait=True)


Lattice Result
result: [19 11]
input args: ['1']
input kwargs: {}
error: None

start_time: 2023-03-13 21:27:15.248600
end_time: 2023-03-13 21:27:15.404917

results_dir: /Users/dave/.local/share/covalent/data
dispatch_id: 642292a8-758c-42cd-a2d3-757755388207

Node Outputs
task_1(0): 2
:parameter:1(1): 1
task_2_new(2): [19 11]