Beispiel #1
0
        //квантовая часть
        public int Period(int n, int a)
        {
            //1.	Выбрать число m такое, чтобы m=2l
            int registerLength = RegisterLength(n * n - 1);

            //2.	Подготовить два квантовых регистра, которые находятся в начальном состоянии |0⟩|0⟩.
            QuantumRegister inputRegister  = new QuantumRegister(0, registerLength); // первый регистр
            QuantumRegister outputRegister = new QuantumRegister(0, registerLength); // второй регистр

            //3.	Применить преобразование Адамара к первому регистру.
            inputRegister = QuantumGate.HadamardGateOfLength(registerLength) * inputRegister;
            //4.	Применить унитарный оператор (оракул)
            Matrix <Complex> modularExponentiationMatrix = ModularExponentiationMatrix(n, a, registerLength);

            QuantumGate     modularExponentiationGate = new QuantumGate(modularExponentiationMatrix);
            QuantumRegister fullRegister = new QuantumRegister(inputRegister, outputRegister);

            fullRegister = modularExponentiationGate * fullRegister;

            //5.	Применить обратное преобразование Фурье к первому регистру

            QuantumGate quantumFourierMatrixPlusIdentity = new QuantumGate(QuantumGate.QuantumFourierTransform(registerLength), QuantumGate.IdentityGateOfLength(registerLength));

            fullRegister = quantumFourierMatrixPlusIdentity * fullRegister;

            //6.	Измеряем первый регистр.
            fullRegister.Collapse(this.Random);
            int inputRegisterValue = fullRegister.GetValue(0, registerLength);

            //7.	Применить алгоритм цепных дробей
            float ratio       = (float)inputRegisterValue / (1 << registerLength);
            int   denominator = Denominator(ratio, n);

            //8. 9.	Убедится, что  . Если да, значит rʹ=r, алгоритм завершается. В противном случае выполнить предыдущий шаг с кратным rʹ.
            for (int period = denominator; period < n; period += denominator)
            {
                //if (ShorsAlgorithm.IntPow(a, period) % n == 1)
                if (ModIntPow(a, period, n) == 1)
                {
                    return(period);  ////////
                }
            }

            // 10.	В противном случае вернуться ко второму шагу.
            return(this.Period(n, a));
        }
        public void QuantumGate_QuantumFourierTranform_IsValid()
        {
            // Start with unnormalized samples
            Complex[] samples = new Complex[] { Complex.One, Complex.One + Complex.ImaginaryOne, Complex.ImaginaryOne, Complex.One - Complex.ImaginaryOne, Complex.One, Complex.Zero, Complex.ImaginaryOne, Complex.ImaginaryOne };

            // Create a quantum register from samples
            QuantumRegister quantumRegister = new QuantumRegister(samples);

            // Get normalized samples
            samples = quantumRegister.Vector.ToArray();

            // Transform quantum register
            quantumRegister = QuantumGate.QuantumFourierTransform(3) * quantumRegister;

            // Transform samples independently using Math.NET
            Fourier.Inverse(samples);

            // Compare results
            Assert.IsTrue(quantumRegister.AlmostEquals(new QuantumRegister(samples)));
        }