## Q# Error: No namespace with the name "Microsoft.Quantum.MachineLearning" exists

2

I'm having trouble getting the namespace Microsoft.Quantum.MachineLearning.

Here is an example Q# code:

namespace Temp {
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.MachineLearning;

operation Hello () : Unit{
Message("Hello");
}
}


Here is the python script which drives the code:

import qsharp
from qsharp import Result
import json
import os
from Temp import Hello
Hello.simulate()


Here's the error that the VSCode terminal shows:

Preparing Q# environment...
fail: Microsoft.Quantum.IQSharp.Workspace[0]
QS6104: No namespace with the name "Microsoft.Quantum.MachineLearning" exists.
Traceback (most recent call last):
File "Driver.py", line 5, in <module>
from Temp import Hello
ModuleNotFoundError: No module named 'Temp'


Here are my component versions:

>>> qsharp.component_versions()
{'iqsharp': LooseVersion ('0.11.2004.2825'), 'Jupyter Core': LooseVersion ('1.3.52077.0'), '.NET Runtime': LooseVersion ('.NETCoreApp,Version=v3.1'), 'qsharp': LooseVersion ('0.11.2004.2825')}


I've tried adding the package manually into my conda environment:

>>> qsharp.packages.add("Microsoft.Quantum.MachineLearning::0.11.2004.2825")


But that doesn't help.

Also, it's worth to note that there is no such problem when I try to do the same in a local Jupyter Notebook which is weird since they run in the same environment.

2

You'll need to actually add these two lines into your Python script:

qsharp.packages.add("Microsoft.Quantum.MachineLearning::0.11.2004.2825")


This causes the packages to be loaded and available at runtime.

You need those two lines before you call from Temp import Hello, otherwise you will get the errors since you try to open the Microsoft.Quantum.MachineLearning namespace inside your Temp namespace.

Notice that you may still see an error message that says No namespace with the name "Microsoft.Quantum.MachineLearning" exists due to the behavior of import qsharp (see this issue for more details), but your code should then compile and run successfully anyway, in which case you can ignore that error message.

1This solves it, I tried adding those two lines in my script before but didn't regard the flow of program. Thank-you. But isn't there supposed to be a permanent solution to calling this package directly? I don't understand why there needs to be an explicit addition of this package for every script. – IE Irodov – 2020-06-17T14:18:00.093

It's not actually installing anything. It's just loading the package so that its contents are available. (Similar to an import statement in Python.) It's roughly the same behavior as in the Q# Jupyter notebook, where you would have to call %package for each package dependency each time you restart the kernel and re-execute the notebook. – Ryan Shaffer – 2020-06-17T14:31:22.263

1

In VS code, you have to manually add the package in the .csproj file under Item Group:

<PackageReference Include="Microsoft.Quantum.MachineLearning" Version="0.11.2006.403" />


or alternatively, you can also add it in the terminal:

dotnet add [project_name].csproj package Microsoft.Quantum.MachineLearning