-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuperdense.js
67 lines (53 loc) · 1.21 KB
/
superdense.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
const logger = require('../src/logger')()
const Bits = require('../src/bits')
encode(0)
encode(1)
encode(2)
encode(3)
function encode(value) {
let circuit = Circuit(`superdense value: ${value}`, 2)
let alice = circuit.alice()
let bob = circuit.bob()
circuit.entangle(alice, bob)
alice.encode(value)
circuit
.detangle(alice, bob)
.run()
let result = circuit.measure().toNumber()
logger.log(`The value encoded to Alice is ${value}.`)
logger.log(`The value decoded from Alice and Bob together is ${result}.\n`)
}
function Circuit(name, size, options) {
let circuit = require('../src/circuit.js')({
name: name,
size: size,
logger: logger,
engine: 'optimized',
order: ['targets', 'controls']
})
return Object.assign(circuit, {
alice: function() {
let alice = this.unit(0)
return Object.assign(alice, {
encode: function(value) {
let array = Bits.fromNumber(value, 2).toArray()
if (array.pop()) alice.z()
if (array.pop()) alice.x()
}
})
},
bob: function() {
return this.unit(1)
},
entangle: function(alice, bob) {
alice.h()
bob.cx(0)
return this
},
detangle: function(alice, bob) {
bob.cx(0)
alice.h()
return this
}
})
}