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

Add Matrix.rows #427

Merged
merged 7 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
12 changes: 6 additions & 6 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.

26 changes: 19 additions & 7 deletions packages/simulator/src/matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,26 @@ export class Matrix {
public buffer: Float64Array

/**
* Return a column vector.
*
* @param elements - The elements of the vector
* @returns A column vector (Matrix with 1 column)
* Creates a new Matrix object from the given rows.
*/
static col(...elements: Array<number | Complex>): Matrix {
const res = Matrix.build(elements.length, 1, row => elements[row])
return res._unsafeUnwrap()
static rows(rows: Array<Array<number | Complex>>): Result<Matrix, Error> {
const height = rows.length
if (height === 0) {
return err(Error('rows is empty'))
}
const width = rows[0].length
if (rows[0].length === 0) {
return err(Error('rows[0] is empty'))
}

return Matrix.build(height, width, (row, col) => rows[row][col])
}

/**
* Creates a column vector from the given elements.
*/
static column_vector(...elements: Array<number | Complex>): Result<Matrix, Error> {
return Matrix.build(elements.length, 1, row => elements[row])
}

/**
Expand Down
17 changes: 10 additions & 7 deletions packages/simulator/src/state-vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,32 +96,32 @@ export class StateVector {
switch (char) {
case '0': {
if (paren) throw invalidBitStringError
kets.push(Matrix.col(1, 0))
kets.push(Matrix.column_vector(1, 0)._unsafeUnwrap())
break
}
case '1': {
if (paren) throw invalidBitStringError
kets.push(Matrix.col(0, 1))
kets.push(Matrix.column_vector(0, 1)._unsafeUnwrap())
break
}
case '+': {
if (paren) throw invalidBitStringError
kets.push(Matrix.col(1, 1).mult(Math.sqrt(0.5))._unsafeUnwrap())
kets.push(Matrix.column_vector(1, 1)._unsafeUnwrap().mult(Math.sqrt(0.5))._unsafeUnwrap())
break
}
case '-': {
if (paren) {
parenToken += '-'
} else {
kets.push(Matrix.col(1, -1).mult(Math.sqrt(0.5))._unsafeUnwrap())
kets.push(Matrix.column_vector(1, -1)._unsafeUnwrap().mult(Math.sqrt(0.5))._unsafeUnwrap())
}
break
}
case 'i': {
if (paren) {
parenToken += 'i'
} else {
kets.push(Matrix.col(1, new Complex(0, 1)).mult(Math.sqrt(0.5))._unsafeUnwrap())
kets.push(Matrix.column_vector(1, new Complex(0, 1))._unsafeUnwrap().mult(Math.sqrt(0.5))._unsafeUnwrap())
}
break
}
Expand All @@ -134,7 +134,7 @@ export class StateVector {
case ')': {
if (!paren) throw invalidBitStringError
if (parenToken !== '-i') throw invalidBitStringError
kets.push(Matrix.col(1, new Complex(0, -1)).mult(Math.sqrt(0.5))._unsafeUnwrap())
kets.push(Matrix.column_vector(1, new Complex(0, -1))._unsafeUnwrap().mult(Math.sqrt(0.5))._unsafeUnwrap())
paren = false
break
}
Expand Down Expand Up @@ -181,7 +181,10 @@ export class StateVector {
.times(this.matrix.element(0, bra)._unsafeUnwrap().conjugate())
if (amp.isEqualTo(0)) continue

const ketMat = removeBits(ket, traceBits) === 0 ? Matrix.col(1, 0) : Matrix.col(0, 1)
const ketMat =
removeBits(ket, traceBits) === 0
? Matrix.column_vector(1, 0)._unsafeUnwrap()
: Matrix.column_vector(0, 1)._unsafeUnwrap()
const braMat = removeBits(bra, traceBits) === 0 ? Matrix.row(1, 0) : Matrix.row(0, 1)
const ketBra = ketMat.mult(braMat)._unsafeUnwrap()

Expand Down
Loading
Loading