In[41]:=

Remove["Global`@*"]

In[42]:=

ket[0] = {1, 0} ; ket[1] = {0, 1} ;

In[44]:=

ket[x_String] := Apply[CircleTimes, Table[If[StringTake[x, {i, i}] == "0", ket[0], ket[1], "NotDef"], {i, StringLength[x]}]]

In[45]:=

Clear[MatrixDisplay] MatrixDisplay[tensor_CircleTimes] := Map[MatrixForm, tensor] MatrixDispla ... matrix_] := Map[MatrixForm, matrix]/;MatrixQ[matrix] MatrixDisplay[ket__] := MatrixDisplay[{ket}]

In[51]:=

bitQ[x_] := If[VectorQ[x] && Dimensions[x] == {2}, True, False, False]

In[52]:=

ket[x_Integer, bits_Integer] := Apply[CircleTimes, Map[ket, IntegerDigits[x, 2, bits]]]

In[53]:=

Clear[Tensor] Tensor[tensorList_CircleTimes] := Apply[Tensor, tensorList] Tensor[{ket_}] := ke ...  Table[fm[[i, j]] tensor2, {j, Length[fm[[i]]]}]], {i, Length[fm]}]]] /;VectorQ[ketList, MatrixQ]

In[59]:=

WalshHadamardMatrix = 1/2^(1/2) (1    1 ) ;                                   1    -1

In[60]:=

Clear[WalshHadamard] WalshHadamard[ket_] := WalshHadamardMatrix . ket /;bitQ[ket] WalshHadamar ... List] /; VectorQ[ketList, bitQ] WalshHadamard[ketList_CircleTimes] := Map[WalshHadamard, ketList]

In[65]:=

Clear[GroverSearchMain] GroverSearchMain[f_, BasicState_List, iteration_Integer] := Module[{Rf ... {n}]]] ; 
amplitude[k_Integer] := amplitude[k] = Uf . amplitude[k - 1] ; 
amplitude[iteration] 
]

In[67]:=

<<Graphics`Graphics`
 Clear[GroverShowGraphics] GroverShowGraphics[amplitude_List] := Mo ... ange -> All, AspectRatio -> 3/2, Axes -> {True, True}, AxesOrigin -> {0, 0}] ; 
]/;VectorQ[amplitude]

In[70]:=

GroverSearchSolve[amplitude_List] := Module[{r, i, s}, 
r = Random[] ; 
s = amplitude[[1]]^2 ; ... i - 1]]^2, "=", N[amplitude[[i - 1]]^2, 3], "で観測されました"] 
]/;VectorQ[amplitude]

In[71]:=

GroverSearchSolveRetNum[amplitude_List] := Module[{r, i, s}, 
r = Random[] ; 
s = amplitude[[1]]^2 ; 
For[i = 2, s< r, i ++, s = s + amplitude[[i]]^2] ; i - 2
]/;VectorQ[amplitude]

In[72]:=

Clear[GroverSearch] GroverSearch[f_, Number_Integer, t_Integer] := Module[{n, stateX, i}, 
n = ... ket[i, n], {i, 0, Num - 1}] ; 
i = Floor[N[π]/4 Num/t^(1/2)] ; 
GroverSearchMain[f, stateX, i] 
]

In[74]:=

f[_] = 0 ; f[ket[14, 5]] = 1 ;

In[76]:=

GroverShowGraphics[GroverSearch[f, 32, 1]] GroverSearchSolve[GroverSearch[f, 32, 1]] GroverSearchSolveRetNum[GroverSearch[f, 32, 1]]

[Graphics:HTMLFiles/index_17.gif]

14番目の状態が確率536431921/536870912 = 0.999で観測されました

Out[78]=

14

In[79]:=

ket[14, 5]//MatrixDisplay Tensor[ket[14, 5]] //MatrixDisplay

Out[79]=

(\[NoBreak] 1 \[NoBreak]) (x) (\[NoBreak] 0 \[NoBreak]) (x) (\[NoBreak] 0 \[NoBreak]) (x) (\[NoBreak] 0 \[NoBreak]) (x) (\[NoBreak] 1 \[NoBreak])     0         1         1         1         0

Out[80]//MatrixForm=

(\[NoBreak] 0 \[NoBreak])     0     0     0     0     0     0     0     0     0     0     0     0     0     1    ...  0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0


Created by Mathematica  (February 3, 2005)