Skip to content

Commit

Permalink
Add unit tests for BlochSphereState (bits-and-electrons#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
saich08 authored Feb 22, 2021
1 parent cc71486 commit 2997441
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 27 deletions.
12 changes: 6 additions & 6 deletions src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,12 @@ var GlobalContext = {
GlobalContext.blochSphereOperation.inProgress = false;
ToolboxEvents.enableQuantumGates();

// Get Current QuantumState
let quantumState = GlobalContext.blochSphere.quantumState;
// Get BlochSphereState
let blochSphereState = GlobalContext.blochSphere.blochSphereState;

// Save Theta & Phi
GlobalContext.blochSphereState.theta = quantumState.theta.toFixed(PRECISION);
GlobalContext.blochSphereState.phi = quantumState.phi.toFixed(PRECISION);
GlobalContext.blochSphereState.theta = blochSphereState.theta.toFixed(PRECISION);
GlobalContext.blochSphereState.phi = blochSphereState.phi.toFixed(PRECISION);

// Save Workspace
ExportWorkspaceEvents.saveWorkspace();
Expand All @@ -228,12 +228,12 @@ var GlobalContext = {
if (GlobalContext.blochSphereOperation.rotation > 0) {
// Apply Delta Quantum Operation
GlobalContext.blochSphereOperation.rotation -= 1;
GlobalContext.blochSphere.updateQuantumState(GlobalContext.blochSphereOperation.transformationAxis, THREE.Math.degToRad(1));
GlobalContext.blochSphere.updateBlochSphereState(GlobalContext.blochSphereOperation.transformationAxis, THREE.Math.degToRad(1));
}
else {
// Apply Delta Quantum Operation
GlobalContext.blochSphereOperation.rotation += 1;
GlobalContext.blochSphere.updateQuantumState(GlobalContext.blochSphereOperation.transformationAxis, THREE.Math.degToRad(-1));
GlobalContext.blochSphere.updateBlochSphereState(GlobalContext.blochSphereOperation.transformationAxis, THREE.Math.degToRad(-1));
}

// Update BlochSphere State
Expand Down
18 changes: 9 additions & 9 deletions src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ var ToolboxEvents = {

var BlochSphereStateEvents = {
updateBlochSphereState: function () {
// Get Current QuantumState
let quantumState = GlobalContext.blochSphere.quantumState;
// Get BlochSphereState
let blochSphereState = GlobalContext.blochSphere.blochSphereState;

// Update Theta & Phi
$("#bloch-sphere-state-theta").html(quantumState.theta.toFixed(PRECISION));
$("#bloch-sphere-state-phi").html(quantumState.phi.toFixed(PRECISION));
$("#bloch-sphere-state-theta").html(blochSphereState.theta.toFixed(PRECISION));
$("#bloch-sphere-state-phi").html(blochSphereState.phi.toFixed(PRECISION));

// Update Alpha & Beta
$("#bloch-sphere-state-alpha").html(quantumState.alpha.toFixed(PRECISION));
$("#bloch-sphere-state-beta").html(quantumState.beta.toFixed(PRECISION));
$("#bloch-sphere-state-alpha").html(blochSphereState.alpha.toFixed(PRECISION));
$("#bloch-sphere-state-beta").html(blochSphereState.beta.toFixed(PRECISION));

// Update X, Y & Z
$("#bloch-sphere-state-x").html(quantumState.x.toFixed(PRECISION));
$("#bloch-sphere-state-y").html(quantumState.y.toFixed(PRECISION));
$("#bloch-sphere-state-z").html(quantumState.z.toFixed(PRECISION));
$("#bloch-sphere-state-x").html(blochSphereState.x.toFixed(PRECISION));
$("#bloch-sphere-state-y").html(blochSphereState.y.toFixed(PRECISION));
$("#bloch-sphere-state-z").html(blochSphereState.z.toFixed(PRECISION));
}
};

Expand Down
21 changes: 11 additions & 10 deletions src/quantum/bloch_sphere.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import {
} from "../geometry/composite_shapes.js";

import {
QuantumState
} from "./quantum_state.js"
BlochSphereState
} from "./bloch_sphere_state.js";


class BlochSphere extends BaseGroup {
constructor(radius, properties) {
Expand Down Expand Up @@ -55,19 +56,19 @@ class BlochSphere extends BaseGroup {
// Add StatePointer to BaseGroup
this.add(this.statePointer);

// Create QuantumState
this.quantumState = new QuantumState(this.statePointer.theta(), this.statePointer.phi());
// Create BlochSphereState
this.blochSphereState = new BlochSphereState(this.statePointer.theta(), this.statePointer.phi());

// Set QuantumState to StatePointer
this.updateQuantumState(CartesianAxes.YAxis, THREE.Math.degToRad(properties.theta));
this.updateQuantumState(CartesianAxes.ZAxis, THREE.Math.degToRad(properties.phi));
// Set StatePointer
this.updateBlochSphereState(CartesianAxes.YAxis, THREE.Math.degToRad(properties.theta));
this.updateBlochSphereState(CartesianAxes.ZAxis, THREE.Math.degToRad(properties.phi));
}

updateQuantumState(axis, angle) {
updateBlochSphereState(axis, angle) {
this.statePointer.rotate(axis, new THREE.Vector3(), angle);

// Update QuantumState
this.quantumState.update(this.statePointer.theta(), this.statePointer.phi());
// Update BlochSphereState
this.blochSphereState.update(this.statePointer.theta(), this.statePointer.phi());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import * as THREE from '../libs/three/three.module.js';

import {
Float,
Complex
} from "../mathutils.js";


class QuantumState {
class BlochSphereState {
constructor(theta, phi) {
this.update(theta, phi);
}
Expand All @@ -29,5 +31,5 @@ class QuantumState {
}

export {
QuantumState
BlochSphereState
}
40 changes: 40 additions & 0 deletions src/quantum/bloch_sphere_state.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
Complex
} from "../mathutils.js";

import {
BlochSphereState
} from "./bloch_sphere_state.js";


test("bloch-sphere-state-basic", () => {
let testObj = new BlochSphereState(0, 90);

expect(
testObj.theta
).toBe(0);

expect(
testObj.phi
).toBe(90);

expect(
testObj.alpha
).toBe(1);

expect(
testObj.beta.toFixed(4)
).toBe(new Complex(0, 0).toFixed(4));

expect(
testObj.x
).toBe(0);

expect(
testObj.y
).toBe(0);

expect(
testObj.z
).toBe(1);
});

0 comments on commit 2997441

Please sign in to comment.