Ask Learn Preview
Please sign in to use this experience.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
In the previous unit, you learned about the basic building blocks of a Q# program. Now, you're ready to write your first quantum program: a quantum program to generate truly random numbers.
You build out your quantum random number generator in two phases. In this unit, you build out the first phase, which is to generate a single random bit.
Main.qs
. This file will contain the Q# code for your program.Main
operationThe Main
operation is the entry point of your program.
operation Main(): Result{
// Your code goes here
}
You start by allocating one qubit with the use
keyword. In Q#, every qubit you allocate starts in the
operation Main(): Result{
// Allocate a qubit
use q = Qubit();
}
The qubit is in the H
, to the qubit. The Hadamard operation changes the state of the qubit and puts it into an equal superposition of
Because the qubit is in an equal superposition, when you measure it, you have a 50% chance of getting 0 and a 50% chance of getting 1.
operation Main(): Result{
use q = Qubit();
H(q);
}
At this point the qubit q
has 50% chance of being measured in the |0〉 state and 50% chance of being measured in the |1〉 state. Thus, if you measure the qubit, you'll get a random bit, either 0 or 1, with equal 50% probability. The value of this bit is truly random, there's no way of knowing in advance the result of the measurement.
To measure the qubit value, use the M
operation and store the measurement value in the result
variable.
operation Main(): Result{
use q = Qubit();
H(q);
let result = M(q);
}
In Q#, every qubit must be in the Reset(q)
to reset the qubit to the zero state.
operation Main(): Result{
use q = Qubit();
H(q);
let result = M(q);
Reset(q);
}
Finally, you return the measurement result with the return
keyword. This result is a random bit, either 0 or 1, with equal probability.
operation Main(): Result{
use q = Qubit();
H(q);
let result = M(q);
Reset(q);
return result;
}
Your Main.qs
file should look like this. The program allocates a qubit, puts it into superposition, measures the qubit, resets the qubit, and returns the measurement result.
Note
The //
symbol represents optional comments to explain each step of the program.
operation Main() : Result {
// Allocate a qubit.
use q = Qubit();
// Set the qubit into superposition of 0 and 1 using the Hadamard
H(q);
// Measure the qubit and store the result.
let result = M(q);
// Reset qubit to the |0〉 state.
Reset(q);
// Return the result of the measurement.
return result;
}
To run your program on the built-in simulator, click Run above the Main
operation or press Ctrl+F5. Your output appears in the debug console in the terminal.
The result is either One
or Zero
, which represents a truly random bit. You can run the program again to see a different result.
In the next unit, you'll implement the second phase of your quantum random number generator: combining multiple random bits to form a larger number.
Having an issue? We can help!
Please sign in to use this experience.
Sign in