I'm trying an interesting experiment using Matlab. The goal here is to model a pair of quantum entangled particles with a simple binary model (The "unknown" or "superposition" state being ignored for the moment). This is not looking for physics advice, I know this is a gross oversimplification of a qubit. I want to model one specific aspect/process of the particle.
The process being modeled is this: whenever you try to "read" the state of the particle, you also change the state of the particle. In this case, I'm only modeling two pure states, 0 and 1. This leads to an interesting programming problem where I can't see a way to model this without breaking conventional programming wisdom, or at all. Here is the source for my first try:
classdef qparticle
%qparticle class
% models spin state of one of two particles in a quantum entangled pair
properties
observed_spin_state;
end
properties (Hidden = true)
spinstate = [];
end
methods
function obj = qparticle(initspin)
if islogical(initspin)
obj.spinstate = initspin;
else
obj.spinstate = NaN;
end
end
function value = get.observed_spin_state(obj)
%Getting spin state changes spinstate
obj.spinstate = ~obj.spinstate;
value = obj.spinstate;
end
%% set.spinstate should be private, only callable by getspinstate
function obj = set.spinstate(obj, value)
if islogical(value)
obj.spinstate = value;
else
obj.spinstate = NaN;
end
end
end
end
I have an overarching class qpair
which will ensure that the two qparticle
s within it are always in opposite states, but that code is not necessary for this.
I think I understand what is going on here, that when I call the getter, the object is being passed "by value" - a copy of it is going to the getter, so when I make the statement obj.spinstate = ~obj.spinstate
, the local copy of obj is modified, but it is not passed back to the obj that called the function, thus the flipping of the bit does not go back to the parent object.
If I use a setter, a handle class, or other function which returns the object itself, then I have no way to return the actual value to the calling function (I think).
How can I have a single method in the object essentially perform both the set/get? (It's OK if I am not able to use the assignment operator '=', and it's OK if it has to call other methods).