def test_mppovm_expectation_pmps(nr_sites, width, local_dim, rank, rgen):
paulis = povm.pauli_povm(local_dim)
mppaulis = povm.MPPovm.from_local_povm(paulis, width)
pmps = factory.random_mpa(nr_sites, (local_dim, local_dim), rank,
dtype=np.complex_, randstate=rgen)
rho = mpsmpo.pmps_to_mpo(pmps)
expect_psi = list(mppaulis.expectations(pmps, mode='pmps'))
expect_rho = list(mppaulis.expectations(rho))
assert len(expect_psi) == len(expect_rho)
for e_rho, e_psi in zip(expect_rho, expect_psi):
assert_array_almost_equal(e_rho.to_array(), e_psi.to_array())
python类complex_()的实例源码
def test_mppovm_pmf_as_array_pmps_benchmark(
nr_sites, local_dim, rank, startsite, width, impl, rgen, benchmark):
pauli_y = povm.pauli_parts(local_dim)[1]
mpp_y = povm.MPPovm.from_local_povm(pauli_y, width) \
.embed(nr_sites, startsite, local_dim)
pmps = factory.random_mpa(nr_sites, (local_dim, local_dim), rank,
dtype=np.complex_, randstate=rgen, normalized=True)
benchmark(lambda: mpp_y.pmf_as_array(pmps, 'pmps', impl=impl))
def test_eig_benchmark(
nr_sites, local_dim, rank, ev_rank, rgen, benchmark):
mpo = factory.random_mpo(nr_sites, local_dim, rank, randstate=rgen,
hermitian=True, normalized=True)
mpo.canonicalize()
mps = factory.random_mpa(nr_sites, local_dim, rank, randstate=rgen,
dtype=np.complex_, normalized=True)
mpo = mpo + mp.mps_to_mpo(mps)
benchmark(
mp.eig,
mpo, startvec_rank=ev_rank, randstate=rgen,
var_sites=1, num_sweeps=1,
)
def test_eig_sum_benchmark(
nr_sites, local_dim, rank, ev_rank, rgen, benchmark):
mpo = factory.random_mpo(nr_sites, local_dim, rank, randstate=rgen,
hermitian=True, normalized=True)
mpo.canonicalize()
mps = factory.random_mpa(nr_sites, local_dim, rank, randstate=rgen,
dtype=np.complex_, normalized=True)
benchmark(
mp.eig_sum,
[mpo, mps], startvec_rank=ev_rank, randstate=rgen,
var_sites=1, num_sweeps=1,
)
def pytest_namespace():
return dict(
# nr_sites, local_dim, rank
MP_TEST_PARAMETERS=[(1, 7, np.nan), (2, 3, 3), (3, 2, 4), (6, 2, 4),
(4, 3, 5), (5, 2, 1)],
MP_TEST_DTYPES=[np.float_, np.complex_]
)
def test_pmps_dm_to_array(nr_sites, local_dim, rank, rgen):
pmps = factory.random_mpa(nr_sites, (local_dim, local_dim), rank,
randstate=rgen, dtype=np.complex_)
mpo = mm.pmps_to_mpo(pmps)
op = mpo.to_array()
op2 = mm.pmps_dm_to_array(pmps)
assert_array_almost_equal(op2, op)
op = mpo.to_array_global()
op2 = mm.pmps_dm_to_array(pmps, True)
assert_array_almost_equal(op2, op)
def test_pmps_dm_to_array_fast(nr_sites, local_dim, rank, rgen, benchmark):
pmps = factory.random_mpa(nr_sites, (local_dim, local_dim), rank,
dtype=np.complex_, normalized=True,
randstate=rgen)
benchmark(mm.pmps_dm_to_array, pmps)
def test_pmps_reduction(nr_sites, local_dim, rank, keep, rgen):
pmps = factory.random_mpa(nr_sites, (local_dim, local_dim), rank,
dtype=np.complex_, normalized=True,
randstate=rgen)
rho = mm.pmps_to_mpo(pmps).to_array_global()
traceout = [pos for pos in range(nr_sites) if pos not in keep]
red = utils.partial_trace(rho, traceout)
pmps_red = mm.pmps_reduction(pmps, keep)
red2 = mm.pmps_to_mpo(pmps_red).to_array_global()
red2 = red2.reshape([local_dim] * (2 * len(keep)))
assert_array_almost_equal(red2, red)
def test_pmps_reduction_array_fast(nr_sites, local_dim, rank, keep, rgen,
benchmark):
pmps = factory.random_mpa(nr_sites, (local_dim, local_dim), rank,
dtype=np.complex_, normalized=True,
randstate=rgen)
benchmark(lambda: mm.pmps_dm_to_array(mm.pmps_reduction(pmps, keep)))
def test_pmps_reduction_array_slow_noprune(
nr_sites, local_dim, rank, keep, rgen, benchmark):
pmps = factory.random_mpa(nr_sites, (local_dim, local_dim), rank,
dtype=np.complex_, normalized=True,
randstate=rgen)
# NB: The maximal distance between sites of the reduction is
# limited by the fact that normal numpy builds support arrays with
# at most 32 indices.
benchmark(lambda: mm.pmps_to_mpo(mm.pmps_reduction(pmps, keep)).to_array())
def test_pmps_reduction_array_slow_prune(
nr_sites, local_dim, rank, keep, rgen, benchmark):
pmps = factory.random_mpa(nr_sites, (local_dim, local_dim), rank,
dtype=np.complex_, normalized=True,
randstate=rgen)
benchmark(
lambda: mp.prune(mm.pmps_to_mpo(mm.pmps_reduction(pmps, keep)),
singletons=True).to_array()
)
def test_pmps_reduction_dm_to_array(nr_sites, local_dim, rank, keep, rgen):
pmps = factory.random_mpa(nr_sites, (local_dim, local_dim), rank,
dtype=np.complex_, randstate=rgen)
rho = mm.pmps_to_mpo(pmps).to_array_global()
traceout = [pos for pos in range(nr_sites) if pos not in keep]
red = utils.partial_trace(rho, traceout)
pmps_red = mm.pmps_reduction(pmps, keep)
red2 = mm.pmps_dm_to_array(pmps_red, True)
assert_array_almost_equal(red2, red)
def test_power_complex(self):
x = np.array([1+2j, 2+3j, 3+4j])
assert_equal(x**0, [1., 1., 1.])
assert_equal(x**1, x)
assert_almost_equal(x**2, [-3+4j, -5+12j, -7+24j])
assert_almost_equal(x**3, [(1+2j)**3, (2+3j)**3, (3+4j)**3])
assert_almost_equal(x**4, [(1+2j)**4, (2+3j)**4, (3+4j)**4])
assert_almost_equal(x**(-1), [1/(1+2j), 1/(2+3j), 1/(3+4j)])
assert_almost_equal(x**(-2), [1/(1+2j)**2, 1/(2+3j)**2, 1/(3+4j)**2])
assert_almost_equal(x**(-3), [(-11+2j)/125, (-46-9j)/2197,
(-117-44j)/15625])
assert_almost_equal(x**(0.5), [ncu.sqrt(1+2j), ncu.sqrt(2+3j),
ncu.sqrt(3+4j)])
norm = 1./((x**14)[0])
assert_almost_equal(x**14 * norm,
[i * norm for i in [-76443+16124j, 23161315+58317492j,
5583548873 + 2465133864j]])
# Ticket #836
def assert_complex_equal(x, y):
assert_array_equal(x.real, y.real)
assert_array_equal(x.imag, y.imag)
for z in [complex(0, np.inf), complex(1, np.inf)]:
z = np.array([z], dtype=np.complex_)
with np.errstate(invalid="ignore"):
assert_complex_equal(z**1, z)
assert_complex_equal(z**2, z*z)
assert_complex_equal(z**3, z*z*z)
def test_loss_of_precision(self):
for dtype in [np.complex64, np.complex_]:
yield self.check_loss_of_precision, dtype
def test_return_dtype(self):
assert_equal(select(self.conditions, self.choices, 1j).dtype,
np.complex_)
# But the conditions need to be stronger then the scalar default
# if it is scalar.
choices = [choice.astype(np.int8) for choice in self.choices]
assert_equal(select(self.conditions, choices).dtype, np.int8)
d = np.array([1, 2, 3, np.nan, 5, 7])
m = np.isnan(d)
assert_equal(select([m], [d]), [0, 0, 0, np.nan, 0, 0])
def test_power_complex(self):
x = np.array([1+2j, 2+3j, 3+4j])
assert_equal(x**0, [1., 1., 1.])
assert_equal(x**1, x)
assert_almost_equal(x**2, [-3+4j, -5+12j, -7+24j])
assert_almost_equal(x**3, [(1+2j)**3, (2+3j)**3, (3+4j)**3])
assert_almost_equal(x**4, [(1+2j)**4, (2+3j)**4, (3+4j)**4])
assert_almost_equal(x**(-1), [1/(1+2j), 1/(2+3j), 1/(3+4j)])
assert_almost_equal(x**(-2), [1/(1+2j)**2, 1/(2+3j)**2, 1/(3+4j)**2])
assert_almost_equal(x**(-3), [(-11+2j)/125, (-46-9j)/2197,
(-117-44j)/15625])
assert_almost_equal(x**(0.5), [ncu.sqrt(1+2j), ncu.sqrt(2+3j),
ncu.sqrt(3+4j)])
norm = 1./((x**14)[0])
assert_almost_equal(x**14 * norm,
[i * norm for i in [-76443+16124j, 23161315+58317492j,
5583548873 + 2465133864j]])
# Ticket #836
def assert_complex_equal(x, y):
assert_array_equal(x.real, y.real)
assert_array_equal(x.imag, y.imag)
for z in [complex(0, np.inf), complex(1, np.inf)]:
z = np.array([z], dtype=np.complex_)
with np.errstate(invalid="ignore"):
assert_complex_equal(z**1, z)
assert_complex_equal(z**2, z*z)
assert_complex_equal(z**3, z*z*z)
def test_loss_of_precision(self):
for dtype in [np.complex64, np.complex_]:
yield self.check_loss_of_precision, dtype
def test_return_dtype(self):
assert_equal(select(self.conditions, self.choices, 1j).dtype,
np.complex_)
# But the conditions need to be stronger then the scalar default
# if it is scalar.
choices = [choice.astype(np.int8) for choice in self.choices]
assert_equal(select(self.conditions, choices).dtype, np.int8)
d = np.array([1, 2, 3, np.nan, 5, 7])
m = np.isnan(d)
assert_equal(select([m], [d]), [0, 0, 0, np.nan, 0, 0])
test_umath.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def test_power_complex(self):
x = np.array([1+2j, 2+3j, 3+4j])
assert_equal(x**0, [1., 1., 1.])
assert_equal(x**1, x)
assert_almost_equal(x**2, [-3+4j, -5+12j, -7+24j])
assert_almost_equal(x**3, [(1+2j)**3, (2+3j)**3, (3+4j)**3])
assert_almost_equal(x**4, [(1+2j)**4, (2+3j)**4, (3+4j)**4])
assert_almost_equal(x**(-1), [1/(1+2j), 1/(2+3j), 1/(3+4j)])
assert_almost_equal(x**(-2), [1/(1+2j)**2, 1/(2+3j)**2, 1/(3+4j)**2])
assert_almost_equal(x**(-3), [(-11+2j)/125, (-46-9j)/2197,
(-117-44j)/15625])
assert_almost_equal(x**(0.5), [ncu.sqrt(1+2j), ncu.sqrt(2+3j),
ncu.sqrt(3+4j)])
norm = 1./((x**14)[0])
assert_almost_equal(x**14 * norm,
[i * norm for i in [-76443+16124j, 23161315+58317492j,
5583548873 + 2465133864j]])
# Ticket #836
def assert_complex_equal(x, y):
assert_array_equal(x.real, y.real)
assert_array_equal(x.imag, y.imag)
for z in [complex(0, np.inf), complex(1, np.inf)]:
z = np.array([z], dtype=np.complex_)
with np.errstate(invalid="ignore"):
assert_complex_equal(z**1, z)
assert_complex_equal(z**2, z*z)
assert_complex_equal(z**3, z*z*z)
test_umath.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def test_loss_of_precision(self):
for dtype in [np.complex64, np.complex_]:
yield self.check_loss_of_precision, dtype