public static bool IsQubitsContainer(this Type t) { if (t == null || t.IsPrimitive || t.IsEnum || t == typeof(String) || t == typeof(MissingParameter)) { return(false); } if (_isContainerCache.TryGetValue(t, out bool __result)) { return(__result); } if (typeof(IApplyData).IsAssignableFrom(t)) { __result = true; } else { var extractor = QubitsExtractor.Get(t); __result = extractor?.IsQubitContainer() ?? false; } _isContainerCache[t] = __result; return(__result); }
/// <summary> /// Returns all the qubits contained in a given type. /// </summary> /// <param name="value">The object from which qubits are extracted</param> public static IEnumerable <Qubit> GetQubits(this object value) { if (value is IApplyData qContainer) { return(qContainer.Qubits); } else if (value == null) { return(null); } else if (value.GetType().IsQubitsContainer()) { return(QubitsExtractor.Get(value.GetType())?.Extract(value)); } return(null); }
/// <summary> /// Extracts the Qubits capatured by the parameters of this Partial Application. /// If received a Mapper, then it Extracts the qubits of the result of calling the Mapper with no new qubits. /// If received a Partial Tuple, it Extracts the qubits from the Partial Tuple itself. /// In both cases, it uses a generic QubitsExtractor as we don't have the type info at compile time. /// </summary> public IEnumerable <Qubit> ExtractQubits() { if (this.Values != null) { IEnumerable <Qubit> captured = null; IEnumerable <Qubit> capturedParent = ((IApplyData)this.BaseOp)?.Qubits; if (this.Values.GetType().IsPartialMapper()) { var mapper = this.Values as Delegate; var P = mapper.GetType().GenericTypeArguments[0]; var d = P.IsValueType ? Activator.CreateInstance(P) : null; var data = mapper.DynamicInvoke(d); captured = QubitsExtractor.Get(data.GetType())?.Extract(data); } else { captured = QubitsExtractor.Get(this.Values.GetType())?.Extract(this.Values); } if (captured == null) { return(capturedParent); } else if (capturedParent == null) { return(captured); } else { return(Qubit.Concat(captured, capturedParent)); } } else { return(null); } }