4
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Are you looking for a way to do so that still allows you to run in parallel? Because if not, and running the circuits sequentially is ok, you could do jobs = [qiskit.execute(qcircuit, backend, ...) for qcircuit in qcircuits] followed by counts = [job.result().get_counts() for job in jobs], but obviously, this isn't ideal for large numbers of circuits. $\endgroup$
    – ryanhill1
    Dec 22, 2021 at 20:23

1 Answer 1

1
$\begingroup$

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  
$\endgroup$
3
  • $\begingroup$ Then you basically suggest using the name for the counts ordering ? I tried following this approach, nevertheless I keep getting nondeterministic results. Is there anything you suggest checking elsewhere ? I always create the circuit in the same fashion $\endgroup$
    – mpro
    Dec 24, 2021 at 12:22
  • $\begingroup$ Yes, naming the circuits won't change the results ordering. It's just a tool that you can use to then order the results afterward. I edited my answer with an example of how to do so. $\endgroup$
    – ryanhill1
    Dec 24, 2021 at 17:55
  • $\begingroup$ now I got it. Thanks! $\endgroup$
    – mpro
    Dec 25, 2021 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.