Skip to content

Commit 941864d

Browse files
authored
Add files via upload
1 parent cc6fc8a commit 941864d

36 files changed

+1355
-0
lines changed

API.docs.md

Lines changed: 438 additions & 0 deletions
Large diffs are not rendered by default.

HelperMath.js

Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
1+
function Complex(real, imaginary)
2+
{
3+
if (typeof real === "number") real = new Decimal(real);
4+
if (typeof imaginary === "number") imaginary = new Decimal(imaginary);
5+
6+
this.Real = real;
7+
this.Imaginary = imaginary;
8+
9+
this.Sub = function(comp)
10+
{
11+
return new Complex(this.Real.sub(comp.Real), this.Imaginary.sub(comp.Imaginary));
12+
};
13+
14+
this.Add = function(comp)
15+
{
16+
return new Complex(this.Real.add(comp.Real), this.Imaginary.add(comp.Imaginary));
17+
};
18+
19+
this.Mult = function(comp)
20+
{
21+
return new Complex(this.Real.mul(comp.Real).sub(this.Imaginary.mul(comp.Imaginary)), this.Real.mul(comp.Imaginary).add(comp.Real.mul(this.Imaginary)));
22+
};
23+
24+
this.Inverse = function()
25+
{
26+
return new Complex(new Decimal(1).div(this.Real), new Decimal(1).div(this.Imaginary));
27+
};
28+
29+
this.Negate = function()
30+
{
31+
return new Complex(this.Real.negated(), this.Imaginary.negated());
32+
};
33+
34+
this.Div = function(comp)
35+
{
36+
return this.Mult(this, comp.Inverse());
37+
};
38+
39+
this.Abs = function()
40+
{
41+
return new Complex(this.Real.abs(), this.Imaginary.abs());
42+
}
43+
44+
this.toString = function()
45+
{
46+
return this.Real.toNumber() + "+" + this.Imaginary.toNumber() + "i";
47+
};
48+
49+
this.Sqr = function()
50+
{
51+
//return new Complex(this.Real.pow(2).sub(this.Imaginary.pow(2)), this.Imaginary.mul(this.Real).mul(2));
52+
return new Complex(this.Real.mul(this.Real).sub(this.Imaginary.mul(this.Imaginary)), this.Imaginary.mul(this.Real).mul(2));
53+
}
54+
55+
this.Clone = function()
56+
{
57+
return new Complex(this.Real, this.Imaginary);
58+
};
59+
60+
return this;
61+
}
62+
63+
Number.prototype.Comp = function()
64+
{
65+
var a = Number(this);
66+
//console.log(a);
67+
//console.log(new Decimal(a));
68+
return new Complex(new Decimal(a), new Decimal(0));
69+
}
70+
71+
//Accepts only (Decimal) objects
72+
//https://en.wikipedia.org/wiki/Euler%27s_formula
73+
function eulerFormula(theta)
74+
{
75+
return new Complex(theta.cos(), theta.sin());
76+
}
77+
78+
function invEulerFormula(comp)
79+
{
80+
return new Decimal(comp.Imaginary.div(comp.Real).atan());
81+
}
82+
83+
function Matrix(width, height, opt = true)
84+
{
85+
//TODO: implement constructor
86+
87+
this.Width = width;
88+
this.Height = height;
89+
90+
this.Rotated = false;
91+
if (opt && this.Height > this.Width) this.Rotated = true;
92+
93+
if (!this.Rotated)
94+
{
95+
this.Vals = new Array(this.Height);
96+
var h = this.Height;
97+
for (var i = 0; i < h; i++)
98+
{
99+
var row = new Array(this.Width);
100+
var w = this.Width;
101+
for (var j = 0; j < w; j++) row[j] = new Complex(new Decimal(0), new Decimal(0));
102+
this.Vals[i] = row;
103+
//this.Vals[i] = [...row];
104+
//this.Vals[i] = //row.slice();
105+
//this.Vals[i] = Array(width).fill(new Complex(new Decimal(0), new Decimal(0)));
106+
}
107+
}
108+
else
109+
{
110+
this.Vals = new Array(this.Width);
111+
var w = this.Width;
112+
for (var i = 0; i < w; i++)
113+
{
114+
var row = new Array(this.Height);
115+
var h = this.Height;
116+
for (var j = 0; j < h; j++) row[j] = new Complex(new Decimal(0), new Decimal(0));
117+
this.Vals[i] = row;
118+
//this.Vals[i] = [...row];
119+
//this.Vals[i] = //row.slice();
120+
//this.Vals[i] = Array(width).fill(new Complex(new Decimal(0), new Decimal(0)));
121+
}
122+
}
123+
124+
125+
this.Set = function(i, j, v)
126+
{
127+
if (this.Rotated) { this.Vals[i][j] = v; return; };
128+
this.Vals[j][i] = v;
129+
};
130+
131+
this.Get = function(i, j)
132+
{
133+
if (this.Rotated) return this.Vals[i][j];
134+
return this.Vals[j][i];
135+
};
136+
137+
this.Mult = function(mat)
138+
{
139+
//if (this.Width != mat.Height) throw "Dim Mismatch";
140+
var res = new Matrix(mat.Width, this.Height);
141+
//console.log(res);
142+
for (var i = 0; i < this.Height; i++)
143+
{
144+
for (var j = 0; j < mat.Width; j++)
145+
{
146+
var sum = new Complex(new Decimal(0), new Decimal(0));
147+
for (var k = 0; k < this.Width; k++)
148+
{
149+
var a = this.Get(k, i);
150+
var b = mat.Get(j, k);
151+
sum = sum.Add(a.Mult(b));
152+
}
153+
//console.log(j, i, sum.toString());
154+
res.Set(j, i, sum);
155+
}
156+
}
157+
158+
return res;
159+
};
160+
161+
this.Rotate90 = function()
162+
{
163+
var res = new Matrix(this.Height, this.Width);
164+
165+
for (var i = 0; i < this.Width; i++)
166+
{
167+
for (var j = 0; j < this.Height; j++)
168+
{
169+
res.Set(this.Height - j - 1, i, this.Get(i, j));
170+
}
171+
}
172+
173+
return res;
174+
};
175+
176+
this.CounterRotate90 = function()
177+
{
178+
var res = new Matrix(this.Height, this.Width);
179+
180+
for (var i = 0; i < this.Width; i++)
181+
{
182+
for (var j = 0; j < this.Height; j++)
183+
{
184+
res.Set(j, this.Width - i - 1, this.Get(i, j));
185+
}
186+
}
187+
188+
return res;
189+
};
190+
191+
this.VerticleFlatten = function()
192+
{
193+
if (this.Rotated) return this.Vals[0];
194+
var res = new Array(this.Height);
195+
for (var i = 0; i < this.Height; i++) res[i] = this.Get(0, i);
196+
return res;
197+
};
198+
199+
this.toString = function()
200+
{
201+
var ret = "";
202+
for (var i = 0; i < this.Height; i++)
203+
{
204+
var str = "";
205+
for (var j = 0; j < this.Width; j++)
206+
{
207+
str += this.Get(j, i).toString() + ",";
208+
}
209+
ret += str + "\n"
210+
//console.log(str);
211+
}
212+
return ret;
213+
};
214+
215+
this.FlatFromArr = function(arr, horiz = false)
216+
{
217+
var res = undefined;
218+
if (!horiz) res = new Matrix(1, arr.length);
219+
else res = new Matrix(arr.length, 1);
220+
for (var i = 0; i < arr.length; i++)
221+
{
222+
if (!horiz) res.Set(0, i, arr[i]);
223+
else res.Set(i, 0, arr[i]);
224+
}
225+
return res;
226+
};
227+
228+
this.From2dArr = function(arr2d)
229+
{
230+
var mat = new Matrix(arr2d[0].length, arr2d.length);
231+
for (var i = 0; i < arr2d[0].length; i++)
232+
{
233+
for (var j = 0; j < arr2d.length; j++)
234+
{
235+
mat.Set(i, j, arr2d[i][j]);
236+
}
237+
}
238+
return mat;
239+
};
240+
241+
this.From2dRealArr = function(arr2d)
242+
{
243+
var mat = new Matrix(arr2d[0].length, arr2d.length);
244+
for (var i = 0; i < arr2d[0].length; i++)
245+
{
246+
for (var j = 0; j < arr2d.length; j++)
247+
{
248+
mat.Set(i, j, arr2d[j][i].Comp());
249+
}
250+
}
251+
return mat;
252+
};
253+
254+
this.From2dComplexArr = function(arr2d)
255+
{
256+
var mat = new Matrix(arr2d[0].length, arr2d.length);
257+
for (var i = 0; i < arr2d[0].length; i++)
258+
{
259+
for (var j = 0; j < arr2d.length; j++)
260+
{
261+
mat.Set(i, j, arr2d[j][i]);
262+
}
263+
}
264+
return mat;
265+
};
266+
267+
this.Scalar = function(a)
268+
{
269+
var res = new Matrix(this.Width, this.Height);
270+
for (var i = 0; i < res.Width; i++)
271+
{
272+
for (var j = 0; j < res.Height; j++)
273+
{
274+
res.Set(i, j, this.Get(i, j).Mult(a));
275+
}
276+
}
277+
return res;
278+
};
279+
280+
return this;
281+
}
282+
283+
// var a = new Matrix(3,2);
284+
// a.Vals = [[new Complex(new Decimal(1), new Decimal(0)),new Complex(new Decimal(2), new Decimal(0)),new Complex(new Decimal(3), new Decimal(0))],[new Complex(new Decimal(4), new Decimal(0)),new Complex(new Decimal(5), new Decimal(0)),new Complex(new Decimal(6), new Decimal(0))]];
285+
286+
// var b = new Matrix(2,3);
287+
// b.Vals = [[new Complex(new Decimal(7), new Decimal(0)),new Complex(new Decimal(8), new Decimal(0))],[new Complex(new Decimal(9), new Decimal(0)),new Complex(new Decimal(10), new Decimal(0))],[new Complex(new Decimal(11), new Decimal(0)),new Complex(new Decimal(12), new Decimal(0))]];
288+
289+
// var c = a.Mult(b);
290+
// c.toString();
291+
292+
//N
293+
294+
// var a = new Matrix(3,2);
295+
// a.Vals = [[new Complex(new Decimal(6), new Decimal(0)),new Complex(new Decimal(5), new Decimal(0)),new Complex(new Decimal(4), new Decimal(0))],[new Complex(new Decimal(3), new Decimal(0)),new Complex(new Decimal(2), new Decimal(0)),new Complex(new Decimal(1), new Decimal(0))]];
296+
297+
// var b = new Matrix(2,3);
298+
// b.Vals = [[new Complex(new Decimal(7), new Decimal(0)),new Complex(new Decimal(8), new Decimal(0))],[new Complex(new Decimal(9), new Decimal(0)),new Complex(new Decimal(10), new Decimal(0))],[new Complex(new Decimal(11), new Decimal(0)),new Complex(new Decimal(12), new Decimal(0))]];
299+
300+
// var c = a.Mult(b);
301+
// c.toString();

Performance.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function PerformanceEval()
2+
{
3+
for (var i = 0; i < 20; i++)
4+
{
5+
var qNum = i + 1;
6+
var qSim = new QVM(qNum);
7+
var t0 = performance.now();
8+
qSim.Reset();
9+
//qSim.Run();
10+
var t1 = performance.now();
11+
var td = t1 - t0;
12+
console.log(qNum, td);
13+
if (td > 10) return qNum - 1;
14+
}
15+
}

SimpleQVM.Gates.js

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)