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 923
/
ResourcesEstimator.cs
201 lines (178 loc) · 7.79 KB
/
ResourcesEstimator.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.Quantum.Simulation.QCTraceSimulatorRuntime;
using Microsoft.Quantum.Simulation.Simulators.QCTraceSimulators;
namespace Microsoft.Quantum.Simulation.Samples
{
/// <summary>
/// The ResourceEstimator estimates statistics about how many resources
/// the given operation needs for execution.
/// The resources it calculates are:
/// <ol>
/// <li>Primitive operations count</li>
/// <li>Qubits depth (max number of qubits allocated at any given point)</li>
/// <li>Gates width (total number of gates used for the computation)</li>
/// </ol>
/// </summary>
public partial class ResourcesEstimator : QCTraceSimulator
{
/// <summary>
/// The ResourceEstimator is based on <see cref="QCTraceSimulator"/>; this returns
/// the correct configuration expected by the ResourceEstimator and what get used when
/// a new instance is created with no parameters. It is
/// optimized for performance and metrics collection.
/// </summary>
public static QCTraceSimulatorConfiguration RecommendedConfig() =>
new QCTraceSimulatorConfiguration
{
CallStackDepthLimit = 1,
ThrowOnUnconstrainedMeasurement = false,
UseDistinctInputsChecker = false,
UseInvalidatedQubitsUseChecker = false,
UsePrimitiveOperationsCounter = true,
UseDepthCounter = true,
UseWidthCounter = true
};
/// <summary>
/// Constructor used by entry point driver to provide the assembly for core types that
/// need to be overriden.
/// </summary>
public ResourcesEstimator(Assembly coreAssembly) : base(RecommendedConfig(), coreAssembly)
{
}
/// <summary>
/// Parameter-less constructor. It initializes the ResourceEstimator
/// with a QCTraceSimulatorConfiguration as returned by <see cref="RecommendedConfig"/>.
/// </summary>
public ResourcesEstimator() : this(RecommendedConfig())
{
}
/// <summary>
/// It initializes the ResourceEstimator with the given QCTraceSimulatorConfiguration.
/// It is recommended to use <see cref="RecommendedConfig"/> to create a new config instance
/// and tweak it, to make sure the data collection is correctly configured.
/// </summary>
public ResourcesEstimator(QCTraceSimulatorConfiguration config) : base(config)
{
}
// Exposed the underlying CoreConfiguration for unittesting.
internal QCTraceSimulatorCoreConfiguration CoreConfig => tCoreConfig;
/// <summary>
/// Returns the label to use for the given metric. If the metric should be skipped
/// it returns null. Otherwise, it returns the same metric's name or some other alias.
/// </summary>
public virtual string GetMetricLabel(string name)
{
if (name == MetricsNames.DepthCounter.StartTimeDifference ||
name == MetricsNames.WidthCounter.InputWidth ||
name == MetricsNames.WidthCounter.ReturnWidth)
{
return null;
}
else if (name == MetricsNames.WidthCounter.ExtraWidth)
{
return "QubitCount";
}
else
{
return name;
}
}
/// <summary>
/// <para>Returns the values collected as a DataTable with the first
/// column two columns: the metric name and its value.
/// The metric name column is marked as PrimaryKey
/// for easy access.
/// </para>
/// <para>
/// The table looks like this:
/// <pre>
/// -------------------------
/// | Metric | Sum |
/// -------------------------
/// | QubitsCount | 100 |
/// | T | 10000 |
/// ...
/// -------------------------
/// </pre>
/// </para>
/// </summary>
public virtual DataTable Data
{
get
{
var table = new DataTable();
table.Columns.Add(new DataColumn { DataType = typeof(string), ColumnName = "Metric" });
table.Columns.Add(new DataColumn { DataType = typeof(double), ColumnName = "Sum" });
table.Columns.Add(new DataColumn { DataType = typeof(double), ColumnName = "Max" });
table.PrimaryKey = new DataColumn[] { table.Columns[0] };
foreach (var l in CoreConfig.Listeners)
{
// All listeners we expected are ICallGraphStatistics
if (l is ICallGraphStatistics collector)
{
var results = collector.Results.ToTable();
Debug.Assert(results.keyColumnNames.Length > 2 && results.keyColumnNames[2] == "Caller");
var roots = results.rows.Where(r => r.KeyRow[2] == CallGraphEdge.CallGraphRootHashed);
var sum_idx = Array.FindIndex(results.statisticsNames, n => n == "Sum");
var max_idx = Array.FindIndex(results.statisticsNames, n => n == "Max");
for (var m_idx = 0; m_idx < results.metricNames.Length; m_idx++)
{
var label = GetMetricLabel(results.metricNames[m_idx]);
if (label == null) continue;
DataRow row = table.NewRow();
row["Metric"] = label;
if (sum_idx >= 0)
{
Double sum = 0;
foreach (var r in roots)
{
sum += r.DataRow[m_idx, sum_idx];
}
row["Sum"] = sum;
}
if (max_idx >= 0) {
Double max = 0; // all our metrics are positive
foreach (var r in roots) {
max = System.Math.Max(max, r.DataRow[m_idx, max_idx]);
}
row["Max"] = max;
}
table.Rows.Add(row);
}
}
else
{
Debug.Assert(false, "Listener is not a collector");
}
}
return table;
}
}
/// <summary>
/// Returns <see cref="Data"/> in TSV format where the key is the Metric name,
/// and the value is the statistics in tab-seperated format.
/// </summary>
public virtual string ToTSV()
{
var content = new StringBuilder();
var table = Data;
content.Append(table.Columns[0].ColumnName.PadRight(15)).Append('\t');
var columns = table.Columns.Cast<DataColumn>().Skip(1).Select(c => c.ColumnName.PadRight(15));
content.Append(string.Join("\t", columns));
foreach(DataRow r in table.Rows)
{
content.Append('\n');
content.Append(r[0].ToString().PadRight(15)).Append('\t');
content.Append(string.Join("\t", r.ItemArray.Skip(1).Select(i => i.ToString())));
}
return content.ToString();
}
}
}