I have some utility operations that I'd like to use across projects. How can I import its namespace using Q# in Visual Studio 2017 in other projects?
1 Answer
First, you need to add the project which contains the utility operations as a reference to the project which will be using them. If you're using Visual Studio Code or command line, you can use dotnet add reference command to do that, and in Visual Studio you can use Reference Manager.
Second, you need to open the namespace which contains the utility operations in each Q# file which uses them. To do this, add an open
statement after the declaration of the namespace and before definition of any functions or operations:
namespace ProjectNamespace {
open Utilities;
// you can use operations defined in Utilities namespace now
}
-
$\begingroup$ Thanks so much, worked like a charm! Now that Q# 0.3 is out, I'd like to update my Utilities project with the new code, but allow some of my other projects to use the old version. I see the quantum development kit's version can be referenced like so ` <PackageReference Include="Microsoft.Quantum.Development.Kit" Version="0.3.1810.2508-preview" />`. I'm having trouble finding documentation on how I can do this with my custom namespace. Can you point me in the right direction? $\endgroup$ Oct 31, 2018 at 22:55
-
1$\begingroup$ QDK can be referenced by version because it's packaged as a NuGet package. I don't think you can do this if you're using references between projects in one solution. One thing you can do is package your old Utilities project as a NuGet package (locally, without pushing it to nuget.org) and use a reference to it instead of a direct project reference when you need a 0.2 version. $\endgroup$ Nov 1, 2018 at 18:11