question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unnecessary zero-probability states returned by Sampler, with different behaviour to Aer Sampler

See original GitHub issue

Environment

  • Qiskit Terra version: 0.23.0.dev0+ad07847
  • Python version: 3.10.4
  • Operating system: macOS Monterey 12.6 M1 2020

What is happening?

  • Sampler returns a dictionary with every possible state, even if some states have a probability 0 of being sampled. As the number of qubits is increased, the size of this dictionary will scale exponentially.
  • The behaviour is different to the qiskit_aer Sampler, which does not return states with probability 0. Importantly, since the number of qubits is not saved, this results in a faulty conversion to binary strings in qiskit_aer using the binary_probabilities() method. This is probably because the length of the binary strings is calculated based on the number of returned states instead of the number of qubits, which in Aer is a subset of all possible states.

How can we reproduce the issue?

from qiskit_aer.primitives import Sampler as aer_sampler
from qiskit.primitives import Sampler as terra_sampler

qc = QuantumCircuit(4)
qc.h(0)
qc.measure_all()

terra_job = terra_sampler().run([qc], [], shots=10)
terra_quasis = terra_job.result().quasi_dists[0]
terra_bin_probs = terra_quasis.binary_probabilities()
print('TERRA RESULTS:')
print('\n', terra_quasis)
print('\n', terra_bin_probs)

aer_job = aer_sampler(backend_options={"method": "statevector"}).run([qc], [], shots=100)
aer_quasis = aer_job.result().quasi_dists[0]
aer_bin_probs = aer_quasis.binary_probabilities()
print('\nAER RESULTS:')
print('\n', aer_quasis)
print('\n', aer_bin_probs)

TERRA RESULTS:

{0: 0.5, 1: 0.5, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0, 6: 0.0, 7: 0.0, 8: 0.0, 9: 0.0, 10: 0.0, 11: 0.0, 12: 0.0, 13: 0.0, 14: 0.0, 15: 0.0}

{β€˜0000’: 0.5, β€˜0001’: 0.5, β€˜0010’: 0.0, β€˜0011’: 0.0, β€˜0100’: 0.0, β€˜0101’: 0.0, β€˜0110’: 0.0, β€˜0111’: 0.0, β€˜1000’: 0.0, β€˜1001’: 0.0, β€˜1010’: 0.0, β€˜1011’: 0.0, β€˜1100’: 0.0, β€˜1101’: 0.0, β€˜1110’: 0.0, β€˜1111’: 0.0}

AER RESULTS:

{1: 0.54, 0: 0.46}

{β€˜1’: 0.54, β€˜0’: 0.46}

What should happen?

We should see only the non-zero probability states in Terra, and a correct conversion to binary in Aer (i.e. matches the number of qubits):

TERRA RESULTS:

{0: 0.5, 1: 0.5}

{β€˜0000’: 0.5, β€˜0001’: 0.5}

AER RESULTS:

{1: 0.54, 0: 0.46}

{β€˜0001’: 0.54, β€˜0000’: 0.46}

Any suggestions?

  • Since the "statevector" method in Aer doesn’t return zero probability states, the same steps could be applied to the Statevector in Terra (which is called by the Sampler).
  • The number of qubits could be saved in the metadata of the SamplerResult, such that Aer can properly do the conversion to binary probabilities.

Issue Analytics

  • State:open
  • Created 10 months ago
  • Reactions:1
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ikkohamcommented, Dec 1, 2022

Also Terra’s Sampler should use probabilities_dict method instead of probabilities. https://github.com/Qiskit/qiskit-terra/blob/main/qiskit/quantum_info/states/quantum_state.py#L216

1reaction
t-imamichicommented, Nov 30, 2022

Yes, it might be good to remove bits with 0 prob at QuasiDistribution side. It can be a good first issue too.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AerSimulator - Qiskit
The final state of the simulator can be saved to the returned Result object by appending the save_state() instruction to a quantum circuit....
Read more >
User-Defined Sampler β€” Optuna 2.0.0 documentation
A sampler samples a parameter value from the distribution. The sampled value is returned to the caller of the suggest API and evaluated...
Read more >
Importance Sampling Policy Evaluation with an Estimated ...
tion is the task of evaluating the expected return of one policy with data generated by a different, behavior policy. Importance sampling isΒ ......
Read more >
Using sampler states - Unity - Manual
Sample (sampler_MainTex, uv);. However, this way, a shader could be written to β€œreuse” samplers from other textures, while sampling more than one texture....
Read more >
SAMPLING METHODS
Learn the reasons for sampling. Develop an understanding about different sampling methods. Distinguish between probability & non probability sampling.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found