Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuhito committed Jun 6, 2024
1 parent b305182 commit 496ae71
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 73 deletions.
2 changes: 1 addition & 1 deletion apps/tutorial/serviceworker.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions apps/tutorial/serviceworker.js.map

Large diffs are not rendered by default.

13 changes: 4 additions & 9 deletions packages/simulator/src/matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ export class Matrix {
return ok(new Matrix(this.height, this.width, newBuffer))
}

mult(other: Matrix | number | Complex): Result<Matrix, Error> {
return other instanceof Matrix ? this.multMatrix(other) : ok(this.multScalar(other))
}

timesQubitOperation(operation2x2: Matrix, qubitIndex: number, controlMask: number): Matrix {
Util.need((controlMask & (1 << qubitIndex)) === 0, 'Matrix.timesQubitOperation: self-controlled')
Util.need(operation2x2.width === 2 && operation2x2.height === 2, 'Matrix.timesQubitOperation: not 2x2')
Expand Down Expand Up @@ -296,15 +300,6 @@ export class Matrix {
return new Matrix(h, w, newBuf)
}

/**
* Matrix multiplication.
*
* @param other - The matrix to multiply
*/
mult(other: Matrix | number | Complex): Result<Matrix, Error> {
return other instanceof Matrix ? this.multMatrix(other) : ok(this.multScalar(other))
}

/**
* Returns the tensor product of matrices.
*
Expand Down
173 changes: 112 additions & 61 deletions packages/simulator/test/matrix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,118 @@ describe('Matrix', () => {
})
})

describe('mult', () => {
test('* -1', () => {
expect(
M([
[new Complex(2, 3), new Complex(5, 7)],
[new Complex(11, 13), new Complex(17, 19)],
])
.mult(-1)
._unsafeUnwrap()
.eq(
M([
[new Complex(-2, -3), new Complex(-5, -7)],
[new Complex(-11, -13), new Complex(-17, -19)],
]),
),
).toBeTruthy()
})

test('* 0', () => {
const v = M([
[new Complex(2, 3), new Complex(5, 7)],
[new Complex(11, 13), new Complex(17, 19)],
])

expect(
v
.mult(0)
._unsafeUnwrap()
.eq(
M([
[0, 0],
[0, 0],
]),
),
).toBeTruthy()
})

test('* 1', () => {
const v = M([
[new Complex(2, 3), new Complex(5, 7)],
[new Complex(11, 13), new Complex(17, 19)],
])

expect(v.mult(1)._unsafeUnwrap().eq(v)).toBeTruthy()
})

test('* 5', () => {
expect(
M([
[1, 2],
[3, 4],
])
.mult(5)
._unsafeUnwrap()
.eq(
M([
[5, 10],
[15, 20],
]),
),
)
})

test('* M', () => {
expect(
M([
[2, 3],
[5, 7],
])
.mult(
M([
[11, 13],
[17, 19],
]),
)
._unsafeUnwrap()
.eq(
M([
[73, 83],
[174, 198],
]),
),
).toBeTruthy()
})

test('-iXYZ = I', () => {
expect(
X.mult(Y)
._unsafeUnwrap()
.mult(Z)
._unsafeUnwrap()
.mult(new Complex(0, -1))
._unsafeUnwrap()
.eq(
M([
[1, 0],
[0, 1],
]),
),
).toBeTruthy()
})

test('incompatible sizes error', () => {
const mErr = M([
[1, 2],
[3, 4],
]).mult(M([[0]]))

expect(mErr._unsafeUnwrapErr().message).toBe('Incompatible sizes.')
})
})

test('columnAt', () => {
const m = squareMatrix(2, 3, 5, 7)
expect(equate(m.columnAt(0)._unsafeUnwrap(), [2, 5])).toBeTruthy()
Expand Down Expand Up @@ -176,67 +288,6 @@ describe('Matrix', () => {
).toBeTruthy()
})

test('multScalar', () => {
const v = squareMatrix(new Complex(2, 3), new Complex(5, 7), new Complex(11, 13), new Complex(17, 19))
const a = squareMatrix(new Complex(-2, -3), new Complex(-5, -7), new Complex(-11, -13), new Complex(-17, -19))

expect(v.mult(-1)._unsafeUnwrap().eq(a)).toBeTruthy()
expect(v.mult(0)._unsafeUnwrap().eq(squareMatrix(0, 0, 0, 0))).toBeTruthy()
expect(v.mult(1)._unsafeUnwrap().eq(v)).toBeTruthy()

expect(
Matrix.column_vector(2, 3)
._unsafeUnwrap()
.mult(5)
._unsafeUnwrap()
.eq(Matrix.column_vector(10, 15)._unsafeUnwrap()),
).toBeTruthy()
expect(
Matrix.rows([[2, 3]])
._unsafeUnwrap()
.mult(5)
._unsafeUnwrap()
.eq(Matrix.rows([[10, 15]])._unsafeUnwrap()),
).toBeTruthy()
})

test('multMatrix', () => {
const mErr = squareMatrix(1, 2, 3, 4).mult(Matrix.column_vector(0)._unsafeUnwrap())
expect(mErr.isErr()).toBeTruthy()
expect(mErr._unsafeUnwrapErr().message).toBe('Incompatible sizes.')

expect(
squareMatrix(2, 3, 5, 7).mult(squareMatrix(11, 13, 17, 19))._unsafeUnwrap().eq(squareMatrix(73, 83, 174, 198)),
).toBeTruthy()

const x = squareMatrix(new Complex(0.5, -0.5), new Complex(0.5, 0.5), new Complex(0.5, 0.5), new Complex(0.5, -0.5))
expect(
x
.mult(x.adjoint())
._unsafeUnwrap()
.eq(
Matrix.rows([
[1, 0],
[0, 1],
])._unsafeUnwrap(),
),
).toBeTruthy()
expect(
X.mult(Y)
._unsafeUnwrap()
.mult(Z)
._unsafeUnwrap()
.mult(new Complex(0, -1))
._unsafeUnwrap()
.eq(
Matrix.rows([
[1, 0],
[0, 1],
])._unsafeUnwrap(),
),
).toBeTruthy()
})

test('tensorProduct', () => {
expect(
Matrix.column_vector(2)
Expand Down

0 comments on commit 496ae71

Please sign in to comment.