-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathfidelity_estimation.py
224 lines (181 loc) · 9 KB
/
fidelity_estimation.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
# Copyright 2021 The Cirq Developers
#
# 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
#
# https://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.
"""Estimation of fidelity associated with experimental circuit executions."""
from typing import Callable, Mapping, Optional, Sequence
import numpy as np
from cirq.circuits import Circuit
from cirq.ops import QubitOrder, QubitOrderOrList
from cirq.sim import final_state_vector
from cirq.value import state_vector_to_probabilities
def linear_xeb_fidelity_from_probabilities(
hilbert_space_dimension: int, probabilities: Sequence[float]
) -> float:
"""Linear XEB fidelity estimator.
Estimates fidelity from ideal probabilities of observed bitstrings.
This estimator makes two assumptions. First, it assumes that the circuit
used in experiment is sufficiently scrambling that its output probabilities
follow the Porter-Thomas distribution. This assumption holds for typical
instances of random quantum circuits of sufficient depth. Second, it assumes
that the circuit uses enough qubits so that the Porter-Thomas distribution
can be approximated with the exponential distribution.
In practice the validity of these assumptions can be confirmed by plotting
a histogram of output probabilities and comparing it to the exponential
distribution.
The mean of this estimator is the true fidelity f and the variance is
(1 + 2f - f^2) / M
where f is the fidelity and M the number of observations, equal to
len(probabilities). This is better than logarithmic XEB (see below)
when fidelity is f < 0.32. Since this estimator is unbiased, the
variance is equal to the mean squared error of the estimator.
The estimator is intended for use with xeb_fidelity() below.
Args:
hilbert_space_dimension: Dimension of the Hilbert space on which
the channel whose fidelity is being estimated is defined.
probabilities: Ideal probabilities of bitstrings observed in
experiment.
Returns:
Estimate of fidelity associated with an experimental realization
of a quantum circuit.
"""
return hilbert_space_dimension * np.mean(probabilities).item() - 1
def log_xeb_fidelity_from_probabilities(
hilbert_space_dimension: int, probabilities: Sequence[float]
) -> float:
"""Logarithmic XEB fidelity estimator.
Estimates fidelity from ideal probabilities of observed bitstrings.
See `linear_xeb_fidelity_from_probabilities` for the assumptions made
by this estimator.
The mean of this estimator is the true fidelity f and the variance is
(pi^2/6 - f^2) / M
where f is the fidelity and M the number of observations, equal to
len(probabilities). This is better than linear XEB (see above) when
fidelity is f > 0.32. Since this estimator is unbiased, the variance
is equal to the mean squared error of the estimator.
The estimator is intended for use with xeb_fidelity() below.
Args:
hilbert_space_dimension: Dimension of the Hilbert space on which
the channel whose fidelity is being estimated is defined.
probabilities: Ideal probabilities of bitstrings observed in
experiment.
Returns:
Estimate of fidelity associated with an experimental realization
of a quantum circuit.
"""
return np.log(hilbert_space_dimension) + np.euler_gamma + np.mean(np.log(probabilities))
def hog_score_xeb_fidelity_from_probabilities(
hilbert_space_dimension: int, probabilities: Sequence[float]
) -> float:
"""XEB fidelity estimator based on normalized HOG score.
Estimates fidelity from ideal probabilities of observed bitstrings.
See `linear_xeb_fidelity_from_probabilities` for the assumptions made
by this estimator.
The mean of this estimator is the true fidelity f and the variance is
(1/log(2)^2 - f^2) / M
where f is the fidelity and M the number of observations, equal to
len(probabilities). This is always worse than log XEB (see above).
Since this estimator is unbiased, the variance is equal to the mean
squared error of the estimator.
The estimator is intended for use with xeb_fidelity() below. It is
based on the HOG problem defined in https://arxiv.org/abs/1612.05903.
Args:
hilbert_space_dimension: Dimension of the Hilbert space on which
the channel whose fidelity is being estimated is defined.
probabilities: Ideal probabilities of bitstrings observed in
experiment.
Returns:
Estimate of fidelity associated with an experimental realization
of a quantum circuit.
"""
score = np.mean(probabilities > np.log(2) / hilbert_space_dimension)
return (2 * score - 1) / np.log(2)
def xeb_fidelity(
circuit: Circuit,
bitstrings: Sequence[int],
qubit_order: QubitOrderOrList = QubitOrder.DEFAULT,
amplitudes: Optional[Mapping[int, complex]] = None,
estimator: Callable[[int, Sequence[float]], float] = linear_xeb_fidelity_from_probabilities,
) -> float:
"""Estimates XEB fidelity from one circuit using user-supplied estimator.
Fidelity quantifies the similarity of two quantum states. Here, we estimate
the fidelity between the theoretically predicted output state of circuit and
the state produced in its experimental realization. Note that we don't know
the latter state. Nevertheless, we can estimate the fidelity between the two
states from the knowledge of the bitstrings observed in the experiment.
In order to make the estimate more robust one should average the estimates
over many random circuits. The API supports per-circuit fidelity estimation
to enable users to examine the properties of estimate distribution over
many circuits.
See https://arxiv.org/abs/1608.00263 for more details.
Args:
circuit: Random quantum circuit which has been executed on quantum
processor under test.
bitstrings: Results of terminal all-qubit measurements performed after
each circuit execution as integer array where each integer is
formed from measured qubit values according to `qubit_order` from
most to least significant qubit, i.e. in the order consistent with
`cirq.final_state_vector`.
qubit_order: Qubit order used to construct bitstrings enumerating
qubits starting with the most significant qubit.
amplitudes: Optional mapping from bitstring to output amplitude.
If provided, simulation is skipped. Useful for large circuits
when an offline simulation had already been performed.
estimator: Fidelity estimator to use, see above. Defaults to the
linear XEB fidelity estimator.
Returns:
Estimate of fidelity associated with an experimental realization of
circuit which yielded measurements in bitstrings.
Raises:
ValueError: Circuit is inconsistent with qubit order or one of the
bitstrings is inconsistent with the number of qubits.
"""
dim = np.prod(circuit.qid_shape()).item()
if isinstance(bitstrings, tuple):
bitstrings = list(bitstrings)
for bitstring in bitstrings:
if not 0 <= bitstring < dim:
raise ValueError(
f'Bitstring {bitstring} could not have been observed '
f'on {len(circuit.qid_shape())} qubits.'
)
if amplitudes is None:
output_state = final_state_vector(circuit, qubit_order=qubit_order)
output_probabilities = state_vector_to_probabilities(output_state)
bitstring_probabilities = output_probabilities[bitstrings].tolist()
else:
bitstring_probabilities = [abs(amplitudes[bitstring]) ** 2 for bitstring in bitstrings]
return estimator(dim, bitstring_probabilities)
def linear_xeb_fidelity(
circuit: Circuit,
bitstrings: Sequence[int],
qubit_order: QubitOrderOrList = QubitOrder.DEFAULT,
amplitudes: Optional[Mapping[int, complex]] = None,
) -> float:
"""Estimates XEB fidelity from one circuit using linear estimator."""
return xeb_fidelity(
circuit,
bitstrings,
qubit_order,
amplitudes,
estimator=linear_xeb_fidelity_from_probabilities,
)
def log_xeb_fidelity(
circuit: Circuit,
bitstrings: Sequence[int],
qubit_order: QubitOrderOrList = QubitOrder.DEFAULT,
amplitudes: Optional[Mapping[int, complex]] = None,
) -> float:
"""Estimates XEB fidelity from one circuit using logarithmic estimator."""
return xeb_fidelity(
circuit, bitstrings, qubit_order, amplitudes, estimator=log_xeb_fidelity_from_probabilities
)