5
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'
luciano
  • 5,763
  • 1
  • 12
  • 34
Rishwi binnu
  • 163
  • 1
  • 10

4 Answers4

4

This should fix your problem:

result = execute(circuit, backend=simulator).result()
plot_histogram(result.get_counts(circuit))
Arthur-1
  • 525
  • 3
  • 9
1

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.

luciano
  • 5,763
  • 1
  • 12
  • 34
Shalini D
  • 11
  • 2
1

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

Paul Nation
  • 2,239
  • 8
  • 8
0

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)
Rishwi binnu
  • 163
  • 1
  • 10