コード例 #1
0
ファイル: test_hamiltonians.py プロジェクト: NourO93/qibo
def test_hamiltonian_matmul(numpy):
    """Test matrix multiplication between Hamiltonians and state vectors."""
    H1 = TFIM(nqubits=3, h=1.0, numpy=numpy)
    H2 = Y(nqubits=3, numpy=numpy)
    if numpy:
        m1 = H1.matrix
        m2 = H2.matrix
    else:
        m1 = H1.matrix.numpy()
        m2 = H2.matrix.numpy()

    np.testing.assert_allclose((H1 @ H2).matrix, m1 @ m2)
    np.testing.assert_allclose((H2 @ H1).matrix, m2 @ m1)

    v = utils.random_numpy_complex(8, dtype=m1.dtype)
    m = utils.random_numpy_complex((8, 8), dtype=m1.dtype)
    np.testing.assert_allclose(H1 @ v, m1.dot(v))
    np.testing.assert_allclose(H1 @ m, m1 @ m)

    from qibo.core.states import VectorState
    state = VectorState.from_tensor(v)
    np.testing.assert_allclose(H1 @ state, m1.dot(v))

    with pytest.raises(ValueError):
        H1 @ np.zeros((8, 8, 8), dtype=m1.dtype)
    with pytest.raises(NotImplementedError):
        H1 @ 2
コード例 #2
0
def test_right_operations(numpy):
    """Tests operations not covered by ``test_hamiltonian_overloading``."""
    H1 = Y(nqubits=3, numpy=numpy)
    H2 = 2 + H1
    target_matrix = 2 * np.eye(8) + H1.matrix
    np.testing.assert_allclose(H2.matrix, target_matrix)
    H2 = H1 - 2
    target_matrix = H1.matrix - 2 * np.eye(8)
    np.testing.assert_allclose(H2.matrix, target_matrix)
コード例 #3
0
def test_different_hamiltonian_addition(numpy):
    """Test adding Hamiltonians of different models."""
    H1 = Y(nqubits=3, numpy=numpy)
    H2 = TFIM(nqubits=3, h=1.0, numpy=numpy)
    H = H1 + H2
    matrix = H1.matrix + H2.matrix
    np.testing.assert_allclose(H.matrix, matrix)
    H = H1 - 0.5 * H2
    matrix = H1.matrix - 0.5 * H2.matrix
    np.testing.assert_allclose(H.matrix, matrix)
コード例 #4
0
ファイル: test_hamiltonians.py プロジェクト: tuliplan/qibo
def test_trotter_hamiltonian_make_compatible_simple():
    """Test ``make_compatible`` on a simple 3-qubit example."""
    h0target = X(3)
    h0 = X(3, trotter=True)
    term1 = Y(1, numpy=True)
    term2 = TFIM(2, numpy=True)
    parts = [{(0, 1): term2, (1, 2): term2, (0, 2): term2, (2, ): term1}]
    h1 = TrotterHamiltonian(*parts)

    h0c = h1.make_compatible(h0)
    assert not h1.is_compatible(h0)
    assert h1.is_compatible(h0c)
    np.testing.assert_allclose(h0c.matrix, h0target.matrix)