Source code for qurry.process.hadamard_test.purity_echo_core
"""Post Processing - Hadamard Test - Purity/Echo(:mod:`qurry.process.hadamard_test.purity_echo_core`)"""importwarningsimportnumpyasnpfrom..availabilityimport(availablility,default_postprocessing_backend,PostProcessingBackendLabel,)from..exceptionsimport(PostProcessingRustImportError,PostProcessingRustUnavailableWarning,PostProcessingBackendDeprecatedWarning,)try:from...boorustimporthadamard# type: ignorepurity_echo_core_rust_source=hadamard.purity_echo_core_rustRUST_AVAILABLE=TrueFAILED_RUST_IMPORT=NoneexceptImportErroraserr:RUST_AVAILABLE=FalseFAILED_RUST_IMPORT=errdefpurity_echo_core_rust_source(*args,**kwargs):"""Dummy function for purity_cell_rust."""raisePostProcessingRustImportError("Rust is not available, using python to calculate purity cell.")fromFAILED_RUST_IMPORTBACKEND_AVAILABLE=availablility("hadamard_test.purity_echo_core",[("Rust",RUST_AVAILABLE,FAILED_RUST_IMPORT),],)DEFAULT_PROCESS_BACKEND=default_postprocessing_backend(RUST_AVAILABLE,False,)
[docs]defpurity_echo_core_allrust(shots:int,counts:list[dict[str,int]],)->float:"""The core function of entangled entropy by Rust. Args: shots (int): Shots of the experiment on quantum machine. counts (list[dict[str, int]]): Counts of the experiment on quantum machine. Raises: ValueError: Get degree neither 'int' nor 'tuple[int, int]'. ValueError: Measure range does not contain subsystem. Returns: float: Purity or Echo of the experiment. """returnpurity_echo_core_rust_source(shots,counts)
[docs]defpurity_echo_core(shots:int,counts:list[dict[str,int]],backend:PostProcessingBackendLabel=DEFAULT_PROCESS_BACKEND,)->float:"""Calculate entangled entropy with more information combined. The entropy we compute is the Second Order Rényi Entropy. Args: shots (int): Shots of the experiment on quantum machine. counts (list[dict[str, int]]): Counts of the experiment on quantum machine. Raises: Warning: Expected '0' and '1', but there is no such keys ValueError: The length of counts is not 1. ValueError: shots does not match sample_shots. Returns: dict[str, float]: Quantity of the experiment. """iflen(counts)!=1:raiseValueError(f"counts should be a list of counts with length 1, but got {len(counts)}")ifbackend=="Rust":ifRUST_AVAILABLE:returnpurity_echo_core_allrust(shots,counts)warnings.warn(PostProcessingRustUnavailableWarning("Rust is not available, using python to calculate purity cell."))ifbackend=="Cython":warnings.warn("Cython backend is deprecated, using Python or Rust to calculate purity cell.",PostProcessingBackendDeprecatedWarning,)backend=DEFAULT_PROCESS_BACKENDonly_counts=counts[0]sample_shots=sum(only_counts.values())assertsample_shots==shots,f"shots {shots} does not match sample_shots {sample_shots}"is_zero_include="0"inonly_countsis_one_include="1"inonly_countsifis_zero_includeandis_one_include:purity=(only_counts["0"]-only_counts["1"])/shotselifis_zero_include:purity=only_counts["0"]/shotselifis_one_include:purity=only_counts["1"]/shotselse:purity=np.nanraiseValueError("Expected '0' and '1', but there is no such keys")returnpurity