Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Probabilities >1 in the sampler (and statevector) #9761

Closed
Cryoris opened this issue Mar 9, 2023 · 0 comments · Fixed by #9762
Closed

Probabilities >1 in the sampler (and statevector) #9761

Cryoris opened this issue Mar 9, 2023 · 0 comments · Fixed by #9762
Labels
bug Something isn't working mod: primitives Related to the Primitives module mod: quantum info Related to the Quantum Info module (States & Operators)

Comments

@Cryoris
Copy link
Contributor

Cryoris commented Mar 9, 2023

Environment

  • Qiskit Terra version: main @ 5c49d14
  • Python version: 3.10
  • Operating system: macOS Ventura

What is happening?

The Statevector.probabilities method sometimes contains roundoff errors, leading to probabilities larger than 1. This breaks the Sampler if shots are emulated, as larger than 1 probabilities are not valid inputs to NumPy's multinomial library.

How can we reproduce the issue?

from qiskit.circuit import QuantumCircuit
from qiskit.primitives import Sampler
import numpy as np

np.random.seed(1)
sampler = Sampler(options={"shots": 1024})

for _ in range(1000):
    try:
        angle = np.random.random(1)[0]
        # this angle breaks on my laptop for example:
        # angle = 0.10233442882782584
        qc = QuantumCircuit(1)
        qc.ry(angle, 0)
        qc.ry(-angle, 0)
        qc.measure_all()
        _ = sampler.run(qc).result()
    except Exception as exc:
        print("Failed on", angle)

The error raised is

  File "/Users/jul/Qiskit/qiskit-terra/qiskit/primitives/sampler.py", line 130, in _call
    counts = rng.multinomial(shots, np.fromiter(prob_dict.values(), dtype=float))
  File "_generator.pyx", line 3916, in numpy.random._generator.Generator.multinomial
  File "_common.pyx", line 391, in numpy.random._common.check_array_constraint
  File "_common.pyx", line 377, in numpy.random._common._check_array_cons_bounded_0_1
ValueError: pvals < 0, pvals > 1 or pvals contains NaNs

What should happen?

The probabilities should not exceed 1.

Any suggestions?

Clip the probabilities.

@Cryoris Cryoris added the bug Something isn't working label Mar 9, 2023
@Cryoris Cryoris added the mod: primitives Related to the Primitives module label Mar 9, 2023
@t-imamichi t-imamichi added the mod: quantum info Related to the Quantum Info module (States & Operators) label Mar 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mod: primitives Related to the Primitives module mod: quantum info Related to the Quantum Info module (States & Operators)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants