0
$\begingroup$

I'm trying to run my quantum circuit and plot the simulation result (counts) using the statevector simulator (to avoid 'shot noise'). However, instead of a 2-d histogram, the examples on Qiskit textbook associated with statevector simulator used 'plot_state_city' (which generates 3-d pictures). I'm wondering how can I generate the histogram. Here's part of my code:

enter image description here

The output statevector isn't the correct one, so the corresponding histogram (and counts) is not what I desired. However, if I use the qasm-simulator, I can get the result I want: enter image description here

Is there a way I can get the similar histogram using the statevector simulator? Thanks!!

$\endgroup$

2 Answers 2

2
$\begingroup$

It's a little difficult to tell without looking at your circuit, but I believe this is because your circuit contains a measurement in it.
(edit: if there were no measurements the qasm simulator wouldn't work, so you obviously have a measurement happening in your circuit)

The statevector_simulator simply gives you the state generated by your circuit. If you have a measurement in there, it will collapse any superposition generated prior to it and you will only see the outcome of the measurement. As you have rightly discovered, using this to plot a histogram is of no use because the get_counts method will return something that looks like {'measurement_outcome' : 1}. However, the qasm_simulator performs as many measurement shots as you tell it to and aggregates the results in counts, which makes it useful for plotting histograms.

So if you want to plot a histogram and you need to use the statevector_simulator, remove the measurements from the circuit. If you don't want to mess with the circuit itself, there's a handy method called remove_final_measurements that you can pass to execute as follows:

execute(qc.remove_final_measurements(inplace=False), backend=simulator)
$\endgroup$
4
  • $\begingroup$ Thank you so much for the explanation, so can I understand the statevector_simulator simulator as it only gives a single shot so the measurement at the end of the circuit collapses the wavefunction? I deleted the measurement at the end of my circuit but the 'counts' still have the same result as before. Should I plot the histogram without using counts? Is there a way I can plot the 'sv' in my code? Thanks!! $\endgroup$
    – ZR-
    Sep 11, 2020 at 15:28
  • 1
    $\begingroup$ You're welcome! When you say you want to plot sv, I assume that means you want to plot the probabilities associated with the statevector? You don't have to do anything more than counts = execute(qc, simulator).result().get_counts() and pass that to plot_histogram... $\endgroup$
    – agiri
    Sep 11, 2020 at 16:32
  • $\begingroup$ Thanks! Yes I'm hoping to plot the probabilities associated with the statevector. I tried that and deleted the measurement in my circuit but it still shows a single state 0000 with probability 1. $\endgroup$
    – ZR-
    Sep 11, 2020 at 17:13
  • $\begingroup$ That's funny, I just tried it out with the following code and it works fine: q = QuantumCircuit(1) q.h(0) plot_histogram(execute(q, backend=statevector_sim).result().get_counts()) Perhaps you could share your code? $\endgroup$
    – agiri
    Sep 12, 2020 at 2:59
1
$\begingroup$

If I understand correctly, Statevector_simulator is a perfect simulator. It's going through all the linear algebra, do the actual math, instead of mimicking the quantum computer. Therefore, you won't have the probabilities distribution like qasm_simulator, or like when you run on the quantum hardware. The statevector simulator will output the statevector of the final quantum state, as the name suggested.

$\endgroup$

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.