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

Remove a validation check of circuits for BaseSampler (follow-up of #8678) #8708

Merged
merged 6 commits into from
Sep 29, 2022

Conversation

t-imamichi
Copy link
Member

Summary

#8678 introduced some validation check of circuits for BaseSampler.
But, @nonhermitian suggested relaxing a check to support mid-circuit measurements in the future.
See the thread for details. #8678 (comment)

Follow-up #8678

Details and comments

The check of measurements is moved from BaseSampler to Sampler (the reference impl) because Sampler requires it. This is the revert of a change of #8678.

@t-imamichi t-imamichi requested review from a team and ikkoham as code owners September 8, 2022 14:40
@qiskit-bot
Copy link
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

One or more of the the following people are requested to review this:

@coveralls
Copy link

coveralls commented Sep 8, 2022

Pull Request Test Coverage Report for Build 3135998431

  • 3 of 3 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.04%) to 84.406%

Totals Coverage Status
Change from base Build 3132502212: 0.04%
Covered Lines: 59614
Relevant Lines: 70628

💛 - Coveralls

@t-imamichi t-imamichi added Changelog: None Do not include in changelog mod: primitives Related to the Primitives module and removed Changelog: None Do not include in changelog labels Sep 13, 2022
@t-imamichi t-imamichi added this to the 0.22 milestone Sep 13, 2022
ikkoham
ikkoham previously approved these changes Sep 13, 2022
Copy link
Contributor

@ikkoham ikkoham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I want to wait @nonhermitian's review.

Copy link
Contributor

@nonhermitian nonhermitian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think this has issues (see my one comment). I think the only check that one can make at this stage is to raise only if there is no classical bits at all. That way at least you know that there is nothing to measure into, guaranteeing a empty dict output

@@ -158,6 +158,12 @@ def _run(
def _preprocess_circuit(circuit: QuantumCircuit):
circuit = init_circuit(circuit)
q_c_mapping = final_measurement_mapping(circuit)
if set(range(circuit.num_clbits)) != set(q_c_mapping.values()):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it is still going to raise if the number of clbits is not equal to the number of final measurements. That is to say that if I wrote to all my bits via mid-circuit measurements then this would be triggered.

Copy link
Member Author

@t-imamichi t-imamichi Sep 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is part of the reference implementation based on quantum_info.Statevector. It requires final measurements. Statevector does not support mid-circuit measurements.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the title of this PR to mention BaseSampler

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then you are blocking real-world use cases for a simulation method that does not scale. Does not seem like a fruitful direction to me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reference implementation was designed to be as simple as possible. Runtime primitives and Aer primitives are more appropriate for scaling, e.g., Qiskit/qiskit-aer#1531
This is similar to the relationship of BasicAer and Aer simulator. The reference implementation of primitives corresponds to BasicAer and Aer primitives corresponds to Aer simulator.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is another way to think about it; Just overload all in Runtime. In some sense that is what would probably need to be done because a state vector simulator is quite far removed from a real hardware sampling routine

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a PR #8668 working on backend-based Sampler. It is closer to real hardware sampling. Once it's merged, we can discuss whether we need the reference implementation.

Copy link
Member Author

@t-imamichi t-imamichi Sep 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is only part of the reference implementation of Sampler and not part of BaseSampler. So, other Sampler implementations does use this check. Other implementations are supposed to inherit only BaseSampler.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reference implementation Sampler needs this check. So, I have to put it in BaseSampler (abstract class) or Sampler (reference implementation). I moved it from Sampler to BaseSampler in #8668. I want to revert it in this PR. If this PR is not merged, the check stays in BaseSampler and raises an error for all implementations not only the reference impl but also qiskit-ibm-runtime.

@nonhermitian
Copy link
Contributor

For the record, I do not think there is a completely automated way to determine what measurements an end-user considers as part of the final output distributions. Dynamic circuits make this impossible in general.

@ikkoham
Copy link
Contributor

ikkoham commented Sep 13, 2022

Does Sampler need to support mid-circuit measurements in the first place? I thought mid-circuit measurement could not be supported the various error-mitigation methods. But I am not aware of any discussions around this, and I don't know the range of circuits Sampler can take (i.e. should Sampler support dynamic circuits?).

@nonhermitian
Copy link
Contributor

nonhermitian commented Sep 13, 2022

I would say absolutely, otherwise how is it a "primitive"? One can correct some mid-circuit measurements provided the results of those measurements are aggregated. Several of us would like this, and I am re-tooling M3 to allow for it. However, the single-shot results cannot be; it would have to be corrected in HW.

@t-imamichi t-imamichi changed the title Remove a validation check of circuits for Sampler (follow-up of #8678) Remove a validation check of circuits for BaseSampler (follow-up of #8678) Sep 13, 2022
@t-imamichi
Copy link
Member Author

I still think this has issues (see my one comment). I think the only check that one can make at this stage is to raise only if there is no classical bits at all. That way at least you know that there is nothing to measure into, guaranteeing a empty dict output

This PR moves the check of classical bits and final measurement mapping from BaseSampler and Sampler (revert of #8668). So, this PR does the check only whether there is no classical bits at all.

@t-imamichi
Copy link
Member Author

I resolved conflicts to merge main.

# Conflicts:
#	qiskit/primitives/base_sampler.py
ikkoham
ikkoham previously approved these changes Sep 26, 2022
Copy link
Contributor

@ikkoham ikkoham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's good. Can I merge this? @nonhermitian

# Conflicts:
#	qiskit/primitives/base_sampler.py
@t-imamichi
Copy link
Member Author

I fixed the conflict.

@t-imamichi
Copy link
Member Author

This PR conflicts with #8760. So, I fixed a test with a note.

Copy link
Contributor

@ikkoham ikkoham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least, this PR is better than before since the validation in BaseSampler is removed.
If it's not sufficient, let's fix it after RC release.

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants