예제 #1
0
def no_result_types_testing(circuit: Circuit, device: Device,
                            run_kwargs: Dict[str, Any], expected: Dict[str,
                                                                       float]):
    shots = run_kwargs["shots"]
    tol = get_tol(shots)
    result = device.run(circuit, **run_kwargs).result()
    probabilities = result.measurement_probabilities
    for bitstring in probabilities:
        assert np.allclose(probabilities[bitstring], expected[bitstring],
                           **tol)
    assert len(result.measurements) == shots
예제 #2
0
def result_types_noncommuting_flipped_targets_testing(device: Device, run_kwargs: Dict[str, Any]):
    circuit = (
        Circuit()
        .h(0)
        .cnot(0, 1)
        .expectation(observable=Observable.H() @ Observable.X(), target=[0, 1])
        .expectation(observable=Observable.H() @ Observable.X(), target=[1, 0])
    )
    result = device.run(circuit, shots=0, **run_kwargs).result()
    assert np.allclose(result.values[0], np.sqrt(2) / 2)
    assert np.allclose(result.values[1], np.sqrt(2) / 2)
예제 #3
0
def noisy_circuit_1qubit_noise_full_probability(device: Device, run_kwargs: Dict[str, Any]):
    shots = run_kwargs["shots"]
    tol = get_tol(shots)
    circuit = Circuit().x(0).x(1).bit_flip(0, 0.1).probability()
    result = device.run(circuit, **run_kwargs).result()
    assert len(result.result_types) == 1
    assert np.allclose(
        result.get_value_by_result_type(ResultType.Probability()),
        np.array([0.0, 0.1, 0, 0.9]),
        **tol
    )
예제 #4
0
def result_types_bell_pair_marginal_probability_testing(device: Device, run_kwargs: Dict[str, Any]):
    shots = run_kwargs["shots"]
    tol = get_tol(shots)
    circuit = Circuit().h(0).cnot(0, 1).probability(0)
    result = device.run(circuit, **run_kwargs).result()
    assert len(result.result_types) == 1
    assert np.allclose(
        result.get_value_by_result_type(ResultType.Probability(target=0)),
        np.array([0.5, 0.5]),
        **tol
    )
예제 #5
0
def result_types_noncommuting_all(device: Device, run_kwargs: Dict[str, Any]):
    array = np.array([[1, 2j], [-2j, 0]])
    circuit = (
        Circuit()
        .h(0)
        .cnot(0, 1)
        .expectation(observable=Observable.Hermitian(array))
        .expectation(observable=Observable.X())
    )
    result = device.run(circuit, shots=0, **run_kwargs).result()
    assert np.allclose(result.values[0], [0.5, 0.5])
    assert np.allclose(result.values[1], [0, 0])
예제 #6
0
def result_types_observable_not_in_instructions(device: Device,
                                                run_kwargs: Dict[str, Any]):
    shots = run_kwargs["shots"]
    tol = get_tol(shots)
    bell = (Circuit().h(0).cnot(0,
                                1).expectation(observable=Observable.X(),
                                               target=[2]).variance(
                                                   observable=Observable.Y(),
                                                   target=[3]))
    result = device.run(bell, **run_kwargs).result()
    assert np.allclose(result.values[0], 0, **tol)
    assert np.allclose(result.values[1], 1, **tol)
def result_types_bell_pair_full_probability_testing(device: Device,
                                                    run_kwargs: Dict[str,
                                                                     Any]):
    shots = run_kwargs["shots"]
    tol = get_tol(shots)
    circuit = Circuit().h(0).cnot(0, 1).probability()
    tasks = (circuit, circuit.to_ir(ir_type=IRType.OPENQASM))
    for task in tasks:
        result = device.run(task, **run_kwargs).result()
        assert len(result.result_types) == 1
        assert np.allclose(
            result.get_value_by_result_type(ResultType.Probability()),
            np.array([0.5, 0, 0, 0.5]), **tol)
예제 #8
0
def noisy_circuit_2qubit_noise_full_probability(device: Device,
                                                run_kwargs: Dict[str, Any]):
    shots = run_kwargs["shots"]
    tol = get_tol(shots)
    K0 = np.eye(4) * np.sqrt(0.9)
    K1 = np.kron(np.array([[0.0, 1.0], [1.0, 0.0]]),
                 np.array([[0.0, 1.0], [1.0, 0.0]])) * np.sqrt(0.1)
    circuit = Circuit().x(0).x(1).kraus((0, 1), [K0, K1]).probability()
    result = device.run(circuit, **run_kwargs).result()
    assert len(result.result_types) == 1
    assert np.allclose(
        result.get_value_by_result_type(ResultType.Probability()),
        np.array([0.1, 0.0, 0, 0.9]), **tol)
def result_types_nonzero_shots_bell_pair_testing(device: Device,
                                                 run_kwargs: Dict[str, Any]):
    circuit = (Circuit().h(0).cnot(0, 1).expectation(
        observable=Observable.H() @ Observable.X(),
        target=[0, 1]).sample(observable=Observable.H() @ Observable.X(),
                              target=[0, 1]))
    result = device.run(circuit, **run_kwargs).result()
    assert len(result.result_types) == 2
    assert (0.6 < result.get_value_by_result_type(
        ResultType.Expectation(observable=Observable.H() @ Observable.X(),
                               target=[0, 1])) < 0.8)
    assert (len(
        result.get_value_by_result_type(
            ResultType.Sample(observable=Observable.H() @ Observable.X(),
                              target=[0, 1]))) == run_kwargs["shots"])
예제 #10
0
def result_types_tensor_z_z_testing(device: Device, run_kwargs: Dict[str, Any]):
    shots = run_kwargs["shots"]
    theta = 0.432
    phi = 0.123
    varphi = -0.543
    obs = Observable.Z() @ Observable.Z()
    obs_targets = [0, 2]
    circuit = get_result_types_three_qubit_circuit(theta, phi, varphi, obs, obs_targets, shots)
    result = device.run(circuit, **run_kwargs).result()

    expected_mean = 0.849694136476246
    expected_var = 0.27801987443788634
    expected_eigs = get_pauli_eigenvalues(1)

    assert_variance_expectation_sample_result(
        result, shots, expected_var, expected_mean, expected_eigs
    )
def result_types_tensor_z_hermitian_testing(device: Device,
                                            run_kwargs: Dict[str, Any]):
    shots = run_kwargs["shots"]
    theta = 0.432
    phi = 0.123
    varphi = -0.543
    array = np.array([
        [-6, 2 + 1j, -3, -5 + 2j],
        [2 - 1j, 0, 2 - 1j, -5 + 4j],
        [-3, 2 + 1j, 0, -4 + 3j],
        [-5 - 2j, -5 - 4j, -4 - 3j, -6],
    ])
    obs = Observable.Z() @ Observable.Hermitian(array)
    obs_targets = [0, 1, 2]
    circuit = get_result_types_three_qubit_circuit(theta, phi, varphi, obs,
                                                   obs_targets, shots)
    tasks = (circuit, circuit.to_ir(ir_type=IRType.OPENQASM))
    for task in tasks:
        result = device.run(task, **run_kwargs).result()

        expected_mean = 0.5 * (
            -6 * np.cos(theta) * (np.cos(varphi) + 1) - 2 * np.sin(varphi) *
            (np.cos(theta) + np.sin(phi) - 2 * np.cos(phi)) +
            3 * np.cos(varphi) * np.sin(phi) + np.sin(phi))
        expected_var = (
            1057 - np.cos(2 * phi) + 12 *
            (27 + np.cos(2 * phi)) * np.cos(varphi) -
            2 * np.cos(2 * varphi) * np.sin(phi) *
            (16 * np.cos(phi) + 21 * np.sin(phi)) + 16 * np.sin(2 * phi) - 8 *
            (-17 + np.cos(2 * phi) + 2 * np.sin(2 * phi)) * np.sin(varphi) -
            8 * np.cos(2 * theta) *
            (3 + 3 * np.cos(varphi) + np.sin(varphi))**2 - 24 * np.cos(phi) *
            (np.cos(phi) + 2 * np.sin(phi)) * np.sin(2 * varphi) -
            8 * np.cos(theta) *
            (4 * np.cos(phi) *
             (4 + 8 * np.cos(varphi) + np.cos(2 * varphi) -
              (1 + 6 * np.cos(varphi)) * np.sin(varphi)) + np.sin(phi) *
             (15 + 8 * np.cos(varphi) - 11 * np.cos(2 * varphi) +
              42 * np.sin(varphi) + 3 * np.sin(2 * varphi)))) / 16

        z_array = np.diag([1, -1])
        expected_eigs = np.linalg.eigvalsh(np.kron(z_array, array))
        assert_variance_expectation_sample_result(result, shots, expected_var,
                                                  expected_mean, expected_eigs)
def result_types_hermitian_testing(device: Device, run_kwargs: Dict[str, Any]):
    shots = run_kwargs["shots"]
    theta = 0.543
    array = np.array([[1, 2j], [-2j, 0]])

    circuit = (Circuit().rx(0, theta).variance(Observable.Hermitian(array),
                                               0).expectation(
                                                   Observable.Hermitian(array),
                                                   0))
    if shots:
        circuit.add_result_type(
            ResultType.Sample(Observable.Hermitian(array), 0))
    result = device.run(circuit, **run_kwargs).result()

    expected_mean = 2 * np.sin(theta) + 0.5 * np.cos(theta) + 0.5
    expected_var = 0.25 * (np.sin(theta) - 4 * np.cos(theta))**2
    expected_eigs = np.linalg.eigvalsh(array)
    assert_variance_expectation_sample_result(result, shots, expected_var,
                                              expected_mean, expected_eigs)
예제 #13
0
def result_types_noncommuting_testing(device: Device, run_kwargs: Dict[str, Any]):
    shots = 0
    theta = 0.432
    phi = 0.123
    varphi = -0.543
    array = np.array(
        [
            [-6, 2 + 1j, -3, -5 + 2j],
            [2 - 1j, 0, 2 - 1j, -5 + 4j],
            [-3, 2 + 1j, 0, -4 + 3j],
            [-5 - 2j, -5 - 4j, -4 - 3j, -6],
        ]
    )
    obs1 = Observable.X() @ Observable.Y()
    obs1_targets = [0, 2]
    obs2 = Observable.Z() @ Observable.Z()
    obs2_targets = [0, 2]
    obs3 = Observable.Y() @ Observable.Hermitian(array)
    obs3_targets = [0, 1, 2]
    circuit = (
        get_result_types_three_qubit_circuit(theta, phi, varphi, obs1, obs1_targets, shots)
        .expectation(obs2, obs2_targets)
        .expectation(obs3, obs3_targets)
    )
    result = device.run(circuit, **run_kwargs).result()

    expected_mean1 = np.sin(theta) * np.sin(phi) * np.sin(varphi)
    expected_var1 = (
        8 * np.sin(theta) ** 2 * np.cos(2 * varphi) * np.sin(phi) ** 2
        - np.cos(2 * (theta - phi))
        - np.cos(2 * (theta + phi))
        + 2 * np.cos(2 * theta)
        + 2 * np.cos(2 * phi)
        + 14
    ) / 16

    expected_mean2 = 0.849694136476246
    expected_mean3 = 1.4499810303182408
    assert np.allclose(result.values[0], expected_var1)
    assert np.allclose(result.values[1], expected_mean1)
    assert np.allclose(result.values[2], expected_mean2)
    assert np.allclose(result.values[3], expected_mean3)
def result_types_tensor_x_y_testing(device: Device, run_kwargs: Dict[str,
                                                                     Any]):
    shots = run_kwargs["shots"]
    theta = 0.432
    phi = 0.123
    varphi = -0.543
    obs = Observable.X() @ Observable.Y()
    obs_targets = [0, 2]
    circuit = get_result_types_three_qubit_circuit(theta, phi, varphi, obs,
                                                   obs_targets, shots)
    result = device.run(circuit, **run_kwargs).result()

    expected_mean = np.sin(theta) * np.sin(phi) * np.sin(varphi)
    expected_var = (8 * np.sin(theta)**2 * np.cos(2 * varphi) * np.sin(phi)**2
                    - np.cos(2 * (theta - phi)) - np.cos(2 * (theta + phi)) +
                    2 * np.cos(2 * theta) + 2 * np.cos(2 * phi) + 14) / 16
    expected_eigs = get_pauli_eigenvalues(1)

    assert_variance_expectation_sample_result(result, shots, expected_var,
                                              expected_mean, expected_eigs)
def openqasm_noisy_circuit_1qubit_noise_full_probability(
        device: Device, run_kwargs: Dict[str, Any]):
    shots = run_kwargs["shots"]
    tol = get_tol(shots)
    openqasm_string = ("OPENQASM 3;"
                       "qubit[2] q;"
                       "x q[0];"
                       "x q[1];"
                       "#pragma braket noise bit_flip(0.1) q[0]"
                       "#pragma braket result probability q[0], q[1]")
    hardcoded_openqasm = OpenQasmProgram(source=openqasm_string)
    circuit = Circuit().x(0).x(1).bit_flip(0, 0.1).probability([0, 1])
    generated_openqasm = circuit.to_ir(ir_type=IRType.OPENQASM)

    for program in hardcoded_openqasm, generated_openqasm:
        result = device.run(program, **run_kwargs).result()
        assert len(result.result_types) == 1
        assert np.allclose(
            result.get_value_by_result_type(
                ResultType.Probability(target=[0, 1])),
            np.array([0.0, 0.1, 0, 0.9]), **tol)
def result_types_tensor_z_h_y_testing(device: Device, run_kwargs: Dict[str,
                                                                       Any]):
    shots = run_kwargs["shots"]
    theta = 0.432
    phi = 0.123
    varphi = -0.543
    obs = Observable.Z() @ Observable.H() @ Observable.Y()
    obs_targets = [0, 1, 2]
    circuit = get_result_types_three_qubit_circuit(theta, phi, varphi, obs,
                                                   obs_targets, shots)
    tasks = (circuit, circuit.to_ir(ir_type=IRType.OPENQASM))
    for task in tasks:
        result = device.run(task, **run_kwargs).result()

        expected_mean = -(np.cos(varphi) * np.sin(phi) +
                          np.sin(varphi) * np.cos(theta)) / np.sqrt(2)
        expected_var = (3 + np.cos(2 * phi) * np.cos(varphi)**2 -
                        np.cos(2 * theta) * np.sin(varphi)**2 - 2 *
                        np.cos(theta) * np.sin(phi) * np.sin(2 * varphi)) / 4
        expected_eigs = get_pauli_eigenvalues(1)
        assert_variance_expectation_sample_result(result, shots, expected_var,
                                                  expected_mean, expected_eigs)
예제 #17
0
def result_types_zero_shots_bell_pair_testing(
    device: Device,
    include_state_vector: bool,
    run_kwargs: Dict[str, Any],
    include_amplitude: bool = True,
):
    circuit = (
        Circuit()
        .h(0)
        .cnot(0, 1)
        .expectation(observable=Observable.H() @ Observable.X(), target=[0, 1])
    )
    if include_amplitude:
        circuit.amplitude(["01", "10", "00", "11"])
    if include_state_vector:
        circuit.state_vector()
    result = device.run(circuit, **run_kwargs).result()
    assert len(result.result_types) == 3 if include_state_vector else 2
    assert np.allclose(
        result.get_value_by_result_type(
            ResultType.Expectation(observable=Observable.H() @ Observable.X(), target=[0, 1])
        ),
        1 / np.sqrt(2),
    )
    if include_state_vector:
        assert np.allclose(
            result.get_value_by_result_type(ResultType.StateVector()),
            np.array([1, 0, 0, 1]) / np.sqrt(2),
        )
    if include_amplitude:
        assert result.get_value_by_result_type(ResultType.Amplitude(["01", "10", "00", "11"])) == {
            "01": 0j,
            "10": 0j,
            "00": (1 / np.sqrt(2)),
            "11": (1 / np.sqrt(2)),
        }
def openqasm_result_types_bell_pair_testing(device: Device,
                                            run_kwargs: Dict[str, Any]):
    openqasm_string = ("OPENQASM 3;"
                       "qubit[2] q;"
                       "h q[0];"
                       "cnot q[0], q[1];"
                       "#pragma braket result expectation h(q[0]) @ x(q[1])"
                       "#pragma braket result sample h(q[0]) @ x(q[1])")
    hardcoded_openqasm = OpenQasmProgram(source=openqasm_string)
    circuit = (Circuit().h(0).cnot(0, 1).expectation(
        Observable.H() @ Observable.X(),
        (0, 1)).sample(Observable.H() @ Observable.X(), (0, 1)))
    generated_openqasm = circuit.to_ir(ir_type=IRType.OPENQASM)

    for program in hardcoded_openqasm, generated_openqasm:
        result = device.run(program, **run_kwargs).result()
        assert len(result.result_types) == 2
        assert (0.6 < result.get_value_by_result_type(
            ResultType.Expectation(observable=Observable.H() @ Observable.X(),
                                   target=[0, 1])) < 0.8)
        assert (len(
            result.get_value_by_result_type(
                ResultType.Sample(observable=Observable.H() @ Observable.X(),
                                  target=[0, 1]))) == run_kwargs["shots"])