Skip to content

Commit

Permalink
store bits to flip always as an int
Browse files Browse the repository at this point in the history
  • Loading branch information
cgogolin committed Dec 18, 2018
1 parent 80315fc commit 6be145a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
13 changes: 5 additions & 8 deletions projectq/ops/_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,21 +368,18 @@ def __init__(self, bits_to_flip):
"""
if isinstance(bits_to_flip, int):
self.bits_to_flip = bits_to_flip
elif isinstance(bits_to_flip, str):
self.bits_to_flip = list([ c != "0" for c in bits_to_flip])
else:
self.bits_to_flip = list(bits_to_flip)
self.bits_to_flip = 0
for c in reversed(list(bits_to_flip)):
bit = 0b1 if c == '1' or c == 1 or c == True else 0b0
self.bits_to_flip = (self.bits_to_flip << 1) | bit
print("self.bits_to_flip="+str(self.bits_to_flip)+"="+str(list(bits_to_flip)))

def __str__(self):
return "FlipBits("+str(self.bits_to_flip)+")"

def __or__(self, qubits):
for qureg in self.make_tuple_of_qureg(qubits):
if isinstance(self.bits_to_flip, int):
bits_to_flip = [((self.bits_to_flip if self.bits_to_flip >= 0 else self.bits_to_flip-1) >> i) & 1 for i in range(len(qureg))]
else:
bits_to_flip = self.bits_to_flip

for i, flip in enumerate(bits_to_flip):
if flip:
XGate() | qureg[i]
Expand Down
4 changes: 2 additions & 2 deletions projectq/ops/_gates_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,5 +230,5 @@ def test_flip_bits_equality_and_hash():


def test_flip_bits_str():
gate1 = _gates.FlipBits([0, 1, 0])
assert str(gate1) == "FlipBits([0, 1, 0])"
gate1 = _gates.FlipBits([0, 0, 1])
assert str(gate1) == "FlipBits(4)"

0 comments on commit 6be145a

Please sign in to comment.