Ejemplo n.º 1
0
    def test_evo_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 = Evolution(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
Ejemplo n.º 2
0
 def test_expm_krylov_expokit(self):
     ham = rand_herm(100, sparse=True, density=0.8)
     psi = rand_ket(100)
     evo_exact = Evolution(psi, ham, method='solve')
     evo_krylov = Evolution(psi,
                            ham,
                            method='expm',
                            expm_backend='slepc-krylov')
     evo_expokit = Evolution(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
Ejemplo n.º 3
0
 def test_progbar_at_times_expm(self, capsys):
     ham = ham_heis(2, cyclic=False)
     p0 = up() & down()
     sim = Evolution(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
Ejemplo n.º 4
0
 def test_evo_at_times(self):
     ham = ham_heis(2, cyclic=False)
     p0 = up() & down()
     sim = Evolution(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, ikron(pauli('z'), [2, 2], 0))
         assert_allclose(x, y, atol=1e-15)
Ejemplo n.º 5
0
    def test_evo_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 = Evolution(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