Pages

Wednesday, 19 June 2013

White Paper - Apply Quantum Physics to Design Patterns


(Published by Fahim Chowdhury. 6th June 2013)



Abstract

This paper will go discuss the ways to architect projects using quantum physics theories.

The idea is to apply parts of quantum theories to the way we code to increase performance and efficiency.

I will combine my research in Schrödinger's cat & Quantum Superposition to devise a unique design pattern that will allow a single object to take shape of its observer and in the case of coding the observation is done through a view.

The benefits of such as design pattern is that is makes it possible to reuse a single object which can adapter to its views. This will help optimise performance and apply a better coding practices.




About me

My name is Fahim Chowdhury (MSc, BSc), I am a Computer Science Masters graduate who has been working in Advertising in the past 5 years building and architecting projects.

I began as a Flash Developer and now I work on HTML5/Javascript projects.

About Quantum Physics (Quantum Mechanics)

Quantum theory explains the laws of very small, discrete units of energy.
According to the Quantum theory it is impossible to know the exact location and velocity of an electron therefore it can exist in more than one location at the same time.

In this paper I will be focusing on a specific part of quantum physic, the quantum superposition and Schrödinger's cat thought experiment.

Schrödinger's cat & Quantum Superposition

This is was a thought experiment which talked about a cat being place in a box with a barrel of unstable gunpower. Outside the box the cat’s state was both ‘dead’ and ‘alive’ (superposition ) because until it the box was opened and the cat was observed the cat would stay in a superposition. After the cat has been observed the state of the cat was forced to either be ‘dead’ or ‘alive’.

Why Apply Quantum physics to Coding (Quantum Computing)

Quantum theory contains many features which can allow us to code more effectively. We can look at quantum computing as an example; computer memory is made of bits which are either set to on or off. Instead of having one state, quantum computers can have multiple states simultaneously which allows much faster computation.

I will try and apply the same principles to architecting web projects.

I’ve started to combine the state design pattern with the schrodinger’s cat experiment and the superposition to try and build the first quantum design pattern.

Let look at a traditional state pattern.

State Design Pattern

This design pattern is to encapsulate different behaviours for a given object. A change in the state will trigger/update a method or a variable.





As you can see the object is flat and cannot contain associated states for multiple contexts/observers.

The Quantum Pattern

The idea is that an object will exist in more than one view/context at the same time but depending on the observing view it will determine the objects states and values.

So how do we accomplish this?

Each view will have an identity and the quantum object will register each view it belongs to. When a view switches to another view, the current views identity is store and the quantum object gets notified of its current observer. The object will change its state and values to associate it to the current view. This will create a quantum entanglement.

When the quantum objects state or values change, the data will be stored in an array/object under the current views identity. This allows the object to exist in each view keeping the association. The object now has multiple quantums depending on the observer.




Javascript Code Example


Below is an object that I have created which allows you to register observers, update its data and change values and appear when the onQuantumChange function is called.

var QuantumButton = {
currentObserverId : null,
display : null,
observers : [],
registerObserver : function(id) {
if (!this.observers[id])
this.observers[id] = {
text : "",
state : "visible",
className : "",
x : 0,
y : 0
};
},
updateData : function(key, val) {

if (this.observers[this.currentObserverId] && this.observers[this.currentObserverId][key]!=undefined)
{
this.observers[this.currentObserverId][key] = val;
this.updateButton();
}
},
create : function() {
if (!this.display) {
this.display = document.createElement("div");
this.display.style.position = "absolute";
}
this.addChildToObserver();
},
addChildToObserver : function() {
if (document.getElementById(this.currentObserverId)) {
document.getElementById(this.currentObserverId).appendChild(this.display);
}
},
removeChildFromObserver : function(id) {
if(!this.currentObserverId)return;
if (document.getElementById(id)) {
document.getElementById(id).removeChild(this.display);
}
},
updateButton : function() {

this.display.innerHTML ="<p>" +this.observers[this.currentObserverId].text+"</p>";
this.display.style.left = this.observers[this.currentObserverId].x + "px";
this.display.style.top = this.observers[this.currentObserverId].y + "px";
this.display.className = this.observers[this.currentObserverId].className;
if (this.observers[this.currentObserverId].state == "hidden") {
this.display.style.display = "none";
} else {
this.display.style.display = "block";
}
},
onQuantumChange : function(id) {
this.removeChildFromObserver(this.currentObserverId);
this.currentObserverId = id;
this.updateButton();
this.addChildToObserver();
}
}




How to use the QuantumButton

First we need to register observers to the object. I’ve created three divs in the HTML and I will register them. Then I set the current observer, create the object and set the values for the object in the current observer.



QuantumButton.currentObserverId="view1";
QuantumButton.registerObserver("view1");
QuantumButton.registerObserver("view2");
QuantumButton.registerObserver("view3");
QuantumButton.create();
QuantumButton.updateData("className","button");
QuantumButton.updateData("text","button in view 1");
QuantumButton.onQuantumChange("view1");




I create a button in the HTML to show and hide each of the divs. And in that click function I will be notifying the object about the change in observer.



QuantumButton.onQuantumChange("view"+currentView);

I can now set new values to the object and it will be store for this observer only and once the observer changes again it will adapt the values to the associating observer.



QuantumButton.updateData("className","button2");
QuantumButton.updateData("x",100);
QuantumButton.updateData("y",100);
QuantumButton.updateData("text","button in view 2");




Benefits of this Method

Having an object which can be placed in more than one place makes it reusable and for it to be unique to a particular environment allows it to be adaptive.

When building large scale projects which require objects that are similar such as buttons across views, this will save overhead. You only have one button which contains only the relevant information it needs to be in a different view. Instead of having multiple buttons you now only have one which will improve performance.

Conclusion

I have the concept and the idea to improve the way we code but this is only my first attempt and its far from perfect.
Now its time to see the design pattern working in real life projects and analyse the impact it has.



Reference List


Wikipedia: Quantum mechanics. http://en.wikipedia.org/wiki/Quantum_mechanics. Updated 14 June 2013 at 22:00.

Oracle ThinkQuest: What is Quantum Physics?. http://library.thinkquest.org/3487/qp.html.
Wikipedia: State pattern. http://en.wikipedia.org/wiki/State_pattern. Updated 12 March 2013 at 08:19.

No comments:

Post a Comment