I am running variational algorithms using QuasmSimulator, which means I am performing a classical optimization where the cost function is computed running a quantum circuit. Moreover, for the optimizer to converge I need high accuracy, which implies a large number of shots.

These two things combined make a run last for days on my laptop, even for a few qubits. Thus, I am trying to run shots in parallel to save some time. Qiskit has a backend option for this:

from qiskit.providers.aer import QasmSimulator
backend = QasmSimulator(method="automatic", max_parallel_threads=6, max_parallel_shots=6)

... the rest of the code...

job = execute(circ, backend, shots=nshots)

However, when setting this option I see no difference, neither in runtime nor in CPU usage from Window's task manager. I think the problem could be that normally a python script just uses one core, so when Qiskit runs it "thinks" that indeed you have one core only. Any suggestion?

I am using Qiskit 0.23.0

(Note that I asking specifically about parallel shots and not parallel circuits, although that might also help with another issue I have)

  • $\begingroup$ Not answering your question, this is a comment about simulation for calculating an expectation value. Depending on your circuit (e.g. does it contain intermediate measurements) and what you're looking for (e.g. do you simulate a noisy device), there are cases where you can run a single shot and use the expectation value snapshot, which calculates <psi|O|psi>, where |psi> is the state and O is the obervable. $\endgroup$ – Yael Ben-Haim Nov 16 at 9:29
  • $\begingroup$ Also, you may find the variational solvers in Qiskit Aqua useful $\endgroup$ – Yael Ben-Haim Nov 16 at 9:33
  • $\begingroup$ You can get short runtime indeed if you just use the state vector simulator, which is exact and performs matrix-vector multiplications etc. Perhaps that's what you mean by "one shot". However, if you are simulating a QC functioning, obviously it's impossible to get a one-shot result. In that case, you would just get a reading that is 1 or 0 which does not hold any info. You need to sample many times until you have a meaningful ration of 1s/0s. Or did I misunderstood you? $\endgroup$ – Enrico Nov 16 at 10:13
  • $\begingroup$ Yes, you understood correctly $\endgroup$ – Yael Ben-Haim Nov 16 at 12:53
  • $\begingroup$ I am also interested in statistical error introduced by a finite sampling of the expectation value, so, unfortunately, I can't bypass the problem as you suggest. $\endgroup$ – Enrico Nov 16 at 13:28

There is a question which is quite alike to your problem in Quantum Computing-stack exchange. I have answered it before, you may see that.

I think the number of shots nearly does not change the running time(although I have been voted -1), you can see the problem and answers yourself.

Just for short, I think after qiskit evaluates the state vector, it gives you measurement results based on that state vector, instead of evaluate state vector $n$ times and gives you one measurement result each time(I guess, since if you import the time package and observe the time consumed about different shots you won't see a pattern that the consumed time grows with the number of shots).

| improve this answer | |
  • $\begingroup$ What you say it's partially true but I don't think it holds for my case because I am running with more than an order of magnitude more shots (order 10^6). For instance, in your example, it's 5 second out of 45 due to the number of shot increase. In my case that would become a 50 seconds increase which effectively doubles the runtime. $\endgroup$ – Enrico Nov 16 at 10:20
  • $\begingroup$ I know what you mean, even you import np.random.random to generate some numbers, the process takes time, right? In my earlier answer, I tested a 10^5 case for about 20 qubits, but if I enlarge the number of shots to 10^6 it can hardly change much because in my case the main part(even the overwhelming part) is still to get the state vector of the 20-qubit state. $\endgroup$ – Yitian Wang Nov 16 at 11:21
  • $\begingroup$ But when you refer to using multiple cores to run multiple codes, I am not familiar with such a thing (like how many cores python use by default), so this answer might just be a bit of tiny advice. $\endgroup$ – Yitian Wang Nov 16 at 11:23
  • $\begingroup$ To your first comment: I am quite sure that's not the case. If you are curious, I invite you to run the same experiment with 1e6 shots and check. When it comes to python, I am no computer scientist myself but you can just check that it uses one processor by opening the task manager while running any intensive task and you'll see what I mean. $\endgroup$ – Enrico Nov 16 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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