/
rpe.py
185 lines (145 loc) · 6.55 KB
/
rpe.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
""" RPE Protocol objects """
# ***************************************************************************************************
# Copyright 2015, 2019 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
# Under the terms of Contract DE-NA0003525 with NTESS, the U.S. Government retains certain rights
# in this software.
# 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 or in the LICENSE file in the root pyGSTi directory.
# ***************************************************************************************************
from . import protocol as _proto
from ..algorithms.robust_phase_estimation import RobustPhaseEstimation as _rpe
from collections import OrderedDict, namedtuple
from argparse import Namespace
import numpy
class RobustPhaseEstimationDesign(_proto.CircuitListsDesign):
""" Experimental design for robust phase estimation """
def __init__(
self,
gate,
req_lengths,
sin_prep,
sin_meas,
sin_outcomes_pos,
sin_outcomes_neg,
cos_prep,
cos_meas,
cos_outcomes_pos,
cos_outcomes_neg,
*,
qubit_labels=None,
req_counts=None
):
"""
Produces an Experiment Design to test the phase that develops on a target
gate, by applying it req_lengths times to a states prepared by sin_prep
and cos_prep circuits, and then measured in the computational basis after
(respective) action by sin_meas and cos_meas circuits. outcomes_pos and
outcomes_neg determine which of those computational basis states count
towards each of the probabilities
P^{γ'γ}_{Ns} = |<γ' y| U^N |γ x>|² = |<γ' x| U^N |-γ y>|² = (1 ± sin(θ))/2
P^{γ'γ}_{Nc} = |<γ' x| U^N |γ x>|² = |<γ' y| U^N | γ y>|² = (1 ± cos(θ))/2
(Computational basis state measurements in neither of these sets are
silently dropped.)
In the above, the +x refers to the |E_0> + |E_1> combination of eigenstates
of U, *not* of computational basis states. For instance, if U is rotation
in the X basis, then cos_prep and cos_meas could be simply the identity:
|± U> = |0> ± |1>
where |±U> are the eigenstates of U, so that, in the notation of the above,
|+x> = |+U> + |-U> = |0>
The circuit would then calculate
P^+_{Nc} = |<+x| U^N | +x>|²
provided that cos_outcomes_pos = [0] and cos_outcomes_neg = [1].
"""
# TODO: Serialize these more naturally
self.sin_prep = (sin_prep,)
self.sin_meas = (sin_meas,)
self.sin_outcomes_pos = sin_outcomes_pos
self.sin_outcomes_neg = sin_outcomes_neg
self.cos_prep = (cos_prep,)
self.cos_meas = (cos_meas,)
self.cos_outcomes_pos = cos_outcomes_pos
self.cos_outcomes_neg = cos_outcomes_neg
self.gate = (gate,)
# What length circuits do we want to run?
self.req_counts = req_counts
self.req_lengths = req_lengths
# Actually build the circuits.
sin_circs = []
cos_circs = []
for N in req_lengths:
sin_circs.append(sin_prep + gate * N + sin_meas)
cos_circs.append(cos_prep + gate * N + cos_meas)
super().__init__([sin_circs, cos_circs], qubit_labels=qubit_labels)
self.auxfile_types["sin_prep"] = "text-circuit-list"
self.auxfile_types["sin_meas"] = "text-circuit-list"
self.auxfile_types["cos_prep"] = "text-circuit-list"
self.auxfile_types["cos_meas"] = "text-circuit-list"
self.auxfile_types["gate"] = "text-circuit-list"
class RobustPhaseEstimation(_proto.Protocol):
""" Robust phase estimation (RPE) protocol """
def _parse_row(self, row, outcomes_pos, outcomes_neg):
pos = 0
neg = 0
for i in outcomes_pos:
pos += row[i]
for i in outcomes_neg:
neg += row[i]
return pos, neg
def parse_dataset(self, design, dataset):
measured = OrderedDict()
for N, sin_circ, cos_circ in zip(design.req_lengths, *design.circuit_lists):
m = measured[N] = numpy.zeros(4, dtype=int)
m[:2] = self._parse_row(
dataset[sin_circ], design.sin_outcomes_pos, design.sin_outcomes_neg
)
m[2:] = self._parse_row(
dataset[cos_circ], design.cos_outcomes_pos, design.cos_outcomes_neg
)
return measured
def raw_angles(self, measured):
"""
Determine the raw angles from the count data. This corresponds to the angle of U^N,
i.e., it is N times the phase of U.
"""
angles = OrderedDict()
# The ordering here is chosen to maintain compatibility.
for N, (Cp_Ns, Cm_Ns, Cp_Nc, Cm_Nc) in measured.items():
# See the description of RobustPhaseEstimationDesign.
# We estimate P^+_{Ns} and P^-_{Nc} from the similarly named counts.
# The MLE for these probabilities is:
Pp_Ns = Cp_Ns / (Cp_Ns + Cm_Ns)
Pp_Nc = Cp_Nc / (Cp_Nc + Cm_Nc)
angles[N] = numpy.arctan2(2 * Pp_Ns - 1, 2 * Pp_Nc - 1) % (2 * numpy.pi)
return angles
def run(self, data, memlimit=None, comm=None):
meas = self.parse_dataset(data.edesign, data.dataset)
angles = self.raw_angles(meas)
_res = _rpe(Namespace(raw_angles=angles, _measured=meas))
ret = RobustPhaseEstimationResults(data, self, _res.angle_estimates)
return ret
class RobustPhaseEstimationResults(_proto.ProtocolResults):
""" Results from the RPE protocol """
def __init__(self, data, protocol_instance, angle_estimates):
"""
Produce an RPE results object, providing access to
- angle_estimates for each generation, and
- the RPE-estimated angle, angle_estimate, from the last generation
"""
super().__init__(data, protocol_instance)
self.angle_estimates = angle_estimates
@property
def angle_estimate(self):
return self.angle_estimates[-1]
@property
def measured_counts(self):
return self.protocol_instance.parse_dataset(
self.data.edesign, self.data.dataset
)
@property
def raw_angles(self):
return self.protocol_instance.rawn_angles(self.measured_counts)
# shorthands
RPEDesign = RobustPhaseEstimationDesign
RPE = RobustPhaseEstimation
RPEResults = RobustPhaseEstimationResults