reducers
¶
Turn measurement counts into numbers.
A :class:Measurement reads chosen qubits from a counts dict and evaluates one
arithmetic expression on them. :func:apply_measurements does that for every
time point of a :class:~shadowsim.simulators.result.SimulationResult.
QUBIT_EXPECTATIONS
module-attribute
¶
observable_kind metadata of a result with one Pauli expectation per qubit.
Measurement
dataclass
¶
One number per time point, computed from the shot counts of chosen qubits.
expression may use numbers, parentheses, unary + and -,
+ - * / **, shots, and counts["01"]. A counts key has one
character per entry of qubits, in that order, so with qubits=(2, 0)
counts["10"] is the number of shots where qubit 2 read 1 and qubit 0 read 0.
A bitstring that was never observed is 0. shots is the total number of
shots at that time point. Values are floats, so ** cannot build an
unbounded integer.
The counts must come from a run in the basis this expression assumes. A Measurement does not change or check that basis.
An expectation backend has no counts. :func:apply_measurements then reads
a one-qubit expression from that qubit's distribution: counts["0"] is
(1 + expectation) / 2, counts["1"] is (1 - expectation) / 2, and
shots is 1. Dividing by shots gives the infinite-shot value. An
expression that uses raw counts without shots reads those probabilities.
Attributes:
-
qubits(tuple[int, ...]) –System qubit indexes, in the order a
countskey lists them. -
expression(str) –Arithmetic on
countsandshots. -
label(str) –Name of the resulting curve. Defaults to
expression.
Examples:
Probability that qubits 0 and 1 agree:
from shadowsim.simulators import Measurement
same = Measurement((0, 1), '(counts["00"] + counts["11"]) / shots')
print(same.evaluate({"000": 6, "110": 2, "011": 2}))
Source code in shadowsim/simulators/reducers.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
__post_init__
¶
Check qubits and parse expression.
Raises:
-
ValueError–qubitsis empty, repeats an index, or holds something other than non-negative integers, orexpressionis not allowed.
Source code in shadowsim/simulators/reducers.py
evaluate
¶
Return this measurement on one time point's counts.
Parameters:
-
counts(dict[str, int]) –Bitstring to shot count, where bit
iof a key is system qubiti.
Raises:
-
ValueError–A key is too short for
qubits, the expression divides by zero or overflows, or the result is not a finite real number.
Source code in shadowsim/simulators/reducers.py
apply_measurements
¶
apply_measurements(
result: SimulationResult,
measurements: Sequence[Measurement],
) -> SimulationResult
Return result with one curve per measurement in place of its observables.
Each curve has one value per entry of result.counts. The metadata gains
measurements: the qubits, expression, and label of each one, in order.
Only a sampling backend, such as Aer, records counts. An expectation backend,
such as PauliPropagation, returns one Pauli expectation per qubit instead
(observable_kind is "qubit_expectations"). A one-qubit measurement is
evaluated on that distribution: counts["0"] is (1 + expectation) / 2,
counts["1"] is (1 - expectation) / 2, and shots is 1. Dividing by
shots gives the infinite-shot value. An expression that uses raw counts
without shots reads those probabilities. Two or more qubits need a
sampling backend, because the result has no correlations between qubits.
Raises:
-
ValueError–measurementsis empty, or a measurement cannot be evaluated onresult.