qermit.taskgraph

class qermit.taskgraph.task_graph.TaskGraph(_label='TaskGraph')[source]

The TaskGraph class stores a networkx graph where vertices are pure functions or tasks, and edges hold data. In the TaskGraph class these tasks and edges have no type restrictions, though for the run method to be succesful, the types of ports edges are attached to must match.

Parameters:

_label (str) – Name for identification of TaskGraph object.

__call__(input_wires)[source]

Call self as a function.

Return type:

Tuple[List[Union[CircuitShots, Circuit, BackendResult, ResultHandle, AnsatzCircuit, ObservableExperiment, int, float, bool, str, QubitPauliOperator, Dict[Qubit, Bit], Dict]]]

__init__(_label='TaskGraph')[source]
__repr__()[source]

Return repr(self).

__str__()[source]

Return str(self).

__weakref__

list of weak references to the object (if defined)

add_n_wires(num_wires)[source]

Adds num_wires number of edges between the input vertex and output vertex, with no type restrictions.

Parameters:

num_wires (int) – Number of edges to add between input and output vertices.

add_wire()[source]

Adds a single edge between the input vertex and output vertex.

append(task)[source]

Inserts new task to end of TaskGraph._task_graph. All in edges to Output vertex are wired as in edges to task in same port ordering (types must match). New edges added from task to Output vertex (any type permitted), ports ordered in arguments order.

Parameters:

task (Union[MitTask, TaskGraph]) – New task to be appended.

check_append_wires(task)[source]

Confirms that the number of in wires of the proposed task to append to the internal task_graph attribute matches the number of out wires to the graph.

Parameters:

task (Union[MitTask, TaskGraph]) – Wrapped pure function to append to graph

Return type:

bool

Returns:

True if append permitted

check_prepend_wires(task)[source]

Confirms that the number of out wires of the proposed task to prepend to the internal task_graph attribute matches the number of in wires to the graph.

Parameters:

task (Union[MitTask, TaskGraph]) – Wrapped pure function to prepend to graph

Return type:

bool

Returns:

True if prepend permitted

decompose_TaskGraph_nodes()[source]

For each node in self._task_graph, if node is a TaskGraph object, substitutes that node with the _task_graph structure held inside the node.

from_TaskGraph(task_graph)[source]

Returns a new TaskGraph object from another TaskGraph object.

Parameters:

task_graph (TaskGraph) – TaskGraph object to copy tasks from.

Returns:

Copied TaskGraph

get_cache()[source]
Return type:

OrderedDict[str, Tuple[MitTask, List[Union[CircuitShots, Circuit, BackendResult, ResultHandle, AnsatzCircuit, ObservableExperiment, int, float, bool, str, QubitPauliOperator, Dict[Qubit, Bit], Dict]]]]

Returns:

Dictionary holding all output data from all MitTask. This is only full after run is called with the cache argument set to True. Keys are stored in graph topological order.

get_task_graph()[source]

Return a visual representation of the DAG as a graphviz object.

Return type:

Digraph

Returns:

Representation of the DAG

property n_in_wires: int

The number of in wires to a TaskGraph object is defined as the number of out edges from the Input Vertex, as when called, a TaskGraph object calls the run method which stores input arguments as data on Input vertex output edges.

property n_out_wires: int

The number of out wires to a TaskGraph object is defined as the number of in edges to the Input Vertex, as when called, a TaskGraph object calls the run method which after running all tasks, returns the data on input edges to the Output Vertex as a tuple.

parallel(task)[source]

Adds new MitTask/TaskGraph to TaskGraph object in parallel. All task in edges wired as out edges from Input vertex. All task out_Edges wired as in edges to Output Vertex.

Parameters:

task (Union[MitTask, TaskGraph]) – New task to be added in parallel.

prepend(task)[source]

Inserts new task to the start of TaskGraph._task_graph. All out edges from the Input vertex are wired as out edges from the task in the same port ordering (types must match). New edges also added from the Input vertex to the task (any type permitted), ports ordered in arguments order.

Parameters:

task (Union[MitTask, TaskGraph]) – New task to be prepended.

run(input_wires, cache=False, characterisation={})[source]

Each task in TaskGraph is a pure function that produces output data from input data to some specification. Data is stored on edges of the internal _task_graph object. The run method first assigns each wire in the list to an output edge of the input vertex. If more wires are passed than there are edges, wire information is not assigned to some edge. If less wires are passed then there are edges, some edges will have no data and later computation will likely fail. Nodes (holding either MitTask or TaskGraph callable objects) on the graph undergo a topological sort to order them, and are then executed sequentially. To be executed, data from in edges to a task are passed arguments to the tasks _method, and data returned from method are assigned to out edges from a task. This process is repeated until all tasks are run, at which point all in edges to the output vertex wil have data on, with each data set returned in a tuple.

Parameters:
Return type:

Tuple[List[Union[CircuitShots, Circuit, BackendResult, ResultHandle, AnsatzCircuit, ObservableExperiment, int, float, bool, str, QubitPauliOperator, Dict[Qubit, Bit], Dict]]]

Returns:

Data from input edges to output vertex, assigned as wires.

property tasks: List[MitTask]

Returns a list of all tasks with both input and output ports in the TaskGraph.

view_task_graph()[source]

View the DAG.

Return type:

None