1
$\begingroup$

I have a circuit that I want to perform measurements on. Here is the code I'm running:

qc1.measure([0,1,2],[0,1,2])
backend=FakeAlmadenV2()    
target = backend.target
pm = generate_preset_pass_manager(target=target, optimization_level=3)
circuit_isa = pm.run(qc1)
circuit_isa.draw(output="mpl", idle_wires=False, style="iqp", filename='img4.png')
# To run on local simulator:
#   1. Use the SatetvectorSampler from qiskit.primitives instead
sampler = Sampler(backend=backend)
sampler.options.default_shots = 10_000
result = sampler.run([circuit_isa]).result()
dist = result[0].data.meas.get_counts()
plot_distribution(dist, filename='dist.png')

This gives me the error:

AttributeError: 'DataBin' object has no attribute 'meas'

However, the same code works perfectly if I use measure_all() instead of measure().

$\endgroup$

1 Answer 1

2
$\begingroup$

This is one of the biggest issues with the latest version of Qiskit (v1.x).

To retrieve your results, the data inside the PubResult object (which you are currently extracting by doing result[0].data) contains an attribute which is named after the classical register. When you use the measure_all() method, the default name qiskit uses for the classical register is meas, so that's why your code works in that case.

On the other hand, if you use the meas() method and pass register numbers, qiskit names those registers c by default, so to extract your results you must replace this line of code:

dist = result[0].data.meas.get_counts()

with:

dist = result[0].data.c.get_counts()

Furthermore, if you were to use a ClassicalRegister with some other name (let's say cr), you would have to then again change your code to reflect that.

This is super problematic because Qiskit is now making the names of attributes depend on register-name values. My understanding is that this is a big "no-no" in Python since it can lead to tons of bugs in people's code (like in your case), so hopefully they will fix this in the near future.

$\endgroup$
1

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.