本文整理汇总了Python中cmath.exp方法的典型用法代码示例。如果您正苦于以下问题:Python cmath.exp方法的具体用法?Python cmath.exp怎么用?Python cmath.exp使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cmath
的用法示例。
在下文中一共展示了cmath.exp方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_or_gate_identity
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import exp [as 别名]
def test_or_gate_identity():
saving_backend = DummyEngine(save_commands=True)
eng = MainEngine(backend=saving_backend, engine_list=[])
qureg = eng.allocate_qureg(4)
hamiltonian = QubitOperator((), 3.4)
correct_h = copy.deepcopy(hamiltonian)
gate = te.TimeEvolution(2.1, hamiltonian)
gate | qureg
eng.flush()
cmd = saving_backend.received_commands[4]
assert isinstance(cmd.gate, Ph)
assert cmd.gate == Ph(-3.4 * 2.1)
correct = numpy.array([[cmath.exp(-1j * 3.4 * 2.1), 0],
[0, cmath.exp(-1j * 3.4 * 2.1)]])
print(correct)
print(cmd.gate.matrix)
assert numpy.allclose(cmd.gate.matrix, correct)
示例2: arrow
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import exp [as 别名]
def arrow(dev, origin, vec, lc, color, ccw=cmath.exp(3j * cmath.pi/4), cw=cmath.exp(-3j * cmath.pi/4)):
length, theta = cmath.polar(vec)
uv = cmath.rect(1, theta) # Unit rotation vector
start = -vec
if length > 3 * lc: # If line is long
ds = cmath.rect(lc, theta)
start += ds # shorten to allow for length of tail chevrons
chev = lc + 0j
polar(dev, origin, vec, color) # Origin to tip
polar(dev, origin, start, color) # Origin to tail
polar(dev, origin + conj(vec), chev*ccw*uv, color) # Tip chevron
polar(dev, origin + conj(vec), chev*cw*uv, color)
if length > lc: # Confusing appearance of very short vectors with tail chevron
polar(dev, origin + conj(start), chev*ccw*uv, color) # Tail chevron
polar(dev, origin + conj(start), chev*cw*uv, color)
# If a (framebuf based) device is passed to refresh, the screen is cleared.
# None causes pending widgets to be drawn and the result to be copied to hardware.
# The pend mechanism enables a displayable object to postpone its renedering
# until it is complete: efficient for e.g. Dial which may have multiple Pointers
示例3: phase2t
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import exp [as 别名]
def phase2t(self, psi):
"""Given phase -pi < psi <= pi,
returns the t value such that
exp(1j*psi) = self.u1transform(self.point(t)).
"""
def _deg(rads, domain_lower_limit):
# Convert rads to degrees in [0, 360) domain
degs = degrees(rads % (2*pi))
# Convert to [domain_lower_limit, domain_lower_limit + 360) domain
k = domain_lower_limit // 360
degs += k * 360
if degs < domain_lower_limit:
degs += 360
return degs
if self.delta > 0:
degs = _deg(psi, domain_lower_limit=self.theta)
else:
degs = _deg(psi, domain_lower_limit=self.theta)
return (degs - self.theta)/self.delta
示例4: hamiltonian
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import exp [as 别名]
def hamiltonian(self): # follow Zhu et al., PRB 87, 024510 (2013)
"""Construct Hamiltonian matrix in harmonic-oscillator basis, following Zhu et al., PRB 87, 024510 (2013)
Returns
-------
ndarray
"""
dimension = self.hilbertdim()
diag_elements = [i * self.E_plasma() for i in range(dimension)]
lc_osc_matrix = np.diag(diag_elements)
exp_matrix = self.exp_i_phi_operator() * cmath.exp(1j * 2 * np.pi * self.flux)
cos_matrix = 0.5 * (exp_matrix + exp_matrix.conjugate().T)
hamiltonian_mat = lc_osc_matrix - self.EJ * cos_matrix
return np.real(hamiltonian_mat) # use np.real to remove rounding errors from matrix exponential,
# fluxonium Hamiltonian in harm. osc. basis is real-valued
示例5: test_SWSH_NINJA_values
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import exp [as 别名]
def test_SWSH_NINJA_values(special_angles, ell_max):
def m2Y22(iota, phi):
return math.sqrt(5 / (64 * np.pi)) * (1 + math.cos(iota)) ** 2 * cmath.exp(2j * phi)
def m2Y21(iota, phi):
return math.sqrt(5 / (16 * np.pi)) * math.sin(iota) * (1 + math.cos(iota)) * cmath.exp(1j * phi)
def m2Y20(iota, phi):
return math.sqrt(15 / (32 * np.pi)) * math.sin(iota) ** 2
def m2Y2m1(iota, phi):
return math.sqrt(5 / (16 * np.pi)) * math.sin(iota) * (1 - math.cos(iota)) * cmath.exp(-1j * phi)
def m2Y2m2(iota, phi):
return math.sqrt(5 / (64 * np.pi)) * (1 - math.cos(iota)) ** 2 * cmath.exp(-2j * phi)
for iota in special_angles:
for phi in special_angles:
assert abs(slow_sYlm(-2, 2, 2, iota, phi) - m2Y22(iota, phi)) < ell_max * precision_SWSH
assert abs(slow_sYlm(-2, 2, 1, iota, phi) - m2Y21(iota, phi)) < ell_max * precision_SWSH
assert abs(slow_sYlm(-2, 2, 0, iota, phi) - m2Y20(iota, phi)) < ell_max * precision_SWSH
assert abs(slow_sYlm(-2, 2, -1, iota, phi) - m2Y2m1(iota, phi)) < ell_max * precision_SWSH
assert abs(slow_sYlm(-2, 2, -2, iota, phi) - m2Y2m2(iota, phi)) < ell_max * precision_SWSH
示例6: test_SWSH_spin_behavior
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import exp [as 别名]
def test_SWSH_spin_behavior(Rs, special_angles, ell_max):
# We expect that the SWSHs behave according to
# sYlm( R * exp(gamma*z/2) ) = sYlm(R) * exp(-1j*s*gamma)
# See http://moble.github.io/spherical_functions/SWSHs.html#fn:2
# for a more detailed explanation
# print("")
for i, R in enumerate(Rs):
# print("\t{0} of {1}: R = {2}".format(i, len(Rs), R))
for gamma in special_angles:
for ell in range(ell_max + 1):
for s in range(-ell, ell + 1):
LM = np.array([[ell, m] for m in range(-ell, ell + 1)])
Rgamma = R * np.quaternion(math.cos(gamma / 2.), 0, 0, math.sin(gamma / 2.))
sYlm1 = sf.SWSH(Rgamma, s, LM)
sYlm2 = sf.SWSH(R, s, LM) * cmath.exp(-1j * s * gamma)
# print(R, gamma, ell, s, np.max(np.abs(sYlm1-sYlm2)))
assert np.allclose(sYlm1, sYlm2, atol=ell ** 6 * precision_SWSH, rtol=ell ** 6 * precision_SWSH)
示例7: DFT2D
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import exp [as 别名]
def DFT2D(image):
global M, N
(M, N) = image.size # (imgx, imgy)
dft2d_red = [[0.0 for k in range(M)] for l in range(N)]
dft2d_grn = [[0.0 for k in range(M)] for l in range(N)]
dft2d_blu = [[0.0 for k in range(M)] for l in range(N)]
pixels = image.load()
for k in range(M):
for l in range(N):
sum_red = 0.0
sum_grn = 0.0
sum_blu = 0.0
for m in range(M):
for n in range(N):
(red, grn, blu, alpha) = pixels[m, n]
e = cmath.exp(- 1j * pi2 * (float(k * m) / M + float(l * n) / N))
sum_red += red * e
sum_grn += grn * e
sum_blu += blu * e
dft2d_red[l][k] = sum_red / M / N
dft2d_grn[l][k] = sum_grn / M / N
dft2d_blu[l][k] = sum_blu / M / N
return (dft2d_red, dft2d_grn, dft2d_blu)
示例8: IDFT2D
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import exp [as 别名]
def IDFT2D(dft2d):
(dft2d_red, dft2d_grn, dft2d_blu) = dft2d
global M, N
image = Image.new("RGB", (M, N))
pixels = image.load()
for m in range(M):
for n in range(N):
sum_red = 0.0
sum_grn = 0.0
sum_blu = 0.0
for k in range(M):
for l in range(N):
e = cmath.exp(1j * pi2 * (float(k * m) / M + float(l * n) / N))
sum_red += dft2d_red[l][k] * e
sum_grn += dft2d_grn[l][k] * e
sum_blu += dft2d_blu[l][k] * e
red = int(sum_red.real + 0.5)
grn = int(sum_grn.real + 0.5)
blu = int(sum_blu.real + 0.5)
pixels[m, n] = (red, grn, blu)
return image
# TEST
# Recreate input image from 2D DFT results to compare to input image
示例9: _apply_unitary_
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import exp [as 别名]
def _apply_unitary_(self,
args: 'cirq.ApplyUnitaryArgs') -> Optional[np.ndarray]:
if cirq.is_parameterized(self):
return None
if self.theta != 0:
inner_matrix = protocols.unitary(cirq.rx(2 * self.theta))
oi = args.subspace_index(0b01)
io = args.subspace_index(0b10)
out = cirq.apply_matrix_to_slices(args.target_tensor,
inner_matrix,
slices=[oi, io],
out=args.available_buffer)
else:
out = args.target_tensor
if self.phi != 0:
ii = args.subspace_index(0b11)
out[ii] *= cmath.exp(-1j * self.phi)
return out
示例10: CRotz
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import exp [as 别名]
def CRotz(theta):
r"""Two-qubit controlled rotation about the z axis.
Args:
theta (float): rotation angle
Returns:
array: unitary 4x4 rotation matrix :math:`|0\rangle\langle 0|\otimes \mathbb{I}+|1\rangle\langle 1|\otimes R_z(\theta)`
"""
return np.array(
[
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, cmath.exp(-1j * theta / 2), 0],
[0, 0, 0, cmath.exp(1j * theta / 2)],
]
)
示例11: coherent_state
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import exp [as 别名]
def coherent_state(a, phi=0, hbar=2.0):
r"""Returns a coherent state.
Args:
a (complex) : the displacement
phi (float): the phase
hbar (float): (default 2) the value of :math:`\hbar` in the commutation
relation :math:`[\x,\p]=i\hbar`
Returns:
array: the coherent state
"""
alpha = a * cmath.exp(1j * phi)
means = np.array([alpha.real, alpha.imag]) * math.sqrt(2 * hbar)
cov = np.identity(2) * hbar / 2
state = [means, cov]
return state
示例12: displaced_squeezed_state
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import exp [as 别名]
def displaced_squeezed_state(a, phi_a, r, phi_r, hbar=2.0):
r"""Returns a squeezed coherent state
Args:
a (real): the displacement magnitude
phi_a (real): the displacement phase
r (float): the squeezing magnitude
phi_r (float): the squeezing phase :math:`\phi_r`
hbar (float): (default 2) the value of :math:`\hbar` in the commutation
relation :math:`[\x,\p]=i\hbar`
Returns:
array: the squeezed coherent state
"""
alpha = a * cmath.exp(1j * phi_a)
means = np.array([alpha.real, alpha.imag]) * math.sqrt(2 * hbar)
state = [means, squeezed_cov(r, phi_r, hbar)]
return state
示例13: arrow
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import exp [as 别名]
def arrow(tft, origin, vec, lc, color):
ccw = cmath.exp(3j * cmath.pi/4) # Unit vectors
cw = cmath.exp(-3j * cmath.pi/4)
length, theta = cmath.polar(vec)
uv = cmath.rect(1, theta) # Unit rotation vector
start = -vec
if length > 3 * lc: # If line is long
ds = cmath.rect(lc, theta)
start += ds # shorten to allow for length of tail chevrons
chev = lc + 0j
pline(tft, origin, vec, color) # Origin to tip
pline(tft, origin, start, color) # Origin to tail
pline(tft, origin + conj(vec), chev*ccw*uv, color) # Tip chevron
pline(tft, origin + conj(vec), chev*cw*uv, color)
if length > lc: # Confusing appearance of very short vectors with tail chevron
pline(tft, origin + conj(start), chev*ccw*uv, color) # Tail chevron
pline(tft, origin + conj(start), chev*cw*uv, color)
# Vector display
示例14: test_tauegamma_implementation
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import exp [as 别名]
def test_tauegamma_implementation(self):
input_dict_list=[{
'Cgamma_mue':np.random.random()*1e-8*exp(1j*2*pi*np.random.random()),
'Cgamma_emu':np.random.random()*1e-8*exp(1j*2*pi*np.random.random()),
} for i in range(10)]
BRs = np.array([
flavio.np_prediction(
'BR(mu->egamma)',
Wilson(input_dict, 100, 'WET', 'flavio')
)
for input_dict in input_dict_list
])
compare_BRs = np.array([
compare_BR(
Wilson(input_dict, 100, 'WET', 'flavio'),
'mu', 'e',
)
for input_dict in input_dict_list
])
self.assertAlmostEqual(np.max(np.abs(1-BRs/compare_BRs)), 0, delta=0.005)
示例15: test_taumugamma_implementation
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import exp [as 别名]
def test_taumugamma_implementation(self):
input_dict_list=[{
'Cgamma_taumu':np.random.random()*1e-8*exp(1j*2*pi*np.random.random()),
'Cgamma_mutau':np.random.random()*1e-8*exp(1j*2*pi*np.random.random()),
} for i in range(10)]
BRs = np.array([
flavio.np_prediction(
'BR(tau->mugamma)',
Wilson(input_dict, 100, 'WET', 'flavio')
)
for input_dict in input_dict_list
])
compare_BRs = np.array([
compare_BR(
Wilson(input_dict, 100, 'WET', 'flavio'),
'tau', 'mu',
)
for input_dict in input_dict_list
])
self.assertAlmostEqual(np.max(np.abs(1-BRs/compare_BRs)), 0, delta=0.02)