-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathadder_qasm.js
88 lines (67 loc) · 2.04 KB
/
adder_qasm.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 QuantumCircuit = require("../../lib/quantum-circuit.js");
var input = "";
input += "// quantum ripple-carry adder from Cuccaro et al, quant-ph/0410184\n";
input += "OPENQASM 2.0;\n";
input += "include \"qelib1.inc\";\n";
input += "gate majority a,b,c\n";
input += "{\n";
input += " cx c,b;\n";
input += " cx c,a;\n";
input += " ccx a,b,c;\n";
input += "}\n";
input += "gate unmaj a,b,c\n";
input += "{\n";
input += " ccx a,b,c;\n";
input += " cx c,a;\n";
input += " cx a,b;\n";
input += "}\n";
input += "qreg cin[1];\n";
input += "qreg a[4];\n";
input += "qreg b[4];\n";
input += "qreg cout[1];\n";
input += "creg ans[5];\n";
input += "// set input states\n";
input += "x a[0]; // a = 0001\n";
input += "x b; // b = 1111\n";
input += "// add a to b, storing result in b\n";
input += "majority cin[0],b[0],a[0];\n";
input += "majority a[0],b[1],a[1];\n";
input += "majority a[1],b[2],a[2];\n";
input += "majority a[2],b[3],a[3];\n";
input += "cx a[3],cout[0];\n";
input += "unmaj a[2],b[3],a[3];\n";
input += "unmaj a[1],b[2],a[2];\n";
input += "unmaj a[0],b[1],a[1];\n";
input += "unmaj cin[0],b[0],a[0];\n";
input += "measure b[0] -> ans[0];\n";
input += "measure b[1] -> ans[1];\n";
input += "measure b[2] -> ans[2];\n";
input += "measure b[3] -> ans[3];\n";
input += "measure cout[0] -> ans[4];\n";
var adder = new QuantumCircuit();
console.log("");
console.log("Importing QASM...");
adder.importQASM(input);
console.log("");
console.log(adder.exportQASM("Sum two numbers"));
console.log("");
console.log(adder.exportPyquil("Sum two numbers"));
//console.log("");
//console.log(JSON.stringify(adder.exportRaw(), null, '\t'));
console.log("");
console.log("Calculating...");
adder.run();
console.log("");
console.log("Answer:", adder.getCregValue("ans"));
console.log("");
console.log("Final amplitudes:");
adder.print(true);
console.log("");
console.log("Angles:");
console.log(adder.angles());
console.log("");
console.log("Probabilities:");
console.log(adder.probabilities());
console.log("");
console.log("Measured:");
console.log(adder.measureAll());