def test_qaoa_callbacks(backend, accelerators):
    from qibo import callbacks
    # use ``Y`` Hamiltonian so that there are no errors
    # in the Trotter decomposition
    if accelerators:
        with K.on_cpu():
            h = hamiltonians.Y(5)
    else:
        h = hamiltonians.Y(5)
    energy = callbacks.Energy(h)
    params = 0.1 * np.random.random(4)
    state = random_state(5)

    ham = hamiltonians.Y(5, dense=False)
    qaoa = models.QAOA(ham, callbacks=[energy], accelerators=accelerators)
    qaoa.set_parameters(params)
    final_state = qaoa(np.copy(state))

    h_matrix = K.to_numpy(h.matrix)
    m_matrix = K.to_numpy(qaoa.mixer.matrix)
    calc_energy = lambda s: (s.conj() * h_matrix.dot(s)).sum()
    target_state = np.copy(state)
    target_energy = [calc_energy(target_state)]
    for i, p in enumerate(params):
        if i % 2:
            u = expm(-1j * p * m_matrix)
        else:
            u = expm(-1j * p * h_matrix)
        target_state = u @ target_state
        target_energy.append(calc_energy(target_state))
    K.assert_allclose(energy[:], target_energy)
Exemple #2
0
def test_qaoa_callbacks(accelerators):
    from qibo import callbacks
    # use ``Y`` Hamiltonian so that there are no errors
    # in the Trotter decomposition
    h = hamiltonians.Y(3)
    energy = callbacks.Energy(h)
    params = 0.1 * np.random.random(4)
    state = utils.random_numpy_state(3)

    ham = hamiltonians.Y(3, trotter=True)
    qaoa = models.QAOA(ham, callbacks=[energy], accelerators=accelerators)
    qaoa.set_parameters(params)
    final_state = qaoa(np.copy(state))

    h_matrix = h.matrix.numpy()
    m_matrix = qaoa.mixer.matrix.numpy()
    calc_energy = lambda s: (s.conj() * h_matrix.dot(s)).sum()
    target_state = np.copy(state)
    target_energy = [calc_energy(target_state)]
    for i, p in enumerate(params):
        if i % 2:
            u = expm(-1j * p * m_matrix)
        else:
            u = expm(-1j * p * h_matrix)
        target_state = u @ target_state
        target_energy.append(calc_energy(target_state))
    np.testing.assert_allclose(energy[:], target_energy)
def test_hamiltonian_matmul(backend, sparse_type):
    """Test matrix multiplication between Hamiltonians."""
    if sparse_type is None:
        nqubits = 3
        H1 = hamiltonians.TFIM(nqubits, h=1.0)
        H2 = hamiltonians.Y(nqubits)
    else:
        nqubits = 5
        nstates = 2 ** nqubits
        H1 = hamiltonians.Hamiltonian(nqubits, random_sparse_matrix(nstates, sparse_type))
        H2 = hamiltonians.Hamiltonian(nqubits, random_sparse_matrix(nstates, sparse_type))

    m1 = K.to_numpy(H1.matrix)
    m2 = K.to_numpy(H2.matrix)
    if K.name == "tensorflow" and sparse_type is not None:
        with pytest.raises(NotImplementedError):
            _ = H1 @ H2
    else:
        K.assert_allclose((H1 @ H2).matrix, m1 @ m2)
        K.assert_allclose((H2 @ H1).matrix, m2 @ m1)

    with pytest.raises(ValueError):
        H1 @ np.zeros(3 * (2 ** nqubits,), dtype=m1.dtype)
    with pytest.raises(NotImplementedError):
        H1 @ 2
Exemple #4
0
def test_hamiltonian_matmul():
    """Test matrix multiplication between Hamiltonians and state vectors."""
    H1 = hamiltonians.TFIM(nqubits=3, h=1.0)
    H2 = hamiltonians.Y(nqubits=3)
    m1 = K.to_numpy(H1.matrix)
    m2 = K.to_numpy(H2.matrix)

    K.assert_allclose((H1 @ H2).matrix, m1 @ m2)
    K.assert_allclose((H2 @ H1).matrix, m2 @ m1)

    v = random_complex(8, dtype=m1.dtype)
    m = random_complex((8, 8), dtype=m1.dtype)
    H1v = H1 @ K.cast(v)
    H1m = H1 @ K.cast(m)
    K.assert_allclose(H1v, m1.dot(v))
    K.assert_allclose(H1m, m1 @ m)

    from qibo.core.states import VectorState
    H1state = H1 @ VectorState.from_tensor(K.cast(v))
    K.assert_allclose(H1state, m1.dot(v))

    with pytest.raises(ValueError):
        H1 @ np.zeros((8, 8, 8), dtype=m1.dtype)
    with pytest.raises(NotImplementedError):
        H1 @ 2
Exemple #5
0
def test_hamiltonian_addition():
    H1 = hamiltonians.Y(nqubits=3)
    H2 = hamiltonians.TFIM(nqubits=3, h=1.0)
    H = H1 + H2
    matrix = H1.matrix + H2.matrix
    K.assert_allclose(H.matrix, matrix)
    H = H1 - 0.5 * H2
    matrix = H1.matrix - 0.5 * H2.matrix
    K.assert_allclose(H.matrix, matrix)

    H1 = hamiltonians.XXZ(nqubits=2, delta=0.5)
    H2 = hamiltonians.XXZ(nqubits=3, delta=0.1)
    with pytest.raises(RuntimeError):
        R = H1 + H2
    with pytest.raises(RuntimeError):
        R = H1 - H2
def test_hamiltonian_addition(sparse_type):
    if sparse_type is None:
        H1 = hamiltonians.Y(nqubits=3)
        H2 = hamiltonians.TFIM(nqubits=3, h=1.0)
    else:
        H1 = hamiltonians.Hamiltonian(6, sparse.rand(64, 64, format=sparse_type))
        H2 = hamiltonians.Hamiltonian(6, sparse.rand(64, 64, format=sparse_type))

    H = H1 + H2
    matrix = H1.matrix + H2.matrix
    K.assert_allclose(H.matrix, matrix)
    H = H1 - 0.5 * H2
    matrix = H1.matrix - 0.5 * H2.matrix
    K.assert_allclose(H.matrix, matrix)

    H1 = hamiltonians.XXZ(nqubits=2, delta=0.5)
    H2 = hamiltonians.XXZ(nqubits=3, delta=0.1)
    with pytest.raises(RuntimeError):
        R = H1 + H2
    with pytest.raises(RuntimeError):
        R = H1 - H2