Esempio n. 1
0
        /// <summary>
        /// Overrides native ToString() method, for friendly printing Register's content.
        /// </summary>
        /// <returns>Formatted string representing register. Prints its possible states and their probabilities.</returns>
        public override string ToString()
        {
            if (Root == null)
            {
                return null;
            }
            StringBuilder buf = new StringBuilder();
            IFormatProvider formatter = new ComplexFormatter();

            IReadOnlyDictionary<ulong, double> probabilities = this.GetProbabilities();
            IReadOnlyDictionary<ulong, Complex> tmpAmpl = this.GetAmplitudes();
            Complex amplitude;
            double prob;
            ulong max = (ulong)(1 << Width);
            for (ulong i = 0; i < max; i++)
            {
                if (probabilities.TryGetValue(i, out prob))
                {
                    if (tmpAmpl != null && tmpAmpl.TryGetValue(i, out amplitude))
                    {
                        buf.Append(String.Format(formatter, "{0:I5} ", amplitude));
                    }
                    else
                    {
                        buf.Append("                    ");
                    }
                    string tmp = "|" + i + ">";
                    buf.Append(String.Format("{0,-12}", tmp));
                    buf.Append(String.Format("{0,-14}", (float)prob)).Append("|");

                    for (int j = this.Width - 1; j >= 0; j--)
                    {
                        if (j % 4 == 3)
                        {
                            buf.Append(" ");
                        }
                        buf.Append((((ulong)1 << j) & i) >> j);
                    }
                    buf.AppendLine(">");
                }
            }
            return buf.ToString();
        }
Esempio n. 2
0
 public static string Print(this Complex[] vector)
 {
     IFormatProvider formatter = new ComplexFormatter();
     StringBuilder sb = new StringBuilder();
     for (int i = 0; i < vector.Length; i++)
     {
         sb.AppendLine(String.Format(formatter, "[ {0:I5} ]", vector[i]));
     }
     return sb.ToString();
 }