// Perform quantum simulation of Hamiltonian at desired bond length and // return estimate of energy. internal static (Double, Double) GetSimulationResult(int idxBond, string inputState = "Greedy") { // Choose the desired hamiltonian indexed by `idx`. var hamiltonian = hamiltonianData.ElementAt(idxBond); // Bond length conversion from Bohr radius to Angstrom var bondLength = Double.Parse(bondLengths[idxBond]); // Create Jordan-Wigner encodinf of Hamiltonian. var jordanWignerEncoding = JordanWignerEncoding.Create(hamiltonian); // Invoke quantum simulator and run `GetEnergyByTrotterization` in the first // molecular Hydrogen sample. using (var qSim = new QuantumSimulator()) { var qSharpData = jordanWignerEncoding.QSharpData(inputState); Console.WriteLine($"Estimating at bond length {idxBond}:"); var(phaseEst, energyEst) = GetEnergyByTrotterization.Run(qSim, qSharpData, bitsOfPrecision, trotterStepSize, IntegratorOrder).Result; return(bondLength, energyEst); } }
internal static (Double, Double) GetSimulationResult(int idxBond, string inputState = "Greedy") { var hamiltonian = hamiltonianData.ElementAt(idxBond); var bondLength = Double.Parse(bondLengths[idxBond]); var jordanWignerEncoding = JordanWignerEncoding.Create(hamiltonian); using (var qSim = new QuantumSimulator()) { var qSharpData = jordanWignerEncoding.QSharpData(inputState); Console.WriteLine($"Estimating at {bondLength} A; {idxBond}-th bond length:"); var(phaseEst, energyEst) = GetEnergyByTrotterization.Run(qSim, qSharpData, bitsOfPrecision, trotterStepSize, IntegratorOrder).Result; return(bondLength, energyEst); } }
// For each Hamiltonian, internal static (Double, Double) GetSimulationResult(int idxBond) { // Choose the desired problem indexed by `idx`. var problem = problemData.ElementAt(idxBond); // Create fermion representation of Hamiltonian. var fermionHamiltonian = problem.OrbitalIntegralHamiltonian .ToFermionHamiltonian(IndexConvention.UpDown); // Create Jordan–Wigner encoding of Hamiltonian. var jordanWignerEncoding = fermionHamiltonian.ToPauliHamiltonian(Paulis.QubitEncoding.JordanWigner); // Bond length conversion from Bohr radius to Angstrom double bondLength = Double.Parse(problem.MiscellaneousInformation.Split(new char[] { ',' }).Last()) * 0.5291772; // Create input wavefunction. var wavefunction = fermionHamiltonian.CreateHartreeFockState(nElectrons: 2); // Choose bits of precision in quantum phase estimation Int64 bitsOfPrecision = 7; // Choose the Trotter step size. Double trotterStepSize = 1.0; // Choose the Trotter integrator order Int64 trotterOrder = 1; // Invoke quantum simulator and run `GetEnergyByTrotterization` in the first // molecular Hydrogen sample. using (var qSim = new QuantumSimulator()) { // Package hamiltonian and wavefunction data into a format // consumed by Q#. var qSharpData = QSharpFormat.Convert.ToQSharpFormat( jordanWignerEncoding.ToQSharpFormat(), wavefunction.ToQSharpFormat()); System.Console.WriteLine($"Estimating at bond length {idxBond}:"); // Loop if excited state energy is obtained. var energyEst = 0.0; do { var(phaseEst, energyEstTmp) = GetEnergyByTrotterization.Run(qSim, qSharpData, bitsOfPrecision, trotterStepSize, trotterOrder).Result; energyEst = energyEstTmp; } while (energyEst > -0.8); return(bondLength, energyEst); } }
// Perform quantum simulation of Hamiltonian at desired bond length and // return estimate of energy. internal static (Double, Double) GetSimulationResult(int idxBond, string inputState = "Greedy") { // Choose the desired Hamiltonian indexed by `idx`. var problem = problemData.ElementAt(idxBond); // Bond length conversion from Bohr radius to Angstrom var bondLength = Double.Parse(bondLengths[idxBond]); // Create fermion representation of Hamiltonian var fermionHamiltonian = problem .OrbitalIntegralHamiltonian .ToFermionHamiltonian(IndexConvention.UpDown); // Crete Pauli representation of Hamiltonian using // the Jordan–Wigner encoding. var pauliHamiltonian = fermionHamiltonian .ToPauliHamiltonian(Paulis.QubitEncoding.JordanWigner); // Create input wavefunction. var wavefunction = inputState == "Greedy" ? fermionHamiltonian.CreateHartreeFockState(problem.NElectrons) : problem.Wavefunctions[inputState].ToIndexing(IndexConvention.UpDown); // Package Hamiltonian and wavefunction data into a format // consumed by Q#. var qSharpData = QSharpFormat.Convert.ToQSharpFormat( pauliHamiltonian.ToQSharpFormat(), wavefunction.ToQSharpFormat()); // Invoke quantum simulator and run `GetEnergyByTrotterization` in the first // molecular Hydrogen sample. using (var qSim = new QuantumSimulator()) { Console.WriteLine($"Estimating at bond length {idxBond}:"); var(phaseEst, energyEst) = GetEnergyByTrotterization.Run(qSim, qSharpData, bitsOfPrecision, trotterStepSize, IntegratorOrder).Result; return(bondLength, energyEst); } }
// For each Hamiltonian, internal static (Double, Double) GetSimulationResult(int idxBond) { // Choose the desired hamiltonian indexed by `idx`. FermionHamiltonian hamiltonian = hamiltonianData.ElementAt(idxBond); // Bond length conversion from Bohr radius to Angstrom double bondLength = Double.Parse(hamiltonian.MiscellaneousInformation.Split(new char[] { ',' }).Last()) * 0.5291772; // Set the number of occupied spin-orbitals to 2. hamiltonian.NElectrons = 2; // Create Jordan-Wigner encodinf of Hamiltonian. JordanWignerEncoding jordanWignerEncoding = JordanWignerEncoding.Create(hamiltonian); // Choose bits of precision in quantum phase estimation Int64 bitsOfPrecision = 7; // Choose the Trotter step size. Double trotterStepSize = 1.0; // Choose the Trotter integrator order Int64 trotterOrder = 1; // Invoke quantum simulator and run `GetEnergyByTrotterization` in the first // molecular Hydrogen sample. using (var qSim = new QuantumSimulator()) { var qSharpData = jordanWignerEncoding.QSharpData(); System.Console.WriteLine($"Estimating at bond length {idxBond}:"); // Loop if excited state energy is obtained. var energyEst = 0.0; do { var(phaseEst, energyEstTmp) = GetEnergyByTrotterization.Run(qSim, qSharpData, bitsOfPrecision, trotterStepSize, trotterOrder).Result; energyEst = energyEstTmp; } while (energyEst > -0.8); return(bondLength, energyEst); } }
public static Double SetUpSimulation( string filename, Config configuration, JordanWignerEncodingData qSharpData, double trotterStep, int trotterOrder, int bits, string testName = "Default" ) { // We specify the bits of precision desired in the phase estimation // algorithm // We specify the step-size of the simulated time-evolution // Choose the Trotter integrator order using (var qsim = new QuantumSimulator(randomNumberGeneratorSeed: GenerateSeed(testName))) { // EstimateEnergyByTrotterization var(phaseEst, energyEst) = GetEnergyByTrotterization.Run(qsim, qSharpData, bits, trotterStep, trotterOrder).Result; return(energyEst); } }
static void Main(string[] args) { ////////////////////////////////////////////////////////////////////////// // Introduction ////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// // In this example, we will create a spin-orbital representation of the molecular // Hydrogen Hamiltonian `H`, given overlap coefficients for its one- and // two - electron integrals. // We when perform quantum phase estimation to obtain an estimate of // the molecular Hydrogen ground state energy. #region Building the Hydrogen Hamiltonian through orbital integrals // One of the simplest representations of Hydrogen uses only two // molecular orbitals indexed by `0` and `1`. var nOrbitals = 2; // This representation also has two occupied spin-orbitals. var nElectrons = 2; // The Coulomb repulsion energy between nuclei is var energyOffset = 0.713776188; // One-electron integrals are listed below // <0|H|0> = -1.252477495 // <1|H|1> = -0.475934275 // Two-electron integrals are listed below // <00|H|00> = 0.674493166 // <01|H|01> = 0.181287518 // <01|H|10> = 0.663472101 // <11|H|11> = 0.697398010 // Note that orbitals are assumed to be real. Moreover, indistinguishability // of electrons means that the following integrals are equal. // <PQ|H|RS> = <PR|H|QS> = <SQ|H|RP> = <SR|H|QP> // = <QP|H|SR> = <RP|H|SQ> = <QS|H|PR> = <RS|H|PQ> // Thus it suffices to specify just any one of these terms from each symmetry // group. // These orbital integrals are represented using the OrbitalIntegral // data structure. var orbitalIntegrals = new OrbitalIntegral[] { new OrbitalIntegral(new[] { 0, 0 }, -1.252477495), new OrbitalIntegral(new[] { 1, 1 }, -0.475934275), new OrbitalIntegral(new[] { 0, 0, 0, 0 }, 0.674493166), new OrbitalIntegral(new[] { 0, 1, 0, 1 }, 0.181287518), new OrbitalIntegral(new[] { 0, 1, 1, 0 }, 0.663472101), new OrbitalIntegral(new[] { 1, 1, 1, 1 }, 0.697398010), // Add the identity term new OrbitalIntegral(new int[] { }, energyOffset) }; // We initialize a fermion Hamiltonian data structure and add terms to it var fermionHamiltonian = new OrbitalIntegralHamiltonian(orbitalIntegrals).ToFermionHamiltonian(); // These orbital integral terms are automatically expanded into // spin-orbitals. We may print the Hamiltonian to see verify what it contains. Console.WriteLine("----- Print Hamiltonian"); Console.Write(fermionHamiltonian); Console.WriteLine("----- End Print Hamiltonian \n"); // We also need to create an input quantum state to this Hamiltonian. // Let us use the Hartree–Fock state. var fermionWavefunction = fermionHamiltonian.CreateHartreeFockState(nElectrons); #endregion #region Jordan–Wigner representation // The Jordan–Wigner encoding converts the fermion Hamiltonian, // expressed in terms of Fermionic operators, to a qubit Hamiltonian, // expressed in terms of Pauli matrices. This is an essential step // for simulating our constructed Hamiltonians on a qubit quantum // computer. Console.WriteLine("----- Creating Jordan–Wigner encoding"); var jordanWignerEncoding = fermionHamiltonian.ToPauliHamiltonian(Paulis.QubitEncoding.JordanWigner); Console.WriteLine("----- End Creating Jordan–Wigner encoding \n"); // Print the Jordan–Wigner encoded Hamiltonian to see verify what it contains. Console.WriteLine("----- Print Hamiltonian"); Console.Write(jordanWignerEncoding); Console.WriteLine("----- End Print Hamiltonian \n"); #endregion #region Performing the simulation // We are now ready to run a quantum simulation of molecular Hydrogen. // We will use this to obtain an estimate of its ground state energy. // Here, we make an instance of the simulator used to run our Q# code. using (var qsim = new QuantumSimulator()) { // This Jordan–Wigner data structure also contains a representation // of the Hamiltonian and wavefunction made for consumption by the Q# algorithms. var qSharpHamiltonianData = jordanWignerEncoding.ToQSharpFormat(); var qSharpWavefunctionData = fermionWavefunction.ToQSharpFormat(); var qSharpData = QSharpFormat.Convert.ToQSharpFormat(qSharpHamiltonianData, qSharpWavefunctionData); // We specify the bits of precision desired in the phase estimation // algorithm var bits = 7; // We specify the step-size of the simulated time-evolution var trotterStep = 0.4; // Choose the Trotter integrator order Int64 trotterOrder = 1; // As the quantum algorithm is probabilistic, let us run a few trials. // This may be compared to true value of Console.WriteLine("Exact molecular Hydrogen ground state energy: -1.137260278.\n"); Console.WriteLine("----- Performing quantum energy estimation by Trotter simulation algorithm"); for (int i = 0; i < 5; i++) { // EstimateEnergyByTrotterization // Name should make clear that it does it by trotterized var(phaseEst, energyEst) = GetEnergyByTrotterization.Run(qsim, qSharpData, bits, trotterStep, trotterOrder).Result; Console.WriteLine($"Rep #{i+1}/5: Energy estimate: {energyEst}; Phase estimate: {phaseEst}"); } Console.WriteLine("----- End Performing quantum energy estimation by Trotter simulation algorithm\n"); Console.WriteLine("----- Performing quantum energy estimation by Qubitization simulation algorithm"); for (int i = 0; i < 1; i++) { // EstimateEnergyByTrotterization // Name should make clear that it does it by trotterized var(phaseEst, energyEst) = GetEnergyByQubitization.Run(qsim, qSharpData, bits).Result; Console.WriteLine($"Rep #{i+1}/1: Energy estimate: {energyEst}; Phase estimate: {phaseEst}"); } Console.WriteLine("----- End Performing quantum energy estimation by Qubitization simulation algorithm\n"); } Console.WriteLine("Press Enter to continue..."); if (System.Diagnostics.Debugger.IsAttached) { Console.ReadLine(); } #endregion }
static void Main(string[] args) { // This is the name of the file we want to load if (args.Length < 6) { Console.WriteLine("Too few parameters provided!"); Console.WriteLine("Must provide the path to the YAML, input state label, precision, step size, trotter order, and sample size"); } else { string YAMLPath = args[0]; string inputState = $"|{args[1]}>"; int nBitsPrecision = Int16.Parse(args[2]); float trotterStepSize = float.Parse(args[3]); int trotterOrder = Int16.Parse(args[4]); var numberOfSamples = Int16.Parse(args[5]); Console.WriteLine($"Extracting the YAML from {YAMLPath}"); Console.WriteLine($"Input state: {inputState}"); Console.WriteLine($"Precision: {nBitsPrecision}"); Console.WriteLine($"Trotter step size: {trotterStepSize}"); Console.WriteLine($"Trotter order: {trotterOrder}"); Console.WriteLine($"Number of samples: {numberOfSamples}"); // This deserializes a Broombridge file, given its filename. var broombridge = Deserializers.DeserializeBroombridge(YAMLPath); // Note that the deserializer returns a list of `ProblemDescription` instances // as the file might describe multiple Hamiltonians. In this example, there is // only one Hamiltonian. So we use `.Single()`, which selects the only element of the list. var problem = broombridge.ProblemDescriptions.Single(); // This is a data structure representing the Jordan-Wigner encoding // of the Hamiltonian that we may pass to a Q# algorithm. // If no state is specified, the Hartree-Fock state is used by default. Console.WriteLine("Preparing Q# data format"); var qSharpData = problem.ToQSharpFormat(inputState); Console.WriteLine("----- Begin simulation -----"); using (var qsim = new QuantumSimulator(randomNumberGeneratorSeed: 42)) { // HelloQ.Run(qsim).Wait(); var runningSum = 0.0; for (int i = 0; i < numberOfSamples; i++) { var(phaseEst, energyEst) = GetEnergyByTrotterization.Run(qsim, qSharpData, nBitsPrecision, trotterStepSize, trotterOrder).Result; Console.WriteLine(energyEst); runningSum += energyEst; } Console.WriteLine("----- End simulation -----"); Console.WriteLine($"Average energy estimate: {runningSum / (float)numberOfSamples}"); } var config = new QCTraceSimulatorConfiguration(); config.usePrimitiveOperationsCounter = true; QCTraceSimulator estimator = new QCTraceSimulator(config); ApplyTrotterOracleOnce.Run(estimator, qSharpData, trotterStepSize, trotterOrder).Wait(); System.IO.Directory.CreateDirectory("_temp"); // var csvData = estimator.ToCSV(); // var csvStringData = String.Join(Environment.NewLine, csvData.Select(d => $"{d.Key};{d.Value};")); // System.IO.File.WriteAllText("./_temp/_costEstimateReference.csv", csvStringData); foreach (var collectedData in estimator.ToCSV()) { File.WriteAllText( Path.Combine("./_temp/", $"CCNOTCircuitsMetrics.{collectedData.Key}.csv"), collectedData.Value); } } }