/// <summary> /// Validates the program for the quantum machine target. /// </summary> /// <param name="machine">The quantum machine target.</param> /// <param name="entryPoint">The program entry point.</param> /// <param name="input">The program input.</param> /// <typeparam name="TIn">The input type.</typeparam> /// <typeparam name="TOut">The output type.</typeparam> /// <returns>The exit code.</returns> private static int Validate <TIn, TOut>(IQuantumMachine machine, IEntryPoint <TIn, TOut> entryPoint, TIn input) { var(isValid, message) = machine.Validate(entryPoint.Info, input); Console.WriteLine(isValid ? "✔️ The program is valid!" : "❌ The program is invalid."); if (!string.IsNullOrWhiteSpace(message)) { Console.WriteLine(); Console.WriteLine(message); } return(isValid ? 0 : 1); }
public Task <IQuantumMachineJob> SubmitAsync(IQuantumMachine machine, AzureSubmissionContext submissionContext) { var parameterTypes = new List <Type>(); var parameterValues = new List <object>(); foreach (var parameter in OperationInfo.RoslynParameters) { if (!submissionContext.InputParameters.ContainsKey(parameter.Name)) { throw new ArgumentException($"Required parameter {parameter.Name} was not specified."); } string rawParameterValue = submissionContext.InputParameters[parameter.Name]; try { var parameterValue = submissionContext.InputParameters.DecodeParameter(parameter.Name, type: parameter.ParameterType); if (parameterValue != null) { parameterTypes.Add(parameter.ParameterType); parameterValues.Add(parameterValue); } } catch (Exception e) { throw new ArgumentException($"The value {rawParameterValue} provided for parameter {parameter.Name} could not be converted to the expected type: {e.Message}"); } } var entryPointInput = parameterValues.Count switch { 0 => QVoid.Instance, 1 => parameterValues.Single(), _ => InputType.GetConstructor(parameterTypes.ToArray()).Invoke(parameterValues.ToArray()) }; // Find and invoke the method on IQuantumMachine that is declared as: // Task<IQuantumMachineJob> SubmitAsync<TInput, TOutput>(EntryPointInfo<TInput, TOutput> info, TInput input, SubmissionContext context) var submitMethod = typeof(IQuantumMachine) .GetMethods() .Single(method => method.Name == "SubmitAsync" && method.IsGenericMethodDefinition && method.GetParameters().Length == 3 && method.GetParameters()[0].ParameterType.GetGenericTypeDefinition() == EntryPointInfo.GetType().GetGenericTypeDefinition() && method.GetParameters()[1].ParameterType.IsGenericMethodParameter && method.GetParameters()[2].ParameterType == typeof(IQuantumMachineSubmissionContext)) .MakeGenericMethod(new Type[] { InputType, OutputType }); var submitParameters = new object[] { EntryPointInfo, entryPointInput, submissionContext }; return((Task <IQuantumMachineJob>)submitMethod.Invoke(machine, submitParameters)); } }