5
$\begingroup$
result = execute(circuit, backend=simulator)
plot_histogram(result.get_counts(circuit))

I used the above code to plot a histogram for a simple entanglement circuit. I got the error mentioned below. I am running the code in Google Colab.

AttributeError: 'AerJob' object has no attribute 'get_counts'
$\endgroup$
0

4 Answers 4

4
$\begingroup$

This should fix your problem:

result = execute(circuit, backend=simulator).result()
plot_histogram(result.get_counts(circuit))
$\endgroup$
1
  • $\begingroup$ Yes, this one fixed the problem too. $\endgroup$ Sep 10, 2019 at 16:01
1
$\begingroup$

Execute returns a job instance. The job has a result, job.result(), and you get counts from the results, ie job.result().get_counts()

$\endgroup$
1
$\begingroup$

You should include shots value. Shots is nothing but how many times the circuit going to run. Because Quantum computing is all about being probabilistic (i.e.) you can't be certain about the results till you measure and once measured qubits collapses to a particular state according to the gates you have used. So, what you are doing here is, you are measuring the circuit using qasm_simulator. so, you should definitely give shots. Shots can be any number. It can be 1000, 1024 and so on.

qc = QuantumCircuit(2)
qc.h([0])
qc.cx(0, 1)
qc.measure_all()
qc.draw('mpl')

Execute the above code in a separate cell for better results.

simulator = Aer.get_backend('qasm_simulator')
job = execute(qc, simulator , shots = 1000)
result = job.result()
counts=result.get_counts(qc)
plot_histogram(counts)

This is my version of the code and its working fine for me.

You should get output like this: 00 and 11 with graph.

$\endgroup$
0
$\begingroup$

I solved this problem by modifying the code to ...

counts=result.get_counts(circuit)
plot_histogram(counts)

When I am using a variable counts to store the output, I am able to plot it but unable to execute the code in the question. Entire code goes like this...

from qiskit import *
from qiskit.tools.visualization import plot_histogram
circuit.h(qr[0])
circuit.cx(qr[0], qr[1])
circuit.measure(qr,cr)
circuit.draw(output='mpl')
simulator = Aer.get_backend('qasm_simulator')
result = execute(circuit, backend=simulator)
counts=result.get_counts(circuit)
plot_histogram(counts)
$\endgroup$

Your Answer

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

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