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

Inconsistent Statevector Results with seed_simulator #2276

Open
vili-1 opened this issue Dec 12, 2024 · 2 comments
Open

Inconsistent Statevector Results with seed_simulator #2276

vili-1 opened this issue Dec 12, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@vili-1
Copy link

vili-1 commented Dec 12, 2024

Informations

  • Qiskit Aer version: 0.15.1
  • Python version: 3.12.6
  • Operating system: 24.1.0 Darwin Kernel Version 24.1.0

What is the current behavior?

There appears to be an issue with Qiskit Aer’s statevector_simulator where statevectors produced by different seed_simulator values are inconsistent.
The inconsistency can create challenges in deterministic simulations that rely on seed_simulator to generate reproducible results.

Steps to reproduce the problem

from qiskit import Aer, QuantumCircuit, transpile
import numpy as np

def try_angles_qiskit():
    qc_test = QuantumCircuit(2, 2)
    
    qc_test.u(np.pi, 1, 1, 0)  # U gate with angle np.pi
    qc_test.rx(np.pi**50, 0)    # RX gate with a large angle
    qc_test.ry(np.pi**50, 0)    # RY gate with a large angle
    qc_test.u(np.random.rand() * 2 * np.pi, np.random.rand() * 2 * np.pi, np.random.rand() * 2 * np.pi, 0)
    qc_test.u(np.random.rand() * 2 * np.pi, np.random.rand() * 2 * np.pi, np.random.rand() * 2 * np.pi, 1)
    qc_test.cx(0, 1)  # CNOT between qubit 0 and qubit 1

    # Measure the qubits
    qc_test.measure_all()

    simulator = Aer.get_backend('statevector_simulator')
    transpiled_qc = transpile(qc_test, simulator)

    seeds = ["42", "-42", (42), -42, 42.4, 42, b"42", "\t42\n", 2**63-1, 0, 0.0, -0.0, 1.0, -1.0, 1e3, -1e3, True, None, 330]
    results = []
    for seed in seeds:
        job = simulator.run(transpiled_qc, seed_simulator=seed, shots=1024)
        results.append(job.result().get_statevector())

    for seed, sv in zip(seeds, results):
        print(f"\nSeed: {seed} \n{sv}")
    print("Are all statevectors equal?", all(np.allclose(results[0], r) for r in results))

    print(qc_test)

    return qc_test

try_angles_qiskit()

Observed Results:

For most seed values ("42", "-42", (42), -42, 42.4, 42, b"42", "\t42\n", 2**63-1, 0, 0.0, -0.0, 1.0, -1.0, 1e3, True, None, 330), the statevector remains the same:

Statevector([-0.        +0.j        ,  0.36316625+0.93172436j,
              0.        +0.j        , -0.        +0.j        ],
            dims=(2, 2))

However, the statevector produced by the seed -1000.0 is different:

Statevector([-0.       +0.j        ,  0.       +0.j        ,
              0.6132849+0.78986178j, -0.       +0.j        ],
            dims=(2, 2))

What is the expected behavior?

The statevector should be deterministic for all seed values, meaning that running the simulation with the same seed value should produce the same result every time. The output should be consistent for all seeds.

Suggested solutions

@vili-1 vili-1 added the bug Something isn't working label Dec 12, 2024
@jakelishman
Copy link
Member

There's a few things going on here:

  1. you must be using a broken or very old Qiskit installation, because from qiskit import Aer hasn't worked for a long time (it should be from qiskit_aer import Aer)
  2. your huge Rx/Ry angles are meaningless noise - $\pi^{50} \approx 7.20267\dotsi,\times 10^{24}$, and at that size of value, the difference between consecutive floats is $2^{30}$, so humongously bigger than any sort of precision needed to represent an angle (not that it matters for this issue).
  3. if you ask for the statevector after you've taken the measurement, you're going to get the collapsed form, which is subject to seed-based randomisation because it depends on the measurement taken. Remove the measure_all and you should get the results you expect.

Just for the future, too - if there's more bugs, it's helpful if you can ensure that you're providing fully deterministic reproducers. It doesn't matter here because the issues are elsewhere, but in general, putting angle-randomisation calls in the middle of circuit generation is likely to mean that we'll not be able to reproduce what you saw (and if you're looking into things, you probably want to be able to control that to reproduce the same things later anyway).

@vili-1
Copy link
Author

vili-1 commented Dec 13, 2024

Ah, sorry, I do use from qiskit_aer import Aer

I removed the measure_all instruction and, as you suggested, the statevector is consistent across simulations. Thx.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants