Q# Program does not contain a static 'Main' method suitable for an entry point

Viewed 281

I'm creating a program in Q#.

Problem

You are given two qubits in state |00⟩. Your task is to create the following state on them: 1/3–√(|00⟩+|01⟩+|10⟩) You have to implement an operation which takes an array of 2 qubits as an input and has no output. The "output" of your solution is the state in which it left the input qubits.

Code

namespace Solution {
    open Microsoft.Quantum.Primitive;
    open Microsoft.Quantum.Canon;
    open Microsoft.Quantum.Math;
    open Microsoft.Quantum.Convert;

    operation Solve (qs : Qubit[]) : Unit
    {
        body
        {
            Ry(ArcCos(Sqrt(2.0/3.0))*2.0,qs[0]);
            (ControlledOnInt(0,H))([qs[0]],qs[1]);
        }
    }    
}

But when I run it show me the following error.

Error

CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point 

[C:\Users\Pawar\Desktop\HK\codeforces\Q#\Solution\Solution.csproj]

So I tried to put EntryPoint() before the method declaration . Which shows me different error as

error QS6231: Invalid entry point. Values of type Qubit may not be used as arguments or return values to entry points. [C:\Users\Pawar\Desktop\HK\codeforces\Q#\Solution\Solution.csproj]

Please help me how to run it properly ? thanks ✌️

1 Answers

In order to run a Q# program as an executable, you need to have an @EntryPoint() operation defined. You can read more in this excellent blog post: https://qsharp.community/blog/qsharp-entrypoint/.

Specifically, in your case, the error message indicates that Qubit[] is not a valid parameter to the main entry point of your program. Which makes sense, because it doesn't make sense to pass an array of qubits when executing a program from the command line. And also, your operation doesn't print anything or return any results, so you won't be able to see what it's doing.

You should probably create an @EntryPoint() wrapper operation that invokes your existing operation with the appropriate parameters, maybe prints some diagnostics, and then returns some result. In your case, you could perhaps do something like this (note the additional namespaces you need to open):

    open Microsoft.Quantum.Diagnostics;
    open Microsoft.Quantum.Measurement;

    @EntryPoint()
    operation SolveForTwoQubits() : Result[]
    {
        using (qubits = Qubit[2])
        {
            Solve(qubits);                 // invoke your existing Solve operation
            DumpMachine();                 // outputs the state of your qubits
            let results = MultiM(qubits);  // measure the qubits
            ResetAll(qubits);              // reset the qubits to the initial state
            return results;                // return the measured results
        }
    } 

This will give some output that looks like:

# wave function for qubits with ids (least to most significant): 0;1
∣0❭:     0.577350 +  0.000000 i  ==     *******              [ 0.333333 ]     --- [  0.00000 rad ]
∣1❭:     0.577350 +  0.000000 i  ==     *******              [ 0.333333 ]     --- [  0.00000 rad ]
∣2❭:     0.577350 +  0.000000 i  ==     *******              [ 0.333333 ]     --- [  0.00000 rad ]
∣3❭:     0.000000 +  0.000000 i  ==                          [ 0.000000 ]
[Zero,One]
Related