-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.go
233 lines (219 loc) · 7.3 KB
/
bench.go
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
// bench.go runs a round of BB84 key negotiation for each entry in the cartesion
// product of a collection of different tuning parameters, e.g. observed error
// rate and quantum bits exchanged, and outputs a CSV of relevant statistics for
// each different combination, e.g. messages exchanged and final key length.
package main
import (
"bytes"
"fmt"
"html/template"
"log"
"math/rand"
"net"
"os"
"strings"
"github.com/alan-christopher/bb84/go/bb84"
"github.com/alan-christopher/bb84/go/bb84/bitmap"
"github.com/alan-christopher/bb84/go/bb84/photon"
flag "github.com/spf13/pflag"
)
var (
qBatch = flag.IntSlice("qBatch", []int{bb84.DefaultMeasurementBatchBytes},
"The bytes of raw quantum information to exchange per round of basis announcement.")
nX = flag.IntSlice("nX", []int{int(1e5)}, "The minimum number of main basis bits to accumulate before EC/priv amp/etc.")
nZ = flag.IntSlice("nZ", []int{int(5e4)}, "The minimum number of test basis bits to accumulate before EC/priv amp/etc.")
pX = flag.Float64Slice("pX", []float64{0.7}, "The probability of sending a bit in the main basis.")
muLo = flag.Float64Slice("muLo", []float64{0.05}, "The mean photons per pulse of the low intensity preparation.")
muMed = flag.Float64Slice("muMed", []float64{0.1}, "The mean photons per pulse of the medium intensity preparation.")
muHi = flag.Float64Slice("muHi", []float64{0.3}, "The mean photons per pulse of the high intensity preparation.")
pLo = flag.Float64Slice("pLo", []float64{0.34}, "The proportion of low intensity photon pulses.")
pMed = flag.Float64Slice("pMed", []float64{0.33}, "The proportion of medium intensity photon pulses.")
pHi = flag.Float64Slice("pHi", []float64{0.33}, "The proportion of high intensity photon pulses.")
qber = flag.Float64Slice("qber", []float64{0.01}, "The qbers to observe when bases align.")
)
var (
inputs = []string{"qBatch", "nX", "nZ", "pX", "muLo", "muMed", "muHi", "pLo", "pMed", "pHi", "qber"}
// TODO: consider using reflection to pull this out of the Experiment data
// type.
columns = []string{"QBatchBytes", "NX", "NZ", "PX", "MuLo", "MuMed", "MuHi",
"PLo", "PMed", "PHi", "QBER", "Pulses", "QBits", "EmpiricalQBER", "KeyBits",
"AliceMessages", "BobMessages", "AliceClassicalBytes", "BobClassicalBytes",
"Succeeded"}
)
// An Experiment packages together the result of benchmarking a single
// parameterization for easy formatting.
type Experiment struct {
// Fields corresponding to experiment parameters
QBatchBytes int
NX, NZ int
PX float64
MuLo, MuMed, MuHi float64
PLo, PMed, PHi float64
QBER float64
// Fields corresponding to experiment results
Pulses int
QBits int
EmpiricalQBER float64
KeyBits int
AliceMessages int
BobMessages int
AliceClassicalBytes int
BobClassicalBytes int
Succeeded bool
}
func main() {
flag.Parse()
fmt.Println(header())
tmpl := template.Must(template.New("line").Parse(lineTmpl()))
var args [][]interface{}
for _, inp := range inputs {
args = append(args, lookupInput(inp))
}
applyCartesian(func(args []interface{}) {
exp := &Experiment{
QBatchBytes: args[inpIndex("qBatch")].(int),
NX: args[inpIndex("nX")].(int),
NZ: args[inpIndex("nZ")].(int),
PX: args[inpIndex("pX")].(float64),
MuLo: args[inpIndex("muLo")].(float64),
MuMed: args[inpIndex("muMed")].(float64),
MuHi: args[inpIndex("muHi")].(float64),
PLo: args[inpIndex("pLo")].(float64),
PMed: args[inpIndex("pMed")].(float64),
PHi: args[inpIndex("pHi")].(float64),
QBER: args[inpIndex("qber")].(float64),
}
if err := bench(exp); err != nil {
log.Printf("Benching %v: %v", exp, err)
}
if err := tmpl.Execute(os.Stdout, exp); err != nil {
log.Fatalf("BUG: could not fill in line template: %v", err)
}
}, args)
}
func inpIndex(v string) int {
for i, inp := range inputs {
if inp == v {
return i
}
}
return -1
}
func bench(exp *Experiment) error {
l, r := net.Pipe()
pa := bb84.PulseAttrs{}
pa.MuLo, pa.MuMed, pa.MuHi = exp.MuLo, exp.MuMed, exp.MuHi
pa.ProbLo, pa.ProbMed, pa.ProbHi = exp.PLo, exp.PMed, exp.PHi
sender, receiver := photon.NewSimulatedChannel(
exp.PX, // pMain
pa.MuLo, // muLo
pa.MuMed, // muMed
pa.MuHi, // muHi
pa.ProbLo, // pLo
pa.ProbMed, // pMed
pa.ProbHi, // pHi
rand.New(rand.NewSource(1234)), // sendrand
rand.New(rand.NewSource(5678)), // receiveRand
)
otp := make([]byte, 1<<23) // TODO: the amount of otp to create should be derived from experiment parameters
rand.Read(otp)
a, err := bb84.NewPeer(bb84.PeerOpts{
Sender: sender,
ClassicalChannel: l,
Rand: rand.New(rand.NewSource(42)),
Secret: bytes.NewBuffer(otp),
WinnowOpts: &bb84.WinnowOpts{
Iters: []int{3, 3, 3, 4, 6, 7, 7, 7},
SyncRand: rand.New(rand.NewSource(17)),
},
PulseAttrs: pa,
MeasurementBatchBytes: exp.QBatchBytes,
MainBlockSize: exp.NX,
TestBlockSize: exp.NZ,
})
if err != nil {
return err
}
b, err := bb84.NewPeer(bb84.PeerOpts{
Receiver: receiver,
ClassicalChannel: r,
Rand: rand.New(rand.NewSource(1337)),
Secret: bytes.NewBuffer(otp),
WinnowOpts: &bb84.WinnowOpts{
Iters: []int{3, 3, 3, 4, 6, 7, 7, 7},
SyncRand: rand.New(rand.NewSource(17)),
},
PulseAttrs: pa,
MeasurementBatchBytes: exp.QBatchBytes,
MainBlockSize: exp.NX,
TestBlockSize: exp.NZ,
})
if err != nil {
return err
}
batchBits := exp.QBatchBytes * 8
legitErrs := bitmap.NewDense(nil, batchBits)
for i := 0; i < int(float64(batchBits)*exp.QBER); i++ {
legitErrs.Flip(i)
}
legitErrs.Shuffle(rand.New(rand.NewSource(99)))
receiver.Errors = legitErrs.Data()
go b.NegotiateKey()
k, stats, err := a.NegotiateKey()
exp.Pulses = stats.Pulses
exp.QBits = stats.QBits
exp.EmpiricalQBER = stats.QBER
exp.KeyBits = k.Size()
exp.AliceMessages = stats.MessagesSent
exp.BobMessages = stats.MessagesSent
exp.AliceClassicalBytes = stats.BytesSent
exp.BobClassicalBytes = stats.BytesRead
exp.Succeeded = err == nil
return err
}
func header() string {
return strings.Join(columns, ", ")
}
func lineTmpl() string {
var els []string
for _, c := range columns {
els = append(els, "{{."+c+"}}")
}
return strings.Join(els, ", ") + "\n"
}
func lookupInput(name string) []interface{} {
var r []interface{}
if v, err := flag.CommandLine.GetIntSlice(name); err == nil {
for _, val := range v {
r = append(r, val)
}
} else if v, err := flag.CommandLine.GetFloat64Slice(name); err == nil {
for _, val := range v {
r = append(r, val)
}
} else {
log.Fatalf("Unknown type for input %s", name)
}
return r
}
func applyCartesian(f func([]interface{}), args [][]interface{}) {
for i := range args {
if len(args[i]) == 1 {
continue
}
l := make([][]interface{}, len(args))
r := make([][]interface{}, len(args))
copy(l, args)
copy(r, args)
l[i] = args[i][:1]
r[i] = args[i][1:]
applyCartesian(f, l)
applyCartesian(f, r)
return
}
x := make([]interface{}, 0, len(args))
for _, a := range args {
x = append(x, a[0])
}
f(x)
}