Skip to content

Commit

Permalink
Matrix.create を private にする
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuhito committed Jun 5, 2024
1 parent 3356911 commit 176859c
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 69 deletions.
16 changes: 8 additions & 8 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.

45 changes: 36 additions & 9 deletions packages/simulator/src/matrix.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Complex, DetailedError, Format} from '@qni/common'
import {Complex, DetailedError, Format, Util} from '@qni/common'
import {range} from 'fp-ts/NonEmptyArray'
import {ok, err, Result} from 'neverthrow'

Expand Down Expand Up @@ -75,14 +75,7 @@ export class Matrix {
return Matrix.create(width, height, buf)
}

/**
* Returns a matrix of the specified `width` and `height`.
*
* @param width - The width of the matrix
* @param height - The height of the matrix
* @param buffer - The buffer containing the matrix elements
*/
static create(width: number, height: number, buffer: Float64Array): Result<Matrix, Error> {
private static create(width: number, height: number, buffer: Float64Array): Result<Matrix, Error> {
if (width < 0) {
return err(Error(`width(${width}) < 0`))
}
Expand All @@ -104,6 +97,40 @@ export class Matrix {
this.plus = this.add // alias for add
}

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')

const {width: w, height: h, buffer: old} = this
const [ar, ai, br, bi, cr, ci, dr, di] = operation2x2.buffer

Util.need(h >= 2 << qubitIndex, 'Matrix.timesQubitOperation: qubit index out of range')

const buf = new Float64Array(old)
let i = 0
for (let r = 0; r < h; r++) {
const isControlled = ((controlMask & r) ^ controlMask) !== 0
const qubitVal = (r & (1 << qubitIndex)) !== 0
for (let c = 0; c < w; c++) {
if (!isControlled && !qubitVal) {
const j = i + (1 << qubitIndex) * 2 * w
const xr = buf[i]
const xi = buf[i + 1]
const yr = buf[j]
const yi = buf[j + 1]

buf[i] = xr * ar - xi * ai + yr * br - yi * bi
buf[i + 1] = xr * ai + xi * ar + yr * bi + yi * br
buf[j] = xr * cr - xi * ci + yr * dr - yi * di
buf[j + 1] = xr * ci + xi * cr + yr * di + yi * dr
}
i += 2
}
}

return Matrix.create(w, h, buf)._unsafeUnwrap()
}

/**
* Returns element (col,row) of the matrix.
*
Expand Down
34 changes: 2 additions & 32 deletions packages/simulator/src/state-vector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Complex, DetailedError, Util} from '@qni/common'
import {Complex, DetailedError} from '@qni/common'
import {Matrix} from './matrix'
import {ok, err, Result} from 'neverthrow'

Expand Down Expand Up @@ -48,37 +48,7 @@ export class StateVector {
}

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')

const {width: w, height: h, buffer: old} = this.matrix
const [ar, ai, br, bi, cr, ci, dr, di] = operation2x2.buffer

Util.need(h >= 2 << qubitIndex, 'Matrix.timesQubitOperation: qubit index out of range')

const buf = new Float64Array(old)
let i = 0
for (let r = 0; r < h; r++) {
const isControlled = ((controlMask & r) ^ controlMask) !== 0
const qubitVal = (r & (1 << qubitIndex)) !== 0
for (let c = 0; c < w; c++) {
if (!isControlled && !qubitVal) {
const j = i + (1 << qubitIndex) * 2 * w
const xr = buf[i]
const xi = buf[i + 1]
const yr = buf[j]
const yi = buf[j + 1]

buf[i] = xr * ar - xi * ai + yr * br - yi * bi
buf[i + 1] = xr * ai + xi * ar + yr * bi + yi * br
buf[j] = xr * cr - xi * ci + yr * dr - yi * di
buf[j + 1] = xr * ci + xi * cr + yr * di + yi * dr
}
i += 2
}
}

this.matrix = Matrix.create(w, h, buf)._unsafeUnwrap()
this.matrix = this.matrix.timesQubitOperation(operation2x2, qubitIndex, controlMask)
return this.matrix
}

Expand Down
14 changes: 0 additions & 14 deletions packages/simulator/test/matrix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,6 @@ describe('Matrix', () => {
expect(m._unsafeUnwrap().toString()).toBe('{{0, 10, 20}, {1, 11, 21}}')
})

test('Matrix.create', () => {
const mErrWidth = Matrix.create(-1, 1, new Float64Array(2))
expect(mErrWidth.isErr()).toBeTruthy()
expect(mErrWidth._unsafeUnwrapErr().message).toBe('width(-1) < 0')

const mErrHeight = Matrix.create(1, -1, new Float64Array(2))
expect(mErrHeight.isErr()).toBeTruthy()
expect(mErrHeight._unsafeUnwrapErr().message).toBe('height(-1) < 0')

const mErrBufferLength = Matrix.create(1, 1, new Float64Array(100))
expect(mErrBufferLength.isErr()).toBeTruthy()
expect(mErrBufferLength._unsafeUnwrapErr().message).toBe('width(1)*height(1)*2 !== buffer.length(100)')
})

test('columnAt', () => {
const m = squareMatrix(2, 3, 5, 7)
expect(equate(m.columnAt(0)._unsafeUnwrap(), [2, 5])).toBeTruthy()
Expand Down
13 changes: 10 additions & 3 deletions packages/simulator/test/state-vector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,17 @@ describe('StateVector', () => {
const numQubits = 10
const numOps = 100
const t0 = performance.now()
const buf = new Float64Array(2 << numQubits)
let bitString
// const buf = new Float64Array(2 << numQubits)

buf[0] = 1
stateVector = new StateVector(Matrix.create(1, 1 << numQubits, buf)._unsafeUnwrap())
// "000...0" (0 が numQubits 個) の文字列を bitString に代入
bitString = ''
for (let i = 0; i < numQubits; i++) {
bitString += '0'
}

// buf[0] = 1
stateVector = new StateVector(bitString)

for (let i = 0; i < numOps; i++) {
stateVector.timesQubitOperation(H, 0, 6)
Expand Down

0 comments on commit 176859c

Please sign in to comment.