Qiskit / qiskit-aer

Aer is a high performance simulator for quantum circuits that includes noise models
https://qiskit.github.io/qiskit-aer/
Apache License 2.0
483 stars 358 forks source link

Sampler with shots=None does not behave as documented when number of qubits is 10 or more #1991

Open kevinsung opened 10 months ago

kevinsung commented 10 months ago

It appears that when the number of qubits is 10 or more, running Sampler with shots=None doesn't actually calculate the exact probabilities. For example, the following raises AssertionError. The true behavior should be documented.

import numpy as np
from qiskit.circuit.library import EfficientSU2
from qiskit.quantum_info import SparsePauliOp
from qiskit_aer.primitives import Sampler
from qiskit_aer.noise import NoiseModel, depolarizing_error

n_qubits = 10
circuit = EfficientSU2(n_qubits)
circuit.measure_all()

observable = SparsePauliOp("Z" * n_qubits)
params = [0.1] * circuit.num_parameters

noise_model = NoiseModel()
cx_depolarizing_prob = 0.02
noise_model.add_all_qubit_quantum_error(
    depolarizing_error(cx_depolarizing_prob, 2), ["cx"]
)

noisy_sampler = Sampler(backend_options={"noise_model": noise_model})
job = noisy_sampler.run(circuit, params, shots=None)
quasis_1 = job.result().quasi_dists[0]
job = noisy_sampler.run(circuit, params, shots=None)
quasis_2 = job.result().quasi_dists[0]

for b in quasis_1 | quasis_2:
    np.testing.assert_allclose(quasis_1[b], quasis_2[b])
ikkoham commented 10 months ago

There are two types of noise model simulations: trajectory and density matrix. In this case, the density matrix should be used. In the case of statevector, it is computed in trajectory, so the noise introduced is different for each run.

noisy_sampler = Sampler(backend_options={"noise_model": noise_model, "method": "density_matrix"})

The noise introduced by each run is different for statevector, but it is not wrong in that it returns an exact probability distribution for the final quantum state.

kevinsung commented 10 months ago

So for qubits < 10 it uses the density matrix and for qubits >= 10 it uses the trajectories. Is that right? Is this behavior documented somewhere? I did not see it at https://qiskit.org/ecosystem/aer/stubs/qiskit_aer.primitives.Sampler.html.

ikkoham commented 10 months ago

TBH, I'm not sure which method is chosen by the simulator method option automatic https://qiskit.org/ecosystem/aer/stubs/qiskit_aer.AerSimulator.html.