Mahathi Vempati
Mahathi Vempati

Reputation: 1398

How do you use an operation written in another file in Q#?

File A has

Operation Foo() : () {
   body{
      ...
   }
}

I want to use Foo in another operation in File B

Operation Bar() : (){
   // How to use Foo?
}

File A and B may not be in the same folder.

Upvotes: 1

Views: 42

Answers (1)

Alan Geller
Alan Geller

Reputation: 494

There are two parts to making this work: namespaces and project references.

All operations (and pretty much everything else) in Q# are in some namespace. Check the namespace directives at the tops of the two files; if the namespace names are the same, you're done with this part. If not, then in file B add an open directive at the top of the namespace that references the namespace for file A:

namespace A {
    open B;

If files A and B are in the same folder (same project, if using full Visual Studio), then that's all you need. If not, then you need to add a reference from project B to project A. In Visual Studio, you right-click on project B, choose Add, and then Reference..., click Project on the left of the dialog that comes up, and select project A. See https://learn.microsoft.com/en-us/visualstudio/ide/how-to-add-or-remove-references-by-using-the-reference-manager for more details.

If you're using Visual Studio Code, then use the dotnet add reference command to add a reference to project A from project B. See https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-add-reference.

Upvotes: 2

Related Questions