-
Notifications
You must be signed in to change notification settings - Fork 18
/
Test_classical_before_quantum.py
129 lines (95 loc) · 4 KB
/
Test_classical_before_quantum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
""" This file allows to test the calculations done before creating the quantum circuit
It asks the user for a number N and then:
Checks if N is 1 or 0 or is an even -> these cases are simple
Checks if N can be put in q^p form, q and p integers -> this can be done quicker classicaly then using the quantum circuit
If it is not an easy case like the above ones, then the program gets an integer a, coprime with N, starting with the
smallest one possible and asking the user if the selected a is ok, if not, going to the second smallest one, and like this until
the user agrees with an a
"""
""" Imports to Python functions """
import math
import array
import fractions
import numpy as np
import sys
""" Function to check if N is of type q^p"""
def check_if_power(N):
""" Check if N is a perfect power in O(n^3) time, n=ceil(logN) """
b=2
while (2**b) <= N:
a = 1
c = N
while (c-a) >= 2:
m = int( (a+c)/2 )
if (m**b) < (N+1):
p = int( (m**b) )
else:
p = int(N+1)
if int(p) == int(N):
print('N is {0}^{1}'.format(int(m),int(b)) )
return True
if p<N:
a = int(m)
else:
c = int(m)
b=b+1
return False
""" Function to get the value a ( 1<a<N ), such that a and N are coprime. Starts by getting the smallest a possible
This normally is be done fully randomly, we just did like this for user (professor) to have complete control
over the a value that gets selected """
def get_value_a(N):
""" ok defines if user wants to used the suggested a (if ok!='0') or not (if ok=='0') """
ok='0'
""" Starting with a=2 """
a=2
""" Get the smallest a such that a and N are coprime"""
while math.gcd(a,N)!=1:
a=a+1
""" Store it as the smallest a possible """
smallest_a = a
""" Ask user if the a found is ok, if not, then increment and find the next possibility """
ok = input('Is the number {0} ok for a? Press 0 if not, other number if yes: '.format(a))
if ok=='0':
if(N==3):
print('Number {0} is the only one you can use. Using {1} as value for a\n'.format(a,a))
return a
a=a+1
""" Cycle to find all possibilities for a not counting the smallest one, until user says one of them is ok """
while ok=='0':
""" Get a coprime with N """
while math.gcd(a,N)!=1:
a=a+1
""" Ask user if ok """
ok = input('Is the number {0} ok for a? Press 0 if not, other number if yes: '.format(a))
""" If user says it is ok, then exit cycle, a has been found """
if ok!='0':
break
""" If user says it is not ok, increment a and check if are all possibilites checked. """
a=a+1
""" If all possibilities for a are rejected, put a as the smallest possible value and exit cycle """
if a>(N-1):
print('You rejected all options for value a, selecting the smallest one\n')
a=smallest_a
break
""" Print the value that is used as a """
print('Using {0} as value for a\n'.format(a))
return a
""" Main program """
if __name__ == '__main__':
""" Ask for analysis number N """
N = int(input('Please insert integer number N: '))
print('input number was: {0}\n'.format(N))
""" Check if N==1 or N==0"""
if N==1 or N==0:
print('Please put an N different from 0 and from 1')
exit()
""" Check if N is even """
if (N%2)==0:
print('N is even, so does not make sense!')
exit()
""" Check if N can be put in N=p^q, p>1, q>=2 """
""" Try all numbers for p: from 2 to sqrt(N) """
if check_if_power(N)==True:
exit()
print('Not an easy case, using the quantum circuit is necessary\n')
a=get_value_a(N)