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); } }
public void StartTest() { if (Circuit.Amplitudes != null && Circuit.Amplitudes.Length > 0) { Circuit.AmplitudeLength = Circuit.Amplitudes.Length; } Amplitudes = simulator.Simulate(Circuit); Probabilities = simulator.GetProbabilities(Amplitudes); QiskitString = Circuit.GetQiskitString(); FloatCircuit = new QuantumCircuitFloat(Circuit); MicroQiskitSimulatorFloat floatSimulator = new MicroQiskitSimulatorFloat(); FloatProbabilities = floatSimulator.GetProbabilities(FloatCircuit); ProbabilitieDifference = new double[Probabilities.Length]; for (int i = 0; i < Probabilities.Length; i++) { ProbabilitieDifference[i] = Probabilities[i] - FloatProbabilities[i]; } }
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(); }
public static Texture2D CalculateColorTexture(QuantumCircuitFloat redCircuit, QuantumCircuitFloat greenCircuit, QuantumCircuitFloat blueCircuit, int width, int height, bool renormalize = false) { Texture2D texture = new Texture2D(width, height, TextureFormat.RGBA32, false); int widthLog = Mathf.CeilToInt(Mathf.Log(width) / Mathf.Log(2)); int heightLog = widthLog; int[] widthLines = MakeLinesInt(widthLog); int[] heightLines = widthLines; if (height != width) { heightLog = Mathf.CeilToInt(Mathf.Log(height) / Mathf.Log(2)); heightLines = MakeLinesInt(heightLog); } MicroQiskitSimulatorFloat simulator = new MicroQiskitSimulatorFloat(); float[] redProbs = simulator.GetProbabilities(redCircuit); float[] greenProbs = simulator.GetProbabilities(greenCircuit); float[] blueProbs = simulator.GetProbabilities(blueCircuit); float normalizationRed = 0; float normalizationGreen = 0; float normalizationBlue = 0; if (!renormalize && redCircuit.OriginalSum > 0 && greenCircuit.OriginalSum > 0 && blueCircuit.OriginalSum > 0) { normalizationRed = redCircuit.OriginalSum; normalizationGreen = greenCircuit.OriginalSum; normalizationBlue = blueCircuit.OriginalSum; } else { for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int pos = widthLines[i] * height + heightLines[j]; if (redProbs[pos] > normalizationRed) { normalizationRed = redProbs[pos]; } if (greenProbs[pos] > normalizationGreen) { normalizationGreen = greenProbs[pos]; } if (blueProbs[pos] > normalizationBlue) { normalizationBlue = blueProbs[pos]; } } } normalizationRed = 1.0f / normalizationRed; normalizationGreen = 1.0f / normalizationGreen; normalizationBlue = 1.0f / normalizationBlue; } Unity.Collections.NativeArray <Color32> data = texture.GetRawTextureData <Color32>(); //TODO set blocks of color float redValue; float greenValue; float blueValue; //Color color = new Color(); int posX = 0; for (int x = 0; x < width; x++) { posX = widthLines[x] * height; for (int y = 0; y < height; y++) { int index = posX + heightLines[y]; redValue = (redProbs[index] * normalizationRed); greenValue = (greenProbs[index] * normalizationGreen); blueValue = (blueProbs[index] * normalizationBlue); //color.r = redValue; //color.g = greenValue; //color.b = blueValue; texture.SetPixel(x, y, new Color(redValue, greenValue, blueValue)); } } texture.Apply(); return(texture); }
public virtual float[] GetProbabilities(QuantumCircuitFloat circuit) { //Doing nothing just preparing an array float[] probabilities = new float[MathHelper.IntegerPower(2, circuit.NumberOfQubits)]; return(probabilities); }
/// <summary> /// Getting a colored texture for given quantum circuits (each one representing 1 color channel of an image) directly without using python. /// Fast version is a lot faster than python versions but does not support logarithmic encoding yet and may still contain some errors. /// </summary> /// <param name="redCircuit">The quantum circuit which represents the red channel of the image.</param> /// <param name="greenCircuit">The quantum circuit which represents the green channel of the image.</param> /// <param name="blueCircuit">The quantum circuit which represents the blue channel of the image.</param> /// <param name="width">The width of the image</param> /// <param name="height">The height of the image</param> /// <param name="renormalize">If the image (colors) should be renormalized. (Giving it the highest possible saturation / becomes most light) </param> /// <param name="useLog">If logarithmic encoding is chosen DOES NOTHING (at the moment)</param> /// <returns>A texture showing the encoded image.</returns> public static Texture2D GetColoreTextureDirectFast(QuantumCircuitFloat redCircuit, QuantumCircuitFloat greenCircuit, QuantumCircuitFloat blueCircuit, int width, int height, bool renormalize = false, bool useLog = false) { return(QuantumImageHelper.CalculateColorTexture(redCircuit, greenCircuit, blueCircuit, width, height, renormalize)); }
/// <summary> /// Getting a quantum circuit representation of each color channel of an image directly without using python. /// Is faster than python versions but does not support logarithmic encoding yet and may still contain some errors. /// </summary> /// <param name="inputTexture">The image which should be converted into quantum circuits representing the channels</param> /// <param name="redChannel">Returns the quantum circuit for the red channel of the image.</param> /// <param name="greenChannel">Returns the quantum circuit for the green channel of the image.</param> /// <param name="blueChannel">Returns the quantum circuit for the blue channel of the image.</param> /// <param name="useLog">If logarithmic encoding is chosen DOES NOTHING (at the moment)</param> public static void GetCircuitDirectPerChannel(Texture2D inputTexture, out QuantumCircuitFloat redChannel, out QuantumCircuitFloat greenChannel, out QuantumCircuitFloat blueChannel, bool useLog = false) { QuantumImageHelper.TextureToColorCircuit(inputTexture, out redChannel, out greenChannel, out blueChannel, useLog); }