public virtual void SimulateInPlace(QuantumCircuitFloat circuit, ref ComplexNumberFloat[] amplitudes)
        {
            int length = circuit.AmplitudeLength;

            if (amplitudes == null || amplitudes.Length != length)
            {
                //Post message
                amplitudes = new ComplexNumberFloat[length];
            }

            float sum = circuit.ProbabilitySum();

            //if
            if (sum > MathHelper.Eps)
            {
                if (sum < 1 - MathHelper.Eps || sum > 1 + MathHelper.Eps)
                {
                    circuit.Normalize(sum);
                }

                for (int i = 0; i < amplitudes.Length; i++)
                {
                    amplitudes[i] = circuit.Amplitudes[i];
                }
            }
            else
            {
                //Initialize the all 0 vector
                amplitudes[0].Real = 1;
            }
        }
        public virtual ComplexNumberFloat[] Simulate(QuantumCircuitFloat circuit)
        {
            float sum = circuit.ProbabilitySum();

            if (sum > MathHelper.EpsFloat)
            {
                if (sum < 1 - MathHelper.EpsFloat || sum > 1 + MathHelper.EpsFloat)
                {
                    circuit.Normalize(sum);
                }

                ComplexNumberFloat[] amplitudes = new ComplexNumberFloat[circuit.AmplitudeLength];

                for (int i = 0; i < amplitudes.Length; i++)
                {
                    amplitudes[i] = circuit.Amplitudes[i];
                }
                return(amplitudes);
            }
            else
            {
                //Initialize the all 0 vector
                ComplexNumberFloat[] amplitudes = new ComplexNumberFloat[circuit.AmplitudeLength];
                amplitudes[0].Real = 1;
                return(amplitudes);
            }
        }
Exemplo n.º 3
0
        public static void TextureToColorCircuit(Texture2D inputTexture, out QuantumCircuitFloat redCircuit, out QuantumCircuitFloat greenCircuit, out QuantumCircuitFloat blueCircuit, bool useLog = false)
        {
            int width  = inputTexture.width;
            int height = inputTexture.height;


            int dimX = Mathf.CeilToInt(Mathf.Log(width) / Mathf.Log(2));

            int[] linesWidth = MakeLinesInt(dimX);

            int dimY = dimX;

            int[] linesHeight = linesWidth;

            if (width != height)
            {
                dimY        = Mathf.CeilToInt(Mathf.Log(height) / Mathf.Log(2));
                linesHeight = MakeLinesInt(dimY);
            }

            int maxHeight      = MathHelper.IntegerPower(2, dimY);
            int numberOfQubits = dimX + dimY;

            redCircuit   = new QuantumCircuitFloat(numberOfQubits, numberOfQubits, true);
            greenCircuit = new QuantumCircuitFloat(numberOfQubits, numberOfQubits, true);
            blueCircuit  = new QuantumCircuitFloat(numberOfQubits, numberOfQubits, true);


            Color color;
            int   index;
            int   posX;

            for (int x = 0; x < width; x++)
            {
                posX = linesWidth[x] * maxHeight;
                for (int y = 0; y < height; y++)
                {
                    index = posX + linesHeight[y];
                    color = inputTexture.GetPixel(x, y);

                    redCircuit.Amplitudes[index].Real   = Mathf.Sqrt(color.r);
                    greenCircuit.Amplitudes[index].Real = Mathf.Sqrt(color.g);
                    blueCircuit.Amplitudes[index].Real  = Mathf.Sqrt(color.b);
                }
            }

            redCircuit.Normalize();
            greenCircuit.Normalize();
            blueCircuit.Normalize();
        }