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
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions qiskit/primitives/backend_estimator_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def _run(self, pubs: list[EstimatorPub]) -> PrimitiveResult[PubResult]:
# reconstruct the result of pubs
for i, pub_result in zip(lst, pub_results):
results[i] = pub_result
return PrimitiveResult(results)
return PrimitiveResult(results, metadata={"version": 2})

def _run_pubs(self, pubs: list[EstimatorPub], shots: int) -> list[PubResult]:
"""Compute results for pubs that all require the same value of ``shots``."""
Expand Down Expand Up @@ -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

param_obs_map = defaultdict(set)
for index in np.ndindex(*bc_param_ind.shape):
param_index = bc_param_ind[index]
Expand Down Expand Up @@ -275,7 +274,14 @@ def _postprocess_pub(
variances[index] += np.abs(coeff) * variance**0.5
stds = variances / np.sqrt(shots)
data_bin = DataBin(evs=evs, stds=stds, shape=evs.shape)
return PubResult(data_bin, metadata={"target_precision": pub.precision})
return PubResult(
data_bin,
metadata={
"target_precision": pub.precision,
"shots": shots,
"circuit_metadata": pub.circuit.metadata,
},
)

def _bind_and_add_measurements(
self,
Expand Down
14 changes: 11 additions & 3 deletions qiskit/primitives/backend_sampler_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def _run(self, pubs: list[SamplerPub]) -> PrimitiveResult[SamplerPubResult]:
# reconstruct the result of pubs
for i, pub_result in zip(lst, pub_results):
results[i] = pub_result
return PrimitiveResult(results)
return PrimitiveResult(results, metadata={"version": 2})

def _run_pubs(self, pubs: list[SamplerPub], shots: int) -> list[SamplerPubResult]:
"""Compute results for pubs that all require the same value of ``shots``."""
Expand Down Expand Up @@ -183,7 +183,12 @@ def _run_pubs(self, pubs: list[SamplerPub], shots: int) -> list[SamplerPubResult
end = start + bound.size
results.append(
self._postprocess_pub(
result_memory[start:end], shots, bound.shape, meas_info, max_num_bytes
result_memory[start:end],
shots,
bound.shape,
meas_info,
max_num_bytes,
pub.circuit.metadata,
)
)
start = end
Expand All @@ -197,6 +202,7 @@ def _postprocess_pub(
shape: tuple[int, ...],
meas_info: list[_MeasureInfo],
max_num_bytes: int,
circuit_metadata: dict,
) -> SamplerPubResult:
"""Converts the memory data into an array of bit arrays with the shape of the pub."""
arrays = {
Expand All @@ -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}

)


def _analyze_circuit(circuit: QuantumCircuit) -> tuple[list[_MeasureInfo], int]:
Expand Down
6 changes: 4 additions & 2 deletions qiskit/primitives/statevector_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def run(
return job

def _run(self, pubs: list[EstimatorPub]) -> PrimitiveResult[PubResult]:
return PrimitiveResult([self._run_pub(pub) for pub in pubs])
return PrimitiveResult([self._run_pub(pub) for pub in pubs], metadata={"version": 2})

def _run_pub(self, pub: EstimatorPub) -> PubResult:
rng = np.random.default_rng(self._seed)
Expand All @@ -162,4 +162,6 @@ def _run_pub(self, pub: EstimatorPub) -> PubResult:
evs[index] = expectation_value

data = DataBin(evs=evs, stds=stds, shape=evs.shape)
return PubResult(data, metadata={"precision": precision})
return PubResult(
data, metadata={"target_precision": precision, "circuit_metadata": pub.circuit.metadata}
)
7 changes: 5 additions & 2 deletions qiskit/primitives/statevector_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def run(

def _run(self, pubs: Iterable[SamplerPub]) -> PrimitiveResult[SamplerPubResult]:
results = [self._run_pub(pub) for pub in pubs]
return PrimitiveResult(results)
return PrimitiveResult(results, metadata={"version": 2})

def _run_pub(self, pub: SamplerPub) -> SamplerPubResult:
circuit, qargs, meas_info = _preprocess_circuit(pub.circuit)
Expand All @@ -197,7 +197,10 @@ def _run_pub(self, pub: SamplerPub) -> SamplerPubResult:
meas = {
item.creg_name: BitArray(arrays[item.creg_name], item.num_bits) for item in meas_info
}
return SamplerPubResult(DataBin(**meas, shape=pub.shape), metadata={"shots": pub.shots})
return SamplerPubResult(
DataBin(**meas, shape=pub.shape),
metadata={"circuit_metadata": pub.circuit.metadata},
)


def _preprocess_circuit(circuit: QuantumCircuit):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
features_primitives:
- |
The metadata of Primitives V2 implementations, i.e., :class:`.StatevectorSampler`,
:class:`.StatevectorEstimator`, :class:`.BackendSamplerV2` and :class:`.BackendEstimatorV2`,
has been updated to match that of IBM quantum devices.
24 changes: 24 additions & 0 deletions test/python/primitives/test_backend_estimator_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,30 @@ def test_iter_pub(self):
np.testing.assert_allclose(result[0].data.evs, [-1.284366511861733], rtol=self._rtol)
np.testing.assert_allclose(result[1].data.evs, [-1.284366511861733], rtol=self._rtol)

def test_metadata(self):
"""Test for metadata"""
qc = QuantumCircuit(2)
qc2 = QuantumCircuit(2)
qc2.metadata = {"a": 1}
backend = BasicSimulator()
estimator = BackendEstimatorV2(backend=backend)
pm = generate_preset_pass_manager(optimization_level=0, backend=backend)
qc, qc2 = pm.run([qc, qc2])
op = SparsePauliOp("ZZ").apply_layout(qc.layout)
op2 = SparsePauliOp("ZZ").apply_layout(qc2.layout)
result = estimator.run([(qc, op), (qc2, op2)], precision=0.1).result()

self.assertEqual(len(result), 2)
self.assertEqual(result.metadata, {"version": 2})
self.assertEqual(
result[0].metadata,
{"target_precision": 0.1, "shots": 100, "circuit_metadata": qc.metadata},
)
self.assertEqual(
result[1].metadata,
{"target_precision": 0.1, "shots": 100, "circuit_metadata": qc2.metadata},
)


if __name__ == "__main__":
unittest.main()
15 changes: 15 additions & 0 deletions test/python/primitives/test_backend_sampler_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,21 @@ def test_iter_pub(self):
self._assert_allclose(result[0].data.meas, np.array({0: self._shots}))
self._assert_allclose(result[1].data.meas, np.array({1: self._shots}))

def test_metadata(self):
"""Test for metadata"""
qc = QuantumCircuit(2)
qc.measure_all()
qc2 = qc.copy()
qc2.metadata = {"a": 1}
backend = BasicSimulator()
sampler = BackendSamplerV2(backend=backend)
result = sampler.run([qc, qc2], shots=10).result()

self.assertEqual(len(result), 2)
self.assertEqual(result.metadata, {"version": 2})
self.assertEqual(result[0].metadata, {"circuit_metadata": qc.metadata})
self.assertEqual(result[1].metadata, {"circuit_metadata": qc2.metadata})


if __name__ == "__main__":
unittest.main()
23 changes: 20 additions & 3 deletions test/python/primitives/test_statevector_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def test_run_single_circuit_observable(self):
self.subTest(f"{val}")
result = est.run([(qc, op, val)]).result()
np.testing.assert_allclose(result[0].data.evs, target)
self.assertEqual(result[0].metadata["precision"], 0)
self.assertEqual(result[0].metadata["target_precision"], 0)

with self.subTest("One parameter"):
param = Parameter("x")
Expand All @@ -145,7 +145,7 @@ def test_run_single_circuit_observable(self):
self.subTest(f"{val}")
result = est.run([(qc, op, val)]).result()
np.testing.assert_allclose(result[0].data.evs, target)
self.assertEqual(result[0].metadata["precision"], 0)
self.assertEqual(result[0].metadata["target_precision"], 0)

with self.subTest("More than one parameter"):
qc = self.psi[0]
Expand All @@ -162,7 +162,7 @@ def test_run_single_circuit_observable(self):
self.subTest(f"{val}")
result = est.run([(qc, op, val)]).result()
np.testing.assert_allclose(result[0].data.evs, target)
self.assertEqual(result[0].metadata["precision"], 0)
self.assertEqual(result[0].metadata["target_precision"], 0)

def test_run_1qubit(self):
"""Test for 1-qubit cases"""
Expand Down Expand Up @@ -290,6 +290,23 @@ def test_iter_pub(self):
np.testing.assert_allclose(result[0].data.evs, [-1.284366511861733])
np.testing.assert_allclose(result[1].data.evs, [-1.284366511861733])

def test_metadata(self):
"""Test for metadata"""
qc = QuantumCircuit(2)
qc2 = QuantumCircuit(2)
qc2.metadata = {"a": 1}
estimator = StatevectorEstimator()
result = estimator.run([(qc, "ZZ"), (qc2, "ZZ")], precision=0.1).result()

self.assertEqual(len(result), 2)
self.assertEqual(result.metadata, {"version": 2})
self.assertEqual(
result[0].metadata, {"target_precision": 0.1, "circuit_metadata": qc.metadata}
)
self.assertEqual(
result[1].metadata, {"target_precision": 0.1, "circuit_metadata": qc2.metadata}
)


if __name__ == "__main__":
unittest.main()
28 changes: 14 additions & 14 deletions test/python/primitives/test_statevector_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,6 @@ def test_run_with_shots_option(self):
self.assertEqual(len(result), 1)
self.assertEqual(result[0].data.meas.num_shots, shots)
self.assertEqual(sum(result[0].data.meas.get_counts().values()), shots)
self.assertIn("shots", result[0].metadata)
self.assertEqual(result[0].metadata["shots"], shots)

with self.subTest("default shots"):
sampler = StatevectorSampler(seed=self._seed)
Expand All @@ -391,8 +389,6 @@ def test_run_with_shots_option(self):
self.assertEqual(len(result), 1)
self.assertEqual(result[0].data.meas.num_shots, default_shots)
self.assertEqual(sum(result[0].data.meas.get_counts().values()), default_shots)
self.assertIn("shots", result[0].metadata)
self.assertEqual(result[0].metadata["shots"], default_shots)

with self.subTest("setting default shots"):
default_shots = 100
Expand All @@ -402,26 +398,20 @@ def test_run_with_shots_option(self):
self.assertEqual(len(result), 1)
self.assertEqual(result[0].data.meas.num_shots, default_shots)
self.assertEqual(sum(result[0].data.meas.get_counts().values()), default_shots)
self.assertIn("shots", result[0].metadata)
self.assertEqual(result[0].metadata["shots"], default_shots)

with self.subTest("pub-like"):
sampler = StatevectorSampler(seed=self._seed)
result = sampler.run([(bell, None, shots)]).result()
self.assertEqual(len(result), 1)
self.assertEqual(result[0].data.meas.num_shots, shots)
self.assertEqual(sum(result[0].data.meas.get_counts().values()), shots)
self.assertIn("shots", result[0].metadata)
self.assertEqual(result[0].metadata["shots"], shots)

with self.subTest("pub"):
sampler = StatevectorSampler(seed=self._seed)
result = sampler.run([SamplerPub(bell, shots=shots)]).result()
self.assertEqual(len(result), 1)
self.assertEqual(result[0].data.meas.num_shots, shots)
self.assertEqual(sum(result[0].data.meas.get_counts().values()), shots)
self.assertIn("shots", result[0].metadata)
self.assertEqual(result[0].metadata["shots"], shots)

with self.subTest("multiple pubs"):
sampler = StatevectorSampler(seed=self._seed)
Expand All @@ -437,13 +427,9 @@ def test_run_with_shots_option(self):
self.assertEqual(len(result), 2)
self.assertEqual(result[0].data.meas.num_shots, shots1)
self.assertEqual(sum(result[0].data.meas.get_counts().values()), shots1)
self.assertIn("shots", result[0].metadata)
self.assertEqual(result[0].metadata["shots"], shots1)

self.assertEqual(result[1].data.meas.num_shots, shots2)
self.assertEqual(sum(result[1].data.meas.get_counts().values()), shots2)
self.assertIn("shots", result[1].metadata)
self.assertEqual(result[1].metadata["shots"], shots2)

def test_run_shots_result_size(self):
"""test with shots option to validate the result size"""
Expand Down Expand Up @@ -636,6 +622,20 @@ def test_iter_pub(self):
self._assert_allclose(result[0].data.meas, np.array({0: self._shots}))
self._assert_allclose(result[1].data.meas, np.array({1: self._shots}))

def test_metadata(self):
"""Test for metadata"""
qc = QuantumCircuit(2)
qc.measure_all()
qc2 = qc.copy()
qc2.metadata = {"a": 1}
sampler = StatevectorSampler()
result = sampler.run([qc, qc2], shots=10).result()

self.assertEqual(len(result), 2)
self.assertEqual(result.metadata, {"version": 2})
self.assertEqual(result[0].metadata, {"circuit_metadata": qc.metadata})
self.assertEqual(result[1].metadata, {"circuit_metadata": qc2.metadata})


if __name__ == "__main__":
unittest.main()
Loading