本文整理汇总了Python中BitVector.get_num_T_bits方法的典型用法代码示例。如果您正苦于以下问题:Python BitVector.get_num_T_bits方法的具体用法?Python BitVector.get_num_T_bits怎么用?Python BitVector.get_num_T_bits使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BitVector
的用法示例。
在下文中一共展示了BitVector.get_num_T_bits方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: hadamard_mat
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import get_num_T_bits [as 别名]
def hadamard_mat(num_bits, is_quantum=True):
"""
This function return a numpy array with the num_bits-fold tensor
product of the 2 dim Hadamard matrix H. If is_quantum=True (False,
resp.), it returns a complex (real, resp.) array.
Parameters
----------
num_bits : int
is_quantum : bool
Returns
-------
"""
num_rows = (1 << num_bits)
norma = np.sqrt(num_rows)
if is_quantum:
ty = complex
else:
ty = float
mat = np.full((num_rows, num_rows),
fill_value=1/norma, dtype=ty)
bvec = BitVector(num_bits, 0)
for j in range(num_rows):
for k in range(num_rows):
bvec.dec_rep = j & k
if bvec.get_num_T_bits() % 2 == 1:
mat[j, k] = - mat[j, k]
return mat
示例2: write_internal
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import get_num_T_bits [as 别名]
def write_internal(self, rads_list, delta=None):
"""
This internal function is used in write() and is less general than
the latter. It expands an c_u2 into a product of 1c_u2 with
intervening cnots.
In the CGateSEO_writer.pdf documentation, we show that any c_u2 can
be expanded into a product of several "generalized n" controlled U(
2) gates of the form W(num_bits-1)^GN, wherein U(2) matrix W(
num_bits-1) is controlled by a "generalized n" equal to GN = n(
n_index_list)
Since the factors W(num_bits-1)^GN in the product commute amongst
themselves, it is possible and convenient to order them in Gray code
order (Qubiter knows about Gray Code via its class BitVector).
Ordering them in Gray Code allows this function to cancel some cnots
from adjacent GN.
An earlier version of this function, now commented, did not use Gray
Code and used more cnots than this one.
Parameters
----------
rads_list : list[float]
list of 3 angles in radians. If it equals [radx, rady, radz],
then U(2) gate given by e^{i*delta} exp(i*(radx*sigx + rady*sigy
+ radz*sigz))
delta : float|None
U(2) gate being controlled equals e^{i*delta} times SU(2) gate
Returns
-------
None
"""
num_bits = self.emb.num_bits_bef
num_trols = num_bits-1
max_f = (1 << num_trols)-1
def write_cnot(tar_bpos, trol_bpos):
trol = Controls.new_knob(num_bits, trol_bpos, True)
self.write_controlled_one_bit_gate(tar_bpos, trol,
OneBitGates.sigx)
def write_cnot_stair(bvec):
tar_bpos = bvec.find_rightmost_T_bit()
trol_bpos = tar_bpos
while True:
trol_bpos = bvec.find_T_bit_to_left_of(trol_bpos)
if trol_bpos == -1:
break
write_cnot(tar_bpos, trol_bpos)
cur_bvec = BitVector(num_trols, 0)
prev_bvec = BitVector(num_trols, 0)
f, lazy = 0, 0
f, lazy = BitVector.lazy_advance(f, lazy)
cur_bvec.dec_rep = lazy
while f <= max_f:
if self.verbose:
print("\nf, lazy", f, lazy)
self.write_NOTA(str(prev_bvec) + "->" + str(cur_bvec))
sign = 1
if cur_bvec.get_num_T_bits() % 2 == 0:
sign = -1
new_rads_list = list(
sign*np.array(rads_list)/(1 << (num_bits-2)))
if delta:
new_delta = sign*delta/(1 << (num_bits-2))
else:
new_delta = None
diff_bvec = BitVector.new_with_T_on_diff(cur_bvec, prev_bvec)
diff_bpos = diff_bvec.find_rightmost_T_bit()
min_prev_bpos = prev_bvec.find_rightmost_T_bit()
min_cur_bpos = cur_bvec.find_rightmost_T_bit()
if f > 1: # first 1c_u2 has not cnots preceding it
if min_cur_bpos == min_prev_bpos:
write_cnot(min_cur_bpos, diff_bpos)
else:
write_cnot_stair(prev_bvec)
write_cnot_stair(cur_bvec)
u2_trol_bpos = min_cur_bpos
self.write_1c_u2(
num_bits - 1, u2_trol_bpos, new_rads_list, new_delta)
prev_bvec = BitVector.copy(cur_bvec)
f, lazy = BitVector.lazy_advance(f, lazy)
cur_bvec.dec_rep = lazy