@qbead/bloch-sphere
Version:
A 3D Bloch Sphere visualisation built with Three.js and TypeScript.
120 lines (109 loc) • 2.28 kB
text/typescript
import { Complex } from './complex'
import { Operator } from './operator'
/**
* The identity operator
*/
export function identity() {
return Operator.identity()
}
/**
* The Pauli-X operator (also known as NOT or bit-flip operator)
*/
export function x() {
return new Operator([
[],
[],
])
}
/**
* Alias for the Pauli-X operator
*/
export const not = x
/**
* The Pauli-Y operator
*/
export function y() {
return new Operator([
[],
[],
])
}
/**
* The Pauli-Z operator
*/
export function z() {
return new Operator([
[],
[],
])
}
/**
* The Hadamard operator
*
* Often used to create superposition states from the |0> state
*/
export function hadamard() {
return x()
.plus(z())
.scale(1 / Math.sqrt(2))
}
/**
* The phase operator
*
* Applies a phase of `phi` to the |1> state
*/
export function phase(phi: number) {
return new Operator([
[],
[],
])
}
// TODO check these
/**
* Rotation around the X axis
* Applies a rotation of `theta` radians around the X axis
*/
export function rx(theta: number) {
return new Operator([
[
Complex.from(Math.cos(theta / 2), 0),
Complex.I.times(Math.sin(theta / 2)),
],
[
Complex.I.times(Math.sin(theta / 2)),
Complex.from(Math.cos(theta / 2), 0),
],
])
}
/**
* Rotation around the Y axis
* Applies a rotation of `theta` radians around the Y axis
*/
export function ry(theta: number) {
return new Operator([
[
Complex.from(Math.cos(theta / 2), 0),
Complex.from(-Math.sin(theta / 2), 0),
],
[
Complex.from(Math.sin(theta / 2), 0),
Complex.from(Math.cos(theta / 2), 0),
],
])
}
/**
* Rotation around the Z axis
* Applies a rotation of `theta` radians around the Z axis
*/
export function rz(theta: number) {
return new Operator([
[
Complex.from(Math.cos(theta / 2), 0),
Complex.I.times(Math.sin(theta / 2)),
],
[
Complex.I.times(Math.sin(theta / 2)),
Complex.from(Math.cos(theta / 2), 0),
],
])
}