示例#1
0
def test_simulator() -> None:
    b = BraketBackend(
        s3_bucket=S3_BUCKET,
        s3_folder=S3_FOLDER,
        device_type="quantum-simulator",
        provider="amazon",
        device="sv1",
    )
    assert b.supports_shots
    c = Circuit(2).H(0).CX(0, 1)
    b.compile_circuit(c)
    n_shots = 100
    h0, h1 = b.process_circuits([c, c], n_shots)
    res0 = b.get_result(h0)
    readouts = res0.get_shots()
    assert all(readouts[i][0] == readouts[i][1] for i in range(n_shots))
    res1 = b.get_result(h1)
    counts = res1.get_counts()
    assert len(counts) <= 2
    assert sum(counts.values()) == n_shots
    zi = QubitPauliString(Qubit(0), Pauli.Z)
    assert b.get_pauli_expectation_value(
        c, zi, poll_timeout_seconds=60, poll_interval_seconds=1
    ) == pytest.approx(0)

    # Circuit with unused qubits
    c = Circuit(3).H(1).CX(1, 2)
    b.compile_circuit(c)
    h = b.process_circuit(c, 1)
    res = b.get_result(h)
    readout = res.get_shots()[0]
    assert readout[1] == readout[2]
示例#2
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)
示例#3
0
def test_state() -> None:
    b = BraketBackend(local=True)
    c = Circuit(3).V(0).V(1).CX(1, 0).S(1).CCX(0, 1, 2)
    b.compile_circuit(c)
    h = b.process_circuit(c)
    res = b.get_result(h)
    v = res.get_state()
    assert np.vdot(v, v) == pytest.approx(1)
示例#4
0
def test_local_simulator() -> None:
    b = BraketBackend(local=True)
    assert b.supports_shots
    assert b.supports_counts
    c = Circuit(2).H(0).CX(0, 1)
    b.compile_circuit(c)
    n_shots = 100
    h = b.process_circuit(c, n_shots)
    res = b.get_result(h)
    readouts = res.get_shots()
    assert all(readouts[i][0] == readouts[i][1] for i in range(n_shots))
    counts = res.get_counts()
    assert len(counts) <= 2
    assert sum(counts.values()) == n_shots
示例#5
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
示例#6
0
def test_shots_bits_edgecases(n_shots, n_bits) -> None:
    braket_backend = BraketBackend(local=True)
    c = Circuit(n_bits, n_bits)

    # TODO TKET-813 add more shot based backends and move to integration tests
    h = braket_backend.process_circuit(c, n_shots)
    res = braket_backend.get_result(h)

    correct_shots = np.zeros((n_shots, n_bits), dtype=int)
    correct_shape = (n_shots, n_bits)
    correct_counts = Counter({(0,) * n_bits: n_shots})
    # BackendResult/
    assert np.array_equal(res.get_shots(), correct_shots)
    assert res.get_shots().shape == correct_shape
    assert res.get_counts() == correct_counts

    # Direct
    assert np.array_equal(braket_backend.get_shots(c, n_shots), correct_shots)
    assert braket_backend.get_shots(c, n_shots).shape == correct_shape
    assert braket_backend.get_counts(c, n_shots) == correct_counts