Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuhito committed Jun 4, 2024
1 parent 433143d commit ece4820
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
14 changes: 7 additions & 7 deletions apps/tutorial/serviceworker.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions apps/tutorial/serviceworker.js.map

Large diffs are not rendered by default.

27 changes: 15 additions & 12 deletions packages/simulator/src/matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,35 @@ export class Matrix {
* @returns A column vector (Matrix with 1 column)
*/
static col(...elements: Array<number | Complex>): Matrix {
const res = Matrix.generate(1, elements.length, row => elements[row])
const res = Matrix.build(elements.length, 1, row => elements[row])
return res._unsafeUnwrap()
}

/**
* Generates a matrix with the specified width and height.
* Builds a matrix with the specified width and height, using the provided
* function to generate each element.
*/
static generate(
width: number,
static build(
height: number,
width: number,
matrixElementGenerator?: (row: number, col: number) => number | Complex,
): Result<Matrix, Error> {
const buf = new Float64Array(width * height * 2)

for (let row = 0; row < height; row++) {
for (let col = 0; col < width; col++) {
const i = (row * width + col) * 2
let c
const ri = (row * width + col) * 2 // real part index
const ii = ri + 1 // imaginary part index

let v: Complex
if (matrixElementGenerator) {
c = Complex.from(matrixElementGenerator(row, col))
v = Complex.from(matrixElementGenerator(row, col))
} else {
c = Complex.from(0)
v = Complex.from(0)
}

buf[i] = c.real
buf[i + 1] = c.imag
buf[ri] = v.real
buf[ii] = v.imag
}
}

Expand All @@ -67,7 +70,7 @@ export class Matrix {
* @returns A row vector (Matrix with 1 row)
*/
static row(...elements: Array<number | Complex>): Matrix {
const res = Matrix.generate(elements.length, 1, (_row, col) => elements[col])
const res = Matrix.build(1, elements.length, (_row, col) => elements[col])
return res._unsafeUnwrap()
}

Expand Down Expand Up @@ -98,7 +101,7 @@ export class Matrix {
return err(Error('Matrix.square: non-square number of arguments'))
}

return Matrix.generate(n, n, (row, col) => elements[row * n + col])
return Matrix.build(n, n, (row, col) => elements[row * n + col])
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/simulator/src/state-vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export class StateVector {
}, num)
}

let densityMatrix = Matrix.generate(2, 2)._unsafeUnwrap()
let densityMatrix = Matrix.build(2, 2)._unsafeUnwrap()

for (let bra = 0; bra < this.matrix.height; bra++) {
for (let ket = 0; ket < this.matrix.height; ket++) {
Expand Down
2 changes: 1 addition & 1 deletion packages/simulator/test/matrix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Matrix', () => {
})

test('Matrix.generate', () => {
const res = Matrix.generate(3, 2, (row, col) => row + 10 * col)
const res = Matrix.build(2, 3, (row, col) => row + 10 * col)

expect(res.isOk()).toBeTruthy()
expect(res._unsafeUnwrap().toString()).toBe('{{0, 10, 20}, {1, 11, 21}}')
Expand Down

0 comments on commit ece4820

Please sign in to comment.