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

Neverthrow Matrix.square #394

Merged
merged 5 commits into from
Aug 20, 2023
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
20 changes: 10 additions & 10 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.

32 changes: 21 additions & 11 deletions packages/simulator/src/gate-matrices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const mi = i.neg()
* | 1 -1 |
* ```
*/
export const H = Matrix.square(1, 1, 1, -1).times(Math.sqrt(0.5))
export const H = safeSquareMatrix(1, 1, 1, -1).times(Math.sqrt(0.5))

/**
* Pauli X gate.
Expand All @@ -22,7 +22,7 @@ export const H = Matrix.square(1, 1, 1, -1).times(Math.sqrt(0.5))
* | 1 0 |
* ```
*/
export const X = Matrix.square(0, 1, 1, 0)
export const X = safeSquareMatrix(0, 1, 1, 0)

/**
* Pauli Y gate.
Expand All @@ -32,7 +32,7 @@ export const X = Matrix.square(0, 1, 1, 0)
* | i 0 |
* ```
*/
export const Y = Matrix.square(0, mi, i, 0)
export const Y = safeSquareMatrix(0, mi, i, 0)

/**
* Pauli Z gate.
Expand All @@ -42,7 +42,7 @@ export const Y = Matrix.square(0, mi, i, 0)
* | 0 -1 |
* ```
*/
export const Z = Matrix.square(1, 0, 0, -1)
export const Z = safeSquareMatrix(1, 0, 0, -1)

/**
* S gate.
Expand All @@ -52,7 +52,7 @@ export const Z = Matrix.square(1, 0, 0, -1)
* | 0 i |
* ```
*/
export const S = Matrix.square(1, 0, 0, i)
export const S = safeSquareMatrix(1, 0, 0, i)

/**
* S† gate.
Expand All @@ -72,7 +72,7 @@ export const SDagger = S.adjoint()
* | 0 exp(iπ/4) |
* ```
*/
export const T = Matrix.square(1, 0, 0, i.times(Math.PI / 4).exp())
export const T = safeSquareMatrix(1, 0, 0, i.times(Math.PI / 4).exp())

/**
* T† gate.
Expand All @@ -95,7 +95,7 @@ export const TDagger = T.adjoint()
export function PHASE(phi: string): Matrix {
const φ = radian(phi)

return Matrix.square(1, 0, 0, i.times(φ).exp())
return safeSquareMatrix(1, 0, 0, i.times(φ).exp())
}

/**
Expand All @@ -106,7 +106,7 @@ export function PHASE(phi: string): Matrix {
* | 1-i 1+i |
* ```
*/
export const RNOT = Matrix.square(i.plus(1), mi.plus(1), mi.plus(1), i.plus(1)).times(0.5)
export const RNOT = safeSquareMatrix(i.plus(1), mi.plus(1), mi.plus(1), i.plus(1)).times(0.5)

/**
* Rx gate.
Expand All @@ -128,7 +128,7 @@ export function RX(theta: string): Matrix {
const cosθ2 = Math.cos(θ / 2)
const sinθ2 = Math.sin(θ / 2)

return Matrix.square(cosθ2, mi.times(sinθ2), mi.times(sinθ2), cosθ2)
return safeSquareMatrix(cosθ2, mi.times(sinθ2), mi.times(sinθ2), cosθ2)
}

/**
Expand All @@ -151,7 +151,7 @@ export function RY(theta: string): Matrix {
const cosθ2 = Math.cos(θ / 2)
const sinθ2 = Math.sin(θ / 2)

return Matrix.square(cosθ2, -sinθ2, sinθ2, cosθ2)
return safeSquareMatrix(cosθ2, -sinθ2, sinθ2, cosθ2)
}

/**
Expand All @@ -172,5 +172,15 @@ export function RY(theta: string): Matrix {
export function RZ(theta: string): Matrix {
const θ = radian(theta)

return Matrix.square(mi.times(θ / 2).exp(), 0, 0, i.times(θ / 2).exp())
return safeSquareMatrix(mi.times(θ / 2).exp(), 0, 0, i.times(θ / 2).exp())
}

function safeSquareMatrix(...elements: Array<number | Complex>): Matrix {
const res = Matrix.square(...elements)

if (res.isOk()) {
return res.value
}

throw res.error
}
35 changes: 22 additions & 13 deletions packages/simulator/src/matrix.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Complex, DetailedError, Format, Util} from '@qni/common'
import {range} from 'fp-ts/NonEmptyArray'
import {ok, err, Result} from 'neverthrow'

type FormatOptions = {
allowAbbreviation?: boolean
Expand All @@ -23,21 +24,36 @@ export class Matrix {
/**
* Generates a 1x1 matrix.
*
* @param element - The element of the matrix.
* @returns A 1x1 Matrix.
* @param element - The element of the matrix
* @returns A 1x1 matrix
*/
static solo(element: number | Complex): Matrix {
return new Matrix(1, 1, new Float64Array([Complex.real(element), Complex.imag(element)]))
}

/**
* Generates a square matrix with the specified elements.
*
* @param elements - The elements of the matrix
* @returns A result object with the generated square matrix or an error
*/
static square(...elements: Array<number | Complex>): Result<Matrix, Error> {
const n = Math.round(Math.sqrt(elements.length))
if (n * n !== elements.length) {
return err(Error('Matrix.square: non-square number of arguments'))
}

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

/**
* Generates a matrix of the specified `width` and `height`.
* The matrix elements are generated by `matrixElementGenerator` with row and column indexes as arguments.
*
* @param width - The width of the matrix.
* @param height - The height of the matrix.
* @param matrixElementGenerator - A function that generates the matrix elements.
* @returns A matrix of the specified width and height.
* @param width - The width of the matrix
* @param height - The height of the matrix
* @param matrixElementGenerator - A function that generates the matrix elements
* @returns A matrix of the specified width and height
*/
static generate(
width: number,
Expand All @@ -59,13 +75,6 @@ export class Matrix {
return new Matrix(width, height, buf)
}

static square(...coefs: Array<number | Complex>): Matrix {
Util.need(Array.isArray(coefs), 'Array.isArray(coefs)', coefs)
const n = Math.round(Math.sqrt(coefs.length))
Util.need(n * n === coefs.length, 'Matrix.square: non-square number of arguments')
return Matrix.generate(n, n, (r, c) => coefs[r * n + c])
}

static col(...coefs: Array<number | Complex>): Matrix {
Util.need(Array.isArray(coefs), 'Array.isArray(coefs)', coefs)
return Matrix.generate(1, coefs.length, r => coefs[r])
Expand Down
Loading
Loading