Skip to content

Commit

Permalink
Add float parser (bits-and-electrons#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
saich08 authored Feb 21, 2021
1 parent b27c154 commit cc71486
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 23 deletions.
6 changes: 6 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

const PRECISION = 4;

export {
PRECISION
}
8 changes: 6 additions & 2 deletions src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import {
ExportWorkspaceEventsListeners
} from "./event_listeners.js";

import {
PRECISION
} from "./constants.js"


var GlobalContext = {
canvas: null,
Expand Down Expand Up @@ -214,8 +218,8 @@ var GlobalContext = {
let quantumState = GlobalContext.blochSphere.quantumState;

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

// Save Workspace
ExportWorkspaceEvents.saveWorkspace();
Expand Down
22 changes: 11 additions & 11 deletions src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import {
QuantumGate
} from "./quantum/quantum_gate.js"

import {
ToolboxEventListeners
} from "./event_listeners.js";

import {
GlobalContext
} from "./context.js";

import {
PRECISION
} from "./constants.js"


var ToolboxEvents = {
enableQuantumGates: function () {
Expand Down Expand Up @@ -53,17 +53,17 @@ var BlochSphereStateEvents = {
let quantumState = GlobalContext.blochSphere.quantumState;

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

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

// Update X, Y & Z
$("#bloch-sphere-state-x").html(quantumState.x.toFixed(4));
$("#bloch-sphere-state-y").html(quantumState.y.toFixed(4));
$("#bloch-sphere-state-z").html(quantumState.z.toFixed(4));
$("#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));
}
};

Expand Down
13 changes: 12 additions & 1 deletion src/mathutils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import * as THREE from './libs/three/three.module.js';

import {
PRECISION
} from "./constants.js"


class Float {
static parse(num) {
return parseFloat(num.toFixed(PRECISION));
}
}

class Complex {
constructor(real, img) {
Expand All @@ -19,7 +29,7 @@ class Vector3Helpers {
let crossProduct = new THREE.Vector3();

crossProduct.crossVectors(vector1, vector2);
if (parseFloat(crossProduct.dot(planeNormal).toFixed(4)) < 0) {
if (Float.parse(crossProduct.dot(planeNormal)) < 0) {
angle *= -1;
}

Expand All @@ -28,6 +38,7 @@ class Vector3Helpers {
}

export {
Float,
Complex,
Vector3Helpers
};
20 changes: 11 additions & 9 deletions src/quantum/quantum_state.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import {
Float,
Complex
} from "../mathutils.js"
} from "../mathutils.js";


class QuantumState {
constructor(theta, phi) {
this.update(theta, phi);
}

load() {
this.alpha = parseFloat(Math.cos(THREE.Math.degToRad(this.theta) / 2).toFixed(4));
this.alpha = Float.parse(Math.cos(THREE.Math.degToRad(this.theta) / 2));
this.beta = new Complex(
parseFloat((Math.cos(THREE.Math.degToRad(this.phi)) * Math.sin(THREE.Math.degToRad(this.theta) / 2)).toFixed(4)),
parseFloat((Math.sin(THREE.Math.degToRad(this.phi)) * Math.sin(THREE.Math.degToRad(this.theta) / 2)).toFixed(4))
Float.parse(Math.cos(THREE.Math.degToRad(this.phi)) * Math.sin(THREE.Math.degToRad(this.theta) / 2)),
Float.parse(Math.sin(THREE.Math.degToRad(this.phi)) * Math.sin(THREE.Math.degToRad(this.theta) / 2))
);

this.x = parseFloat((Math.sin(THREE.Math.degToRad(this.theta)) * Math.cos(THREE.Math.degToRad(this.phi))).toFixed(4));
this.y = parseFloat((Math.sin(THREE.Math.degToRad(this.theta)) * Math.sin(THREE.Math.degToRad(this.phi))).toFixed(4));
this.z = parseFloat(Math.cos(THREE.Math.degToRad(this.theta)).toFixed(4));
this.x = Float.parse(Math.sin(THREE.Math.degToRad(this.theta)) * Math.cos(THREE.Math.degToRad(this.phi)));
this.y = Float.parse(Math.sin(THREE.Math.degToRad(this.theta)) * Math.sin(THREE.Math.degToRad(this.phi)));
this.z = Float.parse(Math.cos(THREE.Math.degToRad(this.theta)));
}

update(theta, phi) {
this.theta = parseFloat(theta.toFixed(4));
this.phi = parseFloat(phi.toFixed(4));
this.theta = Float.parse(theta);
this.phi = Float.parse(phi);
this.load();
}
}
Expand Down

0 comments on commit cc71486

Please sign in to comment.