跳转至

MatchingDAGCircuit

QuICT.qcda.optimization.template_optimization.template_matching.Match

Match(match: List, qubit_mapping: List)

Class to store matches

Source code in QuICT/qcda/optimization/template_optimization/template_matching/matching_dag_circuit.py
def __init__(self, match: List, qubit_mapping: List):
    self.match = sorted(match)
    self.qubit_mapping = qubit_mapping

circuit_nodes cached property

circuit_nodes: Set[int]

Returns:

  • Set[int]

    Set[int]: Set of circuit nodes

template_nodes cached property

template_nodes: Set[int]

Returns:

  • Set[int]

    Set[int]: Set of tempalte nodes

QuICT.qcda.optimization.template_optimization.template_matching.MatchingDAGCircuit

MatchingDAGCircuit(circuit: Circuit)

Bases: DAGCircuit

DAG circuit class tailored for template matching algorithm.

Source code in QuICT/qcda/optimization/template_optimization/template_matching/matching_dag_circuit.py
def __init__(self, circuit: Circuit):
    self._successor_cache = {}
    self._predecessor_cache = {}
    super().__init__(circuit, node_type=MatchingDAGNode)

all_predecessors

all_predecessors(start, cache_enabled=True) -> Set[int]

Return all predecessors (direct and undirect) of start. start can be a node id (int) or many node ids (Iterable). Return values of single node query will be cached if cache_enable is True

Parameters:

  • start (int / Iterable) –

    the start node(s)

  • cache_enabled (bool, default: True ) –

    whether use cache

Returns:

  • set ( Set[int] ) –

    set of predecessors

Source code in QuICT/qcda/optimization/template_optimization/template_matching/matching_dag_circuit.py
def all_predecessors(self, start, cache_enabled=True) -> Set[int]:
    """
    Return all predecessors (direct and undirect) of `start`.
    `start` can be a node id (int) or many node ids (Iterable).
    Return values of single node query will be cached if `cache_enable` is True

    Args:
        start (int/Iterable): the start node(s)
        cache_enabled (bool): whether use cache

    Returns:
        set: set of predecessors
    """

    if not cache_enabled or isinstance(start, Iterable):
        return self._all_reachable(start, 'predecessors')
    elif isinstance(start, int):
        if start not in self._predecessor_cache:
            self._predecessor_cache[start] = set(self.get_node(start).predecessors)
            for succ in self.get_node(start).predecessors:
                self._predecessor_cache[start] |= self.all_predecessors(succ)
        return self._predecessor_cache[start]
    else:
        assert False, 'start must be int or iterable objects'

all_successors

all_successors(start, cache_enabled=True) -> Set[int]

Return all successors (direct and indirect) of start. start can be a node id (int) or many node ids (Iterable). Return values of single node query will be cached if cache_enable is True.

Parameters:

  • start (int / Iterable) –

    the start node(s)

  • cache_enabled (bool, default: True ) –

    whether use cache

Returns:

  • Set[int]

    Set[int]: set of successors

Source code in QuICT/qcda/optimization/template_optimization/template_matching/matching_dag_circuit.py
def all_successors(self, start, cache_enabled=True) -> Set[int]:
    """
    Return all successors (direct and indirect) of `start`.
    `start` can be a node id (int) or many node ids (Iterable).
    Return values of single node query will be cached if `cache_enable` is True.

    Args:
        start (int/Iterable): the start node(s)
        cache_enabled (bool): whether use cache

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

    if not cache_enabled or isinstance(start, Iterable):
        return self._all_reachable(start, 'successors')
    elif isinstance(start, int):
        if start not in self._successor_cache:
            self._successor_cache[start] = set(self.get_node(start).successors)
            for succ in self.get_node(start).successors:
                self._successor_cache[start] |= self.all_successors(succ)
        return self._successor_cache[start]
    else:
        assert False, 'start must be int or iterable objects'

get_circuit

get_circuit() -> Circuit

Output the circuit of this DAG.

Returns:

  • Circuit ( Circuit ) –

    the circuit

Source code in QuICT/qcda/optimization/template_optimization/template_matching/matching_dag_circuit.py
def get_circuit(self) -> Circuit:
    """
    Output the circuit of this DAG.

    Returns:
        Circuit: the circuit
    """

    circ = Circuit(self.width)
    for node_id in self.nodes():
        node: MatchingDAGNode = self.get_node(node_id)
        circ.append(node.gate.copy())
    return circ

init_forward_matching

init_forward_matching(node_id, other_id, s2v_enabled=False)

Initialize forward matching info.

Parameters:

  • node_id (int) –

    the start node of forward matching

  • other_id (int) –

    the start node of the other circuit to match

  • s2v_enabled (bool, default: False ) –

    whether initialize successors_to_visit

Source code in QuICT/qcda/optimization/template_optimization/template_matching/matching_dag_circuit.py
def init_forward_matching(self, node_id, other_id, s2v_enabled=False):
    """
    Initialize forward matching info.

    Args:
        node_id (int): the start node of forward matching
        other_id (int): the start node of the other circuit to match
        s2v_enabled (bool): whether initialize successors_to_visit
    """
    for nid in self.nodes():
        node = self.get_node(nid)
        if node.id == node_id:
            node.matched_with = other_id
            if s2v_enabled:
                node.successors_to_visit = sorted(node.successors.copy())
        else:
            node.matched_with = None
            if s2v_enabled:
                node.successors_to_visit = []
        node.is_blocked = False

matching_info

matching_info() -> List[NodeInfo]

Returns:

  • List[NodeInfo]

    List[NodeInfo]: matching info used by backward_match

Source code in QuICT/qcda/optimization/template_optimization/template_matching/matching_dag_circuit.py
def matching_info(self) -> List[NodeInfo]:
    """
    Returns:
        List[NodeInfo]: matching info used by backward_match
    """
    return [self.get_node(i).node_info() for i in range(self.size)]

QuICT.qcda.optimization.template_optimization.template_matching.MatchingDAGNode

MatchingDAGNode(id: int, gate: BasicGate)

Bases: DAGNode

DAG node class tailored for template matching algorithm.

Source code in QuICT/qcda/optimization/template_optimization/template_matching/matching_dag_circuit.py
def __init__(self, id: int, gate: BasicGate):
    self.successors_to_visit = []
    self.matched_with = None
    self.is_blocked = False
    super().__init__(id, gate)

compare_with

compare_with(other, qubit_mapping=None) -> bool

Compare self to the node other under mapping qubit_mapping. qubit i of self is mapped to qubit qubit_mapping[i] of other. If qubit_mapping is None, only compare gate type. The order of control qubits is ignored.

Parameters:

  • other (MatchingDAGNode) –

    the other node

  • qubit_mapping (list, default: None ) –

    mapping

Returns:

  • bool ( bool ) –

    same or not

Source code in QuICT/qcda/optimization/template_optimization/template_matching/matching_dag_circuit.py
def compare_with(self, other, qubit_mapping=None) -> bool:
    """
    Compare `self` to the node `other` under mapping `qubit_mapping`.
    qubit i of `self` is mapped to qubit qubit_mapping[i] of `other`.
    If `qubit_mapping` is None, only compare gate type.
    The order of control qubits is ignored.

    Args:
        other (MatchingDAGNode): the other node
        qubit_mapping (list): mapping

    Returns:
        bool: same or not
    """

    if self.name != other.name:
        return False
    if qubit_mapping is not None:
        t_cargs = set(map(lambda x: qubit_mapping[x], self.cargs))
        c_cargs = set(other.cargs)
        if t_cargs != c_cargs:
            return False

        for t_qubit, c_qubit in zip(self.targs, other.targs):
            if qubit_mapping[t_qubit] != c_qubit:
                return False
    return True

matchable

matchable() -> bool

Returns:

  • bool ( bool ) –

    Whether the node can be matched

Source code in QuICT/qcda/optimization/template_optimization/template_matching/matching_dag_circuit.py
def matchable(self) -> bool:
    """
    Returns:
        bool: Whether the node can be matched
    """
    return (not self.is_blocked) and (self.matched_with is None)

node_info

node_info() -> tuple
Return

tuple: (the matched node, whether is blocked)

Source code in QuICT/qcda/optimization/template_optimization/template_matching/matching_dag_circuit.py
def node_info(self) -> tuple:
    """
    Return:
        tuple: (the matched node, whether is blocked)
    """
    return NodeInfo(self.matched_with, self.is_blocked)

pop_successors_to_visit

pop_successors_to_visit() -> int

Pop the first element of successors_to_visit. If no more, return None.

Returns:

  • int ( int ) –

    first element of successors_to_visit

Source code in QuICT/qcda/optimization/template_optimization/template_matching/matching_dag_circuit.py
def pop_successors_to_visit(self) -> int:
    """
    Pop the first element of successors_to_visit. If no more, return None.

    Returns:
        int: first element of successors_to_visit
    """

    if not self.successors_to_visit:
        return None

    ret = self.successors_to_visit[0]
    self.successors_to_visit.pop(0)
    return ret