Esempio n. 1
0
def convert_measurements_to_sampleset(
    measurements, qubo, change_bitstring_convention=False
):
    qubo = load_qubo(qubo)
    measurements = Measurements.load_from_file(measurements)
    sampleset = _convert_measurements_to_sampleset(
        measurements, qubo, change_bitstring_convention
    )
    save_sampleset(sampleset.aggregate(), "sampleset.json")
Esempio n. 2
0
def get_summed_expectation_values(
        operator: str,
        measurements: str,
        use_bessel_correction: Optional[bool] = True):
    if isinstance(operator, str):
        operator = load_qubit_operator(operator)
        operator = change_operator_type(operator, openfermion.IsingOperator)
    if isinstance(measurements, str):
        measurements = Measurements.load_from_file(measurements)
    expectation_values = measurements.get_expectation_values(
        operator, use_bessel_correction=use_bessel_correction)
    value_estimate = sum_expectation_values(expectation_values)
    save_value_estimate(value_estimate, "value-estimate.json")
Esempio n. 3
0
    def test_get_distribution(self):
        # Given
        measurements_data = {
            "schema":
            SCHEMA_VERSION + "-measurements",
            "counts": {
                "000": 1,
                "001": 2,
                "010": 1,
                "011": 1,
                "100": 1,
                "101": 1,
                "110": 1,
                "111": 1,
            },
            "bitstrings": [
                [0, 0, 0],
                [0, 0, 1],
                [0, 1, 0],
                [0, 1, 1],
                [1, 0, 0],
                [1, 1, 0],
                [1, 1, 1],
                [1, 0, 1],
                [0, 0, 1],
            ],
        }
        input_filename = "measurements_input_test.json"

        with open(input_filename, "w") as f:
            f.write(json.dumps(measurements_data, indent=2))
        measurements = Measurements.load_from_file(input_filename)

        # When
        distribution = measurements.get_distribution()

        # Then
        assert distribution.distribution_dict == {
            "000": 1 / 9,
            "001": 2 / 9,
            "010": 1 / 9,
            "011": 1 / 9,
            "100": 1 / 9,
            "101": 1 / 9,
            "110": 1 / 9,
            "111": 1 / 9,
        }

        remove_file_if_exists(input_filename)
Esempio n. 4
0
    def test_save_for_numpy_integers(self):
        # Given
        target_bitstrings = [(0, 0, 0)]
        input_bitstrings = [(np.int8(0), np.int8(0), np.int8(0))]

        filename = "measurementstest.json"
        measurements = Measurements(input_bitstrings)
        target_measurements = Measurements(target_bitstrings)

        # When
        measurements.save(filename)

        # Then
        recreated_measurements = Measurements.load_from_file(filename)
        assert target_measurements.bitstrings == recreated_measurements.bitstrings
        remove_file_if_exists("measurementstest.json")
Esempio n. 5
0
    def test_bitstrings(self):
        # Given
        measurements_data = {
            "schema":
            SCHEMA_VERSION + "-measurements",
            "counts": {
                "000": 1,
                "001": 2,
                "010": 1,
                "011": 1,
                "100": 1,
                "101": 1,
                "110": 1,
                "111": 1,
            },
            "bitstrings": [
                [0, 0, 0],
                [0, 0, 1],
                [0, 1, 0],
                [0, 1, 1],
                [1, 0, 0],
                [1, 1, 0],
                [1, 1, 1],
                [1, 0, 1],
                [0, 0, 1],
            ],
        }
        input_filename = "measurements_input_test.json"

        with open(input_filename, "w") as f:
            f.write(json.dumps(measurements_data, indent=2))
        measurements = Measurements.load_from_file(input_filename)

        # When/Then
        assert measurements.bitstrings == [
            (0, 0, 0),
            (0, 0, 1),
            (0, 1, 0),
            (0, 1, 1),
            (1, 0, 0),
            (1, 1, 0),
            (1, 1, 1),
            (1, 0, 1),
            (0, 0, 1),
        ]

        remove_file_if_exists(input_filename)
Esempio n. 6
0
    def test_io(self):
        # Given
        measurements_data = {
            "schema":
            SCHEMA_VERSION + "-measurements",
            "counts": {
                "000": 1,
                "001": 2,
                "010": 1,
                "011": 1,
                "100": 1,
                "101": 1,
                "110": 1,
                "111": 1,
            },
            "bitstrings": [
                [0, 0, 0],
                [0, 0, 1],
                [0, 1, 0],
                [0, 1, 1],
                [1, 0, 0],
                [1, 1, 0],
                [1, 1, 1],
                [1, 0, 1],
                [0, 0, 1],
            ],
        }
        input_filename = "measurements_input_test.json"
        output_filename = "measurements_output_test.json"

        with open(input_filename, "w") as f:
            f.write(json.dumps(measurements_data, indent=2))

        # When
        measurements = Measurements.load_from_file(input_filename)
        measurements.save(output_filename)

        # Then
        with open(output_filename, "r") as f:
            output_data = json.load(f)
        assert measurements_data == output_data

        remove_file_if_exists(input_filename)
        remove_file_if_exists(output_filename)