0

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 qparticles 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).

Floern
  • 33,559
  • 24
  • 104
  • 119
Trashman
  • 1,424
  • 18
  • 27
  • I struggle to understand your code, your description only contains one state variable while your code has two. Further, please describe the problem with your code. You are calling a (sequence of) methods and at some point you don't get the expected value. Could you give an example, including what you get and what you expect instead. – Daniel Nov 09 '15 at 18:04
  • The second state variable was to keep the "true" state variable unknown to the calling function (I did forget Access = private on the true function). When the calling function gets the Observed state, then the true state is modified before the observed state is returned. In my original code, the observed state would keep returning the same value without altering the state (e.g. 1,1,1,1,1). What I was expecting is that the observed state would change with each call (e.g. 0,1,0,1,0,1,0,1). – Trashman Nov 09 '15 at 18:07

1 Answers1

0

I've figured it out. With a handle class, I can both pass a value and reference the original object.

classdef qparticle < handle
    %qparticle class
    %   models spin state of one of two particles in a quantum entangled pair

    properties
       observed_spin_state;
    end

    properties (Hidden = true, Access = private)
        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 [] = set.spinstate(obj, value)
            if islogical(value)
                obj.spinstate = value;
            else
                obj.spinstate = NaN;
            end
        end

    end

end
Trashman
  • 1,424
  • 18
  • 27