Exemplo n.º 1
0
def test_probabilities() -> None:
    b = BraketBackend(local=True)
    c = (
        Circuit(2)
        .H(0)
        .Rx(0.8, 1)
        .Rz(0.5, 0)
        .CX(0, 1)
        .Ry(0.3, 1)
        .CX(1, 0)
        .T(0)
        .S(1)
        .CX(0, 1)
        .Ry(1.8, 0)
    )
    probs01 = b.get_probabilities(c)
    probs10 = b.get_probabilities(c, qubits=[1, 0])
    probs0 = b.get_probabilities(c, qubits=[0])
    probs1 = b.get_probabilities(c, qubits=[1])
    assert probs01[0] == pytest.approx(probs10[0])
    assert probs01[1] == pytest.approx(probs10[2])
    assert probs01[2] == pytest.approx(probs10[1])
    assert probs01[3] == pytest.approx(probs10[3])
    assert probs0[0] == pytest.approx(probs01[0] + probs01[1])
    assert probs1[0] == pytest.approx(probs01[0] + probs01[2])
    h = b.process_circuit(c)
    res = b.get_result(h)
    dist = res.get_distribution()
    for (a0, a1), p in dist.items():
        assert probs01[2 * a0 + a1] == pytest.approx(p)
Exemplo n.º 2
0
def test_probabilities_with_shots() -> None:
    b = BraketBackend(local=True)
    c = Circuit(2).V(1).CX(1, 0).S(1)
    probs_all = b.get_probabilities(c, n_shots=10)
    assert len(probs_all) == 4
    assert sum(probs_all) == pytest.approx(1)
    assert probs_all[1] == 0
    assert probs_all[2] == 0
    probs1 = b.get_probabilities(c, n_shots=10, qubits=[1])
    assert len(probs1) == 2
    assert sum(probs1) == pytest.approx(1)
    h = b.process_circuit(c, n_shots=10)
    res = b.get_result(h)
    dist = res.get_distribution()
    assert (1, 0) not in dist
    assert (0, 1) not in dist