Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

private new Matrix #431

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

47 changes: 37 additions & 10 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 @@ -96,14 +89,48 @@ export class Matrix {
return ok(new Matrix(width, height, buffer))
}

constructor(width: number, height: number, buffer: Float64Array) {
private constructor(width: number, height: number, buffer: Float64Array) {
this.width = width
this.height = height
this.buffer = buffer

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 = new Matrix(w, h, buf)
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(new Matrix(1, 1 << numQubits, buf))
// "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
Loading