def compile(qasm_circuit,
            basis_gates='u1,u2,u3,cx,id',
            coupling_map=None,
            initial_layout=None,
            silent=True,
            get_layout=False,
            format='dag'):
    """Compile the circuit.

    This builds the internal "to execute" list which is list of quantum
    circuits to run on different backends.

    Args:
        qasm_circuit (str): qasm text to compile
        silent (bool): is an option to print out the compiling information
            or not
        basis_gates (str): a comma seperated string and are the base gates,
                           which by default are: u1,u2,u3,cx,id
        coupling_map (dict): A directed graph of coupling::

            {
             control(int):
                 [
                     target1(int),
                     target2(int),
                     , ...
                 ],
                 ...
            }

            eg. {0: [2], 1: [2], 3: [2]}

        initial_layout (dict): A mapping of qubit to qubit::

                              {
                                ("q", strart(int)): ("q", final(int)),
                                ...
                              }
                              eg.
                              {
                                ("q", 0): ("q", 0),
                                ("q", 1): ("q", 1),
                                ("q", 2): ("q", 2),
                                ("q", 3): ("q", 3)
                              }
        format (str): The target format of the compilation:
            {'dag', 'json', 'qasm'}

    Returns:
        Compiled circuit
    """
    compiled_dag_circuit = _unroller_code(qasm_circuit,
                                          basis_gates=basis_gates)
    final_layout = None
    # if a coupling map is given compile to the map
    if coupling_map:
        if not silent:
            print("pre-mapping properties: %s" %
                  compiled_dag_circuit.property_summary())
        # Insert swap gates
        coupling = mapper.Coupling(coupling_map)
        if not silent:
            print("initial layout: %s" % initial_layout)
        compiled_dag_circuit, final_layout = mapper.swap_mapper(
            compiled_dag_circuit,
            coupling,
            initial_layout,
            trials=20,
            verbose=False)
        if not silent:
            print("final layout: %s" % final_layout)
        # Expand swaps
        compiled_dag_circuit = _unroller_code(compiled_dag_circuit.qasm())
        # Change cx directions
        compiled_dag_circuit = mapper.direction_mapper(compiled_dag_circuit,
                                                       coupling)
        # Simplify cx gates
        mapper.cx_cancellation(compiled_dag_circuit)
        # Simplify single qubit gates
        compiled_dag_circuit = mapper.optimize_1q_gates(compiled_dag_circuit)
        if not silent:
            print("post-mapping properties: %s" %
                  compiled_dag_circuit.property_summary())
    # choose output format
    if format == 'dag':
        compiled_circuit = compiled_dag_circuit
    elif format == 'json':
        compiled_circuit = dag2json(compiled_dag_circuit)
    elif format == 'qasm':
        compiled_circuit = compiled_dag_circuit.qasm()
    else:
        raise QiskitCompilerError('unrecognized circuit format')

    if get_layout:
        return compiled_circuit, final_layout
    else:
        return compiled_circuit
def compile(qasm_circuit,
            basis_gates='u1,u2,u3,cx,id',
            coupling_map=None,
            initial_layout=None,
            get_layout=False,
            format='dag'):
    """Compile the circuit.

    This builds the internal "to execute" list which is list of quantum
    circuits to run on different backends.

    Args:
        qasm_circuit (str): qasm text to compile
        basis_gates (str): a comma seperated string and are the base gates,
                           which by default are: u1,u2,u3,cx,id
        coupling_map (dict): A directed graph of coupling::

            {
             control(int):
                 [
                     target1(int),
                     target2(int),
                     , ...
                 ],
                 ...
            }

            eg. {0: [2], 1: [2], 3: [2]}

        initial_layout (dict): A mapping of qubit to qubit::

                              {
                                ("q", start(int)): ("q", final(int)),
                                ...
                              }
                              eg.
                              {
                                ("q", 0): ("q", 0),
                                ("q", 1): ("q", 1),
                                ("q", 2): ("q", 2),
                                ("q", 3): ("q", 3)
                              }
        get_layout (bool): flag for returning the layout.
        format (str): The target format of the compilation:
            {'dag', 'json', 'qasm'}

    Returns:
        object: If get_layout == False, the compiled circuit in the specified
            format. If get_layout == True, a tuple is returned, with the
            second element being the layout.

    Raises:
        QISKitCompilerError: if the format is not valid.
    """
    compiled_dag_circuit = _unroller_code(qasm_circuit,
                                          basis_gates=basis_gates)
    final_layout = None
    # if a coupling map is given compile to the map
    if coupling_map:
        logger.info("pre-mapping properties: %s",
                    compiled_dag_circuit.property_summary())
        # Insert swap gates
        coupling = mapper.Coupling(coupling_map)
        logger.info("initial layout: %s", initial_layout)
        compiled_dag_circuit, final_layout = mapper.swap_mapper(
            compiled_dag_circuit, coupling, initial_layout, trials=20, seed=13)
        logger.info("final layout: %s", final_layout)
        # Expand swaps
        compiled_dag_circuit = _unroller_code(compiled_dag_circuit.qasm())
        # Change cx directions
        compiled_dag_circuit = mapper.direction_mapper(compiled_dag_circuit,
                                                       coupling)
        # Simplify cx gates
        mapper.cx_cancellation(compiled_dag_circuit)
        # Simplify single qubit gates
        compiled_dag_circuit = mapper.optimize_1q_gates(compiled_dag_circuit)
        logger.info("post-mapping properties: %s",
                    compiled_dag_circuit.property_summary())
    # choose output format
    if format == 'dag':
        compiled_circuit = compiled_dag_circuit
    elif format == 'json':
        compiled_circuit = dag2json(compiled_dag_circuit)
    elif format == 'qasm':
        compiled_circuit = compiled_dag_circuit.qasm()
    else:
        raise QISKitCompilerError('unrecognized circuit format')

    if get_layout:
        return compiled_circuit, final_layout
    return compiled_circuit