def from_string(result: str) -> GateModelQuantumTaskResult:
        """
        Create GateModelQuantumTaskResult from string.

        Args:
            result (str): JSON object string, with GateModelQuantumTaskResult attributes as keys.

        Returns:
            GateModelQuantumTaskResult: A GateModelQuantumTaskResult based on the given string

        Raises:
            ValueError: If neither "Measurements" nor "MeasurementProbabilities" is a key
                in the result dict
        """
        obj = GateModelTaskResult.parse_raw(result)
        if obj.resultTypes:
            for result_type in obj.resultTypes:
                type = result_type.type.type
                if type == "probability":
                    result_type.value = np.array(result_type.value)
                elif type == "statevector":
                    result_type.value = np.array(
                        [complex(*value) for value in result_type.value])
                elif type == "amplitude":
                    for state in result_type.value:
                        result_type.value[state] = complex(
                            *result_type.value[state])
        return GateModelQuantumTaskResult._from_object_internal(obj)
예제 #2
0
def test_from_object_equal_to_from_string(result_obj_1, result_str_1,
                                          result_str_3):
    assert GateModelQuantumTaskResult.from_object(
        result_obj_1) == GateModelQuantumTaskResult.from_string(result_str_1)
    assert GateModelQuantumTaskResult.from_object(
        GateModelTaskResult.parse_raw(result_str_3)
    ) == GateModelQuantumTaskResult.from_string(result_str_3)
예제 #3
0
def test_from_string_measurement_probabilities(result_str_3):
    result_obj = GateModelTaskResult.parse_raw(result_str_3)
    task_result = GateModelQuantumTaskResult.from_string(result_str_3)
    assert task_result.measurement_probabilities == result_obj.measurementProbabilities
    shots = 100
    measurement_list = [list("011000") for _ in range(shots)]
    expected_measurements = np.asarray(measurement_list, dtype=int)
    assert np.allclose(task_result.measurements, expected_measurements)
    assert task_result.measurement_counts == Counter(["011000" for x in range(shots)])
    assert not task_result.measurement_counts_copied_from_device
    assert task_result.measurement_probabilities_copied_from_device
    assert not task_result.measurements_copied_from_device
예제 #4
0
def test_from_string_measurements(result_str_1):
    result_obj = GateModelTaskResult.parse_raw(result_str_1)
    task_result = GateModelQuantumTaskResult.from_string(result_str_1)
    expected_measurements = np.asarray(result_obj.measurements, dtype=int)
    assert task_result.task_metadata == result_obj.taskMetadata
    assert task_result.additional_metadata == result_obj.additionalMetadata
    assert np.array2string(task_result.measurements) == np.array2string(expected_measurements)
    assert not task_result.measurement_counts_copied_from_device
    assert not task_result.measurement_probabilities_copied_from_device
    assert task_result.measurements_copied_from_device
    assert task_result.measured_qubits == result_obj.measuredQubits
    assert task_result.values == []
    assert task_result.result_types == []
예제 #5
0
    def from_string(result: str) -> GateModelQuantumTaskResult:
        """
        Create GateModelQuantumTaskResult from string.

        Args:
            result (str): JSON object string, with GateModelQuantumTaskResult attributes as keys.

        Returns:
            GateModelQuantumTaskResult: A GateModelQuantumTaskResult based on the given string

        Raises:
            ValueError: If neither "Measurements" nor "MeasurementProbabilities" is a key
                in the result dict
        """
        obj = GateModelTaskResult.parse_raw(result)
        GateModelQuantumTaskResult.cast_result_types(obj)
        return GateModelQuantumTaskResult._from_object_internal(obj)