Skip to content

Commit 338283c

Browse files
committed
Add Some Tests and Examples
1 parent 1cd7f8c commit 338283c

File tree

3 files changed

+97
-23
lines changed

3 files changed

+97
-23
lines changed

Code/Simulator/QuSim.py

+16-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import random
2-
from math import e, log, pi, sqrt
32
import string
3+
from math import e, log, pi, sqrt
44

55
import numpy as np
66

@@ -81,7 +81,7 @@ def generateGate(gate, numQubits, qubit1, qubit2=-1):
8181
if (gate == 'CNOT' and qubit2 != -1):
8282
control = qubit1
8383
target = qubit2
84-
84+
8585
# Generates List of Combinations of each qubit. there is 2n of them.
8686
# Returns List in string form, in the variable opts
8787
opts = []
@@ -97,35 +97,34 @@ def generateGate(gate, numQubits, qubit1, qubit2=-1):
9797
mapList.append([index, -1])
9898
else:
9999
mapList.append([index, index])
100-
100+
101101
for j, index in enumerate(mapList):
102102
if index[1] == -1:
103103
# Takes the option and splits to each charachter
104104
firstList = list(opts[index[0]])
105-
105+
106106
# Figure out if looking for 0 or 1
107107
toFlip = '0'
108-
if firstList[target-1] == '0':
108+
if firstList[target - 1] == '0':
109109
toFlip = '1'
110-
elif firstList[target-1] == '1':
110+
elif firstList[target - 1] == '1':
111111
toFlip = '0'
112-
113-
112+
114113
targetPattern = firstList
115-
targetPattern[target-1] = toFlip
114+
targetPattern[target - 1] = toFlip
116115
# The String Searching For
117116
targetPattern = string.join(targetPattern, '')
118117
# The Index of the new strng
119118
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
122121
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
125124
for item in mapList:
126125
newMatrix.itemset((item[0], item[1]), 1)
127-
128-
return np.asmatrix(newMatrix)
126+
127+
return np.asmatrix(newMatrix)
129128
else:
130129
# Put these here for handyness
131130
identity = gates.Id
@@ -180,7 +179,8 @@ def amps(self):
180179

181180
def applyGate(self, gate, qubit1, qubit2=-1):
182181
if gate == 'CNOT':
183-
gateMatrix = gates.generateGate(gate, self.numQubits, qubit1, qubit2)
182+
gateMatrix = gates.generateGate(
183+
gate, self.numQubits, qubit1, qubit2)
184184
self.amplitudes = np.dot(self.amplitudes, gateMatrix)
185185
else:
186186
# Qubit 1 is the target
@@ -213,10 +213,3 @@ def measure(self):
213213
)
214214
self.measured = True
215215
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())

Code/Simulator/examples.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from QuSim import QuantumRegister
2+
3+
#############################################
4+
# Introduction #
5+
#############################################
6+
# Here Will Be A Few Example of Different #
7+
# Quantum States / Algorithms, So You Can #
8+
# Get A Feel For How The Module Works, and #
9+
# Some Algorithmic Ideas #
10+
#############################################
11+
12+
#############################################
13+
# Swap 2 Qubits #
14+
#############################################
15+
# Here, We Will Apply a Pauli-X Gate / NOT Gate
16+
# To the first qubit, and then after the algorithm,
17+
# it will be swapped to the second qubit.
18+
19+
Swap = QuantumRegister(2) # New Quantum Register of 2 qubits
20+
Swap.applyGate('X', 1) # Apply The NOT Gate. If Measured Now, it should be 10
21+
22+
# Start the swap algorithm
23+
Swap.applyGate('CNOT', 1, 2)
24+
Swap.applyGate('H', 1)
25+
Swap.applyGate('H', 2)
26+
Swap.applyGate('CNOT', 1, 2)
27+
Swap.applyGate('H', 1)
28+
Swap.applyGate('H', 2)
29+
Swap.applyGate('CNOT', 1, 2)
30+
# End the swap algorithm
31+
32+
print('SWAP: ' + Swap.measure()) # Measure the State, Should be 01

Code/Simulator/test.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import unittest
2+
import QuSim
3+
4+
5+
###########################################
6+
# Cases #
7+
###########################################
8+
# Pauli-X Gate
9+
X = QuSim.QuantumRegister(1)
10+
X.applyGate('X', 1)
11+
12+
# Multiple qubits
13+
Multi = QuSim.QuantumRegister(5)
14+
Multi.applyGate('X', 4)
15+
16+
# CNOT Gates
17+
CNOT1 = QuSim.QuantumRegister(10)
18+
CNOT1.applyGate('CNOT', 3, 7)
19+
20+
CNOT2 = QuSim.QuantumRegister(10)
21+
CNOT2.applyGate('X', 3)
22+
CNOT2.applyGate('CNOT', 3, 7)
23+
24+
CNOT3 = QuSim.QuantumRegister(9)
25+
CNOT3.applyGate('X', 3)
26+
CNOT3.applyGate('X', 7)
27+
CNOT3.applyGate('CNOT', 3, 7)
28+
29+
###########################################
30+
# Tests #
31+
###########################################
32+
class QuSimTests(unittest.TestCase):
33+
# X Gate
34+
def testXGate(self):
35+
self.assertEqual(X.measure(), '1')
36+
37+
def testMultipleQubits(self):
38+
self.assertEqual(Multi.measure(), '00010')
39+
40+
def testCNOT(self):
41+
self.assertEqual(CNOT1.measure(), '0000000000')
42+
self.assertEqual(CNOT2.measure(), '0010001000')
43+
self.assertEqual(CNOT3.measure(), '001000000')
44+
45+
def main():
46+
unittest.main()
47+
48+
if __name__ == '__main__':
49+
main()

0 commit comments

Comments
 (0)