def _combine_args(shared_transpiler_args, unique_config):
    # Pop optimization_level to exclude it from the kwargs when building a
    # PassManagerConfig
    level = shared_transpiler_args.pop("optimization_level")
    pass_manager_config = shared_transpiler_args
    pass_manager_config.update(unique_config.pop("pass_manager_config"))
    pass_manager_config = PassManagerConfig(**pass_manager_config)
    # restore optimization_level in the input shared dict in case it's used again
    # in the same process
    shared_transpiler_args["optimization_level"] = level

    transpile_config = unique_config
    transpile_config["pass_manager_config"] = pass_manager_config

    if transpile_config["faulty_qubits_map"]:
        pass_manager_config.initial_layout = _remap_layout_faulty_backend(
            pass_manager_config.initial_layout,
            transpile_config["faulty_qubits_map"])

    # we choose an appropriate one based on desired optimization level
    if level == 0:
        pass_manager = level_0_pass_manager(pass_manager_config)
    elif level == 1:
        pass_manager = level_1_pass_manager(pass_manager_config)
    elif level == 2:
        pass_manager = level_2_pass_manager(pass_manager_config)
    elif level == 3:
        pass_manager = level_3_pass_manager(pass_manager_config)
    else:
        raise TranspilerError("optimization_level can range from 0 to 3.")
    return transpile_config, pass_manager
Exemple #2
0
def _transpile_circuit(circuit_config_tuple):
    """Select a PassManager and run a single circuit through it.
    Args:
        circuit_config_tuple (tuple):
            circuit (QuantumCircuit): circuit to transpile
            transpile_config (dict): configuration dictating how to transpile. The
                dictionary has the following format:
                {'optimization_level': int,
                 'pass_manager': PassManager,
                 'output_name': string,
                 'callback': callable,
                 'pass_manager_config': PassManagerConfig}
    Returns:
        QuantumCircuit: transpiled circuit
    Raises:
        TranspilerError: if transpile_config is not valid or transpilation incurs error
    """
    circuit, transpile_config = circuit_config_tuple

    pass_manager_config = transpile_config['pass_manager_config']

    # Workaround for ion trap support: If basis gates includes
    # Mølmer-Sørensen (rxx) and the circuit includes gates outside the basis,
    # first unroll to u3, cx, then run MSBasisDecomposer to target basis.
    basic_insts = ['measure', 'reset', 'barrier', 'snapshot']
    device_insts = set(pass_manager_config.basis_gates).union(basic_insts)
    ms_basis_swap = None
    if 'rxx' in pass_manager_config.basis_gates and \
            not device_insts >= circuit.count_ops().keys():
        ms_basis_swap = pass_manager_config.basis_gates
        pass_manager_config.basis_gates = list(
            set(['u3', 'cx']).union(pass_manager_config.basis_gates))

    if transpile_config['pass_manager'] is not None:
        # either the pass manager is already selected...
        pass_manager = transpile_config['pass_manager']
    else:
        # or we choose an appropriate one based on desired optimization level (default: level 1)
        if transpile_config['optimization_level'] is not None:
            level = transpile_config['optimization_level']
        else:
            level = 1

        if level == 0:
            pass_manager = level_0_pass_manager(pass_manager_config)
        elif level == 1:
            pass_manager = level_1_pass_manager(pass_manager_config)
        elif level == 2:
            pass_manager = level_2_pass_manager(pass_manager_config)
        elif level == 3:
            pass_manager = level_3_pass_manager(pass_manager_config)
        else:
            raise TranspilerError("optimization_level can range from 0 to 3.")

    if ms_basis_swap is not None:
        pass_manager.append(MSBasisDecomposer(ms_basis_swap))

    return pass_manager.run(circuit,
                            callback=transpile_config['callback'],
                            output_name=transpile_config['output_name'])
Exemple #3
0
def transpile_circuit(circuit, transpile_config):
    """Select a PassManager and run a single circuit through it.

    Args:
        circuit (QuantumCircuit): circuit to transpile
        transpile_config (TranspileConfig): configuration dictating how to transpile

    Returns:
        QuantumCircuit: transpiled circuit

    Raises:
        TranspilerError: if transpile_config is not valid or transpilation incurs error
    """

    # either the pass manager is already selected...
    if transpile_config.pass_manager is not None:
        pass_manager = transpile_config.pass_manager

    # or we choose an appropriate one based on desired optimization level (default: level 1)
    else:
        # Workaround for ion trap support: If basis gates includes
        # Mølmer-Sørensen (rxx) and the circuit includes gates outside the basis,
        # first unroll to u3, cx, then run MSBasisDecomposer to target basis.
        basic_insts = ['measure', 'reset', 'barrier', 'snapshot']
        device_insts = set(transpile_config.basis_gates).union(basic_insts)

        ms_basis_swap = None
        if 'rxx' in transpile_config.basis_gates and \
                not device_insts >= circuit.count_ops().keys():
            ms_basis_swap = transpile_config.basis_gates
            transpile_config.basis_gates = list(
                set(['u3', 'cx']).union(transpile_config.basis_gates))

        level = transpile_config.optimization_level
        if level is None:
            level = 1

        if level == 0:
            pass_manager = level_0_pass_manager(transpile_config)
        elif level == 1:
            pass_manager = level_1_pass_manager(transpile_config)
        elif level == 2:
            pass_manager = level_2_pass_manager(transpile_config)
        elif level == 3:
            pass_manager = level_3_pass_manager(transpile_config)
        else:
            raise TranspilerError("optimization_level can range from 0 to 3.")

        if ms_basis_swap is not None:
            pass_manager.append(MSBasisDecomposer(ms_basis_swap))

    # Set a callback on the pass manager there is one
    if getattr(transpile_config, 'callback', None):
        pass_manager.callback = transpile_config.callback

    out_circuit = pass_manager.run(circuit)
    out_circuit.name = transpile_config.output_name

    return out_circuit
Exemple #4
0
def _transpile_circuit(
        circuit_config_tuple: Tuple[QuantumCircuit, Dict]) -> QuantumCircuit:
    """Select a PassManager and run a single circuit through it.
    Args:
        circuit_config_tuple (tuple):
            circuit (QuantumCircuit): circuit to transpile
            transpile_config (dict): configuration dictating how to transpile. The
                dictionary has the following format:
                {'optimization_level': int,
                 'output_name': string,
                 'callback': callable,
                 'pass_manager_config': PassManagerConfig}
    Returns:
        The transpiled circuit
    Raises:
        TranspilerError: if transpile_config is not valid or transpilation incurs error
    """
    circuit, transpile_config = circuit_config_tuple

    pass_manager_config = transpile_config['pass_manager_config']

    if transpile_config['faulty_qubits_map']:
        pass_manager_config.initial_layout = _remap_layout_faulty_backend(
            pass_manager_config.initial_layout,
            transpile_config['faulty_qubits_map'])

    # we choose an appropriate one based on desired optimization level
    level = transpile_config['optimization_level']

    if level == 0:
        pass_manager = level_0_pass_manager(pass_manager_config)
    elif level == 1:
        pass_manager = level_1_pass_manager(pass_manager_config)
    elif level == 2:
        pass_manager = level_2_pass_manager(pass_manager_config)
    elif level == 3:
        pass_manager = level_3_pass_manager(pass_manager_config)
    else:
        raise TranspilerError("optimization_level can range from 0 to 3.")

    if pass_manager_config.scheduling_method is not None:
        if pass_manager_config.basis_gates:
            if 'delay' not in pass_manager_config.basis_gates:
                pass_manager_config.basis_gates.append('delay')
        else:
            pass_manager_config.basis_gates = ['delay']

    result = pass_manager.run(circuit,
                              callback=transpile_config['callback'],
                              output_name=transpile_config['output_name'])

    if transpile_config['faulty_qubits_map']:
        return _remap_circuit_faulty_backend(
            result, transpile_config['backend_num_qubits'],
            pass_manager_config.backend_properties,
            transpile_config['faulty_qubits_map'])

    return result
Exemple #5
0
def transpile_circuit(circuit, transpile_config):
    """Select a PassManager and run a single circuit through it.

    Args:
        circuit (QuantumCircuit): circuit to transpile
        transpile_config (TranspileConfig): configuration dictating how to transpile

    Returns:
        QuantumCircuit: transpiled circuit

    Raises:
        TranspilerError: if transpile_config is not valid or transpilation incurs error
    """
    # either the pass manager is already selected...
    if transpile_config.pass_manager:
        pass_manager = transpile_config.pass_manager

    # or we choose an appropriate one based on desired optimization level (default: level 1)
    else:
        level = transpile_config.optimization_level
        if level is None:
            level = 1

        if level == 0:
            pass_manager = level_0_pass_manager(transpile_config)
        elif level == 1:
            pass_manager = level_1_pass_manager(transpile_config)
        elif level == 2:
            pass_manager = level_2_pass_manager(transpile_config)
        elif level == 3:
            pass_manager = level_3_pass_manager(transpile_config)
        else:
            raise TranspilerError("optimization_level can range from 0 to 3.")

    # Set a callback on the pass manager there is one
    if getattr(transpile_config, 'callback', None):
        pass_manager.callback = transpile_config.callback

    out_circuit = pass_manager.run(circuit)
    out_circuit.name = transpile_config.output_name

    return out_circuit
Exemple #6
0
def transpile_circuit(circuit, transpile_config):
    """Select a PassManager and run a single circuit through it.

    Args:
        circuit (QuantumCircuit): circuit to transpile
        transpile_config (TranspileConfig): configuration dictating how to transpile

    Returns:
        QuantumCircuit: transpiled circuit

    Raises:
        TranspilerError: if transpile_config is not valid or transpilation incurs error
    """
    # if the pass manager is not already selected, choose an appropriate one.
    if transpile_config.pass_manager:
        pass_manager = transpile_config.pass_manager

    elif transpile_config.optimization_level is not None:
        level = transpile_config.optimization_level
        if level == 0:
            pass_manager = level_0_pass_manager(transpile_config)
        elif level == 1:
            pass_manager = level_1_pass_manager(transpile_config)
        elif level == 2:
            pass_manager = level_2_pass_manager(transpile_config)
        elif level == 3:
            pass_manager = level_3_pass_manager(transpile_config)
        else:
            raise TranspilerError("optimization_level can range from 0 to 3.")

    # legacy behavior
    elif transpile_config.coupling_map:
        pass_manager = level_1_pass_manager(transpile_config)
    else:
        pass_manager = default_pass_manager_simulator(transpile_config)

    # Set a callback on the pass manager if it's set
    if getattr(transpile_config, 'callback', None):
        pass_manager.callback = transpile_config.callback

    return pass_manager.run(circuit)
Exemple #7
0
def _transpile_circuit(
        circuit_config_tuple: Tuple[QuantumCircuit, Dict]) -> QuantumCircuit:
    """Select a PassManager and run a single circuit through it.
    Args:
        circuit_config_tuple (tuple):
            circuit (QuantumCircuit): circuit to transpile
            transpile_config (dict): configuration dictating how to transpile. The
                dictionary has the following format:
                {'optimization_level': int,
                 'output_name': string,
                 'callback': callable,
                 'pass_manager_config': PassManagerConfig}
    Returns:
        The transpiled circuit
    Raises:
        TranspilerError: if transpile_config is not valid or transpilation incurs error
    """
    circuit, transpile_config = circuit_config_tuple

    pass_manager_config = transpile_config['pass_manager_config']

    if transpile_config['faulty_qubits_map']:
        pass_manager_config.initial_layout = _remap_layout_faulty_backend(
            pass_manager_config.initial_layout,
            transpile_config['faulty_qubits_map'])

    ms_basis_swap = None
    if (pass_manager_config.translation_method == 'unroller'
            and pass_manager_config.basis_gates is not None):
        # Workaround for ion trap support: If basis gates includes
        # Mølmer-Sørensen (rxx) and the circuit includes gates outside the basis,
        # first unroll to u3, cx, then run MSBasisDecomposer to target basis.
        basic_insts = ['measure', 'reset', 'barrier', 'snapshot']
        device_insts = set(pass_manager_config.basis_gates).union(basic_insts)
        if 'rxx' in pass_manager_config.basis_gates and \
                not device_insts >= circuit.count_ops().keys():
            ms_basis_swap = pass_manager_config.basis_gates
            pass_manager_config.basis_gates = list(
                set(['u3', 'cx']).union(pass_manager_config.basis_gates))

    # we choose an appropriate one based on desired optimization level
    level = transpile_config['optimization_level']

    if level == 0:
        pass_manager = level_0_pass_manager(pass_manager_config)
    elif level == 1:
        pass_manager = level_1_pass_manager(pass_manager_config)
    elif level == 2:
        pass_manager = level_2_pass_manager(pass_manager_config)
    elif level == 3:
        pass_manager = level_3_pass_manager(pass_manager_config)
    else:
        raise TranspilerError("optimization_level can range from 0 to 3.")

    if ms_basis_swap is not None:
        pass_manager.append(MSBasisDecomposer(ms_basis_swap))

    result = pass_manager.run(circuit,
                              callback=transpile_config['callback'],
                              output_name=transpile_config['output_name'])

    if transpile_config['faulty_qubits_map']:
        return _remap_circuit_faulty_backend(
            result, transpile_config['backend_num_qubits'],
            pass_manager_config.backend_properties,
            transpile_config['faulty_qubits_map'])

    return result