Skip to content

Commit

Permalink
Fix parallezation in transpile.compile
Browse files Browse the repository at this point in the history
  • Loading branch information
nonhermitian committed Oct 4, 2018
1 parent 07c611e commit 98e78d0
Showing 1 changed file with 63 additions and 36 deletions.
99 changes: 63 additions & 36 deletions qiskit/transpiler/_transpiler.py
Expand Up @@ -70,49 +70,76 @@ def compile(circuits, backend,
basis_gates = basis_gates or backend_conf['basis_gates']
coupling_map = coupling_map or backend_conf['coupling_map']

# step 1: Making the list of dag circuits
dags = _circuits_2_dags(circuits)

# step 2: Transpile all the dags

# FIXME: Work-around for transpiling multiple circuits with different qreg names.
# Make compile take a list of initial_layouts.
_initial_layout = initial_layout

# Pick a good initial layout if coupling_map is not already satisfied
# otherwise keep it as q[i]->q[i].
# TODO: move this inside mapper pass.
initial_layouts = []
for dag in dags:
if (initial_layout is None and not backend.configuration()['simulator']
and not _matches_coupling_map(dag, coupling_map)):
_initial_layout = _pick_best_layout(dag, backend)
initial_layouts.append(_initial_layout)
dags = _transpile_dags(dags, basis_gates=basis_gates, coupling_map=coupling_map,
initial_layouts=initial_layouts, seed=seed,
pass_manager=pass_manager)

# step 3: Making a qobj
qobj = _dags_2_qobj(dags, backend_name=backend_name,
config=config, shots=shots, max_credits=max_credits,
qobj_id=qobj_id, basis_gates=basis_gates,
coupling_map=coupling_map, seed=seed)
qobj_config = deepcopy(config or {})
qobj_config.update({'shots': shots,
'max_credits': max_credits,
'memory_slots': 0})

return qobj
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)

def _circuits_2_dags(circuits):
"""Convert a list of circuits into a list of dags.
qobj.config.n_qubits = max(experiment.config.n_qubits for
experiment in qobj.experiments)

Args:
circuits (list[QuantumCircuit]): circuit to compile
return qobj


def _build_exp_parallel(idx, circuits, backend, initial_layout=None,
basis_gates='u1,u2,u3,cx,id', config=None,
coupling_map=None, seed=None, pass_manager=None):
"""Builds a single Qobj experiment. Usually called in parallel mode.
Args:
idx (int): Index of circuit in circuits list.
circuits (list): List of circuits passed.
backend (BaseBackend or str): a backend to compile for
initial_layout (list): initial layout of qubits in mapping
basis_gates (str): comma-separated basis gate set to compile to
config (dict): dictionary of parameters (e.g. noise) used by runner
coupling_map (list): coupling map (perhaps custom) to target in mapping
initial_layout (list): initial layout of qubits in mapping
seed (int): random seed for simulators
pass_manager (PassManager): pass manager instance for the tranpilation process
If None, a default set of passes are run.
Otherwise, the passes defined in it will run.
If contains no passes in it, no dag transformations occur.
Returns:
list[DAGCircuit]: the dag representation of the circuits
to be used in the transpiler
experiment: An instance of an experiment to be added to a Qobj.
"""
dags = parallel_map(DAGCircuit.fromQuantumCircuit, circuits)
return dags

circuit = circuits[idx]
dag = DAGCircuit.fromQuantumCircuit(circuit)

if (initial_layout is None and not backend.configuration()['simulator']
and not _matches_coupling_map(dag, coupling_map)):
_initial_layout = _pick_best_layout(dag, backend)
else:
_initial_layout = initial_layout

dag = _transpile_dags_parallel(0, [dag], [_initial_layout],
basis_gates=basis_gates, coupling_map=coupling_map,
seed=seed, pass_manager=pass_manager)

experiment = _dags_2_qobj_parallel(
dag, basis_gates=basis_gates, config=config, coupling_map=coupling_map)

return experiment


def _transpile_dags(dags, basis_gates='u1,u2,u3,cx,id', coupling_map=None,
Expand Down

0 comments on commit 98e78d0

Please sign in to comment.