qermit.taskgraph — Qermit 0.2.0 documentation

# qermit.taskgraph

class qermit.taskgraph.task_graph.TaskGraph(_label: str = 'TaskGraph')

  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[Union[qermit.taskgraph.mittask.CircuitShots, pytket._tket.circuit.Circuit, pytket.backends.backendresult.BackendResult, pytket.backends.resulthandle.ResultHandle, qermit.taskgraph.mittask.AnsatzCircuit, qermit.taskgraph.mittask.ObservableExperiment, int, float, bool, str, pytket.utils.operators.QubitPauliOperator, Dict[pytket._tket.circuit.Qubit, pytket._tket.circuit.Bit], Dict]]) → Tuple[List[Union[qermit.taskgraph.mittask.CircuitShots, pytket._tket.circuit.Circuit, pytket.backends.backendresult.BackendResult, pytket.backends.resulthandle.ResultHandle, qermit.taskgraph.mittask.AnsatzCircuit, qermit.taskgraph.mittask.ObservableExperiment, int, float, bool, str, pytket.utils.operators.QubitPauliOperator, Dict[pytket._tket.circuit.Qubit, pytket._tket.circuit.Bit], Dict]]]

    Call self as a function.

  __init__(_label: str = 'TaskGraph') → None
  __repr__()

    Return repr(self).

  __str__()

    Return str(self).

  __weakref__

    list of weak references to the object (if defined)

  add_n_wires(num_wires: int)

    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()

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

  append(task: Union[qermit.taskgraph.mittask.MitTask, qermit.taskgraph.task_graph.TaskGraph])

    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: Union[qermit.taskgraph.mittask.MitTask, qermit.taskgraph.task_graph.TaskGraph]) → bool

    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: Union[qermit.taskgraph.mittask.MitTask, qermit.taskgraph.task_graph.TaskGraph]) → bool

    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()

    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: qermit.taskgraph.task_graph.TaskGraph)

    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[qermit.taskgraph.mittask.MitTask, List[Union[qermit.taskgraph.mittask.CircuitShots, pytket._tket.circuit.Circuit, pytket.backends.backendresult.BackendResult, pytket.backends.resulthandle.ResultHandle, qermit.taskgraph.mittask.AnsatzCircuit, qermit.taskgraph.mittask.ObservableExperiment, int, float, bool, str, pytket.utils.operators.QubitPauliOperator, Dict[pytket._tket.circuit.Qubit, pytket._tket.circuit.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.

    Return type

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

  get_task_graph() → gv.Digraph

    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: Union[qermit.taskgraph.mittask.MitTask, qermit.taskgraph.task_graph.TaskGraph])

    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: Union[qermit.taskgraph.mittask.MitTask, qermit.taskgraph.task_graph.TaskGraph])

    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[Union[qermit.taskgraph.mittask.CircuitShots, pytket._tket.circuit.Circuit, pytket.backends.backendresult.BackendResult, pytket.backends.resulthandle.ResultHandle, qermit.taskgraph.mittask.AnsatzCircuit, qermit.taskgraph.mittask.ObservableExperiment, int, float, bool, str, pytket.utils.operators.QubitPauliOperator, Dict[pytket._tket.circuit.Qubit, pytket._tket.circuit.Bit], Dict]], cache: bool = False) → Tuple[List[Union[qermit.taskgraph.mittask.CircuitShots, pytket._tket.circuit.Circuit, pytket.backends.backendresult.BackendResult, pytket.backends.resulthandle.ResultHandle, qermit.taskgraph.mittask.AnsatzCircuit, qermit.taskgraph.mittask.ObservableExperiment, int, float, bool, str, pytket.utils.operators.QubitPauliOperator, Dict[pytket._tket.circuit.Qubit, pytket._tket.circuit.Bit], Dict]]]

    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[qermit.taskgraph.mittask.MitTask]

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

  view_task_graph() → None

    View the DAG.