Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuhito committed Jun 18, 2024
1 parent 7ff10e4 commit a959dbb
Showing 1 changed file with 36 additions and 44 deletions.
80 changes: 36 additions & 44 deletions packages/simulator/test/state-vector.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Matrix, StateVector} from '../src/'
import {Complex, equate} from '@qni/common'
import {Matrix, StateVector} from '../src'
import {Complex} from '@qni/common'
import {H} from '../src/gate-matrices'
// eslint-disable-next-line import/no-nodejs-modules
import {performance} from 'perf_hooks'
Expand All @@ -10,42 +10,42 @@ describe('StateVector', () => {
describe('creation', () => {
test('|0>', () => {
stateVector = new StateVector('0')
expect(equate(stateVector.toString(), '{{1}, {0}}')).toBeTruthy()
expect(stateVector.toString()).toBe('{{1}, {0}}')
})

test('|1>', () => {
stateVector = new StateVector('1')
expect(equate(stateVector.toString(), '{{0}, {1}}')).toBeTruthy()
expect(stateVector.toString()).toBe('{{0}, {1}}')
})

test('|+>', () => {
stateVector = new StateVector('+')
expect(equate(stateVector.toString(), '{{√½}, {√½}}')).toBeTruthy()
expect(stateVector.toString()).toBe('{{√½}, {√½}}')
})

test('|->', () => {
stateVector = new StateVector('-')
expect(equate(stateVector.toString(), '{{√½}, {-√½}}')).toBeTruthy()
expect(stateVector.toString()).toBe('{{√½}, {-√½}}')
})

test('|i>', () => {
stateVector = new StateVector('i')
expect(equate(stateVector.toString(), '{{√½}, {√½i}}')).toBeTruthy()
expect(stateVector.toString()).toBe('{{√½}, {√½i}}')
})

test('|-i>', () => {
stateVector = new StateVector('(-i)')
expect(equate(stateVector.toString(), '{{√½}, {-√½i}}')).toBeTruthy()
expect(stateVector.toString()).toBe('{{√½}, {-√½i}}')
})

test('|00>', () => {
stateVector = new StateVector('00')
expect(equate(stateVector.toString(), '{{1}, {0}, {0}, {0}}')).toBeTruthy()
expect(stateVector.toString()).toBe('{{1}, {0}, {0}, {0}}')
})

test('|0(-i)>', () => {
stateVector = new StateVector('0(-i)')
expect(equate(stateVector.toString(), '{{√½}, {-√½i}, {0}, {0}}')).toBeTruthy()
expect(stateVector.toString()).toBe('{{√½}, {-√½i}, {0}, {0}}')
})

test('create with an invalid bit string', () => {
Expand All @@ -57,43 +57,42 @@ describe('StateVector', () => {
describe('size', () => {
test('one qubit', () => {
stateVector = new StateVector('0')
expect(equate(stateVector.size, 2)).toBeTruthy()
expect(stateVector.size).toBe(2)
})

test('two qubits', () => {
stateVector = new StateVector('00')
expect(equate(stateVector.size, 4)).toBeTruthy()
expect(stateVector.size).toBe(4)
})
})

describe('nqubit', () => {
test('one qubit', () => {
stateVector = new StateVector('0')
expect(equate(stateVector.nqubit, 1)).toBeTruthy()
expect(stateVector.nqubit).toBe(1)
})

test('two qubits', () => {
stateVector = new StateVector('00')
expect(equate(stateVector.nqubit, 2)).toBeTruthy()
expect(stateVector.nqubit).toBe(2)
})
})

describe('bra', () => {
test('<0|', () => {
stateVector = new StateVector('0')
expect(equate(stateVector.bra, Matrix.rows([[1, 0]])._unsafeUnwrap())).toBeTruthy()
expect(stateVector.bra.eq(Matrix.rows([[1, 0]])._unsafeUnwrap())).toBeTruthy()
})

test('<1|', () => {
stateVector = new StateVector('1')
expect(equate(stateVector.bra, Matrix.rows([[0, 1]])._unsafeUnwrap())).toBeTruthy()
expect(stateVector.bra.eq(Matrix.rows([[0, 1]])._unsafeUnwrap())).toBeTruthy()
})

test('<+|', () => {
stateVector = new StateVector('+')
expect(
equate(
stateVector.bra,
stateVector.bra.eq(
Matrix.rows([[1, 1]])
._unsafeUnwrap()
.mult(Math.sqrt(0.5))
Expand All @@ -105,8 +104,7 @@ describe('StateVector', () => {
test('<-|', () => {
stateVector = new StateVector('-')
expect(
equate(
stateVector.bra,
stateVector.bra.eq(
Matrix.rows([[1, -1]])
._unsafeUnwrap()
.mult(Math.sqrt(0.5))
Expand All @@ -118,8 +116,7 @@ describe('StateVector', () => {
test('<i|', () => {
stateVector = new StateVector('i')
expect(
equate(
stateVector.bra,
stateVector.bra.eq(
Matrix.rows([[1, new Complex(0, -1)]])
._unsafeUnwrap()
.mult(Math.sqrt(0.5))
Expand All @@ -131,8 +128,7 @@ describe('StateVector', () => {
test('<-i|', () => {
stateVector = new StateVector('(-i)')
expect(
equate(
stateVector.bra,
stateVector.bra.eq(
Matrix.rows([[1, new Complex(0, 1)]])
._unsafeUnwrap()
.mult(Math.sqrt(0.5))
Expand All @@ -143,14 +139,13 @@ describe('StateVector', () => {

test('<00|', () => {
stateVector = new StateVector('00')
expect(equate(stateVector.bra, Matrix.rows([[1, 0, 0, 0]])._unsafeUnwrap())).toBeTruthy()
expect(stateVector.bra.eq(Matrix.rows([[1, 0, 0, 0]])._unsafeUnwrap())).toBeTruthy()
})

test('<0(-i)|', () => {
stateVector = new StateVector('0(-i)')
expect(
equate(
stateVector.bra,
stateVector.bra.eq(
Matrix.rows([[1, new Complex(0, 1), 0, 0]])
._unsafeUnwrap()
.mult(Math.sqrt(0.5))
Expand All @@ -163,33 +158,32 @@ describe('StateVector', () => {
describe('ket', () => {
test('|0>', () => {
stateVector = new StateVector('0')
expect(equate(stateVector.ket, Matrix.column_vector(1, 0)._unsafeUnwrap())).toBeTruthy()
expect(stateVector.ket.eq(Matrix.column_vector(1, 0)._unsafeUnwrap())).toBeTruthy()
})

test('|1>', () => {
stateVector = new StateVector('1')
expect(equate(stateVector.ket, Matrix.column_vector(0, 1)._unsafeUnwrap())).toBeTruthy()
expect(stateVector.ket.eq(Matrix.column_vector(0, 1)._unsafeUnwrap())).toBeTruthy()
})

test('|+>', () => {
stateVector = new StateVector('+')
expect(
equate(stateVector.ket, Matrix.column_vector(1, 1)._unsafeUnwrap().mult(Math.sqrt(0.5))._unsafeUnwrap()),
stateVector.ket.eq(Matrix.column_vector(1, 1)._unsafeUnwrap().mult(Math.sqrt(0.5))._unsafeUnwrap()),
).toBeTruthy()
})

test('|->', () => {
stateVector = new StateVector('-')
expect(
equate(stateVector.ket, Matrix.column_vector(1, -1)._unsafeUnwrap().mult(Math.sqrt(0.5))._unsafeUnwrap()),
stateVector.ket.eq(Matrix.column_vector(1, -1)._unsafeUnwrap().mult(Math.sqrt(0.5))._unsafeUnwrap()),
).toBeTruthy()
})

test('|i>', () => {
stateVector = new StateVector('i')
expect(
equate(
stateVector.ket,
stateVector.ket.eq(
Matrix.column_vector(1, new Complex(0, 1))._unsafeUnwrap().mult(Math.sqrt(0.5))._unsafeUnwrap(),
),
).toBeTruthy()
Expand All @@ -198,23 +192,21 @@ describe('StateVector', () => {
test('|-i>', () => {
stateVector = new StateVector('(-i)')
expect(
equate(
stateVector.ket,
stateVector.ket.eq(
Matrix.column_vector(1, new Complex(0, -1))._unsafeUnwrap().mult(Math.sqrt(0.5))._unsafeUnwrap(),
),
).toBeTruthy()
})

test('|00>', () => {
stateVector = new StateVector('00')
expect(equate(stateVector.ket, Matrix.column_vector(1, 0, 0, 0)._unsafeUnwrap())).toBeTruthy()
expect(stateVector.ket.eq(Matrix.column_vector(1, 0, 0, 0)._unsafeUnwrap())).toBeTruthy()
})

test('|0(-i)>', () => {
stateVector = new StateVector('0(-i)')
expect(
equate(
stateVector.ket,
stateVector.ket.eq(
Matrix.column_vector(1, new Complex(0, -1), 0, 0)._unsafeUnwrap().mult(Math.sqrt(0.5))._unsafeUnwrap(),
),
).toBeTruthy()
Expand All @@ -224,8 +216,8 @@ describe('StateVector', () => {
describe('amplifier', () => {
test('get amplifiers', () => {
stateVector = new StateVector('0')
expect(equate(stateVector.amplifier(0), 1)).toBeTruthy()
expect(equate(stateVector.amplifier(1), 0)).toBeTruthy()
expect(stateVector.amplifier(0).eq(1)).toBeTruthy()
expect(stateVector.amplifier(1).eq(0)).toBeTruthy()
expect(() => stateVector.amplifier(2)).toThrow()
})
})
Expand All @@ -237,12 +229,12 @@ describe('StateVector', () => {

test('set first amplifier', () => {
stateVector.setAmplifier(0, Complex.ZERO)
expect(equate(stateVector.toString(), '{{0}, {0}}')).toBeTruthy()
expect(stateVector.toString()).toBe('{{0}, {0}}')
})

test('set last amplifier', () => {
stateVector.setAmplifier(1, Complex.ONE)
expect(equate(stateVector.toString(), '{{1}, {1}}')).toBeTruthy()
expect(stateVector.toString()).toBe('{{1}, {1}}')
})

test('amplifier out of range', () => {
Expand All @@ -256,7 +248,7 @@ describe('StateVector', () => {
describe('blochVector', () => {
test('|0> (bit 0)', () => {
stateVector = new StateVector('0')
expect(equate(stateVector.blochVector(0), [0, 0, 1])).toBeTruthy()
expect(stateVector.blochVector(0)).toEqual([0, 0, 1])
})

test('|0> (bit 1)', () => {
Expand All @@ -271,7 +263,7 @@ describe('StateVector', () => {

test('|1> (bit 0)', () => {
stateVector = new StateVector('1')
expect(equate(stateVector.blochVector(0), [0, 0, -1])).toBeTruthy()
expect(stateVector.blochVector(0)).toEqual([0, 0, -1])
})

test('|+> (bit 0)', () => {
Expand Down

0 comments on commit a959dbb

Please sign in to comment.