This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 93
/
GenericCallable.cs
300 lines (246 loc) · 11.4 KB
/
GenericCallable.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
namespace Microsoft.Quantum.Simulation.Core
{
/// <summary>
/// Represents an operation whose
/// input and output Types are not resolved until it gets Applied at runtime.
/// </summary>
public partial interface ICallable : IApplyData
{
string FullName { get; }
string Name { get; }
OperationFunctor Variant { get; }
QVoid Apply(object args);
O Apply<O>(object args);
ICallable Partial(object partialTuple);
}
/// <summary>
/// This is a wrapper class that holds an Operation's Type and will
/// try to create an instance of it when Apply is called.
///
/// During apply, it uses the input and output parameters to resolve
/// any generic parameters of the BaseOp using reflection.
///
/// Notice GenericOperations are not expected to extend this class. They
/// should instead extend Operation and stay generic. This class will take
/// care of resolving the Generic types are runtime based on the Apply types.
/// </summary>
[DebuggerTypeProxy(typeof(GenericCallable.DebuggerProxy))]
public class GenericCallable : AbstractCallable, IUnitary
{
private Lazy<GenericAdjoint> _adjoint;
private Lazy<GenericControlled> _controlled;
public GenericCallable(IOperationFactory m, Type baseOp) : base(m)
{
this.OperationType = baseOp;
_adjoint = new Lazy<GenericAdjoint>(() => new GenericAdjoint(this));
_controlled = new Lazy<GenericControlled>(() => new GenericControlled(this));
}
public override void Init() { }
public Type OperationType { get; }
public GenericAdjoint Adjoint => _adjoint.Value;
public GenericControlled Controlled => _controlled.Value;
ConcurrentDictionary<(Type, Type), ICallable> _cache = new ConcurrentDictionary<(Type, Type), ICallable>();
// Finds an instance of Operation<'I, 'O> with no resolved Types, used only for debugging.
internal ICallable FindCallable()
{
return FindCallable(typeof(MissingParameter), typeof(MissingParameter));
}
// Tries to get the instance Operation<'I, 'O> based on the type fo the Input/Output
// parameters. It tries to resolve from Cache first, if not, then it calls CreateOperation.
public virtual ICallable FindCallable(Type I, Type O)
{
var key = (I, O);
ICallable result = null;
if (_cache.TryGetValue(key, out result))
{
return result;
}
var normal = I.Normalize();
if (_cache.TryGetValue((normal, O), out result))
{
_cache[key] = result;
return result;
}
result = CreateCallable(normal, O);
_cache[key] = result;
return result;
}
// Creates the corresponding closed Operation<'I, 'O> for the given I O.
protected virtual ICallable CreateCallable(Type I, Type O)
{
var op = this.OperationType;
if (OperationType.ContainsGenericParameters)
{
op = FindClosedType(I, O);
}
var get = this.Factory.GetType()
.GetMethod("Get", new Type[0]);
var result = get
.MakeGenericMethod(typeof(ICallable), op)
.Invoke(this.Factory, new object[] { })
as ICallable;
return result;
}
O Apply<O>(object args)
{
IgnorableAssert.Assert(args != null, "Calling Apply method on a generic with null input, thus Input type can't be determined.");
if (args == null) return default(O);
var op = FindCallable(args.GetType(), typeof(O));
return (O)op.Apply<O>(args);
}
public GenericPartial Partial(object partialTuple) => new GenericPartial(this, partialTuple);
public virtual string Name => FindCallable().Name;
public virtual string FullName => FindCallable().FullName;
public virtual OperationFunctor Variant => OperationFunctor.Body;
O ICallable.Apply<O>(object args) => this.Apply<O>(args);
QVoid ICallable.Apply(object args) => this.Apply<QVoid>(args);
ICallable ICallable.Partial(object partialTuple) => this.Partial(partialTuple);
IAdjointable IAdjointable.Adjoint => this.Adjoint;
IAdjointable IAdjointable.Partial(object partialTuple) => this.Partial(partialTuple);
IControllable IControllable.Controlled => this.Controlled;
IControllable IControllable.Partial(object partialTuple) => this.Partial(partialTuple);
IUnitary IUnitary.Adjoint => this.Adjoint;
IUnitary IUnitary.Controlled => this.Controlled;
IUnitary IUnitary.Partial(object partialTuple) => this.Partial(partialTuple);
/// <summary>
/// Finds the ClosedType for the BaseOp based on the given Input (I) and
/// Output (O) Types that the Apply method is expecting.
///
/// This method finds the ClosedType of the based (generic) operation...
/// For example, if the base operation is Some ['T] : Unitary [('T, long)]
/// and it is called with a.Apply((bool, long))
/// then it maps maps 'T == bool
/// and returns
/// typeof(SomeOp [bool ])
/// </summary>
public virtual Type FindClosedType(Type I, Type O)
{
var typeArgs = new Type[this.OperationType.GetGenericArguments().Length];
// Get the list of Parameters of the Invoke method of the Body of the operation:
var expectedParameters = this.OperationType
.GetProperty("Body").PropertyType
.GetMethod("Invoke").GetParameters();
// Tuple in...
Debug.Assert(expectedParameters.Length == 1, "Expected a single argument Tuple when calling Apply");
Resolve(expectedParameters[0].ParameterType, I, typeArgs);
// Tuple out...
var expectedReturn = this.OperationType
.GetProperty("Body").PropertyType
.GetMethod("Invoke").ReturnType;
Resolve(expectedReturn, O, typeArgs);
// Verify everything is resolved:
ResolveTheUnresolved(typeArgs);
return this.OperationType.MakeGenericType(typeArgs); ;
}
/// <summary>
/// Makes sure the Types that will be sent as parameters to the CreateInstance are exactly
/// 'I and 'O for Operation<'I, 'O>
/// </summary>
public static Type[] MatchOperationTypes(Type originalIn, Type originalOut, Type operationType)
{
var inType = originalIn;
var outType = originalOut;
// Find the Operation<,> definition:
var current = operationType;
while (current != null)
{
if (current.IsGenericType && current.GetGenericTypeDefinition() == typeof(Operation<,>))
{
inType = current.GenericTypeArguments[0];
outType = current.GenericTypeArguments[1];
break;
}
current = current.BaseType;
Debug.Assert(current != null);
}
return new Type[] { inType, outType };
}
/// <summary>
/// There are situations that the type parameters of a Generic operation can't be resolved, because
/// the input tuple does not use them directly, for example, in Bind<'T> the 'T is in the arguments
/// of the input operations which in turn are just passed in as ICallables, with no type info.
/// In this scenario, we simply use object as 'T as we just pass it down.
/// </summary>
public virtual void ResolveTheUnresolved(Type[] typeArgs)
{
for(var i =0; i < typeArgs.Length; i++)
{
if (typeArgs[i] == null)
{
typeArgs[i] = typeof(object);
}
}
}
/// <summary>
/// Populates the generic Type Arguments based on the resolved type.
/// Basically if the resolved type comes from a generic type, then whatever resolved values it
/// had are applied to original.
/// This method is the one that computes what the Type the GenericParameter should take based on the input argument.
/// It recursively checks if the Operations is expecting a
/// ('T, long) and it receives a (bool, long) it maps 'T == bool.
/// </summary>
public virtual void Resolve(Type original, Type resolved, Type[] typeArgs)
{
if (original.IsGenericParameter)
{
typeArgs[original.GenericParameterPosition] = resolved.Normalize();
}
else if (original.IsGenericType)
{
var originalDefinition = original.GetGenericTypeDefinition();
var originalGenericArgs = original.GetGenericArguments();
var resolvedGenericArgs = new Type[0];
var resolvedBase = resolved.BaseType;
// The Types matches, for example original == ValueTuple<,,> and resolved is ValueTuple<,,>, we can do a Type for Type
if (resolved.IsGenericType && resolved.GetGenericTypeDefinition() == originalDefinition)
{
resolvedGenericArgs = resolved.GetGenericArguments();
}
else if (resolved.IsQArray() && originalDefinition.GetGenericTypeDefinition() == typeof(IQArray<>))
{
resolvedGenericArgs = resolved.GetGenericArguments();
}
if (originalGenericArgs.Length == resolvedGenericArgs.Length)
{
for (int i = 0; i < originalGenericArgs.Length; i++)
{
var a = originalGenericArgs[i];
var r = resolvedGenericArgs[i];
Resolve(a, r, typeArgs);
}
}
else if (resolvedGenericArgs.Length == 0)
{
for (int i = 0; i < originalGenericArgs.Length; i++)
{
var a = originalGenericArgs[i];
Resolve(a, resolved, typeArgs);
}
}
else
{
Debug.Assert(false, $"Number of generic parameters missmatch. Most probably the Type inferred will be wrong.");
}
}
}
internal class DebuggerProxy
{
private GenericCallable _op;
public DebuggerProxy(GenericCallable op)
{
this._op = op;
}
public string Name => _op.Name;
public string FullName => _op.FullName;
public OperationFunctor Variant => _op.Variant;
public string Signature => _op.QSharpType();
}
public virtual string QSharpType() => this.OperationType?.QSharpType();
public override string ToString() => this.Name;
}
}