-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic-increment-superposition.js
122 lines (97 loc) · 2.48 KB
/
logic-increment-superposition.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
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
const logger = require('../src/logger')()
const Bits = require('../src/bits')
const Sequence = require('../src/sequence')
increment()
decrement()
function increment() {
input()
output()
function input() {
let circuit = Circuit(`the inputs in superposition before incrementing`, 5)
.initialize()
let shots = 100, tally = {}
repeat(shots, function() {
let result = circuit.run().measure().toNumber()
tally[result] = tally[result] || 0
tally[result]++
})
logger.log(`\nThe inputs are ${JSON.stringify(Object.keys(tally))}\n`)
}
function output() {
let circuit = Circuit(`the outputs in superposition after incrementing`, 5)
.initialize()
.increment()
let shots = 100, tally = {}
repeat(shots, function() {
let result = circuit.run().measure().toNumber()
tally[result] = tally[result] || 0
tally[result]++
})
logger.log(`\nThe incremented outputs are ${JSON.stringify(Object.keys(tally))}\n`)
}
}
function decrement() {
input()
output()
function input() {
let circuit = Circuit(`the inputs in superposition before decrementing`, 5)
.initialize()
let shots = 100, tally = {}
repeat(shots, function() {
let result = circuit.run().measure().toNumber()
tally[result] = tally[result] || 0
tally[result]++
})
logger.log(`\nThe inputs are ${JSON.stringify(Object.keys(tally))}\n`)
}
function output() {
let circuit = Circuit(`the outputs in superposition after decrementing`, 5)
.initialize()
.decrement()
let shots = 100, tally = {}
repeat(shots, function() {
let result = circuit.run().measure().toNumber()
tally[result] = tally[result] || 0
tally[result]++
})
logger.log(`\nThe decremented outputs are ${JSON.stringify(Object.keys(tally))}\n`)
}
}
function Circuit(name, size) {
let circuit = require('../src/circuit.js')({
name: name,
size: size,
logger: logger,
engine: 'optimized',
order: ['targets', 'controls']
})
return Object.assign(circuit, {
initialize: function() {
return this
.x(1)
.h(2)
.t(2)
},
increment: function() {
return this.sequence().apply(circuit)
},
decrement: function() {
return this.sequence().reverse().apply(circuit)
},
sequence: function() {
return new Sequence()
.ccx(4, [0, 1])
.ccx(3, [4, 2])
.ccx(4, [0, 1])
.ccx(3, [0, 1])
.cx(1, [0])
.x(0)
}
})
}
function repeat(number, fn) {
for (let i = 0; i < number; i++) {
let result = fn.apply(this, [i])
if (result === 'break') break
}
}