// Converts JSON to data format with existing Q# pipeline // Input: JSON file // Output: PauliHamiltonian for later use in Q# pipeline public static PauliHamiltonian CallQSharpPipeline( JObject OptimizedHamiltonian, int index ) { // make Hamiltonian to be output var hamiltonian = new PauliHamiltonian(); // Pull the interactions from a specific round var interactionList = OptimizedHamiltonian["terms"]["interactions"][index]; // make the conversion function Func <FermionTerm, TermType.Fermion, double, IEnumerable <(PauliTerm, PauliTermValue)> > conversion = (fermionTerm, termType, coeff) => (fermionTerm.ToJordanWignerPauliTerms(termType, coeff)); // pull the interactions and parse them into the PauliHamiltonian format foreach (var interaction in interactionList) { var(term, type, coeffData) = ConvertToFermionFormat(interaction); hamiltonian.AddRange(conversion(term, type, coeffData)); // if ((string)interaction["type"] == "Identity") // { // // We need to still add the term if it's an identity term // hamiltonian. // } else { // var (term, type, coeff) = ConvertToFermionFormat(interaction); // hamiltonian.AddRange(conversion(term, type, coeff)); // } } // assume that the indices are simply an ascending 0-indexed list List <int> indices = new List <int>(); for (int i = 0; i < interactionList.Count(); i++) { indices.Add(i); } hamiltonian.SystemIndices = new HashSet <int>(indices); // hamiltonian.SystemIndices = new HashSet<int>(sourceHamiltonian.SystemIndices.ToList()); return(hamiltonian); }
/// <summary> /// Method for constructing a Pauli Hamiltonian from a fermion Hamiltonina. /// </summary> /// <param name="sourceHamiltonian">Input orbital integral Hamiltonian.</param> /// <param name="encoding">Identifies how the terms should be encoded on the qubits.</param> /// <returns>Fermion Hamiltonian constructed from orbital integrals.</returns> public static PauliHamiltonian ToPauliHamiltonian( this FermionHamiltonian sourceHamiltonian, QubitEncoding encoding = QubitEncoding.JordanWigner) { var nFermions = sourceHamiltonian.SystemIndices.Max() + 1; var hamiltonian = new PauliHamiltonian(); Func <FermionTerm, TermType.Fermion, double, IEnumerable <(PauliTerm, PauliTermValue)> > conversion = (fermionTerm, termType, coeff) => (fermionTerm.ToJordanWignerPauliTerms(termType, coeff)); foreach (var termType in sourceHamiltonian.Terms) { foreach (var term in termType.Value) { hamiltonian.AddRange(conversion(term.Key, termType.Key, term.Value.Value)); } } // Create a copy of fermion indices hash set. hamiltonian.SystemIndices = new HashSet <int>(sourceHamiltonian.SystemIndices.ToList()); return(hamiltonian); }