Beispiel #1
0
        /// <summary>
        /// Returns the value contained in a quantum register, with optional portion start and length
        /// </summary>
        /// <param name="portionStart">The portion start.</param>
        /// <param name="portionLength">Length of the portion.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentException">The supplied portion overflows the given quantum register.</exception>
        /// <exception cref="SystemException">A value can only be extracted from a pure state quantum register.</exception>
        public int GetValue(int portionStart = 0, int portionLength = 0)
        {
            int registerLength = QCUtil.Log2((int)this.BitRegister.Size - 1);

            if (portionLength == 0)
            {
                portionLength = registerLength - portionStart;
            }

            int trailingBitCount = registerLength - portionStart - portionLength;

            if (trailingBitCount < 0)
            {
                throw new ArgumentException("The supplied portion overflows the given quantum register.");
            }

            int index = -1;

            for (int i = 0; i < this.BitRegister.Size; i++)
            {
                if (this.BitRegister[i].Real == 1)
                {
                    index = i;
                    break;
                }
            }

            if (index == -1)
            {
                throw new SystemException("A value can only be extracted from a pure state quantum register.");
            }

            // If trailing bits need to be removed
            if (trailingBitCount > 0)
            {
                index >>= trailingBitCount;
            }

            // If leading bits need to be removed
            if (portionStart > 0)
            {
                index &= (1 << portionLength) - 1;
            }

            return(index);
        }
Beispiel #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Qubit"/> class. Constructor from Bloch sphere coordinates
 /// </summary>
 /// <param name="colatitude">The colatitude.</param>
 /// <param name="longitude">The longitude.</param>
 public Qubit(float colatitude, float longitude)
     : this(Math.Cos(colatitude / 2), Math.Sin(colatitude / 2) * QCUtil.ComplexExp(Complex.ImaginaryOne * longitude))
 {
 }