qiskit-aer icon indicating copy to clipboard operation
qiskit-aer copied to clipboard

Unexpected Error Thrown by `StatevectorSampler`

Open weucode opened this issue 1 year ago • 7 comments

Informations

  • Qiskit Aer version:0.15.0
  • Python version:Python 3.12.4
  • Operating system:Ubuntu 22.04.3 LTS

What is the current behavior?

When I use measure_active to measure all used qubits, an error is thrown:

Traceback (most recent call last):
  File "/home/sampler_usage.py", line 19, in <module>
    print(job.result()[0].data.meas.get_int_counts())
          ^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'DataBin' object has no attribute 'meas'

However, this error does not occur when using measure_all or run this circuit using AerSimulator directly.

Steps to reproduce the problem

Here is the code that triggers the error:

from qiskit.circuit import QuantumCircuit
from qiskit import transpile

qc = QuantumCircuit(1)
qc.x(0)
qc.measure_active()

from qiskit_aer import AerSimulator

backend1 = AerSimulator()
qc1 = transpile(qc, backend=backend1)
print(qc1)

from qiskit.primitives import StatevectorSampler

statevectorsampler = StatevectorSampler()
pub = (qc1)
job = statevectorsampler.run([pub])
print(job.result()[0].data.meas.get_int_counts())

What is the expected behavior?

No errors should be thrown.

Suggested solutions

weucode avatar Sep 09 '24 14:09 weucode

You can get counts by

print(job.result()[0].data.measure.get_int_counts())

anyway, please consider using SamplerV2 of Aer because StatevectorSampler does not use Aer backend.

from qiskit_aer.primitives import SamplerV2

doichanj avatar Sep 10 '24 02:09 doichanj

You can get counts by

print(job.result()[0].data.measure.get_int_counts())

This works, but I’ve noticed that measure_all must be used with data.meas and measure_active must be used with data.measure. Otherwise, similar errors will be thrown. Is this correct?

anyway, please consider using SamplerV2 of Aer because StatevectorSampler does not use Aer backend.

Also, thank you for the suggestion about using SamplerV2 from Aer. Could you clarify if qiskit_aer.primitives.SamplerV2 is a newer version of the samplers? I’m still a bit unclear on the differences between qiskit.primitives.StatevectorSampler and qiskit_aer.primitives.SamplerV2.

weucode avatar Sep 10 '24 08:09 weucode

By the way, the phenomenon about measure_all and measure_active occurs in both qiskit.primitives.StatevectorSampler and qiskit_aer.primitives.SamplerV2.

weucode avatar Sep 10 '24 09:09 weucode

The implementation of StatevectorSampler and SamplerV2 are almost the same, so the same issue occurs on both.

I could not find source that measure_all is labeled as data.meas

doichanj avatar Sep 12 '24 06:09 doichanj

Here is a detailed program:

from qiskit.circuit import QuantumCircuit
from qiskit import transpile

qc = QuantumCircuit(1)
qc.x(0)
qc.measure_all()

from qiskit_aer import AerSimulator

backend1 = AerSimulator()
qc1 = transpile(qc, backend=backend1)

job0 = backend1.run(qc1, shots=10000)

from qiskit.primitives import StatevectorSampler

statevectorsampler = StatevectorSampler()
pub = (qc1)
job = statevectorsampler.run([pub])
print(job.result()[0].data.meas.get_int_counts())

The output is:

{1: 1024}

However, when I replace the last line with print(job.result()[0].data.measure.get_int_counts()), the output is:

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

weucode avatar Sep 12 '24 07:09 weucode

I just want to know is there any documents that says meas is labeled

doichanj avatar Sep 12 '24 07:09 doichanj

I couldn't find any content explaining that users can obtain measurement results by accessing meas or measure from a DataBin instance.

For reference, here is a page with an example that uses meas:
Simulate with Qiskit SDK Primitives

When I use job.result()[0].data.items() to print the contents, the circuit with measure_all shows:

key: meas value: BitArray(<shape=(), num_shots=1024, num_bits=1>)

And the circuit with measure_active shows:

key: measure value: BitArray(<shape=(), num_shots=1024, num_bits=1>)

weucode avatar Sep 12 '24 08:09 weucode