1
1
from qiskit import QuantumCircuit
2
2
from qiskit import QuantumRegister , ClassicalRegister , QuantumCircuit
3
3
from math import pi
4
- from qiskit import Aer , execute
4
+ from qiskit_aer import AerSimulator
5
5
import random
6
6
7
7
def binaryToDecimal (binary ):
@@ -16,8 +16,8 @@ def binaryToDecimal(binary):
16
16
def createCircuit (numUsers , QECQubits , registers ):
17
17
users = QuantumRegister (numUsers , "user" )
18
18
ancillas = QuantumRegister (QECQubits , "ancilla" )
19
- cr = ClassicalRegister (registers , 'c ' )
20
- qc = QuantumCircuit (users , ancillas , cr )
19
+ meas = ClassicalRegister (registers , 'measure ' )
20
+ qc = QuantumCircuit (users , ancillas , meas )
21
21
return qc
22
22
23
23
numUsers = 3
@@ -46,6 +46,12 @@ def errorX(qc, userIn, userEn):
46
46
def errorCorrection (qc , userIn , userEn , QECQubit ):
47
47
qc .cnot (userIn , QECQubit )
48
48
qc .cnot (userEn , QECQubit )
49
+ qc .barrier ()
50
+
51
+ qc .measure (QECQubit , 2 )
52
+ with qc .if_test ((2 , 1 )):
53
+ qc .x (userIn )
54
+
49
55
return qc
50
56
51
57
def getPrivateKeys (userIn , userEn , QECQubit , keyLength ):
@@ -57,22 +63,21 @@ def getPrivateKeys(userIn, userEn, QECQubit, keyLength):
57
63
58
64
qc = errorCorrection (qc , userIn , userEn , QECQubit )
59
65
qc .barrier ()
60
- qc .measure ([userIn , userEn , QECQubit ], [0 ,1 , 2 ])
66
+ qc .measure ([userIn , userEn ], [0 ,1 ])
61
67
62
68
lstIn = []
63
69
lstEn = []
64
70
counts = []
65
71
66
-
67
72
while len (lstEn ) <= keyLength :
68
- backend = Aer . get_backend ( 'qasm_simulator' )
69
- job_sim = execute (qc , backend , shots = 1 )
70
- result = job_sim .result ()
71
- count = result .get_counts (qc )
73
+ sim = AerSimulator ()
74
+ job = sim . run (qc , shots = 1 )
75
+ result = job .result ()
76
+ count = result .get_counts ()
72
77
counts .append (count )
73
- if ( int ( list ( count . keys ())[ 0 ][ 0 ]) == 0 ):
74
- lstIn .append (list (count .keys ())[0 ][registers - userIn - 1 ])
75
- lstEn .append (list (count .keys ())[0 ][registers - userEn - 1 ])
78
+
79
+ lstIn .append (list (count .keys ())[0 ][registers - userIn - 1 ])
80
+ lstEn .append (list (count .keys ())[0 ][registers - userEn - 1 ])
76
81
77
82
78
83
if (len (lstIn ) != 0 and len (lstEn ) != 0 ):
@@ -84,7 +89,3 @@ def getPrivateKeys(userIn, userEn, QECQubit, keyLength):
84
89
keyEn = binaryToDecimal (int ("" .join (lstEn )))
85
90
86
91
return keyIn , keyEn
87
-
88
-
89
- result = getPrivateKeys (0 , 1 , 3 , 16 )
90
- print (result )
0 commit comments