2
$\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 collab.

AttributeError: 'AerJob' object has no attribute 'get_counts'

$\endgroup$
2
$\begingroup$

This should fix your problem:

result = execute(circuit, backend=simulator).result()
plot_histogram(result.get_counts(circuit))
$\endgroup$
  • $\begingroup$ Yes, this one fixed the problem too. $\endgroup$ – Rishwi binnu Sep 10 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$
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, privacy policy and cookie policy

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