本文整理汇总了Python中numpy.cosh方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.cosh方法的具体用法?Python numpy.cosh怎么用?Python numpy.cosh使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.cosh方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: psi2c2c3
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cosh [as 别名]
def psi2c2c3(self, psi0):
c2 = np.zeros(len(psi0))
c3 = np.zeros(len(psi0))
psi12 = np.sqrt(np.abs(psi0))
pos = psi0 >= 0
neg = psi0 < 0
if np.any(pos):
c2[pos] = (1 - np.cos(psi12[pos]))/psi0[pos]
c3[pos] = (psi12[pos] - np.sin(psi12[pos]))/psi12[pos]**3.
if any(neg):
c2[neg] = (1 - np.cosh(psi12[neg]))/psi0[neg]
c3[neg] = (np.sinh(psi12[neg]) - psi12[neg])/psi12[neg]**3.
tmp = c2+c3 == 0
if any(tmp):
c2[tmp] = 1./2.
c3[tmp] = 1./6.
return c2,c3
示例2: c2c3
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cosh [as 别名]
def c2c3(psi): # Stumpff functions definitions
c2, c3 = 0, 0
if np.any(psi > 1e-6):
c2 = (1 - np.cos(np.sqrt(psi))) / psi
c3 = (np.sqrt(psi) - np.sin(np.sqrt(psi))) / np.sqrt(psi ** 3)
if np.any(psi < -1e-6):
c2 = (1 - np.cosh(np.sqrt(-psi))) / psi
c3 = (np.sinh(np.sqrt(-psi)) - np.sqrt(-psi)) / np.sqrt(-psi ** 3)
if np.any(abs(psi) <= 1e-6):
c2 = 0.5
c3 = 1. / 6.
return c2, c3
示例3: test_swish_grad
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cosh [as 别名]
def test_swish_grad(self):
def swish_grad(x, beta):
return (
beta * (2 - beta * x * np.tanh(beta * x / 2)) / (1 + np.cosh(beta * x))
)
x = testing_utils.generate_real_values_with_zeros(shape=(8, 3, 3, 16))
tf_x = tf.Variable(x)
with tf.GradientTape() as tape:
activation = lq.quantizers.SwishSign()(tf_x)
grad = tape.gradient(activation, tf_x)
np.testing.assert_allclose(grad.numpy(), swish_grad(x, beta=5.0))
with tf.GradientTape() as tape:
activation = lq.quantizers.SwishSign(beta=10.0)(tf_x)
grad = tape.gradient(activation, tf_x)
np.testing.assert_allclose(grad.numpy(), swish_grad(x, beta=10.0))
示例4: fields
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cosh [as 别名]
def fields(x,y,z, kx, ky, kz, B0):
k1 = -B0*kx/ky
k2 = -B0*kz/ky
kx_x = kx*x
ky_y = ky*y
kz_z = kz*z
cosx = np.cos(kx_x)
sinhy = np.sinh(ky_y)
cosz = np.cos(kz_z)
Bx = k1*np.sin(kx_x)*sinhy*cosz #// here kx is only real
By = B0*cosx*np.cosh(ky_y)*cosz
Bz = k2*cosx*sinhy*np.sin(kz_z)
#Bx = ne.evaluate("k1*sin(kx*x)*sinhy*cosz")
#By = ne.evaluate("B0*cosx*cosh(ky*y)*cosz")
#Bz = ne.evaluate("k2*cosx*sinhy*sin(kz*z)")
return Bx, By, Bz
示例5: test_CXgate_decomp_equal
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cosh [as 别名]
def test_CXgate_decomp_equal(self, setup_eng, s, tol):
"""Tests that the CXgate gives the same transformation as its decomposition."""
eng, prog = setup_eng(2)
r = np.arcsinh(-s / 2)
y = -1 / np.cosh(r)
x = -np.tanh(r)
theta = np.arctan2(y, x) / 2
with prog.context as q:
ops.CXgate(s) | q
# run decomposition with reversed arguments
ops.BSgate(-(np.pi / 2 + theta), 0) | q
ops.Sgate(r, np.pi) | q[0]
ops.Sgate(r, 0) | q[1]
ops.BSgate(-theta, 0) | q
eng.run(prog)
assert np.all(eng.backend.is_vacuum(tol))
示例6: TMS
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cosh [as 别名]
def TMS(r, phi):
"""Two-mode squeezing.
Args:
r (float): squeezing magnitude
phi (float): rotation parameter
Returns:
array: symplectic transformation matrix
"""
cp = np.cos(phi)
sp = np.sin(phi)
ch = np.cosh(r)
sh = np.sinh(r)
S = np.array(
[
[ch, cp * sh, 0, sp * sh],
[cp * sh, ch, sp * sh, 0],
[0, sp * sh, ch, -cp * sh],
[sp * sh, 0, -cp * sh, ch],
]
)
return S
示例7: _squeezing
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cosh [as 别名]
def _squeezing(r, phi, mode, num_modes):
"""Squeezing in the phase space.
Args:
r (float): squeezing magnitude
phi (float): rotation parameter
mode (int): mode it is applied to
num_modes (int): total number of modes in the system
Returns:
array: symplectic transformation matrix
"""
cp = np.cos(phi)
sp = np.sin(phi)
ch = np.cosh(r)
sh = np.sinh(r)
S = np.array([[ch - cp * sh, -sp * sh], [-sp * sh, ch + cp * sh]])
return expand(S, mode, num_modes)
示例8: test_squeezed_state_gaussian
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cosh [as 别名]
def test_squeezed_state_gaussian(self, r, phi, hbar, tol):
"""test squeezed state returns correct means and covariance"""
means, cov = utils.squeezed_state(r, phi, basis="gaussian", hbar=hbar)
cov_expected = (hbar / 2) * np.array(
[
[
np.cosh(2 * r) - np.cos(phi) * np.sinh(2 * r),
-2 * np.cosh(r) * np.sin(phi) * np.sinh(r),
],
[
-2 * np.cosh(r) * np.sin(phi) * np.sinh(r),
np.cosh(2 * r) + np.cos(phi) * np.sinh(2 * r),
],
]
)
assert np.all(means == np.zeros([2]))
assert np.allclose(cov, cov_expected, atol=tol, rtol=0)
示例9: test_displaced_squeezed_state_gaussian
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cosh [as 别名]
def test_displaced_squeezed_state_gaussian(self, r_d, phi_d, r_s, phi_s, hbar, tol):
"""test displaced squeezed state returns correct means and covariance"""
means, cov = utils.displaced_squeezed_state(r_d, phi_d, r_s, phi_s, basis="gaussian", hbar=hbar)
a = r_d * np.exp(1j * phi_d)
means_expected = np.array([[a.real, a.imag]]) * np.sqrt(2 * hbar)
cov_expected = (hbar / 2) * np.array(
[
[
np.cosh(2 * r_s) - np.cos(phi_s) * np.sinh(2 * r_s),
-2 * np.cosh(r_s) * np.sin(phi_s) * np.sinh(r_s),
],
[
-2 * np.cosh(r_s) * np.sin(phi_s) * np.sinh(r_s),
np.cosh(2 * r_s) + np.cos(phi_s) * np.sinh(2 * r_s),
],
]
)
assert np.allclose(means, means_expected, atol=tol, rtol=0)
assert np.allclose(cov, cov_expected, atol=tol, rtol=0)
示例10: test_displaced_squeezed_state_fock
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cosh [as 别名]
def test_displaced_squeezed_state_fock(self, r_d, phi_d, r_s, phi_s, hbar, cutoff, tol):
"""test displaced squeezed state returns correct Fock basis state vector"""
state = utils.displaced_squeezed_state(r_d, phi_d, r_s, phi_s, basis="fock", fock_dim=cutoff, hbar=hbar)
a = r_d * np.exp(1j * phi_d)
if r_s == 0:
pytest.skip("test only non-zero squeezing")
n = np.arange(cutoff)
gamma = a * np.cosh(r_s) + np.conj(a) * np.exp(1j * phi_s) * np.sinh(r_s)
coeff = np.diag(
(0.5 * np.exp(1j * phi_s) * np.tanh(r_s)) ** (n / 2) / np.sqrt(fac(n) * np.cosh(r_s))
)
expected = H(gamma / np.sqrt(np.exp(1j * phi_s) * np.sinh(2 * r_s)), coeff)
expected *= np.exp(
-0.5 * np.abs(a) ** 2 - 0.5 * np.conj(a) ** 2 * np.exp(1j * phi_s) * np.tanh(r_s)
)
assert np.allclose(state, expected, atol=tol, rtol=0)
示例11: test_squeezed_coherent
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cosh [as 别名]
def test_squeezed_coherent(setup_backend, hbar, tol):
"""Test Wigner function for a squeezed coherent state
matches the analytic result"""
backend = setup_backend(1)
backend.prepare_coherent_state(np.abs(A), np.angle(A), 0)
backend.squeeze(R, PHI, 0)
state = backend.state()
W = state.wigner(0, XVEC, XVEC)
rot = rotm(PHI / 2)
# exact wigner function
alpha = A * np.cosh(R) - np.conjugate(A) * np.exp(1j * PHI) * np.sinh(R)
mu = np.array([alpha.real, alpha.imag]) * np.sqrt(2 * hbar)
cov = np.diag([np.exp(-2 * R), np.exp(2 * R)])
cov = np.dot(rot, np.dot(cov, rot.T)) * hbar / 2.0
Wexact = wigner(GRID, mu, cov)
assert np.allclose(W, Wexact, atol=0.01, rtol=0)
示例12: test_two_mode_squeezing
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cosh [as 别名]
def test_two_mode_squeezing(self, setup_backend, r, p, cutoff, pure, tol):
r""" Test two-mode squeezing on vacuum-state for both pure states and
mixed states with the amplitude given by
:math:`\delta_{kl} \frac{e^{in\phi} \tanh^n{r}}{\cosh{r}}`
"""
backend = setup_backend(2)
backend.two_mode_squeeze(r, p, 0, 1)
state = backend.state()
if pure:
for k in it.product(range(cutoff), repeat=2):
tmsv = get_amplitude(k, r, p)
assert np.allclose(state.data[k], tmsv, atol=tol, rtol=0)
else:
for k in it.product(range(cutoff), repeat=2):
for l in it.product(range(cutoff), repeat=2):
t = (k[0], l[0], k[1], l[1])
tmsv2 = get_amplitude(k, r, p) * np.conj(get_amplitude(l, r, p))
assert np.allclose(state.data[t], tmsv2, atol=tol, rtol=0)
示例13: test_squeezed_coherent
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cosh [as 别名]
def test_squeezed_coherent(self, setup_backend, hbar, batch_size, tol):
"""Test squeezed coherent state has correct mean and variance"""
# quadrature rotation angle
backend = setup_backend(1)
qphi = 0.78
backend.prepare_displaced_squeezed_state(np.abs(a), np.angle(a), r, phi, 0)
state = backend.state()
res = np.array(state.quad_expectation(0, phi=qphi)).T
xphi_mean = (a.real * np.cos(qphi) + a.imag * np.sin(qphi)) * np.sqrt(2 * hbar)
xphi_var = (np.cosh(2 * r) - np.cos(phi - 2 * qphi) * np.sinh(2 * r)) * hbar / 2
res_exact = np.array([xphi_mean, xphi_var])
if batch_size is not None:
res_exact = np.tile(res_exact, batch_size)
assert np.allclose(res.flatten(), res_exact.flatten(), atol=tol, rtol=0)
示例14: testAcoshFunction
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cosh [as 别名]
def testAcoshFunction(self):
ma5 = MovingAverage(5, 'close')
holder = Acosh(ma5)
sampleClose = np.cosh(self.sampleClose)
for i, close in enumerate(sampleClose):
data = {'close': close}
ma5.push(data)
holder.push(data)
expected = math.acosh(ma5.result())
calculated = holder.result()
self.assertAlmostEqual(calculated, expected, 12, "at index {0:d}\n"
"expected: {1:f}\n"
"calculated: {2:f}".format(i, expected, calculated))
示例15: cheb1ap
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cosh [as 别名]
def cheb1ap(N, rp):
"""Return (z,p,k) zero, pole, gain for Nth order Chebyshev type I lowpass
analog filter prototype with `rp` decibels of ripple in the passband.
The filter's angular (e.g. rad/s) cutoff frequency is normalized to 1,
defined as the point at which the gain first drops below -`rp`.
"""
z = numpy.array([])
eps = numpy.sqrt(10 ** (0.1 * rp) - 1.0)
n = numpy.arange(1, N + 1)
mu = 1.0 / N * numpy.log((1.0 + numpy.sqrt(1 + eps * eps)) / eps)
theta = pi / 2.0 * (2 * n - 1.0) / N
p = (-numpy.sinh(mu) * numpy.sin(theta) +
1j * numpy.cosh(mu) * numpy.cos(theta))
k = numpy.prod(-p, axis=0).real
if N % 2 == 0:
k = k / sqrt((1 + eps * eps))
return z, p, k