示例#1
0
def test_evolve_trotter_euclidean(num_sites, phys_dim, graph):
    psi = tf.complex(
        tf.random_normal([phys_dim] * num_sites, dtype=tf.float64),
        tf.random_normal([phys_dim] * num_sites, dtype=tf.float64))
    h = tf.complex(
        tf.random_normal((phys_dim**2, phys_dim**2), dtype=tf.float64),
        tf.random_normal((phys_dim**2, phys_dim**2), dtype=tf.float64))
    h = 0.5 * (h + tf.linalg.adjoint(h))
    h = tf.reshape(h, (phys_dim, phys_dim, phys_dim, phys_dim))
    H = [h] * (num_sites - 1)

    norm1 = wavefunctions.inner(psi, psi)
    en1 = sum(wavefunctions.expval(psi, H[i], i) for i in range(num_sites - 1))

    if graph:
        psi, t = wavefunctions.evolve_trotter_defun(psi,
                                                    H,
                                                    0.1,
                                                    10,
                                                    euclidean=True)
    else:
        psi, t = wavefunctions.evolve_trotter(psi, H, 0.1, 10, euclidean=True)

    norm2 = wavefunctions.inner(psi, psi)
    en2 = sum(wavefunctions.expval(psi, H[i], i) for i in range(num_sites - 1))

    np.testing.assert_allclose(t, 1.0)
    np.testing.assert_almost_equal(norm2, 1.0)
    assert en2.numpy() / norm2.numpy() < en1.numpy() / norm1.numpy()
def test_evolve_trotter(num_sites, phys_dim, graph):
    psi = tf.complex(
        tf.random.normal([phys_dim] * num_sites, dtype=tf.float64),
        tf.random.normal([phys_dim] * num_sites, dtype=tf.float64))
    h = tf.complex(
        tf.random.normal((phys_dim**2, phys_dim**2), dtype=tf.float64),
        tf.random.normal((phys_dim**2, phys_dim**2), dtype=tf.float64))
    h = 0.5 * (h + tf.linalg.adjoint(h))
    h = tf.reshape(h, (phys_dim, phys_dim, phys_dim, phys_dim))
    H = [h] * (num_sites - 1)

    norm1 = wavefunctions.inner(psi, psi)
    en1 = sum(wavefunctions.expval(psi, H[i], i) for i in range(num_sites - 1))

    if graph:
        psi, t = wavefunctions.evolve_trotter_defun(psi, H, 0.001, 10)
    else:
        psi, t = wavefunctions.evolve_trotter(psi, H, 0.001, 10)

    norm2 = wavefunctions.inner(psi, psi)
    en2 = sum(wavefunctions.expval(psi, H[i], i) for i in range(num_sites - 1))

    np.testing.assert_allclose(t, 0.01)
    np.testing.assert_almost_equal(norm1 / norm2, 1.0)
    np.testing.assert_almost_equal(en1 / en2, 1.0, decimal=2)
示例#3
0
def test_apply_op(num_sites):
    psi1 = np.zeros([2] * num_sites)
    psi1_vec = psi1.reshape((2**num_sites, ))
    psi1_vec[0] = 1.0
    psi1 = tf.convert_to_tensor(psi1)

    for j in range(num_sites):
        psi2 = np.zeros([2] * num_sites)
        psi2_vec = psi2.reshape((2**num_sites, ))
        psi2_vec[2**j] = 1.0
        psi2 = tf.convert_to_tensor(psi2)

        opX = tf.convert_to_tensor(np.array([[0.0, 1.0], [1.0, 0.0]]))
        psi2 = wavefunctions.apply_op(psi2, opX, num_sites - 1 - j)

        res = wavefunctions.inner(psi1, psi2)
        np.testing.assert_allclose(res, 1.0)
    euclidean_evolution = False

    print("----------------------------------------------------")
    print("Evolving a random state by the Ising Hamiltonian.")
    print("----------------------------------------------------")
    print("System size:", N)
    print("Trotter step size:", dt)
    print("Euclidean?:", euclidean_evolution)

    X = tf.convert_to_tensor([[0.0, 1.0], [1.0, 0.0]], dtype=dtype)
    H = ising_hamiltonian(N, dtype)
    psi = random_state(N, 2, dtype)

    if build_graph:
        f = wavefunctions.evolve_trotter_defun
    else:
        f = wavefunctions.evolve_trotter

    print("----------------------------------------------------")
    print("step\tnorm\t<X_0>")
    print("----------------------------------------------------")
    psi_t, t = f(psi,
                 H,
                 dt,
                 num_steps,
                 euclidean=euclidean_evolution,
                 callback=callback)

    print("Final norm:", tf.norm(psi_t).numpy().real)
    print("<psi | psi_t>:", wavefunctions.inner(psi, psi_t).numpy())
    dt = 0.1
    num_steps = 100
    euclidean_evolution = False

    print("----------------------------------------------------")
    print("Evolving a random state by the Ising Hamiltonian.")
    print("----------------------------------------------------")
    print("System size:", N)
    print("Trotter step size:", dt)
    print("Euclidean?:", euclidean_evolution)

    X = tf.convert_to_tensor([[0.0, 1.0], [1.0, 0.0]], dtype=dtype)
    H = ising_hamiltonian(N, dtype)
    psi = random_state(N, 2, dtype)

    if build_graph:
        f = wavefunctions.evolve_trotter_defun
    else:
        f = wavefunctions.evolve_trotter

    print("----------------------------------------------------")
    print("step\tnorm\t<X_0>")
    print("----------------------------------------------------")
    psi_t, t = f(
        psi, H, dt, num_steps,
        euclidean=euclidean_evolution,
        callback=callback)

    print("Final norm:", tf.norm(psi_t).numpy().real)
    print("<psi | psi_t>:", wavefunctions.inner(psi, psi_t).numpy())