forked from microsoft/iqsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorkspaceController.cs
123 lines (109 loc) · 4.53 KB
/
WorkspaceController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Microsoft.AspNetCore.Mvc;
using Microsoft.Quantum.IQSharp.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
#pragma warning disable VSTHRD200 // Use "Async" suffix for async methods
namespace Microsoft.Quantum.IQSharp
{
/// <summary>
/// The Workspace endpoint. Provides an interface to query for the list of operations
/// in the workspace and to simulate them.
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class WorkspaceController : AbstractOperationsController
{
// The default Workspace instance.
public IWorkspace Workspace { get; set; }
// The list of operations available in the workspace.
public override IEnumerable<OperationInfo> Operations => Workspace?.AssemblyInfo?.Operations;
public WorkspaceController(IWorkspace workspace)
{
this.Workspace = workspace;
}
/// <summary>
/// Default entry point. Returns the list of operations in the Workspace.
/// </summary>
[HttpGet]
public async Task<Response<string[]>> GetMany() =>
await AsResponse(async(logger) =>
await IfReady(async () =>
{
var names = Operations?.Select(c => c.FullName).ToArray();
return names;
}));
/// <summary>
/// Get one operation.
/// </summary>
[HttpGet("{id}")]
public async Task<Response<OperationInfo>> GetOne(string id) =>
await AsResponse(async (logger) =>
await IfReady(async () =>
{
OperationInfo op = Find(id);
return op;
}));
/// <summary>
/// Triggers a Reload of the Workspace.
/// </summary>
[HttpGet("reload")]
public async Task<Response<string[]>> Reload()
{
try
{
Workspace?.Reload();
if (Workspace.HasErrors) return new Response<string[]>(Status.error, Workspace.ErrorMessages.ToArray());
return await GetMany();
}
catch (Exception e)
{
return new Response<string[]>(Status.error, new string[] { e.Message });
}
}
/// <summary>
/// Simulates the execution of the given operation.
/// Supports both, GET and POST.
/// If GET, then the parameters are expected as normal query parameters.
/// If POST, then the parameters are expected as a JSON object in the body.
/// </summary>
[HttpGet("{id}/simulate")]
[HttpPost("{id}/simulate")]
public async Task<Response<object>> Simulate(string id) =>
await AsResponse(async (logger) =>
await Simulate(id, await GetRunArguments(Request), logger));
/// <summary>
/// Returns an estimate of how many resources are needed to run the given operation on a quantum computer.
/// As with simulate, supports both, GET and POST.
/// If GET, then the parameters are expected as normal query parameters
/// If POST, then the parameters are expected as a JSON object in the body.
/// </summary>
[HttpGet("{id}/estimate")]
[HttpPost("{id}/estimate")]
public async Task<Response<Dictionary<string, double>>> Estimate(string id) =>
await AsResponse(async (logger) =>
await Estimate(id, await GetRunArguments(Request), logger));
/// <summary>
/// Performs checks to verify if the Controller is ready to execute operations, namely
/// it checks if the Workspace is avaialble and in a success (no errors) state.
/// The method throws Exceptions if it finds it is not ready to execute.
/// </summary>
public override void CheckIfReady()
{
if (Workspace == null)
{
throw new InvalidWorkspaceException($"Workspace is not ready. Try again.");
}
else if (Workspace.HasErrors)
{
throw new InvalidWorkspaceException(Workspace.ErrorMessages.ToArray());
}
}
}
}
#pragma warning restore VSTHRD200 // Use "Async" suffix for async methods
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously