def test_add_result_type_observable_conflict_different_selected_targets_then_all_target( ): circ = Circuit().add_result_type( ResultType.Expectation(observable=Observable.Z(), target=[0])) circ.add_result_type( ResultType.Expectation(observable=Observable.Y(), target=[1])) circ.add_result_type(ResultType.Expectation(observable=Observable.Y()))
def test_add_result_type_observable_conflict_different_selected_targets_then_all_target( ): circ = Circuit().add_result_type( ResultType.Expectation(observable=Observable.Z(), target=[0])) circ.add_result_type( ResultType.Expectation(observable=Observable.Y(), target=[1])) circ.add_result_type(ResultType.Expectation(observable=Observable.Y())) assert not circ.observables_simultaneously_measurable assert not circ.basis_rotation_instructions
def test_add_result_type_same_observable_wrong_target_order_tensor_product(): Circuit().add_result_type( ResultType.Expectation( observable=Observable.Y() @ Observable.X(), target=[0, 1])).add_result_type( ResultType.Variance(observable=Observable.Y() @ Observable.X(), target=[1, 0]))
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_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_add_result_type_observable_no_conflict_target_all(): expected = [ ResultType.Expectation(observable=Observable.Z(), target=[0]), ResultType.Probability(), ] circ = Circuit(expected) assert circ.result_types == expected
def test_add_result_type_observable_no_conflict_all(): expected = [ ResultType.Variance(observable=Observable.Y()), ResultType.Expectation(observable=Observable.Y()), ] circ = Circuit(expected) assert circ.result_types == expected
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 test_add_result_type_observable_no_conflict_state_vector_obs_return_value( ): expected = [ ResultType.StateVector(), ResultType.Expectation(observable=Observable.Y()), ] circ = Circuit(expected) assert circ.result_types == expected
def test_add_result_type_same_observable_wrong_target_order_tensor_product(): circ = (Circuit().add_result_type( ResultType.Expectation( observable=Observable.Y() @ Observable.X(), target=[0, 1])).add_result_type( ResultType.Variance(observable=Observable.Y() @ Observable.X(), target=[1, 0]))) assert not circ.observables_simultaneously_measurable assert not circ.basis_rotation_instructions
def test_add_result_type_same_observable_wrong_target_order_hermitian(): array = np.eye(4) Circuit().add_result_type( ResultType.Expectation( observable=Observable.Hermitian(matrix=array), target=[0, 1])).add_result_type( ResultType.Variance( observable=Observable.Hermitian(matrix=array), target=[1, 0]))
def test_obs_rt_equality(): a1 = ObservableResultType(ascii_symbols=["Obs"], observable=Observable.X(), target=0) a2 = ObservableResultType(ascii_symbols=["Obs"], observable=Observable.X(), target=0) a3 = ObservableResultType(ascii_symbols=["Obs"], observable=Observable.X(), target=1) a4 = "hi" assert a1 == a2 assert a1 != a3 assert a1 != a4 assert ResultType.Variance(observable=Observable.Y(), target=0) != ResultType.Expectation( observable=Observable.Y(), target=0 )
def test_add_result_type_same_observable_wrong_target_order_hermitian(): array = np.eye(4) circ = (Circuit().add_result_type( ResultType.Expectation( observable=Observable.Hermitian(matrix=array), target=[0, 1])).add_result_type( ResultType.Variance( observable=Observable.Hermitian(matrix=array), target=[1, 0]))) assert not circ.observables_simultaneously_measurable assert not circ.basis_rotation_instructions
def test_get_value_by_result_type(result_obj_4): result = GateModelQuantumTaskResult.from_object(result_obj_4) assert np.allclose( result.get_value_by_result_type(ResultType.Probability(target=0)), result.values[0] ) assert np.allclose(result.get_value_by_result_type(ResultType.StateVector()), result.values[1]) assert ( result.get_value_by_result_type(ResultType.Expectation(observable=Observable.Y(), target=0)) == result.values[2] ) assert ( result.get_value_by_result_type(ResultType.Variance(observable=Observable.Y(), target=0)) == result.values[3] ) assert result.get_value_by_result_type(ResultType.Amplitude(state=["00"])) == result.values[4]
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"])
] @pytest.mark.parametrize("testclass,subroutine_name,irclass,input,ir_input", testdata) def test_ir_result_level(testclass, subroutine_name, irclass, input, ir_input): expected = irclass(**ir_input) actual = testclass(**input).to_ir() assert actual == expected @pytest.mark.parametrize( "result_type, serialization_properties, expected_ir", [ ( ResultType.Expectation(Observable.I(), target=0), OpenQASMSerializationProperties( qubit_reference_type=QubitReferenceType.VIRTUAL), "#pragma braket result expectation i(q[0])", ), ( ResultType.Expectation(Observable.I()), OpenQASMSerializationProperties( qubit_reference_type=QubitReferenceType.VIRTUAL), "#pragma braket result expectation i all", ), ( ResultType.StateVector(), OpenQASMSerializationProperties( qubit_reference_type=QubitReferenceType.VIRTUAL), "#pragma braket result state_vector",
def test_add_result_type_observable_conflict_selected_target_then_all_target(): circ = Circuit().add_result_type( ResultType.Expectation(observable=Observable.Y(), target=[0, 1])) circ.add_result_type(ResultType.Probability())
def test_add_result_type_observable_conflict_all(): circ = Circuit().add_result_type(ResultType.Probability()) circ.add_result_type(ResultType.Expectation(observable=Observable.Y()))
def test_add_result_type_observable_conflict_all(): circ = Circuit().add_result_type(ResultType.Probability()) circ.add_result_type(ResultType.Expectation(observable=Observable.Y())) assert not circ.observables_simultaneously_measurable assert not circ.basis_rotation_instructions
def test_expectation_parent_class(): assert isinstance( ResultType.Expectation(observable=Observable.X(), target=0), ObservableResultType)
0.15), gates.CPhaseShift(0.15).to_matrix()), (Circuit().cphaseshift00(1, 0, 0.15), gates.CPhaseShift00(0.15).to_matrix()), (Circuit().cphaseshift01(1, 0, 0.15), gates.CPhaseShift01(0.15).to_matrix()), (Circuit().cphaseshift10(1, 0, 0.15), gates.CPhaseShift10(0.15).to_matrix()), (Circuit().cy(1, 0), gates.CY().to_matrix()), (Circuit().cz(1, 0), gates.CZ().to_matrix()), (Circuit().xx(1, 0, 0.15), gates.XX(0.15).to_matrix()), (Circuit().yy(1, 0, 0.15), gates.YY(0.15).to_matrix()), (Circuit().zz(1, 0, 0.15), gates.ZZ(0.15).to_matrix()), (Circuit().ccnot(2, 1, 0), gates.CCNot().to_matrix()), ( Circuit().ccnot(2, 1, 0).add_result_type( ResultType.Expectation(observable=Observable.Y(), target=[1])), gates.CCNot().to_matrix(), ), (Circuit().ccnot(1, 2, 0), gates.CCNot().to_matrix()), (Circuit().cswap(2, 1, 0), gates.CSwap().to_matrix()), (Circuit().cswap(2, 0, 1), gates.CSwap().to_matrix()), (Circuit().h(1), np.kron(gates.H().to_matrix(), np.eye(2))), (Circuit().x(1).i(2), np.kron(np.eye(2), np.kron(gates.X().to_matrix(), np.eye(2)))), ( Circuit().y(1).z(2), np.kron(gates.Z().to_matrix(), np.kron(gates.Y().to_matrix(), np.eye(2))), ), (Circuit().rx(1, 0.15), np.kron(gates.Rx(0.15).to_matrix(), np.eye(2))),