-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.py
108 lines (76 loc) · 2.22 KB
/
random.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
from qiskit import QuantumCircuit
import qiskit.quantum_info as qi
from qiskit import Aer
from qiskit.providers.aer.library import SaveStatevector
from qiskit.compiler import transpile
import numpy as np
import matplotlib.pyplot as plt
from Entagleclass import EntagleClass
def InsertSaveStates(qc,nq):
num_qubits = qc.num_qubits
pos=1
nIns=len(qc.data)
for i in range(nIns):
_inst = SaveStatevector(nq, label = 'psi_' + str(i))
qc.data.insert(pos, [_inst, qc.qubits, None])
pos+=2
nIns=len(qc.data)
def printInstrucciones(qc):
nInstrucs=len(qc.data)
print("\nINSTRUCCIONES " , nInstrucs)
for i in range(nInstrucs):
tupi=qc.data[i]
print(tupi[0])
sq="Qubits : "
for k in range(len(tupi[1])):
sq+=str(tupi[1][k].index)+ " "
print(sq)
def printProbabilies(sv):
dic = sv.probabilities_dict()
i=0
total=0
print("\nProbabilities\n")
for key, value in dic.items():
if value>0:
total += value
print(i, ") ",key, ' : ', np.round(value*100,3), " %")
i+=1
print("total= ", np.round(total*100,3), " %")
nq=5
nstates=int(2**nq)
simulator = Aer.get_backend('aer_simulator')
qc = QuantumCircuit(nq)
qc.h([0,1])
##qc.cx(0,1)
##qc.ry(np.pi/4,1)
##qc.x(0)
#qc.ccx(0,1,2)
##qc.mct([0,1],2)
sv = qi.random_statevector(nstates)
qc.initialize(sv)
for d in range(20):
qc=qc.decompose()
qc = transpile(qc, simulator)
InsertSaveStates(qc,nq )
#printInstrucciones(qc)
job = simulator.run(qc,shots=100)
res = job.result()
objentagle= EntagleClass(nq)
for i in range (int(len(qc.data)/2)):
svsaved=res.data(0)['psi_'+str(i)]
posInstruc=i*2
tupi=qc.data[posInstruc]
if True:
#if tupi[0].name=="cx":
si=str(i+1) + ") " + tupi[0].name + " ( "
for k in range(len(tupi[1])):
si+=str(tupi[1][k].index)
if k < len(tupi[1]) -1 : si +=' , '
print(si + ")")
objentagle.calcSVentagle(svsaved)
print (objentagle.GetEntagleString(True))
##printProbabilies(svsaved)
#qc.draw(output='mpl',scale =0.4)
#plt.show()
#printInstrucciones(qc)
#printProbabilies(qc)