def test_reductions_pmps(nr_sites, local_dim, rank, max_red_width, rgen): pmps = factory.random_mpa(nr_sites, (local_dim, local_dim), rank, dtype=np.complex_, randstate=rgen) op = mm.pmps_to_mpo(pmps).to_array_global() start, stop, red = _get_reductions(mm.reductions_pmps, pmps, max_red_width) for start, stop, reduced_pmps in zip(start, stop, red): # Check that startsites/stopsites and width produce the same result: reduced_pmps2 = tuple(mm.reductions_pmps(pmps, stop - start))[start] # NB: reduced_pmps and reduced_pmps2 are in general not equal, # but red and red2 are. red = mm.pmps_to_mpo(reduced_pmps).to_array_global() red2 = mm.pmps_to_mpo(reduced_pmps2).to_array_global() assert_array_almost_equal(red, red2) traceout = tuple(range(start)) + tuple(range(stop, nr_sites)) red_from_op = utils.partial_trace(op, traceout) assert_array_almost_equal(red, red_from_op, err_msg="not equal at {}:{}".format( start, stop)) # check default argument for startsite assert len(list(mm.reductions_pmps(pmps, max_red_width))) \ == nr_sites - max_red_width + 1
def test_mpo_to_pmps(nr_sites, local_dim, rgen): pmps = factory.random_mpa(nr_sites, (local_dim, local_dim), 1, dtype=np.complex_, randstate=rgen) assert_almost_equal(mp.normdist(mm.mpo_to_pmps(mm.pmps_to_mpo(pmps)), pmps), 0) mpo = factory.random_mpa(nr_sites, (local_dim, local_dim), 1, dtype=np.complex_, randstate=rgen, normalized=True) assert_almost_equal(mp.normdist(mm.pmps_to_mpo(mm.mpo_to_pmps(mpo)), mpo), 0)
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_dm_to_array_slow(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(lambda x: mm.pmps_to_mpo(pmps).to_array(), pmps)
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_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_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_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())
def test_reductions_pmps(nr_sites, local_dim, rank, max_red_width, rgen): pmps = factory.random_mpa(nr_sites, (local_dim, local_dim), rank, dtype=np.complex_, randstate=rgen) op = mm.pmps_to_mpo(pmps).to_array_global() start, stop, red = _get_reductions(mm.reductions_pmps, pmps, max_red_width) for start, stop, reduced_pmps in zip(start, stop, red): # Check that startsites/stopsites and width produce the same result: reduced_pmps2 = tuple(mm.reductions_pmps(pmps, stop - start))[start] # NB: reduced_pmps and reduced_pmps2 are in general not equal, # but red and red2 are. red = mm.pmps_to_mpo(reduced_pmps).to_array_global() red2 = mm.pmps_to_mpo(reduced_pmps2).to_array_global() assert_array_almost_equal(red, red2) traceout = tuple(range(start)) + tuple(range(stop, nr_sites)) red_from_op = utils.partial_trace(op, traceout) assert_array_almost_equal( red, red_from_op, err_msg="not equal at {}:{}".format(start, stop)) # check default argument for startsite assert len(list(mm.reductions_pmps(pmps, max_red_width))) \ == nr_sites - max_red_width + 1
def test_pmps_to_mpo(nr_sites, local_dim, rank, rgen): if (nr_sites % 2) != 0: return nr_sites = nr_sites // 2 pmps = factory.random_mpa(nr_sites, (local_dim, local_dim), rank, dtype=np.complex_, randstate=rgen) rho_mp = mm.pmps_to_mpo(pmps).to_array_global() # Local form is what we will use: One system site, one ancilla site, etc purification = pmps.to_array() # Convert to a density matrix purification = np.outer(purification, purification.conj()) purification = purification.reshape((local_dim,) * (2 * 2 * nr_sites)) # Trace out the ancilla sites traceout = tuple(range(1, 2 * nr_sites, 2)) rho_np = utils.partial_trace(purification, traceout) # Here, we need global form assert_array_almost_equal(rho_mp, rho_np)
def test_mppovm_pmf_as_array_pmps( nr_sites, local_dim, rank, startsite, width, rgen): if hasattr(local_dim, '__len__'): pdims = [d for d, _ in local_dim] mppaulis = mp.chain( povm.MPPovm.from_local_povm(povm.pauli_povm(d), 1) for d in pdims[startsite:startsite + width] ) else: pdims = local_dim local_dim = (local_dim, local_dim) mppaulis = povm.MPPovm.from_local_povm(povm.pauli_povm(pdims), width) mppaulis = mppaulis.embed(nr_sites, startsite, pdims) pmps = factory.random_mpa(nr_sites, local_dim, rank, dtype=np.complex_, randstate=rgen, normalized=True) rho = mpsmpo.pmps_to_mpo(pmps) expect_rho = mppaulis.pmf_as_array(rho, 'mpdo') for impl in ['default', 'pmps-ltr', 'pmps-symm']: expect_pmps = mppaulis.pmf_as_array(pmps, 'pmps', impl=impl) assert_array_almost_equal(expect_rho, expect_pmps, err_msg=impl)