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
/
Copy pathRepeatUntilSuccessCircuitsMetrics.cs
82 lines (67 loc) · 3.82 KB
/
RepeatUntilSuccessCircuitsMetrics.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Microsoft.Quantum.Simulation.Simulators.QCTraceSimulators;
using Xunit;
// We can use C# type aliases to shorten long caller names
using caller = Microsoft.Quantum.Canon.ApplyToFirstQubit;
///////////////////////////////////////////////////////////////////////////////////////////////////
// This file illustrates how to use QCTraceSimulator for metrics calculation
// of Repeat Until Success circuits
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace Microsoft.Quantum.Samples.UnitTesting
{
public class RepeatUntilSuccessCircuitsMetrics
{
/// <summary>
/// Interface provided by Xunit framework for logging during test execution.
/// When the test is selected in Visual Studio Test Explore window
/// there is an Output text link available for each test.
/// </summary>
private readonly Xunit.Abstractions.ITestOutputHelper output;
public RepeatUntilSuccessCircuitsMetrics(Xunit.Abstractions.ITestOutputHelper output)
{
this.output = output;
}
// [Fact] attribute indicates that this function is a usual Xunit test
[Fact(DisplayName = "RepeatUntilSuccessCircuitsMetrics")]
public void RepeatUntilSuccessCircuitsMetricsTest()
{
// Get an instance of the appropriately configured QCTraceSimulator
var sim = MetricCalculationUtils.GetSimulatorForMetricsCalculation();
// Run tests against trace simulator to collect metrics
var result = CheckRepeatUntilSuccessRotatesByCorrectAngle.Run(sim).Result;
var TCount = PrimitiveOperationsGroupsNames.T;
var extraQubits = MetricsNames.WidthCounter.ExtraWidth;
var min = StatisticsNames.Min;
var max = StatisticsNames.Max;
var avg = StatisticsNames.Average;
// Let us check how many auxiliary qubits we used
// 4 for Nielsen & Chuang
// version because we used CCNOT3 which itself required 2 auxiliary
Assert.Equal(4, sim.GetMetric<ExpIZArcTan2NC, caller>(extraQubits));
// 2 for Paetznick & Svore version
Assert.Equal(2, sim.GetMetric<ExpIZArcTan2PS, caller>(extraQubits));
// One trial of ExpIZArcTan2NC requires at least 8 T gates
Assert.True(8 <= sim.GetMetricStatistic<ExpIZArcTan2NC, caller>(TCount,min));
// One trial of ExpIZArcTan2NC requires at least 3 T gates
Assert.True(3 <= sim.GetMetricStatistic<ExpIZArcTan2PS, caller>(TCount, min));
// The rest of the statistical information we print into test output
// For ExpIZArcTan2NC:
output.WriteLine($"Statistics collected for{nameof(ExpIZArcTan2NC)}");
output.WriteLine($"Average number of T-gates:" +
$" {sim.GetMetricStatistic<ExpIZArcTan2NC, caller>(TCount, avg)}");
output.WriteLine($"Minimal number of T-gates: " +
$"{sim.GetMetricStatistic<ExpIZArcTan2NC, caller>(TCount, min)}");
output.WriteLine($"Maximal number of T-gates: " +
$"{sim.GetMetricStatistic<ExpIZArcTan2NC, caller>(TCount, max)}");
// And for ExpIZArcTan2PS
output.WriteLine($"Statistics collected for{nameof(ExpIZArcTan2PS)}");
output.WriteLine($"Average number of T-gates:" +
$" {sim.GetMetricStatistic<ExpIZArcTan2PS, caller>(TCount, avg)}");
output.WriteLine($"Minimal number of T-gates: " +
$"{sim.GetMetricStatistic<ExpIZArcTan2PS, caller>(TCount, min)}");
output.WriteLine($"Maximal number of T-gates: " +
$"{sim.GetMetricStatistic<ExpIZArcTan2PS, caller>(TCount, max)}");
}
}
}