def result_types_zero_shots_bell_pair_testing(device: Device, include_state_vector: bool, run_kwargs: Dict[str, Any]): circuit = (Circuit().h(0).cnot( 0, 1).expectation(observable=Observable.H() @ Observable.X(), target=[0, 1]).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), ) 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 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"] )
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() 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) == 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: amplitude = result.get_value_by_result_type( ResultType.Amplitude(["01", "10", "00", "11"])) assert np.isclose(amplitude["01"], 0) assert np.isclose(amplitude["10"], 0) assert np.isclose(amplitude["00"], 1 / np.sqrt(2)) assert np.isclose(amplitude["11"], 1 / np.sqrt(2))
def test_flattened_tensor_product(): observable_one = Observable.Z() @ Observable.Y() observable_two = Observable.X() @ Observable.H() actual = Observable.TensorProduct([observable_one, observable_two]) expected = Observable.TensorProduct( [Observable.Z(), Observable.Y(), Observable.X(), Observable.H()] ) assert expected == actual
def test_basis_rotation_instructions_multiple_result_types_tensor_product_probability( ): circ = (Circuit().h(0).cnot(0, 1).cnot(1, 2).probability([0, 1]).sample( observable=Observable.Z() @ Observable.Z() @ Observable.H(), target=[0, 1, 2]).variance(observable=Observable.H(), target=[2])) expected = [ Instruction(Gate.Ry(-np.pi / 4), 2), ] assert circ.basis_rotation_instructions == expected
def test_basis_rotation_instructions_multiple_result_types_same_targets(): circ = (Circuit().h(0).cnot(0, 1).expectation( observable=Observable.H() @ Observable.X(), target=[0, 1]).sample(observable=Observable.H() @ Observable.X(), target=[0, 1]).variance( observable=Observable.H() @ Observable.X(), target=[0, 1])) expected = [Instruction(Gate.Ry(-np.pi / 4), 0), Instruction(Gate.H(), 1)] assert circ.basis_rotation_instructions == expected
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)
def test_basis_rotation_instructions_multiple_result_types_tensor_product_hermitian_qubit_count_2( ): circ = (Circuit().h(0).cnot(0, 1).cnot(1, 2).expectation( observable=Observable.I(), target=[1]).sample( observable=Observable.Hermitian(matrix=np.eye(4)) @ Observable.H(), target=[0, 1, 2]).variance(observable=Observable.H(), target=[2]).expectation( observable=Observable.I(), target=[0])) expected = [ Instruction(Gate.Unitary(matrix=np.eye(4)), target=[0, 1]), Instruction(Gate.Ry(-np.pi / 4), 2), ] assert circ.basis_rotation_instructions == expected
def test_hermitian_equality(): matrix = Observable.H().to_matrix() a1 = Observable.Hermitian(matrix=matrix) a2 = Observable.Hermitian(matrix=matrix) a3 = Observable.Hermitian(matrix=Observable.I().to_matrix()) a4 = "hi" assert a1 == a2 assert a1 != a3 assert a1 != a4
def test_basis_rotation_instructions_multiple_result_types_tensor_product_hermitian( ): circ = (Circuit().h(0).cnot(0, 1).cnot(1, 2).sample( observable=Observable.Hermitian(matrix=np.array([[1, 0], [0, -1]])) @ Observable.H(), target=[0, 1], ).variance( observable=Observable.Hermitian(matrix=np.array([[1, 0], [0, -1]])) @ Observable.H(), target=[0, 1], ).expectation( observable=Observable.Hermitian(matrix=np.array([[0, 1], [1, 0]])), target=[2])) expected = [ Instruction(Gate.Unitary(matrix=np.array([[0, 1], [1, 0]])), target=[0]), Instruction(Gate.Ry(-np.pi / 4), 1), Instruction( Gate.Unitary(matrix=1.0 / np.sqrt(2.0) * np.array([[1.0, 1.0], [1.0, -1.0]], dtype=complex)), target=[2], ), ] assert circ.basis_rotation_instructions == expected
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"])
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) result = device.run(circuit, **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)
def test_qubit_count_getter(h): assert h.qubit_count is h._moments.qubit_count @pytest.mark.xfail(raises=AttributeError) def test_qubit_count_setter(h): h.qubit_count = 1 @pytest.mark.parametrize( "circuit,expected_qubit_count", [ (Circuit().h(0).h(1).h(2), 3), ( Circuit().h(0).expectation( observable=Observable.H() @ Observable.X(), target=[0, 1]).sample( observable=Observable.H() @ Observable.X(), target=[0, 1]), 2, ), ( Circuit().h(0).probability([1, 2]).state_vector(), 1, ), ( Circuit().h(0).variance(observable=Observable.H(), target=1).state_vector().amplitude(["01"]), 2, ), ], )
from braket.circuits.observables import observable_from_ir from braket.circuits.quantum_operator_helpers import get_pauli_eigenvalues testdata = [ (Observable.I(), Gate.I(), ["i"], (), np.array([1, 1])), (Observable.X(), Gate.X(), ["x"], tuple([Gate.H()]), get_pauli_eigenvalues(1)), ( Observable.Y(), Gate.Y(), ["y"], tuple([Gate.Z(), Gate.S(), Gate.H()]), get_pauli_eigenvalues(1), ), (Observable.Z(), Gate.Z(), ["z"], (), get_pauli_eigenvalues(1)), (Observable.H(), Gate.H(), ["h"], tuple([Gate.Ry(-math.pi / 4)]), get_pauli_eigenvalues(1)), ] invalid_hermitian_matrices = [ (np.array([[1]])), (np.array([1])), (np.array([0, 1, 2])), (np.array([[0, 1], [1, 2], [3, 4]])), (np.array([[0, 1, 2], [2, 3]])), (np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])), (Gate.T().to_matrix()), ] @pytest.mark.parametrize(
from braket.circuits import Gate, Observable from braket.circuits.observables import observable_from_ir from braket.circuits.quantum_operator_helpers import get_pauli_eigenvalues testdata = [ (Observable.I(), Gate.I(), ["i"], (), np.array([1, 1])), (Observable.X(), Gate.X(), ["x"], tuple([Gate.H()]), get_pauli_eigenvalues(1)), ( Observable.Y(), Gate.Y(), ["y"], tuple([Gate.Z(), Gate.S(), Gate.H()]), get_pauli_eigenvalues(1), ), (Observable.Z(), Gate.Z(), ["z"], (), get_pauli_eigenvalues(1)), (Observable.H(), Gate.H(), ["h"], tuple([Gate.Ry(-math.pi / 4)]), get_pauli_eigenvalues(1)), ] invalid_hermitian_matrices = [ (np.array([[1]])), (np.array([1])), (np.array([0, 1, 2])), (np.array([[0, 1], [1, 2], [3, 4]])), (np.array([[0, 1, 2], [2, 3]])), (np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])), (Gate.T().to_matrix()), ] @pytest.mark.parametrize( "testobject,gateobject,expected_ir,basis_rotation_gates,eigenvalues", testdata