-
Notifications
You must be signed in to change notification settings - Fork 59
/
ch04_01_basic_teleportation.js
104 lines (92 loc) · 2.2 KB
/
ch04_01_basic_teleportation.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
// Programming Quantum Computers
// by Eric Johnston, Nic Harrigan and Mercedes Gimeno-Segovia
// O'Reilly Media
// To run this online, go to http://oreilly-qc.github.io?p=4-1
// This sample demonstrates basic teleportation.
qc.reset(3);
var alice = qint.new(1, 'alice');
var ep = qint.new(1, 'ep');
var bob = qint.new(1, 'bob');
var a1 = 0;
var a2 = 0;
// This will work with entangle() and alice_prep() in either order.
// Try swapping them to verify this.
entangle();
alice_prep();
alice_send();
bob_receive();
bob_verify();
function entangle()
{
// First, create an entangled pair
qc.write(0, 2|4);
qc.label('entangle');
ep.had();
bob.cnot(ep);
qc.label('');
}
function alice_prep()
{
// Alice prepares her payload to teleport
alice.write(0);
qc.label('prep payload');
alice.had();
alice.phase(45);
alice.had();
qc.label('');
qc.nop();
}
function alice_send()
{
// Alice sends the payload (and destroys it in the process)
qc.label('send');
ep.cnot(alice);
alice.had();
a1 = alice.read();
a2 = ep.read();
qc.label('');
qc.nop();
}
function bob_receive()
{
// Bob receives the payload, using the two bits Alice sent
qc.label('receive');
var bob_is_asleep = false;
var use_conditonals = true;
// Option 1: Bob is asleep (can't respond to Alice's data), so he just does whatever.
if (bob_is_asleep)
{
bob.phase(180);
bob.not();
}
// Option 2: Bob is responsive, and we use conditional-ops for visual clarity
else if (use_conditonals)
{
// Here, we use conditional gates, just for visual clarity.
// The "conditions" are on qubits which have already been read and
// turned into classical bits.
bob.cz(alice);
bob.cnot(ep);
}
// Option 3: Bob is responsive, and we use straightforward "if" in the code.
else
{
if (a1)
bob.phase(180);
if (a2)
bob.not();
}
qc.label('');
qc.nop();
}
function bob_verify()
{
// Verify that the teleportation worked
qc.label('verify');
bob.had();
bob.phase(-45);
bob.had();
bob.read();
qc.label('');
qc.nop();
}