qiskit/mapper/_mapping.py
def direction_mapper(circuit_graph, coupling_graph):
    """Change the direction of CNOT gates to conform to CouplingGraph.

    circuit_graph = input DAGCircuit
    coupling_graph = corresponding CouplingGraph

    Adds "h" to the circuit basis.

    Returns a DAGCircuit object containing a circuit equivalent to
    circuit_graph but with CNOT gate directions matching the edges
    of coupling_graph. Raises an exception if the circuit_graph
    does not conform to the coupling_graph.
    """
    if "cx" not in circuit_graph.basis:
        return circuit_graph
    if circuit_graph.basis["cx"] != (2, 0, 0):
        raise MapperError("cx gate has unexpected signature %s" %
                          circuit_graph.basis["cx"])

    flipped_cx_circuit = DAGCircuit()
    flipped_cx_circuit.add_qreg('q', 2)
    flipped_cx_circuit.add_basis_element("CX", 2)
    flipped_cx_circuit.add_basis_element("U", 1, 0, 3)
    flipped_cx_circuit.add_basis_element("cx", 2)
    flipped_cx_circuit.add_basis_element("u2", 1, 0, 2)
    flipped_cx_circuit.add_basis_element("h", 1)
    flipped_cx_circuit.add_gate_data("cx", cx_data)
    flipped_cx_circuit.add_gate_data("u2", u2_data)
    flipped_cx_circuit.add_gate_data("h", h_data)
    flipped_cx_circuit.apply_operation_back("h", [("q", 0)])
    flipped_cx_circuit.apply_operation_back("h", [("q", 1)])
    flipped_cx_circuit.apply_operation_back("cx", [("q", 1), ("q", 0)])
    flipped_cx_circuit.apply_operation_back("h", [("q", 0)])
    flipped_cx_circuit.apply_operation_back("h", [("q", 1)])

    cg_edges = coupling_graph.get_edges()
    for cx_node in circuit_graph.get_named_nodes("cx"):
        nd = circuit_graph.multi_graph.node[cx_node]
        cxedge = tuple(nd["qargs"])
        if cxedge in cg_edges:
            logger.debug("cx %s[%d], %s[%d] -- OK",
                         cxedge[0][0], cxedge[0][1],
                         cxedge[1][0], cxedge[1][1])
            continue
        elif (cxedge[1], cxedge[0]) in cg_edges:
            circuit_graph.substitute_circuit_one(cx_node,
                                                 flipped_cx_circuit,
                                                 wires=[("q", 0), ("q", 1)])
            logger.debug("cx %s[%d], %s[%d] -FLIP",
                         cxedge[0][0], cxedge[0][1],
                         cxedge[1][0], cxedge[1][1])
        else:
            raise MapperError("circuit incompatible with CouplingGraph: "
                              "cx on %s" % pprint.pformat(cxedge))
    return circuit_graph
Change the direction of CNOT gates to conform to CouplingGraph.

    circuit_graph = input DAGCircuit
    coupling_graph = corresponding CouplingGraph

    Adds "h" to the circuit basis.

    Returns a DAGCircuit object containing a circuit equivalent to
    circuit_graph but with CNOT gate directions matching the edges
    of coupling_graph. Raises an exception if the circuit_graph
    does not conform to the coupling_graph.
    
Change the direction of CNOT gates to conform to CouplingGraph.

Adds "h" to the circuit basis.

Returns a DAGCircuit object containing a circuit equivalent to
circuit_graph but with CNOT gate directions matching the edges
of coupling_graph. Raises an exception if the circuit_graph
does not conform to the coupling_graph.
Change the direction of CNOT gates to conform to CouplingGraph . Adds " h " to the circuit basis . Returns a DAGCircuit object containing a circuit equivalent to circuit_graph but with CNOT gate directions matching the edges of coupling_graph . Raises an exception if the circuit_graph does not conform to the coupling_graph .
def direction_mapper(circuit_graph, coupling_graph):
    if "cx" not in circuit_graph.basis:
        return circuit_graph
    if circuit_graph.basis["cx"] != (2, 0, 0):
        raise MapperError("cx gate has unexpected signature %s" %
                          circuit_graph.basis["cx"])
    flipped_cx_circuit = DAGCircuit()
    flipped_cx_circuit.add_qreg('q', 2)
    flipped_cx_circuit.add_basis_element("CX", 2)
    flipped_cx_circuit.add_basis_element("U", 1, 0, 3)
    flipped_cx_circuit.add_basis_element("cx", 2)
    flipped_cx_circuit.add_basis_element("u2", 1, 0, 2)
    flipped_cx_circuit.add_basis_element("h", 1)
    flipped_cx_circuit.add_gate_data("cx", cx_data)
    flipped_cx_circuit.add_gate_data("u2", u2_data)
    flipped_cx_circuit.add_gate_data("h", h_data)
    flipped_cx_circuit.apply_operation_back("h", [("q", 0)])
    flipped_cx_circuit.apply_operation_back("h", [("q", 1)])
    flipped_cx_circuit.apply_operation_back("cx", [("q", 1), ("q", 0)])
    flipped_cx_circuit.apply_operation_back("h", [("q", 0)])
    flipped_cx_circuit.apply_operation_back("h", [("q", 1)])
    cg_edges = coupling_graph.get_edges()
    for cx_node in circuit_graph.get_named_nodes("cx"):
        nd = circuit_graph.multi_graph.node[cx_node]
        cxedge = tuple(nd["qargs"])
        if cxedge in cg_edges:
            logger.debug("cx %s[%d], %s[%d] -- OK",
                         cxedge[0][0], cxedge[0][1],
                         cxedge[1][0], cxedge[1][1])
            continue
        elif (cxedge[1], cxedge[0]) in cg_edges:
            circuit_graph.substitute_circuit_one(cx_node,
                                                 flipped_cx_circuit,
                                                 wires=[("q", 0), ("q", 1)])
            logger.debug("cx %s[%d], %s[%d] -FLIP",
                         cxedge[0][0], cxedge[0][1],
                         cxedge[1][0], cxedge[1][1])
        else:
            raise MapperError("circuit incompatible with CouplingGraph: "
                              "cx on %s" % pprint.pformat(cxedge))
    return circuit_graph
def direction_mapper ( circuit_graph , coupling_graph ) : if "cx" not in circuit_graph . basis : return circuit_graph if circuit_graph . basis [ "cx" ] != ( 2 , 0 , 0 ) : raise MapperError ( "cx gate has unexpected signature %s" % circuit_graph . basis [ "cx" ] ) flipped_cx_circuit = DAGCircuit ( ) flipped_cx_circuit . add_qreg ( 'q' , 2 ) flipped_cx_circuit . add_basis_element ( "CX" , 2 ) flipped_cx_circuit . add_basis_element ( "U" , 1 , 0 , 3 ) flipped_cx_circuit . add_basis_element ( "cx" , 2 ) flipped_cx_circuit . add_basis_element ( "u2" , 1 , 0 , 2 ) flipped_cx_circuit . add_basis_element ( "h" , 1 ) flipped_cx_circuit . add_gate_data ( "cx" , cx_data ) flipped_cx_circuit . add_gate_data ( "u2" , u2_data ) flipped_cx_circuit . add_gate_data ( "h" , h_data ) flipped_cx_circuit . apply_operation_back ( "h" , [ ( "q" , 0 ) ] ) flipped_cx_circuit . apply_operation_back ( "h" , [ ( "q" , 1 ) ] ) flipped_cx_circuit . apply_operation_back ( "cx" , [ ( "q" , 1 ) , ( "q" , 0 ) ] ) flipped_cx_circuit . apply_operation_back ( "h" , [ ( "q" , 0 ) ] ) flipped_cx_circuit . apply_operation_back ( "h" , [ ( "q" , 1 ) ] ) cg_edges = coupling_graph . get_edges ( ) for cx_node in circuit_graph . get_named_nodes ( "cx" ) : nd = circuit_graph . multi_graph . node [ cx_node ] cxedge = tuple ( nd [ "qargs" ] ) if cxedge in cg_edges : logger . debug ( "cx %s[%d], %s[%d] -- OK" , cxedge [ 0 ] [ 0 ] , cxedge [ 0 ] [ 1 ] , cxedge [ 1 ] [ 0 ] , cxedge [ 1 ] [ 1 ] ) continue elif ( cxedge [ 1 ] , cxedge [ 0 ] ) in cg_edges : circuit_graph . substitute_circuit_one ( cx_node , flipped_cx_circuit , wires = [ ( "q" , 0 ) , ( "q" , 1 ) ] ) logger . debug ( "cx %s[%d], %s[%d] -FLIP" , cxedge [ 0 ] [ 0 ] , cxedge [ 0 ] [ 1 ] , cxedge [ 1 ] [ 0 ] , cxedge [ 1 ] [ 1 ] ) else : raise MapperError ( "circuit incompatible with CouplingGraph: " "cx on %s" % pprint . pformat ( cxedge ) ) return circuit_graph
Change the direction of CNOT gates to conform to CouplingGraph.
Change the direction of CNOT gates to conform to CouplingGraph .
"""Change the direction of CNOT gates to conform to CouplingGraph.

    circuit_graph = input DAGCircuit
    coupling_graph = corresponding CouplingGraph

    Adds "h" to the circuit basis.

    Returns a DAGCircuit object containing a circuit equivalent to
    circuit_graph but with CNOT gate directions matching the edges
    of coupling_graph. Raises an exception if the circuit_graph
    does not conform to the coupling_graph.
    """
circuit_graph
coupling_graph