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

Sampler primitive in Terra does not support mid-circuit measurements #9657

Open
garrison opened this issue Feb 24, 2023 · 8 comments
Open

Sampler primitive in Terra does not support mid-circuit measurements #9657

garrison opened this issue Feb 24, 2023 · 8 comments
Labels
mod: primitives Related to the Primitives module type: feature request New feature or request

Comments

@garrison
Copy link
Member

What should we add?

The reference Sampler (in qiskit.primitives) does not support mid-circuit measurements. However, other Samplers, including those provided by qiskit-aer and qiskit-ibm-runtime, do support mid-circuit measurements.

I understand the desire to keep the reference implementation simple, but it is hardly a reference implementation if it only implements the Sampler for a subset of possible inputs.

This was previously discussed at #8708. My goal here is to open a centralized place for discussion going forward

@garrison garrison added the type: feature request New feature or request label Feb 24, 2023
@garrison
Copy link
Member Author

garrison commented Feb 24, 2023

As for the question of "why don't you just use the Aer sampler instead?" ...

I'd like to calculate the exact probability of each outcome in a circuit with mid-circuit measurements. Aer cannot calculate exact probabilities of each classical register outcome, but the reference Sampler has this ability (set shots=None). EDIT: Aer has the same problem as this package when shots=None: Qiskit/qiskit-aer#1810.

@woodsp-ibm woodsp-ibm added the mod: primitives Related to the Primitives module label Feb 24, 2023
@garrison
Copy link
Member Author

I figured out and implemented a way to meet my current need by evolving the Statevector according to each instruction, and considering both outcomes each time there is a measurement in the circuit. If desired, I could attempt to contribute this upstream to Qiskit Terra, so that we could close this issue. This would also have the side effect of allowing unused bits in the classical registers, which is currently not supported:

https://github.com/Qiskit/qiskit-terra/blob/94e9480ddb5d1304a45ad8b463668ebe144a43d2/qiskit/primitives/sampler.py#L168-L173

@mtreinish
Copy link
Member

I think this is reasonable to add to the Sampler class. That current restriction looks like it's only in place because of how the sampler goes from the probabilities to the expect bitstring and isn't really inherent to sampling. So if we can remove that and support midcircuit measurement that seems reasonable to me. @t-imamichi @ikkoham what do you think?

@t-imamichi
Copy link
Member

t-imamichi commented Mar 6, 2023

Because the reference implementation Sampler and Estimator were developed before mid-circuit measurements were released, the reference Sampler and Estimator cannot handle mid-circuit measurements.
I recently opened #9692 to raise an error for Estimator.

Now that we have BackendEstimator and BackendSampler, it is an option to extend them to handle mid-circuit measurements and deprecate the current reference impl Sampler and Estimator. @ikkoham opened #9700 for the discussion.

@jacobwatkins1
Copy link

Hello, is there any update on this? I would like to use Sampler for an iterative phase estimation algorithm, which necessarily requires mid circuit measurements.

@garrison
Copy link
Member Author

garrison commented Oct 5, 2023

I would like to use Sampler for an iterative phase estimation algorithm, which necessarily requires mid circuit measurements.

You could use Aer's sampler with shots != None.

@jacobwatkins1
Copy link

jacobwatkins1 commented Oct 11, 2023

Edit: @garrison already noticed this prior to the comment below.

I am actually running into the same issue with the qiskit_aer.primitives Sampler.

QiskitError: 'Some classical bits are not used for measurements. The number of classical bits 4, the used classical bits set().'

Here is the code generating the problem. It is pretty minimal. Using version >= 12 for qiskit_aer and 0.44.0 for qiskit:

image

@garrison
Copy link
Member Author

I am actually running into the same issue with the qiskit_aer.primitives Sampler.

The corresponding issue for qiskit-aer (which is triggered only when shots=None) is Qiskit/qiskit-aer#1810.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mod: primitives Related to the Primitives module type: feature request New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants