1
$\begingroup$

I am need help in understanding about the measure_all() of the qiskit. The below code is correct even though measure_all() is not being used.

from qiskit import Aer
from qiskit import execute
from qiskit import QuantumCircuit


circ = QuantumCircuit(3,3)
circ.h(0)
circ.cx(0,1)
circ.cx(0,2)

backend = Aer.get_backend("qasm_simulator")


job = execute(circ, backend, shots = 100000)
result = job.result()
counts = result.get_counts(circ)
print(counts)

It would be great if someone can explain about the measure_all() concept, where to use it and where not to use it?

$\endgroup$
4
  • $\begingroup$ I tried your code on my machine. It gave me the error message: "No counts for experiment ..." $\endgroup$ Mar 17 at 8:10
  • $\begingroup$ Thank you @Egretta.Thula for the response. I tested the code on qiksit version 0.32.1, python version 3.8.18. It gives me result {'000': 100000} $\endgroup$
    – Manu
    Mar 17 at 8:31
  • $\begingroup$ I ran my test on version 0.46.0 $\endgroup$ Mar 17 at 8:36
  • $\begingroup$ Really not sure why things changes so much even for such a basic code. $\endgroup$
    – Manu
    Mar 17 at 9:08

1 Answer 1

2
$\begingroup$

You are noticing that measurement behavior is different in different versions of Qiskit. In qiskit==0.33.1, we have the following behavior:

import qiskit
print(qiskit.__qiskit_version__)
{'qiskit-terra': '0.19.1', 'qiskit-aer': '0.9.1', 'qiskit-ignis': '0.7.0', 'qiskit-ibmq-provider': '0.18.2', 'qiskit-aqua': '0.9.5', 'qiskit': '0.33.1', 'qiskit-nature': None, 'qiskit-finance': None, 'qiskit-optimization': None, 'qiskit-machine-learning': None}
from qiskit import Aer
from qiskit import execute
from qiskit import QuantumCircuit


circ = QuantumCircuit(3,3)
circ.h(0)
circ.cx(0,1)
circ.cx(0,2)

backend = Aer.get_backend("qasm_simulator")


job = execute(circ, backend, shots = 100000)
result = job.result()
counts = result.get_counts(circ)
print(counts)
{'000': 100000}

But in the next update qiskit==0.34.0 there is an error:

import qiskit
print(qiskit.__qiskit_version__)
{'qiskit-terra': '0.19.1', 'qiskit-aer': '0.10.1', 'qiskit-ignis': '0.7.0', 'qiskit-ibmq-provider': '0.18.3', 'qiskit-aqua': '0.9.5', 'qiskit': '0.34.0', 'qiskit-nature': None, 'qiskit-finance': None, 'qiskit-optimization': None, 'qiskit-machine-learning': None}
from qiskit import Aer
from qiskit import execute
from qiskit import QuantumCircuit


circ = QuantumCircuit(3,3)
circ.h(0)
circ.cx(0,1)
circ.cx(0,2)

backend = Aer.get_backend("qasm_simulator")


job = execute(circ, backend, shots = 100000)
result = job.result()
counts = result.get_counts(circ)
print(counts)
QiskitError: 'No counts for experiment "<qiskit.circuit.quantumcircuit.QuantumCircuit object at 0x1550e7790>"'

So your issue is not with the measure_all() concept per say, but rather you just need to keep notes of the versions in which these changes take place.

Clarification: It should also be noted that in the qiskit==0.33.1 case, the output {'000': 100000} is reflecting the fact that no measurements have been loaded in to the classical bits (so the values all remain zero). If a measurement statement is included, then this circuit will have GHZ state measurement statistics:

from qiskit import Aer
from qiskit import execute
from qiskit import QuantumCircuit


circ = QuantumCircuit(3,3)
circ.h(0)
circ.cx(0,1)
circ.cx(0,2)
circ.measure(range(3), range(3)) # actually measure the circuit

backend = Aer.get_backend("qasm_simulator")


job = execute(circ, backend, shots = 100000)
result = job.result()
counts = result.get_counts(circ)
print(counts)
{'000': 50094, '111': 49906}
$\endgroup$
1
  • $\begingroup$ Thank you @Nick Mertes for this great help. $\endgroup$
    – Manu
    Mar 17 at 21:18

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.