Пример #1
0
        /// <summary>
        /// Simulates the entry point.
        /// </summary>
        /// <param name="settings">The driver settings.</param>
        /// <param name="entryPoint">The entry point.</param>
        /// <param name="parseResult">The command-line parsing result.</param>
        /// <param name="simulator">The simulator to use.</param>
        /// <returns>The exit code.</returns>
        internal static async Task <int> Simulate(
            DriverSettings settings, IEntryPoint <TIn, TOut> entryPoint, ParseResult parseResult, string simulator)
        {
            if (simulator == settings.ResourcesEstimatorName)
            {
                var resourcesEstimator = new ResourcesEstimator();
                await resourcesEstimator.Run <TCallable, TIn, TOut>(entryPoint.CreateArgument(parseResult));

                Console.WriteLine(resourcesEstimator.ToTSV());
            }
            else
            {
                var(isCustom, createSimulator) =
                    simulator == settings.QuantumSimulatorName
                        ? (false, () => new QuantumSimulator())
                        : simulator == settings.ToffoliSimulatorName
                        ? (false, new Func <IOperationFactory>(() => new ToffoliSimulator()))
                        : (true, entryPoint.CreateDefaultCustomSimulator);
                if (isCustom && simulator != entryPoint.DefaultSimulatorName)
                {
                    DisplayCustomSimulatorError(simulator);
                    return(1);
                }
                await RunSimulator(entryPoint, parseResult, createSimulator);
            }
            return(0);
        }
Пример #2
0
        /// <summary>
        /// Simulates the entry point.
        /// </summary>
        /// <param name="settings">The driver settings.</param>
        /// <param name="entryPoint">The entry point.</param>
        /// <param name="parseResult">The command-line parsing result.</param>
        /// <param name="simulator">The simulator to use.</param>
        /// <returns>The exit code.</returns>
        internal static async Task <int> Simulate(
            DriverSettings settings, IEntryPoint <TIn, TOut> entryPoint, ParseResult parseResult, string simulator)
        {
            if (simulator == settings.ResourcesEstimatorName)
            {
                // Force the explicit load of the QSharp.Core assembly so that the ResourcesEstimator
                // can discover it dynamically at runtime and override the defined callables.
                var coreAssemblyName =
                    (from aName in entryPoint.GetType().Assembly.GetReferencedAssemblies()
                     where aName.Name == "Microsoft.Quantum.QSharp.Core"
                     select aName).First();
                var coreAssembly = Assembly.Load(coreAssemblyName.FullName);

                var resourcesEstimator = new ResourcesEstimator(coreAssembly);
                await resourcesEstimator.Run <TCallable, TIn, TOut>(entryPoint.CreateArgument(parseResult));

                Console.WriteLine(resourcesEstimator.ToTSV());
            }
            else
            {
                var(isCustom, createSimulator) =
                    simulator == settings.QuantumSimulatorName
                        ? (false, () => new QuantumSimulator())
                        : simulator == settings.ToffoliSimulatorName
                        ? (false, new Func <IOperationFactory>(() => new ToffoliSimulator()))
                        : (true, entryPoint.CreateDefaultCustomSimulator);
                if (isCustom && simulator != entryPoint.DefaultSimulatorName)
                {
                    DisplayCustomSimulatorError(simulator);
                    return(1);
                }
                await RunSimulator(entryPoint, parseResult, createSimulator);
            }
            return(0);
        }
Пример #3
0
        /// <summary>
        /// Runs the entry point on a simulator and displays its return value.
        /// </summary>
        /// <param name="entryPoint">The entry point.</param>
        /// <param name="parseResult">The command-line parsing result.</param>
        /// <param name="createSimulator">A function that creates an instance of the simulator to use.</param>
        private static async Task RunSimulator(
            IEntryPoint <TIn, TOut> entryPoint, ParseResult parseResult, Func <IOperationFactory> createSimulator)
        {
            var simulator = createSimulator();

            try
            {
                var value = await simulator.Run <TCallable, TIn, TOut>(entryPoint.CreateArgument(parseResult));

                if (!(value is QVoid))
                {
                    Console.WriteLine(value);
                }
            }
            finally
            {
                if (simulator is IDisposable disposable)
                {
                    disposable.Dispose();
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Submits the entry point to Azure Quantum.
        /// </summary>
        /// <param name="entryPoint">The entry point.</param>
        /// <param name="parseResult">The command-line parsing result.</param>
        /// <param name="settings">The submission settings.</param>
        /// <typeparam name="TIn">The entry point's argument type.</typeparam>
        /// <typeparam name="TOut">The entry point's return type.</typeparam>
        /// <returns>The exit code.</returns>
        internal static async Task <int> Submit <TIn, TOut>(
            IEntryPoint <TIn, TOut> entryPoint, ParseResult parseResult, AzureSettings settings)
        {
            if (settings.Verbose)
            {
                Console.WriteLine(settings);
                Console.WriteLine();
            }

            var machine = CreateMachine(settings);

            if (machine is null)
            {
                DisplayWithColor(ConsoleColor.Red, Console.Error,
                                 $"The target '{settings.Target}' was not recognized.");
                return(1);
            }

            var input = entryPoint.CreateArgument(parseResult);

            return(settings.DryRun
                ? Validate(machine, entryPoint, input)
                : await SubmitJob(machine, entryPoint, input, settings));
        }