Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions phonebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,25 @@ def main():
# chooses random name
k = np.random.randint((len(names)))
print(numbers[k])
print("Looking for " + names[k] + "'s number")
print(f"Looking for {names[k]}" + "'s number")

# oracle construction
fdef = bin(k)[2:]
fdef = "0"*(n-len(fdef)) + fdef
g.def_oracle(fdef)
print("We are looking for " + fdef)
print(f"We are looking for {fdef}")

# running the algorithm
reg = g.run_iteration()
final = ""
for i in range(reg.nbits-1):
_, p = reg.get_qbit(i)
if p.state[0]>0.5:
final += '0'
else:
final += '1'
final += '0' if p.state[0]>0.5 else '1'
for i in range(reg.nbits):
print(reg.get_qbit(i)[0])

# output of answers
print("Probably looking for : "+final)
print("Found Number: " + numbers[int(final,2)])
print(f"Probably looking for : {final}")
print(f"Found Number: {numbers[int(final,2)]}")
Comment on lines -26 to +45
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:


main()
25 changes: 12 additions & 13 deletions qcomp/qcomp/gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,14 @@ def construct_unitary_F(fdef, in_size=None):
cmatrix = np.zeros([2**reg_size,2**reg_size])
for base, i_col in zip(dummy_bases, range(0,2**reg_size,2)):
if base in fdef:
target_state = int(base+"1",2)
target_state = int(f"{base}1", 2)
cmatrix[target_state,i_col] = 1
target_state = int(base+"0",2)
cmatrix[target_state,i_col+1] = 1
target_state = int(f"{base}0", 2)
else:
target_state = int(base+"0",2)
target_state = int(f"{base}0", 2)
cmatrix[target_state,i_col] = 1
target_state = int(base+"1",2)
cmatrix[target_state,i_col+1] = 1
target_state = int(f"{base}1", 2)
cmatrix[target_state,i_col+1] = 1
Comment on lines -43 to +50
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function construct_unitary_F refactored with the following changes:

return MGate(cmatrix,reg_size)

class Gate():
Expand Down Expand Up @@ -87,7 +86,10 @@ def apply(self, qreg):
----------
new_qreg : QReg object created after transformation
"""
assert qreg.nbits == self.qreg_size, "This gate cannot be applied to register of size {}. Expected size: {}".format(qreg.nbits, self.qreg_size)
assert (
qreg.nbits == self.qreg_size
), f"This gate cannot be applied to register of size {qreg.nbits}. Expected size: {self.qreg_size}"

Comment on lines -90 to +92
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Gate.apply refactored with the following changes:

raise NotImplementedError("Apply method not implemented yet!")

class Sequence(Gate):
Expand All @@ -99,10 +101,7 @@ def __init__(self, qreg_size):
super(Sequence, self).__init__(qreg_size)

def add(self, other):
if isinstance(other, Sequence):
self.seq += other.seq
else:
self.seq += [other]
self.seq += other.seq if isinstance(other, Sequence) else [other]
Comment on lines -102 to +104
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Sequence.add refactored with the following changes:


def apply(self, qreg):
tqreg = qreg.copy()
Expand Down Expand Up @@ -211,14 +210,14 @@ def __init__(self, matrix, qbpos, qreg_size):
*qreg_size* : size of register it will act on
"""
super().__init__(qreg_size)

self.matrix = matrix
self.msize = 2**len(qbpos)
self.qsize = 2**qreg_size
self.bases = np.arange(self.qsize)

self.qbpos = qbpos
self.specpos = list(filter(lambda x : not x in qbpos, range(qreg_size)))
self.specpos = list(filter(lambda x: x not in qbpos, range(qreg_size)))
Comment on lines -214 to +220
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function LazyGate.__init__ refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)


# time to shuffle indices
# first group the indices which has same spectators
Expand Down
12 changes: 7 additions & 5 deletions qcomp/qcomp/qregister.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ def __len__(self):
def __repr__(self):
"""print state of qreg as a|00..0> + b|00..1> + ...
"""
representation = ""
for coeff, base in zip(self.state, self.bases):
representation += "{0:.2f} \t |{1}> \n".format(coeff, base)
return representation
return "".join(
"{0:.2f} \t |{1}> \n".format(coeff, base)
for coeff, base in zip(self.state, self.bases)
)
Comment on lines -93 to +96
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function QReg.__repr__ refactored with the following changes:


def get_qbit(self, i_qbit):
"""Get state of ith qbit
Expand All @@ -102,4 +102,6 @@ def get_qbit(self, i_qbit):
state_1_proba = (state_1_coeffs**2).sum()
state_0_coeffs = self.state[[b[i_qbit] == '0' for b in self.bases]]
state_0_proba = (state_0_coeffs**2).sum()
return "{}|0> + {}|1>".format(state_0_proba, state_1_proba), QBit([state_0_proba, state_1_proba])
return f"{state_0_proba}|0> + {state_1_proba}|1>", QBit(
[state_0_proba, state_1_proba]
)
8 changes: 3 additions & 5 deletions qcomp/qcomp/shors.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,16 @@ def func(self):
print(M.gcd(self.a,self.N))
if M.gcd(self.a,self.N) == 1:
self.QFT()
if self.period % 2:
break
else:
if not self.period % 2:
self.root1 = M.gcd(int(self.a**(self.period/2)+1),self.N)
self.root2 = M.gcd(int(self.a**(self.period/2)-1),self.N)
break
break
Comment on lines -22 to +25
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function shors.func refactored with the following changes:

else:
self.root3 = self.N/self.gcd


def generateSequence(self):
for i in range(0,self.Q):
for i in range(self.Q):
Comment on lines -33 to +31
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function shors.generateSequence refactored with the following changes:

self.seqList.append([i, self.a**i % self.N])


Expand Down
4 changes: 2 additions & 2 deletions qcomp/qcomp/tests/test_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def test_smatrix():
sM = sparseMatrix(3,4)


for i in range(0,3):
for j in range(0,3):
for i in range(3):
for j in range(3):
Comment on lines -21 to +22
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_smatrix refactored with the following changes:

if i%2 == 0:
sN.elements.append(element(i,j,1))
if j%2 == 0:
Expand Down
19 changes: 9 additions & 10 deletions qcomp/qcomp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def dot(self, vector):
def multiplySparseMatrix(self,that):
elements = []
assert self.colSize == that.rowSize,"Matrix Sizes Are Incompatible!!!"
for i in range(0,self.rowSize):
for j in range(0,that.colSize):
for i in range(self.rowSize):
for j in range(that.colSize):
Comment on lines -46 to +47
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SparseMatrix.multiplySparseMatrix refactored with the following changes:

values = []
for n in self.elements:
for m in that.elements:
Expand All @@ -65,30 +65,29 @@ def convertToSparse(matrix):
_ = matrix.shape[1]
except IndexError:
matrix = matrix.reshape([-1,1])
new = SparseMatrix(matrix.shape[0],matrix.shape[1])
for i in range(0,matrix.shape[0]):
for j in range(0,matrix.shape[1]):
new = SparseMatrix(matrix.shape[0],matrix.shape[1])
for i in range(matrix.shape[0]):
for j in range(matrix.shape[1]):
Comment on lines -68 to +70
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SparseMatrix.convertToSparse refactored with the following changes:

if matrix[i][j] != 0:
new.elements.append(element(i,j,matrix[i][j]))
return new

def scalarMultiply(self,scalar):
for i in range(0,len(self.elements)):
for i in range(len(self.elements)):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SparseMatrix.scalarMultiply refactored with the following changes:

self.elements[i].value = scalar*self.elements[i].value
@staticmethod
def kronSparse(this,that):
new1 = []
for element in this.elements:
new = copy.deepcopy(that)
for i in range(0,len(new.elements)):
for i in range(len(new.elements)):
new.elements[i].value = element.value*new.elements[i].value
cornerRow = element.rowIndex*that.rowSize
cornerCol = element.colIndex*that.colSize
for i in range (0,len(new.elements)):
for i in range(len(new.elements)):
new.elements[i].rowIndex += cornerRow
new.elements[i].colIndex += cornerCol
for element in new.elements:
new1.append(element)
new1.extend(iter(new.elements))
Comment on lines -83 to +90
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SparseMatrix.kronSparse refactored with the following changes:

kronProd = SparseMatrix(this.rowSize*that.rowSize, this.colSize*that.colSize)
kronProd.elements = new1
return kronProd