forked from pierochiappina/Quantum-Computing-Projects
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathamodC.m
More file actions
36 lines (30 loc) · 823 Bytes
/
amodC.m
File metadata and controls
36 lines (30 loc) · 823 Bytes
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
function A = amodC(a, C, L, N)
%Creates controlled matrices for the function f(x)=mod(a^x, C) acting on
%L-qubits in an N-qubit register
for i = 1:L
row = [];
col = 1:2^N;
s = ones(1, 2^N);
if i>4
Ai = mod(vpa([num2str(a) '^(2^' num2str(i-1) ')'], 2^i ), C);
Ai = double(Ai);
%To ensure precision in MATLAB, we must use vpa
else
Ai = mod(a^(2^(i-1)), C);
end
for k = 1:2^N
n = dec2bin(k-1, N);
l = n(1:L);
m = n(L+1:end);
if l(L-i+1)=='0' || bin2dec(m)>=C
row = [row k];
else
f = bin2dec(m);
f = mod(Ai*f, C);
j = [l dec2bin(f, N-L)];
row = [row bin2dec(j)+1];
end
end
A.(['A' num2str(i-1)]) = sparse(row, col, s);
end
end