Unnecessary zero-probability states returned by Sampler, with different behaviour to Aer Sampler
See original GitHub issueEnvironment
- 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 thebinary_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 theStatevector
in Terra (which is called by theSampler
). - 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:
- Created 10 months ago
- Reactions:1
- Comments:6 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Also Terraβs Sampler should use
probabilities_dict
method instead ofprobabilities
. https://github.com/Qiskit/qiskit-terra/blob/main/qiskit/quantum_info/states/quantum_state.py#L216Yes, it might be good to remove bits with 0 prob at
QuasiDistribution
side. It can be a good first issue too.