def test_mppovm_expectation(nr_sites, width, local_dim, rank, nopovm, rgen): # Verify that :func:`povm.MPPovm.expectations()` produces # correct results. pmap = nopovm.probability_map mpnopovm = povm.MPPovm.from_local_povm(nopovm, width) # Use a random MPO rho for testing (instead of a positive MPO). rho = factory.random_mpo(nr_sites, local_dim, rank, rgen) reductions = mpsmpo.reductions_mpo(rho, width) # Compute expectation values with mpnopovm.expectations(), which # uses mpnopovm.probability_map. expectations = list(mpnopovm.expectations(rho)) assert len(expectations) == nr_sites - width + 1 for evals_mp, rho_red in zip_longest(expectations, reductions): # Compute expectation values by constructing each tensor # product POVM element. rho_red_matrix = rho_red.to_array_global().reshape( (local_dim**width,) * 2) evals = [] for factors in it.product(nopovm, repeat=width): elem = utils.mkron(*factors) evals.append(np.trace(np.dot(elem, rho_red_matrix))) evals = np.array(evals).reshape((len(nopovm),) * width) # Compute expectation with a different construction. In the # end, this is (should be, we verify it here) equivalent to # what `mpnopovm.expectations()` does. evals_ten = rho_red.ravel().to_array() for _ in range(width): evals_ten = np.tensordot(evals_ten, pmap, axes=(0, 1)) assert_array_almost_equal(evals_ten, evals) assert_array_almost_equal(evals_mp.to_array(), evals)
def test_reductions_mps(nr_sites, local_dim, rank, width, rgen): mps = factory.random_mps(nr_sites, local_dim, rank, randstate=rgen) mpo = mp.localouter(mps, mps.conj()) pmps_reds = mm.reductions_mps_as_mpo(mps, width) mpo_reds = mm.reductions_mpo(mpo, width) for red1, red2 in zip(pmps_reds, mpo_reds): assert_array_almost_equal(red1.to_array(), red2.to_array())
def test_reductions_mpo(nr_sites, local_dim, rank, max_red_width, rgen): mpo = factory.random_mpo(nr_sites, local_dim, rank, randstate=rgen) op = mpo.to_array_global() start, stop, red = _get_reductions(mm.reductions_mpo, mpo, max_red_width) for start, stop, reduced_mpo in zip(start, stop, red): # Check that startsites/stopsites and width produce the same result: reduced_mpo2 = tuple(mm.reductions_mpo(mpo, stop - start))[start] assert_array_almost_equal(reduced_mpo.to_array(), reduced_mpo2.to_array()) traceout = tuple(range(start)) + tuple(range(stop, nr_sites)) red_from_op = utils.partial_trace(op, traceout) assert_array_almost_equal( reduced_mpo.to_array_global(), red_from_op, err_msg="not equal at {}:{}".format(start, stop)) # check default argument for startsite assert len(list(mm.reductions_mpo(mpo, max_red_width))) \ == nr_sites - max_red_width + 1