Ejemplo n.º 1
0
        private static void SingleResourceTestNoCost <TypeQop>(
            RunParameterizedQop runner,
            ReaderWriterLock locker,
            int n,
            int m,
            bool isControlled,
            string filename,
            bool full_depth)
        {
            QCTraceSimulator estimator = GetTraceSimulator(full_depth); // construct simulator object

            // we must generate a new simulator in each round, to clear previous estimates
            var res = runner(estimator, n, isControlled, m).Result; // run test

            // Get results

            // Create string of a row of parameters
            string thisCircuitCosts = DisplayCSV.CSV(estimator.ToCSV(), typeof(TypeQop).FullName, false, string.Empty, false, string.Empty);

            // add the row to the string of the csv
            thisCircuitCosts += $"{n}, {m}";
            try
            {
                locker.AcquireWriterLock(int.MaxValue); // absurd timeout value
                System.IO.File.AppendAllText(filename, thisCircuitCosts);
            }
            finally
            {
                locker.ReleaseWriterLock();
            }
        }
Ejemplo n.º 2
0
        private static void SingleTwoArgResourceTest <TypeQop>(
            RunTwoArgQop runner,
            ReaderWriterLock locker,
            int n,
            int[] ms,
            bool isControlled,
            string filename,
            bool full_depth)
        {
            // Iterate through values of the second parameter
            foreach (int m in ms)
            {
                QCTraceSimulator estimator = GetTraceSimulator(full_depth); // construct simulator object

                // we must generate a new simulator in each round, to clear previous estimates
                var res = runner(estimator, n, m, isControlled).Result; // run test

                // Get results
                var roundDepth  = estimator.GetMetric <TypeQop>(MetricsNames.DepthCounter.Depth);
                var roundTGates = estimator.GetMetric <TypeQop>(PrimitiveOperationsGroupsNames.T);


                // Create string of a row of parameters
                string thisCircuitCosts = DisplayCSV.CSV(estimator.ToCSV(), typeof(TypeQop).FullName, false, string.Empty, false, string.Empty);

                // add the row to the string of the csv
                thisCircuitCosts += $"{n}, {m}";
                try
                {
                    locker.AcquireWriterLock(int.MaxValue); // absurd timeout value
                    System.IO.File.AppendAllText(filename, thisCircuitCosts);
                }
                finally
                {
                    locker.ReleaseWriterLock();
                }
            }
        }
Ejemplo n.º 3
0
        private static void ParameterizedResourceTestSingleThreaded <TypeQop>(
            RunParameterizedQop runner,
            int[] ns,
            bool isControlled,
            bool isAmortized,
            string filename,
            bool full_depth,
            int[] minParameters,
            int[] maxParameters)
        {
            if (full_depth)
            {
                filename += "-all-gates";
            }

            if (isControlled)
            {
                filename += "-controlled";
            }

            filename += ".csv";

            // Create table headers if file does not already exist
            if (!System.IO.File.Exists(filename))
            {
                string estimation = string.Empty;
                estimation += " operation, CNOT count, 1-qubit Clifford count, T count, R count, M count, ";
                if (full_depth)
                {
                    estimation += "Full depth, ";
                }
                else
                {
                    estimation += "T depth, ";
                }

                estimation += "initial width, extra width, comment, size, parameter";
                System.IO.File.WriteAllText(filename, estimation);
            }

            var bestParameter = minParameters[0];

            for (int i = 0; i < ns.Length; i++)
            {
                // Starts a thread for each value in ns.
                // Each thread will independently search for an optimal size.
                var bestCost = 9223372036854775807.0;

                // Iterate through values of the second parameter
                for (int j = bestParameter; j < maxParameters[i]; j++)
                {
                    QCTraceSimulator estimator = GetTraceSimulator(full_depth); // construct simulator object

                    // we must generate a new simulator in each round, to clear previous estimates
                    var res = runner(estimator, ns[i], isControlled, j).Result; // run test

                    // Get results
                    var roundCost = 0.0;
                    if (DriverParameters.MinimizeDepthCostMetric)
                    { // depth
                        roundCost = estimator.GetMetric <TypeQop>(MetricsNames.DepthCounter.Depth);
                    }
                    else
                    {
                        roundCost = estimator.GetMetric <TypeQop>(PrimitiveOperationsGroupsNames.T);
                    }

                    // If amortized, we divide out the cost of this round
                    if (isAmortized)
                    {
                        roundCost = roundCost / j;
                    }

                    // Create string of a row of parameters
                    string thisCircuitCosts = DisplayCSV.CSV(estimator.ToCSV(), typeof(TypeQop).FullName, false, string.Empty, false, string.Empty);

                    // add the row to the string of the csv
                    thisCircuitCosts += $"{ns[i]}, {j}";

                    System.IO.File.AppendAllText(filename, thisCircuitCosts);

                    // Breaks if it's reached the minimum in both metrics
                    // Assumes the metrics are convex and increasing in n
                    if (roundCost > bestCost)
                    {
                        break;
                    }
                    else if (roundCost < bestCost)
                    {
                        bestCost      = roundCost;
                        bestParameter = j;
                    }
                }
            }
        }
Ejemplo n.º 4
0
        private static void SingleParameterizedResourceTest <TypeQop>(
            RunParameterizedQop runner,
            ReaderWriterLock locker,
            int n,
            int minParameter,
            int maxParameter,
            bool isControlled,
            string filename,
            bool full_depth,
            bool isAmortized)
        {
            // Track best cost
            var bestDepth  = 9223372036854775807.0;
            var bestTGates = 9223372036854775807.0;

            // Iterate through values of the second parameter
            for (int j = minParameter; j < maxParameter; j++)
            {
                QCTraceSimulator estimator = GetTraceSimulator(full_depth); // construct simulator object

                // we must generate a new simulator in each round, to clear previous estimates
                var res = runner(estimator, n, isControlled, j).Result; // run test

                // Get results
                var roundDepth  = estimator.GetMetric <TypeQop>(MetricsNames.DepthCounter.Depth);
                var roundTGates = estimator.GetMetric <TypeQop>(PrimitiveOperationsGroupsNames.T);

                // If amortized, we divide out the cost of this round
                if (isAmortized)
                {
                    roundDepth  = roundDepth / j;
                    roundTGates = roundTGates / j;
                }

                // Create string of a row of parameters
                string thisCircuitCosts = DisplayCSV.CSV(estimator.ToCSV(), typeof(TypeQop).FullName, false, string.Empty, false, string.Empty);

                // add the row to the string of the csv
                thisCircuitCosts += $"{n}, {j}";
                try
                {
                    locker.AcquireWriterLock(int.MaxValue); // absurd timeout value
                    System.IO.File.AppendAllText(filename, thisCircuitCosts);
                }
                finally
                {
                    locker.ReleaseWriterLock();
                }

                // Breaks if it's reached the minimum in both metrics
                // Assumes the metrics are convex
                if (roundDepth >= bestDepth && roundTGates >= bestTGates)
                {
                    break;
                }
                else
                {
                    if (roundDepth < bestDepth)
                    {
                        bestDepth = roundDepth;
                    }

                    if (roundTGates < bestTGates)
                    {
                        bestTGates = roundTGates;
                    }
                }
            }
        }