[docs]defdecomposer(qc:QuantumCircuit,reps:int=2,)->QuantumCircuit:"""Decompose the circuit with giving times. Args: qc (QuantumCircuit): The circuit wanted to be decomposed. reps (int, optional): Decide the times of decomposing the circuit. Draw quantum circuit with composed circuit. Defaults to 2. Returns: QuantumCircuit: The decomposed circuit. """returnqc.decompose(reps=reps)
[docs]defget_counts_and_exceptions(result:Optional[Result],num:Optional[int]=None,result_idx_list:Optional[list[int]]=None,)->tuple[list[dict[str,int]],dict[str,Exception]]:"""Get counts and exceptions from result. Args: result (Optional[Result]): The result of job. num (Optional[int], optional): The number of counts wanted to be extracted. Defaults to None. result_idx_list (Optional[list[int]], optional): The index of counts wanted to be extracted. Defaults to None. Returns: tuple[list[dict[str, int]], dict[str, Exception]]: Counts and exceptions. """counts:list[dict[str,int]]=[]exceptions:dict[str,Exception]={}ifnumisNone:idx_list=[]ifresult_idx_listisNoneelseresult_idx_listelse:ifresult_idx_listisNone:idx_list=list(range(num))else:warnings.warn(("The number of result is not equal to the length of "+"'result_idx_list', use length of 'result_idx_list'.")ifnum!=len(result_idx_list)else("The 'num' is not None, but 'result_idx_list' is not None, "+"use 'result_idx_list'."))idx_list=result_idx_listifresultisNone:exceptions["None"]=QurryCountLost("Result is None")print("| Failed Job result skip.")for_inidx_list:counts.append({})returncounts,exceptionsiflen(idx_list)==0:try:get:Union[list[dict[str,int]],dict[str,int]]=result.get_counts()ifisinstance(get,list):counts:list[dict[str,int]]=getelse:counts.append(get)exceptQiskitErroraserr_1:exceptions[f"{result.job_id}"]=err_1print("| Failed Job result skip, Job ID:",result.job_id,err_1)returncounts,exceptionsforiinidx_list:try:all_meas=result.get_counts(i)assertisinstance(all_meas,dict),"The counts is not a dict."exceptQiskitErroraserr_2:exceptions[f"{result.job_id}.{i}"]=err_2print("| Failed Job result skip, Job ID/which counts:",result.job_id,i,err_2,)all_meas={}counts.append(all_meas)returncounts,exceptions