예제 #1
0
 def test_parallel_progbar_used(self):
     """Test that correct progressbar is used."""
     not_used = TextProgressBar()
     not_used.touched = True
     used = TextProgressBar()
     parallel_map(_parfunc, list(range(10)))
     self.assertTrue(used.channel_id not in rec.channels.keys())
     self.assertTrue(not_used.channel_id in rec.channels.keys())
예제 #2
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,
            hpc=None,
            skip_transpiler=False):
    """Compile a list of circuits into a qobj.

    Args:
        circuits (QuantumCircuit or list[QuantumCircuit]): circuits to compile
        backend (BaseBackend or str): 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
        qobj_id (int): identifier for the generated qobj
        hpc (dict): HPC simulator parameters
        skip_transpiler (bool): If True, bypass most of the compilation process and
            creates a qobj with minimal check nor translation
    Returns:
        Qobj: the qobj to be run on the backends

    Raises:
        TranspilerError: in case of bad compile options, e.g. the hpc options.
    """
    # pylint: disable=redefined-builtin

    # Check for valid parameters for the experiments.
    if hpc is not None and \
            not all(key in hpc for key in ('multi_shot_optimization', 'omp_num_threads')):
        raise TranspilerError('Unknown HPC parameter format!')

    if isinstance(circuits, QuantumCircuit):
        circuits = [circuits]

    if isinstance(backend, str):
        try:
            backend = Aer.get_backend(backend)
        except KeyError:
            backend = IBMQ.get_backend(backend)

    pass_manager = None  # default pass manager which executes predetermined passes
    if skip_transpiler:  # empty pass manager which does nothing
        pass_manager = PassManager()

    backend_conf = backend.configuration()
    backend_name = backend_conf['name']
    basis_gates = basis_gates or backend_conf['basis_gates']
    coupling_map = coupling_map or backend_conf['coupling_map']

    qobj_config = deepcopy(config or {})
    qobj_config.update({
        'shots': shots,
        'max_credits': max_credits,
        'memory_slots': 0
    })

    qobj = Qobj(qobj_id=qobj_id or str(uuid.uuid4()),
                config=QobjConfig(**qobj_config),
                experiments=[],
                header=QobjHeader(backend_name=backend_name))

    if seed:
        qobj.config.seed = seed

    qobj.experiments = parallel_map(_build_exp_parallel,
                                    list(range(len(circuits))),
                                    task_args=(circuits, backend),
                                    task_kwargs={
                                        'initial_layout': initial_layout,
                                        'basis_gates': basis_gates,
                                        'config': config,
                                        'coupling_map': coupling_map,
                                        'seed': seed,
                                        'pass_manager': pass_manager
                                    })

    qobj.config.memory_slots = max(experiment.config.memory_slots
                                   for experiment in qobj.experiments)

    qobj.config.n_qubits = max(experiment.config.n_qubits
                               for experiment in qobj.experiments)

    return qobj
예제 #3
0
def dags_2_qobj(dags,
                backend_name,
                config=None,
                shots=None,
                max_credits=None,
                qobj_id=None,
                basis_gates=None,
                coupling_map=None,
                seed=None):
    """Convert a list of dags into a qobj.

    Args:
        dags (list[DAGCircuit]): dags to compile
        backend_name (str): name of runner backend
        config (dict): dictionary of parameters (e.g. noise) used by runner
        shots (int): number of repetitions of each circuit, for sampling
        max_credits (int): maximum credits to use
        qobj_id (int): identifier for the generated qobj
        basis_gates (list[str])): basis gates for the experiment
        coupling_map (list): coupling map (perhaps custom) to target in mapping
        seed (int): random seed for simulators

    Returns:
        Qobj: the Qobj to be run on the backends
    """
    # TODO: the following will be removed from qobj and thus removed here:
    # `basis_gates`, `coupling_map`

    # Step 1: create the Qobj, with empty experiments.
    # Copy the configuration: the values in `config` have preference
    qobj_config = deepcopy(config or {})
    # TODO: "memory_slots" is required by the qobj schema in the top-level
    # qobj.config, and is user-defined. At the moment is set to the maximum
    # number of *register* slots for the circuits, in order to have `measure`
    # behave properly until the transition is over; and each circuit stores
    # its memory_slots in its configuration.
    qobj_config.update({
        'shots': shots,
        'max_credits': max_credits,
        'memory_slots': 0
    })

    qobj = Qobj(qobj_id=qobj_id or str(uuid.uuid4()),
                config=QobjConfig(**qobj_config),
                experiments=[],
                header=QobjHeader(backend_name=backend_name))
    if seed:
        qobj.config.seed = seed

    qobj.experiments = parallel_map(_dags_2_qobj_parallel,
                                    dags,
                                    task_kwargs={
                                        'basis_gates': basis_gates,
                                        'config': config,
                                        'coupling_map': coupling_map
                                    })

    # Update the `memory_slots` value.
    # TODO: remove when `memory_slots` can be provided by the user.
    qobj.config.memory_slots = max(experiment.config.memory_slots
                                   for experiment in qobj.experiments)

    # Update the `n_qubits` global value.
    # TODO: num_qubits is not part of the qobj specification, but needed
    # for the simulator.
    qobj.config.n_qubits = max(experiment.config.n_qubits
                               for experiment in qobj.experiments)

    return qobj
예제 #4
0
 def test_parallel_progressbar(self):
     """Test parallel_map with progress bar"""
     TextProgressBar()
     ans = parallel_map(_parfunc, list(range(10)))
     self.assertEqual(ans, list(range(10)))
예제 #5
0
 def test_parallel(self):
     """Test parallel_map """
     ans = parallel_map(_parfunc, list(range(10)))
     self.assertEqual(ans, list(range(10)))
예제 #6
0
 def test_parallel_circuit_names(self):
     """Verify unique circuit names in parallel"""
     out_circs = parallel_map(_build_simple, list(range(10)))
     names = [circ.name for circ in out_circs]
     self.assertEqual(len(names), len(set(names)))