Beispiel #1
0
def compile(circuits,
            backend,
            config=None,
            basis_gates=None,
            coupling_map=None,
            initial_layout=None,
            shots=1024,
            max_credits=10,
            seed=None,
            qobj_id=None,
            seed_mapper=None,
            pass_manager=None,
            memory=False):
    """Compile a list of circuits into a qobj.

    Args:
        circuits (QuantumCircuit or list[QuantumCircuit]): circuits to compile
        backend (BaseBackend): a backend to compile for
        config (dict): dictionary of parameters (e.g. noise) used by runner
        basis_gates (str): comma-separated basis gate set to compile to
        coupling_map (list): coupling map (perhaps custom) to target in mapping
        initial_layout (list): initial layout of qubits in mapping
        shots (int): number of repetitions of each circuit, for sampling
        max_credits (int): maximum credits to use
        seed (int): random seed for simulators
        seed_mapper (int): random seed for swapper mapper
        qobj_id (int): identifier for the generated qobj
        pass_manager (PassManager): a pass manger for the transpiler pipeline
        memory (bool): if True, per-shot measurement bitstrings are returned as well

    Returns:
        Qobj: the qobj to be run on the backends

    Raises:
        QiskitError: if the desired options are not supported by backend
    """
    if config:
        warnings.warn(
            'The `config` argument is deprecated and '
            'does not do anything', DeprecationWarning)

    circuits = transpiler.transpile(circuits, backend, basis_gates,
                                    coupling_map, initial_layout, seed_mapper,
                                    pass_manager)

    # step 4: Making a qobj
    run_config = RunConfig()

    if seed:
        run_config.seed = seed
    if shots:
        run_config.shots = shots
    if max_credits:
        run_config.max_credits = max_credits
    if memory:
        run_config.memory = memory
    qobj = circuits_to_qobj(circuits,
                            user_qobj_header=QobjHeader(),
                            run_config=run_config,
                            qobj_id=qobj_id)

    return qobj
    def __init__(self, backend, run_config=None, initial_layout=None, pass_manager=None, seed_mapper=None,
                 backend_options=None, noise_model=None, timeout=None, wait=5, circuit_cache=None,
                 skip_qobj_validation=False):
        """Constructor.

        Args:
            backend (BaseBackend): instance of selected backend
            run_config (RunConfig): the run config see https://github.com/Qiskit/qiskit-terra/blob/master/qiskit/qobj/run_config.py
            initial_layout (dict): initial layout of qubits in mapping
            pass_manager (PassManager): pass manager to handle how to compile the circuits
            seed_mapper (int): the random seed for circuit mapper
            backend_options (dict): all config setting for backend
            noise_model (qiskit.provider.aer.noise.noise_model.NoiseModel): noise model for simulator
            timeout (float or None): seconds to wait for job. If None, wait indefinitely.
            wait (float): seconds between queries to result
            circuit_cache (CircuitCache): A CircuitCache to use when calling compile_and_run_circuits
            skip_qobj_validation (bool): Bypass Qobj validation to decrease submission time
        """
        self._backend = backend
        # setup run config
        if run_config is None:
            run_config = RunConfig(shots=1024, max_credits=10, memory=False)

        if getattr(run_config, 'shots', None) is not None:
            if self.is_statevector and run_config.shots == 1:
                logger.info("statevector backend only works with shot=1, change "
                            "shots from {} to 1.".format(run_config.shots))
                run_config.shots = 1

        if getattr(run_config, 'memory', None) is not None:
            if not self.is_simulator and run_config.memory is True:
                logger.info("The memory flag only supports simulator rather than real device. "
                            "Change it to from {} to False.".format(run_config.memory))
                run_config.memory = False
        self._run_config = run_config

        # setup backend config
        coupling_map = getattr(backend.configuration(), 'coupling_map', None)
        # TODO: basis gates will be [str] rather than comma-separated str
        basis_gates = backend.configuration().basis_gates
        if isinstance(basis_gates, list):
            basis_gates = ','.join(basis_gates)

        self._backend_config = {
            'basis_gates': basis_gates,
            'coupling_map': coupling_map
        }

        # setup noise config
        noise_config = None
        if noise_model is not None:
            if is_aer_provider(self._backend):
                if not self.is_statevector:
                    noise_config = noise_model
                else:
                    logger.info("The noise model can be only used with Aer qasm simulator. "
                                "Change it to None.")
            else:
                logger.info("The noise model can be only used with Qiskit Aer. "
                            "Please install it.")
        self._noise_config = {} if noise_config is None else {'noise_model': noise_config}

        # setup compile config
        self._compile_config = {
            'pass_manager': pass_manager,
            'initial_layout': initial_layout,
            'seed_mapper': seed_mapper,
            'qobj_id': None
        }

        # setup job config
        self._qjob_config = {'timeout': timeout} if self.is_local \
            else {'timeout': timeout, 'wait': wait}

        # setup backend options for run
        self._backend_options = {}
        if isinstance(self._backend.provider(), IBMQProvider):
            logger.info("backend_options can not used with the backends in IBMQ provider.")
        else:
            self._backend_options = {} if backend_options is None \
                else {'backend_options': backend_options}

        self._shared_circuits = False
        self._circuit_summary = False
        self._circuit_cache = circuit_cache
        self._skip_qobj_validation = skip_qobj_validation

        logger.info(self)