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
500 stars 361 forks source link

Sampler with `shots=None` supports neither mid-circuit measurements nor unwritten classical bits #1810

Open garrison opened 1 year ago

garrison commented 1 year ago

Informations

What is the current behavior?

Sampler with shots=None only works if each classical bit is set by a measurement at the end of the circuit.

Steps to reproduce the problem

from qiskit_aer.primitives import Sampler as AerSampler
qc = QuantumCircuit(2, 2)
qc.h(0)
qc.cx(0, 1)
qc.measure(0, 0)
qc.reset(0)
qc.measure(0, 1)
AerSampler().run(qc, shots=None).result().quasi_dists

raises

QiskitError: 'Some classical bits are not used for measurements. The number of classical bits 2, the used classical bits {1}.'

What is the expected behavior?

Sampler should support calculating exact probabilities for circuits with arbitrary measurements

Suggested solutions

My understanding is that this will not be simple to fix in Aer. However, I wanted to have an open issue to refer to. In the meantime, I have created my own Sampler implementation based on Qiskit Terra's Statevector that supports mid-circuit measurements correctly: https://github.com/Qiskit-Extensions/circuit-knitting-toolbox/blob/main/circuit_knitting_toolbox/utils/simulation.py

Related Qiskit Terra issue: https://github.com/Qiskit/qiskit-terra/issues/9657

hhorii commented 1 year ago

As you mentioned, we need time to support quasi_dists of a dynamic circuit in Aer. Instead, I think, your ExactSample can use AerStatevector that inherits Statevector of quantum_info.