Close

Your First Quantum "Hello World" with Q#

Now that you have your Quantum Development Environment set up, you're ready to write your first Q# program. In this blog post, we'll guide you through creating a simple quantum program that demonstrates the concepts of qubits, quantum gates, and basic quantum operations.

Step 1: Create a New Q# Operation

In your Q# project, open the main Q# file with the .qs extension (e.g., Operations.qs). Replace the existing code with the following template:

namespace MyFirstQuantumProgram {
    open Microsoft.Quantum.Intrinsic;
    open Microsoft.Quantum.Canon;

    operation MyOperation() : Result {
        // Your Q# code goes here
    }
}

Step 2: Allocate a Qubit

In Q#, you can allocate qubits using the using statement. Add the following lines inside the MyOperation operation to allocate a single qubit:

using (q = Qubit()) {
    // Quantum operations on qubit `q` go here
}

Step 3: Apply Quantum Gates

Quantum gates are the building blocks of quantum circuits. They perform operations on qubits to manipulate their states. In this example, we'll apply a Hadamard gate (H) to our qubit, which puts it into a superposition state:

H(q); // Apply a Hadamard gate to the qubit `q`

Step 4: Measure the Qubit

After applying quantum gates, you can measure the state of a qubit using the M operation. Measurement collapses the qubit's state to either |0⟩ or |1⟩, with probabilities determined by the qubit's superposition coefficients. Add the following line to measure the qubit:

let result = M(q); // Measure the qubit `q`

Step 5: Return the Measurement Result

To return the measurement result from the Q# operation, modify the MyOperation signature to return a Result type and add a return statement:





operation MyOperation() : Result {
    // ...
    return result;
}

Step 6: Add an Entry Point

To run your Q# operation from the command line, you need to add an entry point to your program. Add the following attribute and operation to your code:

@EntryPoint()
operation RunMyOperation() : Result {
    return MyOperation();
}

Your complete Q# program should now look like this:





namespace MyFirstQuantumProgram {
    open Microsoft.Quantum.Intrinsic;
    open Microsoft.Quantum.Canon;

    operation MyOperation() : Result {
        using (q = Qubit()) {
            H(q); // Apply a Hadamard gate to the qubit `q`
            let result = M(q); // Measure the qubit `q`
            return result;
        }
    }

    @EntryPoint()
    operation RunMyOperation() : Result {
        return MyOperation();
    }
}

Step 7: Run Your Quantum Program

To run your quantum program, open the terminal in Visual Studio Code and navigate to your project folder using the cd command (e.g., cd MyFirstQuantumProgram). Then, run the following command:

dotnet run

You should see the output "0" or "1" in the terminal, representing the measured state of the qubit after applying the Hadamard gate.

Congratulations! You've just written and executed your first Q# program. In the next blog post, we'll explore more advanced topics.

In the meantime, may your qubits always be in superposition!

Share