Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Workspace"/> class.
        /// </summary>
        /// <param name="subscriptionId">The subscription identifier.</param>
        /// <param name="resourceGroupName">Name of the resource group.</param>
        /// <param name="workspaceName">Name of the workspace.</param>
        /// <param name="location">Azure region where the workspace was created.</param>
        /// <param name="credential">The credentials to connect to Azure. If not provided it defaults to an interactive DefaultAzureCredentials.</param>
        /// <param name="options">Options for the client library when communication with Azure Service..</param>
        public Workspace(
            string subscriptionId,
            string resourceGroupName,
            string workspaceName,
            string location,
            TokenCredential credential      = null,
            QuantumJobClientOptions options = default)
        {
            // Required parameters:
            Ensure.NotNullOrWhiteSpace(subscriptionId, nameof(subscriptionId));
            Ensure.NotNullOrWhiteSpace(resourceGroupName, nameof(resourceGroupName));
            Ensure.NotNullOrWhiteSpace(workspaceName, nameof(workspaceName));
            Ensure.NotNullOrWhiteSpace(location, nameof(location));

            // Optional parameters:
            credential ??= CredentialFactory.CreateCredential(CredentialType.Default, subscriptionId);

            // Make sure use the property Setter as we have some logic
            // tto apply here
            this.ClientOptions = options;

            this.ResourceGroupName = resourceGroupName;
            this.WorkspaceName     = workspaceName;
            this.SubscriptionId    = subscriptionId;
            this.Location          = location;

            this.Client = new QuantumJobClient(
                subscriptionId,
                resourceGroupName,
                workspaceName,
                location,
                credential,
                this.ClientOptions);
        }
Пример #2
0
 public Azure.Quantum.IWorkspace CreateWorkspace(string subscriptionId,
                                                 string resourceGroup,
                                                 string workspaceName,
                                                 string location,
                                                 TokenCredential credential,
                                                 QuantumJobClientOptions options) =>
 new Azure.Quantum.Workspace(
     subscriptionId: subscriptionId,
     resourceGroupName: resourceGroup,
     workspaceName: workspaceName,
     location: location,
     credential: credential,
     options: options);
Пример #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="QuantumJobClient"/> class.
        /// </summary>
        /// <param name="subscriptionId">The subscription identifier.</param>
        /// <param name="resourceGroupName">Name of the resource group.</param>
        /// <param name="workspaceName">Name of the workspace.</param>
        /// <param name="location">The location.</param>
        /// <param name="credential">The token credential.</param>
        /// <param name="options">The options.</param>
        public QuantumJobClient(
            string subscriptionId,
            string resourceGroupName,
            string workspaceName,
            string location,
            TokenCredential credential      = default,
            QuantumJobClientOptions options = default)
        {
            if (options == null)
            {
                options = new QuantumJobClientOptions();
            }

            var authPolicy  = new BearerTokenAuthenticationPolicy(credential, "https://quantum.microsoft.com");
            var diagnostics = new ClientDiagnostics(options);
            var pipeline    = HttpPipelineBuilder.Build(options, authPolicy);
            var endpoint    = new Uri($"https://{location}.quantum.azure.com");

            _jobs      = new JobsRestClient(diagnostics, pipeline, subscriptionId, resourceGroupName, workspaceName, endpoint);
            _providers = new ProvidersRestClient(diagnostics, pipeline, subscriptionId, resourceGroupName, workspaceName, endpoint);
            _quotas    = new QuotasRestClient(diagnostics, pipeline, subscriptionId, resourceGroupName, workspaceName, endpoint);
            _storage   = new StorageRestClient(diagnostics, pipeline, subscriptionId, resourceGroupName, workspaceName, endpoint);
        }
Пример #4
0
        private IWorkspace GetLiveWorkspace()
        {
            if (string.IsNullOrWhiteSpace(System.Environment.GetEnvironmentVariable("AZURE_QUANTUM_SUBSCRIPTION_ID")) ||
                string.IsNullOrWhiteSpace(System.Environment.GetEnvironmentVariable("AZURE_QUANTUM_WORKSPACE_RG")) ||
                string.IsNullOrWhiteSpace(System.Environment.GetEnvironmentVariable("AZURE_QUANTUM_WORKSPACE_NAME")) ||
                string.IsNullOrWhiteSpace(System.Environment.GetEnvironmentVariable("AZURE_QUANTUM_SUBSCRIPTION_ID")))
            {
                Assert.Inconclusive(SETUP);
            }

            var options = new QuantumJobClientOptions();

            options.Diagnostics.ApplicationId = Environment.GetEnvironmentVariable("AZURE_QUANTUM_NET_APPID") ?? "ClientTests";

            var credential = Authentication.CredentialFactory.CreateCredential(Authentication.CredentialType.Default);

            return(new Workspace(
                       subscriptionId: System.Environment.GetEnvironmentVariable("AZURE_QUANTUM_SUBSCRIPTION_ID"),
                       resourceGroupName: System.Environment.GetEnvironmentVariable("AZURE_QUANTUM_WORKSPACE_RG"),
                       workspaceName: System.Environment.GetEnvironmentVariable("AZURE_QUANTUM_WORKSPACE_NAME"),
                       location: System.Environment.GetEnvironmentVariable("AZURE_QUANTUM_WORKSPACE_LOCATION"),
                       options: options,
                       credential: credential));
        }
Пример #5
0
        public async Task ApplicationIdTest()
        {
            const string ENV_VAR_APPID           = "EnvVarAppId";
            const string OPTIONS_APPID           = "OptionAppId";
            const string LONG_ENV_VAR_APPID      = "LongEnvVarAppId";
            const string LONG_OPTIONS_APPID      = "LongOptionAppId";
            const string VERY_LONG_ENV_VAR_APPID = "VeryVeryVeryVeryVeryVeryLongEnvVarAppId";
            const string VERY_LONG_OPTIONS_APPID = "VeryVeryVeryVeryVeryVeryLongOptionAppId";
            const string APPID_ENV_VAR_NAME      = "AZURE_QUANTUM_NET_APPID";

            Func <QuantumJobClientOptions, Workspace> createWorkspace = (QuantumJobClientOptions options) =>
            {
                var credential = new ClientSecretCredential(tenantId: "72f988bf-86f1-41af-91ab-2d7cd011db47",
                                                            clientId: "00000000-0000-0000-0000-000000000000",
                                                            clientSecret: "PLACEHOLDER");
                return(new Workspace(subscriptionId: "SubscriptionId",
                                     resourceGroupName: "ResourceGroupName",
                                     workspaceName: "WorkspaceName",
                                     location: "WestUs",
                                     options: options,
                                     credential: credential));
            };

            var originalEnvironmentAppId = Environment.GetEnvironmentVariable(APPID_ENV_VAR_NAME);

            try
            {
                // Test with no Environment AppId and no Options AppId
                Environment.SetEnvironmentVariable(APPID_ENV_VAR_NAME, null);
                var workspace = createWorkspace(null);
                Assert.IsNotNull(workspace.ClientOptions);
                Assert.IsNotNull(workspace.ClientOptions.Diagnostics);
                Assert.AreEqual("", workspace.ClientOptions.Diagnostics.ApplicationId);

                // Test with Environment AppId and no Options AppId
                Environment.SetEnvironmentVariable(APPID_ENV_VAR_NAME, ENV_VAR_APPID);
                workspace = createWorkspace(null);
                Assert.IsNotNull(workspace.ClientOptions);
                Assert.IsNotNull(workspace.ClientOptions.Diagnostics);
                Assert.AreEqual(ENV_VAR_APPID, workspace.ClientOptions.Diagnostics.ApplicationId);

                // Test with no Environment AppId and with Options AppId
                Environment.SetEnvironmentVariable(APPID_ENV_VAR_NAME, null);
                var options = new QuantumJobClientOptions();
                options.Diagnostics.ApplicationId = OPTIONS_APPID;
                workspace = createWorkspace(options);
                Assert.IsNotNull(workspace.ClientOptions);
                Assert.IsNotNull(workspace.ClientOptions.Diagnostics);
                Assert.AreEqual(OPTIONS_APPID, workspace.ClientOptions.Diagnostics.ApplicationId);

                // Test with Environment AppId and with Options AppId
                Environment.SetEnvironmentVariable(APPID_ENV_VAR_NAME, ENV_VAR_APPID);
                options = new QuantumJobClientOptions();
                options.Diagnostics.ApplicationId = OPTIONS_APPID;
                workspace = createWorkspace(options);
                Assert.IsNotNull(workspace.ClientOptions);
                Assert.IsNotNull(workspace.ClientOptions.Diagnostics);
                Assert.AreEqual($"{OPTIONS_APPID}-{ENV_VAR_APPID}", workspace.ClientOptions.Diagnostics.ApplicationId);

                // Test with long (>24 chars) combination of Environment AppId and Options AppId
                Environment.SetEnvironmentVariable(APPID_ENV_VAR_NAME, LONG_ENV_VAR_APPID);
                options = new QuantumJobClientOptions();
                options.Diagnostics.ApplicationId = LONG_OPTIONS_APPID;
                workspace = createWorkspace(options);
                Assert.IsNotNull(workspace.ClientOptions);
                Assert.IsNotNull(workspace.ClientOptions.Diagnostics);
                var truncatedAppId = $"{LONG_OPTIONS_APPID}-{LONG_ENV_VAR_APPID}".Substring(0, 24);
                Assert.AreEqual(truncatedAppId, workspace.ClientOptions.Diagnostics.ApplicationId);

                // Test with long (>24 chars) Environment AppId and no Options AppId
                Environment.SetEnvironmentVariable(APPID_ENV_VAR_NAME, VERY_LONG_ENV_VAR_APPID);
                workspace = createWorkspace(null);
                Assert.IsNotNull(workspace.ClientOptions);
                Assert.IsNotNull(workspace.ClientOptions.Diagnostics);
                Assert.AreEqual(VERY_LONG_ENV_VAR_APPID.Substring(0, 24), workspace.ClientOptions.Diagnostics.ApplicationId);

                // Test with long (>24 chars) Options AppId and no Environment AppId
                Environment.SetEnvironmentVariable(APPID_ENV_VAR_NAME, null);
                options = new QuantumJobClientOptions();
                Assert.ThrowsException <System.ArgumentOutOfRangeException>(() =>
                                                                            options.Diagnostics.ApplicationId = VERY_LONG_OPTIONS_APPID);
            }
            finally
            {
                // restore original env var AZURE_QUANTUM_NET_APPID
                if (originalEnvironmentAppId != null)
                {
                    Environment.SetEnvironmentVariable(APPID_ENV_VAR_NAME, originalEnvironmentAppId);
                }
            }
        }
Пример #6
0
        private async Task <ExecutionResult> ConnectToWorkspaceAsync(IChannel?channel,
                                                                     string subscriptionId,
                                                                     string resourceGroupName,
                                                                     string workspaceName,
                                                                     string location,
                                                                     TokenCredential credential,
                                                                     CancellationToken cancellationToken = default)
        {
            if (string.IsNullOrWhiteSpace(location))
            {
                channel?.Stderr($"No location provided.");
                return(AzureClientError.NoWorkspaceLocation.ToExecutionResult());
            }

            location = GetNormalizedLocation(location, channel);
            if (string.IsNullOrWhiteSpace(location))
            {
                return(AzureClientError.InvalidWorkspaceLocation.ToExecutionResult());
            }

            try
            {
                var options = new QuantumJobClientOptions();

                // This value will be added as a prefix in the UserAgent when
                // calling the Azure Quantum APIs
                options.Diagnostics.ApplicationId = IsPythonUserAgent ? "IQ#/Py" : "IQ#";

                var workspace = AzureFactory.CreateWorkspace(
                    subscriptionId: subscriptionId,
                    resourceGroup: resourceGroupName,
                    workspaceName: workspaceName,
                    location: location,
                    credential: credential,
                    options: options);

                var providers = new List <ProviderStatusInfo>();
                var status    = workspace.ListProvidersStatusAsync(cancellationToken);
                await foreach (var s in status)
                {
                    providers.Add(s);
                }

                ActiveWorkspace    = workspace;
                AvailableProviders = providers;

                return(ExecuteStatus.Ok.ToExecutionResult());
            }
            catch (TaskCanceledException tce)
            {
                throw tce;
            }
            catch (Exception e)
            {
                var msg = $"The Azure Quantum workspace {workspaceName} in location {location} could not be reached.";
                Logger.LogError(e, msg);
                channel?.Stderr($"{msg} Please check the provided parameters and try again.");
                channel?.Stderr($"Error details:\n\n{e.Message}");

                return(AzureClientError.WorkspaceNotFound.ToExecutionResult());
            }
        }