Advertisement
Guest User

Untitled

a guest
May 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.42 KB | None | 0 0
  1. using Quantum;
  2. using Quantum.Operations;
  3. using System;
  4. using System.Numerics;
  5. using System.Collections.Generic;
  6.  
  7. namespace QuantumConsole
  8. {
  9.     public class QuantumTest
  10.     {  
  11.         public static Tuple<int, int> FractionalApproximation(int a, int b, int width)
  12.         {
  13.             double f = (double)a / (double)b;
  14.             double g = f;
  15.             int i, num2 = 0, den2 = 1, num1 = 1, den1 = 0, num = 0, den = 0;
  16.             int max = 1 << width;
  17.  
  18.             do
  19.             {
  20.                 i = (int)g;  // integer part
  21.                 g = 1.0 / (g - i);  // reciprocal of the fractional part
  22.  
  23.                 if (i * den1 + den2 > max) // if denominator is too big
  24.                 {
  25.                     break;
  26.                 }
  27.  
  28.                 // new numerator and denominator
  29.                 num = i * num1 + num2;
  30.                 den = i * den1 + den2;
  31.  
  32.                 // previous nominators and denominators are memorized
  33.                 num2 = num1;
  34.                 den2 = den1;
  35.                 num1 = num;
  36.                 den1 = den;
  37.  
  38.             }
  39.             while (Math.Abs(((double)num / (double)den) - f) > 1.0 / (2 * max));
  40.             // this condition is from Shor algorithm
  41.  
  42.             return new Tuple<int, int>(num, den);
  43.         }
  44.        
  45.         public static int FindPeriod(int N, int a) {
  46.             ulong ulongN = (ulong)N;
  47.             int width = (int)Math.Ceiling(Math.Log(N, 2));
  48.  
  49.             Console.WriteLine("Width for N: {0}", width);
  50.             Console.WriteLine("Total register width (7 * w + 2) : {0}", 7 * width + 2);
  51.            
  52.             QuantumComputer comp = QuantumComputer.GetInstance();
  53.            
  54.             //input register
  55.             Register regX = comp.NewRegister(0, 2 * width);
  56.            
  57.             // output register (must contain 1):
  58.             Register regX1 = comp.NewRegister(1, width + 1);
  59.            
  60.             // perform Walsh-Hadamard transform on the input register
  61.             // input register can contains N^2 so it is 2*width long
  62.             Console.WriteLine("Applying Walsh-Hadamard transform on the input register...");
  63.             comp.Walsh(regX);
  64.            
  65.             // perform exp_mod_N
  66.             Console.WriteLine("Applying f(x) = a^x mod N ...");
  67.             comp.ExpModulo(regX, regX1, a, N);
  68.            
  69.             // output register is no longer needed
  70.             regX1.Measure();
  71.            
  72.             // perform Quantum Fourier Transform on the input register
  73.             Console.WriteLine("Applying QFT on the input register...");
  74.             comp.QFT(regX);
  75.            
  76.             comp.Reverse(regX);
  77.            
  78.             // getting the input register
  79.             int Q = (int)(1 << 2 * width);
  80.             int inputMeasured = (int)regX.Measure();
  81.             Console.WriteLine("Input measured = {0}", inputMeasured);
  82.             Console.WriteLine("Q = {0}", Q);
  83.            
  84.             Tuple<int, int> result = FractionalApproximation(inputMeasured, Q, 2 * width - 1);
  85.  
  86.             Console.WriteLine("Fractional approximation:  {0} / {1}", result.Item1, result.Item2);
  87.            
  88.             int period = result.Item2;
  89.            
  90.             if(BigInteger.ModPow(a, period, N) == 1) {
  91.                 Console.WriteLine("Success !!!    period = {0}", period);
  92.                 return period;
  93.             }
  94.            
  95.             int maxMult = (int)(Math.Sqrt(N)) + 1;
  96.             int mult = 2;
  97.             while(mult < maxMult)
  98.             {
  99.                 Console.WriteLine("Trying multiply by {0} ...", mult);
  100.                 period = result.Item2 * mult;
  101.                 if(BigInteger.ModPow(a, period, N) == 1)
  102.                 {
  103.                     Console.WriteLine("Success !!!    period = {0}", period);
  104.                     return period;
  105.                 }
  106.                 else
  107.                 {      
  108.                     mult++;
  109.                 }
  110.             }
  111.            
  112.             Console.WriteLine("Failure !!!    Period not found, try again.");
  113.             return -1;
  114.         }
  115.        
  116.         public static int modInverse(int a, int n)
  117.         {
  118.             int i = n, v = 0, d = 1;
  119.             while (a>0) {
  120.                 int t = i/a, x = a;
  121.                 a = i % x;
  122.                 i = x;
  123.                 x = d;
  124.                 d = v - t*x;
  125.                 v = x;
  126.             }
  127.             v %= n;
  128.             if (v<0) v = (v+n)%n;
  129.             return v;
  130.         }
  131.  
  132.        
  133.        
  134.         public static void Main()
  135.         {
  136.             int N = 55;
  137.             int a = 4;
  138.             int c = 17;
  139.        
  140.             Console.WriteLine("a = {0}", a);
  141.             Console.WriteLine("N = {0}", N);
  142.            
  143.             int period = FindPeriod(N, a);
  144.            
  145.             Console.WriteLine();
  146.             Console.WriteLine("period = {0}", period); 
  147.  
  148.             int x;
  149.             x = (int) BigInteger.ModPow(a, period/2, N);
  150.             int p = (int) BigInteger.GreatestCommonDivisor(x-1, N);
  151.             int q = (int) BigInteger.GreatestCommonDivisor(x+1, N);
  152.            
  153.             Console.WriteLine("p = {0}, q = {1}, p*q = {2}", p, q, p*q);
  154.            
  155.             int d = modInverse(c, (p-1) * (q-1));
  156.             Console.WriteLine("d = {0}", d);
  157.            
  158.             Console.WriteLine("Zaszyfrowana wiadomosc to {0}", BigInteger.ModPow(a, d, N));
  159.            
  160.         }
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement