Google News
logo
Quantum Computing - Interview Questions
Write a Q Sharp operation in Quantum Computing?
Now, replace the contents of the Program.qs file with the following code :
namespace Qrng {
    open Microsoft.Quantum.Convert;
    open Microsoft.Quantum.Math;
    open Microsoft.Quantum.Measurement;
    open Microsoft.Quantum.Canon;
    open Microsoft.Quantum.Intrinsic;
    
    operation SampleQuantumRandomNumberGenerator() : Result {
        // Allocate a qubit        
        use q = Qubit();  
        // Put the qubit to superposition
        // It now has a 50% chance of being measured 0 or 1  
        H(q);      
        // Measure the qubit value            
        return M(q); 
    }
}
Now take a look at new code.
 
* First, you open the necessary namespaces from the Q# libraries for the functions and operations needed.
* You define the SampleQuantumRandomNumberGenerator operation, which takes no input and produces a value of type Result. The Result type represents the result of a measurement and can have two possible values: Zero or One.
* Allocate a single qubit with the use keyword.
* Use the H (Hadamard) operation to place the qubit in an equal superposition.
* Use the M operation to measure the qubit and return the measured value (Zero or One).
Advertisement