Ejemplo n.º 1
0
def test_get_state():
    q = Qubits(2)
    q.set_state('11')
    assert np.allclose(
        q.get_state(),
        np.array([0, 0, 0, 1])
    )
    q.set_state('01')
    assert np.allclose(
        q.get_state(flatten=False),
        np.array([[0, 1], [0, 0]])
    )
Ejemplo n.º 2
0
def test_expect():
    q = Qubits(2)
    res = q.expect(np.kron(I, I))
    assert res == 1

    q = Qubits(2)
    res = q.expect(np.kron(I, X))
    assert res == 0

    q = Qubits(2)
    res = q.expect({'YI': 2.5, 'II': 2, 'IX': 1.5, 'IZ': 1})
    assert res == 3

    q = Qubits(2)
    q.set_state('01')
    res = q.expect({'IZ': 2})
    assert res == -2

    q = Qubits(2)
    q.set_state('10')
    res = q.expect({'IZ': 2})
    assert res == 2

    q = Qubits(2)
    q.set_state('01')
    res = q.expect({'ZI': 2})
    assert res == 2

    q = Qubits(2)
    q.set_state('11')
    res = q.expect({'ZZ': 2})
    assert res == 2
Ejemplo n.º 3
0
def test_set_state():
    q = Qubits(2)
    q.set_state('10')
    assert q.data[0, 0] == 0
    assert q.data[0, 1] == 0
    assert q.data[1, 0] == 1
    assert q.data[1, 1] == 0
    assert np.allclose(
        q.data.flatten(),
        np.array([0, 0, 1, 0])
    )
    q.set_state([0, 1, 0, 0])
    assert q.data[0, 0] == 0
    assert q.data[0, 1] == 1
    assert q.data[1, 0] == 0
    assert q.data[1, 1] == 0
    assert np.allclose(
        q.data.flatten(),
        np.array([0, 1, 0, 0])
    )
    q.set_state([[0, 0], [0, 1]])
    assert q.data[0, 0] == 0
    assert q.data[0, 1] == 0
    assert q.data[1, 0] == 0
    assert q.data[1, 1] == 1
    assert np.allclose(
        q.data,
        np.array([[0, 0], [0, 1]])
    )