Repository navigation
Expand file tree
/
Copy pathmatrix.ts
More file actions
259 lines (217 loc) · 5.85 KB
/
Copy pathmatrix.ts
File metadata and controls
259 lines (217 loc) · 5.85 KB
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
258
259
import { Complex } from "./complex";
export class Matrix {
readonly components: Complex[][];
constructor(components: Complex[][]) {
this.components = components;
}
static zero(width: number, height: number): Matrix {
let out = [];
for (let y = 0; y < height; y++) {
let row = [];
for (let x = 0; x < width; x++) {
row.push(new Complex(0, 0));
}
out.push(row);
}
return new Matrix(out);
}
at(col: number, row: number): Complex {
return this.components[row][col];
}
width(): number {
return this.components[0].length;
}
height(): number {
return this.components.length;
}
isEmpty(): boolean {
return this.height() == 0;
}
add(other: Matrix): Matrix {
this.assert_bounds(other);
let out: Complex[][] = [];
for (let row = 0; row < this.height(); row++) {
let newRow: Complex[] = [];
for (let col = 0; col < this.width(); col++) {
newRow.push(this.at(col, row).add(other.at(col, row)));
}
out.push(newRow);
}
return new Matrix(out);
}
sub(other: Matrix): Matrix {
this.assert_bounds(other);
let out: Complex[][] = [];
for (let row = 0; row < this.height(); row++) {
let newRow: Complex[] = [];
for (let col = 0; col < this.width(); col++) {
newRow.push(this.at(col, row).sub(other.at(col, row)));
}
out.push(newRow);
}
return new Matrix(out);
}
print() {
let out = "[";
for (let row = 0; row < this.height(); row++) {
out += "\n [ ";
for (let col = 0; col < this.width(); col++) {
out += this.at(col, row) + ", ";
}
out += "]";
}
out += "\n]";
console.log(out);
}
transpose(): Matrix {
let out: Complex[][] = [];
for (let col = 0; col < this.width(); col++) {
let newRow: Complex[] = [];
for (let row = 0; row < this.height(); row++) {
newRow.push(this.at(col, row));
}
out.push(newRow);
}
return new Matrix(out);
}
assert_bounds(other: Matrix) {
if (this.width() != other.width() || this.height() != other.height()) {
throw new Error(
`Invalid size: ${this.width()}x${this.height()} vs ${other.width()}x${other.height()}`
);
}
}
assertSquare() {
if (this.width() != this.height()) {
throw new Error(
`Invalid square dimensions ${this.width()} ${this.height()}`
);
}
}
equals(other: Matrix, epsilon: number = 0.00001): boolean {
this.assert_bounds(other);
for (let row = 0; row < this.height(); row++) {
for (let col = 0; col < this.width(); col++) {
if (!this.at(col, row).equals(other.at(col, row), epsilon)) {
return false;
}
}
}
return true;
}
mulScalar(c: Complex): Matrix {
let out: Complex[][] = [];
for (let row = 0; row < this.height(); row++) {
let newRow: Complex[] = [];
for (let col = 0; col < this.width(); col++) {
newRow.push(this.at(col, row).mul(c));
}
out.push(newRow);
}
return new Matrix(out);
}
conj(): Matrix {
let out: Complex[][] = [];
for (let row = 0; row < this.height(); row++) {
let newRow: Complex[] = [];
for (let col = 0; col < this.width(); col++) {
newRow.push(this.at(col, row).conj());
}
out.push(newRow);
}
return new Matrix(out);
}
adjoint(): Matrix {
return this.transpose().conj();
}
mulMatrix(other: Matrix): Matrix {
if (this.width() != other.height()) {
throw new Error(`invalid dimensions ${this.width()}, ${other.height()}`);
}
let out: Complex[][] = [];
for (let row = 0; row < this.height(); row++) {
let newRow: Complex[] = [];
for (let col = 0; col < other.width(); col++) {
let sum: Complex = new Complex(0, 0);
for (let i = 0; i < other.height(); i++) {
sum = sum.add(this.at(i, row).mul(other.at(col, i)));
}
newRow.push(sum);
}
out.push(newRow);
}
return new Matrix(out);
}
trace(): Complex {
if (this.width() != this.height()) {
throw new Error(
`invalid dimensions for trace ${this.width()}, ${this.height()}`
);
}
let out = new Complex(0, 0);
for (let i = 0; i < this.width(); i++) {
out = out.add(this.at(i, i));
}
return out;
}
isUnitary(): boolean {
this.assertSquare();
return this.mulMatrix(this.adjoint()).equals(identity(this.width()));
}
isHermitian(): boolean {
this.assertSquare();
return this.adjoint().equals(this);
}
assert_hermitian(msg: string) {
if (!this.isHermitian()) {
throw new Error("not hermitian: " + msg);
}
}
assert_unitary(msg: string = "") {
if (!this.isUnitary()) {
throw new Error("not unitary: " + msg);
}
}
tensor(other: Matrix): Matrix {
if (other.isEmpty()) {
return new Matrix(this.components);
}
if (this.isEmpty()) {
return new Matrix(other.components);
}
let out = [];
for (let y1 = 0; y1 < this.height(); y1++) {
for (let y2 = 0; y2 < other.height(); y2++) {
let row = [];
for (let x1 = 0; x1 < this.width(); x1++) {
for (let x2 = 0; x2 < other.width(); x2++) {
row.push(this.at(x1, y1).mul(other.at(x2, y2)));
}
}
out.push(row);
}
}
return new Matrix(out);
}
qubitCount(): number {
return Math.log2(this.height());
}
}
export function buildIdentityMatrix(qubit_count: number): Matrix {
return identity(2 ** qubit_count);
}
export function identity(width: number): Matrix {
let out = [];
for (let x = 0; x < width; x++) {
let row = [];
for (let y = 0; y < width; y++) {
if (x == y) {
row.push(new Complex(1, 0));
} else {
row.push(new Complex(0, 0));
}
}
out.push(row);
}
return new Matrix(out);
}