Write a Q# standalone program to run on a local quantum simulator

The Microsoft Quantum Development Kit contains several quantum simulators that allow you to test and run quantum programs locally, without having to access the Azure Quantum service.

Prerequisites

Set up your preferred local environment with the Microsoft Quantum Development Kit following the steps in Set up a Q# standalone environment.

Run a Q# program

Follow the instructions on the tab corresponding to your development environment that you created during set up.

  1. From your environment (that is, either the conda environment you created, or the Python environment where you installed Jupyter), run the following command to start the Jupyter Notebook server:

    jupyter notebook
    
    • If the Jupyter Notebook doesn't open automatically in your browser, copy and paste the URL provided by the command line into your browser.
  2. Choose New → Q# to create a Jupyter Notebook with a Q# kernel, and add the following code to the first notebook cell:

    operation SampleQuantumRandomNumberGenerator() : Result {
        use q = Qubit(); // Allocate a qubit in the |0⟩ state.
        H(q);            // Put the qubit to superposition. It now has a 50% chance of being 0 or 1.
        let r = M(q);    // Measure the qubit value.
        Reset(q);
        return r;
    }
    
  3. Run this cell of the notebook. You should see SampleQuantumRandomNumberGenerator in the output of the cell. When running in Jupyter Notebook, the Q# code is compiled, and the cell outputs the name of any operations that it finds.

  4. In a new cell, run the operation you just created in a simulator by using the %simulate magic command:

    Jupyter Notebook cell with %simulate magic

    You should see the result of the operation you invoked. In this case, because your operation generates a random result, you will see either Zero or One printed on the screen. If you run the cell repeatedly, you should see each result approximately half the time.

Next steps

Now that you have run a Q# program on a local quantum simulator, learn how you can also run your program on remote quantum hardware.