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 55
Expand file tree
/
Copy pathExecutionPath.cs
More file actions
160 lines (141 loc) · 5.25 KB
/
Copy pathExecutionPath.cs
File metadata and controls
160 lines (141 loc) · 5.25 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#nullable enable
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Microsoft.Quantum.Simulation.Core;
namespace Microsoft.Quantum.IQSharp.ExecutionPathTracer
{
/// <summary>
/// Represents the qubit resources and operations traced out in an execution path of a Q# operation.
/// </summary>
public class ExecutionPath
{
/// <summary>
/// Initializes a new instance of the <see cref="ExecutionPathTracer"/> class.
/// </summary>
/// <param name="qubits">
/// A list of <see cref="QubitDeclaration"/> that represents the declared qubits used in the execution path.
/// </param>
/// <param name="operations">
/// A list of <see cref="Operation"/> that represents the operations used in the execution path.
/// </param>
public ExecutionPath(IEnumerable<QubitDeclaration> qubits, IEnumerable<Operation> operations)
{
this.Qubits = qubits;
this.Operations = operations;
}
/// <summary>
/// A list of <see cref="QubitDeclaration"/> that represents the declared qubits used in the execution path.
/// </summary>
[JsonProperty("qubits")]
public IEnumerable<QubitDeclaration> Qubits { get; }
/// <summary>
/// A list of <see cref="Operation"/> that represents the operations used in the execution path.
/// </summary>
[JsonProperty("operations")]
public IEnumerable<Operation> Operations { get; }
/// <summary>
/// Serializes <see cref="ExecutionPath"/> into its JSON representation.
/// </summary>
/// <param name="prettyPrint">
/// Pretty prints the JSON (i.e. with white space and indents) if <c>true</c>.
/// </param>
public string ToJson(bool prettyPrint = false) =>
JsonConvert.SerializeObject(this,
(prettyPrint) ? Formatting.Indented : Formatting.None,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
}
);
}
/// <summary>
/// Represents a qubit resource used in an execution path.
/// </summary>
public class QubitDeclaration
{
/// <summary>
/// Initializes a new instance of the <see cref="QubitDeclaration"/> class.
/// </summary>
/// <param name="id">
/// Id of qubit.
/// </param>
/// <param name="numChildren">
/// Number of associated classical registers.
/// </param>
public QubitDeclaration(int id, int numChildren = 0)
{
this.Id = id;
this.NumChildren = numChildren;
}
/// <summary>
/// Id of qubit.
/// </summary>
[JsonProperty("id")]
public int Id { get; }
/// <summary>
/// Number of associated classical registers.
/// </summary>
[JsonProperty("numChildren")]
public int NumChildren { get; }
/// <summary>
/// Used by <see cref="Newtonsoft" /> to determine if <see cref="NumChildren" />
/// should be included in the JSON serialization.
/// </summary>
public bool ShouldSerializeNumChildren() => NumChildren > 0;
}
/// <summary>
/// Represents an operation used in an execution path.
/// </summary>
public class Operation
{
/// <summary>
/// Label of gate.
/// </summary>
[JsonProperty("gate")]
public string Gate { get; set; } = "";
/// <summary>
/// Arguments (except <see cref="Qubit" /> types) provided to gate that
/// will be displayed by the visualizer.
/// </summary>
[JsonProperty("displayArgs")]
public string? DisplayArgs { get; set; }
/// <summary>
/// Group of operations for each classical branch.
/// </summary>
[JsonProperty("children")]
public IEnumerable<IEnumerable<Operation>>? Children { get; set; }
/// <summary>
/// True if operation is a measurement operations.
/// </summary>
[JsonProperty("isMeasurement")]
public bool IsMeasurement { get; set; }
/// <summary>
/// True if operation is a classically-controlled operations.
/// </summary>
[JsonProperty("isConditional")]
public bool IsConditional { get; set; }
/// <summary>
/// True if operation is a controlled operations.
/// </summary>
[JsonProperty("isControlled")]
public bool IsControlled { get; set; }
/// <summary>
/// True if operation is an adjoint operations.
/// </summary>
[JsonProperty("isAdjoint")]
public bool IsAdjoint { get; set; }
/// <summary>
/// List of control registers.
/// </summary>
[JsonProperty("controls")]
public IEnumerable<Register> Controls { get; set; } = new List<Register>();
/// <summary>
/// List of target registers.
/// </summary>
[JsonProperty("targets")]
public IEnumerable<Register> Targets { get; set; } = new List<Register>();
}
}