TransWikia.com

Run circuit N times in Q#

Quantum Computing Asked on December 16, 2020

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.

2 Answers

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.

Correct answer by Chris Granade on December 16, 2020

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.

Answered by C. Kang on December 16, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP