Skip to content

Commit

Permalink
Stop use of eval() in QuSim.py
Browse files Browse the repository at this point in the history
  • Loading branch information
adamisntdead committed Jan 5, 2017
1 parent cc26eec commit 7a55b4c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 39 deletions.
82 changes: 45 additions & 37 deletions Code/Simulator/QuSim.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,49 @@ class gates:
####################################################
# Gates #
####################################################
singleQubitGates = {
# Pauli-X / Not Gate
'X': np.matrix([
[0, 1],
[1, 0]
]),
# Pauli-Y Gate
'Y': np.matrix([
[0, -i],
[i, 0]
]),
# Pauli-Z Gate
'Z': np.matrix([
[1, 0],
[0, -1]
]),
# Hadamard Gate
'H': np.multiply(1. / sqrt(2), np.matrix([
[1, 1],
[1, -1]
])),
# Identity Gate
'Id': np.eye(2),
# S & S Dagger Gate
'S': np.matrix([
[1, 0],
[0, i]
]),
'SDagger': np.matrix([
[1, 0],
[0, i]
]).conjugate().transpose(),
# T & T Dagger / Pi over 8 Gate
'T': np.matrix([
[1, 0],
[0, e**(i * pi / 4.)]
]),
'TDagger': np.matrix([
[1, 0],
[0, e**(i * pi / 4.)]
]).conjugate().transpose()
}

# Pauli-X / Not Gate
X = np.matrix([
[0, 1],
[1, 0]
])
# Pauli-Y Gate
Y = np.matrix([
[0, -i],
[i, 0]
])
# Pauli-Z Gate
Z = np.matrix([
[1, 0],
[0, -1]
])
# Hadamard Gate
H = np.multiply(1. / sqrt(2), np.matrix([
[1, 1],
[1, -1]
]))
# Identity Gate
Id = np.eye(2)
# S & S Dagger Gate
S = np.matrix([
[1, 0],
[0, i]
])
SDagger = S.conjugate().transpose()
# T & T Dagger / Pi over 8 Gate
T = np.matrix([
[1, 0],
[0, e**(i * pi / 4.)]
])
TDagger = T.conjugate().transpose()

####################################################
# Helper Functions #
Expand Down Expand Up @@ -89,7 +97,7 @@ def generateGate(gate, numQubits, qubit1, qubit2=-1):
targetPattern = firstList
targetPattern[target - 1] = toFlip
# The String Searching For
targetPattern = string.join(targetPattern, '')
targetPattern = ''.join(targetPattern)
# The Index of the new strng
mapList[j][1] = opts.index(targetPattern)

Expand All @@ -103,8 +111,8 @@ def generateGate(gate, numQubits, qubit1, qubit2=-1):
return np.asmatrix(newMatrix)
else:
# Put these here for handyness
identity = gates.Id
mainGate = eval('gates.' + gate)
identity = gates.singleQubitGates['Id']
mainGate = gates.singleQubitGates[gate]

# Check if there is no modification needed
if numQubits == 1:
Expand Down
2 changes: 0 additions & 2 deletions Code/Simulator/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@


class QuSimTests(unittest.TestCase):
# X Gate

def testXGate(self):
self.assertEqual(X.measure(), '1')

Expand Down

0 comments on commit 7a55b4c

Please sign in to comment.