-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathQTM.js
257 lines (202 loc) · 6.87 KB
/
QTM.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
"use strict";
/**
* Created by sdiemert on 2017-03-14.
*/
const math = require("mathjs");
const util = require("util");
class Configuration{
/**
* @param cid {number}
* @param i {number} head position
* @param T {Array} tape
* @param q {number} state
* @param a {math.Complex=1} coefficient for the configuration.
*/
constructor(cid, i, T, q, a){
this.configurationId = cid;
this.headPosition = i;
this.tape = JSON.parse(JSON.stringify(T));
this.machineState = q;
/** @type {math.Complex} */
this.coefficent = null;
if(a !== undefined && a !== null) this.coefficent = math.complex(a);
else this.coefficent = math.complex(1,0);
/** @type {number} */
this.amplitudeSquared = math.pow(this.coefficent.re, 2) + math.pow(this.coefficent.im, 2);
}
/**
*
* @param z {Complex}
* @param p {number} fixed point precision
*/
complexToString(z, p){
if(z){
return z.re.toFixed(p) + (z.im !==0 ? ("i" + z.im.toFixed(p)) : "");
}else{
return "";
}
}
toString(){
let S = "{ id: "+this.configurationId+", state: " + this.machineState
+ (this.coefficent !== null ? ", coeff : "+this.complexToString(this.coefficent, 2) : "")
+ (this.amplitudeSquared !== null ? ", amp^2: "+ this.amplitudeSquared.toFixed(2) : "")
+", tape: ";
let s = "";
for(let i = 0; i < this.tape.length; i++){
if(this.tape[i] === 2) s = "#";
else s = this.tape[i];
if(i === this.headPosition){
S += "["+s+"] ";
}else{
S += s+" ";
}
}
S += "}";
return S;
}
setCoefficent(c){
this.coefficent = c;
this.amplitudeSquared = math.pow(this.coefficent.re, 2) + math.pow(this.coefficent.im, 2);
}
}
class QTM{
/**
* @param U {math.Matrix}
* @param numStates {number}
* @param start {number}
* @param tapeLength {number}
* @param halt {number}
*/
constructor(U, numStates, start, tapeLength, halt){
/** @type U {math.Matrix} */
this.U = U;
this.tapeLength = tapeLength;
this.numStates = numStates;
this.start = start;
this.halt = halt || (numStates - 1); // default is highest state number
this.V = null;
}
/**
* Returns the index of a 1 for the state vector.
* @param i {number} tape cell number
* @param T {Array} the input tape
*/
indexFromState(i,T){
let j = (i*this.numStates)*math.pow(3, this.tapeLength) + this.start*math.pow(3,this.tapeLength);
for(let y = 0; y < this.tapeLength; y++){
j = j + math.pow(3,y)*T[y];
}
return j;
}
/**
*
* @param i {number} the index into the state vector
*
* @return {Configuration}
*/
stateFromIndex(i){
const index = i;
const pow3t = Math.pow(3, this.tapeLength);
const head = Math.floor(i / (this.numStates * pow3t));
const state = Math.floor((i - (head * this.numStates * pow3t)) / pow3t);
i = Math.floor(i - head * state * pow3t);
let tape = [];
for(let k = this.tapeLength - 1; k >= 0; k--){
tape.push(i % 3);
i = Math.floor(i / 3);
}
return new Configuration(index, head,tape,state);
}
/**
* @returns {Configuration[]}
* @private
*/
getSuperposition(){
const L = this.tapeLength * this.numStates * math.pow(3, this.tapeLength);
let v = null, r = null, R = [];
for(let i = 0; i < L; i++){
v = math.subset(this.V, math.index(i,0));
if(v != 0){
r = this.stateFromIndex(i);
r.setCoefficent(math.complex(v));
R.push(r);
}
}
if(R.length === 0) R.push("empty");
return R
}
/**
* Executes the QTM. The machine will halt if: 1) the number of steps taken exceeds in provided bound, or
* 2) the machine ends up in a superposition of the halting state.
*
* @param T {Array} an array of integers (0,1,2) to represent the tape, must be same size as machine.
* @param i {number} the index to start the head of the machine at on the tape, defaults to zero.
* @param n {number} an upper bound on the number of steps to allow the machine to take.
* @param fn {function=} call this function after each machine step, passes machine reference.
*
* @return nothing, finishes when the machine has halted (after n steps or when halting state is reached).
*/
execute(T, i, n, fn) {
if (i === null || i === undefined) i = 0;
// TODO : Check tape length is OK.
this.V = math.zeros(this.U.size()[0], 1);
let j = this.indexFromState(0, T);
this.V = math.subset(this.V, math.index(j,0), 1);
if(fn) fn(this);
for (let i = 0; i < n && !this.haltingSuperposition(); i++) {
this.V = math.multiply(this.U, this.V);
if(fn) fn(this);
}
}
/**
* Determines if the machine is in a superposition of halting states.
*
* @return {boolean}
*/
haltingSuperposition(){
if(this.start === this.halt) return false; //if the start and halting state are the same we have a problem.
const S = this.getSuperposition();
for(let s = 0; s < S.length; s++){
if(S[s].machineState !== this.halt) return false;
}
return true;
}
_checkRow(i){
let u, v;
for(let k = 0; k < this.U.size()[0]; k++){
u = math.subset(this.U, math.index(i,k));
if(u != 0){
v = math.subset(this.V, math.index(k, 0));
console.log(u, v);
}
}
}
/**
* Measures the current state of the QTM - does this using "real" quantum
* measurement, i.e. collapses the super position and returns a machine
* configuration based on a probabilistic measure.
*
* @return {Configuration}
*/
measure(){
let S = this.getSuperposition();
const r = Math.random(); // random between 0 and 1.
let s = 0;
for(let i = 0; i < S.length; i++){
if(r >= s && r < s + S[i].amplitudeSquared){
S[i].amplitudeSquared = null;
S[i].coefficent = null;
return S[i]; // return the Configuration
}
s += S[i].amplitudeSquared;
}
}
/**
* @return {string}
*/
toString(){
return "{ numStates : " + this.numStates +", tapeLength: "
+ this.tapeLength + ", start:" + this.start + ", halt: " + this.halt + ", U: " + this.U.size()[0] + " }"
}
}
module.exports = {QTM : QTM};