-
Notifications
You must be signed in to change notification settings - Fork 7
/
estimator.py
313 lines (269 loc) · 11.7 KB
/
estimator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from collections.abc import Collection, Iterable, Sequence
from dataclasses import dataclass
from functools import cached_property
from math import sqrt
from quri_parts.core.estimator import (
ConcurrentQuantumEstimator,
Estimatable,
Estimate,
GeneralQuantumEstimator,
QuantumEstimator,
create_general_estimator_from_estimator,
)
from quri_parts.core.estimator.sampling.pauli import (
general_pauli_sum_expectation_estimator,
general_pauli_sum_sample_variance,
)
from quri_parts.core.estimator.utils import is_estimatable
from quri_parts.core.measurement import (
CommutablePauliSetMeasurement,
CommutablePauliSetMeasurementFactory,
PauliReconstructorFactory,
)
from quri_parts.core.operator import PAULI_IDENTITY, CommutablePauliSet, Operator
from quri_parts.core.sampling import (
ConcurrentSampler,
MeasurementCounts,
PauliSamplingShotsAllocator,
)
from quri_parts.core.state import CircuitQuantumState, ParametricCircuitQuantumState
from .estimator_helpers import (
CircuitShotPairPreparationFunction,
distribute_shots_among_pauli_sets,
get_sampling_circuits_and_shots,
)
class _Estimate:
def __init__(
self,
op: Operator,
const: complex,
pauli_sets: Sequence[CommutablePauliSet],
pauli_recs: Sequence[PauliReconstructorFactory],
sampling_counts: Sequence[MeasurementCounts],
):
self._op = op
self._const = const
self._pauli_sets = pauli_sets
self._pauli_recs = pauli_recs
self._sampling_counts = sampling_counts
@cached_property
def value(self) -> complex:
val = self._const
for pauli_set, pauli_rec, counts in zip(
self._pauli_sets, self._pauli_recs, self._sampling_counts
):
val += general_pauli_sum_expectation_estimator(
counts, pauli_set, self._op, pauli_rec
)
return val
@cached_property
def error(self) -> float:
square_err: float = 0.0
for pauli_set, pauli_rec, counts in zip(
self._pauli_sets, self._pauli_recs, self._sampling_counts
):
total_counts = sum(counts.values())
var = general_pauli_sum_sample_variance(
counts, pauli_set, self._op, pauli_rec
)
square_err += var / total_counts
return sqrt(square_err)
@dataclass
class _ConstEstimate:
value: complex
error: float = 0.0
def get_estimate_from_sampling_result(
op: Operator,
measurement_groups: Iterable[CommutablePauliSetMeasurement],
const: complex,
sampling_counts: Iterable[MeasurementCounts],
) -> Estimate[complex]:
"""Converts sampling counts into the estimation of the operator's
expectation value."""
pauli_sets = tuple(m.pauli_set for m in measurement_groups)
pauli_recs = tuple(m.pauli_reconstructor_factory for m in measurement_groups)
return _Estimate(op, const, pauli_sets, pauli_recs, tuple(sampling_counts))
def sampling_estimate(
op: Estimatable,
state: CircuitQuantumState,
total_shots: int,
sampler: ConcurrentSampler,
measurement_factory: CommutablePauliSetMeasurementFactory,
shots_allocator: PauliSamplingShotsAllocator,
circuit_shot_pair_prep_fn: CircuitShotPairPreparationFunction = get_sampling_circuits_and_shots, # noqa: E501
) -> Estimate[complex]:
"""Estimate expectation value of a given operator with a given state by
sampling measurement.
The sampling measurements are configured with arguments as follows.
Args:
op: An operator of which expectation value is estimated.
state: A quantum state on which the operator expectation is evaluated.
total_shots: Total number of shots available for sampling measurements.
sampler: A Sampler that actually performs the sampling measurements.
measurement_factory: A function that performs Pauli grouping and returns
a measurement scheme for Pauli operators constituting the original operator.
shots_allocator: A function that allocates the total shots to Pauli groups to
be measured.
circuit_shot_pair_prep_fn: A :class:`~CircuitShotPairPreparationFunction` that
prepares the set of circuits to perform measurement with. It is default to
a function that concatenates the measurement circuits after the state
preparation circuit.
Returns:
The estimated value (can be accessed with :attr:`.value`) with standard error
of estimation (can be accessed with :attr:`.error`).
"""
assert is_estimatable(
op, state
), "Number of qubits of the operator is too large to estimate."
if not isinstance(op, Operator):
op = Operator({op: 1.0})
if len(op) == 0:
return _ConstEstimate(0.0)
if len(op) == 1 and PAULI_IDENTITY in op:
return _ConstEstimate(op[PAULI_IDENTITY])
const = op.constant
measurements = measurement_factory(op)
measurements = [m for m in measurements if m.pauli_set != {PAULI_IDENTITY}]
shots_map = distribute_shots_among_pauli_sets(
op, measurements, shots_allocator, total_shots
)
circuit_and_shots = circuit_shot_pair_prep_fn(state, measurements, shots_map)
sampling_counts = sampler(circuit_and_shots)
return get_estimate_from_sampling_result(op, measurements, const, sampling_counts)
def create_sampling_estimator(
total_shots: int,
sampler: ConcurrentSampler,
measurement_factory: CommutablePauliSetMeasurementFactory,
shots_allocator: PauliSamplingShotsAllocator,
) -> QuantumEstimator[CircuitQuantumState]:
"""Create a :class:`QuantumEstimator` that estimates operator expectation
value by sampling measurement.
The sampling measurements are configured with arguments as follows.
Args:
total_shots: Total number of shots available for sampling measurements.
sampler: A Sampler that actually performs the sampling measurements.
measurement_factory: A function that performs Pauli grouping and returns
a measurement scheme for Pauli operators constituting the original operator.
shots_allocator: A function that allocates the total shots to Pauli groups to
be measured.
"""
def estimator(op: Estimatable, state: CircuitQuantumState) -> Estimate[complex]:
return sampling_estimate(
op, state, total_shots, sampler, measurement_factory, shots_allocator
)
return estimator
def concurrent_sampling_estimate(
operators: Collection[Estimatable],
states: Collection[CircuitQuantumState],
total_shots: int,
sampler: ConcurrentSampler,
measurement_factory: CommutablePauliSetMeasurementFactory,
shots_allocator: PauliSamplingShotsAllocator,
circuit_shot_pair_prep_fn: CircuitShotPairPreparationFunction = get_sampling_circuits_and_shots, # noqa: E501
) -> Iterable[Estimate[complex]]:
"""Estimate expectation value of given operators with given states by
sampling measurement.
The sampling measurements are configured with arguments as follows.
Args:
operators: Operators of which expectation value is estimated.
states: Quantum states on which the operator expectation is evaluated.
total_shots: Total number of shots available for sampling measurements.
sampler: A Sampler that actually performs the sampling measurements.
measurement_factory: A function that performs Pauli grouping and returns
a measurement scheme for Pauli operators constituting the original operator.
shots_allocator: A function that allocates the total shots to Pauli groups to
be measured.
circuit_shot_pair_prep_fn: A :class:`~CircuitShotPairPreparationFunction` that
prepares the set of circuits to perform measurement with. It is default to
a function that concatenates the measurement circuits after the state
preparation circuit.
Returns:
The estimated values (can be accessed with :attr:`.value`) with standard errors
of estimation (can be accessed with :attr:`.error`).
"""
num_ops = len(operators)
num_states = len(states)
if num_ops == 0:
raise ValueError("No operator specified.")
if num_states == 0:
raise ValueError("No state specified.")
if num_ops > 1 and num_states > 1 and num_ops != num_states:
raise ValueError(
f"Number of operators ({num_ops}) does not match"
f"number of states ({num_states})."
)
if num_states == 1:
states = [next(iter(states))] * num_ops
if num_ops == 1:
operators = [next(iter(operators))] * num_states
return [
sampling_estimate(
op,
state,
total_shots,
sampler,
measurement_factory,
shots_allocator,
circuit_shot_pair_prep_fn,
)
for op, state in zip(operators, states)
]
def create_sampling_concurrent_estimator(
total_shots: int,
sampler: ConcurrentSampler,
measurement_factory: CommutablePauliSetMeasurementFactory,
shots_allocator: PauliSamplingShotsAllocator,
) -> ConcurrentQuantumEstimator[CircuitQuantumState]:
"""Create a :class:`ConcurrentQuantumEstimator` that estimates operator
expectation value by sampling measurement.
The sampling measurements are configured with arguments as follows.
Args:
total_shots: Total number of shots available for sampling measurements.
sampler: A Sampler that actually performs the sampling measurements.
measurement_factory: A function that performs Pauli grouping and returns
a measurement scheme for Pauli operators constituting the original operator.
shots_allocator: A function that allocates the total shots to Pauli groups to
be measured.
"""
def estimator(
operators: Collection[Estimatable],
states: Collection[CircuitQuantumState],
) -> Iterable[Estimate[complex]]:
return concurrent_sampling_estimate(
operators,
states,
total_shots,
sampler,
measurement_factory,
shots_allocator,
)
return estimator
def create_general_sampling_estimator(
total_shots: int,
sampler: ConcurrentSampler,
measurement_factory: CommutablePauliSetMeasurementFactory,
shots_allocator: PauliSamplingShotsAllocator,
) -> GeneralQuantumEstimator[CircuitQuantumState, ParametricCircuitQuantumState]:
"""Creates a :class:`GeneralQuantumEstimator` that performs sampling
estimation.
Args:
total_shots: Total number of shots available for sampling measurements.
sampler: A Sampler that actually performs the sampling measurements.
measurement_factory: A function that performs Pauli grouping and returns
a measurement scheme for Pauli operators constituting the original operator.
shots_allocator: A function that allocates the total shots to Pauli groups to
be measured.
"""
sampling_estimator = create_sampling_estimator(
total_shots, sampler, measurement_factory, shots_allocator
)
return create_general_estimator_from_estimator(sampling_estimator)