def _read_instruction_parameter(file_obj, version, vectors): type_key, bin_data = common.read_instruction_param(file_obj) if type_key == common.ProgramTypeKey.CIRCUIT: param = common.data_from_binary(bin_data, read_circuit, version=version) elif type_key == common.ContainerTypeKey.RANGE: data = formats.RANGE._make(struct.unpack(formats.RANGE_PACK, bin_data)) param = range(data.start, data.stop, data.step) elif type_key == common.ContainerTypeKey.TUPLE: param = tuple( common.sequence_from_binary( bin_data, _read_instruction_parameter, version=version, vectors=vectors, )) elif type_key == common.ValueTypeKey.INTEGER: # TODO This uses little endian. Should be fixed in the next QPY version. param = struct.unpack("<q", bin_data)[0] elif type_key == common.ValueTypeKey.FLOAT: # TODO This uses little endian. Should be fixed in the next QPY version. param = struct.unpack("<d", bin_data)[0] else: param = value.loads_value(type_key, bin_data, version, vectors) return param
def _loads_instruction_parameter(type_key, data_bytes, version, vectors): if type_key == type_keys.Program.CIRCUIT: param = common.data_from_binary(data_bytes, read_circuit, version=version) elif type_key == type_keys.Container.RANGE: data = formats.RANGE._make( struct.unpack(formats.RANGE_PACK, data_bytes)) param = range(data.start, data.stop, data.step) elif type_key == type_keys.Container.TUPLE: param = tuple( common.sequence_from_binary( data_bytes, _loads_instruction_parameter, version=version, vectors=vectors, )) elif type_key == type_keys.Value.INTEGER: # TODO This uses little endian. Should be fixed in the next QPY version. param = struct.unpack("<q", data_bytes)[0] elif type_key == type_keys.Value.FLOAT: # TODO This uses little endian. Should be fixed in the next QPY version. param = struct.unpack("<d", data_bytes)[0] else: param = value.loads_value(type_key, data_bytes, version, vectors) return param
def _loads_operand(type_key, data_bytes, version): if type_key == type_keys.ScheduleOperand.WAVEFORM: return common.data_from_binary(data_bytes, _read_waveform, version=version) if type_key == type_keys.ScheduleOperand.SYMBOLIC_PULSE: return common.data_from_binary(data_bytes, _read_symbolic_pulse, version=version) if type_key == type_keys.ScheduleOperand.CHANNEL: return common.data_from_binary(data_bytes, _read_channel, version=version) return value.loads_value(type_key, data_bytes, version, {})
def _read_pauli_evolution_gate(file_obj, version, vectors): pauli_evolution_def = formats.PAULI_EVOLUTION_DEF._make( struct.unpack(formats.PAULI_EVOLUTION_DEF_PACK, file_obj.read(formats.PAULI_EVOLUTION_DEF_SIZE))) if pauli_evolution_def.operator_size != 1 and pauli_evolution_def.standalone_op: raise ValueError( "Can't have a standalone operator with {pauli_evolution_raw[0]} operators in the payload" ) operator_list = [] for _ in range(pauli_evolution_def.operator_size): op_elem = formats.SPARSE_PAULI_OP_LIST_ELEM._make( struct.unpack( formats.SPARSE_PAULI_OP_LIST_ELEM_PACK, file_obj.read(formats.SPARSE_PAULI_OP_LIST_ELEM_SIZE), )) op_raw_data = common.data_from_binary(file_obj.read(op_elem.size), np.load) operator_list.append(SparsePauliOp.from_list(op_raw_data)) if pauli_evolution_def.standalone_op: pauli_op = operator_list[0] else: pauli_op = operator_list time = value.loads_value( common.ValueTypeKey(pauli_evolution_def.time_type), file_obj.read(pauli_evolution_def.time_size), version=version, vectors=vectors, ) synth_data = json.loads( file_obj.read(pauli_evolution_def.synth_method_size)) synthesis = getattr(evo_synth, synth_data["class"])(**synth_data["settings"]) return_gate = library.PauliEvolutionGate(pauli_op, time=time, synthesis=synthesis) return return_gate
def _read_header_v2(file_obj, version, vectors): data = formats.CIRCUIT_HEADER_V2._make( struct.unpack( formats.CIRCUIT_HEADER_V2_PACK, file_obj.read(formats.CIRCUIT_HEADER_V2_SIZE), )) name = file_obj.read(data.name_size).decode(common.ENCODE) global_phase = value.loads_value( data.global_phase_type, file_obj.read(data.global_phase_size), version=version, vectors=vectors, ) header = { "global_phase": global_phase, "num_qubits": data.num_qubits, "num_clbits": data.num_clbits, "num_registers": data.num_registers, "num_instructions": data.num_instructions, } metadata_raw = file_obj.read(data.metadata_size) metadata = json.loads(metadata_raw) return header, name, metadata