def concatenate_qubit_operator_lists( qubit_operator_list_A: Union[str, List[QubitOperator]], qubit_operator_list_B: Union[str, List[QubitOperator]], ): if isinstance(qubit_operator_list_A, str): qubit_operator_list_A = load_qubit_operator_set(qubit_operator_list_A) if isinstance(qubit_operator_list_B, str): qubit_operator_list_B = load_qubit_operator_set(qubit_operator_list_B) qubit_operator_list_final = qubit_operator_list_A + qubit_operator_list_B save_qubit_operator_set(qubit_operator_list_final, "concatenated-qubit-operators.json")
def create_farhi_qaoa_circuits(number_of_layers: Union[int, List[int], str], hamiltonians): if isinstance(number_of_layers, str): number_of_layers = load_list(number_of_layers) hamiltonians_objects = load_qubit_operator_set(hamiltonians) circuits = farhi_ansatz.create_farhi_qaoa_circuits(hamiltonians_objects, number_of_layers) save_circuitset(circuits, "circuits.json")
def concatenate_qubit_operator_lists( qubit_operator_list_A: Union[str, List[QubitOperator]], qubit_operator_list_B: Union[str, List[QubitOperator]], ): if isinstance(qubit_operator_list_A, str): qubit_operator_list_A_loaded = load_qubit_operator_set(qubit_operator_list_A) else: qubit_operator_list_A_loaded = qubit_operator_list_A if isinstance(qubit_operator_list_B, str): qubit_operator_list_B_loaded = load_qubit_operator_set(qubit_operator_list_B) else: qubit_operator_list_B_loaded = qubit_operator_list_B qubit_operator_list_final = ( qubit_operator_list_A_loaded + qubit_operator_list_B_loaded ) save_qubit_operator_set( qubit_operator_list_final, "concatenated-qubit-operators.json" )
def evaluate_qubit_operator_list( qubit_operator_list: Union[str, List[QubitOperator]], expectation_values: Union[str, ExpectationValues], ): if isinstance(qubit_operator_list, str): qubit_operator_list = load_qubit_operator_set(qubit_operator_list) if isinstance(expectation_values, str): expectation_values = load_expectation_values(expectation_values) value_estimate = _evaluate_qubit_operator_list(qubit_operator_list, expectation_values) save_value_estimate(value_estimate, "value-estimate.json")
def test_concatenate_qubit_operator_lists(): group_A = h2_hamiltonian_grouped[:3] group_B = h2_hamiltonian_grouped[3:] save_qubit_operator_set(group_A, "groupA.json") save_qubit_operator_set(group_B, "groupB.json") concatenate_qubit_operator_lists("groupA.json", "groupB.json") group_read = load_qubit_operator_set("concatenated-qubit-operators.json") os.remove("groupA.json") os.remove("groupB.json") os.remove("concatenated-qubit-operators.json") assert group_read == h2_hamiltonian_grouped
def grouped_hamiltonian_analysis( groups: str, expectation_values: Optional[str] = None, ): """Calculates the number of measurements required for computing the expectation value of a qubit hamiltonian, where co-measurable terms are grouped as a list of QubitOperators. We are assuming the exact expectation values are provided (i.e. infinite number of measurements or simulations without noise) M ~ (\sum_{i} prec(H_i)) ** 2.0 / (epsilon ** 2.0) where prec(H_i) is the precision (square root of the variance) for each group of co-measurable terms H_{i}. It is computed as prec(H_{i}) = \sum{ab} |h_{a}^{i}||h_{b}^{i}| cov(O_{a}^{i}, O_{b}^{i}) where h_{a}^{i} is the coefficient of the a-th operator, O_{a}^{i}, in the i-th group. Covariances are assumed to be zero for a != b: cov(O_{a}^{i}, O_{b}^{i}) = <O_{a}^{i} O_{b}^{i}> - <O_{a}^{i}> <O_{b}^{i}> = 0 ARGS: groups (str): The name of a file containing a list of QubitOperator objects, where each element in the list is a group of co-measurable terms. expectation_values (str): The name of a file containing a single ExpectationValues object with all expectation values of the operators contained in groups. If absent, variances are assumed to be maximal, i.e. 1. NOTE: YOU HAVE TO MAKE SURE THAT THE ORDER OF EXPECTATION VALUES MATCHES THE ORDER OF THE TERMS IN THE *GROUPED* TARGET QUBIT OPERATOR, OTHERWISE THIS FUNCTION WILL NOT RETURN THE CORRECT RESULT. """ grouped_operator = load_qubit_operator_set(groups) if expectation_values is not None: expecval = load_expectation_values(expectation_values) else: expecval = None K_coeff, nterms, frame_meas = estimate_nmeas_for_frames( grouped_operator, expecval) save_nmeas_estimate( nmeas=K_coeff, nterms=nterms, frame_meas=frame_meas, filename="hamiltonian_analysis.json", )
def expectation_values_from_rdms_for_qubitoperator_list( interactionrdm: str, qubit_operator_list: str, sort_terms: bool = False): """Computes expectation values of Pauli strings in a list of QubitOperator given a fermionic InteractionRDM from OpenFermion. All the expectation values for the operators in the list are written to file in a single ExpectationValues object in the same order the operators came in. ARGS: interactionrdm (str): The name of the file containing the interaction RDM to use for the expectation values computation qubitoperator_list (str): The name of the file containing the list of qubit operators to compute the expectation values for in the form of OpenFermion QubitOperator objects sort_terms (bool): whether or not each input qubit operator needs to be sorted before calculating expectations """ operator_list = load_qubit_operator_set(qubit_operator_list) rdms = load_interaction_rdm(interactionrdm) expecval = get_expectation_values_from_rdms_for_qubitoperator_list( rdms, operator_list, sort_terms=sort_terms) save_expectation_values(expecval, "expectation_values.json")