Beispiel #1
0
    public void Run()
    {
        int registerLength = 1; //кол-во кубитов. квантовый регистр это совокупность некоторого числа кубитов

        //Подготовка начально состояния
        QuantumRegister inputRegister  = new QuantumRegister(0, registerLength);             // Входной регистр
        QuantumRegister outputRegister = new QuantumRegister(1, registerLength);             // Выходной регистр
        QuantumRegister fullRegister   = new QuantumRegister(inputRegister, outputRegister); // Полный регистр (получается тензорным произведением)

        //Console.WriteLine(inputRegister);
        //Console.WriteLine(outputRegister);
        //Console.WriteLine(fullRegister);

        fullRegister = QuantumGate.HadamardGateOfLength(2) * fullRegister; // преобразование адамара
        Matrix <Complex> matrixOracle = MatrixOracle(registerLength);      //Проектирование оракула
        QuantumGate      GateOracle   = new QuantumGate(matrixOracle);     //Получаем гейт (на основе matrixOracle)

        fullRegister = GateOracle * fullRegister;
        fullRegister = QuantumGate.HadamardGateOfLength(2) * fullRegister; // преобразование адамара
        //Console.WriteLine(fullRegister);

        fullRegister.Collapse(this.Random);//измерение (наблюление) первого регистра
        int inputRegesterValue = fullRegister.GetValue(0, registerLength);

        FunctionDefinition(inputRegesterValue);

        Console.ReadKey();
    }
 public void QuantumGate_HadamardGateOfLength2_IsValid()
 {
     Assert.IsTrue(QuantumGate.HadamardGateOfLength(2).AlmostEquals(new QuantumGate(new Complex[, ] {
         { 0.5, 0.5, 0.5, 0.5 },
         { 0.5, -0.5, 0.5, -0.5 },
         { 0.5, 0.5, -0.5, -0.5 },
         { 0.5, -0.5, -0.5, 0.5 },
     })));
 }
Beispiel #3
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));
        }
Beispiel #4
0
        public void Run()
        {
            int registerLength = 3;//кол-во кубитов.
            //registerLength = Int32.Parse(Console.ReadLine());

            QuantumRegister fullRegister = new QuantumRegister(0, registerLength);          // Полный регистр

            fullRegister = QuantumGate.HadamardGateOfLength(registerLength) * fullRegister; // преобразование адамара

            Matrix <Complex> matrixOracle = MatrixOracle(registerLength);                   //Проектирование оракула
            QuantumGate      GateOracle   = new QuantumGate(matrixOracle);                  //Получаем гейт (на основе matrixOracle)


            Matrix <Complex> matrixPhaseInverse = PhaseInverse(registerLength);
            QuantumGate      GatePhaseInverse   = new QuantumGate(matrixPhaseInverse);

            ///////////////
            for (int count = 2; count > 0; count--)
            {
                // Console.WriteLine(1);
                fullRegister = GateOracle * fullRegister;
                //Console.WriteLine(fullRegister);
                //fullRegister = QuantumGate.HadamardGateOfLength(registerLength) * fullRegister;
                //Console.WriteLine(fullRegister);
                fullRegister = GatePhaseInverse * fullRegister;
                //Console.WriteLine(GatePhaseInverse);
                //fullRegister = QuantumGate.HadamardGateOfLength(registerLength) * fullRegister;
            }
            //fullRegister = QuantumGate.HadamardGateOfLength(registerLength) * fullRegister;


            //Console.WriteLine(fullRegister);
            fullRegister.Collapse(this.Random);//измерение (наблюление)
            int    RegisterValue       = fullRegister.GetValue(0, registerLength);
            string BinaryRegisterValue = Convert.ToString(RegisterValue, 2);

            Console.WriteLine(BinaryRegisterValue);
            Console.ReadKey();
        }