qubiter.BitVector module

class qubiter.BitVector.BitVector(length, dec_rep)[source]

Bases: object

This class uses an int called dec_rep as a vector of self.len many bits, where self.len <= self.max_len. The class wraps some common bitwise operations, and some less common ones too (like Gray coding that is needed by Qubiter). In some cases, the bitwise manipulation might be more succinct than the corresponding function in this wrapper, but the wrapper function’s name spells out in words what is wanted.

Variables:
  • dec_rep (int) – decimal representation, the int whose binary representation carries a bit vector of length self.len
  • len (int) – the length of the bit vector
  • max_len (int) – maximum self.len allowed
__init__(length, dec_rep)[source]

Constructor

Parameters:
  • length (int) –
  • dec_rep (int) –
__str__()[source]

Readable representation of self

Returns:
Return type:str
bit_is_T(bpos)[source]

Returns True iff bit at position bpos is 1 (True)

Parameters:bpos (int) – bit position
Returns:
Return type:bool
static copy(bvec)[source]

Copy constructor, returns a new BitVector which is a copy of the BitVector bvec.

Parameters:bvec (BitVector) –
Returns:
Return type:BitVector
find_T_bit_to_left_of(bpos)[source]

Returns position of 1 (True) bit immediately to the left of position bpos. Returns -1 if there is no such bit.

Parameters:bpos (int) – bit position
Returns:
Return type:int
find_T_bit_to_right_of(bpos)[source]

Returns position of 1 (True) bit immediately to the right of position bpos. Returns -1 if there is no such bit.

Parameters:bpos (int) – bit position
Returns:
Return type:int
find_leftmost_T_bit()[source]

Out of all 1 (True) bits, returns position of the leftmost one of those to the the left of position bpos. Returns -1 if there is no such bit.

Returns:
Return type:int
find_rightmost_T_bit()[source]

Out of all 1 (True) bits, returns position of the rightmost one of those to the the right of position bpos. Returns -1 if there is no such bit.

Returns:
Return type:int
get_bit_string()[source]

Returns self represented as string of length self.len of ones and zeros. If bit_str is the output, [int(x) for x in bit_str] will turn result to list of ints.

Returns:
Return type:str
static get_lazy_from_normal(bit_len, normal)[source]

Throughout Qubiter, we will often refer to “Gray Code” as “lazy ordering”. In lazy ordering with bit_len many bits, one gives a sequence of bit vectors of length bit_len, so that two adjacent items of the sequence differ by just one bit. For example 000=0, 100=4, 110=6, 010=2, 011=3, 111=7, 101=5, 001=1. Each element of this sequence represented as an int will be called lazy, and each int in the sequence 0, 1, 2, 3, 4, 5, 6, 7 will be called normal. Normal ordering is usually called dictionary ordering. Normal and lazy sequences both start at 0.

Suppose bit_len = 3. The lazy sequence 000, 100, 110, 010, 011, 111, 101, 001 is easily obtained from the “standard” lazy sequence 000, 001, 011, 010, 110, 111, 101, 100 by “reflecting” each sequence term. We will use the second sequence because it is more common in the literature.

References

1. Martin Gardener, “Knotted DoughNuts and Other Mathematical Entertainments”, chapt. 2, “The Binary Gray Code” 2. “Numerical Recipies in C” 3. Many books on Discrete Mathematics for CompSci types 4. On the web, in Eric’s Treasure Trove/Math/Gray Codes

Parameters:
  • bit_len (int) –
  • normal (int) – Function returns the lazy int that corresponds to this normal int.
Returns:

Return type:

int

get_num_T_bits()[source]

Returns the number of 1 (True) bits at positions bpos from 0 to len-1 inclusive.

Returns:
Return type:int
static lazy_advance(old_normal, old_lazy)[source]

This method takes int “old_lazy” (which corresponds to bit vector “old_normal”), and changes it to the next lazy int, “new_lazy” ( which corresponds to “new_normal”).

example:

lazy sequence: 000, 001, 011, 010, 110, 111, 101, 100

old_lazy = 011 old_normal = 2 = 010

new_normal = 3 = 011 mask = (new_normal & ~old_normal) = 011 & 101 = 001 new_lazy = new_normal ^ mask = 011 ^ 001 = 010

Parameters:
  • old_normal (int) –
  • old_lazy (int) –
Returns:

Return type:

int, int

static new_with_T_on_diff(bvec1, bvec2)[source]

Given two BitVectors bevc1 and bvec2, this return a BitVector which is a bitwise xor (mod 2 sum) of the bits of bvec1 and bvec2.

Parameters:
Returns:

Return type:

BitVector

set_all_bits_F()[source]

Sets to 0 (False) the bits of self at positions bpos from 0 to len-1 inclusive.

Returns:
Return type:None
set_all_bits_T()[source]

Sets to 1 (True) the bits of self at position bpos from 0 to len-1 inclusive.

Returns:
Return type:None
set_bit_F(bpos)[source]

Sets to 0 (False) the bit of self at position bpos.

Parameters:bpos (int) – bit position
Returns:
Return type:None
set_bit_T(bpos)[source]

Sets to 1 (True) the bit of self at position bpos.

Parameters:bpos (int) – bit position
Returns:
Return type:None