1
1
import random
2
- from math import e , log , pi , sqrt
3
2
import string
3
+ from math import e , log , pi , sqrt
4
4
5
5
import numpy as np
6
6
@@ -81,7 +81,7 @@ def generateGate(gate, numQubits, qubit1, qubit2=-1):
81
81
if (gate == 'CNOT' and qubit2 != - 1 ):
82
82
control = qubit1
83
83
target = qubit2
84
-
84
+
85
85
# Generates List of Combinations of each qubit. there is 2n of them.
86
86
# Returns List in string form, in the variable opts
87
87
opts = []
@@ -97,35 +97,34 @@ def generateGate(gate, numQubits, qubit1, qubit2=-1):
97
97
mapList .append ([index , - 1 ])
98
98
else :
99
99
mapList .append ([index , index ])
100
-
100
+
101
101
for j , index in enumerate (mapList ):
102
102
if index [1 ] == - 1 :
103
103
# Takes the option and splits to each charachter
104
104
firstList = list (opts [index [0 ]])
105
-
105
+
106
106
# Figure out if looking for 0 or 1
107
107
toFlip = '0'
108
- if firstList [target - 1 ] == '0' :
108
+ if firstList [target - 1 ] == '0' :
109
109
toFlip = '1'
110
- elif firstList [target - 1 ] == '1' :
110
+ elif firstList [target - 1 ] == '1' :
111
111
toFlip = '0'
112
-
113
-
112
+
114
113
targetPattern = firstList
115
- targetPattern [target - 1 ] = toFlip
114
+ targetPattern [target - 1 ] = toFlip
116
115
# The String Searching For
117
116
targetPattern = string .join (targetPattern , '' )
118
117
# The Index of the new strng
119
118
mapList [j ][1 ] = opts .index (targetPattern )
120
-
121
- # Generate Empty Matrix To Use To Create New One
119
+
120
+ # Generate Empty Matrix To Use To Create New One
122
121
newMatrix = np .zeros ((2 ** numQubits , 2 ** numQubits ))
123
-
124
- # Go through the map of 1's and put them in
122
+
123
+ # Go through the map of 1's and put them in
125
124
for item in mapList :
126
125
newMatrix .itemset ((item [0 ], item [1 ]), 1 )
127
-
128
- return np .asmatrix (newMatrix )
126
+
127
+ return np .asmatrix (newMatrix )
129
128
else :
130
129
# Put these here for handyness
131
130
identity = gates .Id
@@ -180,7 +179,8 @@ def amps(self):
180
179
181
180
def applyGate (self , gate , qubit1 , qubit2 = - 1 ):
182
181
if gate == 'CNOT' :
183
- gateMatrix = gates .generateGate (gate , self .numQubits , qubit1 , qubit2 )
182
+ gateMatrix = gates .generateGate (
183
+ gate , self .numQubits , qubit1 , qubit2 )
184
184
self .amplitudes = np .dot (self .amplitudes , gateMatrix )
185
185
else :
186
186
# Qubit 1 is the target
@@ -213,10 +213,3 @@ def measure(self):
213
213
)
214
214
self .measured = True
215
215
return self .value
216
-
217
-
218
- qureg = QuantumRegister (10 )
219
- qureg .applyGate ('X' , 1 )
220
- qureg .applyGate ('X' , 2 )
221
- qureg .applyGate ('CNOT' , 1 , 2 )
222
- print (qureg .measure ())
0 commit comments