Overview

Quickstart: Create your first Q# program

When two or more qubits are entangled, they share quantum information — whatever happens to one qubit also happens to the other. In this quickstart, you create a particular two-qubit entangled state called a Bell pair. In a Bell pair, if you measure one qubit in the |0⟩ state, you know the other qubit is also in the |0⟩ state without measuring it.

In this quickstart, you:

  • Create a Q# file
  • Allocate a pair of qubits
  • Entangle the qubits

Prerequisites#

1
Create a Q# file
  1. Open Visual Studio Code.
  2. Select File > New Text File.
  3. Save the file as Main.qs. The .qs extension denotes a Q# program.
2
Import a quantum library

The QDK includes the Q# standard library with predefined functions and operations for your quantum programs. Use an import statement to open the Std.Diagnostics library, which gives you access to DumpMachine — used later to display the entangled state.

import Std.Diagnostics.*;
3
Define an operation

Define your quantum operation and its input and output values. For this quickstart, the operation is Main. It takes no parameters and returns two Result values, either Zero or One, representing the results of the qubit measurements:

operation Main() : (Result, Result) {
    // Your entanglement code goes here.
}
4
Allocate two qubits

Allocate two qubits, q1 and q2, using the use keyword. In Q#, qubits are always allocated in the |0⟩ state.

// Allocate two qubits, q1 and q2, in the 0 state.
use (q1, q2) = (Qubit(), Qubit());
5
Put one qubit into superposition

To prepare the qubits for entanglement, put one of them into an even superposition, where it has a 50% chance of being measured as |0⟩ or |1⟩, by applying the Hadamard (H) operation:

// Put q1 into an even superposition.
H(q1);
6
Entangle the qubits

Entangle the qubits using the controlled-NOT (CNOT) operation. CNOT takes two qubits — one as control, one as target. Here, q1 is the control qubit and q2 is the target: CNOT flips the state of q2 when the state of q1 is |1⟩.

// Entangle q1 and q2, making q2 depend on q1.
CNOT(q1, q2);

The resulting state of both qubits is the Bell pair.

7
Display, measure, and reset

Use DumpMachine to output the current state, M to measure the qubits, and Reset to return them to the |0⟩ state before they're released:

// Show the entangled state of the qubits.
DumpMachine();
 
// Measure q1 and q2 and store the results in m1 and m2.
let (m1, m2) = (M(q1), M(q2));
 
// Reset q1 and q2 to the 0 state.
Reset(q1);
Reset(q2);
 
// Return the measurement results.
return (m1, m2);

Run your Q# code#

Your final Q# program should look like this:

import Std.Diagnostics.*;
 
operation Main() : (Result, Result) {
    // Allocate two qubits, q1 and q2, in the 0 state.
    use (q1, q2) = (Qubit(), Qubit());
 
    // Put q1 into an even superposition.
    // It now has a 50% chance of being measured as 0 or 1.
    H(q1);
 
    // Entangle q1 and q2, making q2 depend on q1.
    CNOT(q1, q2);
 
    // Show the entangled state of the qubits.
    DumpMachine();
 
    // Measure q1 and q2 and store the results in m1 and m2.
    let (m1, m2) = (M(q1), M(q2));
 
    // Reset q1 and q2 to the 0 state.
    Reset(q1);
    Reset(q2);
 
    // Return the measurement results.
    return (m1, m2);
}

To run your program and view the result of both qubits, select Run above the Main operation or press Ctrl+F5. Each run gives a different result in the debug console — this demonstrates the probabilistic nature of quantum measurements. For example, if the result is Zero:

DumpMachine:
 
 Basis | Amplitude       | Probability | Phase
 -----------------------------------------------
  |00⟩ |  0.7071+0.0000i |    50.0000% |   0.0000
  |11⟩ |  0.7071+0.0000i |    50.0000% |   0.0000
 
Result: "(Zero, Zero)"

Updated

Was this page helpful?