Skip to content

Commit 6ede10a

Browse files
committed
Add probability normalization to DynamicsBackend sampling routine (qiskit-community#239)
1 parent 6ca6293 commit 6ede10a

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

qiskit_dynamics/backend/backend_utils.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,21 +146,31 @@ def _get_memory_slot_probabilities(
146146

147147

148148
def _sample_probability_dict(
149-
probability_dict: Dict, shots: int, seed: Optional[int] = None
149+
probability_dict: Dict,
150+
shots: int,
151+
normalize_probabilities: bool = True,
152+
seed: Optional[int] = None,
150153
) -> List[str]:
151154
"""Sample outcomes based on probability dictionary.
152155
153156
Args:
154157
probability_dict: Dictionary representing probability distribution, with keys being
155158
outcomes, values being probabilities.
156159
shots: Number of shots.
160+
normalize_probabilities: Whether or not to normalize the probabilities to sum to 1 before
161+
sampling.
157162
seed: Seed to use in rng construction.
158163
159164
Return:
160165
List: of entries of probability_dict, sampled according to the probabilities.
161166
"""
162167
rng = np.random.default_rng(seed=seed)
163168
alphabet, probs = zip(*probability_dict.items())
169+
170+
if normalize_probabilities:
171+
probs = np.array(probs)
172+
probs = probs / probs.sum()
173+
164174
return rng.choice(alphabet, size=shots, replace=True, p=probs)
165175

166176

qiskit_dynamics/backend/dynamics_backend.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ class DynamicsBackend(BackendV2):
118118
indicating that the ground state for the system Hamiltonian should be used, or an arbitrary
119119
``Statevector`` or ``DensityMatrix``. Defaults to ``"ground_state"``.
120120
* ``normalize_states``: Boolean indicating whether to normalize states before computing outcome
121-
probabilities. Defaults to ``True``. Setting to ``False`` can result in errors if the solution
122-
tolerance results in probabilities with significant numerical deviation from a proper
123-
probability distribution.
121+
probabilities, and normalize probablities before sampling. Defaults to ``True``. Setting to
122+
``False`` can result in errors if the solution tolerance results in probabilities with
123+
significant numerical deviation from a proper probability distribution.
124124
* ``meas_level``: Form of measurement output. Supported values are ``1`` and ``2``. ``1``
125125
returns IQ points and ``2`` returns counts. Defaults to ``meas_level == 2``.
126126
* ``meas_return``: Level of measurement data to return. For ``meas_level = 1`` ``"single"``
@@ -819,7 +819,10 @@ def default_experiment_result_function(
819819

820820
# sample
821821
memory_samples = _sample_probability_dict(
822-
memory_slot_probabilities, shots=backend.options.shots, seed=seed
822+
memory_slot_probabilities,
823+
shots=backend.options.shots,
824+
normalize_probabilities=backend.options.normalize_states,
825+
seed=seed,
823826
)
824827
counts = _get_counts_from_samples(memory_samples)
825828

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
``DynamicsBackend.options.normalize_states`` now also controls whether or not the probability
5+
distribution over outcomes is normalized before sampling outcomes.

0 commit comments

Comments
 (0)