Google News
logo
Quantum Computing Interview Questions
Learn to write a basic quantum program in Q# that leverages the nature of quantum mechanics to produce a random number.
 
In this tutorial, you'll learn how to
 
* Create a Q# project.
* Prepare your development environment for writing quantum programs in Q#.
* Understand how Q# programs are structured.
* Work with qubits and superposition to build a quantum random number generator.

Prerequisites : 
* Install the Quantum Development Kit (QDK) using your preferred language and development environment. This tutorial presents the solution in three different formats:
* Q# standalone (Set up a Q# standalone environment)
* Q# and Python (Set up a Q# and Python environment)
* Q# and C# (Set up a Q# and .NET environment)
* If you already have the QDK installed, make sure you have updated the QDK (and the Python qsharp package, if applicable) to the latest version.
The first thing you need to do is to create a new Q#(Q Sharp) project. This tutorial uses the environment based on Q# applications with VS Code, but you can use your preferred IDE.
 
To create a new project in Visual Studio Code :
 
* Select View -> Command Palette and select Q#: Create New Project.
* Select Standalone console application.
* Select a location to save the project, name it Qrng, and select Create Project.
* When the project is successfully created, select Open new project... in the lower right.

This generates two files : the project file, Qrng.csproj, and a Q# application template, Program.qs, that you will use to write your application.
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).
Linear algebra is the language of quantum computing. Although you don’t need to know it to implement or write quantum programs, it is widely used to describe qubit states, quantum operations, and to predict what a quantum computer does in response to a sequence of instructions.
 
Just like being familiar with the basic concepts of quantum physics can help you understand quantum computing, knowing some basic linear algebra can help you understand how quantum algorithms work. At the least, you’ll want to be familiar with vectors and matrix multiplication.

To build multi-qubit states out of single-qubit states and discusses the gate operations needed to include in a gate set to form a universal many-qubit quantum computer. These tools are absolutely necessary to understand the gate sets that are commonly used in Q# code, and also to gain intuition about why quantum effects such as entanglement or interference render quantum computing more powerful than classical computing.

The true power of quantum computing only becomes evident as we increase the number of qubits. Single-qubit gates possess some counter-intuitive features, such as the ability to be in more than one state at a given time. However, if all we had in a quantum computer were single-qubit gates, then a calculator and certainly a classical supercomputer would dwarf its computational power.
 
Quantum computing power arises, in part, because the dimension of the vector space of quantum state vectors grows exponentially with the number of qubits. This means that while a single qubit can be trivially modeled, simulating a fifty-qubit quantum computation would arguably push the limits of existing supercomputers. Increasing the size of the computation by only one additional qubit doubles the memory required to store the state and roughly doubles the computational time. This rapid doubling of computational power is why a quantum computer with a relatively small number of qubits can far surpass the most powerful supercomputers of today, tomorrow, and beyond for some computational tasks.

As you work with Q#, Pauli measurements are a common kind of measurement, which generalize computational basis measurements to include measurements in other bases and of parity between different qubits. In such cases, it is common to discuss measuring a Pauli operator, in general an operator such as X,Y,Z or Z⊗Z,X⊗X,X⊗Y, and so forth.

Discussing measurement in terms of Pauli operators is especially common in the subfield of quantum error correction.

Q# guide follows a similar convention; this article explains this alternative view of measurements.

Some quantum algorithms are easier to understand in a circuit diagram than in the equivalent written matrix representation once you understand the visual conventions.

With Azure Quantum, you can use the azure-quantum Python package to submit quantum circuits with Qiskit, Cirq, and also provider-specific formatted circuits.

Quantum circuit diagram conventions : In a circuit diagram, each solid line depicts a qubit, or more generally, a qubit register. By convention, the top line is qubit register 0 and the remainder are labeled sequentially.

Operations are represented by quantum gates. The term quantum gate is analogous to classical logic gates. Gates acting on one or more qubit registers are denoted as a box. For example, the symbol

Symbol for a Hadamard operation acting on a single-qubit register

is a Hadamard operation acting on a single-qubit register.

In a quantum circuit, time flows from left to right. Quantum gates are ordered in chronological order with the left-most gate as the gate first applied to the qubits. In other words, if you picture the wires as holding the quantum state, the wires bring the quantum state through each of the gates in the diagram from left to right. That is to say

Diagram of quantum gates being applied left-to-right

is the unitary matrix CBA.

The previous examples have had precisely the same number of wires (qubits) input to a quantum gate as the number of wires out from the quantum gate. It may at first seem reasonable that quantum circuits could have more, or fewer, outputs than inputs in general. This is impossible, however, because all quantum operations, save measurement, are unitary and hence reversible. If they did not have the same number of outputs as inputs they would not be reversible and hence not unitary, which is a contradiction. For this reason any box drawn in a circuit diagram must have precisely the same number of wires entering it as exiting it.

Multi-qubit circuit diagrams follow similar conventions to single-qubit ones. As a clarifying example, a two-qubit unitary operation B can be defined to be (HS⊗X), so the equivalent quantum circuit is:

Circuit diagram of a two-qubit unitary operation

You can also view B as having an action on a single two-qubit register rather than two one-qubit registers depending on the context in which the circuit is used.

Perhaps the most useful property of such abstract circuit diagrams is that they allow complicated quantum algorithms to be described at a high level without having to compile them down to fundamental gates. This means that you can get an intuition about the data flow for a large quantum algorithm without needing to understand all the details of how each of the subroutines within the algorithm work.

The action of a quantum singly controlled gate, denoted Λ(G)">Λ(G), where a single qubit's value controls the application of G">G, can be understood by looking at the following example of a product state input:
Controlled Gates
That is to say, the controlled gate applies G">G to the register containing ψ">ψ if and only if the control qubit takes the value 1">1. In general, such controlled operations are described in circuit diagrams as

Circuit diagram of a singly controlled gate

Here the black circle denotes the quantum bit on which the gate is controlled and a vertical wire denotes the unitary that is applied when the control qubit takes the value 1. For the special cases where G=X and G=ZG=Z, the following notation is used to describe the controlled version of the gates (note that the controlled-X gate is the CNOT gate):

Circuit diagram for special cases of controlled gates