Skip to content

Commit

Permalink
bit
Browse files Browse the repository at this point in the history
  • Loading branch information
wistaria committed Nov 5, 2023
1 parent 818fb4c commit 32d1f0e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
12 changes: 6 additions & 6 deletions example/grover.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

def oracle(v, target):
n = q.num_qubits(v)
for i in range(n):
if (target >> (n - i - 1)) & 1 == 0:
v = q.apply(v, q.op.x(), [i])
for k in range(n):
if q.util.bit(n, target, k) == 0:
v = q.apply(v, q.op.x(), [k])
v = q.apply_seq(v, q.op.controlled_seq(q.op.z(), list(range(n))))
for i in range(n):
if (target >> (n - i - 1)) & 1 == 0:
v = q.apply(v, q.op.x(), [i])
for k in range(n):
if q.util.bit(n, target, k) == 0:
v = q.apply(v, q.op.x(), [k])
return v


Expand Down
4 changes: 2 additions & 2 deletions src/qailo/util/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .strops import binary2str, str2binary
from .bitops import binary2str, bit, str2binary

__all__ = [binary2str, str2binary]
__all__ = [binary2str, bit, str2binary]
5 changes: 5 additions & 0 deletions src/qailo/util/strops.py → src/qailo/util/bitops.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
def bit(n, s, k):
"""extract k-th bit from integer s"""
return (s >> (n - k - 1)) & 1


def binary2str(n, i):
return bin(i)[2:].zfill(n)[::-1]

Expand Down
2 changes: 1 addition & 1 deletion test/util/test_binary2str.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from qailo.util.strops import binary2str, str2binary
from qailo.util.bitops import binary2str, str2binary


def test_binary2str():
Expand Down
7 changes: 7 additions & 0 deletions test/util/test_bit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from qailo.util.bitops import bit


def test_bit():
assert bit(3, 0b100, 0) == 1
assert bit(3, 0b100, 1) == 0
assert bit(3, 0b100, 2) == 0

0 comments on commit 32d1f0e

Please sign in to comment.