Пример #1
0
    def test_quevo_multi_compute(self, method, qtype):

        ham = ham_heis(2, cyclic=False)
        p0 = qu(up() & down(), qtype=qtype)

        def some_quantity(t, _):
            return t

        def some_other_quantity(_, pt):
            return logneg(pt)

        evo = QuEvo(p0,
                    ham,
                    method=method,
                    compute={
                        't': some_quantity,
                        'logneg': some_other_quantity
                    })
        manual_lns = []
        for pt in evo.at_times(np.linspace(0, 1, 6)):
            manual_lns.append(logneg(pt))
        ts = evo.results['t']
        lns = evo.results['logneg']
        assert len(lns) >= len(manual_lns)
        # check a specific value of logneg at t=0.8 was computed automatically
        checked = False
        for t, ln in zip(ts, lns):
            if abs(t - 0.8) < 1e-12:
                assert abs(ln - manual_lns[4]) < 1e-12
                checked = True
        assert checked
Пример #2
0
 def test_progbar_update_to_integrate(self, capsys):
     ham = ham_heis(2, cyclic=False)
     p0 = up() & down()
     sim = QuEvo(p0, ham, method='integrate', progbar=True)
     sim.update_to(100)
     # check something as been printed
     _, err = capsys.readouterr()
     assert err and "%" in err
Пример #3
0
 def test_progbar_at_times_expm(self, capsys):
     ham = ham_heis(2, cyclic=False)
     p0 = up() & down()
     sim = QuEvo(p0, ham, method='expm', progbar=True)
     for _ in sim.at_times(np.linspace(0, 100, 11)):
         pass
     # check something as been printed
     _, err = capsys.readouterr()
     assert err and "%" in err
Пример #4
0
 def test_quevo_at_times(self):
     ham = ham_heis(2, cyclic=False)
     p0 = up() & down()
     sim = QuEvo(p0, ham, method='solve')
     ts = np.linspace(0, 10)
     for t, pt in zip(ts, sim.at_times(ts)):
         x = cos(t)
         y = expec(pt, eyepad(pauli('z'), [2, 2], 0))
         assert_allclose(x, y, atol=1e-15)
Пример #5
0
    def test_quevo_compute_callback(self, qtype, method):
        ham = ham_heis(2, cyclic=False)
        p0 = qu(up() & down(), qtype=qtype)

        def some_quantity(t, pt):
            return t, logneg(pt)

        evo = QuEvo(p0, ham, method=method, compute=some_quantity)
        manual_lns = []
        for pt in evo.at_times(np.linspace(0, 1, 6)):
            manual_lns.append(logneg(pt))
        ts, lns = zip(*evo.results)
        assert len(lns) >= len(manual_lns)
        # check a specific value of logneg at t=0.8 was computed automatically
        checked = False
        for t, ln in zip(ts, lns):
            if abs(t - 0.8) < 1e-12:
                assert abs(ln - manual_lns[4]) < 1e-12
                checked = True
        assert checked
Пример #6
0
 def test_expm_krylov_expokit(self):
     ham = rand_herm(100, sparse=True, density=0.8)
     psi = rand_ket(100)
     evo_exact = QuEvo(psi, ham, method='solve')
     evo_krylov = QuEvo(psi,
                        ham,
                        method='expm',
                        expm_backend='slepc-krylov')
     evo_expokit = QuEvo(psi,
                         ham,
                         method='expm',
                         expm_backend='slepc-expokit')
     ts = np.linspace(0, 100, 21)
     for p1, p2, p3 in zip(evo_exact.at_times(ts), evo_krylov.at_times(ts),
                           evo_expokit.at_times(ts)):
         assert abs(expec(p1, p2) - 1) < 1e-9
         assert abs(expec(p1, p3) - 1) < 1e-9
Пример #7
0
 def test_quevo_ham_dense_ket_solve(self, ham_rcr_psi, sparse, presolve):
     ham, trc, p0, tm, pm = ham_rcr_psi
     ham = qu(ham, sparse=sparse)
     if presolve:
         l, v = eigsys(ham)
         sim = QuEvo(p0, (l, v))
         assert sim._solved
     else:
         sim = QuEvo(p0, ham, method='solve')
     sim.update_to(tm)
     assert_allclose(sim.pt, pm)
     assert expec(sim.pt, p0) < 1.0
     sim.update_to(trc)
     assert_allclose(sim.pt, p0)
     assert isinstance(sim.pt, np.matrix)
     assert sim.t == trc
Пример #8
0
    def test_quevo_ham(self, ham_rcr_psi, sparse, dop, method):
        ham, trc, p0, tm, pm = ham_rcr_psi
        if dop:
            if method == 'expm':
                # XXX: not implemented
                return
            p0 = p0 @ p0.H
            pm = pm @ pm.H

        if method == 'bad':
            with raises(ValueError):
                QuEvo(p0, ham, method=method)
            return

        ham = qu(ham, sparse=sparse)
        sim = QuEvo(p0, ham, method=method)
        sim.update_to(tm)
        assert_allclose(sim.pt, pm, rtol=1e-4)
        assert expec(sim.pt, p0) < 1.0
        sim.update_to(trc)
        assert_allclose(sim.pt, p0, rtol=1e-4)
        assert isinstance(sim.pt, np.matrix)
        assert sim.t == trc