What is the best way to get the circuit run time in Q# ?

More precisely, I run a circuit using dotnet -run command and I would like to know how many time it took for running my circuit. Actually I compute the delay between the moment I start the command and the return, but I suppose it includes building time, I really just need the circuit running time.

Thank you.

Best regards.


2 Answers 2


Here is what I did when I was exploring optimizing the code to run as fast as possible in simulation (here is the blog post describing the things I looked into).

  1. Add a classical driver to your project that calls your Q# code.
    I used C# (you can also use Python if you prefer it) and the code you'll use is going to be something like this:
using QuantumSimulator qsim = new QuantumSimulator();
bool[] result = GroversSearch.Run(qsim).Result.ToArray();
  1. In this classical driver, wrap the call to quantum operation's Run in classical code that tracks the start and the end time. Here is C# code for it:
Stopwatch stopWatch = new Stopwatch();
// ... call quantum operation here ...
TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
    ts.Hours, ts.Minutes, ts.Seconds,
    ts.Milliseconds / 10);
Console.WriteLine("RunTime " + elapsedTime);

You can do any pre-processing before the call to quantum code and any post-processing afterwards, and they won't count towards the execution time.

You can see the full example here.


Note: This was suppose to be a comment but it got too long so I just put it here as an answer.

I don't think Microsoft has successfully built their quantum machine, and therefore it would not be be possible to do that, without having to run your program through actual quantum gate. The run time you get by seeing seeing how fast you can execute the simulation is heavily depend on your classical computer resources. The run time on the quantum circuits depend on the number of gates and depth of the circuit, and how fast you can implement those gates. For IBM's devices, the sampling time of single qubit gate is around 100-150 ns (not sure, but you can look it up and they are different for different qubits), and the CNOT gate time is around 1000 ns (again, they are different on which two qubit you operate on). Then you can work out the math of how long it takes to execute your ciruit. You probably can write small script to do this. Between the gates, you may want to add some buffer time as well, maybe 20ns or so.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.