Exemple #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Qubit"/> class. Constructor from probability amplitudes
 /// </summary>
 /// <param name="zeroAmplitude">The zero amplitude.</param>
 /// <param name="oneAmplitude">The one amplitude.</param>
 public Qubit(Complex zeroAmplitude, Complex oneAmplitude)
 {
     BitRegister    = new ComplexVector(2);
     BitRegister[0] = zeroAmplitude;
     BitRegister[1] = oneAmplitude;
     Normalize();
 }
Exemple #2
0
        /// <summary>
        /// Collapses a quantum register into a pure state
        /// </summary>
        internal void Collapse()
        {
            Random        rand                 = new Random();
            ComplexVector collapsed            = new ComplexVector(BitRegister.Size);
            double        probabilityThreshold = QCUtil.Random.NextDouble();

            if (QState == QubitState.Superposition)
            {
                if (probabilityThreshold < 0.5)
                {
                    collapsed[0] = new Complex(1, 0);
                    collapsed[1] = new Complex(0, 0);
                }
                else
                {
                    collapsed[0] = new Complex(0, 0);
                    collapsed[1] = new Complex(1, 0);
                }

                BitRegister = collapsed;
            }

            if (Entangled != null)
            {
                if (QState == QubitState.One)
                {
                    new ControlledNot().Apply(this, Entangled);
                }
            }
        }
        /// <summary>
        /// Fills the complex number vector with specified value.
        /// </summary>
        /// <param name="value">The value.</param>
        public void Init(Complex value)
        {
            ComplexVector result = new ComplexVector(Size);

            Parallel.For(0, Size, (i) => {
                result[i] = value;
            });
        }
Exemple #4
0
        /// <summary>
        /// Created Generalized W Statue of specified length
        /// </summary>
        /// <param name="length">The length.</param>
        /// <returns></returns>
        public static QuantumRegister GHZStateOfLength(int length)
        {
            ComplexVector vector = ComplexVector.Zero(1 << length);

            vector[0] = Complex.One;
            vector[(1 << length) - 1] = Complex.One;

            return(new QuantumRegister(GetQubits(vector / (float)Math.Sqrt(2))));
        }
        /// <summary>
        ///  A vector of complex number that represents zero.
        /// </summary>
        public static ComplexVector Zero(int size)
        {
            ComplexVector result = new ComplexVector(size);

            Parallel.For(0, size, (i) => {
                result[i] = Complex.Zero;
            });

            return(result);
        }
        /// <summary>
        /// Computes the pow value for the vector of complex number.
        /// </summary>
        /// <param name="vector">The vector.</param>
        /// <returns></returns>
        public static ComplexVector Pow(ComplexVector vector, ComplexVector powerVector)
        {
            ComplexVector result = new ComplexVector(vector.Size);

            Parallel.For(0, vector.Size, (i) => {
                result[i] = Complex.Pow(vector[i], powerVector[i]);
            });

            return(result);
        }
        /// <summary>
        /// Computes the exponential value for the vector of complex number.
        /// </summary>
        /// <param name="vector">The vector.</param>
        /// <returns></returns>
        public static ComplexVector Exp(ComplexVector vector)
        {
            ComplexVector result = new ComplexVector(vector.Size);

            Parallel.For(0, vector.Size, (i) => {
                result[i] = Complex.Exp(vector[i]);
            });

            return(result);
        }
        /// <summary>
        /// Computes the square value for the vector of complex number.
        /// </summary>
        /// <param name="vector">The vector.</param>
        /// <returns></returns>
        public static ComplexVector Square(ComplexVector vector)
        {
            ComplexVector result = new ComplexVector(vector.Size);

            Parallel.For(0, vector.Size, (i) => {
                result[i] = Complex.Pow(vector[i], 2);
            });

            return(result);
        }
        /// <summary>
        /// Implements the operator /.
        /// </summary>
        /// <param name="lhs">The LHS.</param>
        /// <param name="rhs">The RHS.</param>
        /// <returns>
        /// The result of the operator.
        /// </returns>
        public static ComplexVector operator /(float lhs, ComplexVector rhs)
        {
            ComplexVector result = new ComplexVector(rhs.Size);

            Parallel.For(0, rhs.Size, (i) => {
                result[i] = lhs / rhs[i];
            });

            return(result);
        }
        /// <summary>
        /// Implements the operator /.
        /// </summary>
        /// <param name="lhs">The LHS.</param>
        /// <param name="rhs">The RHS.</param>
        /// <returns>
        /// The result of the operator.
        /// </returns>
        public static ComplexVector operator /(ComplexVector lhs, float rhs)
        {
            ComplexVector result = new ComplexVector(lhs.Size);

            Parallel.For(0, lhs.Size, (i) => {
                result[i] = lhs[i] / rhs;
            });

            return(result);
        }
        /// <summary>
        ///  A vector of complex number that represents imaginary one.
        /// </summary>
        public static ComplexVector ImaginaryOne(int size)
        {
            ComplexVector result = new ComplexVector(size);

            Parallel.For(0, size, (i) => {
                result[i] = Complex.ImaginaryOne;
            });

            return(result);
        }
Exemple #12
0
        /// <summary>
        /// Creates a WState of specified length
        /// </summary>
        /// <param name="length">The length.</param>
        /// <returns></returns>
        public static QuantumRegister WStateOfLength(int length)
        {
            ComplexVector vector = ComplexVector.Zero(1 << length);

            for (int i = 0; i < length; i++)
            {
                vector[1 << i] = Complex.One;
            }

            return(new QuantumRegister(GetQubits(vector / (float)Math.Sqrt(3))));
        }
Exemple #13
0
        /// <summary>
        /// Gets the qubits from the register.
        /// </summary>
        /// <returns>
        /// The qubit.
        /// </returns>
        public static Qubit[] GetQubits(ComplexVector vector)
        {
            int num = vector.Size / 2;

            Qubit[] qubits = new Qubit[num];

            for (int i = 0; i < num; i++)
            {
                qubits[i] = new Qubit(vector[i * 2], vector[i * 2 + 1]);
            }

            return(qubits);
        }
Exemple #14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="QuantumRegister"/> class.
 /// </summary>
 /// <param name="vector">The vector.</param>
 public QuantumRegister(ComplexVector vector)
 {
     Qubits = GetQubits(vector);
 }