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
Add Runtime Metadata to Operations #301
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a774650
Implement GetRuntimeMetadata methods
2576463
Improve error messages
936f44e
Add nullable
045eab2
Respond to feedback, add more tests, and modify logic of ArgsToString…
61127d4
Remove override for Reset*'s GetRuntimeMetadata and add more tests
a50ad48
Implement GetRuntimeMetadata for UDTPartial
e6e42ac
Rename Args to FormattedNonQubitArgs
b737581
Respond to PR feedback
53fa910
Fix typo
53855f5
Remove useless test
9d76677
Merge branch 'master' of https://github.com/microsoft/qsharp-runtime …
49a2bbb
Empty commit to trigger CI build
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
|
||
| namespace Microsoft.Quantum.Simulation.Core | ||
| { | ||
| /// <summary> | ||
| /// Contains the metadata associated with an operation's runtime execution path. | ||
| /// </summary> | ||
| public class RuntimeMetadata | ||
| { | ||
| /// <summary> | ||
| /// Label of gate. | ||
| /// </summary> | ||
| public string Label { get; set; } = ""; | ||
|
|
||
| /// <summary> | ||
| /// Non-qubit arguments provided to gate, formatted as string. | ||
| /// </summary> | ||
| public string FormattedNonQubitArgs { get; set; } = ""; | ||
|
|
||
| /// <summary> | ||
| /// True if operation is an adjoint operation. | ||
| /// </summary> | ||
| public bool IsAdjoint { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// True if operation is a controlled operation. | ||
| /// </summary> | ||
| public bool IsControlled { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// True if operation is a measurement operation. | ||
| /// </summary> | ||
| public bool IsMeasurement { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// True if operation is composed of multiple operations. | ||
theRoughCode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// </summary> | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Currently not used as this is intended for composite operations, | ||
| /// such as <c>ApplyToEach</c>. | ||
| /// </remarks> | ||
| public bool IsComposite { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Group of operations for each classical branch (<c>true</c> and <c>false</c>). | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Currently not used as this is intended for classically-controlled operations. | ||
| /// </remarks> | ||
| public IEnumerable<IEnumerable<RuntimeMetadata>>? Children { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// List of control registers. | ||
| /// </summary> | ||
| public IEnumerable<Qubit> Controls { get; set; } = new List<Qubit>(); | ||
|
|
||
| /// <summary> | ||
| /// List of target registers. | ||
| /// </summary> | ||
| public IEnumerable<Qubit> Targets { get; set; } = new List<Qubit>(); | ||
|
|
||
| private static bool OnlyOneNull(object? a, object? b) => | ||
| (a == null && b != null) || (b == null && a != null); | ||
|
|
||
| private static bool IsBothNull(object? a, object? b) => | ||
| a == null && b == null; | ||
|
|
||
| private static bool ListEquals<T>(IEnumerable<T> a, IEnumerable<T> b) => | ||
| IsBothNull(a, b) || (!OnlyOneNull(a, b) && a.SequenceEqual(b)); | ||
|
|
||
| public override bool Equals(object? obj) | ||
| { | ||
| var other = obj as RuntimeMetadata; | ||
|
|
||
| if (other is null) return false; | ||
|
|
||
| if (this.Label != other.Label || this.FormattedNonQubitArgs != other.FormattedNonQubitArgs || | ||
| this.IsAdjoint != other.IsAdjoint || this.IsControlled != other.IsControlled || | ||
| this.IsMeasurement != other.IsMeasurement || this.IsComposite != other.IsComposite) | ||
| return false; | ||
|
|
||
| if (!ListEquals<Qubit>(this.Controls, other.Controls)) return false; | ||
|
|
||
| if (!ListEquals<Qubit>(this.Targets, other.Targets)) return false; | ||
|
|
||
| // If only one children is null, return false | ||
| if (OnlyOneNull(this.Children, other.Children)) return false; | ||
|
|
||
| // If both children are not null, compare each child element-wise and return | ||
| // false if any of them are not equal | ||
| if (!IsBothNull(this.Children, other.Children)) | ||
| { | ||
| if (this.Children.Count() != other.Children.Count() || | ||
| this.Children.Zip(other.Children, ListEquals<RuntimeMetadata>).Contains(false)) | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| public override int GetHashCode() | ||
| { | ||
| // Stringify qubits, concatenate as string, and get resulting hashcode | ||
| var controlsHash = string.Join(",", this.Controls.Select(q => q?.ToString() ?? "")).GetHashCode(); | ||
| var targetsHash = string.Join(",", this.Targets.Select(q => q?.ToString() ?? "")).GetHashCode(); | ||
|
|
||
| // Recursively get hashcode of inner `RuntimeMetadata` objects, concatenate into a string, | ||
| // and get resulting hashcode | ||
| var childrenHash = (this.Children != null) | ||
| ? string.Join(", ", this.Children.Select(child => (child != null) | ||
| ? string.Join(",", child.Select(m => m?.GetHashCode().ToString() ?? "0")) | ||
| : "0" | ||
| )).GetHashCode() | ||
| : 0; | ||
|
|
||
| // Combine all other properties and get the resulting hashcode | ||
| var otherHash = HashCode.Combine(this.Label, this.FormattedNonQubitArgs, this.IsAdjoint, this.IsControlled, | ||
| this.IsMeasurement, this.IsComposite); | ||
|
|
||
| // Combine them all together to get the final hashcode | ||
| return HashCode.Combine(controlsHash, targetsHash, childrenHash, otherHash); | ||
| } | ||
|
|
||
| public static bool operator ==(RuntimeMetadata? x, RuntimeMetadata? y) => | ||
| IsBothNull(x, y) || (x?.Equals(y) ?? false); | ||
|
|
||
| public static bool operator !=(RuntimeMetadata? x, RuntimeMetadata? y) => !(x == y); | ||
| } | ||
| } | ||
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.