跳转至

DagCircuit

QuICT.core.circuit.dag_circuit.DAGNode

DAGNode(id: int, gate: BasicGate, successors: list = None, predecessors: list = None)

The node in DAG Circuit, represent a quantum gate in circuit.

Parameters:

  • id (int) –

    The unique identity, usually be the quantum gates' index

  • gate (BasicGate) –

    The quantum gate

  • successors (list, default: None ) –

    The successors for nodes. Defaults to [].

  • predecessors (list, default: None ) –

    The predecessors for nodes. Defaults to [].

Source code in QuICT/core/circuit/dag_circuit.py
def __init__(self, id: int, gate: BasicGate, successors: list = None, predecessors: list = None):
    self._id = id
    self._gate = gate
    self._name = gate.qasm_name
    self._cargs = gate.cargs
    self._targs = gate.targs
    self._qargs = gate.cargs + gate.targs
    self._type = gate.type
    self._successors = [] if successors is None else successors
    self._predecessors = [] if predecessors is None else predecessors

append_pred

append_pred(u)

Append a node id to successors.

Parameters:

  • u(int)

    the node

Source code in QuICT/core/circuit/dag_circuit.py
def append_pred(self, u):
    """
    Append a node id to successors.

    Args:
        u(int): the node
    """
    self._predecessors.append(u)

append_succ

append_succ(u)

Append a node id to predecessors.

Parameters:

  • u(int)

    the node

Source code in QuICT/core/circuit/dag_circuit.py
def append_succ(self, u):
    """
    Append a node id to predecessors.

    Args:
        u(int): the node
    """

    self._successors.append(u)

QuICT.core.circuit.DAGCircuit

DAGCircuit(circuit, node_type=DAGNode)

The DAG Circuit using networkx.DiGraph()

The nodes in the graph represented the quantum gates, and the edges means the two quantum gates is non-commutation. In other words, a directed edge between node A with quantum gate GA and node B with quantum gate GB, the quantum gate GA does not commute with GB.

The nodes in the graph have the following attributes: 'name', 'gate', 'cargs', 'targs', 'qargs', 'successors', 'predecessors'.

Reference

[1] Iten, R., Moyard, R., Metger, T., Sutter, D. and Woerner, S., 2020. Exact and practical pattern matching for quantum circuit optimization. https://arxiv.org/abs/1909.05270

Parameters:

  • circuit (Circuit) –

    The quantum circuit.

Source code in QuICT/core/circuit/dag_circuit.py
def __init__(self, circuit, node_type=DAGNode):
    self._circuit = circuit
    self._name = f"DAG_{self._circuit.name}"
    self._size = self._circuit.size()
    self._width = self._circuit.width()
    self._graph = nx.DiGraph()
    self._node_type = node_type
    # Build DAG Circuit
    self._to_dag_circuit()

__getitem__

__getitem__(item)

to fit the slice operator, overloaded this function.

get a smaller qureg/qubit from this circuit

Parameters:

  • item(int/slice)

    slice passed in.

Return: Qubit/Qureg: the result or slice

Source code in QuICT/core/circuit/dag_circuit.py
def __getitem__(self, item):
    """ to fit the slice operator, overloaded this function.

    get a smaller qureg/qubit from this circuit

    Args:
        item(int/slice): slice passed in.
    Return:
        Qubit/Qureg: the result or slice
    """
    return self.nodes()[item]

add_edge

add_edge(u, v)

Add a directed edge to DAG.

Parameters:

  • u(int)

    start node

  • v(int)

    end node

Source code in QuICT/core/circuit/dag_circuit.py
def add_edge(self, u, v):
    """
    Add a directed edge to DAG.

    Args:
        u(int): start node
        v(int): end node
    """

    self._graph.add_edge(u, v)
    self.get_node(u).append_succ(v)
    self.get_node(v).append_pred(u)

add_node

add_node(node: DAGNode)

Add a node into DAG.

Parameters:

Source code in QuICT/core/circuit/dag_circuit.py
def add_node(self, node: DAGNode):
    """ Add a node into DAG.

    Args:
        node (DAGNode): The DAG Node
    """
    assert isinstance(node, DAGNode), TypeError("DagCircuit.add_node.node", "DAGNode", type(node))
    if not self._graph.has_node(node.id):
        self._graph.add_node(node.id, node=node)

all_predecessors

all_predecessors(start) -> Set[int]

Return all predecessors (direct and indirect) of start. start can be a node id (int) or many node ids (Iterable).

Parameters:

  • start(int/Iterable)

    the start node(s)

Returns:

  • set ( Set[int] ) –

    set of predecessors

Source code in QuICT/core/circuit/dag_circuit.py
def all_predecessors(self, start) -> Set[int]:
    """
    Return all predecessors (direct and indirect) of `start`.
    `start` can be a node id (int) or many node ids (Iterable).

    Args:
        start(int/Iterable): the start node(s)

    Returns:
        set: set of predecessors
    """

    return self._all_reachable(start, 'predecessors')

all_successors

all_successors(start) -> Set[int]

Return all successors (direct and indirect) of start.

Parameters:

  • start(int/Iterable)

    the start node(s)

Returns:

  • Set[int]

    Set[int]: set of successors

Source code in QuICT/core/circuit/dag_circuit.py
def all_successors(self, start) -> Set[int]:
    """
    Return all successors (direct and indirect) of `start`.

    Args:
        start(int/Iterable): the start node(s)

    Returns:
        Set[int]: set of successors
    """

    return self._all_reachable(start, 'successors')

draw

draw(layout=nx.shell_layout)

Draw a DAG circuit, save as jpg file.

Parameters:

  • layout (layout, default: shell_layout ) –

    The networkx.layout. Defaults to nx.shell_layout.

Source code in QuICT/core/circuit/dag_circuit.py
def draw(self, layout=nx.shell_layout):
    """ Draw a DAG circuit, save as jpg file.

    Args:
        layout (layout, optional): The networkx.layout. Defaults to nx.shell_layout.
    """
    graph_name = f"{self.name}.jpg"
    plt.figure()
    nx.draw(self._graph, pos=layout(self._graph), with_labels=True)
    plt.savefig(graph_name)

edges

edges() -> list

Get all edges in DAG.

Returns:

  • list ( list ) –

    The list of edges of DAG.

Source code in QuICT/core/circuit/dag_circuit.py
def edges(self) -> list:
    """ Get all edges in DAG.

    Returns:
        list: The list of edges of DAG.
    """
    return list(self._graph.edges)

get_node

get_node(node_id: int)

Get DAG node's data.

Parameters:

  • node_id (int) –

    the unique identity for node

Source code in QuICT/core/circuit/dag_circuit.py
def get_node(self, node_id: int):
    """ Get DAG node's data.

    Args:
        node_id (int): the unique identity for node
    """
    return self._graph.nodes[node_id]["node"]

in_edges

in_edges(node_id: int)

Get all in-edges of node with given node id.

Parameters:

  • node_id (int) –

    the unique identity for node

Source code in QuICT/core/circuit/dag_circuit.py
def in_edges(self, node_id: int):
    """ Get all in-edges of node with given node id.

    Args:
        node_id (int): the unique identity for node
    """
    return self._graph.in_edges(node_id)

nodes

nodes() -> list

Get all nodes in DAG.

Returns:

  • list ( list ) –

    The list of nodes

Source code in QuICT/core/circuit/dag_circuit.py
def nodes(self) -> list:
    """ Get all nodes in DAG.

    Returns:
        list: The list of nodes
    """
    return list(self._graph.nodes)

out_edges

out_edges(node_id: int)

Get all out-edges of node with given node id.

Parameters:

  • node_id (int) –

    the unique identity for node

Source code in QuICT/core/circuit/dag_circuit.py
def out_edges(self, node_id: int):
    """ Get all out-edges of node with given node id.

    Args:
        node_id (int): the unique identity for node
    """
    return self._graph.out_edges(node_id)