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.
Quantum Computing Stack Exchange is a question and answer site for engineers, scientists, programmers, and computing professionals interested in quantum computing. It only takes a minute to sign up.
Sign up to join this communityI 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.
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.
@EntryPoint()
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.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.