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_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 _create_results_obj( results: List[Dict[str, Any]], circuit_ir: Program, simulation: StateVectorSimulation, ) -> GateModelTaskResult: result_dict = { "taskMetadata": TaskMetadata(id=str(uuid.uuid4()), shots=simulation.shots, deviceId=DefaultSimulator.DEVICE_ID), "additionalMetadata": AdditionalMetadata(action=circuit_ir), } if results: result_dict["resultTypes"] = results if simulation.shots: result_dict[ "measurements"] = DefaultSimulator._formatted_measurements( simulation) result_dict[ "measuredQubits"] = DefaultSimulator._get_measured_qubits( simulation.qubit_count) return GateModelTaskResult.construct(**result_dict)
def task_metadata(): return TaskMetadata(**{"id": "task_arn", "deviceId": "arn1", "shots": 100})
def task_metadata_zero_shots(): return TaskMetadata(**{ "id": "task_arn", "deviceId": "default", "shots": 0 })
# or in the "license" file accompanying this file. This file is # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF # ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. import uuid import numpy as np import pytest from braket.task_result import TaskMetadata from braket.tasks import GateModelQuantumTaskResult from braket.tasks.local_quantum_task import LocalQuantumTask RESULT = GateModelQuantumTaskResult( task_metadata=TaskMetadata(**{"id": str(uuid.uuid4()), "deviceId": "default", "shots": 100}), additional_metadata=None, measurements=np.array([[0, 1], [1, 0]]), measured_qubits=[0, 1], result_types=None, values=None, ) TASK = LocalQuantumTask(RESULT) def test_id(): # Task ID is valid UUID uuid.UUID(TASK.id)