forked from microsoft/Quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCNOTCircuitsMetrics.cs
114 lines (94 loc) · 5.55 KB
/
CCNOTCircuitsMetrics.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Microsoft.Quantum.Simulation.Simulators.QCTraceSimulators;
using System.Collections.Generic;
using System.IO;
using Xunit;
///////////////////////////////////////////////////////////////////////////////////////////////////
// This file illustrates how to use QCTraceSimulator for metrics calculation of CCNOT gates
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace Microsoft.Quantum.Samples.UnitTesting
{
public class CCNOTCircuitsMetrics
{
/// <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 CCNOTCircuitsMetrics(Xunit.Abstractions.ITestOutputHelper output)
{
this.output = output;
}
// [Fact] attribute indicates that this function is a usual Xunit test
[Fact(DisplayName = "CCNOTCircuitsMetrics")]
public void CCNOTCircuitsMetricsTest()
{
// Get an instance of the appropriately configured QCTraceSimulator
QCTraceSimulator sim = MetricCalculationUtils.GetSimulatorForMetricsCalculation();
// Run tests against trace simulator to collect metrics
var result1 = CCNOTCircuitsTest.Run(sim).Result;
// Let us check that number of T gates in all the circuits is as expected
string Tcount = PrimitiveOperationsGroupsNames.T;
// group of circuits with 7 T gates
Assert.Equal(7, sim.GetMetric<TDepthOneCCNOT,CollectMetrics>(Tcount));
Assert.Equal(7, sim.GetMetric<CCNOT1, CollectMetrics>(Tcount));
Assert.Equal(7, sim.GetMetric<CCNOT2, CollectMetrics>(Tcount));
Assert.Equal(7, sim.GetMetric<CCNOT4, CollectMetrics>(Tcount));
// group of circuits with 4 T gates
Assert.Equal(4, sim.GetMetric<CCNOT3, CollectMetrics>(Tcount));
Assert.Equal(4, sim.GetMetric<UpToPhaseCCNOT1, CollectMetrics>(Tcount));
Assert.Equal(4, sim.GetMetric<UpToPhaseCCNOT2, CollectMetrics>(Tcount));
// For UpToPhaseCCNOT3 the number of T gates in it is the number of T
// gates used in CCNOT plus the number of T gates in Controlled-S
double expectedTcount =
sim.GetMetric<Primitive.S, UpToPhaseCCNOT3>(
Tcount,
functor: Simulation.Core.OperationFunctor.ControlledAdjoint)
+ sim.GetMetric<Primitive.CCNOT, UpToPhaseCCNOT3>(Tcount);
Assert.Equal(
expectedTcount,
sim.GetMetric<UpToPhaseCCNOT3, CollectMetrics>(Tcount) );
// The number of extra qubits used by the circuits
string extraQubits = MetricsNames.WidthCounter.ExtraWidth;
Assert.Equal(4, sim.GetMetric<TDepthOneCCNOT, CollectMetrics>(extraQubits));
Assert.Equal(0, sim.GetMetric<CCNOT1, CollectMetrics>(extraQubits));
Assert.Equal(0, sim.GetMetric<CCNOT2, CollectMetrics>(extraQubits));
Assert.Equal(0, sim.GetMetric<CCNOT4, CollectMetrics>(extraQubits));
Assert.Equal(0, sim.GetMetric<UpToPhaseCCNOT1, CollectMetrics>(extraQubits));
Assert.Equal(2, sim.GetMetric<CCNOT3, CollectMetrics>(extraQubits));
Assert.Equal(1, sim.GetMetric<UpToPhaseCCNOT2, CollectMetrics>(extraQubits));
// All of the CCNOT circuit take 3 qubits as an input
string inputQubits = MetricsNames.WidthCounter.InputWidth;
Assert.Equal(3, sim.GetMetric<TDepthOneCCNOT, CollectMetrics>(inputQubits));
Assert.Equal(3, sim.GetMetric<CCNOT1, CollectMetrics>(inputQubits));
Assert.Equal(3, sim.GetMetric<CCNOT2, CollectMetrics>(inputQubits));
Assert.Equal(3, sim.GetMetric<UpToPhaseCCNOT1, CollectMetrics>(inputQubits));
Assert.Equal(3, sim.GetMetric<CCNOT3, CollectMetrics>(inputQubits));
Assert.Equal(3, sim.GetMetric<UpToPhaseCCNOT2, CollectMetrics>(inputQubits));
// CCNOT3 uses one measurement
Assert.Equal(
1,
sim.GetMetric<CCNOT3, CollectMetrics>(PrimitiveOperationsGroupsNames.Measure));
// Finally, let us check T depth of various CCNOT circuits:
string tDepth = MetricsNames.DepthCounter.Depth;
Assert.Equal(1, sim.GetMetric<TDepthOneCCNOT, CollectMetrics>(tDepth));
Assert.Equal(1, sim.GetMetric<CCNOT3, CollectMetrics>(tDepth));
Assert.Equal(1, sim.GetMetric<UpToPhaseCCNOT2, CollectMetrics>(tDepth));
Assert.Equal(4, sim.GetMetric<CCNOT2, CollectMetrics>(tDepth));
Assert.Equal(5, sim.GetMetric<CCNOT4, CollectMetrics>(tDepth));
Assert.Equal(5, sim.GetMetric<CCNOT1, CollectMetrics>(tDepth));
// Finally we write all the collected statistics into CSV files
string directory = Directory.GetCurrentDirectory();
output.WriteLine($"Writing all collected metrics result to" +
$" {directory}");
foreach( KeyValuePair<string,string> collectedData in sim.ToCSV() )
{
File.WriteAllText(
$"{directory}\\CCNOTCircuitsMetrics.{collectedData.Key}.csv",
collectedData.Value);
}
}
}
}