Exemplo n.º 1
0
def test_energy():
    """Check energy callback for state vectors and density matrices."""
    from qibo import hamiltonians
    ham = hamiltonians.TFIM(4, h=1.0)
    energy = callbacks.Energy(ham)
    matrix = np.array(ham.matrix)

    state = np.random.random(16) + 1j * np.random.random(16)
    target_energy = state.conj().dot(matrix.dot(state))
    np.testing.assert_allclose(energy(state), target_energy)

    state = np.random.random((16, 16)) + 1j * np.random.random((16, 16))
    target_energy = np.trace(matrix.dot(state))
    np.testing.assert_allclose(energy(state, True), target_energy)
Exemplo n.º 2
0
def test_energy(backend, density_matrix):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    from qibo import hamiltonians
    ham = hamiltonians.TFIM(4, h=1.0)
    energy = callbacks.Energy(ham)
    matrix = np.array(ham.matrix)
    if density_matrix:
        energy.density_matrix = True
        state = np.random.random((16, 16)) + 1j * np.random.random((16, 16))
        target_energy = np.trace(matrix.dot(state))
    else:
        state = np.random.random(16) + 1j * np.random.random(16)
        target_energy = state.conj().dot(matrix.dot(state))
    np.testing.assert_allclose(energy(state), target_energy)
    qibo.set_backend(original_backend)
Exemplo n.º 3
0
def test_energy_callback(solver, atol, dt=1e-2):
    """Test using energy callback in adiabatic evolution."""
    h0 = hamiltonians.X(2)
    h1 = hamiltonians.TFIM(2)
    energy = callbacks.Energy(h1)
    adev = models.AdiabaticEvolution(h0,
                                     h1,
                                     lambda t: t,
                                     dt=dt,
                                     callbacks=[energy],
                                     solver=solver)
    final_psi = adev(final_time=1)

    target_psi = np.ones(4) / 2
    calc_energy = lambda psi: psi.conj().dot(h1.matrix.numpy().dot(psi))
    target_energies = [calc_energy(target_psi)]
    ham = lambda t: h0 * (1 - t) + h1 * t
    for n in range(int(1 / dt)):
        prop = ham(n * dt).exp(dt).numpy()
        target_psi = prop.dot(target_psi)
        target_energies.append(calc_energy(target_psi))

    assert_states_equal(final_psi, target_psi, atol=atol)
    np.testing.assert_allclose(energy[:], target_energies, atol=atol)