示例#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_moments_with_shots() -> None:
    b = BraketBackend(local=True)
    c = Circuit(1).H(0)
    z = QubitPauliString(Qubit(0), Pauli.Z)
    e = b.get_pauli_expectation_value(c, z, n_shots=10)
    assert abs(e) <= 1
    v = b.get_pauli_variance(c, z, n_shots=10)
    assert v <= 1
    op = QubitPauliOperator({z: 3})
    e = b.get_operator_expectation_value(c, op, n_shots=10)
    assert abs(e) <= 3
    v = b.get_operator_variance(c, op, n_shots=10)
    assert v <= 9
示例#3
0
def test_variance() -> None:
    b = BraketBackend(local=True)
    assert b.supports_variance
    # - Prepare a state (1/sqrt(2), 1/sqrt(2)).
    # - Measure w.r.t. the operator Z which has evcs (1,0) (evl=+1) and (0,1) (evl=-1).
    # - Get +1 with prob. 1/2 and -1 with prob. 1/2.
    c = Circuit(1).H(0)
    z = QubitPauliString(Qubit(0), Pauli.Z)
    assert b.get_pauli_expectation_value(c, z) == pytest.approx(0)
    assert b.get_pauli_variance(c, z) == pytest.approx(1)
    op = QubitPauliOperator({z: 3})
    assert b.get_operator_expectation_value(c, op) == pytest.approx(0)
    assert b.get_operator_variance(c, op) == pytest.approx(9)