def test_simulator_run_result_types_shots_basis_rotation_gates():
    simulator = DefaultSimulator()
    ir = Program.parse_raw(
        json.dumps({
            "instructions": [
                {
                    "type": "h",
                    "target": 0
                },
                {
                    "type": "cnot",
                    "target": 1,
                    "control": 0
                },
            ],
            "basis_rotation_instructions": [{
                "type": "h",
                "target": 1
            }],
            "results": [{
                "type": "expectation",
                "observable": ["x"],
                "targets": [1]
            }],
        }))
    shots_count = 1000
    result = simulator.run(ir, qubit_count=2, shots=shots_count)
    assert all([len(measurement) == 2] for measurement in result.measurements)
    assert len(result.measurements) == shots_count
    assert not result.resultTypes
    assert result.measuredQubits == [0, 1]
def test_simulator_run_result_types_shots_basis_rotation_gates_value_error():
    simulator = DefaultSimulator()
    ir = Program.parse_raw(
        json.dumps({
            "instructions": [
                {
                    "type": "h",
                    "target": 0
                },
                {
                    "type": "cnot",
                    "target": 1,
                    "control": 0
                },
            ],
            "basis_rotation_instructions": [{
                "type": "foo",
                "target": 1
            }],
            "results": [{
                "type": "expectation",
                "observable": ["x"],
                "targets": [1]
            }],
        }))
    shots_count = 1000
    simulator.run(ir, qubit_count=2, shots=shots_count)
def test_simulator_run_grcs_16(grcs_16_qubit, batch_size):
    simulator = DefaultSimulator()
    result = simulator.run(grcs_16_qubit.circuit_ir,
                           qubit_count=16,
                           shots=0,
                           batch_size=batch_size)
    state_vector = result.resultTypes[0].value
    assert cmath.isclose(abs(state_vector[0])**2,
                         grcs_16_qubit.probability_zero,
                         abs_tol=1e-7)
def test_simulator_run_statevector_shots():
    simulator = DefaultSimulator()
    ir = Program.parse_raw(
        json.dumps({
            "instructions": [{
                "type": "h",
                "target": 0
            }],
            "results": [{
                "type": "statevector"
            }]
        }))
    simulator.run(ir, qubit_count=2, shots=100)
def test_simulator_run_amplitude_no_shots_invalid_states():
    simulator = DefaultSimulator()
    ir = Program.parse_raw(
        json.dumps({
            "instructions": [{
                "type": "h",
                "target": 0
            }],
            "results": [{
                "type": "amplitude",
                "states": ["0"]
            }],
        }))
    simulator.run(ir, qubit_count=2, shots=0)
def test_simulator_bell_pair_result_types(bell_ir_with_result, targets,
                                          batch_size):
    simulator = DefaultSimulator()
    ir = bell_ir_with_result(targets)
    result = simulator.run(ir, qubit_count=2, shots=0, batch_size=batch_size)
    assert len(result.resultTypes) == 2
    assert result.resultTypes[0] == ResultTypeValue.construct(
        type=ir.results[0], value={"11": complex(1 / 2**0.5)})
    assert result.resultTypes[1] == ResultTypeValue.construct(
        type=ir.results[1], value=(0 if targets else [0, 0]))
    assert result.taskMetadata == TaskMetadata(
        id=result.taskMetadata.id,
        deviceId=DefaultSimulator.DEVICE_ID,
        shots=0)
    assert result.additionalMetadata == AdditionalMetadata(action=ir)
def test_simulator_fails_samples_0_shots():
    simulator = DefaultSimulator()
    prog = Program.parse_raw(
        json.dumps({
            "instructions": [{
                "type": "h",
                "target": 0
            }],
            "results": [{
                "type": "sample",
                "observable": ["x"],
                "targets": [0]
            }],
        }))
    simulator.run(prog, qubit_count=1, shots=0)
Beispiel #8
0
def test_layered_continuous_gates_circuit(benchmark,
                                          generate_continuous_gates_circuit,
                                          nqubits, nlayers):
    circuit = generate_continuous_gates_circuit(nqubits, nlayers,
                                                [jaqcd.StateVector()])
    device = DefaultSimulator()
    benchmark(device.run, circuit, nqubits, shots=0)
def test_validate_and_consolidate_observable_result_types_tensor_product():
    obs_rts = [
        Expectation(
            observables.TensorProduct(
                [observables.PauliX([0]),
                 observables.PauliY([1])])),
        Variance(
            observables.TensorProduct(
                [observables.PauliX([0]),
                 observables.PauliY([1])])),
        Expectation(
            observables.TensorProduct(
                [observables.PauliX([2]),
                 observables.PauliY([3])])),
    ]
    actual_obs = DefaultSimulator._validate_and_consolidate_observable_result_types(
        obs_rts, 4)
    assert len(actual_obs) == 2
    assert actual_obs[0].measured_qubits == (
        0,
        1,
    )
    assert actual_obs[1].measured_qubits == (
        2,
        3,
    )
Beispiel #10
0
def test_layered_continuous_gates_circuit_result_types(
        benchmark, generate_continuous_gates_circuit, results):
    nqubits = 12
    nlayers = 15
    shots = 0
    circuit = generate_continuous_gates_circuit(nqubits, nlayers, results)
    device = DefaultSimulator()
    benchmark(device.run, circuit, nqubits, shots=shots)
def test_validate_and_consolidate_observable_result_types_same_target(obs):
    obs_rts = [
        Expectation(obs),
        Variance(obs),
    ]
    actual_obs = DefaultSimulator._validate_and_consolidate_observable_result_types(
        obs_rts, 2)
    assert len(actual_obs) == 1
    assert actual_obs[0].measured_qubits == (1, )
def test_validate_and_consolidate_observable_result_types_none(obs1, obs2):
    obs_rts = [
        Expectation(obs1),
        Variance(obs2),
    ]
    actual_obs = DefaultSimulator._validate_and_consolidate_observable_result_types(
        obs_rts, 2)
    assert len(actual_obs) == 1
    assert actual_obs[0].measured_qubits is None
def test_validate_and_consolidate_observable_result_types_targets(obs1, obs2):
    obs_rts = [
        Expectation(obs1),
        Expectation(obs2),
    ]
    actual_obs = DefaultSimulator._validate_and_consolidate_observable_result_types(
        obs_rts, 3)
    assert len(actual_obs) == 2
    assert actual_obs[0].measured_qubits == (1, )
    assert actual_obs[1].measured_qubits == (2, )
def test_simulator_fails_overlapping_targets_different_observable(
        result_types):
    simulator = DefaultSimulator()
    prog = Program.parse_raw(
        json.dumps({
            "instructions": [
                {
                    "type": "h",
                    "target": 0
                },
                {
                    "type": "cnot",
                    "target": 1,
                    "control": 0
                },
            ],
            "results":
            result_types,
        }))
    simulator.run(prog, qubit_count=2, shots=0)
def test_simulator_run_bell_pair(bell_ir, batch_size):
    simulator = DefaultSimulator()
    shots_count = 10000
    result = simulator.run(bell_ir,
                           qubit_count=2,
                           shots=shots_count,
                           batch_size=batch_size)

    assert all([len(measurement) == 2] for measurement in result.measurements)
    assert len(result.measurements) == shots_count
    counter = Counter(
        ["".join(measurement) for measurement in result.measurements])
    assert counter.keys() == {"00", "11"}
    assert 0.4 < counter["00"] / (counter["00"] + counter["11"]) < 0.6
    assert 0.4 < counter["11"] / (counter["00"] + counter["11"]) < 0.6
    assert result.taskMetadata == TaskMetadata(
        id=result.taskMetadata.id,
        deviceId=DefaultSimulator.DEVICE_ID,
        shots=shots_count)
    assert result.additionalMetadata == AdditionalMetadata(action=bell_ir)
def test_simulator_identity():
    simulator = DefaultSimulator()
    shots_count = 1000
    result = simulator.run(
        Program.parse_raw(
            json.dumps({
                "instructions": [{
                    "type": "i",
                    "target": 0
                }, {
                    "type": "i",
                    "target": 1
                }]
            })),
        qubit_count=2,
        shots=shots_count,
    )
    counter = Counter(
        ["".join(measurement) for measurement in result.measurements])
    assert counter.keys() == {"00"}
    assert counter["00"] == shots_count
def test_simulator_accepts_overlapping_targets_same_observable(
        result_types, expected_expectation, expected_variance):
    simulator = DefaultSimulator()
    prog = Program.parse_raw(
        json.dumps({
            "instructions": [
                {
                    "type": "h",
                    "target": 0
                },
                {
                    "type": "cnot",
                    "target": 1,
                    "control": 0
                },
            ],
            "results":
            result_types,
        }))
    result = simulator.run(prog, qubit_count=2, shots=0)
    expectation = result.resultTypes[0].value
    variance = result.resultTypes[1].value
    assert np.allclose(expectation, expected_expectation)
    assert np.allclose(variance, expected_variance)
def test_simulator_run_observable_references_invalid_qubit(ir, qubit_count):
    simulator = DefaultSimulator()
    shots_count = 0
    simulator.run(ir, qubit_count=qubit_count, shots=shots_count)
def test_properties():
    simulator = DefaultSimulator()
    observables = ["X", "Y", "Z", "H", "I", "Hermitian"]
    max_shots = sys.maxsize
    qubit_count = 26
    expected_properties = GateModelSimulatorDeviceCapabilities.parse_obj({
        "service": {
            "executionWindows": [{
                "executionDay": "Everyday",
                "windowStartHour": "00:00",
                "windowEndHour": "23:59:59",
            }],
            "shotsRange": [0, max_shots],
        },
        "action": {
            "braket.ir.jaqcd.program": {
                "actionType":
                "braket.ir.jaqcd.program",
                "version": ["1"],
                "supportedOperations":
                sorted([
                    instruction.__name__ for instruction in
                    gate_operations._from_braket_instruction.registry
                    if type(instruction) is not type
                ]),
                "supportedResultTypes": [
                    {
                        "name": "Sample",
                        "observables": observables,
                        "minShots": 1,
                        "maxShots": max_shots,
                    },
                    {
                        "name": "Expectation",
                        "observables": observables,
                        "minShots": 0,
                        "maxShots": max_shots,
                    },
                    {
                        "name": "Variance",
                        "observables": observables,
                        "minShots": 0,
                        "maxShots": max_shots,
                    },
                    {
                        "name": "Probability",
                        "minShots": 0,
                        "maxShots": max_shots
                    },
                    {
                        "name": "StateVector",
                        "minShots": 0,
                        "maxShots": 0
                    },
                    {
                        "name": "Amplitude",
                        "minShots": 0,
                        "maxShots": 0
                    },
                ],
            }
        },
        "paradigm": {
            "qubitCount": qubit_count
        },
        "deviceParameters":
        GateModelSimulatorDeviceParameters.schema(),
    })
    assert simulator.properties == expected_properties
def test_default_simulator_instance_braket_simulator():
    assert isinstance(DefaultSimulator(), BraketSimulator)
Beispiel #21
0
def test_grcs_simulation(benchmark, grcs_circuit_16):
    device = DefaultSimulator()
    benchmark(device.run, grcs_circuit_16, 16, shots=0)
Beispiel #22
0
def test_qft(benchmark, generate_qft_circuit, nqubits):
    circuit = generate_qft_circuit(nqubits)
    device = DefaultSimulator()
    benchmark(device.run, circuit, nqubits, shots=0)
def test_simulator_run_non_contiguous_qubits(ir, qubit_count):
    simulator = DefaultSimulator()
    shots_count = 1000
    simulator.run(ir, qubit_count=qubit_count, shots=shots_count)
def test_simulator_run_no_results_no_shots(bell_ir):
    simulator = DefaultSimulator()
    simulator.run(bell_ir, qubit_count=2, shots=0)