This repository was archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
Create the correct default values of Q# types #401
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
4de89e2
Add tests for default values
e11cd52
Add tests for default tuples and UDTs
5cc10cf
Add test for default Unit
44a8794
Add special-case defaults for a few types
1bd7b2c
Basic handling for defaults of tuples and UDTs
bfc3c14
Add defaults for all value tuples
10565e3
Refactor tuple defaults
392b081
Factor out different default types
ebc036e
Update namespace
4e99d93
Update default value in UDT constructor
3153fda
Add test for UDT inside tuple
5eefcb0
Fix range default
9e60afb
Rename QDefault to Default
0d3b8b9
Default to null instead of throwing
6463656
Fix nullable warning
b01922b
Assert default qubit and callable are null
d4d22ab
Add doc comments
afb47a1
Fix C# generation tests
9e835c8
Fix tuple tests
6d34172
Merge branch 'main' into samarsha/default-values
bettinaheim 4458809
More efficient array initialization
e78bbad
Merge branch 'main' into samarsha/default-values
bettinaheim ecb54db
Merge branch 'main' into samarsha/default-values
bettinaheim e35488e
Merge branch 'main' into samarsha/default-values
bettinaheim 5e965b5
Merge branch 'main' into samarsha/default-values
bettinaheim 0a13107
Merge branch 'main' into samarsha/default-values
bamarsha 66bdd82
Merge branch 'main' into samarsha/default-values
bamarsha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| #nullable enable | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Linq; | ||
|
|
||
| namespace Microsoft.Quantum.Simulation.Core | ||
| { | ||
| /// <summary> | ||
| /// Creates default values of Q# types. | ||
| /// </summary> | ||
| public static class Default | ||
| { | ||
| /// <summary> | ||
| /// A dictionary from basic types to their default values. | ||
| /// </summary> | ||
| private static readonly IReadOnlyDictionary<Type, object> BasicValues = new Dictionary<Type, object> | ||
| { | ||
| [typeof(QRange)] = QRange.Empty, | ||
| [typeof(QVoid)] = QVoid.Instance, | ||
| [typeof(Result)] = Result.Zero, | ||
| [typeof(string)] = "" | ||
| }; | ||
|
|
||
| /// <summary> | ||
| /// A list of all generic tuple types. | ||
| /// </summary> | ||
| private static readonly IReadOnlyList<Type> Tuples = new List<Type> | ||
| { | ||
| typeof(ValueTuple<>), | ||
| typeof(ValueTuple<,>), | ||
| typeof(ValueTuple<,,>), | ||
| typeof(ValueTuple<,,,>), | ||
| typeof(ValueTuple<,,,,>), | ||
| typeof(ValueTuple<,,,,,>), | ||
| typeof(ValueTuple<,,,,,,>), | ||
| typeof(ValueTuple<,,,,,,,>) | ||
| }; | ||
|
|
||
| /// <summary> | ||
| /// Returns the default value of the Q# type. May return null when null is the default value of the type, or if | ||
| /// the type is not a valid Q# type. | ||
| /// </summary> | ||
| [return: MaybeNull] | ||
| public static T OfType<T>() => OfType(typeof(T)) is T value ? value : default; | ||
|
|
||
| /// <summary> | ||
| /// Returns the default value of the Q# type. May return null when null is the default value of the type, or if | ||
| /// the type is not a valid Q# type. | ||
| /// </summary> | ||
| private static object? OfType(Type type) => OfAnyType(type).FirstOrDefault(value => !(value is null)); | ||
|
|
||
| /// <summary> | ||
| /// Enumerates the default values of different kinds of types. Yields null if the given type is not the right | ||
| /// kind, and yields a non-null value if a default value is found. | ||
| /// </summary> | ||
| private static IEnumerable<object?> OfAnyType(Type type) | ||
| { | ||
| yield return BasicValues.GetValueOrDefault(type); | ||
| yield return OfArrayType(type); | ||
| yield return OfTupleType(type); | ||
| yield return OfUserDefinedType(type); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// If the given type is a Q# array type, returns the default array of that type, or null otherwise. | ||
| /// </summary> | ||
| private static object? OfArrayType(Type type) => | ||
| type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IQArray<>) | ||
| ? Activator.CreateInstance(typeof(QArray<>).MakeGenericType(type.GenericTypeArguments)) | ||
| : null; | ||
|
|
||
| /// <summary> | ||
| /// If the given type is a Q# tuple type, returns the default tuple of that type, or null otherwise. | ||
| /// </summary> | ||
| private static object? OfTupleType(Type type) => | ||
| type.IsGenericType && Tuples.Contains(type.GetGenericTypeDefinition()) | ||
| ? Activator.CreateInstance(type, type.GenericTypeArguments.Select(OfType).ToArray()) | ||
| : null; | ||
|
|
||
| /// <summary> | ||
| /// If the given type is a Q# user-defined type, returns the default value of that type, or null otherwise. | ||
| /// </summary> | ||
| private static object? OfUserDefinedType(Type type) => | ||
| !(type.BaseType is null) | ||
| && type.BaseType.IsGenericType | ||
| && type.BaseType.GetGenericTypeDefinition() == typeof(UDTBase<>) | ||
| ? Activator.CreateInstance(type, type.BaseType.GenericTypeArguments.Select(OfType).ToArray()) | ||
| : null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.