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

Update metadata of Primitives V2 #12784

Merged
merged 6 commits into from
Jul 30, 2024
Merged

Conversation

t-imamichi
Copy link
Member

@t-imamichi t-imamichi commented Jul 18, 2024

Summary

This PR updates the metadata of Primitives V2 to be compatible with that of real devices.

  • Added "version" and "circuit_metadata" to all classes
  • StatevectorEstimator
    • Replace precision -> target_precision
  • BackendEstimatorV2
    • Added shots
  • BackendSamplerV2
    • Added shots

If I need to keep the compatibility of the metadata within Qiskit 1.*, I will revert the rename and removal.

I'm wondering StatevectorEstimator because it does not compute expectation values directly from the state vector not based on shots. So, there is not relevant information about shots and I did not add shots to StatevectorEstimator yet. If necessary, I can add something like shots=inf, for example.

Addresses Qiskit/qiskit-ibm-runtime#1724

Summary of discussion:

  1. Update all metadata to match that of IBM devices.
    1-a. Shall we add shots=inf to StatevectorEstimator or not add shots?
  2. Do not remove any existing metadata, i.e., precision of StatevectorEstimator and shots of StatevectorSampler.

TODO

  • discussion
  • reno

Details and comments

SamplerV2 of qiskit-ibm-runtime returns the following metadata

result.metadata = {'version': 2}
result[i].metadata = {'circuit_metadata': {}}

EstimatorV2 of qiskit-ibm-runtime returns the following metadata

result.metadata = {'dynamical_decoupling': {'enable': False, 'sequence_type': 'XX', 'extra_slack_distribution': 'middle', 'scheduling_method': 'alap'}, 'twirling': {'enable_gates': False, 'enable_measure': True, 'num_randomizations': 'auto', 'shots_per_randomization': 'auto', 'interleave_randomizations': True, 'strategy': 'active-accum'}, 'resilience': {'measure_mitigation': True, 'zne_mitigation': False, 'pec_mitigation': False}, 'version': 2}
result[i].metadata = {'shots': 128, 'target_precision': 0.1, 'circuit_metadata': {}, 'resilience': {}, 'num_randomizations': 2}

@t-imamichi t-imamichi added the mod: primitives Related to the Primitives module label Jul 18, 2024
@t-imamichi t-imamichi requested review from a team as code owners July 18, 2024 11:35
@qiskit-bot
Copy link
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core
  • @ajavadia
  • @levbishop
  • @t-imamichi

@t-imamichi t-imamichi added the Changelog: API Change Include in the "Changed" section of the changelog label Jul 18, 2024
@coveralls
Copy link

coveralls commented Jul 18, 2024

Pull Request Test Coverage Report for Build 10164614695

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 8 of 8 (100.0%) changed or added relevant lines in 4 files are covered.
  • 3 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.02%) to 89.77%

Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 3 92.48%
Totals Coverage Status
Change from base Build 10164137701: 0.02%
Covered Lines: 66965
Relevant Lines: 74596

💛 - Coveralls

@ElePT ElePT added this to the 1.2.0 milestone Jul 18, 2024
@t-imamichi t-imamichi requested a review from jyu00 July 26, 2024 08:45
@@ -238,7 +238,6 @@ def _preprocess_pub(self, pub: EstimatorPub) -> _PreprocessedData:
param_indices = np.fromiter(np.ndindex(param_shape), dtype=object).reshape(param_shape)
bc_param_ind, bc_obs = np.broadcast_arrays(param_indices, observables)

# calculate expectation values for each pair of parameter value set and pauli
Copy link
Member Author

Choose a reason for hiding this comment

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

Removed a wrong and unnecessary comment

@t-imamichi
Copy link
Member Author

t-imamichi commented Jul 29, 2024

FYI: neko is failing due to #12833

jyu00
jyu00 previously approved these changes Jul 29, 2024
Copy link
Contributor

@jyu00 jyu00 left a comment

Choose a reason for hiding this comment

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

1 small comment but not a blocker.

@@ -213,7 +219,9 @@ def _postprocess_pub(
meas = {
item.creg_name: BitArray(arrays[item.creg_name], item.num_bits) for item in meas_info
}
return SamplerPubResult(DataBin(**meas, shape=shape), metadata={})
return SamplerPubResult(
DataBin(**meas, shape=shape), metadata={"circuit_metadata": circuit_metadata}
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this have shots too? Given EstimatorPubResult has target_precision. We should update the QRT Sampler to also return shots.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for reviewing this PR.

Qiskit Runtime Sampler V2 does not have shots in the metadata because it can be obtained by result[i].data.{creg}.num_shots. I removed shots from the metadata of StatevectorSampler in this PR because of the same reason.
On the other hand, Estimator V2 has shots in the metadata.

SamplerV2 of qiskit-ibm-runtime returns the following metadata with the default option.

result.metadata = {'version': 2}
result[i].metadata = {'circuit_metadata': {}}

EstimatorV2 of qiskit-ibm-runtime returns the following metadata

result.metadata = {'dynamical_decoupling': {'enable': False, 'sequence_type': 'XX', 'extra_slack_distribution': 'middle', 'scheduling_method': 'alap'}, 'twirling': {'enable_gates': False, 'enable_measure': True, 'num_randomizations': 'auto', 'shots_per_randomization': 'auto', 'interleave_randomizations': True, 'strategy': 'active-accum'}, 'resilience': {'measure_mitigation': True, 'zne_mitigation': False, 'pec_mitigation': False}, 'version': 2}
result[i].metadata = {'shots': 128, 'target_precision': 0.1, 'circuit_metadata': {}, 'resilience': {}, 'num_randomizations': 2}

@ElePT ElePT requested a review from jyu00 July 30, 2024 12:02
@ElePT
Copy link
Contributor

ElePT commented Jul 30, 2024

Adding PR to the queue after approval and offline confirmation from @jyu00

@ElePT ElePT enabled auto-merge July 30, 2024 12:35
ElePT
ElePT previously approved these changes Jul 30, 2024
@ElePT ElePT added this pull request to the merge queue Jul 30, 2024
@ElePT ElePT removed this pull request from the merge queue due to a manual request Jul 30, 2024
@ElePT ElePT added the stable backport potential The bug might be minimal and/or import enough to be port to stable label Jul 30, 2024
@t-imamichi t-imamichi dismissed stale reviews from ElePT and jyu00 via 099189b July 30, 2024 14:41
ElePT
ElePT previously approved these changes Jul 30, 2024
Copy link
Contributor

@ElePT ElePT left a comment

Choose a reason for hiding this comment

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

LGTM (also based on offline discussion with @t-imamichi and @jyu00)

@ElePT ElePT enabled auto-merge July 30, 2024 14:47
@ElePT ElePT added this pull request to the merge queue Jul 30, 2024
Merged via the queue into Qiskit:main with commit 68687d3 Jul 30, 2024
15 checks passed
mergify bot pushed a commit that referenced this pull request Jul 30, 2024
* update metadata of primitives v2

* remove `shots` and add reno

* add version

* add shots to SamplerV2 metadata

* udpate reno

---------

Co-authored-by: Elena Peña Tapia <57907331+ElePT@users.noreply.github.com>
(cherry picked from commit 68687d3)
github-merge-queue bot pushed a commit that referenced this pull request Jul 30, 2024
* update metadata of primitives v2

* remove `shots` and add reno

* add version

* add shots to SamplerV2 metadata

* udpate reno

---------

Co-authored-by: Elena Peña Tapia <57907331+ElePT@users.noreply.github.com>
(cherry picked from commit 68687d3)

Co-authored-by: Takashi Imamichi <31178928+t-imamichi@users.noreply.github.com>
@t-imamichi t-imamichi deleted the primitives-metadata branch July 31, 2024 01:35
Procatv pushed a commit to Procatv/qiskit-terra-catherines that referenced this pull request Aug 1, 2024
* update metadata of primitives v2

* remove `shots` and add reno

* add version

* add shots to SamplerV2 metadata

* udpate reno

---------

Co-authored-by: Elena Peña Tapia <57907331+ElePT@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: API Change Include in the "Changed" section of the changelog mod: primitives Related to the Primitives module stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants