- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_reference.py
More file actions
Latest commit
138 lines (105 loc) · 4.38 KB
/
Copy pathpython_reference.py
File metadata and controls
138 lines (105 loc) · 4.38 KB
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
130
131
132
133
134
135
136
137
importnumpyasnp
importmatplotlib.pyplotasplt
importitertools
importrandom
gates= {
'h' : np.array([[1/np.sqrt(2),1/np.sqrt(2)] , [1/np.sqrt(2),-1/np.sqrt(2)]]),
'cn' : np.array([[1,0,0,0], [0,1,0,0] , [0,0,0,1] , [0,0,1,0]]),
'icn': np.array([[1,0,0,0] , [0,0,0,1] , [0,0,1,0], [0,1,0,0]]),
'x' : np.array([[0,1] , [1,0]]),
'z' : np.array([[1,0] , [0,-1]]),
'sw' : np.array([[1,0,0,0] , [0,0,1,0] , [0,1,0,0] , [0,0,0,1]]),
's' : np.array([[1,0] , [0, 1j]]),
't' : np.array([[1,0] , [0 , np.exp((1j*np.pi)/4 )]]),
'cz' : np.array([[1,0,0,0], [0,1,0,0] , [0,0,1 ,0] , [0,0,0,-1]]),
'icz': np.array([[1,0,0,0], [0,1,0,0] , [0,0,1 ,0] , [0,0,0,-1]]),
'y' : np.array([[0,-1j] , [1j,0]]),
'i' : np.array([[1,0] , [0,1]])
}
qcircuit= []
# qreg_no = int(input('Enter number of qubits : '))
# creg_no = int(input('Enter number of classical bits : '))
qreg_no,creg_no=3, 2# TEMPORARY
foriinrange(qreg_no):
qcircuit.append(['i'])
whileTrue:
cmd=input("Enter command : ")
ifcmd=='end':
break
gate , reg_no , pos=cmd.split(',')
reg_no=int(reg_no)
pos=int(pos)
#------------ Preprocessing -------------#
ifgatenotingates.keys():
print('Invalid Gate.')
continue
ifreg_no>=qreg_no:
print('Register number is too big.')
continue
if ((gate=='cn'orgate=='cz'orgate=='sw') andreg_no==0) or ((gate=='icn'orgate=='icz') andreg_no==qreg_no-1):
print('This gate cannot be applied at this register.')
continue
ifpos>=len(qcircuit[reg_no]):
pos=len(qcircuit[reg_no])
ifpos<len(qcircuit[reg_no]):
qcircuit[reg_no][pos] =gate
else:
qcircuit[reg_no].append(gate)
foriinqcircuit:
iflen(i) <len(qcircuit[reg_no]):
i.append('i')
ifgate=='cn'orgate=='cz'orgate=='sw':
qcircuit[reg_no-1][pos] ='i'
elifgate=='icn'orgate=='icz':
qcircuit[reg_no+1][pos] ='i'
else:
ifreg_no!=qreg_no-1and(qcircuit[reg_no+1][pos] =='cn'orqcircuit[reg_no+1][pos] =='cz'orqcircuit[reg_no+1][pos] =='sw'):
qcircuit[reg_no+1][pos] ='i'
ifreg_no!=0and(qcircuit[reg_no-1][pos] =='icn'orqcircuit[reg_no-1][pos] =='icz'):
qcircuit[reg_no-1][pos] ='i'
#----------------------------------------#
print(qcircuit)
qcircuit=np.array(qcircuit)
print(qcircuit)
column_matrix= []
foriinrange(len(qcircuit[0])):
tensor_product='temp'
forjinrange(len(qcircuit)):
if (j!=len(qcircuit) -1and (qcircuit[j+1][i] =='cn'orqcircuit[j+1][i] =='cz'orqcircuit[j+1][i] =='sw')) or (j!=0and (qcircuit[j-1][i] =='icn'orqcircuit[j-1][i] =='icz')):
continue
iftype(tensor_product) ==str:
tensor_product=gates.get(qcircuit[j][i])
continue
else:
tensor_product=np.kron(tensor_product,gates.get(qcircuit[j][i]))
column_matrix.append(tensor_product)
# print(column_matrix)
circuit_matrix='temp'
foriinrange(len(column_matrix)):
ifi==0:
circuit_matrix=column_matrix[i]
else:
circuit_matrix=np.dot(circuit_matrix , column_matrix[i])
input_vector=np.array([1,0])
foriinrange(qreg_no-1):
input_vector=np.kron(input_vector , np.array([1,0]) )
output_vector=np.tensordot(input_vector,circuit_matrix,axes=1)
probability_amplitudes= [round(i*i , 2) foriinoutput_vector]
output_names= [''.join(i) foriinitertools.product(['0', '1'], repeat=qreg_no)]
shots=int(input("Enter number of shots : "))
defmeasure_probability(probability_amplitudes=probability_amplitudes , shots=shots , output_names=output_names):
counts= [0foriinrange(len(probability_amplitudes))]
foriinrange(shots):
measured_value=random.choices(output_names, weights=(probability_amplitudes))
counts[output_names.index(measured_value[0])] +=1
result=dict(zip(output_names, counts))
returnresult
defaddlabels(x,y):
foriinrange(len(x)):
plt.text(i,y[i],y[i])
print(probability_amplitudes)
result=measure_probability()
plt.bar(range(len(result)), list(result.values()), align='center')
plt.xticks(range(len(result)), list(result.keys()))
addlabels(list(result.keys()),list(result.values()))
plt.show()