コード例 #1
0
        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);
                }
            }
        }
コード例 #2
0
        static void Main(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("You did not provide the gatefile path, number of samples, and precision.");
            }
            else
            {
                // Extract important command line arguments
                string JSONPath        = args[0];
                int    numberOfSamples = Int16.Parse(args[1]);
                var    nBitsPrecision  = Int64.Parse(args[2]);

                if (File.Exists(JSONPath))
                {
                    #region Extract Fermion Terms
                    string raw_JSON = System.IO.File.ReadAllText(JSONPath);
                    var    output   = JObject.Parse(raw_JSON);

                    // get the constants specifically
                    var constants = output["constants"];

                    // get important constants
                    float  trotterStepSize = (float)constants["trotterStep"];
                    double rescaleFactor   = 1.0 / trotterStepSize;
                    float  energyOffset    = (float)constants["energyOffset"];
                    int    nSpinOrbitals   = (int)constants["nSpinOrbitals"];
                    int    trotterOrder    = (int)constants["trotterOrder"];
                    #endregion

                    #region Convert to Q# Format
                    // var test = Auxiliary.ProduceLowLevelTerms(output);
                    // Console.WriteLine(test);
                    // var data = Auxiliary.ProduceCompleteHamiltonian(output);
                    // var qSharpData = Auxiliary.CreateQSharpFormat(output);
                    var data = Auxiliary.ProduceCompleteHamiltonian(output);
                    #endregion

                    #region Simulate Optimized Fermion Terms
                    using (var qsim = new QuantumSimulator(randomNumberGeneratorSeed: 42))
                    {
                        // keep track of the running total of the energy to produce the average energy amount
                        var runningSum = 0.0;

                        // iterate over the sample size
                        for (int i = 0; i < numberOfSamples; i++)
                        {
                            var(phaseEst, energyEst) = EstimateEnergyLevel.Run(qsim, data, nBitsPrecision).Result;
                            runningSum += energyEst;
                            Console.WriteLine($"Predicted energy: {energyEst}");
                        }

                        // Output to stdout
                        Console.WriteLine($"Average predicted energy: {runningSum / (float)numberOfSamples}");
                    }
                    #endregion

                    #region Produce Cost Estimates
                    var config = new QCTraceSimulatorConfiguration();
                    config.usePrimitiveOperationsCounter = true;
                    QCTraceSimulator estimator = new QCTraceSimulator(config);
                    ApplyTrotterOracleOnce.Run(estimator, data).Wait();
                    System.IO.Directory.CreateDirectory("_temp");
                    // System.IO.File.WriteAllLines("./_temp/_costEstimateOptimized.csv",estimator.ToCSV().Select(x => x.Key + " " + x.Value).ToArray());
                    foreach (var collectedData in estimator.ToCSV())
                    {
                        File.WriteAllText(
                            Path.Combine("./_temp/", $"CCNOTCircuitsMetrics.{collectedData.Key}.csv"),
                            collectedData.Value);
                    }
                    #endregion
                }
            }
        }