Exemple #1
0
        public static string Print(this IQuantumState state, params Register[] registers)
        {
            var           test = state.ToArray().Where(b => b.Magnitude > ComplexExt.Precision);
            StringBuilder sb   = new StringBuilder();

            for (long i = 0; i < state.Dimension; i++)
            {
                var basis = new ComputationalBasis(i, state.NumQubits());
                var amp   = state.GetAmplitude(basis);
                if (amp.Magnitude < ComplexExt.Precision)
                {
                    continue;
                }
                sb.Append($"+{(amp.Imaginary == 0 ? amp.Real.ToString("F2") : amp.ToString("F2"))}");

                var labels = basis.GetLabels();
                foreach (var register in registers)
                {
                    var registerLabels = new List <bool>();
                    foreach (var index in register.QubitIndexes)
                    {
                        registerLabels.Add(labels[index]);
                    }
                    long regValue = ComputationalBasis.FromLabels(registerLabels.ToArray()).AmpIndex;
                    sb.Append($"|{regValue}>");
                }
            }
            return(sb.ToString());
        }
Exemple #2
0
        /// <summary>
        /// The probabilities of each possible register value indexed by that value.
        /// </summary>
        public static double[] GetDistribution(this IQuantumState state, Register reg)
        {
            int  regSize     = reg.QubitIndexes.Count();
            long maxRegValue = (long)Math.Pow(2, regSize);

            // probabilities indexed by register value
            double[] regProbabilities = new double[maxRegValue];
            // go through every probability
            for (long i = 0; i < state.Dimension; i++)
            {
                var basis = new ComputationalBasis(i, state.NumQubits());
                var amp   = state.GetAmplitude(basis);
                if (amp.Magnitude < ComplexExt.Precision)
                {
                    continue;
                }

                var labels         = basis.GetLabels();
                var registerLabels = new List <bool>();
                foreach (var index in reg.QubitIndexes)
                {
                    registerLabels.Add(labels[index]);
                }
                long regValue = ComputationalBasis.FromLabels(registerLabels.ToArray()).AmpIndex;
                regProbabilities[regValue] += amp.Magnitude * amp.Magnitude;
            }

            return(regProbabilities);
        }