Exemplo n.º 1
0
        /// <summary>
        ///     Simulates an operation given a string with its name and a JSON
        ///     encoding of its arguments.
        /// </summary>
        public async Task <ExecutionResult> RunAsync(string input, IChannel channel)
        {
            var inputParameters = ParseInputParameters(input, firstParameterInferredName: ParameterNameOperationName);

            var name   = inputParameters.DecodeParameter <string>(ParameterNameOperationName);
            var symbol = SymbolResolver.Resolve(name) as IQSharpSymbol;

            if (symbol == null)
            {
                throw new InvalidOperationException($"Invalid operation name: {name}");
            }

            var maxNQubits = 0L;

            using var qsim = new QuantumSimulator()
                             .WithJupyterDisplay(channel, ConfigurationSource)
                             .WithStackTraceDisplay(channel);
            qsim.OnDisplayableDiagnostic += channel.Display;
            qsim.AfterAllocateQubits     += (args) =>
            {
                maxNQubits = System.Math.Max(qsim.QubitManager.AllocatedQubitsCount, maxNQubits);
            };
            var stopwatch = Stopwatch.StartNew();
            var value     = await symbol.Operation.RunAsync(qsim, inputParameters);

            stopwatch.Stop();
            var result = value.ToExecutionResult();

            (Monitor as PerformanceMonitor)?.ReportSimulatorPerformance(new SimulatorPerformanceArgs(
                                                                            simulatorName: qsim.GetType().FullName,
                                                                            nQubits: (int)maxNQubits,
                                                                            duration: stopwatch.Elapsed
                                                                            ));
            return(result);
        }