I use dotnet run command to run my .qs quantum circuit. Is there a way to run my circuit N times, and get results of each run (like the shots parameter in Qiskit) ?

Thank you.

  • $\begingroup$ You could create a python or C# host program to further process data. However, there must be a dotnet command. $\endgroup$ Sep 7, 2020 at 10:57

2 Answers 2


Quantum programs in Q# can include classical logic as well as low-level quantum instructions, such that you can directly include the "shot" concept from circuits in your Q# programs by using a for-loop.

operation RunSeveralTimes(nTimes : Int) : Result[] {
    mutable results = new Result[nTimes];
    for (idx in 0..nTimes - 1) {
        set results w/= idx <- DoSomething();
    return results;

When running this program with dotnet run, you can now pass --n-times as a command-line argument to control how many times your DoSomething() operation gets called. Similarly, if you're using Q# in Jupyter Notebooks, you can use a command like %simulate RunSeveralTimes nTimes=50. From Python interoperability, you can pass nTimes as a keyword argument, e.g.: RunSeveralTimes.simulate(nTimes=50).

The Q# standard libraries also offer several functions and operations to make this sort of loop easier to write:

  • EstimateFrequency and EstimateFrequencyA: run an operation many times, and collect the probability with which a measurement at the end returns One.
  • DrawMany: run an operation many times with the same input, and collect all outputs.
  • ForEach: run an operation once for each of many inputs, and collect all outputs.
  • 1
    $\begingroup$ Exactly what I wanted, thank you very much ! $\endgroup$
    – user12910
    Sep 18, 2020 at 19:28

As mentioned by Jonathcraft, it's possible to modify the host program. For example, set the driver of the Q# program to be C#, then call the Q# program multiple times with a for loop.


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.