コード例 #1
0
ファイル: assemble.py プロジェクト: ntx62/qiskit-terra
def _parse_common_args(backend, qobj_id, qobj_header, shots, memory,
                       max_credits, seed_simulator, **run_config):
    """Resolve the various types of args allowed to the assemble() function through
    duck typing, overriding args, etc. Refer to the assemble() docstring for details on
    what types of inputs are allowed.

    Here the args are resolved by converting them to standard instances, and prioritizing
    them in case a run option is passed through multiple args (explicitly setting an arg
    has more priority than the arg set by backend)

    Returns:
        RunConfig: a run config, which is a standardized object that configures the qobj
            and determines the runtime environment.

    Raises:
        QiskitError: if the memory arg is True and the backend does not support
        memory.
    """
    # grab relevant info from backend if it exists
    backend_config = None
    if backend:
        backend_config = backend.configuration()
        # check for memory flag applied to backend that does not support memory
        if memory and not backend_config.memory:
            raise QiskitError("memory not supported by backend {}".format(
                backend_config.backend_name))

    # an identifier for the Qobj
    qobj_id = qobj_id or str(uuid.uuid4())

    # The header that goes at the top of the Qobj (and later Result)
    # we process it as dict, then write entries that are not None to a QobjHeader object
    qobj_header = qobj_header or {}
    if isinstance(qobj_header, QobjHeader):
        qobj_header = qobj_header.to_dict()
    backend_name = getattr(backend_config, 'backend_name', None)
    backend_version = getattr(backend_config, 'backend_version', None)
    qobj_header = {
        **dict(backend_name=backend_name, backend_version=backend_version),
        **qobj_header
    }
    qobj_header = QobjHeader(
        **{k: v
           for k, v in qobj_header.items() if v is not None})

    # create run configuration and populate
    run_config_dict = dict(shots=shots,
                           memory=memory,
                           max_credits=max_credits,
                           seed_simulator=seed_simulator,
                           **run_config)

    return qobj_id, qobj_header, run_config_dict
コード例 #2
0
ファイル: assemble.py プロジェクト: wahaj/qiskit-terra
def _parse_common_args(backend, qobj_id, qobj_header, shots, memory,
                       max_credits, seed_simulator, init_qubits, rep_delay,
                       **run_config):
    """Resolve the various types of args allowed to the assemble() function through
    duck typing, overriding args, etc. Refer to the assemble() docstring for details on
    what types of inputs are allowed.

    Here the args are resolved by converting them to standard instances, and prioritizing
    them in case a run option is passed through multiple args (explicitly setting an arg
    has more priority than the arg set by backend)

    Returns:
        RunConfig: a run config, which is a standardized object that configures the qobj
            and determines the runtime environment.

    Raises:
        QiskitError: if the memory arg is True and the backend does not support
            memory. Also if shots exceeds max_shots for the configured backend.
    """
    # grab relevant info from backend if it exists
    backend_config = None
    if backend:
        backend_config = backend.configuration()
        # check for memory flag applied to backend that does not support memory
        if memory and not backend_config.memory:
            raise QiskitError("memory not supported by backend {}".format(
                backend_config.backend_name))

    # an identifier for the Qobj
    qobj_id = qobj_id or str(uuid.uuid4())

    # The header that goes at the top of the Qobj (and later Result)
    # we process it as dict, then write entries that are not None to a QobjHeader object
    qobj_header = qobj_header or {}
    if isinstance(qobj_header, QobjHeader):
        qobj_header = qobj_header.to_dict()
    backend_name = getattr(backend_config, 'backend_name', None)
    backend_version = getattr(backend_config, 'backend_version', None)
    qobj_header = {
        **dict(backend_name=backend_name, backend_version=backend_version),
        **qobj_header
    }
    qobj_header = QobjHeader(
        **{k: v
           for k, v in qobj_header.items() if v is not None})

    max_shots = getattr(backend_config, 'max_shots', None)
    if shots is None:
        if max_shots:
            shots = min(1024, max_shots)
        else:
            shots = 1024
    elif max_shots and max_shots < shots:
        raise QiskitError(
            'Number of shots specified: %s exceeds max_shots property of the '
            'backend: %s.' % (shots, max_shots))

    dynamic_reprate_enabled = getattr(backend_config,
                                      'dynamic_reprate_enabled', False)
    if dynamic_reprate_enabled:
        default_rep_delay = getattr(backend_config, "default_rep_delay", None)
        rep_delay_range = getattr(backend_config, "rep_delay_range", None)
        rep_delay = _parse_rep_delay(rep_delay, default_rep_delay,
                                     rep_delay_range)
    else:
        if rep_delay is not None:
            rep_delay = None
            warnings.warn(
                "Dynamic rep rates not supported on this backend, cannot use rep_delay.",
                RuntimeWarning,
            )

    # create run configuration and populate
    run_config_dict = dict(shots=shots,
                           memory=memory,
                           max_credits=max_credits,
                           seed_simulator=seed_simulator,
                           init_qubits=init_qubits,
                           rep_delay=rep_delay,
                           **run_config)

    return qobj_id, qobj_header, run_config_dict
コード例 #3
0
def _parse_run_args(backend, qobj_id, qobj_header, shots, memory, max_credits,
                    seed_simulator, default_qubit_los, default_meas_los,
                    schedule_los, meas_level, meas_return, memory_slots,
                    memory_slot_size, rep_time, parameter_binds, **run_config):
    """Resolve the various types of args allowed to the assemble() function through
    duck typing, overriding args, etc. Refer to the assemble() docstring for details on
    what types of inputs are allowed.

    Here the args are resolved by converting them to standard instances, and prioritizing
    them in case a run option is passed through multiple args (explicitly setting an arg
    has more priority than the arg set by backend)

    Returns:
        RunConfig: a run config, which is a standardized object that configures the qobj
            and determines the runtime environment.
    """
    # grab relevant info from backend if it exists
    backend_config = None
    backend_default = None
    if backend:
        backend_config = backend.configuration()
        # TODO : Remove usage of config.defaults when backend.defaults() is updated.
        try:
            backend_default = backend.defaults()
        except (ModelValidationError, AttributeError):
            from collections import namedtuple
            backend_config_defaults = getattr(backend_config, 'defaults', {})
            BackendDefault = namedtuple('BackendDefault',
                                        ('qubit_freq_est', 'meas_freq_est'))
            backend_default = BackendDefault(
                qubit_freq_est=backend_config_defaults.get('qubit_freq_est'),
                meas_freq_est=backend_config_defaults.get('meas_freq_est'))

    memory_slots = memory_slots or getattr(backend_config, 'memory_slots',
                                           None)
    rep_time = rep_time or getattr(backend_config, 'rep_times', None)
    if isinstance(rep_time, list):
        rep_time = rep_time[-1]

    parameter_binds = parameter_binds or []

    # add default empty lo config
    schedule_los = schedule_los or []
    if isinstance(schedule_los, (LoConfig, dict)):
        schedule_los = [schedule_los]

    # Convert to LoConfig if lo configuration supplied as dictionary
    schedule_los = [
        lo_config if isinstance(lo_config, LoConfig) else LoConfig(lo_config)
        for lo_config in schedule_los
    ]

    qubit_lo_freq = default_qubit_los or getattr(backend_default,
                                                 'qubit_freq_est', [])
    meas_lo_freq = default_meas_los or getattr(backend_default,
                                               'meas_freq_est', [])

    # an identifier for the Qobj
    qobj_id = qobj_id or str(uuid.uuid4())

    # The header that goes at the top of the Qobj (and later Result)
    # we process it as dict, then write entries that are not None to a QobjHeader object
    qobj_header = qobj_header or {}
    if isinstance(qobj_header, QobjHeader):
        qobj_header = qobj_header.to_dict()
    backend_name = getattr(backend_config, 'backend_name', None)
    backend_version = getattr(backend_config, 'backend_version', None)
    qobj_header = {
        **dict(backend_name=backend_name, backend_version=backend_version),
        **qobj_header
    }
    qobj_header = QobjHeader(
        **{k: v
           for k, v in qobj_header.items() if v is not None})

    # create run configuration and populate
    run_config_dict = dict(
        shots=shots,
        memory=memory,
        max_credits=max_credits,
        seed_simulator=seed_simulator,
        seed=seed_simulator,  # deprecated
        qubit_lo_freq=qubit_lo_freq,
        meas_lo_freq=meas_lo_freq,
        schedule_los=schedule_los,
        meas_level=meas_level,
        meas_return=meas_return,
        memory_slots=memory_slots,
        memory_slot_size=memory_slot_size,
        rep_time=rep_time,
        parameter_binds=parameter_binds,
        **run_config)
    run_config = RunConfig(
        **{k: v
           for k, v in run_config_dict.items() if v is not None})

    return qobj_id, qobj_header, run_config
コード例 #4
0
def _parse_common_args(
    backend,
    qobj_id,
    qobj_header,
    shots,
    memory,
    max_credits,
    seed_simulator,
    init_qubits,
    rep_delay,
    qubit_lo_freq,
    meas_lo_freq,
    qubit_lo_range,
    meas_lo_range,
    schedule_los,
    **run_config,
):
    """Resolve the various types of args allowed to the assemble() function through
    duck typing, overriding args, etc. Refer to the assemble() docstring for details on
    what types of inputs are allowed.

    Here the args are resolved by converting them to standard instances, and prioritizing
    them in case a run option is passed through multiple args (explicitly setting an arg
    has more priority than the arg set by backend)

    Returns:
        RunConfig: a run config, which is a standardized object that configures the qobj
            and determines the runtime environment.

    Raises:
        QiskitError:
            - If the memory arg is True and the backend does not support memory.
            - If ``shots`` exceeds ``max_shots`` for the configured backend.
            - If ``shots`` are not int type.
            - If any of qubit or meas lo's, or associated ranges do not have length equal to
            ``n_qubits``.
            - If qubit or meas lo's do not fit into perscribed ranges.
    """
    # grab relevant info from backend if it exists
    backend_config = None
    backend_defaults = None
    n_qubits = None
    if backend:
        backend_config = backend.configuration()
        n_qubits = backend_config.n_qubits
        # check for memory flag applied to backend that does not support memory
        if memory and not backend_config.memory:
            raise QiskitError(
                f"memory not supported by backend {backend_config.backend_name}"
            )

        # try to set defaults for pulse, other leave as None
        if backend_config.open_pulse:
            try:
                backend_defaults = backend.defaults()
            except AttributeError:
                pass

    # an identifier for the Qobj
    qobj_id = qobj_id or str(uuid.uuid4())

    # The header that goes at the top of the Qobj (and later Result)
    # we process it as dict, then write entries that are not None to a QobjHeader object
    qobj_header = qobj_header or {}
    if isinstance(qobj_header, QobjHeader):
        qobj_header = qobj_header.to_dict()
    backend_name = getattr(backend_config, "backend_name", None)
    backend_version = getattr(backend_config, "backend_version", None)
    qobj_header = {
        **dict(backend_name=backend_name, backend_version=backend_version),
        **qobj_header,
    }
    qobj_header = QobjHeader(
        **{k: v
           for k, v in qobj_header.items() if v is not None})

    max_shots = getattr(backend_config, "max_shots", None)
    if shots is None:
        if max_shots:
            shots = min(1024, max_shots)
        else:
            shots = 1024
    elif not isinstance(shots, int):
        raise QiskitError("Argument 'shots' should be of type 'int'")
    elif max_shots and max_shots < shots:
        raise QiskitError(
            "Number of shots specified: %s exceeds max_shots property of the "
            "backend: %s." % (shots, max_shots))

    dynamic_reprate_enabled = getattr(backend_config,
                                      "dynamic_reprate_enabled", False)
    if dynamic_reprate_enabled:
        default_rep_delay = getattr(backend_config, "default_rep_delay", None)
        rep_delay_range = getattr(backend_config, "rep_delay_range", None)
        rep_delay = _parse_rep_delay(rep_delay, default_rep_delay,
                                     rep_delay_range)
    else:
        if rep_delay is not None:
            rep_delay = None
            warnings.warn(
                "Dynamic rep rates not supported on this backend, cannot use rep_delay.",
                RuntimeWarning,
            )

    qubit_lo_freq = qubit_lo_freq or getattr(backend_defaults,
                                             "qubit_freq_est", None)
    meas_lo_freq = meas_lo_freq or getattr(backend_defaults, "meas_freq_est",
                                           None)

    qubit_lo_range = qubit_lo_range or getattr(backend_config,
                                               "qubit_lo_range", None)
    meas_lo_range = meas_lo_range or getattr(backend_config, "meas_lo_range",
                                             None)

    # check that LO frequencies are in the perscribed range
    _check_lo_freqs(qubit_lo_freq, qubit_lo_range, "qubit")
    _check_lo_freqs(meas_lo_freq, meas_lo_range, "meas")

    # configure experiment level LO frequencies
    schedule_los = schedule_los or []
    if isinstance(schedule_los, (LoConfig, dict)):
        schedule_los = [schedule_los]

    # Convert to LoConfig if LO configuration supplied as dictionary
    schedule_los = [
        lo_config if isinstance(lo_config, LoConfig) else LoConfig(lo_config)
        for lo_config in schedule_los
    ]

    # create run configuration and populate
    run_config_dict = dict(
        shots=shots,
        memory=memory,
        max_credits=max_credits,
        seed_simulator=seed_simulator,
        init_qubits=init_qubits,
        rep_delay=rep_delay,
        qubit_lo_freq=qubit_lo_freq,
        meas_lo_freq=meas_lo_freq,
        qubit_lo_range=qubit_lo_range,
        meas_lo_range=meas_lo_range,
        schedule_los=schedule_los,
        n_qubits=n_qubits,
        **run_config,
    )

    return qobj_id, qobj_header, run_config_dict