-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithm-exclusive-or-pairs-classical.js
88 lines (70 loc) · 1.99 KB
/
algorithm-exclusive-or-pairs-classical.js
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
const logger = require('../src/logger')()
const Bits = require('../src/bits')
// a classical illustration of Simon's algorithm
repeat(20, function() {
run()
})
function run() {
let host = new Host()
let oracle = new Oracle({ length: 5 })
let result = host.test(oracle)
logger.log('')
logger.log(`The host detected an oracle value of "${result}".`)
logger.log(`Does the oracle confirm this? ${oracle.confirm(result)}`)
logger.log('')
}
function Host() {
Object.assign(this, {
test: function(oracle) {
let answer = null
this.results = {}
repeat(Math.pow(2, oracle.length), function(index) {
let bitstring = Bits.fromNumber(index, oracle.length).toString()
let result = oracle.query(bitstring)
if (this.results[result] !== undefined) {
let a = Bits.fromString(bitstring)
let b = Bits.fromString(this.results[result])
answer = a.xor(b).toString()
return 'break'
} else {
this.results[result] = bitstring
}
}.bind(this))
return answer
}
})
}
function Oracle(options) {
Object.assign(this, {
initialize: function() {
this.length = options && options.length ? options.length : 4
let random = Math.floor(Math.random() * (Math.pow(2, this.length) - 1)) + 1 // the secret cannot be zero
this.secret = Bits.fromNumber(random, this.length).toString()
this.table = {}
let link = 0
repeat(Math.pow(2, this.length), function(index) {
let a = Bits.fromNumber(index, this.length).toString()
let b = this.secret
let xor = Bits.fromString(a).xor(Bits.fromString(b)).toString()
if (this.table[a] === undefined) {
link++
this.table[a] = link
this.table[xor] = link
}
}.bind(this))
},
query: function(value) {
return this.table[value]
},
confirm: function(value) {
return this.secret === value ? 'yes' : 'no'
}
})
this.initialize()
}
function repeat(number, fn) {
for (let i = 0; i < number; i++) {
let result = fn.apply(this, [i])
if (result === 'break') break
}
}