documentation icon indicating copy to clipboard operation
documentation copied to clipboard

Fix incorrect `meas` attribute in example code for sampler

Open srdtrk opened this issue 1 year ago • 4 comments

URL to the relevant documentation

https://docs.quantum.ibm.com/guides/get-started-with-primitives#4-invoke-the-sampler-and-get-results

Select all that apply

  • [ ] typo
  • [x] code bug
  • [ ] out-of-date content
  • [ ] broken link
  • [ ] other

Describe the fix.

Description

The code snippet in the Qiskit Runtime Sampler documentation appears to contain an error when retrieving measurement results.

The current example provided:

print(f"Counts for the meas output register: {pub_result.data.meas.get_counts()}")

produces an error because DataBin does not have an attribute named meas.

To successfully retrieve the counts, the following code should be used instead:

print(f"Counts for the meas output register: {pub_result.data.c.get_counts()}")

This change allows the code to run correctly by accessing the appropriate slot for the measurement output.

I’d love to open a PR to contribute this fix if agreed.

srdtrk avatar Oct 06 '24 17:10 srdtrk

Hello and thanks for opening the issue! Yes, please do go ahead and work on this - I've assigned you.

abbycross avatar Oct 08 '24 13:10 abbycross

The field after pub_result.data is the name of the classical register used. In the code example above, it uses circuit.measure_all(), which by default name the classical register meas.

Now it's possible that this is not true for all Qiskit versions. @srdtrk, do you know which Qiskit version you were using when you encountered this?

jyu00 avatar Oct 10 '24 21:10 jyu00

Thanks for info @jyu00. I'm actually using qiskit v1.2.4 and qiskit-ibm-runtime v0.30.0. I'm not experienced enough in qiskit or python to actually find the code-snippet in qiskit. (Although the sampler should be in qiskit-ibm-runtime). This is the code snippet that works for me in my notebook:

# make a new simulator object
backend = AerSimulator()
sampler = Sampler(backend)

# run the experiment
job = sampler.run([qcEx1])

# get the results
result = job.result()[0]

# interpret the results as a "counts" dictionary
result.data.c.get_counts()

and the imports are as follows:

### Import the required packages

from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator
from qiskit_ibm_runtime import QiskitRuntimeService
from qiskit_ibm_runtime import SamplerV2 as Sampler
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager

srdtrk avatar Oct 11 '24 18:10 srdtrk

@srdtrk the names of the classical registers depend on how you construct your circuit (in this case, your qcEx1). For example, if I do

from qiskit import QuantumCircuit

qc = QuantumCircuit(2, 2)
qc.measure([0, 1], [0, 1])
print(qc.cregs)    # <--- this prints [ClassicalRegister(2, 'c')]

The code above shows the classical register is named c. However, if I use measure_all(), e.g.

qc = QuantumCircuit(2)
qc.measure_all()
print(qc.cregs)    # <--- this prints [ClassicalRegister(2, 'meas')]

The classical register name is meas.

The tutorial uses measure_all() and hence the counts are in pub_result.data.meas.get_counts().

jyu00 avatar Oct 11 '24 19:10 jyu00

I see. Thanks for the clarification @jyu00

srdtrk avatar Oct 14 '24 02:10 srdtrk