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 _calculate_result_types( ir_string: str, measurements: np.ndarray, measured_qubits: List[int]) -> List[ResultTypeValue]: ir = json.loads(ir_string) result_types = [] if not ir.get("results"): return result_types for result_type in ir["results"]: ir_observable = result_type.get("observable") observable = observable_from_ir( ir_observable) if ir_observable else None targets = result_type.get("targets") rt_type = result_type["type"] if rt_type == "probability": value = GateModelQuantumTaskResult._probability_from_measurements( measurements, measured_qubits, targets) casted_result_type = Probability(targets=targets) elif rt_type == "sample": value = GateModelQuantumTaskResult._calculate_for_targets( GateModelQuantumTaskResult._samples_from_measurements, measurements, measured_qubits, observable, targets, ) casted_result_type = Sample(targets=targets, observable=ir_observable) elif rt_type == "variance": value = GateModelQuantumTaskResult._calculate_for_targets( GateModelQuantumTaskResult._variance_from_measurements, measurements, measured_qubits, observable, targets, ) casted_result_type = Variance(targets=targets, observable=ir_observable) elif rt_type == "expectation": value = GateModelQuantumTaskResult._calculate_for_targets( GateModelQuantumTaskResult._expectation_from_measurements, measurements, measured_qubits, observable, targets, ) casted_result_type = Expectation(targets=targets, observable=ir_observable) else: raise ValueError(f"Unknown result type {rt_type}") result_types.append( ResultTypeValue.construct(type=casted_result_type, value=value)) return result_types
def _generate_results( circuit_ir: Program, non_observable_result_types: Dict[int, ResultType], observable_result_types: Dict[int, ObservableResultType], observables: List[Observable], simulation, ) -> List[ResultTypeValue]: results = [0] * len(circuit_ir.results) for index in non_observable_result_types: results[index] = ResultTypeValue.construct( type=circuit_ir.results[index], value=non_observable_result_types[index].calculate(simulation), ) if observable_result_types: simulation.apply_observables(observables) for index in observable_result_types: results[index] = ResultTypeValue.construct( type=circuit_ir.results[index], value=observable_result_types[index].calculate(simulation), ) return results
def result_obj_4(task_metadata_zero_shots, additional_metadata): return GateModelTaskResult( resultTypes=[ ResultTypeValue.construct(type=jaqcd.Probability(targets=[0]), value=np.array([0.5, 0.5])), ResultTypeValue.construct( type=jaqcd.StateVector(), value=np.array( [complex(0.70710678, 0), 0, 0, complex(0.70710678, 0)]), ), ResultTypeValue.construct(type=jaqcd.Expectation(observable=["y"], targets=[0]), value=0.0), ResultTypeValue.construct(type=jaqcd.Variance(observable=["y"], targets=[0]), value=0.1), ResultTypeValue.construct(type=jaqcd.Amplitude(states=["00"]), value={"00": complex(0.70710678, 0)}), ], measuredQubits=list(range(2)), taskMetadata=task_metadata_zero_shots, additionalMetadata=additional_metadata, )