4

I'm kinda new to qiskit and I find really fascinating its parallelization capabilities, then I'm trying to creating all the needed circuits for my application at once and transpile, assemble and execute them all at once using the execute method.

Basically,

qcircuits = []
# construct my circuits and append to qcircuits
# ...
backend: backend: BaseBackend = qiskit.Aer.get_backend('qasm_simulator')

n_shots = 1024 job = qiskit.execute( qcircuits, backend, optimization_level=optimization_level, shots=n_shots )

counts = job.result().get_counts()

Nevertheless, I noticed that the job.result().get_couts() output is not deterministic as, I guess, it runs all the circuits using a parallel_map and appends in the returned list in the order they finish. Is there any way to force the execute method respecting the order of qcircuits ? If it is not the case, is there any way to label the execution results so that I can sort them myself afterwards ? Thanks in advance.

ryanhill1
  • 2,623
  • 1
  • 11
  • 39
mpro
  • 527
  • 2
  • 12

1 Answers1

1

Each qiskit.QuantumCircuit has a name attribute that is also accessible through each qiskit.result.Result. So, you can do the following to match the circuits to the measurement counts after running in parallel:

for circuit in qcircuits:
    print(circuit.name)

result_dict = jobs.result().to_dict()["results"] result_counts = jobs.result().get_counts() for i in range(len(qcircuits)): name = result_dict[i]["header"]["name"] counts = result_counts[i]
print(f"{name}: {counts}")

Before running your circuits, you could also assign your own descriptive names, e.g.

for i in range(len(qcircuits)):
    qcircuits[i].name = "mycircuit"+str(i)

Edit:

Combining the ideas above to show how to order the results:

# number input circuits in ascending order
for i in range(len(qcircuits)):
    qcircuits[i].name = "circuit_"+str(i)

job = qiskit.execute(qcircuits, backend, ...)

result_dict = job.result().to_dict()["results"] result_counts = job.result().get_counts()

initialize list to store ordered results

results_ordered = [None] * len(qcircuits)

for i in range(len(qcircuits)): name = result_dict[i]["header"]["name"] n = int(name.split('_')[1]) # index of circuit in input list results_ordered[n] = result_counts[i] # add to result list at same index

ryanhill1
  • 2,623
  • 1
  • 11
  • 39