r"""Running SPAM error mitigation via Qermit MitRes on measurement of expectation value for H2.""" from inquanto.protocols import gen_UnCorrelated_SPAM_MitRes from inquanto.ansatzes import MultiConfigurationStateSparse from inquanto.express import get_noisy_backend, load_h5 from inquanto.protocols import PauliAveraging # get an example system from express database, get fermionic objects, and qubit encode h2 = load_h5("h2_sto3g_symmetry.h5", as_tuple=True) print("HF energy:", h2.energy_hf) hamiltonian = h2.hamiltonian_operator state = h2.hf_state qstate = state.qubit_encode() kernel = hamiltonian.qubit_encode() # prepare a compact chemical ansatz, just Hartree-Fock here as an example (note it has no symbols) ansatz = MultiConfigurationStateSparse(qstate) noisy_backend = get_noisy_backend(ansatz.n_qubits) # SPAM error mitigation. spam = gen_UnCorrelated_SPAM_MitRes( backend=noisy_backend, calibration_shots=1000, num_calibration_qubits=ansatz.n_qubits, ) # instantiate and build the protocol. protocol = PauliAveraging( noisy_backend, shots_per_circuit=1000, ) protocol.build( {}, ansatz, kernel, noise_mitigation=None, ).compile_circuits() # run the protocol via SPAM MitRes protocol.run_mitres(spam) exp_val = protocol.evaluate_expectation_value(ansatz, kernel) print("Energy from SPAM-mitigated measurement:", exp_val)