Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Commit 6eb9d45

Browse files
Aggregate results in resource estimator across multiple runs (#264)
* Aggregate results in resource estimator across multiple runs. The metrics are expanded to show both total sum and max across the runs.
1 parent a268e08 commit 6eb9d45

File tree

4 files changed

+85
-20
lines changed

4 files changed

+85
-20
lines changed

src/Simulation/EntryPointDriver.Tests/Tests.fs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -474,15 +474,15 @@ let ``Shadows --shots`` () =
474474

475475
// The expected output from the resources estimator.
476476
let private resourceSummary =
477-
"Metric Sum
478-
CNOT 0
479-
QubitClifford 1
480-
R 0
481-
Measure 1
482-
T 0
483-
Depth 0
484-
Width 1
485-
BorrowedWidth 0"
477+
"Metric Sum Max
478+
CNOT 0 0
479+
QubitClifford 1 1
480+
R 0 0
481+
Measure 1 1
482+
T 0 0
483+
Depth 0 0
484+
Width 1 1
485+
BorrowedWidth 0 0"
486486

487487
[<Fact>]
488488
let ``Supports QuantumSimulator`` () =

src/Simulation/Simulators.Tests/Circuits/ResourcesEstimator.qs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,27 @@ namespace Microsoft.Quantum.Simulation.Simulators.Tests
1717
}
1818
}
1919

20+
// When multiple operations are traced by resource estimator,
21+
// it should report cumulative statistics in the end.
22+
operation Operation_1_of_2() : Unit
23+
{
24+
using ((a, b) = (Qubit(), Qubit())) {
25+
H(a);
26+
CNOT(a, b);
27+
T(b);
28+
}
29+
}
30+
operation Operation_2_of_2() : Result
31+
{
32+
using ((a, b, c) = (Qubit(), Qubit(), Qubit())) {
33+
X(a);
34+
CNOT(a, b);
35+
Rx(0.42, b);
36+
CNOT(b, c);
37+
return M(c);
38+
}
39+
}
40+
2041
// Tests for Depth and Width lower bounds
2142
operation DepthDifferentQubits () : Unit
2243
{

src/Simulation/Simulators.Tests/ResourcesEstimatorTests.cs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
// Licensed under the MIT License.
33

44
using System;
5-
using System.Collections.Generic;
65
using System.Linq;
7-
using System.Text;
6+
using System.Data;
87
using Microsoft.Quantum.Simulation.QCTraceSimulatorRuntime;
98
using Xunit;
109

@@ -95,10 +94,10 @@ public void ToTSVTest()
9594

9695
var cols = rows[0].Split('\t');
9796
Assert.Equal("Metric", cols[0].Trim());
98-
Assert.Equal(2, cols.Length);
97+
Assert.Equal(3, cols.Length);
9998

10099
var cliffords = rows.First(r => r.StartsWith("QubitClifford")).Split('\t');
101-
Assert.Equal(2, cliffords.Length);
100+
Assert.Equal(3, cliffords.Length);
102101
Assert.Equal("2", cliffords[1]);
103102
}
104103

@@ -138,5 +137,43 @@ public void DepthVersusWidthTest()
138137
Assert.Equal(1.0, data.Rows.Find("Width")["Sum"]);
139138
Assert.Equal(1.0, data.Rows.Find("Depth")["Sum"]);
140139
}
140+
141+
/// <summary>
142+
/// Verifies that for multiple separately traced operations, the final
143+
/// statistics are cumulative.
144+
/// </summary>
145+
[Fact]
146+
public void VerifyTracingMultipleOperationsTest()
147+
{
148+
ResourcesEstimator sim = new ResourcesEstimator();
149+
150+
Operation_1_of_2.Run(sim).Wait();
151+
DataTable data1 = sim.Data;
152+
153+
Assert.Equal(1.0, data1.Rows.Find("CNOT")["Sum"]);
154+
Assert.Equal(1.0, data1.Rows.Find("QubitClifford")["Sum"]);
155+
Assert.Equal(1.0, data1.Rows.Find("T")["Sum"]);
156+
Assert.Equal(0.0, data1.Rows.Find("R")["Sum"]);
157+
Assert.Equal(0.0, data1.Rows.Find("Measure")["Sum"]);
158+
Assert.Equal(2.0, data1.Rows.Find("Width")["Sum"]);
159+
160+
Operation_2_of_2.Run(sim).Wait();
161+
DataTable data2 = sim.Data;
162+
163+
// Aggregated stats for both operations.
164+
Assert.Equal(1.0 + 2.0, data2.Rows.Find("CNOT")["Sum"]);
165+
Assert.Equal(1.0 + 1.0, data2.Rows.Find("QubitClifford")["Sum"]);
166+
Assert.Equal(1.0 + 0.0, data2.Rows.Find("T")["Sum"]);
167+
Assert.Equal(0.0 + 1.0, data2.Rows.Find("R")["Sum"]);
168+
Assert.Equal(0.0 + 1.0, data2.Rows.Find("Measure")["Sum"]);
169+
Assert.Equal(2.0 + 3.0, data2.Rows.Find("Width")["Sum"]);
170+
Assert.Equal(System.Math.Max(2.0, 3.0), data2.Rows.Find("Width")["Max"]);
171+
172+
// Run again to confirm two operations isn't the limit!
173+
VerySimpleEstimate.Run(sim).Wait();
174+
DataTable data3 = sim.Data;
175+
Assert.Equal(2.0 + 3.0 + 3.0, data3.Rows.Find("Width")["Sum"]);
176+
Assert.Equal(3.0, data3.Rows.Find("Width")["Max"]);
177+
}
141178
}
142179
}

src/Simulation/Simulators/ResourcesEstimator/ResourcesEstimator.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License.
33

44
using System;
5-
using System.Collections.Generic;
65
using System.Data;
76
using System.Diagnostics;
87
using System.Linq;
@@ -113,6 +112,7 @@ public virtual DataTable Data
113112

114113
table.Columns.Add(new DataColumn { DataType = typeof(string), ColumnName = "Metric" });
115114
table.Columns.Add(new DataColumn { DataType = typeof(double), ColumnName = "Sum" });
115+
table.Columns.Add(new DataColumn { DataType = typeof(double), ColumnName = "Max" });
116116
table.PrimaryKey = new DataColumn[] { table.Columns[0] };
117117

118118
foreach (var l in CoreConfig.Listeners)
@@ -121,24 +121,31 @@ public virtual DataTable Data
121121
if (l is ICallGraphStatistics collector)
122122
{
123123
var results = collector.Results.ToTable();
124-
Debug.Assert(results.rows.Count() == 1);
125124
Debug.Assert(results.keyColumnNames.Length > 2 && results.keyColumnNames[2] == "Caller");
126125

127-
var root = results.rows.FirstOrDefault(r => r.KeyRow[2] == CallGraphEdge.CallGraphRootHashed);
128-
126+
var roots = results.rows.Where(r => r.KeyRow[2] == CallGraphEdge.CallGraphRootHashed);
129127
var s_idx = Array.FindIndex(results.statisticsNames, n => n == "Sum");
130128

131129
for (var m_idx = 0; m_idx < results.metricNames.Length; m_idx++)
132130
{
133131
var label = GetMetricLabel(results.metricNames[m_idx]);
134132
if (label == null) continue;
135133

136-
var row = table.NewRow();
134+
DataRow row = table.NewRow();
137135
row["Metric"] = label;
138136

139137
if (m_idx >= 0 && s_idx >= 0)
140-
{
141-
row["Sum"] = root.DataRow[m_idx, s_idx];
138+
{
139+
Double sum = 0;
140+
Double max = 0; // all our metrics are positive
141+
foreach (var r in roots)
142+
{
143+
Double metric_value = r.DataRow[m_idx, s_idx];
144+
sum += metric_value;
145+
max = System.Math.Max(max, metric_value);
146+
}
147+
row["Sum"] = sum;
148+
row["Max"] = max;
142149
}
143150

144151
table.Rows.Add(row);

0 commit comments

Comments
 (0)