Skip to content

Conversation

@sourcery-ai
Copy link

@sourcery-ai sourcery-ai bot commented Apr 27, 2022

Branch master refactored by Sourcery.

If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.

See our documentation here.

Run Sourcery locally

Reduce the feedback loop during development by using the Sourcery editor plugin:

Review changes via command line

To manually merge these changes, make sure you're on the master branch, then run:

git fetch origin sourcery/master
git merge --ff-only FETCH_HEAD
git reset HEAD^

Help us improve this pull request!

@sourcery-ai sourcery-ai bot requested a review from bgbashiro April 27, 2022 18:12
Comment on lines -26 to +45
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)]}")
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:

Comment on lines -43 to +50
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
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:

Comment on lines -90 to +92
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}"

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:

Comment on lines -102 to +104
if isinstance(other, Sequence):
self.seq += other.seq
else:
self.seq += [other]
self.seq += other.seq if isinstance(other, Sequence) else [other]
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:

Comment on lines -214 to +220

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)))
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)

Comment on lines -46 to +47
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):
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:

Comment on lines -68 to +70
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]):
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:


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:

Comment on lines -83 to +90
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))
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:

Comment on lines -21 to +22
for i in range(0,3):
for j in range(0,3):
for i in range(3):
for j in range(3):
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:

@sourcery-ai
Copy link
Author

sourcery-ai bot commented Apr 27, 2022

Sourcery Code Quality Report

✅  Merging this PR will increase code quality in the affected files by 0.38%.

Quality metrics Before After Change
Complexity 3.47 ⭐ 3.20 ⭐ -0.27 👍
Method Length 38.64 ⭐ 38.37 ⭐ -0.27 👍
Working memory 6.78 🙂 6.78 🙂 0.00
Quality 77.22% 77.60% 0.38% 👍
Other metrics Before After Change
Lines 478 473 -5
Changed files Quality Before Quality After Quality Change
phonebook.py 56.22% 🙂 57.11% 🙂 0.89% 👍
qcomp/qcomp/gate.py 83.77% ⭐ 83.91% ⭐ 0.14% 👍
qcomp/qcomp/qregister.py 85.44% ⭐ 84.81% ⭐ -0.63% 👎
qcomp/qcomp/shors.py 80.96% ⭐ 82.00% ⭐ 1.04% 👍
qcomp/qcomp/utils.py 70.25% 🙂 71.04% 🙂 0.79% 👍
qcomp/qcomp/tests/test_sample.py 75.91% ⭐ 76.11% ⭐ 0.20% 👍

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation
qcomp/qcomp/utils.py SparseMatrix.multiplySparseMatrix 16 🙂 91 🙂 14 😞 48.77% 😞 Extract out complex expressions
phonebook.py main 5 ⭐ 199 😞 9 🙂 55.58% 🙂 Try splitting into smaller methods
qcomp/qcomp/shors.py shors.func 7 ⭐ 102 🙂 10 😞 61.83% 🙂 Extract out complex expressions
qcomp/qcomp/utils.py SparseMatrix.kronSparse 5 ⭐ 106 🙂 10 😞 63.23% 🙂 Extract out complex expressions
qcomp/qcomp/gate.py construct_unitary_F 5 ⭐ 122 😞 7 🙂 67.79% 🙂 Try splitting into smaller methods

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.


Please see our documentation here for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Help us improve this quality report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants