Skip to content
This repository has been archived by the owner on Aug 29, 2024. It is now read-only.

Commit

Permalink
Add a trace simulator in C# for custom resources estimation
Browse files Browse the repository at this point in the history
Count every operation and not just T in the depth
  • Loading branch information
jond01 committed Jun 5, 2022
1 parent 0b4d428 commit 79593ad
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
13 changes: 11 additions & 2 deletions mcx/qsharp/MultiControlX.qs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace ClassiqChallenge {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Diagnostics;

// Programs for the MCX gate

Expand All @@ -9,8 +11,11 @@ namespace ClassiqChallenge {
// Simple implementation of the Multi-Controlled X gate
Controlled X(controlRegister, target);
}

@EntryPoint()

internal function XOR(a : Bool, b : Bool) : Bool {
return (a and not b) or (b and not a);
}

operation ApplyMultiControlledX(initControl : Bool, initTarget : Bool) :
Result {
let controlRegisterSize = 14;
Expand All @@ -30,6 +35,10 @@ namespace ClassiqChallenge {
apply {
MultiControlledX(controlRegister, target);
}

let expectedResult = BoolAsResult(XOR(initControl, initTarget));
AssertQubit(expectedResult, target);

return M(target);
}

Expand Down
64 changes: 64 additions & 0 deletions mcx/qsharp/ResourcesEstimator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.Quantum.Simulation.Simulators.QCTraceSimulators;
using ClassiqChallenge;

namespace host
{
static class Program
{
static int DefaultPrimitiveDepth = 1;

// See:
// https://docs.microsoft.com/en-us/dotnet/api/microsoft.quantum.simulation.simulators.qctracesimulators.qctracesimulatorconfiguration
static QCTraceSimulatorConfiguration GetConfig()
{
var config = new QCTraceSimulatorConfiguration();
config.UseWidthCounter = true;
config.UseDepthCounter = true;

// Optimize for using less qubits
config.OptimizeDepth = false;

// Configure depth counting:
// Count every relevant primitive in the depth metric.
// By default only T primitives are counted, i.e. the depth is T-depth.
foreach (var primitive in new List<PrimitiveOperationsGroups>{
PrimitiveOperationsGroups.CNOT,
PrimitiveOperationsGroups.QubitClifford,
PrimitiveOperationsGroups.R,
PrimitiveOperationsGroups.T,
}
)
{
config.TraceGateTimes[primitive] = DefaultPrimitiveDepth;
}
// Do not count measurements - they are only helpers in this case.
config.TraceGateTimes[PrimitiveOperationsGroups.Measure] = 0;

foreach (var kvp in config.TraceGateTimes)
{
Console.WriteLine(kvp);
}
return config;
}

// See:
// https://docs.microsoft.com/en-us/azure/quantum/machines/qc-trace-simulator/width-counter
static async Task Main(string[] args)
{
var sim = new QCTraceSimulator(GetConfig());

var singleQubitResult = await ApplyMultiControlledX.Run(
sim, initControl: false, initTarget: false
);

double width = sim.GetMetric<ApplyMultiControlledX>(MetricsNames.WidthCounter.ExtraWidth);
double depth = sim.GetMetric<ApplyMultiControlledX>(MetricsNames.DepthCounter.Depth);

Console.WriteLine($"Single qubit result: {singleQubitResult}");
Console.WriteLine($"Width: {width}, Depth: {depth}.");
}
}
}

0 comments on commit 79593ad

Please sign in to comment.