Search code examples
quantum-computingq#

Polynomial Equations in Q# E=MC^2


I am trying to understand how to use quantum computing and have started to understand some of the basic gates and other concepts, but am not able to understand how to put it to practice in real world problems.

Let's say I want to write a function in Q# that returns the value of E in the equation

E= MC^2

Can someone help me write this operation?


Solution

  • To answer the literal question: if M and C are just floating-point numbers, the calculation can be done using purely classical Q# constructs:

    // The function that carries out the computation itself
    function Energy (m : Double, c : Double) : Double {
        return m * c ^ 2.0;
    }
    
    // The operation that you'll invoke to pass the parameters to the function and to print the results
    operation PrintEnergy () : Unit {
        let c = 299792458.0;
        let energy1 = Energy(1.0, c);
        Message($"Calculated energy of 1 gram of mass = {energy1}");
        let energy2 = Energy(2.0, c);
        Message($"Calculated energy of 2 grams of mass = {energy2}");
    }
    

    The output is:

    Calculated energy of 1 gram of mass = 89875517873681760
    Calculated energy of 2 grams of mass = 1.7975103574736352E+17
    

    You will notice that this code fragment does not use any qubits or gates, so it's not really a good example of using quantum computing to solve real-world problems, even though it's implemented using a quantum programming language. This problem involved very simple mathematical computations, which can be done very efficiently using classical computers.

    Quantum computers are going to use a co-processor model of computation - we'll use them to do computations that they are well suited to do (such as solving chemistry problems), and use classical computers for the rest of the computations.

    To learn to apply quantum computing to solving problems with Q#, you can check out the Quantum Katas - a collection of tutorials and programming exercises. In particular, they show how to translate classical problems such as SAT or graph coloring into a form that can take advantage of quantum computing algorithms. (Disclosure: I'm the maintainer of this project)