@@ -6,58 +6,63 @@ def hadamardN( n : int):
66 base [1 ][1 ] *= - 1
77 hadamardMatrix = np .full ((2 ,2 ), 1 / math .sqrt (2 ))
88 hadamardMatrix [1 ][1 ] *= - 1
9- for i in range (n - 1 ):
10- hadamardMatrix = np .kron (hadamardMatrix , base )
11-
9+ for _ in range (n - 1 ):
10+ hadamardMatrix = np .kron (base ,hadamardMatrix )
11+ hadamardMatrix = np .kron (hadamardMatrix ,np .eye (2 ),)
12+ # print(hadamardMatrix)
1213 return hadamardMatrix
1314
14- def projectOverMedian (states , N ):
15+ def projectOverMedian (N ):
1516 identidad = np .eye (N )
1617 matriz = np .full ((N , N ), 0.0 )
1718 matriz [0 ][0 ] = 2.0
1819 matriz -= identidad
19- states @= matriz
20- # states = np.kron(states, np.eye(2))
21- return states
20+ matriz = np .kron (matriz , np .eye (2 ))
21+ return matriz
2222
2323
24- def phaseInversion (states , oracleMatrix ):
25- return states @ oracleMatrix
24+ def phaseInversion (N , f : callable ):
25+ oracleMatrix = np .eye (N )
26+ for i in range (0 ,N ):
27+ oracleMatrix [i ][i ] = ((- 1 ) ** f (i ))
28+ oracleMatrix = np .kron (np .eye (2 ), oracleMatrix )
29+ for i in range (N , N * 2 ):
30+ oracleMatrix [i ][i ] = 1
31+ return oracleMatrix
2632
2733def normalityCheck (states ):
2834 sum = 0
2935 for x in states [0 ]:
3036 sum += x * x
31- assert (sum <= 1.001 )
37+ print (sum )
38+ assert (sum <= 1.1 and sum >= 0.9 )
3239
3340
3441def grover (n : int , f : callable ):
35- N = 2 ** n
36- iterations = 1
37- # iterations = math.pi / 4.0 * math.sqrt(N)
38- states = np .full ((1 ,N ), 0 )
42+ N = 2 ** ( n + 1 )
43+ # iterations = 1
44+ iterations = math .pi / 4.0 * math .sqrt (N )
45+ states = np .full ((1 ,2 ** n ), 0 )
3946 states [0 ][0 ] = 1
40-
47+ one = np .full ((1 ,2 ), 0 )
48+ one [0 ][1 ] = 1
49+ states = np .kron (one , states )# Inicializar en | - >
50+ # print(states)
4151 hadamardMatrix = hadamardN (n )
52+ phaseInversionMatrix = phaseInversion (2 ** n , f )
53+ meanInversionMatrix = projectOverMedian (2 ** n )
54+ # print(phaseInversionMatrix)
4255 states = states @ hadamardMatrix
43- normalityCheck (states )
44- print (states )
45- oracleMatrix = np .eye (N )
46- for i in range (0 ,N ):
47- oracleMatrix [i ][i ] = ((- 1 ) ** f (i ))
48-
49- # # print(oracleMatrix)
50- # print(oracleMatrix)
5156 # print(states)
57+
5258 for _ in range (math .ceil (iterations )):
53- states = phaseInversion ( states , oracleMatrix ) # correr función (Negar los que dan 1 en la función)
59+ states = states @ phaseInversionMatrix
5460
55- # Inversión de la media
56- states = states @ hadamardMatrix
57- states = projectOverMedian (states , N )
58- states = states @ hadamardMatrix
61+ states = states @hadamardMatrix
62+ states = states @meanInversionMatrix
63+ states = states @hadamardMatrix
5964
60- return states
65+ return states [:,: N // 2 ]
6166
6267def f (x : int ):
6368 return 1 if x == 0 else 0
0 commit comments