qermit.taskgraph#

class qermit.taskgraph.task_graph.TaskGraph(_label: str = '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: List[CircuitShots | Circuit | BackendResult | ResultHandle | AnsatzCircuit | ObservableExperiment | int | float | bool | str | QubitPauliOperator | Dict[Qubit, Bit] | Dict]) Tuple[List[CircuitShots | Circuit | BackendResult | ResultHandle | AnsatzCircuit | ObservableExperiment | int | float | bool | str | QubitPauliOperator | Dict[Qubit, Bit] | Dict]][source]#

Call self as a function.

__init__(_label: str = 'TaskGraph') None[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: int)[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: MitTask | TaskGraph)[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 (MitTask) – New task to be appended.

check_append_wires(task: MitTask | TaskGraph) bool[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

Returns:

True if append permitted

Return type:

bool

check_prepend_wires(task: MitTask | TaskGraph) bool[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

Returns:

True if prepend permitted

Return type:

bool

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: TaskGraph)[source]#

Returns a new TaskGraph object from another TaskGraph object.

Parameters:

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

Returns:

Copied TaskGraph

Return type:

TaskGraph

get_cache() OrderedDict[str, Tuple[MitTask, List[CircuitShots | Circuit | BackendResult | ResultHandle | AnsatzCircuit | ObservableExperiment | int | float | bool | str | QubitPauliOperator | Dict[Qubit, Bit] | Dict]]][source]#
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.

Return type:

Dict[str, Tuple[MitTask, List[Wire]]]

get_task_graph() gv.Digraph[source]#

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

Returns:

Representation of the DAG

Return type:

graphviz.DiGraph

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: MitTask | TaskGraph)[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 (MitTask) – New task to be added in parallel.

prepend(task: MitTask | TaskGraph)[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 (MitTask) – New task to be prepended.

run(input_wires: List[CircuitShots | Circuit | BackendResult | ResultHandle | AnsatzCircuit | ObservableExperiment | int | float | bool | str | QubitPauliOperator | Dict[Qubit, Bit] | Dict], cache: bool = False, characterisation: dict = {}) Tuple[List[CircuitShots | Circuit | BackendResult | ResultHandle | AnsatzCircuit | ObservableExperiment | int | float | bool | str | QubitPauliOperator | Dict[Qubit, Bit] | Dict]][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:
  • input_wires (List[Wire]) – Each Wire holds information assigned as data to an output edge from the input vertex of the _task_graph.

  • cache (bool) – If True each Tasks output data is stored in an OrderedDict with the Task._label attribute as its key.

Returns:

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

Return type:

Tuple[List[Wire]]

property tasks: List[MitTask]#

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

view_task_graph() None[source]#

View the DAG.